File: FeatureDecor.pm

package info (click to toggle)
libchado-perl 1.23-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,976 kB
  • ctags: 10,378
  • sloc: xml: 192,540; sql: 165,945; perl: 28,339; sh: 101; python: 73; makefile: 46
file content (448 lines) | stat: -rw-r--r-- 14,308 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package SOI::FeatureDecor;

=head1 NAME

SOI::FeatureDecor

=head1 SYNOPSIS

=head1 USAGE

=cut

use Exporter;

use SOI::Feature;
use SOI::Visitor;
use Bio::SeqFeatureI;
use Bio::Graphics;
use Carp;
use base qw(Exporter SOI::Feature Bio::SeqFeatureI);
use vars qw($AUTOLOAD);

#@EXPORT_OK = qw();
%EXPORT_TAGS = (all=> [@EXPORT_OK]);

use strict;

=head1 FUNCTIONS

=cut


sub new {
    my $proto = shift; my $class = ref($proto) || $proto;;
    my $self = {};
    bless $self, $class;

    $self->feature(@_);
    return $self;
}

sub feature {
    my $self = shift;
    my $f = shift;
    if ($f) {
        confess("must be SOI::Feature") unless ($f->isa("SOI::Feature"));
        map{
            map{
                SOI::Visitor->set_loc($_);
                $_->_setup_coord;
            }@{$_->nodes || []};
        }grep{$_->type eq 'companalysis'}@{$f->nodes || []};
        #$f->_setup_coord;
        $self->_morph($f);
        $self->{_feature} = $f;
    }
    return $self->{_feature};
}
sub _morph {
    my $self = shift;
    my $f = shift;
    my $class = ref($self);
    bless($f, $class);
    $self->_morph2($f);
    map{$self->_morph($_)}@{$f->nodes || []};
    return $f;
}
sub _morph2 {
    my $self = shift;
    my $f = shift;
    my $class = ref($self);
    map{
        my $sec = $_;
        my $sseq = $sec->sseq;
        if ($sseq && $sseq->isa("SOI::Feature")) {
            bless($sseq, $class);
        }
        bless($sec, $class);
    }@{$f->secondary_nodes || []};
}

