in reply to bold color text and export to file

Dear Roboticus

Greatly appreciate your help. Well I tried your solution and it worked like charm. However, I am trying to do automatic array building but looks like I am not writing proper syntax.

I am trying to make the @highlights by reading a file having the position number and it doesn't seem to work, whereas if I manually put the positions in the code it the output is not as I am trying to get.

Below is my code and data:

#!/usr/bin/perl use strict; use warnings; use Term::ANSIColor; my $file = $ARGV[0]; if (@ARGV < 1){ print STDERR "Usage: $0 input_fasta_file\n"; exit 1; } my ($header, $sequence); open (A, "<", $file) or die "Check the file: $!"; while (my $line = <A>){ chomp $line; if ($line =~ /^(>.*)/){ $header = $1; } else{ $sequence .= $line; } } close (A); $sequence =~s/[\n\s]//; #print "$sequence\n"; =comment my @highlights = ( [ 88, 1, 'bold red' ], [ 101, 1, 'bold red' ], [ 113, 1, 'bold red' ], [ 121, 1, 'bold red' ], [ 124, 1, 'bold red' ], [ 134, 1, 'bold red' ], [ 140, 1, 'bold red' ], [ 146, 1, 'bold red' ], ); =cut my @highlights; my $pos_file = "sorted_position_walk.txt"; open (A, "<", $pos_file) or die "Check the file: $!"; while (my $line = <A>){ chomp $line; my $pos = (split /\t/,$line)[0]; # push(@position, $pos); push(@highlights, "([ $pos, 1, 'bold yellow' ],)"); } #foreach my $pp(@highlights){ # print "[ $pp, 1, 'bold red' ],\n"; #} #print "@highlights\n"; for my $ar (@highlights) { my ($start, $len, $color) = @$ar; $sequence = substr($sequence, 0, $start-1) # fi +rst part . colored(substr($sequence,$start,$len), $color) # colo +red part . substr($sequence,$start+$len); # fina +l part } print $sequence, "\n";
Output with some weird characters: TCTGTCCGCTCG3m1m[0mGGCAT

Regards

Replies are listed 'Best First'.
Re^2: bold color text and export to file
by roboticus (Chancellor) on Sep 22, 2014 at 20:26 UTC

    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.

      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.