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

filter: Statementstmt 0.0% 0 / 340 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 45 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 536 CC 54 SCAR 48.0
CC
54
Cov
0%
draw_component
420.0
trace_path
56.0
trace_data
56.0
CRAP 121.0
1 package Bio::Graphics::Glyph::trace;
2  
3 use strict;
4 use GD;
5 use Bio::SCF;
6 use File::Temp qw( tempdir );
7 use Digest::MD5 qw( md5_hex );
8 use base 'Bio::Graphics::Glyph::generic';
9 our @ISA;
10  
11 use constant VERTICAL_SPACING => 20;
12 my %complement = (
13 g => 'c',
14 a => 't',
15 t => 'a',
16 c => 'g',
17 n => 'n',
18 G => 'C',
19 A => 'T',
20 T => 'A',
21 C => 'G',
22 N => 'N'
23 );
24  
25 sub new {
26 my $self = shift->SUPER::new(@_);
27  
28 if ( $self->dna_fits ) {
29 $self->{parsed_trace} = $self->get_parsed_trace();
30 }
31 return $self;
32 }
33  
34 sub get_parsed_trace {
35 my $self = shift;
36 my ( $format, $trace_file ) = eval { $self->trace_data };
37 unless ($trace_file) {
38 warn $@ if $@;
39 return;
40 }
41  
42 if ($self->{'content_type'} eq "ABI"){
43 require ABI;
44 my $abi = ABI->new(-file=>$trace_file);
45 my %scf;
46 @{$scf{'bases'}} = split //, $abi->get_sequence();
47 @{$scf{'index'}} = $abi->get_base_calls();
48 @{$scf{'samples'}{'A'}} = $abi->get_trace('A');
49 @{$scf{'samples'}{'C'}} = $abi->get_trace('C');
50 @{$scf{'samples'}{'G'}} = $abi->get_trace('G');
51 @{$scf{'samples'}{'T'}} = $abi->get_trace('T');
52  
53 my $scale_factor = $self->option('abi_scale') || 1;
54 $self->{'max_trace'} = $scale_factor * $abi->get_max_trace();
55  
56 return \%scf;
57 }
58 else{
59 my %scf;
60 tie %scf, 'Bio::SCF', $trace_file;
61 $self->{'max_trace'} = 1600;
62 return \%scf;
63 }
64 }
65  
66 sub _guess_format {
67 my $self = shift;
68 my $path = shift;
69  
70 my $modded_path = $path;
71 $self->{'gzipped'} = ($modded_path =~ s/\.gz\s*$// );
72 if ($modded_path =~ /\.scf/){
73 $self->{'content_type'} = 'Bio::SCF';
74 }
75 elsif ($modded_path =~ /\.ab1/){
76 $self->{'content_type'} = 'ABI';
77 }
78 else{
79 die "$path: Trace file not of recognized format\n"
80 }
81  
82 return;
83 }
84  
85 sub trace_path {
86 my $self = shift;
87 my $feature = $self->feature or die "no feature!";
88 my $dirname = $self->trace_dir;
89 my $basename = $self->option('trace');
90  
91 # can't get it from callback, so try looking for an 'trace' attribute
92 if ( !$basename ) {
93 if ( $feature->can('attributes') ) {
94 ($basename) = $feature->attributes('trace');
95 }
96 elsif ( $feature->can('has_tag') && $feature->has_tag('trace') ) {
97 ($basename) = $feature->get_tag_values('trace');
98 }
99 }
100  
101 return unless $basename;
102 return $basename if $basename =~ m!^\w+:/!; # looks like a URL
103 return $basename if $basename =~ m!^/!; # looks like an abs path
104 return "$dirname/$basename";
105 }
106  
107 sub trace_data {
108 my $self = shift;
109 my $path = $self->trace_path;
110 $self->_guess_format($path);
111  
112 if ( $path =~ m!^\w+:/! ) { # looks like a URL
113 require LWP::UserAgent;
114 my $ua = LWP::UserAgent->new;
115 my $response = $ua->get($path);
116 if ( $response->is_success ) {
117  
118 # In the future, make extensible to ABI format
119 my $data = $response->content;
120 my $signature = md5_hex($data);
121 my $extension;
122 if ($self->{'content_type'} eq 'ABI'){
123 $extension = 'ab1';
124 }
125 else{
126 $extension = 'scf';
127 }
128 if ($self->{'gzipped'}){
129 $extension .= '.gz';
130 }
131  
132 # untaint signature for use in open
133 $signature =~ /^([0-9A-Fa-f]+)$/g or return;
134 $signature = $1;
135  
136 my $dir_path = tempdir();
137 my $file_name
138 = sprintf( "%s/%s.%s", $dir_path, $signature, $extension );
139 open( F, ">$file_name" )
140 || die("Can't open file $file_name for writing: $!\n");
141 binmode(F);
142 print F $data;
143 close F;
144
145 if ($self->{'gzipped'}){
146 $file_name = $self->gunzip_file( $file_name );
147 }
148  
149 return ( $self->{'content_type'}, $file_name );
150 }
151 else {
152 die $response->status_line;
153 }
154  
155 }
156 else {
157 if ($self->{'gzipped'}){
158 $path = $self->gunzip_file( $path );
159 }
160 return ( $self->{'content_type'}, $path );
161 }
162 }
163  
164 sub gunzip_file {
165 my $self = shift;
166 my $file_name = shift;
167  
168 $file_name =~ /(.+)\.gz$/;
169 my $new_file_name = $1;
170  
171 unless ( -e $new_file_name ){
172 `gunzip -c $file_name > $new_file_name`;
173 }
174  
175 return $new_file_name;
176 }
177  
178 sub trace_height {
179 my $self = shift;
180 return $self->{trace_height} if exists $self->{trace_height};
181 return $self->{trace_height} = $self->option('trace_height')
182 || 90; # what the factory says
183 }
184  
185 sub pad_left {
186 my $self = shift;
187 my $pad = $self->SUPER::pad_left;
188 if ( $self->dna_fits ) {
189 my $width_needed = ( 20 - $self->width ) / 2; #BF FIX ME
190 $pad = $pad > $width_needed ? $pad : $width_needed;
191 }
192 return $pad;
193 }
194  
195 sub pad_right {
196 my $self = shift;
197 my $pad = $self->SUPER::pad_right;
198  
199 if ( $self->dna_fits ) {
200 my $width_needed = ( 20 - $self->width ) / 2; #BF FIX ME
201 $pad = $pad > $width_needed ? $pad : $width_needed;
202 }
203 return $pad;
204 }
205  
206 sub pad_bottom {
207 my $self = shift;
208 my $pb = 0;
209 if ( $self->dna_fits ) {
210 $pb += $self->vertical_spacing;
211 $pb += $self->trace_height;
212 }
213 else{
214 $pb = $self->SUPER::pad_bottom;
215 }
216 return $pb;
217 }
218  
219 sub vertical_spacing {
220 my $self = shift;
221 my $vs = $self->option('vertical_spacing');
222 return $vs if defined $vs;
223 return VERTICAL_SPACING;
224 }
225  
226 sub draw_description {
227 my $self = shift;
228 my ( $gd, $left, $top, $partno, $total_parts ) = @_;
229  
230 $self->SUPER::draw_description( $gd, $left, $top, $partno, $total_parts );
231 }
232  
233 sub draw_label {
234 my $self = shift;
235 my ( $gd, $left, $top, $partno, $total_parts ) = @_;
236 $left += $self->pad_left;
237 $self->SUPER::draw_label( $gd, $left, $top, $partno, $total_parts );
238 }
239  
240 sub trace_dir {
241 my $self = shift;
242 return $self->option('trace_prefix');
243 }
244  
245 sub draw_component {
246 my $self = shift;
247 my $gd = shift;
248 my ( $x1, $y1, $x2, $y2 ) = $self->bounds(@_);
249  
250 # Draw the regular glyph
251 unless ( $self->dna_fits ) {
252 my $delegate = $self->option('glyph_delegate') || 'generic';
253 if ( $delegate eq 'generic' ) {
254 $self->SUPER::draw_component( $gd, @_ );
255 }
256 else {
257 eval "require Bio::Graphics::Glyph::$delegate";
258 local @ISA = ("Bio::Graphics::Glyph::$delegate");
259 my $method = "Bio::Graphics::Glyph::${delegate}::draw_component";
260 $self->$method( $gd, @_ );
261 }
262 return;
263 }
264  
265 # Draw Trace
266  
267 my $fgcolor = $self->fgcolor;
268 my $bgcolor = $self->bgcolor;
269  
270 my $parsed_trace = $self->{parsed_trace} or return;
271 my $feature = $self->feature;
272 my $pixels_per_base = $self->scale;
273 my $panel = $self->panel;
274 my $strand = $feature->strand;
275 my $forward = $self->{flip} ? ( $strand < 0 ) : ( $strand >= 0 );
276 my $flipped = $self->{flip};
277 my $opp_strand = $strand < 0;
278  
279 # Get Window Sequence Information
280 my $panel_start_base = $panel->offset + 1;
281 my $panel_end_base = $panel_start_base + $panel->length - 1;
282  
283 my ( $feature_display_start_base, $feature_display_end_base,
284 $feature_display_center_base );
285  
286 if ( $panel_start_base >= $feature->start() ) {
287 $feature_display_start_base = $panel_start_base;
288 }
289 else {
290 $feature_display_start_base = $feature->start();
291 }
292  
293 if ( $panel_end_base <= $feature->end() ) {
294 $feature_display_end_base = $panel_end_base;
295 }
296 else {
297 $feature_display_end_base = $feature->end();
298 }
299  
300 # We need to know if there are an even number of bases
301 # because the center is going to be off by a bit.
302 my $even_number_of_bases = 0;
303 $feature_display_center_base
304 = ( $feature_display_start_base + $feature_display_end_base ) / 2;
305 unless (
306 $feature_display_center_base == int($feature_display_center_base) )
307 {
308 $even_number_of_bases = 1;
309 $feature_display_center_base
310 = int( 0.5 + $feature_display_center_base );
311 }
312  
313 my $trace_glyph_top = $y1;
314 my $trace_glyph_bottom = $trace_glyph_top + $self->trace_height;
315  
316 # Find the Center for the Trace Glyph
317 my $trace_center_base_index =
318 ( !$opp_strand )
319 ? $feature_display_center_base - $feature->start
320 : $feature->end - $feature_display_center_base;
321  
322 my $trace_center_px = $self->panel->left
323 + $self->trace_map_pt($feature_display_center_base);
324  
325 # Center base test lines
326 #$gd->line( $trace_center_px, 0, $trace_center_px, 700, $fgcolor );
327 #if ( !$flipped ) {
328 # $gd->line(
329 # $trace_center_px + $pixels_per_base, 0,
330 # $trace_center_px + $pixels_per_base, 700,
331 # $self->factory->translate_color('red')
332 # );
333 #}
334 #else {
335 # $gd->line(
336 # $trace_center_px - $pixels_per_base, 0,
337 # $trace_center_px - $pixels_per_base, 700,
338 # $self->factory->translate_color('red')
339 # );
340 #}
341  
342 # Figure out the number of bases to display on each side
343 # with respect to the trace.
344 my $five_prime_bases
345 = $feature_display_center_base - $feature_display_start_base;
346 my $three_prime_bases
347 = $feature_display_end_base - $feature_display_center_base;
348 if ($opp_strand) {
349 ( $five_prime_bases, $three_prime_bases )
350 = ( $three_prime_bases, $five_prime_bases );
351 }
352  
353 # Work out the starting base on each side
354 my $trace_start_base_index = $trace_center_base_index - $five_prime_bases;
355 if ( $trace_start_base_index < 0 ) {
356 $five_prime_bases
357 += $trace_start_base_index; # trace_start_base_index is negative
358 $trace_start_base_index = 0;
359 }
360 my $trace_end_base_index = $trace_center_base_index + $three_prime_bases;
361 if ( $trace_end_base_index >= scalar @{ $parsed_trace->{bases} } ) {
362 $three_prime_bases
363 += scalar @{ $parsed_trace->{bases} } - $trace_end_base_index - 1;
364 $trace_end_base_index = scalar @{ $parsed_trace->{bases} } - 1;
365 }
366  
367 # Figure out the end points of the trace section
368 my ( $trace_left_px, $trace_right_px );
369 if ($forward) {
370 $trace_left_px
371 = $trace_center_px - ( ( $five_prime_bases * $pixels_per_base ) );
372 $trace_right_px
373 = $trace_center_px + ( ( $three_prime_bases * $pixels_per_base ) )
374 + $pixels_per_base;
375 }
376 else {
377 $trace_left_px = $trace_center_px
378 - ( ( $three_prime_bases * $pixels_per_base ) );
379 $trace_right_px
380 = $trace_center_px + ( ( $five_prime_bases * $pixels_per_base ) )
381 + $pixels_per_base;
382 }
383  
384 # Adjust for flipping
385 if ($flipped) {
386 $trace_left_px -= $pixels_per_base;
387 $trace_right_px -= $pixels_per_base;
388 }
389  
390 # Get Text Info
391 my $font = $self->mono_font;
392 my $text_buffer = 2;
393 my $text_height = $font->height + ( $text_buffer * 2 );
394  
395 my $trace_base_line = $trace_glyph_bottom - $text_height;
396 my $max_trace_val = $self->{'max_trace'} ;
397 my $vertical_scale
398 = ( $self->trace_height - $text_height - 2 ) / $max_trace_val;
399 my $total_trace_bases = $three_prime_bases + $five_prime_bases + 1;
400  
401 my $trace_start_sample
402 = int( $parsed_trace->{index}[$trace_start_base_index] );
403 my $trace_end_sample
404 = int( $parsed_trace->{index}[$trace_end_base_index] );
405 my $trace_center_sample
406 = $parsed_trace->{index}[$trace_center_base_index];
407  
408 my %base_colors = (
409 'A' => $self->factory->translate_color(
410 $self->option('a_color') || 'green'
411 ),
412 'C' => $self->factory->translate_color(
413 $self->option('c_color') || 'blue'
414 ),
415 'G' => $self->factory->translate_color(
416 $self->option('g_color') || 'black'
417 ),
418 'T' => $self->factory->translate_color(
419 $self->option('t_color') || 'red'
420 ),
421 );
422 my $current_px;
423 my $current_height;
424  
425 my $trace_center_base_px = $trace_center_px + int( $pixels_per_base / 2 );
426 if ($flipped) {
427 $trace_center_base_px -= $pixels_per_base;
428 }
429  
430 # Draw Trace
431 my $horizontal_scale_5p = ( $pixels_per_base
432 * ( $trace_center_base_index - $trace_start_base_index ) ) /
433 ( $parsed_trace->{index}[$trace_center_base_index]
434 - $parsed_trace->{index}[$trace_start_base_index] + 1 );
435 my $horizontal_scale_3p = ( $pixels_per_base
436 * ( $trace_end_base_index - $trace_center_base_index ) ) /
437 ( $parsed_trace->{index}[$trace_end_base_index]
438 - $parsed_trace->{index}[$trace_center_base_index] + 1 );
439 $trace_start_sample
440 -= int( ( $pixels_per_base / 2 ) / $horizontal_scale_5p )
441 if $horizontal_scale_5p;
442 $trace_end_sample
443 += int( ( $pixels_per_base / 2 ) / $horizontal_scale_3p )
444 if $horizontal_scale_3p;
445 my $last_px = $self->_get_pixel_position_x(
446 current_sample => $trace_start_sample,
447 trace_center_sample => $trace_center_sample,
448 forward => $forward,
449 trace_center_base_px => $trace_center_base_px,
450 horizontal_scale_5p => $horizontal_scale_5p,
451 horizontal_scale_3p => $horizontal_scale_3p,
452 );
453 my %last_heights;
454 foreach my $base ( keys %base_colors ) {
455 $last_heights{$base} = int(
456 $trace_base_line - (
457 $vertical_scale
458 * $parsed_trace->{samples}{$base}[$trace_start_sample]
459 )
460 );
461 }
462 my $passed_center = 0;
463 my $horizontal_scale = $horizontal_scale_5p;
464 for (
465 my $current_sample = $trace_start_sample + 1;
466 $current_sample <= $trace_end_sample;
467 $current_sample++
468 )
469 {
470 $current_px = $self->_get_pixel_position_x(
471 current_sample => $current_sample,
472 trace_center_sample => $trace_center_sample,
473 forward => $forward,
474 trace_center_base_px => $trace_center_base_px,
475 horizontal_scale_5p => $horizontal_scale_5p,
476 horizontal_scale_3p => $horizontal_scale_3p,
477 );
478 $self->_draw_trace_sample(
479 gd => $gd,
480 last_px => $last_px,
481 current_px => $current_px,
482 current_sample => $current_sample,
483 last_heights => \%last_heights,
484 base_colors => \%base_colors,
485 parsed_trace => $parsed_trace,
486 vertical_scale => $vertical_scale,
487 trace_base_line => $trace_base_line,
488 forward => $forward,
489 );
490 $last_px = $current_px;
491 }
492  
493 # Print Trace Sequence
494 my $base_count = 0;
495 my $seq_y = $trace_glyph_bottom - $text_height + $text_buffer;
496 for (
497 my $base_index = $trace_start_base_index;
498 $base_index <= $trace_end_base_index;
499 $base_index++
500 )
501 {
502  
503 if ($forward) {
504 my $x;
505 if ($opp_strand) {
506 $x = $trace_left_px + ( $base_count * $pixels_per_base )
507 + $pixels_per_base - $font->width - 1;
508 }
509 else {
510 $x = $trace_left_px + $base_count * $pixels_per_base;
511 }
512 my $base = $parsed_trace->{bases}[$base_index];
513 my $color = $base_colors{$base} || $fgcolor;
514 $gd->char( $font, $x + 2, $seq_y, $base, $color );
515 }
516 else {
517 my $x;
518 if ($opp_strand) {
519 $x = $trace_right_px
520 - ( ( $base_count + 1 ) * $pixels_per_base );
521 }
522 else {
523 $x = $trace_right_px
524 - ( ( $base_count + 1 ) * $pixels_per_base )
525 + $pixels_per_base - $font->width - 1;
526 }
527 my $base = $parsed_trace->{bases}[$base_index];
528 $base = $complement{$base} || $base;
529 my $color = $base_colors{$base} || $fgcolor;
530 $gd->char( $font, $x + 2, $seq_y, $base, $color );
531  
532 }
533  
534 $base_count++;
535 }
536  
537 if ( $self->option('show_border') ) {
538 # Outline Box
539 # left side
540 $gd->line(
541 $trace_left_px, $trace_glyph_top, $trace_left_px,
542 $trace_glyph_bottom, $fgcolor
543 );
544  
545 # right side
546 $gd->line(
547 $trace_right_px, $trace_glyph_top, $trace_right_px,
548 $trace_glyph_bottom, $fgcolor
549 );
550  
551 # top
552 $gd->line(
553 $trace_left_px, $trace_glyph_top, $trace_right_px,
554 $trace_glyph_top, $fgcolor
555 );
556  
557 # bottom
558 $gd->line(
559 $trace_left_px, $trace_glyph_bottom, $trace_right_px,
560 $trace_glyph_bottom, $fgcolor
561 );
562 }
563  
564 }
565  
566 sub _get_pixel_position_x {
567 my $self = shift;
568 my %args = @_;
569 my $current_sample = $args{'current_sample'};
570 my $trace_center_sample = $args{'trace_center_sample'};
571 my $forward = $args{'forward'};
572 my $trace_center_base_px = $args{'trace_center_base_px'};
573 my $horizontal_scale_5p = $args{'horizontal_scale_5p'};
574 my $horizontal_scale_3p = $args{'horizontal_scale_3p'};
575  
576 my $horizontal_scale;
577 if ( $current_sample >= $trace_center_sample ) {
578 $horizontal_scale = $horizontal_scale_3p;
579 }
580 else {
581 $horizontal_scale = $horizontal_scale_5p;
582 }
583  
584 if ($forward) {
585 return $trace_center_base_px +
586 int(
587 $horizontal_scale * ( $current_sample - $trace_center_sample ) );
588 }
589 else {
590 return $trace_center_base_px -
591 int(
592 $horizontal_scale * ( $current_sample - $trace_center_sample ) );
593 }
594  
595 }
596  
597 sub _draw_trace_sample {
598 my $self = shift;
599 my %args = @_;
600 my $gd = $args{'gd'};
601 my $last_px = $args{'last_px'};
602 my $current_px = $args{'current_px'};
603 my $current_sample = $args{'current_sample'};
604 my $last_heights = $args{'last_heights'};
605 my $base_colors = $args{'base_colors'};
606 my $parsed_trace = $args{'parsed_trace'};
607 my $vertical_scale = $args{'vertical_scale'};
608 my $trace_base_line = $args{'trace_base_line'};
609 my $forward = $args{'forward'};
610  
611 my $current_height;
612 foreach my $base ( keys %{ $base_colors || {} } ) {
613 if ( $current_sample < 0
614 or $current_sample
615 >= scalar @{ $parsed_trace->{samples}{$base} } )
616 {
617  
618 # Off the end of the trace
619 $current_height = 0;
620 }
621 else {
622 $current_height = int(
623 $trace_base_line - (
624 $vertical_scale
625 * $parsed_trace->{samples}{$base}[$current_sample]
626 )
627 );
628 }
629 my $color =
630 ($forward)
631 ? $base_colors->{$base}
632 : $base_colors->{ $complement{$base} || $base };
633 $gd->line( $last_px, $last_heights->{$base},
634 $current_px, $current_height, $color );
635  
636 $last_heights->{$base} = $current_height;
637 }
638 }
639  
640 sub trace_map_pt {
641 my $self = shift;
642 my $panel = $self->panel;
643 my $offset = $panel->{offset};
644 my $scale = $panel->{scale} || $panel->scale;
645 my $pl = $panel->{pad_left};
646 my $width = $panel->{width};
647 my $flip = $panel->{flip};
648 my $length = $panel->{length};
649 my @result;
650  
651 foreach (@_) {
652 my $val = $flip
653  
654 #? int (0.5 + $self->{width} - ($length - ($_- 1)) * $scale)
655 ? int( 0.5 + $width - ( $_ - $offset - 1 ) * $scale )
656 : int( 0.5 + ( $_ - $offset - 1 ) * $scale );
657 $val = -1 if $val < 0;
658 $val = $width + 1 if $val > $width;
659 push @result, $val;
660 }
661 return (wantarray) ? @result : $result[0];
662 }
663  
664 1;
665  
666 __END__