genbank2image_SOLUTION.pl
$inFile = "seqs.gb";
use Bio::SeqIO;
use GD;
$in = Bio::SeqIO->new('-file' => "$inFile", '-format' => 'Genbank');
$ntWidth = 15;
$ntPerLine = 50;
while ($seqobj = $in->next_seq())
{
readSequence();
setupImage();
@nt = split(//, $rawseq);
for ($i = 0; $i <= $
{
$thisNT = $nt[$i];
$x1 += $ntWidth;
$x2 = $x1 + $ntWidth;
$y2 = $y1 + $ntWidth;
$img->filledRectangle($x1, $y1, $x2, $y2, $ntToColor{$thisNT});
$img->string(gdGiantFont, $x1 + 3, $y1, $thisNT, $black);
if (($i + 1) % $ntPerLine == 0)
{
$y1 += 2 * $ntWidth;
$x1 = 25;
$img->string(gdTinyFont, 5, $y1 + 4, $i + 2, $black);
}
}
open(OUT_IMG, ">$imagefile") || die "Cannot write to $imagefile: $!\n";
print OUT_IMG $img->png;
close OUT_IMG;
printSeqs();
}
$imagefileList = join (" ", @imagefiles);
print "\nFor images, see\n$imagefileList\n";
sub readSequence
{
$rawseq = $seqobj->seq();
$seqLength = length($rawseq);
$id = $seqobj->display_id();
parseSeqFeatures();
$title = "$id ($seqLength nt, from $inFile)";
$title .= " CDS = $cdsStart - $cdsEnd";
}
sub setupImage
{
$imagefile = $id . ".png";
push (@imagefiles, $imagefile);
$imageHeight = $seqLength / $ntPerLine * $ntWidth * 2 + 80;
$imageWidth = $ntPerLine * $ntWidth + 100;
$img = new GD::Image($imageWidth, $imageHeight);
defineColors();
defineNtColors();
$x1 = 25; $y1 = 40;
$img->string(gdGiantFont, 45, 5, $title, $black);
$img->string(gdTinyFont, 5, $y1 + 4, "1", $black);
}
sub defineColors
{
$white = $img->colorAllocate(255,255,255);
$black = $img->colorAllocate(0,0,0);
$green = $img->colorAllocate(0, 255, 0);
$blue = $img->colorAllocate(0, 0, 255);
$red = $img->colorAllocate(255, 0, 0);
$yellow = $img->colorAllocate(255, 255, 0);
}
sub defineNtColors
{
$ntToColor{"A"} = $red;
$ntToColor{"G"} = $green;
$ntToColor{"C"} = $blue;
$ntToColor{"T"} = $yellow;
}
sub parseSeqFeatures
{
foreach $feat ($seqobj->all_SeqFeatures())
{
$featName = $feat->primary_tag;
if ($featName eq "CDS")
{
$cdsStart = $feat->start;
$cdsEnd = $feat->end;
}
foreach $tag ($feat->all_tags())
{
if ($tag eq "translation")
{
@protein = $feat->each_tag_value("translation");
$protein = join ("", @protein);
$protId = "$id";
$protSeqObj = Bio::PrimarySeq->new('-seq' => $protein, '-format' => 'raw',
-id => $protId, -type=> 'protein');
}
}
}
}
sub printSeqs
{
$outDna = Bio::SeqIO->new('-format' => 'Fasta');
$outDna->write_seq($seqobj);
$outProt = Bio::SeqIO->new('-format' => 'Fasta');
$outProt->write_seq($protSeqObj);
}