blib/lib/Bio/Align/Graphics.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 #Author: William McCaig
2 #Date: 06/16/2006
3 #Purpose: To print visual images of alignments
4 #
5 #Requires: An alignment file
6 #
7 #Produces: An image file
8 #
9 #Revision History:
10 #09/01/2006 - WDM - Introduction of "wrap" flag, allowing alignment to be
11 # wrapped at a set base and stacked vertically
12 # Addition of internal members y_num and y_size for tracking
13 # of number of vertical panels and size of panels,
14 # respectively
15 #
16 #09/06/2006 - WDM - Introduction of "p_legend" flag, for printing of an optional
17 # colored legend when protein coloring is selected
18 #
19 #09/24/2008 - WDM - Test file created for the module
20 #
21 #03/01/2009 - YH - Introduction of "show_nonsynonymous" flag which enables
22 # highlighting of nonsynonymous mutations in nucleotide
23 # alignments. Addition of internal members codon_table and
24 # missense_pos for translating codons -> amino acids and for
25 # keeping track of missense mutation positions respectively.
26 #
27 #03/05/2009 - YH - Swapped names of subroutines x_label and y_label to match
28 # both documentation and intuition. Finalized implementation
29 # of show_nonsynonymous functionality.
30  
31 # docs after the code!
32  
33 package Bio::Align::Graphics;
34 $Bio::Align::Graphics::VERSION = '1.7.3';
35 1 ▶use vars qw( @PRINT_PARAMS %OK_FIELD);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 1 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 0 ▶use GD;
Subroutine BEGIN: called CC=1 SCAR=2.6
42 use GD::Simple;
43 use Bio::AlignIO;
44 use Data::Dumper;
45 use POSIX qw(ceil floor);
46  
47 require Exporter;
48  
49 our @ISA = qw(Exporter);
50  
51 # Items to export into callers namespace by default. Note: do not export
52 # names by default without a very good reason. Use EXPORT_OK instead.
53 # Do not simply export all your public functions/methods/constants.
54  
55 # This allows declaration use PrintAlignment ':all';
56 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
57 # will save memory.
58 our %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
59  
60 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
61  
62 our @EXPORT = qw( );
63  
64 # Preloaded methods go here.
65 our %FONT_TABLE = (1 => gdTinyFont, 2 => gdSmallFont, 3 => gdMediumBoldFont, 4 => gdLargeFont, 5 => gdGiantFont );
66 our %PROTEIN_COLORS = ('Q' => [255, 0, 204], 'E' => [255, 0, 102], 'D' => [255, 0, 0] , 'S' => [255, 51, 0] , 'T' => [255, 102, 0 ],
67 'G' => [255, 153, 0] , 'P' => [255, 204, 0] , 'C' => [255, 255, 0] , 'A' => [204, 255, 0] , 'V' => [153, 255, 0],
68 'I' => [102, 255, 0] , 'L' => [51 , 255, 0] , 'M' => [0, 255, 0] , 'F' => [0 , 255, 102] , 'Y' => [0 , 255, 204],
69 'W' => [0, 204, 255] , 'H' => [0, 102, 255] , 'R' => [0, 0, 255] , 'K' => [102, 0, 255] , 'N' => [204, 0, 255] );
70 #################################################################
71 #New
72 sub new {
73 my $class = shift;
74 my %options = @_;
75  
76 my $self = {
77
78 #####OPTIONS#####
79 #Display Defaults
80 font => defined($options{font}) ? $FONT_TABLE{$options{font}} : $FONT_TABLE{2},
81 x_label => defined($options{x_label}) ? $options{x_label} : 1,
82 y_label => defined($options{y_label}) ? $options{y_label} : 1,
83
84 #Colors
85 bg_color => $options{bg_color} || 'white',
86 fg_color => $options{font_color} || 'black',
87 x_label_color => $options{x_label_color} || 'blue',
88 y_label_color => $options{y_label_color} || 'red',
89 p_color => $options{p_color} || undef,
90 p_legend => $options{p_legend} || undef,
91 p_color_table => undef,
92
93 #Sequence Defaults
94 reference => $options{reference} || undef,
95 reference_id => $options{reference_id} || undef,
96 match_char => $options{match_char} || ".",
97 block_size => defined($options{block_size}) ? $options{block_size} : 10,
98 block_space => defined ($options{block_space}) ? ($options{block_space} * ($options{font} ? $FONT_TABLE{$options{font}}->width : $FONT_TABLE{2}->width)) : ( ($options{font} ? ($FONT_TABLE{$options{font}}->width * 2 ) : ($FONT_TABLE{2}->width * 2)) ),
99 wrap => $options{wrap} || 80,
100 show_nonsynonymous => $options{show_nonsynonymous} || undef, # If turned on, will highlight nonsynonymous (missense) mutations. Valid only for nucleotide alignments
101
102 #Padding
103 pad_left => $options{pad_left} || 5, #space between x label and border
104 pad_right => $options{pad_right} || 5, #space between end of sequences and border
105 pad_top => $options{pad_top} || 5, #space between y label and border
106 pad_bottom => $options{pad_bottom} || 5, #space between bottom of sequences and border
107 x_label_space => $options{x_label_space} || 1, #space between x label and sequences
108 y_label_space => $options{y_label_space} || 1, #space between y label and sequences
109
110 #Labels
111 labels => $options{labels} || undef,
112 dm_labels => $options{dm_labels} || undef,
113 dm_label_start => $options{dml_start} || undef,
114 dm_label_end => $options{dml_end} || undef,
115 dm_label_color => $options{dml_color} || undef,
116 domain_start => $options{dm_start} || undef,
117 domain_end => $options{dm_end} || undef,
118 domain_color => $options{dm_color} || undef,
119
120 #File Defaults
121 align => $options{align} || undef,
122 output => $options{output} || undef,
123 out_format => $options{out_format} || undef,
124
125 ####PRIVATE VALUES#####
126
127 image => $options{image} || undef,
128 seq_format => undef,
129
130 #X and Y size of char
131 x_char_size => ($options{font} ? $FONT_TABLE{$options{font}}->width : $FONT_TABLE{2}->width),
132 y_char_size => ($options{font} ? $FONT_TABLE{$options{font}}->height : $FONT_TABLE{2}->height),
133
134 #Image W & H
135 width => undef, #overall width of the image
136 height => undef, #overall height of image
137
138 #Sequences
139 sequences => undef,
140 seq_ids => undef,
141 ref_sequence => undef,
142 id_length => 0,
143 seq_length => $options{align}->length() || 0,
144 no_sequences => $options{align}->num_sequences() || 0,
145 seq_start_x => undef,
146 seq_start_y => undef,
147 start => $options{start} || 1,
148 end => $options{end} || $options{align}->length(),
149 y_num => undef,
150 y_size => undef,
151 footer_size => 110,
152 footer_start => undef
153
154 };
155  
156 bless ($self, $class);
157  
158 die "new:Must supply alignment for drawing!\n"
159 unless defined ($self->{align});
160  
161  
162 foreach my $seq ($self->{align}->each_seq)
163 {
164 $self->{id_length} = ( length($seq->id()) > $self->{id_length} ) ? length($seq->id()) : $self->{id_length};
165  
166
167
168 if( $self->{reference_id} && ($seq->id() eq $self->{reference_id}) )
169 {
170 @{$self->{ref_sequence}} = split //, $seq->seq;
171 unshift @{$self->{sequences}}, $seq->seq;
172 unshift @{$self->{seq_ids}}, $seq->id();
173 }else
174 {
175 push @{$self->{sequences}}, $seq->seq;
176 push @{$self->{seq_ids}}, $seq->id();
177 }
178
179 if(!defined($self->{seq_format}))
180 {
181 $self->{seq_format} = $seq->alphabet;
182 }
183 }
184  
185 if(!($self->{reference_id}) )
186 {
187 @{$self->{ref_sequence}} = split //, ${$self->{sequences}}[0];
188 $self->{reference_id} = ${$self->{seq_ids}}[0];
189 }
190  
191 $self->{y_num} = ($self->{seq_length} > $self->{wrap}) ? ( sprintf( "%.0f", ( ($self->{seq_length} / $self->{wrap}) + .5) ) ) : 1;
192 $self->{y_size} = ( ($self->{no_sequences} + $self->{pad_bottom}) * $self->{y_char_size});
193 $self->{seq_start_x} = ($self->{pad_left} + $self->{id_length} + $self->{x_label_space}) * $self->{x_char_size};
194  
195 if( defined($self->{show_nonsynonymous}) ) # Extra column changes dimensions
196 {
197 $self->{seq_length_aa} = ($self->{seq_length} / 3) + $self->{seq_length}; # Consider length of sequence plus extra column every 3 nucleotides
198 $self->{seq_start_y} = ($self->{pad_top} + length($self->{seq_length_aa}) + $self->{y_label_space}) * $self->{y_char_size};
199 $self->{width} = $self->{seq_start_x} + ((( $self->{wrap} / $self->{block_size}) + 1) * $self->{block_space}) + ( ($self->{wrap} + $self->{pad_right}) * ($self->{x_char_size} + 1.2) ) + ( ($self->{seq_length} / 3) * 2); # Needed to add this for width to fit whole sequence on one line
200 }else
201 {
202 $self->{seq_start_y} = ($self->{pad_top} + length($self->{seq_length}) + $self->{y_label_space}) * $self->{y_char_size};
203 $self->{width} = $self->{seq_start_x} + ((( $self->{wrap} / $self->{block_size}) + 1) * $self->{block_space}) + ($self->{wrap} + $self->{pad_right}) * $self->{x_char_size};
204 }
205  
206 $self->{footer_start} = $self->{seq_start_y} + $self->{y_size} * $self->{y_num};
207  
208 if(defined($self->{p_color}) && defined($self->{p_legend}) && $self->{p_legend}){
209 $self->{height} = $self->{seq_start_y} + $self->{footer_size} + $self->{y_size} * $self->{y_num};
210 }else{
211 $self->{height} = $self->{seq_start_y} + $self->{y_size} * $self->{y_num};
212 }
213 $self->{image} = GD::Simple->new($self->{width},$self->{height});
214 $self->{image}->alphaBlending(1);
215 $self->{image}->saveAlpha(1);
216 $self->{image}->bgcolor($self->{bg_color});
217 $self->{image}->fgcolor($self->{fg_color});
218 $self->{image}->rectangle(0,0,$self->{width}-1, $self->{height} - 1);
219 return $self;
220  
221 } #End new Subroutine#########################################################
222  
223  
224 sub draw{
225 my $self = shift;
226  
227 die "draw:Must supply alignment for drawing!\n"
228 unless defined ($self->{align});
229  
230 if(defined($self->{x_label}) && $self->{x_label})
231 {
232 $self->x_label();
233 }
234  
235 if(defined($self->{y_label}) && $self->{y_label})
236 {
237 $self->y_label();
238 }
239  
240  
241 if(defined($self->{domain_start}) && defined($self->{domain_end}) && not defined($self->{p_color}) )
242 {
243 $self->_draw_domain();
244 }
245  
246 #
247 if( defined($self->{show_nonsynonymous}) && ( $self->{seq_format} eq "protein" ) )
248 {
249 die "draw:Option show_nonsynonymous only works with Nucleotide alignments!\n";
250 }elsif ( defined($self->{show_nonsynonymous}) )
251 {
252 $self->{codon_table} = Bio::Tools::CodonTable->new();
253 $self->{missense_pos} = {};
254 # print STDERR "You are using option show_nonsynonymous. Option works best if wrap value is a multiple of 4.\n"
255 }
256  
257 if(defined($self->{p_color}) && $self->{seq_format} eq "protein")
258 {
259 $self->_draw_colored_sequences();
260 if(defined($self->{p_legend}) && $self->{p_legend})
261 {
262 $self->_draw_legend();
263 }
264 }elsif(defined($self->{p_color}) && ($self->{seq_format} ne "protein"))
265 {
266 die "draw:Option p_color only works with Protein alignments!\n";
267 }else
268 {
269 $self->_draw_sequences();
270 }
271  
272 if(defined($self->{dm_label_start}))
273 {
274 $self->_domain_label();
275 }
276  
277  
278
279 if($self->{output})
280 {
281 open my $OUTPUT, '>', $self->{output} or die "Could not read file '$self->{output}': $!\n";
282 binmode $OUTPUT;
283
284 if(defined($self->{out_format}))
285 {
286 SWITCH: {
287 if($self->{out_format} eq "png") {print $OUTPUT $self->{image}->png; last SWITCH;}
288 if($self->{out_format} eq "jpeg") {print $OUTPUT $self->{image}->jpeg; last SWITCH;}
289 if($self->{out_format} eq "gif") {print $OUTPUT $self->{image}->gif; last SWITCH;}
290 if($self->{out_format} eq "gd") {print $OUTPUT $self->{image}->gd; last SWITCH;}
291 }
292  
293 }else
294 {
295 print $OUTPUT $self->{image}->png;
296 }
297
298 close $OUTPUT;
299 }else
300 {
301 binmode STDOUT;
302
303 if(defined($self->{out_format}))
304 {
305 SWITCH: {
306 if($self->{out_format} eq "png") {print STDOUT $self->{image}->png; last SWITCH;}
307 if($self->{out_format} eq "jpeg") {print STDOUT $self->{image}->jpeg; last SWITCH;}
308 if($self->{out_format} eq "gif") {print STDOUT $self->{image}->gif; last SWITCH;}
309 if($self->{out_format} eq "gd") {print STDOUT $self->{image}->gd; last SWITCH;}
310 }
311  
312 }else
313 {
314 print STDOUT $self->{image}->png;
315 }
316
317
318 }#End Output if/else
319  
320  
321  
322  
323 #print "Left\tRight\tTop\tBottom\n";
324 #print $self->{pad_left}, "\t", $self->{pad_right}, "\t", $self->{pad_top}, "\t", $self->{pad_bottom}, "\n";
325  
326 };
327  
328 ##########################################
329 #Draws Sequences
330 sub _draw_sequences{
331 my $self = shift;
332  
333 my $block_num = 0;
334 my $block_total = 0;
335 my $print_char;
336  
337  
338 $self->{image}->fgcolor($self->{fg_color});
339  
340 for (my $i=0; $i < $self->{no_sequences}; $i++)
341 {
342
343 my @letters = split //, ${$self->{sequences}}[$i];
344
345
346
347 my $y_num = $self->{y_num}; #sprintf( "%.0f", ( ($self->{seq_length} / $self->{wrap}) + .5) ) - 1;
348 my $y_char = $self->{y_size}; #( ($self->{no_sequences} + $self->{pad_bottom}) * $self->{y_char_size});
349
350 for(my $k=0; $k<=$y_num; $k++)
351 {
352 my $x_char = $k * $self->{wrap};
353
354 for (my $j=$x_char; $j <= ( ($x_char + $self->{wrap}) - 1); $j++)
355 {
356 last unless defined($letters[$j]);
357
358
359 # If show_nonsynonymous is on, and this is the 3rd nucleotide,
360 # save the codon and amino acid for comparison
361 my ($codon, $aa);
362 if ((defined($self->{show_nonsynonymous})) && ((($j+1) % 3) == 0))
363 {
364 $codon = $letters[$j-2] . $letters[$j-1] . $letters[$j];
365 $aa = $self->{codon_table}->translate($codon);
366 }
367
368 if( $self->{reference} )
369 {
370 if(${$self->{seq_ids}}[$i] eq $self->{reference_id})
371 {
372 $print_char = $letters[$j];
373 }else
374 {
375 if($letters[$j] eq ${$self->{ref_sequence}}[$j])
376 {
377 $print_char = $self->{match_char};
378 }else
379 {
380 $print_char = $letters[$j];
381 }
382 }
383 }else
384 {
385 $print_char = $letters[$j];
386 }
387
388 if( ( ($j + 1) % ($self->{block_size})) == 0)
389 {
390 $block_num = $self->{block_space};
391 }else
392 {
393 $block_num = 0;
394 }
395
396 #print "J is: $j\n";
397 #print "Char is: $print_char\n";
398 my $new_x_pos = $self->{seq_start_x} + ( ($j - $x_char) * $self->{x_char_size}) + $block_total;
399 my $new_y_pos = $self->{seq_start_y} + ($i * $self->{y_char_size}) + ($k * $y_char);
400
401 $new_x_pos += ( ( floor( ($j-$x_char)/3 ) * $self->{x_char_size} ) +
402 ( ( floor( ($j-$x_char)/3 ) ) * 6 ))
403 if ( defined($self->{show_nonsynonymous}) );
404
405 $self->{image}->moveTo( $new_x_pos, $new_y_pos );
406 $self->{image}->font($self->{font});
407 $self->{image}->string($print_char);
408
409 if ( (defined($self->{show_nonsynonymous})) && ((($j+1) % 3) == 0) )
410 {
411 $new_x_pos += ($self->{x_char_size} + 3);
412 $self->{image}->moveTo( $new_x_pos, $new_y_pos );
413
414 # If show_nonsynonymous is on, and this is the 3rd nucleotide
415 # on reference, print the amino acid after the nucleotide
416 if(($self->{reference}) && (${$self->{seq_ids}}[$i] eq $self->{reference_id}))
417 {
418 $self->{image}->font(gdMediumBoldFont);
419 $self->{image}->string($aa);
420 $self->{image}->font($self->{font});
421 }elsif ( ( $self->{reference} ) && ( ${$self->{seq_ids}}[$i] ne $self->{reference_id} ) )
422 { # In case current sequence is not reference
423 my $ref_codon = ${$self->{ref_sequence}}[$j-2] .
424 ${$self->{ref_sequence}}[$j-1] .
425 ${$self->{ref_sequence}}[$j];
426 my $ref_aa = $self->{codon_table}->translate($ref_codon);
427
428 if ( $ref_aa eq $aa ) # Synonymous mutation
429 {
430 $self->{image}->string($self->{match_char});
431 }else # Nonsynonymous mutation
432 {
433 $self->{image}->font(gdMediumBoldFont);
434 $self->{image}->string($aa);
435 $self->{image}->font($self->{font});
436
437 # Highlight nonsynonymous mutations by drawing a rectangle around them
438 if ( ( ${$self->{seq_ids}}[$i] ne $self->{reference_id} ) && !( ${$self->{missense_pos}}{$j} ) )
439 {
440 ${$self->{missense_pos}}{$j} = 1;
441 $self->{image}->bgcolor(undef);
442 $self->{image}->rectangle( $new_x_pos - 2, ( $new_y_pos - ( ( $self->{y_char_size} * ($i+1)) ) ) - 2, ( $new_x_pos + ( $self->{x_char_size} + 1) ), ( $new_y_pos + ( $self->{y_char_size} * ( $self->{no_sequences} - ( $i+1 ) ) ) ) + 2);
443 $self->{image}->bgcolor($self->{bg_color});
444 }
445 }
446 }else # No reference sequence defined
447 {
448 $self->{image}->string($aa);
449 }
450
451 }
452
453 if( defined($self->{labels}) && $i == ($self->{no_sequences} - 1))
454 {
455
456 if(${$self->{labels}}{$j + 1})
457 {
458 my $label = ${$self->{labels}}{$j + 1};
459 my $offset = defined($self->{dm_label_start}) ? 3 : 0;
460 $self->{image}->moveTo($self->{seq_start_x} + ( ( ($j - $x_char) + 1.25) * $self->{x_char_size}) + $block_total, $self->{seq_start_y} + (($self->{no_sequences}) * $self->{y_char_size}) + ($k * $y_char) + ( (length($label) + $offset) * ($self->{x_char_size}) ) );
461 $self->{image}->font($self->{font});
462 $self->{image}->angle(-90);
463 $self->{image}->string($label);
464 $self->{image}->angle(0);
465 }
466 }
467
468
469 $block_total += $block_num;
470 }
471 $block_total = 0;
472 }
473  
474 }
475  
476  
477 }
478  
479 # WARNING YH - This function has not been modified to work with show_nonsynonymous: needs test data to make sure it will work!
480 ##############################################
481 #Draw Domain Label
482 sub _domain_label{
483 my $self = shift;
484 my $start_block_total = 0;
485 my $end_block_total = 0;
486 my $wrap_block_total = 0;
487  
488 my $y_char = $self->{y_size};# ( ($self->{no_sequences} + $self->{pad_bottom}) * $self->{y_char_size});
489  
490 for(my $i = 0; $i <= $#{$self->{dm_label_start}}; $i++)
491 {
492
493 my $start = ${$self->{dm_label_start}}[$i];
494 my $end = ${$self->{dm_label_end}}[$i];
495
496 my $y_num_start = int( $start / $self->{wrap});
497 my $y_num_end = int( $end / $self->{wrap});
498
499 my $x_num_start;
500
501 if($start >= $self->{wrap})
502 {
503 $x_num_start = ($start % $self->{wrap}) - 1;
504 }else
505 {
506 $x_num_start = $start - 1;
507 }
508
509 my $x_num_end;
510
511 if($end >= $self->{wrap})
512 {
513 $x_num_end = ($end % $self->{wrap});
514 }else
515 {
516 $x_num_end = $end;
517 }
518
519 my $label = ${$self->{dm_labels}}[$i];
520 my $color = ${$self->{dm_label_color}}[$i] || ${$self->{dm_label_color}}[-1] || "silver";
521
522 my $label_x = (($x_num_end - $x_num_start) / 2) - (length($label) / 2);
523
524 my $label_x_start = (($self->{wrap} - $x_num_start) / 2) - (length($label) / 2);
525 my $label_x_end = ($x_num_end / 2) - (length($label) / 2);
526
527 $start_block_total = ( ($x_num_start - ($x_num_start % $self->{block_size}) ) / $self->{block_size} ) * $self->{block_space};
528 $end_block_total = ( ($x_num_end - ($x_num_end % $self->{block_size}) ) / $self->{block_size} ) * $self->{block_space};
529 $wrap_block_total = ( ($self->{wrap} - ( ($self->{wrap} - 1) % $self->{block_size}) ) / $self->{block_size} ) * $self->{block_space};
530
531 $self->{image}->bgcolor($color);
532 $self->{image}->fgcolor($color);
533
534 if($y_num_start == $y_num_end) #if the label does not cross the wrap line
535 {
536
537 $self->{image}->rectangle( $self->{seq_start_x} + ( ($x_num_start) * $self->{x_char_size} ) + $start_block_total, $self->{seq_start_y} + (($self->{no_sequences}) * $self->{y_char_size}) + ($y_num_start * $y_char), $self->{seq_start_x} + (($x_num_end) * $self->{x_char_size}) + $end_block_total, $self->{seq_start_y} + (($self->{no_sequences} + 1) * $self->{y_char_size}) + ($y_num_start * $y_char));
538 $self->{image}->fgcolor($self->{fg_color});
539 $self->{image}->bgcolor($self->{bg_color});
540
541 $self->{image}->moveTo( $self->{seq_start_x} + ( ($x_num_start + $label_x) * $self->{x_char_size}) + $start_block_total, $self->{seq_start_y} + (($self->{no_sequences} + 1) * $self->{y_char_size}) + ($y_num_start * $y_char) );
542 $self->{image}->font($self->{font});
543 $self->{image}->string($label);
544 }else
545 {
546 $self->{image}->rectangle( $self->{seq_start_x} + ( ($x_num_start) * $self->{x_char_size} ) + $start_block_total, $self->{seq_start_y} + (($self->{no_sequences}) * $self->{y_char_size}) + ($y_num_start * $y_char), $self->{seq_start_x} + (($self->{wrap}) * $self->{x_char_size}) + $wrap_block_total, $self->{seq_start_y} + (($self->{no_sequences} + 1) * $self->{y_char_size}) + ($y_num_start * $y_char));
547 $self->{image}->rectangle( $self->{seq_start_x} , $self->{seq_start_y} + (($self->{no_sequences}) * $self->{y_char_size}) + ($y_num_end * $y_char), $self->{seq_start_x} + (($x_num_end) * $self->{x_char_size}) + $end_block_total, $self->{seq_start_y} + (($self->{no_sequences} + 1) * $self->{y_char_size}) + ($y_num_end * $y_char));
548 $self->{image}->fgcolor($self->{fg_color});
549 $self->{image}->bgcolor($self->{bg_color});
550
551 $self->{image}->moveTo( $self->{seq_start_x} + ( ($x_num_start + $label_x_start) * $self->{x_char_size}) + $start_block_total, $self->{seq_start_y} + (($self->{no_sequences} + 1) * $self->{y_char_size}) + ($y_num_start * $y_char) );
552 $self->{image}->font($self->{font});
553 $self->{image}->string($label);
554
555 $self->{image}->moveTo( $self->{seq_start_x} + ( $label_x_end * $self->{x_char_size}), $self->{seq_start_y} + (($self->{no_sequences} + 1) * $self->{y_char_size}) + ($y_num_end * $y_char) );
556 $self->{image}->font($self->{font});
557 $self->{image}->string($label);
558
559
560 }
561  
562 }
563  
564 }
565  
566  
567 ##############################################
568 #Draw Y Label
569 sub y_label{
570 my $self = shift;
571  
572 $self->{image}->fgcolor($self->{y_label_color});
573  
574 my $y_num = $self->{y_num}; #sprintf( "%.0f" , (($self->{seq_length} / $self->{wrap}) + .5)) - 1;
575 my $y_char = $self->{y_size}; # ( ($self->{no_sequences} + $self->{pad_bottom}) * $self->{y_char_size});
576
577 for(my $k=0; $k<$y_num; $k++)
578 {
579  
580 for (my $i=0; $i< $self->{no_sequences}; $i++)
581 {
582 $self->{image}->moveTo($self->{pad_left}, $self->{seq_start_y} + ($i * $self->{y_char_size}) + ($k * $y_char) );
583 $self->{image}->font($self->{font});
584 $self->{image}->string(${$self->{seq_ids}}[$i]);
585 }
586
587 }
588  
589  
590 }
591 #####################################################
592 #Draw X Label
593 sub x_label{
594 my $self = shift;
595  
596 my $block_num = 0;
597 my $block_total = 0;
598 $self->{image}->fgcolor($self->{x_label_color});
599  
600 my $y_char = $self->{y_size}; # ( ($self->{no_sequences} + $self->{pad_bottom}) * $self->{y_char_size});
601  
602 for (my $i=1; $i<= $self->{seq_length}; $i++)
603 {
604  
605  
606 my $y_num = floor( $i / $self->{wrap}); # Used to be int(), but perl documentation advises against this
607 my $x_num;
608
609 if($i >= $self->{wrap})
610 {
611 $x_num = ($i % $self->{wrap});
612 }else
613 {
614 $x_num = $i;
615 }
616
617 my @digits = split //, reverse($i);
618
619 if( ($i % $self->{block_size}) == 0)
620 {
621 $block_num = $self->{block_space};
622 }else
623 {
624 $block_num = 0;
625 }
626
627 if( (($i - 1) % $self->{block_size}) == 0)
628 {
629 for (my $j=0; $j<=$#digits; $j++)
630 {
631
632 if ( defined($self->{show_nonsynonymous}) )
633 {
634 $self->{image}->moveTo($self->{seq_start_x} + $block_total + ( ($x_num-1) * $self->{x_char_size}) + ( ( floor( ($x_num-1)/3 ) * $self->{x_char_size} ) + ( ( floor( ($x_num-1)/3 ) ) * 6 )), ($self->{pad_top} + length($self->{seq_length_aa}) - $j) * $self->{y_char_size} + ($y_num * $y_char));
635 }else
636 {
637 $self->{image}->moveTo($self->{seq_start_x} + $block_total + ( ($x_num-1) * $self->{x_char_size}), ($self->{pad_top} + length($self->{seq_length}) - $j) * $self->{y_char_size} + ($y_num * $y_char));
638 }
639
640 $self->{image}->font($self->{font});
641 $self->{image}->string($digits[$j]);
642  
643 }
644 }
645 if($x_num == 0)
646 {
647 $block_total = 0;
648 }else
649 {
650 $block_total += $block_num;
651 }
652 }
653  
654 }
655  
656 ####################################################
657 #Domain Highlighting
658  
659 sub _draw_domain{
660 my $self = shift;
661  
662  
663 my $block_total = 0;
664 my ($start, $end, $block_num);
665  
666  
667 my $y_char = $self->{y_size}; # ( ($self->{no_sequences} + $self->{pad_bottom}) * $self->{y_char_size});
668  
669 for (my $k=0; $k <= $#{$self->{domain_start}}; $k++)
670 {
671  
672 #print STDERR join "\n", GD::Simple->color_names;
673 my $dmc = $self->{domain_color}[$k] || $self->{domain_color}[-1] || "silver";
674 $start = ${$self->{domain_start}}[$k] - 1;
675 $end = ${$self->{domain_end}}[$k] - 1;
676  
677  
678
679
680 for (my $i=0; $i < $self->{no_sequences}; $i++)
681 {
682
683 for (my $j = $start; $j <= $end; $j++)
684 {
685
686 my $y_num = int( $j / $self->{wrap});
687 my $x_num;
688
689 if($j >= $self->{wrap})
690 {
691 $x_num = ($j % $self->{wrap});
692 }else
693 {
694 $x_num = $j;
695 }
696
697 #print "J: $j\nXNUM: $x_num\nYNUM: $y_num\n";
698 $block_total = ( ($x_num - ($x_num % $self->{block_size}) ) / $self->{block_size} ) * $self->{block_space};
699
700
701 $self->{image}->bgcolor($dmc);
702 $self->{image}->fgcolor($dmc);
703
704 if ( defined($self->{show_nonsynonymous}) )
705 { # NOTE To shade amino acids as well, change $x_num HERE and HERE to $x_num + 1
706 $self->{image}->rectangle( $self->{seq_start_x} + ( ($x_num ) * $self->{x_char_size} ) + $block_total - 1 + ( ( floor( $x_num / 3 ) * $self->{x_char_size} ) + ( ( floor( $x_num / 3 ) ) * 6 )), $self->{seq_start_y} + ( $i * $self->{y_char_size} ) - $self->{y_char_size} + ($y_num * $y_char) , $self->{seq_start_x} + (($x_num + 1) * $self->{x_char_size}) + $block_total - 1 + ( ( floor( ($x_num)/3 ) * $self->{x_char_size} ) + ( ( floor( ($x_num)/3 ) ) * 6 )), $self->{seq_start_y} + ( $i * $self->{y_char_size}) + ($y_num * $y_char));
707 }else
708 {
709 $self->{image}->rectangle( $self->{seq_start_x} + ( ($x_num ) * $self->{x_char_size} ) + $block_total - 1, $self->{seq_start_y} + ( $i * $self->{y_char_size} ) - $self->{y_char_size} + ($y_num * $y_char) , $self->{seq_start_x} + (($x_num + 1) * $self->{x_char_size}) + $block_total - 1, $self->{seq_start_y} + ( $i * $self->{y_char_size}) + ($y_num * $y_char));
710 }
711 #$self->{image}->rectangle( $self->{seq_start_x} + ( ($j) * $self->{x_char_size} ) + $block_total, $self->{seq_start_y} + ($i - 1 * $self->{y_char_size}), $self->{seq_start_x} + (($j + 1) * $self->{x_char_size}) + $block_total , $self->{seq_start_y} + ( ($i) * $self->{y_char_size}));
712
713 $self->{image}->fgcolor($self->{fg_color});
714 $self->{image}->bgcolor($self->{bg_color});
715
716
717 }
718
719 $block_total = 0;
720 }
721 }
722  
723 }
724  
725  
726 sub _draw_colored_sequences{
727 my $self = shift;
728  
729 my $block_num = 0;
730 my $block_total = 0;
731 my $print_char;
732 my %colors;
733  
734 for my $values ( keys %PROTEIN_COLORS)
735 {
736 #print STDERR "$values : @{ $PROTEIN_COLORS{$values} }\n";
737 $colors{$values} = $self->{image}->colorAllocate(@{ $PROTEIN_COLORS{$values} });
738 }
739  
740 $self->{p_color_table} = \%colors;
741  
742 $self->{image}->fgcolor($self->{fg_color});
743  
744 for (my $i=0; $i < $self->{no_sequences}; $i++)
745 {
746
747 my @letters = split //, ${$self->{sequences}}[$i];
748
749  
750 my $y_num = $self->{y_num}; #sprintf( "%.0f", ( ($self->{seq_length} / $self->{wrap}) + .5) ) - 1;
751 my $y_char = $self->{y_size}; #( ($self->{no_sequences} + $self->{pad_bottom}) * $self->{y_char_size});
752
753 for(my $k=0; $k<=$y_num; $k++)
754 {
755 my $x_char = $k * $self->{wrap};
756
757 for (my $j=$x_char; $j <= ( ($x_char + $self->{wrap}) - 1); $j++)
758 {
759 last unless defined($letters[$j]);
760
761 $print_char = $letters[$j];
762
763 if( ( ($j + 1) % ($self->{block_size})) == 0)
764 {
765 $block_num = $self->{block_space};
766 }else
767 {
768 $block_num = 0;
769 }
770
771 #print "Chunk Space: $chunk_space\n";
772 $self->{image}->bgcolor($colors{$print_char});
773 $self->{image}->fgcolor($colors{$print_char});
774 $self->{image}->rectangle( $self->{seq_start_x} + ( ($j - $x_char) * $self->{x_char_size} ) + $block_total - 1 , $self->{seq_start_y} + ( $i * $self->{y_char_size} ) + ($k * $y_char) - $self->{y_char_size} , $self->{seq_start_x} + (($j - $x_char + 1) * $self->{x_char_size}) + $block_total - 1 , $self->{seq_start_y} + ($k * $y_char) + ( $i * $self->{y_char_size}));
775 $self->{image}->moveTo($self->{seq_start_x} + ( ($j - $x_char) * $self->{x_char_size}) + $block_total, $self->{seq_start_y} + ($k * $y_char) + ($i * $self->{y_char_size}) );
776 $self->{image}->fgcolor($self->{fg_color});
777 $self->{image}->font($self->{font});
778 $self->{image}->string($print_char);
779
780 if( defined($self->{labels}) && $i == ($self->{no_sequences} - 1))
781 {
782
783 if(${$self->{labels}}{$j + 1})
784 {
785 my $label = ${$self->{labels}}{$j + 1};
786 my $offset = defined($self->{dm_label_start}) ? 3 : 0;
787 $self->{image}->moveTo($self->{seq_start_x} + ( ( ($j - $x_char) + 1.25) * $self->{x_char_size}) + $block_total, $self->{seq_start_y} + (($self->{no_sequences}) * $self->{y_char_size}) + ($k * $y_char) + ( (length($label) + $offset) * ($self->{x_char_size}) ) );
788 $self->{image}->font($self->{font});
789 $self->{image}->angle(-90);
790 $self->{image}->string($label);
791 $self->{image}->angle(0);
792 }
793 }
794
795
796 $block_total += $block_num;
797 }
798 $block_total = 0;
799 }
800 }
801 }
802  
803 sub _draw_legend{
804  
805 my $self = shift;
806 my $title_font = $FONT_TABLE{3};
807 my @l_order = ("Negatively Charged", "Positively Charged", "Hydrophobic", "Aromatic", "Found in Loops", "Large Polar Acids");
808 my %legend = ("Negatively Charged" => ["D" , "E"] , "Positively Charged" => ["K", "R"] , "Hydrophobic" => ["A","F","I","L","M","V","W","Y"] ,
809 "Aromatic" => ["F", "H", "W", "Y"] , "Found in Loops" => ["D", "G", "P", "S", "T"] , "Large Polar Acids" => ["H", "K", "N", "Q", "R"]);
810  
811 my $x1 = 2;
812 my $x2 = 42;
813  
814 my $colors = $self->{p_color_table};
815  
816 my $y_start = $self->{footer_start};
817 my $label = "Protein Color Legend";
818 $self->{image}->bgcolor($self->{bg_color});
819 $self->{image}->fgcolor($self->{fg_color});
820 $self->{image}->rectangle(1,$y_start, 70 * $self->{x_char_size}, $self->{height} - 2);
821  
822 $self->{image}->moveTo((35 - (length($label) / 2) ) * $self->{x_char_size} , $y_start + $self->{y_char_size});
823 $self->{image}->font($title_font);
824 $self->{image}->string($label);
825  
826 my $count = 3;
827  
828 foreach my $c_label (@l_order)
829 {
830  
831 if( ($count % 2) == 0)
832 {
833  
834 $self->{image}->moveTo( $x2 * $self->{x_char_size}, $y_start + ( ($count - 1) * $self->{y_char_size}));
835 $self->{image}->font($self->{font});
836 $self->{image}->string($c_label);
837 my $i = 0;
838 foreach my $chars(@{$legend{$c_label}})
839 {
840 $self->{image}->bgcolor($$colors{$chars});
841 $self->{image}->fgcolor($$colors{$chars});
842 $self->{image}->rectangle( ($x2 + 20 + $i) * $self->{x_char_size}, $y_start + ( ($count - 2) * $self->{y_char_size}), ($x2 + 20 + $i + 1) * $self->{x_char_size}, $y_start + ( ($count -1) * $self->{y_char_size}));
843 $self->{image}->bgcolor($self->{bg_color});
844 $self->{image}->fgcolor($self->{fg_color});
845 $i++;
846 }
847  
848 }else
849 {
850 $self->{image}->moveTo($x1 * $self->{x_char_size} , $y_start + ($count * $self->{y_char_size}));
851 $self->{image}->font($self->{font});
852 $self->{image}->string($c_label);
853 my $i = 0;
854 foreach my $chars(@{$legend{$c_label}})
855 {
856 $self->{image}->bgcolor($$colors{$chars});
857 $self->{image}->fgcolor($$colors{$chars});
858 $self->{image}->rectangle( ($x1 + 20 + $i) * $self->{x_char_size}, $y_start + ( ($count - 1) * $self->{y_char_size}), ($x1 + 20 + $i + 1) * $self->{x_char_size}, $y_start + ( ($count) * $self->{y_char_size}));
859 $self->{image}->bgcolor($self->{bg_color});
860 $self->{image}->fgcolor($self->{fg_color});
861 $i++;
862 }
863 }
864  
865 $count += 1;
866 }
867  
868 }
869 ########################################
870 #####ACCESSORS#####
871 sub width{
872 my $self = shift;
873 return $self->{image}->width if exists $self->{image};
874 }
875  
876 sub height{
877 my $self = shift;
878 return $self->{image}->height if exists $self->{image};
879 }
880  
881 sub aln_length{
882 my $self = shift;
883 return $self->{seq_length} if exists $self->{seq_length};
884 }
885  
886 sub aln_format{
887 my $self = shift;
888 return $self->{seq_format} if exists $self->{seq_format};
889 }
890  
891 sub no_sequences{
892 my $self = shift;
893 return $self->{no_sequences} if exists $self->{no_sequences};
894 }
895  
896 1;
897 __END__
↑ Summary