in reply to Print N characters per line in Cgi Script with Html Tags
This counts along the string and inserts line breaks at regular intervals
poj#!/usr/bin/perl use strict; use warnings; use CGI; my $WIDTH = 40; my %hash = ( 'name1' => "ATATTATCCCCCTATATATGGAGGGAGAGGGGGGGGGGGGGGGGGGGGGGGGGGGAG +AGAGGAGATTTTTTTTTTTTTTTT", 'name2' => "ATATATTATATATATTATATTCGCGCGCGCGGCGCGCGCGGCGCGCGCGTTTTTTTT +TTTTTTAGGAGAGAGAGGGAGGAGGAGAGGGGAGT", ); my @select=(); my %pattern = ( 'AGGAG' => 'sd', 'TTTTTTT' => 'terminator' ); my $re = join '|',keys %pattern; # add markup foreach my $key (sort keys %hash) { if ($hash{$key} =~ s!($re)!<span class="$pattern{$1}">$1</span>!g){ push @select,$key; } } # create table my $table = q!<table width="100" cellpadding="5" cellspacing="5"> <tr style="font-style:italic"> <td width="20%">Key</td> <td width="5%"> </td> <td width="75%">Sequence</td> </tr>!; # selected keys foreach my $key (@select) { my $seq = $hash{$key}; # count ACTG character to determine # position to insert line breaks my $count = 0; my @br; for my $p (0..length($seq)-1){ ++$count if substr($seq,$p,1) =~ /[ACTG]/; if ($count > $WIDTH){ push @br,$p; $count = 1; }; } # insert line breaks working backwards for my $p (reverse @br){ substr($seq,$p,0,'<br/>'); } # create row my $radio = qq!<input type="radio" name="selected_intergenic" value= +"$key"/>!; $table .= qq!<tr valign="top"> <td>$key</td> <td>$radio</td> <td>$seq</td></tr>!; } $table .= q!</table>!; # html page my $q = CGI->new; my $style = <<EOF; body { background: lightgrey; color: black; font-family: Courier; margin:20px; } h1 { color: black; font-family: Verdana; font-size: 150%; } h2 { color:red; font-family: Courier; } .sd { color:red; } .terminator { color: royalblue; } EOF my $note = qq!Sequences with <span class="sd"> SD sequence</span> or <span class="terminator">terminator structure</span>!; print $q->header, $q->start_html(-style=>{'code'=>$style}), $q->h1( $note ), $table, $q->end_html;
|
|---|