sub make_panel {
    my $self = shift;
    my $options = shift;

    my $feature = $self->feature;
    confess("no feature set and nothing to make from") unless ($feature);

    my ($contig) = grep{$_->type =~ /contig/}@{$feature->nodes || []};
    unless ($contig) {#not mini-view arm feature
        $contig = SOI::Feature->new
          ({name=>'fake',src_seq=>$feature->src_seq,fmin=>$feature->fmin,fmax=>$feature->fmax,strand=>1});
        $self->_morph($contig);
        $feature->transform($contig);
    }
    my $line = SOI::Feature->new({src=>$contig->src_seq,fmin=>0,fmax=>$contig->length,strand=>1});
    $self->_morph($line);

    my $len = $line->length;

    my %keyed_h = ();
    my $panel =
      Bio::Graphics::Panel->new
          (
           -offset => $options->{offset} || 0,
           -length=>$len,
           -width  => $options->{width} || 1000,
           -pad_left  => $options->{pad_left} || 50,
           -pad_right  => $options->{pad_right} || 50,
           -key_style  => $options->{key_style},
          );

    my @all_trs = ();
    foreach my $gene (grep{$_->type eq 'gene'}@{$feature->nodes || []}) {
        my @trs = @{$gene->nodes || []}; #all immediate children of gene
        push(@all_trs, @trs);
    }

    my @tes = ();
    @all_trs = 
      grep {
          if ($_->type eq 'transposable_element') {
              push(@tes, $_);
              0;
          }
          else {
              1;
          }
      } @all_trs;

    unless (@all_trs) {#not mini-view feature?
        push @all_trs, grep{$_}@{$feature->nodes || []};
    }

    my @analyses = grep{$_->type eq 'companalysis'}@{$feature->nodes || []};


    ####################################### TO DO #####################################
    #   analysis glyph type and color need more work, better default and optionally set
    #   and group analysis by type?
    ###################################################################################

    my %an_color = ();
    my @color_names = ();
    map {
        push @color_names, $_
          if $_ ne 'yellow' && $_ ne 'steelblue'
            && $_ ne 'white' && $_ ne 'darkred'
              && $_ ne 'honeydew' && $_ ne 'red'
          }$panel->color_names;
    $an_color{'clonelocator:scaffoldBACs'} = 'honeydew';
    $an_color{'clonelocator:BACs'} = 'honeydew';
    $an_color{'tilingpath BAC'} = 'magenta'; # 'pink'; #'honeydew';
    $an_color{'P Insertion'} = 'darkturquoise'; # 'turquoise'; #'red';
    $an_color{'cDNA'} = 'mediumseagreen'; #'green';
    $an_color{'Your BLAST hit'} = 'darkred';
    $an_color{'Repeat'} = 'purple';

    my $track = 0;
    my $middle_track;
    foreach my $strand (+1, -1) {
        my $meth = "unshift_track";
        if ($strand == 1) {
            $meth = "unshift_track";
        }
        $meth = "add_track";
        if ($strand == -1) {
            # Add the scale in the middle using the "arrow" glyph
            $panel->$meth(arrow => [$line],
                          -bump => 0,
                          -tick=>1);#1->major tick, 2->major + minor ticks

            $middle_track = $track;
            $track++;
        }
        # draw analyses
        my $an_height = $options->{analysis_height} || 12;
        foreach my $analysis (@analyses) {
            my $ap = $analysis->program . ":" . $analysis->sourcename;
            my %uniquetypes =
              map {s/_/ /g;$_=>1}($analysis->get_property("type"));
            #$ap = join(" ",grep {$_} keys %uniquetypes) || $ap;

            my $an_type = $ap;
            my $color = $an_color{$an_type};

            ## hard-coded color designations for evidence

            # blast
            if (lc($analysis->program) =~ /blastx/) {
                $color = 'darkorange';
            }
            if (lc($analysis->program) =~ /sptr/) {
                $color = 'darkorange';
            }

            # cDNA, DGC
            if (lc($analysis->sourcename) =~ /na_dgc/) {
                $color = 'mediumseagreen';
            }
            if (lc($analysis->sourcename) =~ /na_cdna/) {
                $color = 'green';
            }
            if (lc($analysis->sourcename) =~ /na_users/) {
                $color = 'green';
            }
            if (lc($analysis->database) =~ /na_gb/) {
                $color = 'green';
            }

            # tiling BACs
            if (lc($analysis->sourcename) =~ /bac/) {
                $color = 'lightslategray';
            }
            if (lc($analysis->sourcename) =~ /clone/) {
                $color = 'lightslategray';
            }
            if (lc($analysis->sourcename) =~ /all_nr/) {
                $color = 'black';
            }

            # P elements
            if (lc($analysis->sourcename) =~ /na_pe.dros/) {
                $color = 'darkturquoise';
            }

            # ESTs
            if (lc($analysis->database) =~ /est/) {
                $color = 'mediumaquamarine';
            }

            # gene prediction
            if (lc($analysis->program) =~ /genscan/) {
                $color = 'lightskyblue';
            }
            if (lc($analysis->program) =~ /genie/) {
                $color = 'lightskyblue';
            }
            if (lc($analysis->program) =~ /trnascan/) {
                $color = 'lightskyblue';
            }

            # affy oligo
            if (lc($analysis->database) =~ /na_affy_oligo.dros/) {
                $color = 'crimson';
            }

            if ($options->{colormap}) {
                my $cmap = $options->{colormap};
                my $c = $cmap->{$analysis->program .':'. $analysis->sourcename};
                if (!$c) {
                    $c = $cmap->{$analysis->sourcename};
                }
                if (!$c) {
                    $c = $cmap->{$analysis->program};
                }
                if ($c) {
                    $color = $c;
                }
            }

            my @rsets = @{$analysis->nodes || []};
            my ($bump, $labelling) = (0, 0);
            if (defined($options->{bump_analyses})) {
                $bump = $options->{bump_analyses};
            }
            $labelling = ($an_type =~ /scaffold bac/i) ? 0
              : ($options->{label_analyses} || 0);
            if ($analysis->program eq "assembly" ||
                $analysis->program eq "gulliver") {
                $bump = 1;
                $labelling = 
                  defined($options->{label_assembly}) ? 
                    $options->{label_assembly} : 1;
            }
            my @todraw = grep {$_->strand == $strand} @rsets;

            my $max = $options->{max_results_per_tier};
            if (defined($max) && 
                @rsets > $max) {
                $bump = 0;
                $an_type .= " (too many results - tier collapsed)";
            }
            if (@todraw) {
                $panel->$meth([@todraw]
                              =>$self->glyph_type($analysis),
                              -bgcolor =>  $color,
                              -fillcolor=> $color,
                              -fgcolor   =>  'black',
                              -bump      => $bump,
                              -key => ($keyed_h{$an_type} ? '' : $an_type),
                              -height    => $an_height,
                              -label     => $labelling
                             );
                $keyed_h{$an_type}++;
                $track++;
            }
        }
        # draw genes: actually transcript
        my $gene_height = $options->{gene_height} || 10;
        my @trs = grep {$_->strand == $strand} @all_trs;
        if (@trs) {
            $panel->$meth([@trs]
                          =>'transcript',
                          -fillcolor => 'blue',
                          -bgcolor   => 'blue',
                          -fgcolor   =>  'black',
                          -bump      =>  1,
                          -key =>  ($keyed_h{annotation} ? '' : 'annotation'),
                          -mark_cds => 1,
                          -height    => $gene_height,
                          -label     => $options->{label_annotations} || 1,
                         );
            $keyed_h{annotation}++;
            $track++;
        }
        if (@tes) {
            $panel->$meth([grep {$_->strand == $strand} @tes] 
                          =>'transcript',
                          -fillcolor => 'red',
                          -bgcolor   => 'red',
                          -fgcolor   =>  'black',
                          -bump      =>  1,
                          -key => ($keyed_h{te} ? '' : 'transposable element'),
                          -mark_cds => 1,
                          -height    => $gene_height,
                          -label     => $options->{label_annotations} || 1,
                         );
            $keyed_h{te}++;
            $track++;
        }
        # draw overlapping segments
        my @segs = grep{$_->type eq 'golden_path_scaffold'}@{$feature->nodes || []};
        if (@segs) {
            $panel->$meth([grep {$_->strand == $strand} @segs]=>'arrow',
                          -fillcolor => 'yellow',
                          -bgcolor => 'yellow',
                          -fgcolor =>   'black',
                          -bump =>      1,
                          -key =>       ($keyed_h{segment} ? '' : 'segment'),
                          -height =>    10,
                          -label =>     defined($options->{label_segments}) ? $options->{label_segments} : 1,
                         );
            $keyed_h{segment}++;
            $track++;
        }
    }
    # Panel object has no concept of being seperated into
    # 2 strands; lets do a vertical reverse on the negative strand
    my @rev = splice(@{$panel->{tracks}}, $middle_track+1);
    push(@{$panel->{tracks}}, reverse @rev);

    # imagemap
    my $map = "";
    my @boxes = $panel->boxes;
    foreach my $box (@boxes) {
        my $f = shift @$box;
        my $coords = join(" ", @$box);
        my $alt_text = scalar(@{$f->secondary_locations || []}) ? $f->secondary_loc->src : $f->name;
        $alt_text =~ s/[\"\']//g;
        my $href = $f->{url} || "#";
	
        $map .= 
          qq[<area shape="rect"
	     coords="$coords" 
	     href=$href
	     onmouseover='window.status = "$alt_text"; return true;'
	     onmouseexit='window.status = ""; return true;'>
	    ];
    }
    $panel->{maptext} = $map;

    return $panel;
}

sub glyph_type {
    my $self = shift;
    my $an = shift;
    if (grep{lc($an->program) =~ /$_/i}('blastn', 'pinsertion') and
        !$an->sourcename) {
        return 'pinsertion';
    } elsif (lc($an->program) eq 'blastn' &&
             lc($an->sourcename) eq 'na_pe.dros') {
        return 'pinsertion';
    } elsif (lc($an->program) eq "clonelocator") {
        return 'arrow';
    } elsif ($an->sourcename =~ /(dgc|cdna|est)/i) {
        return "bdgp_ests";
    } else {
        return 'segments';
    }
}


#conform to Bio::FeatureI or Gadfly API for drawing
sub seq_id {return shift->src}
sub display_name {
    my $self = shift;
    return $self->name || $self->uniquename;
}
#only for drawing, mixed type and one of them span whole parent range?
sub sub_SeqFeature {
    return grep{$_->type !~ 'protein' && $_->type !~ 'polypeptide'
                  && $_->type !~ /cds/i && $_->type !~ /intron/i
              }@{shift->nodes || []};
}
sub homol_sf {
    my $self = shift;
    if (@{$self->secondary_nodes || []}) {
        #check rank?
        my $homol = $self->secondary_nodes->[0];
        $homol->src_seq(SOI::Feature->new({name=>$homol->src_seq}));
        return $homol;
    }
}
sub start_codon {
    my $self = shift;

    my ($sc) = grep{$_->type eq 'start_codon'}@{$self->nodes || []};
    unless ($sc) {
        my ($p)= grep{$_->type =~ /protein/i or $_->type =~ /polypeptdie/}@{$self->nodes || []};
        return unless ($p);
        #no junction in start/stop codon?
        my $fmin = ($p->strand > 0)?$p->fmin:$p->fmax;
        $sc = SOI::Feature->new({src_seq=>$p->src_seq,fmin=>$fmin,fmax=>$fmin+3,strand=>$p->strand});
    }
    $sc->_setup_coord;
    $self->_morph($sc);
    return $sc;
}
sub stop_codon {
    my $self = shift;

    my ($sc) = grep{$_->type eq 'stop_codon'}@{$self->nodes || []};
    unless ($sc) {
        my ($p)= grep{$_->type =~ /protein/i or $_->type =~ /polypeptdie/}@{$self->nodes || []};
        return unless ($p);
        #no junction in start/stop codon? and protein feature end at stop codon start?
        my $fmin = ($p->strand > 0)?$p->fmax:$p->fmin;
        $sc = SOI::Feature->new({src_seq=>$p->src_seq,fmin=>$fmin,fmax=>$fmin+3,strand=>$p->strand});
    }
    $sc->_setup_coord;
    $self->_morph($sc);
    return $sc;
}
sub stop {shift->end}

1;