blib/lib/Bio/Gonzales/Seq/LongestTranscript.pm untested

filter: Statementstmt 0.0% 0 / 125 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 194 CC 16 SCAR 30.4
CC
16
Cov
0%
run
42.0
feats
30.0
freeze_seqs
30.0
CRAP 21.0
1 package Bio::Gonzales::Seq::LongestTranscript;
2  
3 use Mouse;
4  
5 use warnings;
6 use strict;
7 use Carp;
8  
9 use 5.010;
10  
11 use Bio::Gonzales::Seq::IO qw/faiterate faspew/;
12 use Bio::Gonzales::Feat::IO::GFF3;
13 use List::MoreUtils qw/all any none/;
14  
15 our $VERSION = '0.062'; # VERSION
16  
17 has gff_file => ( is => 'rw', required => 1 );
18 has seq_files => ( is => 'rw', default => sub { [] } );
19 has _cached_feats => ( is => 'rw', default => sub { {} } );
20 has _cached_lengths => ( is => 'rw', default => sub { {} } );
21 has _cached_lt_feats => ( is => 'rw', default => sub { [] } );
22 has _ran => ( is => 'rw' );
23 has feat_type => ( is => 'rw', default => sub {qr/mRNA/} );
24 has feat_element => ( is => 'rw', default => sub {qr/exon/} );
25 has feat_source => ( is => 'rw' );
26  
27 sub BUILD {
28 my $self = shift;
29 my $args = shift;
30  
31 push @{ $self->seq_files }, $args->{seq_file}
32 if ( exists( $args->{seq_file} ) );
33  
34 confess "one or more supplied sequence files do not exist.\n" . join( "\n", @{ $self->seq_files } )
35 unless ( all { -f $_ } @{ $self->seq_files } );
36 }
37  
38 sub run {
39 my ($self) = @_;
40  
41 say STDERR "Using anntotation from " . $self->gff_file ;
42  
43 #cache type for frequent usage
44 my $type = $self->feat_type;
45 my $src = $self->feat_source;
46 my $element = $self->feat_element;
47  
48 my $gz = () = $self->gff_file =~ /\.gz$/;
49 my $gffio = Bio::Gonzales::Feat::IO::GFF3->new(
50 file => $self->gff_file,
51 gz => $gz,
52 record_filter => sub {
53 my $l = shift;
54 return if ( $src && $l !~ /\t$src\t/ );
55 return unless ( $l =~ /\t$type\t/ || $l =~ /\t$element\t/ );
56 return 1;
57 }
58 );
59  
60 #gene_id/parent_id => [ list of feats ]
61 my %splice_variants;
62 my %lengths;
63  
64 #iterate over the features and sort them into bins according to their parent id
65 while ( my $f = $gffio->next_feat ) {
66 next if ( $src && $f->source !~ /^$src$/ );
67  
68 # two steps:
69 # a. collect exonlengths for every mrna
70 # b. collect the parent gene id for every mrna
71  
72 #collect exons to determine the correct length w/o introns
73 if ( $f->type =~ /^$element$/ ) {
74 #add up exon lengths
75 $lengths{ $f->parent_id } += $f->length;
76 } elsif ( $f->type eq 'mRNA' ) {
77 $splice_variants{ $f->parent_id } //= [];
78 push @{ $splice_variants{ $f->parent_id } }, $f;
79 } else {
80 confess "Error, found: " . $f->type;
81 }
82 }
83  
84 $gffio->close;
85  
86 #build cache
87 $self->_cached_feats( \%splice_variants );
88 $self->_cached_lengths( \%lengths );
89 $self->_cached_lt_feats( [] );
90 $self->_ran(1);
91 say STDERR "Finished feat filtering";
92 }
93  
94 sub feats {
95 my ($self) = @_;
96  
97 #run if not run before
98 $self->run unless ( $self->_ran );
99  
100 #take advantage of the cache, if possible
101 return @{ $self->_cached_lt_feats }
102 if ( @{ $self->_cached_lt_feats } > 0 );
103  
104 my @lt_list;
105 my $feats = $self->_cached_feats;
106 my $lengths = $self->_cached_lengths;
107  
108 for my $flist ( values %$feats ) {
109  
110 #find the longest feature
111 my $longest_feat;
112  
113 for my $f (@$flist) {
114 confess "could not find length for " . $f->id unless ( exists( $lengths->{ $f->id } ) );
115 $longest_feat = $f
116 if ( !$longest_feat || $lengths->{ $f->id } > $lengths->{ $longest_feat->id } );
117 }
118  
119 # Bio::Gonzales::Feat
120 $longest_feat->add_attr( spliced_length => $lengths->{ $longest_feat->id } );
121 push @lt_list, $longest_feat;
122 }
123  
124 #cache the longest feats
125 $self->_cached_lt_feats( \@lt_list );
126  
127 return @lt_list;
128 }
129  
130 sub freeze_feats {
131 my ( $self, $file ) = @_;
132  
133 my $gffout = Bio::Gonzales::Feat::IO::GFF3->new( file => $file, mode => '>' );
134  
135 #write the features
136 map { $gffout->write_feat($_) } $self->feats;
137  
138 $gffout->close;
139  
140 return;
141 }
142  
143 sub freeze_ids {
144 my ( $self, $file ) = @_;
145  
146 yspew( $file, [ map { $_->id } $self->feats ] );
147 }
148  
149 sub freeze_seqs {
150 my ( $self, $file, $map ) = @_;
151  
152 #check if sequence file exists
153 croak "no sequence file supplied"
154 unless ( @{ $self->seq_files } > 0 && all { -f $_ } @{ $self->seq_files } );
155 say STDERR "Using sequences in: " . join( ", ", @{ $self->seq_files } ) . " out: $file";
156  
157 #a map with id => 1 for sequence filtering
158 my %lt_feats_ids;
159  
160 if ($map) {
161 #incorporate supplied feat_id - seq_id map
162 for my $f ( $self->feats ) {
163 croak "the supplied map doesn't cover all features: " . $f->id
164 unless ( exists( $map->{ $f->id } ) );
165  
166 $lt_feats_ids{ $map->{ $f->id } } = 1;
167 }
168 } else {
169 %lt_feats_ids = map { $_->id => 1 } $self->feats;
170 }
171  
172 my $fai = faiterate( $self->seq_files );
173 open my $faout_fh, '>', $file or confess "Can't open filehandle: $!";
174  
175 #write all seqs that are in the map
176 while ( my $s = $fai->() ) {
177 if ( exists( $lt_feats_ids{ $s->id } ) ) {
178 faspew( $faout_fh, $s );
179 }
180 }
181  
182 #close filehandle
183 $faout_fh->close;
184 }
185  
186 1;