blib/lib/Bio/Graphics/Glyph/phylo_align.pm untested

filter: Statementstmt 0.0% 0 / 506 Branchbran 0.0% 0 / 150 Conditioncond 0.0% 0 / 104 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 809 CC 76 SCAR 45.5
CC
76
Cov
0%
draw
240.0
_draw_dna
240.0
pairwise_draw_graph
90.0
CRAP 94.9
1 package Bio::Graphics::Glyph::phylo_align;
2  
3 use strict;
4 use base qw(Bio::Graphics::Glyph::generic Bio::Graphics::Glyph::xyplot);
5 use Bio::TreeIO;
6 use Bio::Graphics::Wiggle;
7 use POSIX qw(log10);
8  
9 use Carp 'croak','cluck';
10 use Data::Dumper;
11  
12 my %complement = (g=>'c',a=>'t',t=>'a',c=>'g',n=>'n',
13 G=>'C',A=>'T',T=>'A',C=>'G',N=>'N');
14  
15  
16 # turn off description
17 sub description { 0 }
18  
19 # turn off label
20 # sub label { 1 }
21  
22 sub height {
23 my $self = shift;
24 my $font = $self->mono_font;
25
26 #adjust the space to take if conservation scores are drawn instead
27 if (! $self->dna_fits) {
28 my $species_spacing_score = $self->option('species_spacing_score') || 5;
29 $self->factory->set_option('species_spacing', $species_spacing_score);
30 }
31
32 my $species_spacing = $self->option('species_spacing') || 1;
33
34 #Height = NumSpecies x Spacing/species x FontHeight
35 my $height = ($self->known_species + $self->unknown_species + 1)
36 * $species_spacing
37 * $font->height;
38
39 #use this if you want to show only those species that have alignments in the viewing window
40 #$height = ($self->extract_features + 1) * 2 * $font->height;
41
42 $self->factory->set_option('height', $height);
43
44
45 return $height;
46
47 }
48  
49 #####
50 # TODO: extract the wigfiles covering the range as well
51 #####
52 # get all features within the viewing window
53 sub extract_features {
54 my $self = shift;
55 #my $segment = $self->feature->{'factory'}->segment($self->feature->refseq,
56 # $self->feature->start => $self->feature->stop);
57 #my @match = $segment->features('submatch:pa');
58 my @match = $self->feature->features('submatch:pa');
59 warn $self->feature->features;
60 #print "Match has ",$#match,"<p>\n";
61
62 # exract wifiles here too:
63 my @wmatch = $self->feature->features('wfile:pa');
64 #push @match, $self->feature->features('wfile:pa');
65 # print "xxxxxx<pre>",Dumper(@wmatch),"</pre>cccccc";
66 #print "WMatch has ",$#wmatch,"<p>\n";
67  
68 #print "Halfwaypoint<p>";
69 #for my $feature (@match,@wmatch) {
70 #my %attributes = $feature->attributes;
71 #my $species = $attributes{'species'};
72 #print "<pre>Feature $species:\n",Dumper(%attributes),"</pre>\n";
73 #}
74  
75 my %alignments;
76 # for my $feature (@match) {
77 for my $feature (@match,@wmatch) {
78 my %attributes = $feature->attributes;
79 my $species = $attributes{'species'};
80
81 push @{$alignments{$species}}, $feature;
82 }
83
84 %alignments;
85 }
86  
87 #known species (all those that are in the Phylo tree)
88 sub known_species {
89 my $self = shift;
90
91 my $tree = shift;
92
93 if ($tree) {
94 my @leaves = $tree->get_leaf_nodes;
95 my @allspecies = map {$_->id} @leaves;
96 return @allspecies
97
98 } else {
99 #this may be too simple of an assumption, especially for non newick files
100 my $tree_file = $self->option('tree_file');
101
102 open (FH, $tree_file);
103 my $newick = <FH>;
104 close FH;
105
106 my @allspecies = $newick =~ /([a-zA-Z]\w*)/g;
107 return @allspecies;
108 }
109 }
110  
111 sub unknown_species {
112 my $self = shift;
113  
114 my %alignments; #all species in viewing window
115 my $refspecies; #all species from cladogram info
116 my @current_species; #all species in viewing window
117 my @known_species; #species in GFF but and clado
118 my @unknown_species; #species in GFF but not in clado
119 # current - known = unknown
120
121
122 if (@_) {
123 %alignments = %{$_[0]};
124 $refspecies = $_[1];
125 @current_species = @{$_[2]};
126 @known_species = @{$_[3]};
127 } else {
128 %alignments = $self->extract_features;
129 $refspecies = $self->option('reference_species');
130 @current_species = keys %alignments; #all species in viewing window
131 @known_species = $self->known_species; #all species from cladogram info
132 } #would have combined the two cases into one line using || but Perl will treat the arrays as num of elem
133
134 #do set subtraction to see which species in viewing range but not in tree
135 my %seen; # build lookup table
136 @seen{@known_species} = ();
137 foreach my $item (@current_species, $refspecies) {
138 push(@unknown_species, $item) unless exists $seen{$item};
139 }
140
141 return @unknown_species;
142
143 }
144  
145 sub set_tree {
146 my $self = shift;
147
148 #warn"My species are ".Dumper(@species);
149
150 my $tree_file = $self->option('tree_file');
151 my $tree_format = $self->option('tree_format') || 'newick';
152
153 my $treeio = new Bio::TreeIO(-file => $tree_file,
154 -format => $tree_format);
155
156
157
158 my $tree = $treeio->next_tree;
159 my $root = $tree->get_root_node;
160
161 # would be ideal to remove all species that don't have features (alignments) within
162 # viewing window but there is a bug in Bio::Tree library where you can't remove the
163 # first leaf node.
164
165 # $tree->remove_Node('dog'); # etc...
166
167 #set the leaf x coodinate (make all evenly spaced)
168 my @leaves = $tree->get_leaf_nodes;
169 for (my $i=0; $i<@leaves; $i++) {
170 my $leaf = $leaves[$i];
171
172 #note that leaves can use "description" functions while intermediate nodes cannot
173 #thus objects must be handled directly
174 $leaf->description({'x'=>$i});
175
176 }
177
178
179 #set root height to 0
180 $root->{'description'}{'y'} = 0;
181
182 #set the x and y coordinates of all intermediate nodes
183 get_n_set_next_treenode($root, 0);
184
185 flip_xy($tree);
186  
187
188 $tree;
189
190 }
191  
192 sub get_max_height {
193 my $tree = shift;
194 my $max_height;
195
196 #get the max height
197 for my $child ($tree->get_leaf_nodes) {
198 my $x = $child->{'_description'}{'x'};
199 $max_height = $x if $max_height < $x;
200 }
201
202 $max_height;
203 }
204  
205  
206 sub draw_clado {
207 my $self = shift;
208 my $tree = shift;
209 my $gd = shift;
210 my ($x1, $y1, $x2, $y2, $color, $xscale, $yscale, $xoffset, $yoffset, $start_x, $draw_clado_left) = @_;
211
212 my @bounds = $gd->getBounds;
213
214 my $root = $tree->get_root_node;
215 my @nodes = $root->get_all_Descendents;
216
217 #draw bg for cladogram
218 my $font = $self->mono_font;
219 my $clado_bg = $self->color('clado_bg') || $self->bgcolor;
220 my @coords = (0, $y1, $start_x+$xoffset+$font->width-1, $y2+1);
221 my @coords2 = ($x1, $y1, $start_x+$xoffset/2, $y2);
222 if ($draw_clado_left) {
223 $gd->filledRectangle(@coords, $clado_bg);
224 $gd->filledRectangle(@coords2, $self->color('clado_bg'));
225 $gd->filledRectangle($x2, $x1, $bounds[0], $bounds[1], $self->color('bg_color')) if $self->dna_fits;
226 } else {
227 $gd->filledRectangle($bounds[0]-$coords[2], $coords[1], $bounds[0]-$coords[0], $coords[3],
228 $self->color('bg_color'));
229 $gd->filledRectangle($bounds[0]-$coords2[2], $coords2[1], $bounds[0]-$coords2[0], $coords2[3],
230 $clado_bg);
231 $gd->filledRectangle(0, $y1, $x1, $y2+1, $self->color('bg_color')) if $self->dna_fits;
232 }
233  
234
235
236 #draw the lines of the tree
237 for my $node ($root,@nodes) {
238 next if $node->is_Leaf;
239 my $x = $node->{'_description'}{'x'} * $xscale;
240
241 #draw vertical line covering all children
242 my $topx = $node->{'_description'}{'childmin'} * $yscale;
243 my $botx = $node->{'_description'}{'childmax'} * $yscale;
244
245 @coords = ($x+$xoffset, $topx+$yoffset, $x+$xoffset, $botx+$yoffset);
246 if ($draw_clado_left) {
247 $gd->line(@coords, $self->fgcolor);
248 } else {
249 $gd->line($bounds[0]-$coords[2], $coords[1], $bounds[0]-$coords[0], $coords[3], $self->fgcolor);
250 }
251
252 #draw a line connecting the bar to each child
253 my @children = $node->each_Descendent;
254 for my $child (@children) {
255 my $cx = $child->{'_description'}{'x'} * $xscale;
256 my $cy = $child->{'_description'}{'y'} * $yscale;
257 $cx = $start_x if $child->is_Leaf;
258
259 #print"($cx, $cy)";
260
261 @coords = ($x+$xoffset, $cy+$yoffset, $cx+$xoffset, $cy+$yoffset);
262 if ($draw_clado_left) {
263 $gd->line(@coords, $self->fgcolor);
264 } else {
265 $gd->line($bounds[0]-$coords[2], $coords[1], $bounds[0]-$coords[0], $coords[3], $self->fgcolor);
266 }
267
268 }
269
270 }
271
272
273
274
275 #my $tree = $treeio->next_tree;
276 #my @nodes = grep { $_->bootstrap > 70 } $tree->get_nodes;
277 #warn"my nodes are:\n".Dumper(@nodes);
278 print"</pre>";
279
280 $start_x + $xscale;
281
282 }
283  
284 #tree is made with root on top but this will switch x and y coords so root is on left
285 sub flip_xy {
286 my $tree = shift;
287 my $root = $tree->get_root_node;
288
289 my @nodes = $root->get_all_Descendents;
290
291 for my $node ($root, @nodes) {
292 if ($node->is_Leaf) {
293 $node->{'_description'} = {
294 'x' => $node->{'_description'}{'y'},
295 'y' => $node->{'_description'}{'x'}
296 }
297 } else {
298 $node->{'_description'} = {
299 'x' => $node->{'_description'}{'y'},
300 'y' => $node->{'_description'}{'x'},
301 'child_pos' => $node->{'_description'}{'child_pos'},
302 'childmin' => $node->{'_description'}{'childmin'},
303 'childmax' => $node->{'_description'}{'childmax'}
304 }
305 }
306 }
307
308
309 }
310  
311  
312 #recursive function that sets the x and y coordinates of tree nodes
313 sub get_n_set_next_treenode {
314 my $node = shift;
315 my $height = $node->{'_description'}{'y'};
316
317 my @children = $node->each_Descendent;
318
319
320 my $x = 0;
321 my $min_child_x = -1;
322 my $max_child_x = -1;
323
324 #iterate through children to find the x's and set the y's (height's)
325 for my $child (@children) {
326 #set the y coordinate as parent's height + 1
327 $child->{'_description'}{'y'} = $height + 1;
328 get_n_set_next_treenode($child);
329
330 #retrieve the child's x coordinate
331 my $child_x = $child->{'_description'}{'x'} || 0;
332 $x += $child_x;
333
334 $min_child_x = $child_x if $min_child_x==-1 || $child_x < $min_child_x;
335 $max_child_x = $child_x if $max_child_x==-1 || $max_child_x < $child_x;
336 #$x += $child->discription->{'x'}; #cannot do this for intermediate nodes
337
338 #store the x values of all children
339 push @{$node->{'_description'}{'child_pos'}}, $child_x;
340
341
342 }
343
344 #set the current x coordinate as the average of all children's x's
345 if (@children) {
346 $x = $x / @children;
347 $node->{'_description'}{'x'} = $x;
348 $node->{'_description'}{'childmin'} = $min_child_x;
349 $node->{'_description'}{'childmax'} = $max_child_x;
350  
351 }
352
353 $node->{'_description'}{'y'} = $height;
354
355 }
356  
357 sub get_legend_and_scale {
358 my $yscale = shift;
359 my $height = shift;
360
361 if ($yscale < 2*$height - 1) {
362 $height = 0;
363 }
364
365 #########
366 # chage scale later so that the base can be anything and not just 1!!
367
368 # scale legend goes in order from min, axis, max, if either min or max = 1, then will only have min & max
369 my @order = sort {$a <=> $b} (1, @_);
370 my $graph_scale = - ($yscale - $height) / (log10($order[2]) - log10($order[0]));
371 my $graph_legend = {1 => $graph_scale * (log10(1) - log10($order[2])),
372 $order[0] => $graph_scale * (log10($order[0]) - log10($order[2])),
373 $order[2] => 0};
374
375 #print "order is @order and the yscale is $yscale and height is $height<br>";
376
377 return ($graph_legend, $graph_scale);
378 }
379  
380 #main method that draws everything
381 sub draw {
382 my $self = shift;
383 my $font = $self->mono_font;
384 my $height = $font->height;
385 my $scale = $self->scale;
386
387 my $gd = shift;
388 my ($left,$top,$partno,$total_parts) = @_;
389 my ($x1,$y1,$x2,$y2) = $self->bounds($left, $top);
390
391
392 my @bounds = $gd->getBounds;
393 my $draw_clado_left = $self->option('draw_clado_left');
394
395
396 #spacing of either DNA alignments or score histograms in units of font height
397 my $species_spacing = $self->option('species_spacing') || 1;
398
399 my $xscale = $font->width;
400 my $yscale = $height * $species_spacing;
401
402
403 my $xoffset = $x1;
404 my $yoffset = $y1 + 0.5*$font->height;
405
406 #method that reads the tree file to create the tree objects
407 my $tree = $self->set_tree;
408 my $max_height = get_max_height($tree);
409 my $start_x =($max_height-1) * $xscale +$xoffset;
410
411
412 my $connector = $self->connector;
413
414 #all species having alignments in viewing window (key=name, val=feat obj)
415 my %alignments = $self->extract_features;
416 #print "Species are:",keys %alignments,"<br>\n";
417
418 my ($min_score, $max_score) = $self->get_score_bounds(%alignments);
419 #$min_score = 0 unless $min_score;
420 my ($graph_legend, $graph_scale) = get_legend_and_scale($yscale, $height, $min_score, $max_score);
421 #print "min/max scores: $min_score, $max_score<br>\n",
422 #"graph legend and scale: $graph_legend, $graph_scale";
423 # TODO: Gap entries give an undef for the min values for some reason
424
425
426 my $refspecies = $self->option('reference_species');
427
428 my @current_species = keys %alignments; #all species in viewing window
429 my @known_species = $self->known_species($tree); #all species from cladogram info
430 my @unknown_species = $self->unknown_species(\%alignments,
431 $refspecies,
432 \@current_species,
433 \@known_species);
434 #species in GFF but not in clado
435  
436 ########is this even used?
437 #my @allfeats;
438 #for my $species (keys %alignments) {
439 # push @allfeats, @{$alignments{$species}};
440 #}
441
442
443 #this y value is the base for the next species' alignment/histogram and is incremented at each step
444 my $y = $y1;
445
446
447 for my $species (@known_species,@unknown_species) {
448 my $y_track_top = $y + $height;
449 my $y_track_bottom = $y + $yscale;
450
451
452 if ($yscale < 2*$height-1) {
453 #small scale
454 $y_track_top = $y;# + $height;
455 my $y_track_bottom = $y + $height;
456 }
457
458
459 #process the reference sequence differently
460 if ($species eq $refspecies) {
461 #draw DNA alignments if zoomed close enough
462 my ($fx1,$fy1) = ($x1, $y_track_top);
463 my ($fx2,$fy2) = ($x2,$y_track_bottom);
464
465  
466 if ($self->dna_fits) {
467 my $dna = eval { $self->feature->seq };
468 $dna = $dna->seq if ref($dna) and $dna->can('seq'); # to catch Bio::PrimarySeqI objects
469 my $bg_color = $self->color('ref_color') || $self->bgcolor;
470
471 $fy2 = $fy1 + $font->height || $y2;
472
473
474 $self->_draw_dna($gd,$dna,$fx1,$fy1,$fx2,$fy2, $self->fgcolor, $bg_color);
475 } else {
476 }
477
478 my $x_label_start = $start_x + $xoffset + $font->width;
479 $self->species_label($gd, $draw_clado_left, $x_label_start, $y, $species) unless ($self->option('hide_label'));
480
481 $y += $yscale;
482 next;
483 }
484
485
486 #skip if the there is no alignments for this species in this window
487 unless ($alignments{$species}) {
488 my $x_label_start = $start_x + $xoffset + $font->width;
489 $self->species_label($gd, $draw_clado_left, $x_label_start, $y, $species) unless ($self->option('hide_label'));
490
491 $y += $yscale;
492 next;
493 }
494
495
496
497 my @features = @{$alignments{$species}};
498
499
500
501
502 #draw the axis for the plots
503 $self->draw_pairwisegraph_axis($gd,
504 $graph_legend,
505 $x1,
506 $x2,
507 $y_track_top,
508 $y_track_bottom,
509 $draw_clado_left,
510 @bounds) unless $self->dna_fits;
511
512
513 #iterate through the wigfiles and put them on the graph
514 ###
515 # todo
516 ###
517
518
519 #iterate through features, and put them on the graph
520 for my $feat (@features) {
521 my ($start, $stop, %attributes) = ($feat->start, $feat->stop, $feat->attributes);
522
523 my ($fx1,$fy1) = ($x1 + ($start-$self->start)*$scale, $y_track_top);
524 my ($fx2,$fy2) = ($x1 + ($stop-$self->start)*$scale,$y_track_bottom);
525
526 my $gapstr = $attributes{'Gap'} || "M".($stop-$start+1);
527 my @gapstr = split " ", $gapstr;
528 my @gaps;
529 for my $gap (@gapstr) {
530 my ($type, $num) = $gap =~ /^(.)(\d+)/;
531 push @gaps, [$type, $num+0];
532 }
533
534
535
536 #draw DNA alignments if zoomed close enough
537 if ($self->dna_fits) {
538
539
540 my $test = 0;
541
542 my $hit = $feat->hit;
543 if (!defined $hit) {
544 warn "No hit for feature $feat, skipping drawing DNA";
545 next;
546 }
547  
548 my $hit_seq = $hit->seq || print "No seq for hit<br>\n";
549 my $targ_dna = $hit_seq->seq || print "No seq for hit_seq<br>\n";
550
551
552 my $seq = $feat->seq || print "No sequence object for feature<br>\n";
553 my $ref_dna = $seq->seq || print "No ref dna";
554
555 #doesn't work as planned
556 # my $ref_dna = $feat->seq->seq || print "No ref dna";
557 # my $targ_dna = $feat->hit->seq->seq || print "No targ dna";
558
559
560 next if !defined $ref_dna || !defined $targ_dna;
561
562 $self->draw_dna($gd,$ref_dna, $targ_dna,$fx1,$fy1,$fx2,$fy2,\@gaps);
563 } else {
564 my $wigfile = $attributes{'wigfile'};
565 if ($wigfile) {
566 if (-e $wigfile) {
567 $self->pairwise_draw_wig_graph($gd, $feat, $x1, $scale, \@gaps, $graph_legend->{1}, $graph_scale, $fx1, $fy1, $fx2, $fy2,$wigfile);
568 } else {
569 warn "Wigfile $wigfile does not exist, skipping ...";
570 }
571
572 } else {
573 $self->pairwise_draw_graph($gd, $feat, $x1, $scale, \@gaps, $graph_legend->{1}, $graph_scale, $fx1, $fy1, $fx2, $fy2);
574 }
575
576 }
577 }
578
579
580 #label the species in the cladogram
581 my $x_label_start = $start_x + $xoffset + $font->width;
582 $self->species_label($gd, $draw_clado_left, $x_label_start, $y, $species) unless ($self->option('hide_label'));
583
584 $y += $yscale;
585 }
586
587 $self->draw_clado($tree, $gd, $x1, $y1, $x2, $y2, $self->fgcolor,
588 $xscale, $yscale, $xoffset, $yoffset, $start_x, $draw_clado_left);
589  
590 }
591  
592  
593  
594 sub species_label {
595 my $self = shift;
596 my $gd = shift;
597 my $draw_clado_left = shift;
598 my $x_start = shift;
599 my $y_start = shift;
600 my $species = shift;
601
602 my $font = $self->mono_font;
603  
604 $x_start += 2;
605 my $text_width = $font->width * length($species);
606 my $bgcolor = $self->color('bg_color');
607
608 #make label
609 if ($draw_clado_left) {
610
611 $gd->filledRectangle($x_start-2, $y_start, $x_start + $text_width, $y_start+$font->height, $bgcolor);
612 $gd->rectangle($x_start-2, $y_start, $x_start + $text_width, $y_start+$font->height, $self->fgcolor);
613 $gd->string($font, $x_start, $y_start, $species, $self->fgcolor);
614
615 } else {
616 my ($x_max, $y_max) = $gd->getBounds;
617 my $write_pos = $x_max - $x_start - $text_width;
618
619 $gd->filledRectangle($write_pos, $y_start, $write_pos + $text_width+2, $y_start+$font->height, $bgcolor);
620 $gd->rectangle($write_pos, $y_start, $write_pos + $text_width+2, $y_start+$font->height, $self->fgcolor);
621 $gd->string($font, $write_pos+2, $y_start, $species, $self->fgcolor);
622
623 }
624 }
625  
626  
627 # draws the legends on the conservation scale
628 sub draw_pairwisegraph_axis {
629 my $self = shift;
630 my ($gd, $graph_legend, $x1, $x2, $y_track_top, $y_track_bottom, $draw_clado_left, @bounds) = @_;
631
632 my $font = $self->mono_font;
633 my $axis_color = $self->color('axis_color') || $self->fgcolor;
634 my $mid_axis_color = $self->color('mid_axis_color') || $axis_color;
635
636 for my $label (keys %$graph_legend) {
637 my $y_label = $graph_legend->{$label} + $y_track_top;
638  
639
640 my $col = $axis_color;
641 $col = $mid_axis_color if ($y_label != $y_track_top && $y_label != $y_track_bottom);
642 $gd->line($x1,$y_label,$x2,$y_label,$col);
643
644 my @coords = (0, $y_label, $x1, $y_label);
645
646
647 if ($draw_clado_left) {
648 #draw the legend on the right
649 $coords[0] = $bounds[0] - $coords[0];
650 $coords[2] = $bounds[0] - $coords[2];
651
652 my $x_text_offset = length($label) * $font->width;
653
654 $gd->string($font, $coords[0]-$x_text_offset, $coords[1], $label, $self->fgcolor);
655 $gd->line(@coords, $self->fgcolor);
656
657 $gd->line($x2,$y_track_top,$x2,$y_track_bottom,$self->fgcolor);
658 } else {
659 #draw the legned on the left
660 $gd->string($font, @coords[0..1], $label, $self->fgcolor);
661 $gd->line(@coords, $self->fgcolor);
662
663 $gd->line($x1,$y_track_top,$x1,$y_track_bottom,$self->fgcolor);
664 }
665
666 }
667
668 }
669  
670  
671 #find min and max from features within the bounds
672 sub get_score_bounds {
673 my $self = shift;
674 my %alignments = @_;
675
676 my $min = -1;
677 my $max = -1;
678
679 for my $species (keys %alignments) {
680 for my $feature (@{$alignments{$species}}) {
681 my $score = $feature->score;
682
683 #check to see if wigfile
684 if ($score == undef) {
685 my %attributes = $feature->attributes;
686 if (-e $attributes{'wigfile'}) {
687 ($min, $max) = $self->get_score_bounds_wigfile($feature,$min,$max,$attributes{'wigfile'});
688 }
689 next;
690 }
691
692 $min = $score if $min == -1 || $score < $min;
693 $max = $score if $max == -1 || $max < $score;
694 }
695 }
696
697
698 my @parts = $self->parts;
699
700 return ($min, $max)
701 }
702  
703 #find min and max of sampled wigfile
704 sub get_score_bounds_wigfile {
705 my $self = shift;
706 my $feature = shift;
707 my ($min,$max,$wigfile) = @_;
708
709 my $start = $feature->start < $self->start ? $self->start : $feature->start;
710 my $stop = $self->stop < $feature->stop ? $self->stop : $feature->stop;
711
712 #print $self->stop, "-", $feature->stop, "checking @_ $start - $stop\n";
713
714 #extract wig file contents
715 my $wig = Bio::Graphics::Wiggle->new($wigfile, 0, {step => 1}) or die;
716
717 #todo: make step configurable
718 my $step = 100;
719
720 for (my $i=$start; $i<$stop; $i += $step) {
721 my $v = $wig->value($i);
722
723 next unless defined $v;
724
725 $min = $v if !defined $min or $v < $min;
726 $max = $v if !defined $max or $max < $v;
727
728 }
729
730 #print "min and max are $min , $max<br>\n";
731
732 return ($min,$max);
733
734 }
735  
736  
737  
738 sub pairwise_draw_graph {
739 my $self = shift;
740 my $gd = shift;
741 my $feat = shift; # current feature object
742 my $x_edge = shift; # x start position of the track
743 my $scale = shift; # pixels / bp
744 my $gaps = shift; # gap data for insertions, deletions and matches
745 my $zero_y = shift; # y coordinate of 0 position
746 my $graph_scale = shift; # scale for the graph. y_coord = graph_scale x log(score)
747
748 my ($x1,$y1,$x2,$y2) = @_;
749 my $fgcolor = $self->fgcolor;
750 my $errcolor = $self->color('errcolor') || $fgcolor;
751
752 my $score = $feat->score;
753 my %attributes = $feat->attributes;
754
755
756 my $log_y = log10($score);
757 my $y_bottom = log10($score) * $graph_scale + $zero_y + $y1;
758 my $y_top = $zero_y+$y1;
759
760 my @y = sort {$a <=> $b} ($y_bottom, $y_top);
761
762
763
764 #missing gap data
765 unless ($gaps) {
766 $x1 = $x_edge if $x1 < $x_edge;
767 return if $x2 < $x_edge;
768 #$gd->filledRectangle($x1,$y[0],$x2,$y[1],$fgcolor);
769 return;
770 }
771
772 my $bp = 0;
773
774 #draw a bar representing the score for the span of base pairs
775 for my $tuple (@$gaps) {
776 my ($type, $num) = @$tuple;
777
778 #warn"$type and $num";
779 if ($type eq "M") {
780 my $x_left = $x1 + ($bp*$scale);
781 my $x_right = $x_left + $num*$scale;
782
783 $bp += $num;
784
785 $x_left = $x_edge if $x_left < $x_edge;
786 next if $x_right < $x_edge;
787 $gd->filledRectangle($x_left,$y[0],$x_right,$y[1],$fgcolor);
788 } elsif ($type eq "D") {
789
790 $bp += $num;
791
792 } elsif ($type eq "I") {
793 my $x_left = $x1 + ($bp*$scale);
794 $gd->line($x_left-2, $y1-4, $x_left, $y1, $errcolor);
795 $gd->line($x_left, $y1, $x_left+2, $y1-4, $errcolor);
796 }
797
798  
799 }
800
801
802 }
803  
804  
805  
806 sub pairwise_draw_wig_graph {
807 my $self = shift;
808 my $gd = shift;
809 my $feat = shift; # current feature object
810 my $x_edge = shift; # x start position of the track
811 my $scale = shift; # pixels / bp
812 my $gaps = shift; # gap data for insertions, deletions and matches
813 my $zero_y = shift; # y coordinate of 0 position
814 my $graph_scale = shift; # scale for the graph. y_coord = graph_scale x log(score)
815
816 my ($x1,$y1,$x2,$y2,$wigfile) = @_;
817 my $fgcolor = $self->fgcolor;
818 my $errcolor = $self->color('errcolor') || $fgcolor;
819
820 my $score = $feat->score;
821 my %attributes = $feat->attributes;
822
823
824 # my $log_y = log10($score);
825 # my $y_bottom = log10($score) * $graph_scale + $zero_y + $y1;
826 # my $y_top = $zero_y+$y1;
827 #
828 # my @y = sort {$a <=> $b} ($y_bottom, $y_top);
829
830
831
832 #print "checking wigfile $wigfile<br>\n";
833
834
835 #todo: make step variable
836 my $start = $feat->start < $self->start ? $self->start : $feat->start;
837 my $stop = $self->stop < $feat->stop ? $self->stop : $feat->stop;
838 # my $wig = Bio::Graphics::Wiggle->new($wigfile, 0) or die;
839 my $wig = Bio::Graphics::Wiggle->new($wigfile, 0, {step => 1}) or die;
840  
841  
842 ##### not sure why this was here
843 # my $vals = $wig->values($start,$stop);
844 # my ($vmin,$vmax);
845 # for my $v (@$vals) {
846 # next unless defined $v;
847 # $vmin = $v if !defined $vmin or $v < $vmin;
848 # $vmax = $v if !defined $vmax or $vmax < $v;
849 # }
850
851
852 # print "min and max are $vmin , $vmax\n";
853 # $min = $min < $vmin ? $min : $vmin;
854 # $max = $vmax < $max ? $max : $vmax;
855 # print "min and max are $min , $max\n";
856 # return ($min,$max);
857
858
859 # print Dumper($vals);
860
861
862 # my $pos = $start;
863
864
865 # $gd->rectangle($x1,$y1,$x2,$y2,$fgcolor);
866 # $gd->line($x1,$y1,$x2,$y2,$fgcolor);
867 # $gd->line($x2,$y1,$x1,$y2,$fgcolor);
868 # print "Start and stop: $start and $stop<br>\n";
869 # print "<br>\n$x1, $x2, $x_edge, $start, $stop<br>\n";
870
871
872
873 if ($scale < 1) {
874 #### Algorithm 1, when zoomed at scale 1bp / pixel or more
875 #### sample 10 values across the pixel (prevents redrawing same pixel over
876 #### and over). The sample values are averaged.
877
878 my $bp = $start;
879 for (my $pix=$x1; $pix < $x2; $pix++) {
880
881 $bp = ($pix - $x1)/$scale + $start;
882
883 #todo: make samplesize an option
884 my $samplesize = 10;
885 my $score = 0;
886 my $trial;
887 for ($trial=0; $trial < $samplesize; $trial++) {
888
889 my $samp_bp = $bp + ($trial/$samplesize)/$scale;
890 last if $samp_bp > $stop;
891
892 $score += $wig->value($samp_bp);
893
894 #print "trial $trial: $samp_bp\tPixel $pix\tpos $x1<br>\n" if ($bp > 1700 && $bp < 1750);
895
896
897 }
898
899 #print "Pixel: $pix : Total $score and trial $trial<br>\n";
900 next if $trial == 0 || $score == 0;
901 $score = $score / $trial;
902
903 $self->draw_log10_rectangle($score, $graph_scale, $zero_y, $y1,
904 $zero_y, $pix, $pix, $gd, $fgcolor);
905
906
907 }
908
909 } else {
910 #### Algorithm 2, when zoomed in close at less than 1bp / pixel (1 bp spans
911 #### 1 pixel or more), draw each value directly
912
913 my $step = 1;
914
915 for (my $i=$start; $i<$stop; $i += $step) {
916 my $val = $wig->value($i);
917 next if !defined $val;
918
919 my $bp = $i - $start;
920
921 my $x_left = $x1 + ($bp*$scale);
922 my $x_right = $x_left + 1*$scale;
923
924 $score = $val;
925
926
927 $self->draw_log10_rectangle($score, $graph_scale, $zero_y, $y1,
928 $zero_y, $x_left, $x_right, $gd, $fgcolor);
929
930
931 }
932
933
934 }
935
936 return;
937  
938
939 }
940  
941 sub draw_log10_rectangle {
942 my $self = shift;
943 my $score = shift;
944 my $graph_scale = shift;
945 my $zero_y = shift;
946 my $y1 = shift;
947 $zero_y = shift; # oy vey - this will be overwritten
948 my $x_left = shift;
949 my $x_right = shift;
950 my $gd = shift;
951 my $fgcolor = shift;
952
953 my $log_y = log10($score);
954 my $y_bottom = log10($score) * $graph_scale + $zero_y + $y1;
955 my $y_top = $zero_y+$y1;
956
957 my @y = sort {$a <=> $b} ($y_bottom, $y_top);
958
959 #print "$x_left,$y[0],$x_right,$y[1]<br>\n";
960
961 $gd->filledRectangle($x_left,$y[0],$x_right,$y[1],$fgcolor);
962 }
963  
964  
965  
966 sub draw_dna {
967 my $self = shift;
968 my ($gd,$ref_dna, $dna,$x1,$y1,$x2,$y2, $gaps) = @_;
969 my $pixels_per_base = $self->scale;
970
971 my $fgcolor = $self->fgcolor;
972 my $bg_color = $self->color('targ_color') || $self->bgcolor;
973 my $errcolor = $self->color('errcolor') || $fgcolor;
974 my $font = $self->mono_font;
975
976 $y2 = $y1 + $font->height || $y2;
977
978
979
980 #missing gap data, draw as is
981 unless ($gaps) {
982 warn"no gap data for DNA sequence $dna";
983 $self->_draw_dna($gd, $dna, $x1, $y1, $x2, $y2);
984 return;
985 }
986
987 #parse the DNA segments by the gaps
988 for my $tuple (@$gaps) {
989 my ($type, $num) = @$tuple;
990
991 if ($type eq "M") {
992 my $dnaseg = substr($dna, 0, $num);
993 my $ref_dnaseg = substr($ref_dna, 0, $num);
994
995 $self->_draw_dna($gd,$dnaseg, $x1, $y1, $x2, $y2, $fgcolor, $bg_color,$ref_dnaseg);
996 $dna = substr($dna, $num);
997 $ref_dna = substr($ref_dna, $num);
998 $x1 += $num * $pixels_per_base;
999 } elsif ($type eq "D") {
1000 my $dnaseg = '-' x $num;
1001 $self->_draw_dna($gd, $dnaseg, $x1, $y1, $x2, $y2, $fgcolor);
1002 $gd->rectangle($x1, $y1-1, $x1+$num * $pixels_per_base, $y2+1, $errcolor);
1003 $ref_dna = substr($ref_dna, $num);
1004 $x1 += $num * $pixels_per_base;
1005 } elsif ($type eq "I") {
1006 $dna = substr($dna, $num);
1007 $gd->line($x1-2, $y1-2, $x1+2, $y1-2, $errcolor);
1008 $gd->line($x1, $y1-1, $x1, $y2+1, $errcolor);
1009 $gd->line($x1-2, $y2+1, $x1+2, $y2+1, $errcolor);
1010 }
1011
1012  
1013 }
1014
1015
1016 }
1017  
1018 sub _draw_dna {
1019 my $self = shift;
1020  
1021 #the last argument is optional. If the reference seq is given, it will check it with target
1022 my ($gd,$dna,$x1,$y1,$x2,$y2, $color, $bg_color, $ref_dna) = @_;
1023
1024 my $pixels_per_base = $self->scale;
1025 my $feature = $self->feature;
1026
1027 unless ($ref_dna) {
1028 $gd->filledRectangle($x1+1, $y1, $x2, $y2, $bg_color);
1029 }
1030
1031 my $strand = $feature->strand || 1;
1032 $strand *= -1 if $self->{flip};
1033  
1034 my @bases = split '',$strand >= 0 ? $dna : $self->reversec($dna);
1035 my @refbases = split '',$strand >= 0 ? $ref_dna : $self->reversec($ref_dna);
1036
1037
1038
1039 $color = $self->fgcolor unless $color;
1040 $bg_color = 0 unless $bg_color;
1041 my $font = $self->mono_font;
1042 my $lineheight = $font->height;
1043 # $y1 -= $lineheight/2 - 3; ##################NOT SURE WHY THIS WAS HERE BEFORE
1044 my $strands = $self->option('strand') || 'auto';
1045  
1046 my ($forward,$reverse);
1047 if ($strands eq 'auto') {
1048 $forward = $feature->strand >= 0;
1049 $reverse = $feature->strand <= 0;
1050 } elsif ($strands eq 'both') {
1051 $forward = $reverse = 1;
1052 } elsif ($strands eq 'reverse') {
1053 $reverse = 1;
1054 } else {
1055 $forward = 1;
1056 }
1057 # minus strand features align right, not left
1058 $x1 += $pixels_per_base - $font->width - 1 if $strand < 0;
1059 for (my $i=0;$i<@bases;$i++) {
1060 my $x = $x1 + $i * $pixels_per_base;
1061
1062 my $x_next = $x + $pixels_per_base;
1063  
1064
1065 #draw background if DNA base aligns with reference (if ref given)
1066 $gd->filledRectangle($x+1, $y1, $x_next, $y2, $bg_color)
1067 if ( ($forward && $bases[$i] eq $refbases[$i]) ||
1068 ($reverse && $complement{$bases[$i]} eq $refbases[$i]) );
1069
1070 $gd->char($font,$x+2,$y1,$bases[$i],$color) if $forward;
1071 $gd->char($font,$x+2,$y1+($forward ? $lineheight:0),
1072 $complement{$bases[$i]}||$bases[$i],$color) if $reverse;
1073 }
1074 }
1075  
1076  
1077 1;
1078  
1079 __END__