GenOO/GeneCollection/Factory/GTF.pm

filter: Statementstmt 97.5% 80 / 82 Branchbran 84.3% 27 / 32 Conditioncond 44.4% 12 / 27 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 79.5% 132 / 166 CC 29 SCAR 33.1
CC
29
Cov
84%
_read_gtf
35.7
BEGIN
1.0
BEGIN
1.0
CRAP 27.5
↑ 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 method
20 "read_collection" (as the consumed role requires) which returns the actual
21 L<GenOO::GeneCollection> object in the form of
22 L<GenOO::RegionCollection::Type::DoubleHashArray>. The latter is the implementation
23 of the L<GenOO::RegionCollection> class based on the complex data structure
24 L<GenOO::Data::Structure::DoubleHashArray>.
25  
26 =head1 EXAMPLES
27  
28 # Create a concrete factory
29 my $factory_implementation = GenOO::GeneCollection::Factory->new('GTF',{
30 file => 'sample.gtf'
31 });
32
33 # Return the actual GenOO::GeneCollection object
34 my $collection = $factory_implementation->read_collection;
35 print ref($collection) # GenOO::GeneCollection::Type::DoubleHashArray
36  
37 =cut
38  
39 # Let the code begin...
40  
41 package GenOO::GeneCollection::Factory::GTF;
42 1 $GenOO::GeneCollection::Factory::GTF::VERSION = '1.4.9';
43  
44 #######################################################################
45 ####################### Load External modules #####################
46 #######################################################################
47 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52  
53 #######################################################################
54 ####################### Load GenOO modules #####################
55 #######################################################################
56 1 ▶use GenOO::RegionCollection::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use GenOO::Transcript;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use GenOO::Gene;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use GenOO::Data::File::GFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61  
62 #######################################################################
63 ####################### Interface attributes ######################
64 #######################################################################
65 1 has 'file' => (
66 isa => 'Maybe[Str]',
67 is => 'ro'
68 );
69  
70  
71 #######################################################################
72 ########################## Consumed Roles #########################
73 #######################################################################
74 1 with 'GenOO::RegionCollection::Factory::Requires';
75  
76  
77 #######################################################################
78 ######################## Interface Methods ########################
79 #######################################################################
80 sub read_collection {
81 6 ▶ my ($self) = @_;
Subroutine read_collection: called CC=1 SCAR=0.0
Pod undocumented
82
83 6 my @genes = $self->_read_gtf($self->file);
84
85 6 return GenOO::RegionCollection::Factory->create('RegionArray', {
86 array => \@genes
87 })->read_collection;
88 }
89  
90 #######################################################################
91 ######################### Private methods ##########################
92 #######################################################################
93 sub _read_gtf {
94 6 ▶ my ($self, $file)=@_;
Subroutine _read_gtf: called CC=29 SCAR=35.8
95
96 6 my %transcripts;
97 my %transcript_splice_starts;
98 6 my %transcript_splice_stops;
99 6 my %genes;
100
101 6 my $gff = GenOO::Data::File::GFF->new(file => $file);
102  
103 6 ▶ while (my $record = $gff->next_record){
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $gff->next_record) 5808 6 5814
104 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
105 or die "transcript_id attribute must be defined\n";
106 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
107 or die "gene_id attribute must be defined\n";
108
109 5808 ▶ if ($record->strand == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($record->strand == 0) 0 5808 5808
110 0 warn "Skipping transcript $tid: strand symbol". $record->strand_symbol." not accepted\n";
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 foreach my $gid (keys %genes) {
180 156 my $gene;
181 156 my @gene_transcripts = sort {$a->start <=> $b->start} @{$genes{$gid}};
182 156 foreach my $tr (@gene_transcripts) {
183 348 ▶ if (not defined $gene or $tr->start > $gene->stop) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $gene or $tr->start > $gene->stop) { } 156 192 348
Condition66%2/3
not defined $gene or $tr->start > $gene->stop
l!l&&r!l&&!r
1560192
MC/DC50%1/2
not defined $gene or $tr->start > $gene->stop
not defined $gene $tr->start > $gene->stop
Truth table66%2/3
not(A) or B
A= not defined $geneB= $tr->start > $gene->stop
ABresult
1X1
011
000
184 156 $gene = GenOO::Gene->new(name => $gid);
185 156 $gene->add_transcript($tr);
186 156 $tr->gene($gene);
187 156 push @genes, $gene;
188 }
189 else {
190 192 $gene->add_transcript($tr);
191 192 $tr->gene($gene);
192 }
193 }
194 }
195  
196 6 return @genes;
197 }
198  
199 1 1;
↑ Summary GenOO/Data/File/BED/Record.pm »