GenOO/GeneCollection/Factory/FromTranscriptCollection.pm

filter: Statementstmt 98.3% 59 / 60 Branchbran 75.0% 15 / 20 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 92.5% 87 / 94 CC 16 SCAR 19.0
CC
16
Cov
93%
_merge
11.4
_make_list_of_genes
4.0
read_collection
2.0
CRAP 6.7
« GenOO/Gene.pm ↑ Summary GenOO/Data/File/SAM/MDZ.pm »
1 # POD documentation - main docs before the code
2  
3 =head1 NAME
4  
5 GenOO::GeneCollection::Factory::FromTranscriptCollection - Factory for creating GenOO::GeneCollection object from a Transcript Collection and a hash{transcript_name} = genename
6  
7 =head1 SYNOPSIS
8  
9 # Creates GenOO::GeneCollection object from a Transcript Collection and a hash
10  
11 # Preferably use it through the generic GenOO::GeneCollection::Factory
12 my $factory = GenOO::GeneCollection::Factory->create(
13 'FromTranscriptCollection',
14 {
15 annotation_hash => \%annotation,
16 transcript_collection => $transcript_collection
17 }
18 );
19  
20 =head1 DESCRIPTION
21  
22 An instance of this class is a concrete factory for the creation of a
23 L<GenOO::GeneCollection> object from a Transcript Collection
24 and a hash that has transcript names as keys and gene names as values.
25 It offers the method "read_collection" (as the consumed role requires) which returns the actual
26 L<GenOO::GeneCollection> object in the form of L<GenOO::RegionCollection::Type::DoubleHashArray>.
27 The latter is the implementation of the L<GenOO::RegionCollection> class based on the complex
28 data structure L<GenOO::Data::Structure::DoubleHashArray>.
29  
30 =head1 EXAMPLES
31  
32 # Create a concrete factory
33 my $factory_implementation = GenOO::GeneCollection::Factory->create(
34 'FromTranscriptCollection',
35 {
36 annotation_hash => \%annotation,
37 transcript_collection => $transcript_collection
38 }
39 );
40
41 # Return the actual GenOO::GeneCollection object
42 my $collection = $factory_implementation->read_collection;
43 print ref($collection) # GenOO::RegionCollection::Type::DoubleHashArray
44  
45 =cut
46  
47 # Let the code begin...
48  
49 package GenOO::GeneCollection::Factory::FromTranscriptCollection;
50 1 $GenOO::GeneCollection::Factory::FromTranscriptCollection::VERSION = '1.4.9';
51 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 1 ▶use GenOO::RegionCollection::Type::DoubleHashArray;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use GenOO::Gene;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 1 has 'annotation_hash' => (
58 isa => 'HashRef',
59 is => 'ro',
60 required => 1
61 );
62 1 has 'transcript_collection' => (
63 isa => 'GenOO::RegionCollection',
64 is => 'ro',
65 required => 1
66 );
67  
68 1 with 'GenOO::RegionCollection::Factory::Requires';
69  
70 #######################################################################
71 ######################## Interface Methods ########################
72 #######################################################################
73 sub read_collection {
74 1 ▶ my ($self) = @_;
Subroutine read_collection: called CC=2 SCAR=6.9
Pod undocumented
75
76 1 my $collection = GenOO::RegionCollection::Type::DoubleHashArray->new;
77 1 my @all_genes = $self->_make_list_of_genes;
78 1 foreach my $gene ( @all_genes ) {
79 6 $collection->add_record($gene);
80 }
81 1 return $collection;
82 }
83  
84 sub _make_list_of_genes {
85 1 ▶ my ($self) = @_;
Subroutine _make_list_of_genes: called CC=4 SCAR=13.9
86
87 1 my @outgenes;
88 my %transcripts_for_genename;
89 $self->transcript_collection->foreach_record_do( sub {
90 70 ▶ my ($transcript) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
91
92 70 ▶ if (exists $self->annotation_hash->{$transcript->id}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->annotation_hash->{$transcript->id}) 27 43 70
93 27 my $gene_name = $self->annotation_hash->{$transcript->id};
94 27 push @{$transcripts_for_genename{$gene_name}}, $transcript;
95 }
96 1 });
97
98 1 foreach my $genename (keys %transcripts_for_genename) {
99 5 my ($merged_regions,$included_transcripts) = _merge($transcripts_for_genename{$genename});
100 5 ▶ for (my $i=0;$i<@$merged_regions;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$merged_regions) 6 5 11
101 6 my $gene = GenOO::Gene->new(
102 name => $genename,
103 transcripts => $$included_transcripts[$i]
104 );
105 6 foreach my $transcript (@{$gene->transcripts}) {
106 27 $transcript->gene($gene);
107 }
108 6 push @outgenes, $gene;
109 }
110 }
111
112 1 return @outgenes;
113 }
114  
115 sub _merge {
116 5 ▶ my ($regions_ref, $params) = @_;
Subroutine _merge: called CC=11 SCAR=24.4
117
118 5 ▶ my $offset = exists $params->{'OFFSET'} ? $params->{'OFFSET'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{'OFFSET'} ? : 0 5 5
119 5 ▶ my $use_strand = exists $params->{'USE_STRAND'} ? $params->{'USE_STRAND'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{'USE_STRAND'} ? : 0 5 5
120
121 5 ▶ my @sorted_regions = (@$regions_ref > 1) ? sort{$a->start <=> $b->start} @$regions_ref : @$regions_ref;
Branch: 1 branch
BranchTrueFalseTotal
@$regions_ref > 1 ? : 3 2 5
122
123 5 my @merged_regions;
124 my @included_regions;
125 5 foreach my $region (@sorted_regions) {
126
127 27 my $merged_region = $merged_regions[-1];
128 27 ▶ if (defined $merged_region and $merged_region->overlaps($region, $use_strand, $offset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $merged_region and $merged_region->overlaps($region, $use_strand, $offset)) { } 21 6 27
Condition100%3/3
defined $merged_region and $merged_region->overlaps($region, $use_strand, $offset)
!ll&&!rl&&r
5121
MC/DC100%2/2
defined $merged_region and $merged_region->overlaps($region, $use_strand, $offset)
defined $merged_region $merged_region->overlaps($region, $use_strand, $offset)
Truth table100%3/3
A and B
A= defined $merged_regionB= $merged_region->overlaps($region, $use_strand, $offset)
ABresult
0X0
100
111
129 21 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 21 0 21
130 21 push @{$included_regions[-1]}, $region;
131 }
132 21 ▶ if ($region->stop() > $merged_region->stop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($region->stop > $merged_region->stop) 1 20 21
133 1 $merged_region->stop($region->stop);
134 }
135 }
136 else {
137 6 push @merged_regions, GenOO::GenomicRegion->new
138 (
139 start => $region->start,
140 stop => $region->stop,
141 strand => $region->strand,
142 chromosome => $region->chromosome,
143 );
144 6 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 6 0 6
145 6 push @included_regions,[$region];
146 }
147 }
148 }
149  
150 5 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 5 0 5
151 5 return (\@merged_regions, \@included_regions);
152 }
153 else {
154 0 return \@merged_regions;
155 }
156 }
157  
158 1 1;
« GenOO/Gene.pm ↑ Summary GenOO/Data/File/SAM/MDZ.pm »