lib/Bio/ViennaNGS/Expression.pm

filter: Statementstmt 19.6% 34 / 173 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 9 / 15 Podpod 80.0% 4 / 5 totaltotal 21.6% 47 / 217 CC 20 SCAR 37.0
CC
20
Cov
17%
write_expression_bed12
110.0
parse_readcounts_bed12
30.0
computeTPM
12.0
CRAP 40.3
« lib/Bio/ViennaNGS/FeatureIO.pm ↑ Summary lib/Bio/ViennaNGS/Fasta.pm »
1 # -*-CPerl-*-
2 # Last changed Time-stamp: <2017-06-10 18:59:03 michl>
3  
4 package Bio::ViennaNGS::Expression;
5  
6 1 ▶use Bio::ViennaNGS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Bio::ViennaNGS::Bed;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Bio::ViennaNGS::Util qw(sortbed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use version; our $VERSION = version->declare("$Bio::ViennaNGS::VERSION");
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 has 'readcountfile' => (
17 is => 'rw',
18 predicate => 'has_readcountfile',
19 );
20  
21 has 'data' => (
22 is => 'rw',
23 isa => 'ArrayRef',
24 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 1 );
26  
27 1 has 'conds' => (
28 is => 'rw',
29 isa => 'Int',
30 predicate => 'has_conds',
31 );
32  
33 1 has 'nr_features' => (
34 is => 'rw',
35 isa => 'Int',
36 predicate => 'has_features',
37 );
38  
39 0 ▶sub parse_readcounts_bed12 {
Subroutine parse_readcounts_bed12: not called CC=5 SCAR=34.0
Pod documented
40 0 my ($self,$file) = @_;
41 0 my @mcData = ();
42 0 my ($i,$n) = (0)x2;
43 0 my $this_function = (caller(0))[3];
44  
45 0 ▶ croak "ERROR [$this_function] readcount / multicov file $self->readcountfile not available\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
46 unless (-e $file);
47 0 $self->readcountfile($file);
48 0 ▶ open (RC_IN, "< $file") or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open RC_IN, "< $file" or croak($!) 0 0 0
49  
50 0 ▶ while (<RC_IN>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline RC_IN)) 0 0 0
51 0 $n++;
52 0 chomp;
53 # 0:chr|1:start|2:end|3:name|4:score|5:strand
54 # 6:thickStart|7:thickEnd|8:itemRgb|9:blockCount|
55 # 10:blockSizes|11:blockStarts
56 0 @mcData = split(/\t/);
57 0 my $conditions = (scalar @mcData)-12; # multicov extends BED12
58 0 $self->conds($conditions);
59  
60 # NOTE: Better keep BED12 entries in a hash, generating UUIDs as
61 # keys instead of storing the same BED12 entry n times (ie for
62 # each sample) in $self->data
63  
64 0 my $bedobj = Bio::ViennaNGS::Bed->new(chromosome => $mcData[0],
65 start => $mcData[1],
66 end => $mcData[2],
67 name => $mcData[3],
68 score => $mcData[4],
69 strand => $mcData[5],
70 thickStart => $mcData[6],
71 thickEnd => $mcData[7],
72 itemRgb => $mcData[8],
73 blockCount => $mcData[9],
74 blockSizes => $mcData[10],
75 blockStarts => $mcData[11],
76 );
77 0 my $len = $bedobj->length;
78 0 my $id = sprintf("%s:%d-%d_%s_%s", $mcData[0],$mcData[1],
79 $mcData[2],$mcData[3],$mcData[5]);
80 #print "\$id: $id\n";
81 0 ▶ for ($i=0;$i<$self->conds;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->conds) 0 0 0
82 0 ${$self->data}[$i]{$id} = {
83 bed_entry => $bedobj,
84 length => $len,
85 count => $mcData[eval(12+$i)],
86 };
87 # print "sample $i:\n";
88 # print Dumper(${$self->data}[$i]);
89 }
90 }
91 0 $self->nr_features($n);
92 0 close(RC_IN);
93 }
94  
95 0 ▶sub write_expression_bed12 {
Subroutine write_expression_bed12: not called CC=10 SCAR=47.0
Pod documented
96 0 my ($self,$item,$dest,$base_name) = @_;
97 0 my ($bedname,$bedname_u,$outfile,$feat,$i);
98 0 my $this_function = (caller(0))[3];
99  
100 0 ▶ croak "ERROR [$this_function]: $dest does not exist\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dest 0 0 0
101 unless (-d $dest);
102  
103 0 $bedname = $base_name.".".$item.".multicov.bed12";
104 0 $bedname_u = $base_name.".".$item.".multicov.u.bed12";
105  
106 0 $outfile = file($dest,$bedname_u);
107  
108 0 ▶ croak "ERROR [$this_function]: $self->conds not available\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_conds 0 0 0
109 unless ($self->has_conds);
110  
111 0 ▶ croak "ERROR [$this_function]: $self->nr_features not available\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_features 0 0 0
112 unless ($self->has_features);
113  
114 #print "=====> write_multicov: writing multicov file $bedfile with ".
115 # eval($self->nr_features)." lines and ".eval($self->conds)." conditions\n";
116  
117 # check whether each element in @{$self->data} has the same amount of entries
118 0 ▶ for($i=0;$i<$self->conds;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->conds) 0 0 0
119 0 my $fc = scalar keys %{$self->data->[$i]}; # of keys in %{$featCount}
120 #print "condition $i => $fc keys\n";
121 0 ▶ croak "ERROR [$this_function]: unequal element count in @{$self->data}"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->nr_features == $fc 0 0 0
122 unless($self->nr_features == $fc)
123 }
124  
125 0 ▶ open (MULTICOV_OUT, "> $bedname_u") or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open MULTICOV_OUT, "> $bedname_u" or croak($!) 0 0 0
126  
127 # use BED12 data stored with condition 0 here, assuming its the same for all conditions
128 0 foreach $feat (keys %{${$self->data}[0]} ){
129 0 my @mcLine = ();
130 # retrieve BED12 line first
131 0 my $bedo = ${${$self->data}[0]}{$feat}{bed_entry};
132 0 my $bedline = $bedo->as_bed_line(12);
133 0 push @mcLine, $bedline;
134  
135 # process multicov values for all samples
136 0 ▶ for($i=0;$i<$self->conds;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->conds) 0 0 0
137 # print "------------> "; print "processing $i th condition "; print "<-----------\n";
138 croak "Could not find item $feat in mcSample $i\n"
139 0 ▶ unless ( defined ${${$self->data}[$i]}{$feat} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined do { ${$self->data;}[$i] }->{$feat} 0 0 0
140 0 push @mcLine, ${${$self->data}[$i]}{$feat}{$item};
141  
142 }
143 #print MULTICOV_OUT join("\t",@mcLine)."\n";
144 0 my @bed12 = splice @mcLine,0,1;
145 0 print MULTICOV_OUT join("\t",@bed12)."\t";
146 0 print MULTICOV_OUT join("\t", map { sprintf "%20.4f", $_ }@mcLine),"\n";
147 }
148 0 close(MULTICOV_OUT);
149  
150 0 sortbed($bedname_u,"./",$bedname,1,undef); # sort bed file
151 }
152  
153  
154 0 ▶sub computeTPM {
Subroutine computeTPM: not called CC=3 SCAR=24.8
Pod documented
155 0 my ($self,$sample,$rl) = @_;
156 0 my ($TPM,$T,$totalTPM) = (0)x3;
157 0 my ($i,$meanTPM);
158  
159 # iterate through $self->data[$i] twice:
160 # 1. for computing T (denominator in TPM formula)
161 0 foreach $i (keys %{${$self->data}[$sample]}){
162 0 my $count = ${${$self->data}[$sample]}{$i}{count};
163 0 my $length = ${${$self->data}[$sample]}{$i}{length};
164 #print "count: $count\nlength: $length\n";
165 0 $T += $count * $rl / $length;
166 }
167  
168 # 2. for computng actual TPM values
169 0 foreach $i (keys %{${$self->data}[$sample]}){
170 0 my $count = ${${$self->data}[$sample]}{$i}{count};
171 0 my $length = ${${$self->data}[$sample]}{$i}{length};
172 0 $TPM = 1000000 * $count * $rl/($length * $T);
173 0 ${${$self->data}[$sample]}{$i}{TPM} = $TPM;
174 0 $totalTPM += $TPM;
175 }
176  
177 0 $meanTPM = $totalTPM/$self->nr_features;
178  
179 #print Dumper(${$self->data}[$sample]);
180 #print "totalTPM=$totalTPM | meanTPM=$meanTPM\n";
181  
182 0 return $meanTPM;
183 }
184  
185 0 ▶sub computeRPKM {
Subroutine computeRPKM: not called CC=3 SCAR=24.8
Pod documented
186 0 my ($self,$sample) = @_;
187 0 my ($R,$length,$count,$totalRPKM) = (0)x4;
188 0 my ($i,$meanRPKM);
189  
190 # iterate through $self->data[$i] twice:
191 # 1. compute the total number of reads for a sample
192 0 foreach $i (keys %{${$self->data}[$sample]}){
193 0 $R += ${${$self->data}[$sample]}{$i}{count};
194 }
195  
196 # 2. for computing actual RPKM values
197 0 foreach $i (keys %{${$self->data}[$sample]}){
198 0 $count = ${${$self->data}[$sample]}{$i}{count};
199 0 $length = ${${$self->data}[$sample]}{$i}{length};
200 0 my $RPKM = ( $count * 1000000000 ) / ( $length * $R );
201 0 ${${$self->data}[$sample]}{$i}{RPKM} = $RPKM;
202 0 $totalRPKM += $RPKM;
203 }
204 0 $meanRPKM = $totalRPKM/$self->nr_features;
205 0 return $meanRPKM;
206 }
207  
208 0 ▶sub computeRPKMfromTPM {
Subroutine computeRPKMfromTPM: not called CC=3 SCAR=24.8
Pod undocumented
209 0 my ($self,$sample,$rl) = @_;
210 0 my ($R,$T,$length,$count,$totalRPKM) = (0)x5;
211 0 my ($i,$meanRPKM);
212  
213 # iterate through $self->data[$i] twice:
214 # 1. compute T (denominator in TPM formula) and total number of reads
215 0 foreach $i (keys %{${$self->data}[$sample]}){
216 0 $count = ${${$self->data}[$sample]}{$i}{count};
217 0 $length = ${${$self->data}[$sample]}{$i}{length};
218 #print "count: $count\nlength: $length\n";
219 0 $T += $count * $rl / $length;
220 0 $R += $count;
221 }
222 # print "T: $T\tR: $R\n";
223  
224 # 2. compute RPKM from TPM
225 0 foreach $i (keys %{${$self->data}[$sample]}){
226 0 my $RPKMfromTPM = ${${$self->data}[$sample]}{$i}{TPM} * ( $T * 1000 ) / ( $R * $rl);
227 0 ${${$self->data}[$sample]}{$i}{RPKMfromTPM} = $RPKMfromTPM;
228 0 $totalRPKM += $RPKMfromTPM;
229 }
230 0 $meanRPKM = $totalRPKM/$self->nr_features;
231  
232 # print Dumper(${$self->data}[$sample]);
233  
234 0 return $meanRPKM;
235 }
236  
237 1 __PACKAGE__->meta->make_immutable;
238  
239 1 1;
240 __END__
« lib/Bio/ViennaNGS/FeatureIO.pm ↑ Summary lib/Bio/ViennaNGS/Fasta.pm »