in reply to Re: bold color text and export to file
in thread bold color text and export to file

newtoperlprog:

When you use the color editing code, it inserts characters into the string. That shifts all the character positions to the right. That's why I suggested you always go from the right and work left in the note at the end. That's the reason that you're seeing weird characters in the text. Those weird characters are parts the terminal commands that tell the terminal to change colors.

For example, suppose you wanted to highlight the string CAT every time it appears, and also that the terminal command for changing the color to normal is BEER and the command to change the color to highlight is KETCHUP. Then if your input string looks like this:

ATCGCGATCATCCATACTCATTAG

Then the positions you're wanting to highlight are at 8, 12 and 18. If we apply the edits from left to right we get:

ATCGCGATBEERCATKETCHUPCCATACTCATTAG edit at 8 ATCGCGATBEEBEERRCAKETCHUPTKETCHUPCCATACTCATTAG then 12 ATCGCGATBEEBEERRCABEERKETKETCHUPCHUPTKETCHUPCCATACTCATTAG then 18 ???^^^^? ^^^^???vvvvvvv?????vvvvvvv

UGH! The string has garbage in it now. To point it out, I've marked the resulting string with ^^^^ to indicate the command to switch to bold, vvvvvvv to show the command that switches back to normal, and ? for any garbage characters. So we'd see BEE in the regular color, followed by RCAKET in bold, then CHUPTCCATACTCATTAG in regular color. However, if we do the edits from right to left, though, we get:

ATCGCGATCATCCATACTBEERCATKETCHUPTAG edit at 18 ATCGCGATCATCBEERCATKETCHUPACTBEERCATKETCHUPTAG then 12 ATCGCGATBEERCATKETCHUPCBEERCATKETCHUPACTBEERCATKETCHUPTAG then 8 ^^^^ vvvvvvv ^^^^ vvvvvvv ^^^^ vvvvvvv

So the three "CAT" sequences are in bold, and the rest of the string is displayed in regular color.

To convert your data to use HTML formatting instead, you can use roughly the same code, but instead of inserting BEER and KETCHUP from Term::ANSIColor (or whatever it is that it uses for your terminal), you could use <font color="red"> and </font> as Anonymous Monk indicated later in the thread. In that case, though, you'll *still* want to go from right to left!

Notes:

1) Now that the node is formatted, I'm really revolted by the pairing of beer and ketchup. But it was annoying enough to format that I'll leave it alone, nausea-inducing as it is.

2) If you had replied to my node instead of yourself, I'd've seen the message sooner and replied. (When time pressed, I generally only look at top-level nodes and replies to my nodes.)

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^3: bold color text and export to file
by newtoperlprog (Sexton) on Sep 23, 2014 at 17:42 UTC

    Dear Roboticus and Anonymous Monk

    Thank you for your suggestions and directions and I am extremely sorry for any confusion if any.

    As you mentioned in your earlier post, that this will not work if the sequences are overlapping and I have found that some of my regions would be overlapping.

    I was also thinking if this could be done by an array of range operator and for each array element the sequence will be either bold or colored.

    Below is my code which I was trying:

    #!/usr/bin/perl #use strict; use warnings; my $filename = "NM_014143.3.fasta"; my @name = split( /\./, $filename ); my $name = $name[0]; my ($infile, @temp); open( $infile, "<", $filename ) || die "Check the $filename $!\n"; while ( my $line = <$infile> ) { chomp $line; if ( $line =~ /^>/ ) { next; } elsif ( $line =~ /^\s*$/ ) { next; } elsif ( $line =~ /^\s*#/ ) { next; } else { $sequence .= $line; } } $sequence =~ s/\n//g; $sequence =~ s/\s+//g; #print "$sequence\n"; close ($infile); my @seq = (1 .. 15, 30 .. 40, 50 .. 60); #this is a range array for (my $pos=1;$pos<=length($sequence);$pos++){ foreach my $ar(@seq){ if($pos == $ar){ push (@temp, "<b>",$sequence, "</b"); } else{ push (@temp, $sequence); } } } my $tmp = join ("", @temp); print "$tmp\n";
    Data file: GGCGCAACGCTGAGCAGCTGGCGCGTCCCGCGCGGCCCCAGTTCTGCGCAGCTTCCCGAGGCTCCGCACC + CC +TGCAGGGCATTCCAGAAAGATGAGGATATTTGCTGTCTTTATATTCATGACCATTTGCTGAACGCATT TACTGTCACGGTTCCCAAGGACCTATATGTGGTAGAGTATGGTAGC + AT +GACAATTGAATGCAAATTCCCAGTAGAAAAACAATTAGACCTGGCTGCACTAATTGTCTATTGGG AAATGGAGGATAAGAACATTATTCAATTTGTGCATGGAGAGGAAGACCTGAAGGTTCAGCATAGTAGCTA CAGACAGAGGGCCCGGCTGTTGAAGGACCAGCTCTCCCTGGGAAATGCTGCACTTCAGATCACAGATGTG AAATTGCAGGATGCAGGGGTGTACCGCTGCATGATCAGCTATGGTGGTGCCGACTACAAGCGAATTACTG TGAAAGTCAATGCCCCATACAACAAAATCAACCAAAGAATTTTGGTTGTGGATCCAGTCACCTCTGAACA TGAACTGACATGTCAGGCTGAGGGCTACCCCAAGGCCGAAGTCATCTGGACAAGCAGTGACCATCAAGTC CTGAGTGGTAAGACCACCACCACCAATTCCAAGAGAGAGGAGAAGCTTTTCAATGTGACCAGCACACTGA

      newtoperlprog:

      If your ranges overlap, there are several strategies you can use. If the overlapping regions are supposed to be highlighted in a single color, then you can simply merge the overlapped regions before applying the edits. If you want to highlight the overlapping region in a different style, then you need to break the ranges up into the non-overlapping and overlapping pieces. Once you've preprocessed your list so that there are no overlaps remaining, you can then apply your edits.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        Dear Roboticus

        Thank you for your suggestion. The ranges don't overlap and they are discreet from each other.

        The problem with the code is its not able to demarcate the start and stop of the range (example: 1 till 15 and so on) to which the html tags can be applied. I can understand that my logic in the loop is incorrect and am trying to learn to fix this problem.

        Any help will be greatly appreciated