« blib/lib/Bio/Graphics/Browser2/DataLoader/generic.pm
↑ Summary
blib/lib/Bio/Graphics/Browser2/Render/HTML.pm »
| 1 | package Bio::Graphics::Browser2::TrackDumper::RichSeqMaker; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Bio::Seq::RichSeq; | ||
| 5 | use Bio::PrimarySeq; | ||
| 6 | use Bio::SeqFeature::Generic; | ||
| 7 | use Bio::Location::Simple; | ||
| 8 | use Bio::Location::Split; | ||
| 9 | use Bio::Location::Fuzzy; | ||
| 10 | use POSIX qw(strftime); | ||
| 11 | |||
| 12 | sub stream_to_rich_seq { | ||
| 13 | my $self = shift; | ||
| 14 | my ($segment,$stream) = @_; | ||
| 15 | |||
| 16 | my $seq = new Bio::Seq::RichSeq(-display_id => $segment->display_id, | ||
| 17 | -desc => $segment->desc, | ||
| 18 | -accession_number => $segment->accession_number, | ||
| 19 | -alphabet => $segment->alphabet || 'dna', | ||
| 20 | ); | ||
| 21 | $seq->add_date(strftime("%d-%b-%Y",localtime)); | ||
| 22 | my $ps = $segment->primary_seq; | ||
| 23 | |||
| 24 | # not sure if the following workaround really necessary | ||
| 25 | if ($ps->isa('Bio::PrimarySeq')) { | ||
| 26 | $seq->primary_seq($ps); | ||
| 27 | } elsif (ref $ps && $ps->can('seq')) { | ||
| 28 | $seq->primary_seq($ps->seq) | ||
| 29 | } else { | ||
| 30 | $seq->primary_seq(Bio::PrimarySeq->new(-seq=>$ps)); | ||
| 31 | } | ||
| 32 | |||
| 33 | $segment->absolute(1); | ||
| 34 | my $offset = $segment->start - 1; | ||
| 35 | my $segmentend = $segment->length; | ||
| 36 | |||
| 37 | while (my $feature = $stream->next_seq) { | ||
| 38 | $self->_add_feature($seq,$feature,$offset,$segmentend); | ||
| 39 | } | ||
| 40 | return $seq; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub _add_feature { | ||
| 44 | my $self = shift; | ||
| 45 | my ($seq,$feature,$offset,$length,$parent_type,$parent_id) = @_; | ||
| 46 | |||
| 47 | my $score = $feature->score; | ||
| 48 | $score = ref $score eq 'HASH' ? $score->{sumData}/$score->{validCount} : $score; | ||
| 49 | my $id = $feature->display_name || eval {($feature->get_tag_values('load_id'))[0]}|| $feature->primary_id ; | ||
| 50 | |||
| 51 | my $bsg = Bio::SeqFeature::Generic->new(-primary_tag => $feature->primary_tag, | ||
| 52 | -source_tag => $feature->source_tag, | ||
| 53 | -frame => eval{$feature->phase}||eval{$feature->frame}||undef, | ||
| 54 | -score => $score, | ||
| 55 | ); | ||
| 56 | for my $tag ( $feature->get_all_tags ) { | ||
| 57 | next if $tag =~ /^(load_id|parent_id)$/; | ||
| 58 | my %seen; | ||
| 59 | $bsg->add_tag_value($tag, grep { ! $seen{$_}++ } | ||
| 60 | grep { defined } $feature->get_tag_values($tag)); | ||
| 61 | } | ||
| 62 | if ($parent_type && $parent_id) { | ||
| 63 | $bsg->add_tag_value($parent_type=>$parent_id) if $parent_id; | ||
| 64 | } | ||
| 65 | |||
| 66 | $bsg->add_tag_value('name',$id) if $id; | ||
| 67 | |||
| 68 | my @subf = $feature->get_SeqFeatures; | ||
| 69 | my @loc; | ||
| 70 | |||
| 71 | # this detects the case in which a feature is "parent" to its subparts, | ||
| 72 | # such as CDS => CDS1..CDS2..CDS3 | ||
| 73 | my %subftypes = map {$_->primary_tag=>1} @subf; | ||
| 74 | if (keys %subftypes == 1 && $subftypes{$feature->primary_tag}) { | ||
| 75 | @loc = $feature->each_Location; | ||
| 76 | @subf = (); | ||
| 77 | } else { | ||
| 78 | @loc = Bio::Location::Simple->new(-start=>$feature->start, | ||
| 79 | -end =>$feature->end); | ||
| 80 | } | ||
| 81 | |||
| 82 | my $location = Bio::Location::Split->new; | ||
| 83 | for my $sl (@loc) { | ||
| 84 | my $start = $sl->start - $offset; | ||
| 85 | my $end = $sl->end - $offset; | ||
| 86 | next if $start < 1 && $end < 1; | ||
| 87 | next if $start > length && $end > $length; | ||
| 88 | my $fuzzy; | ||
| 89 | if ($start < 1) { | ||
| 90 | $fuzzy++; | ||
| 91 | $start = "<1"; | ||
| 92 | } | ||
| 93 | if ($end > $length) { | ||
| 94 | $fuzzy++; | ||
| 95 | $end = ">$length"; | ||
| 96 | } | ||
| 97 | my $loc = $fuzzy ? Bio::Location::Fuzzy->new(-start => $start, | ||
| 98 | -end => $end, | ||
| 99 | -strand => $feature->strand, | ||
| 100 | -location_type => '..') | ||
| 101 | : Bio::Location::Simple->new(-start => $start, | ||
| 102 | -end => $end, | ||
| 103 | -strand => $feature->strand); | ||
| 104 | $location->add_sub_Location($loc); | ||
| 105 | } | ||
| 106 | |||
| 107 | return unless $location->sub_Location; | ||
| 108 | $bsg->location($location); | ||
| 109 | $seq->add_SeqFeature($bsg); | ||
| 110 | $self->_add_feature($seq,$_,$offset,$length,$feature->primary_tag,$id) | ||
| 111 | foreach @subf; | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; |