GenOO/GeneCollection/Factory/GTF.pm

filter: Statementstmt 97.5% 80 / 82 Branchbran 81.2% 26 / 32 Conditioncond 41.6% 10 / 24 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 79.5% 128 / 161 CC 28 SCAR 32.8
CC
28
Cov
84%
_read_gtf
34.8
BEGIN
1.0
BEGIN
1.0
CRAP 26.6
↑ Summary GenOO/Data/File/BED/Record.pm »
1 # POD documentation - main docs before the code
2  
3 =head1 NAME
4  
5 GenOO::GeneCollection::Factory::GTF - Factory to create GeneCollection from a GTF file
6  
7 =head1 SYNOPSIS
8  
9 Creates GenOO::GeneCollection containing genes from a GTF file
10 Preferably use it through the generic GenOO::GeneCollection::Factory
11  
12 my $factory = GenOO::GeneCollection::Factory->new('GTF',{
13 file => 'sample.gtf'
14 });
15  
16 =head1 DESCRIPTION
17  
18 # An instance of this class is a concrete factory for the creation of a
19 # L<GenOO::GeneCollection> containing genes from a GTF file. It offers the
20 # method "read_collection" (as the consumed role requires) which returns
21 # the actual L<GenOO::GeneCollection> object in the form of
22 # L<GenOO::RegionCollection::Type::DoubleHashArray>. The latter is the
23 # implementation
24 # of the L<GenOO::RegionCollection> class based on the complex data
25 # structure L<GenOO::Data::Structure::DoubleHashArray>.
26  
27 =head1 EXAMPLES
28  
29 # Create a concrete factory
30 my $factory_implementation = GenOO::GeneCollection::Factory->new('GTF',{
31 file => 'sample.gtf'
32 });
33  
34 # Return the actual GenOO::GeneCollection object
35 my $collection = $factory_implementation->read_collection;
36 print ref($collection) # GenOO::GeneCollection::Type::DoubleHashArray
37  
38 =cut
39  
40 # Let the code begin...
41  
42 package GenOO::GeneCollection::Factory::GTF;
43 1 $GenOO::GeneCollection::Factory::GTF::VERSION = '1.5.0';
44  
45 #######################################################################
46 ####################### Load External modules #####################
47 #######################################################################
48 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53  
54 #######################################################################
55 ####################### Load GenOO modules #####################
56 #######################################################################
57 1 ▶use GenOO::RegionCollection::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use GenOO::Transcript;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use GenOO::Gene;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use GenOO::Data::File::GFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62  
63 #######################################################################
64 ####################### Interface attributes ######################
65 #######################################################################
66 1 has 'file' => (
67 isa => 'Maybe[Str]',
68 is => 'ro'
69 );
70  
71  
72 #######################################################################
73 ########################## Consumed Roles #########################
74 #######################################################################
75 1 with 'GenOO::RegionCollection::Factory::Requires';
76  
77  
78 #######################################################################
79 ######################## Interface Methods ########################
80 #######################################################################
81 sub read_collection {
82 6 ▶ my ($self) = @_;
Subroutine read_collection: called CC=1 SCAR=0.0
Pod undocumented
83  
84 6 my $genes = $self->_read_gtf($self->file);
85  
86 6 return GenOO::RegionCollection::Factory->create('RegionArray', {
87 array => $genes
88 })->read_collection;
89 }
90  
91 #######################################################################
92 ######################### Private methods ##########################
93 #######################################################################
94 sub _read_gtf {
95 6 ▶ my ($self, $file)=@_;
Subroutine _read_gtf: called CC=28 SCAR=35.5
96  
97 6 my %transcripts;
98 my %transcript_splice_starts;
99 6 my %transcript_splice_stops;
100 6 my %genes;
101  
102 6 my $gff = GenOO::Data::File::GFF->new(file => $file);
103  
104 6 ▶ while (my $record = $gff->next_record){
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $gff->next_record) 5808 6 5814
105 5808 ▶ my $tid = $record->attribute('transcript_id')
Branch: 1 branch
BranchTrueFalseTotal
my $tid = $record->attribute("transcript_id") or die "transcript_id attribute must be defined\n" 0 5808 5808
106 or die "transcript_id attribute must be defined\n";
107 5808 ▶ my $gid = $record->attribute('gene_id')
Branch: 1 branch
BranchTrueFalseTotal
my $gid = $record->attribute("gene_id") or die "gene_id attribute must be defined\n" 0 5808 5808
108 or die "gene_id attribute must be defined\n";
109  
110 5808 ▶ if ($record->strand == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($record->strand == 0) 0 5808 5808
111 0 next;
112 }
113  
114 # Get transcript with id or create a new one. Update coordinates if required
115 5808 my $transcript = $transcripts{$tid};
116 5808 ▶ if (not defined $transcript) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $transcript) { } 348 5460 5808
117 348 $transcript = GenOO::Transcript->new(
118 id => $tid,
119 chromosome => $record->rname,
120 strand => $record->strand,
121 start => $record->start,
122 stop => $record->stop,
123 splice_starts => [$record->start], # will be re-written later
124 splice_stops => [$record->stop], # will be re-written later
125 );
126 348 $transcripts{$tid} = $transcript;
127 348 $transcript_splice_starts{$tid} = [];
128 348 $transcript_splice_stops{$tid} = [];
129 348 ▶ if (!exists $genes{$gid}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $genes{$gid}) 156 192 348
130 156 $genes{$gid} = [];
131 }
132 348 push @{$genes{$gid}}, $transcript;
133 }
134 else {
135 5460 ▶ $transcript->start($record->start) if ($record->start < $transcript->start);
Branch: 1 branch
BranchTrueFalseTotal
if $record->start < $transcript->start 180 5280 5460
136 5460 ▶ $transcript->stop($record->stop) if ($record->stop > $transcript->stop);
Branch: 1 branch
BranchTrueFalseTotal
if $record->stop > $transcript->stop 3342 2118 5460
137 }
138  
139 5808 ▶ if ($record->feature eq 'exon') {
Branch: 3 branches
BranchTrueFalseTotal
if ($record->feature eq 'exon') { } 2982 2826 5808
elsif ($record->feature eq 'start_codon') { } 270 2556 2826
elsif ($record->feature eq 'stop_codon') { } 270 2286 2556
140 2982 push @{$transcript_splice_starts{$tid}}, $record->start;
141 2982 push @{$transcript_splice_stops{$tid}}, $record->stop;
142 }
143 elsif ($record->feature eq 'start_codon') {
144 270 ▶ if ($record->strand == 1 and
Branch: 2 branches
BranchTrueFalseTotal
if ($record->strand == 1 and !defined($transcript->coding_start) || $record->start < $transcript->coding_start) { } 168 102 270
elsif ($record->strand == -1 and !defined($transcript->coding_stop) || $record->stop > $transcript->coding_stop) { } 102 0 102
Condition50%3/6
$record->strand == 1 and !defined($transcript->coding_start) || $record->start < $transcript->coding_start
!ll&&!rl&&r
1020168
not defined $transcript->coding_start or $record->start < $transcript->coding_start
l!l&&r!l&&!r
16800
MC/DC33%1/3
$record->strand == 1 and !defined($transcript->coding_start) || $record->start < $transcript->coding_start
$record->strand == 1 not defined $transcript->coding_start $record->start < $transcript->coding_start
Truth table50%2/4
A and not(B) or C
A= $record->strand == 1B= not defined $transcript->coding_startC= $record->start < $transcript->coding_start
ABCresult
0XX0
1000
11X1
1011
145 (!defined $transcript->coding_start or
146 $record->start < $transcript->coding_start)) {
147  
148 168 $transcript->coding_start($record->start);
149 }
150 ▶ elsif ($record->strand == -1 and
Condition33%2/6
$record->strand == -1 and !defined($transcript->coding_stop) || $record->stop > $transcript->coding_stop
!ll&&!rl&&r
00102
not defined $transcript->coding_stop or $record->stop > $transcript->coding_stop
l!l&&r!l&&!r
10200
MC/DC0%0/3
$record->strand == -1 and !defined($transcript->coding_stop) || $record->stop > $transcript->coding_stop
$record->strand == -1 not defined $transcript->coding_stop $record->stop > $transcript->coding_stop
Truth table25%1/4
A and not(B) or C
A= $record->strand == -1B= not defined $transcript->coding_stopC= $record->stop > $transcript->coding_stop
ABCresult
0XX0
1000
11X1
1011
151 (!defined $transcript->coding_stop or
152 $record->stop > $transcript->coding_stop)) {
153  
154 102 $transcript->coding_stop($record->stop);
155 }
156 }
157 elsif ($record->feature eq 'stop_codon') {
158 270 ▶ if ($record->strand == 1 and
Branch: 2 branches
BranchTrueFalseTotal
if ($record->strand == 1 and !defined($transcript->coding_stop) || $record->stop > $transcript->coding_stop) { } 168 102 270
elsif ($record->strand == -1 and !defined($transcript->coding_start) || $record->start < $transcript->coding_start) { } 102 0 102
Condition50%3/6
$record->strand == 1 and !defined($transcript->coding_stop) || $record->stop > $transcript->coding_stop
!ll&&!rl&&r
1020168
not defined $transcript->coding_stop or $record->stop > $transcript->coding_stop
l!l&&r!l&&!r
16800
MC/DC33%1/3
$record->strand == 1 and !defined($transcript->coding_stop) || $record->stop > $transcript->coding_stop
$record->strand == 1 not defined $transcript->coding_stop $record->stop > $transcript->coding_stop
Truth table50%2/4
A and not(B) or C
A= $record->strand == 1B= not defined $transcript->coding_stopC= $record->stop > $transcript->coding_stop
ABCresult
0XX0
1000
11X1
1011
159 (!defined $transcript->coding_stop or
160 $record->stop > $transcript->coding_stop)) {
161  
162 168 $transcript->coding_stop($record->stop);
163 }
164 ▶ elsif ($record->strand == -1 and
Condition33%2/6
$record->strand == -1 and !defined($transcript->coding_start) || $record->start < $transcript->coding_start
!ll&&!rl&&r
00102
not defined $transcript->coding_start or $record->start < $transcript->coding_start
l!l&&r!l&&!r
10200
MC/DC0%0/3
$record->strand == -1 and !defined($transcript->coding_start) || $record->start < $transcript->coding_start
$record->strand == -1 not defined $transcript->coding_start $record->start < $transcript->coding_start
Truth table25%1/4
A and not(B) or C
A= $record->strand == -1B= not defined $transcript->coding_startC= $record->start < $transcript->coding_start
ABCresult
0XX0
1000
11X1
1011
165 (!defined $transcript->coding_start or
166 $record->start < $transcript->coding_start)) {
167  
168 102 $transcript->coding_start($record->start);
169 }
170 }
171 }
172  
173 6 foreach my $tid (keys %transcripts) {
174 $transcripts{$tid}->set_splice_starts_and_stops(
175 348 $transcript_splice_starts{$tid}, $transcript_splice_stops{$tid});
176 }
177  
178 6 my @genes;
179 6 GENE: foreach my $gid (keys %genes) {
180 156 my $gene = GenOO::Gene->new(name => $gid);
181 156 my @gene_transcripts = sort {$a->start <=> $b->start} @{$genes{$gid}};
182 156 my $tr = shift @gene_transcripts;
183 156 $gene->add_transcript($tr);
184 156 $tr->gene($gene);
185 156 foreach $tr (@gene_transcripts) {
186 # if transcrpt doesn't overlap previous ones then skip the gene.
187 192 ▶ if (!$gene->overlaps($tr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gene->overlaps($tr)) 0 192 192
188 0 next GENE;
189 }
190 192 $gene->add_transcript($tr);
191 192 $tr->gene($gene);
192 }
193 156 push @genes, $gene;
194 }
195  
196 6 return \@genes;
197 }
198  
199 1 1;
↑ Summary GenOO/Data/File/BED/Record.pm »