CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
color font output html

 



newtoperlprog
Novice

Sep 22, 2014, 10:46 AM

Post #1 of 2 (2719 views)
color font output html Can't Post

Dear All

I am trying to make selected part of the the sequence file as red color for an html output.

For the sequence file, I am trying to color the font red for a part of sequence, leaving rest as default font color (black).


Code
#!/usr/bin/perl 
use strict;
use warnings;
my $sequence = '';
my $filename = "NM_014143.3.fasta";
my @name = split( /\./, $filename );
my $name = $name[0];
my $infile;
my $outfile;
my $out;
my $reject;
my @missing;
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 );
for ( my $pos = 0 ; $pos <= length($sequence) ; $pos++ ) {
foreach my $ran (@seq) {
my $frag = substr( $sequence, $pos, $ran );
print "<font color=\"red\">$frag</font>\n";
}
}


Code
Data file: >gi|292658763|ref|NM_014143.3| Homo sapiens CD274 molecule (CD274), transcript variant 1, mRNA GGCGCAACGCTGAGCAGCTGGCGCGTCCCGCGCGG 
CCCCAGTTCTGCGCAGCTTCCCGAGGCTCCGCACC
AGCCGCGCTTCTGTCCGCCTGCAGGGCATTCCAGAA
AGATGAGGATATTTGCTGTCTTTATATTCATGAC CTAC
TGGCATTTGCTGAACGCATTTACTGTCACGGT
TCCCAAGGACCTATATGTGGTAGAGTATGGTAGC
AATATGACAATTGAATGCAAATTCCCAGTAGAAAAA
CAATTAGACCTGGCTGCACTAATTGTCTATTGGG
AAATGGAGGATAAGAACATTATTCAATTTGTGCAT
GGAGAGGAAGACCTGAAGGTTCAGCATAGTAGCTA
CAGACAGAGGGCCCGGCTGTTGAAGGACCAGCTCT
CCCTGGGAAATGCTGCACTTCAGATCACAGATGTG
AAATTGCAGGATGCAGGGGTGTACCGCTGCATGATC
AGCTATGGTGGTGCCGACTACAAGCGAATTACTG
TGAAAGTCAATGCCCCATACAACAAAATCAACCAA
AGAATTTTGGTTGTGGATCCAGTCACCTCTGAACA
TGAACTGACATGTCAGGCTGAGGGCTACCCCAAGG
CCGAAGTCATCTGGACAAGCAGTGACCATCAAGTC
CTGAGTGGTAAGACCACCACCACCAATTCCAAGAG
AGAGGAGAAGCTTTTCAATGTGACCAGCACACTGA
GAATCAACACAACAACTAATGAGATTTTCTACTGCA
CTTTTAGGAGATTAGATCCTGAGGAAAACCATAC


The problem I am facing is how to print the sequence with selected regions as red font color.

Code
Desired output: 
<font color="red">GGCGCAACGCTGAGC</font>AGCTGGCGCGTCCCG<font color="red">CGCGGCCCCA</font>GTTCTGCGCA<font color="red">GCTTCCCGAG</font>GCTCCGCACC
AGCCGCGCTTCTGTCCGCCTGCAGGGCATTCCAGAAAGATGAGGATATTTGCTGTCTTTATATTCATGAC
CTACTGGCATTTGCTGAACGCATTTACTGTCACGGTTCCCAAGGACCTATATGTGGTAGAGTATGGTAGC
AATATGACAATTGAATGCAAATTCCCAGTAGAAAAACAATTAGACCTGGCTGCACTAATTGTCTATTGGG
AAATGGAGGATAAGAACATTATTCAATTTGTGCATGGAGAGGAAGACCTGAAGGTTCAGCATAGTAGCTA
CAGACAGAGGGCCCGGCTGTTGAAGGACCAGCTCTCCCTGGGAAATGCTGCACTTCAGATCACAGATGTG
AAATTGCAGGATGCAGGGGTGTACCGCTGCATGATCAGCTATGGTGGTGCCGACTACAAGCGAATTACTG
TGAAAGTCAATGCCCCATACAACAAAATCAACCAAAGAATTTTGGTTGTGGATCCAGTCACCTCTGAACA
TGAACTGACATGTCAGGCTGAGGGCTACCCCAAGGCCGAAGTCATCTGGACAAGCAGTGACCATCAAGTC
CTGAGTGGTAAGACCACCACCACCAATTCCAAGAGAGAGGAGAAGCTTTTCAATGTGACCAGCACACTGA
GAATCAACACAACAACTAATGAGATTTTCTACTGCACTTTTAGGAGATTAGATCCTGAGGAAAACCATAC


Thank you for help.
Regards


(This post was edited by FishMonger on Sep 23, 2014, 6:59 AM)


FishMonger
Veteran / Moderator

Sep 23, 2014, 7:04 AM

Post #2 of 2 (2700 views)
Re: [newtoperlprog] color font output html [In reply to] Can't Post

Please use proper indentation and line spacing in your code so that anyone can (including yourself) can easily read and follow what it's doing.

I went ahead and used perltidy to cleanup the code in your post. http://search.cpan.org/~shancock/Perl-Tidy-20140711/bin/perltidy

There are a couple different approaches you can use to add the outdated/depreciated html font tags. Here's a regex approach.


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $sequence = '';
my $filename = "NM_014143.3.fasta";
my @name = split( /\./, $filename );
my $name = $name[0];
my $infile;
my $outfile;
my $out;
my $reject;
my @missing;

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; }
}
close($infile);
$sequence =~ s/\s+//g;

$sequence =~ s~^(.{15})
(.{15})
(.{10})
(.{10})
(.{10})
~<font color="red">$1</font>$2<font color="red">$3</font>$4<font color="red">$5</font>~x;
print $sequence;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives