lib/Bio/RNA/RNAaliSplit/WrapRscape.pm

filter: Statementstmt 26.8% 44 / 164 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 53.8% 7 / 13 Podpod 0.0% 0 / 2 totaltotal 19.0% 51 / 268 CC 41 SCAR 46.2
CC
41
Cov
18%
_check_attributes
182.0
_parse_rscape
156.0
run_rscape
72.0
CRAP 101.2
« lib/Bio/RNA/RNAaliSplit/WrapRNAz.pm ↑ Summary script/RNAalisplit.pl »
1 # -*-CPerl-*-
2 # Last changed Time-stamp: <2019-01-07 00:39:42 mtw>
3 # place of birth: somewhere over Newfoundland
4  
5 # Bio::RNA::RNAaliSplit::WrapRscape.pm: A versatile object-oriented
6 # wrapper for R-scape
7 #
8 # Requires R-scape v1.2.2 or above available to the Perl interpreter.
9  
10 package Bio::RNA::RNAaliSplit::WrapRscape;
11  
12 1 ▶use version; our $VERSION = qv('0.09');
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use IPC::Cmd qw(can_run run);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 my ($rscape,$oodir);
21 1 my $exe = "R-scape";
22  
23 1 has 'basename' => (
24 is => 'rw',
25 isa => 'Str',
26 predicate => 'has_basename',
27 documentation => q(Set this to override output basename),
28 );
29  
30 1 has 'statistic' => (
31 is => 'rw',
32 isa => 'Str',
33 predicate => 'has_statistic',
34 default => 'RAFS',
35 documentation => q(Covariation statistic),
36 );
37 1 has 'cseq' => (
38 is => 'rw',
39 isa => 'Int',
40 predicate => 'has_cseq',
41 documentation => q(Number of sequences counted from stk file),
42 init_arg => undef,
43 );
44  
45 1 has 'nseq' => (
46 is => 'rw',
47 isa => 'Int',
48 predicate => 'has_nseq',
49 documentation => q(Number of sequences returned by R-scape),
50 init_arg => undef,
51 );
52  
53 1 has 'alen' => (
54 is => 'rw',
55 isa => 'Int',
56 predicate => 'has_alen',
57 documentation => q(Alignment length),
58 init_arg => undef,
59 );
60  
61 1 has 'nbpairs' => (
62 is => 'rw',
63 isa => 'Int',
64 predicate => 'has_nbpairs',
65 documentation => q(Number of base pairs),
66 init_arg => undef,
67 );
68  
69 1 has 'evalue' => (
70 is => 'rw',
71 isa => 'Num',
72 predicate => 'has_evalue',
73 documentation => q(E-value threshold),
74 init_arg => undef,
75 );
76  
77 1 has 'FP' => (
78 is => 'rw',
79 isa => 'Int',
80 predicate => 'has_FP',
81 documentation => q(Covarying non base pairs (FP)),
82 init_arg => undef,
83 );
84  
85 1 has 'TP' => (
86 is => 'rw',
87 isa => 'Int',
88 predicate => 'has_TP',
89 documentation => q(Covarying base pairs (TP)),
90 init_arg => undef,
91 );
92  
93 1 has 'T' => (
94 is => 'rw',
95 isa => 'Int',
96 predicate => 'has_T',
97 documentation => q(Number of base pairs (T)),
98 init_arg => undef,
99 );
100  
101 1 has 'F' => (
102 is => 'rw',
103 isa => 'Int',
104 predicate => 'has_F',
105 documentation => q(Total number of covarying base pairs (F)),
106 init_arg => undef,
107 );
108  
109 1 has 'Sen' => (
110 is => 'rw',
111 isa => 'Num',
112 predicate => 'has_Sen',
113 documentation => q(Sensitivity (TP/T)),
114 init_arg => undef,
115 );
116  
117 1 has 'PPV' => (
118 is => 'rw',
119 isa => 'Num',
120 predicate => 'has_PPV',
121 documentation => q(Positive predictive value (TP/F)),
122 init_arg => undef,
123 );
124  
125 1 has 'Fmeasure' => (
126 is => 'rw',
127 isa => 'Num',
128 predicate => 'has_Fmeasure',
129 documentation => q(F-measure (2*Sen*PPV(Sen+PPV))),
130 init_arg => undef,
131 );
132  
133 1 has 'nofigures' => (
134 is => 'rw',
135 isa => 'Int',
136 predicate => 'has_nofigures',
137 documentation => q(Turn off all image procudtion by R-scape),
138 );
139  
140 has 'sigBP' => ( # significantly covarying base pairs
141 is => 'rw',
142 isa => 'ArrayRef',
143 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
144 1 traits => ['Array'],
145 predicate => 'has_data',
146 handles => {
147 all => 'elements',
148 count => 'count',
149 add => 'push',
150 pop => 'pop',
151 },
152 );
153  
154 1 has 'status' => (
155 is => 'rw',
156 isa => 'Int',
157 predicate => 'has_status',
158 documentation => q(R-scape program status), # 0:OK ; >0:NOTOK
159 );
160  
161  
162 1 with 'FileDirUtil';
163 1 with 'Bio::RNA::RNAaliSplit::Roles';
164  
165  
166 sub BUILD {
167 0 ▶ my $self = shift;
Subroutine BUILD: not called CC=7 SCAR=40.3
Pod undocumented
168 0 my $this_function = (caller(0))[3];
169 0 ▶ confess "ERROR [$this_function] \$self->ifile not available"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_ifile 0 0 0
170 unless ($self->has_ifile);
171 0 ▶ $rscape = can_run($exe) or
Branch: 1 branch
BranchTrueFalseTotal
$rscape = can_run($exe) or croak("ERROR [$this_function] $exe not found") 0 0 0
172 croak "ERROR [$this_function] $exe not found";
173 0 ▶ unless($self->has_odir){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_odir) 0 0 0
174 0 ▶ unless($self->has_dirnam){self->dirnam("as")}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_dirnam) 0 0 0
175 0 $self->odir( [$self->ifile->dir,$self->dirnam] );
176 }
177 0 $oodir = $self->odir->subdir("rscape");
178 0 my @created = make_path($oodir, {error => \my $err});
179 0 ▶ confess "ERROR [$this_function] could not create output directory $self->oodir"
Branch: 1 branch
BranchTrueFalseTotal
if @$err 0 0 0
180 if (@$err);
181 0 $self->set_ifilebn;
182 0 $self->_count_seq();
183 0 ▶ if ($self->cseq > 1){ $self->run_rscape() }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cseq > 1) 0 0 0
184 }
185  
186 sub _count_seq {
187 0 ▶ my $self = shift;
Subroutine _count_seq: not called CC=5 SCAR=34.0
188 0 my $this_function = (caller(0))[3];
189 0 my $count = 0;
190 0 ▶ open my $stk, "<", $self->ifile or croak "ERROR [$this_function] Cannot open Stockholm file $self->ifile for reading";
Branch: 1 branch
BranchTrueFalseTotal
open my $stk, "<", $self->ifile or croak("ERROR [$this_function] Cannot open Stockholm file $self->ifile for reading") 0 0 0
191 0 ▶ while (<$stk>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $stk)) 0 0 0
192 0 ▶ next if (/^#/);
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ 0 0 0
193 0 ▶ next if (/^\/\//);
Branch: 1 branch
BranchTrueFalseTotal
if m[^//] 0 0 0
194 0 $count++;
195 }
196 0 close ($stk);
197 0 $self->cseq($count);
198 }
199  
200 sub run_rscape {
201 0 ▶ my $self = shift;
Subroutine run_rscape: not called CC=8 SCAR=42.8
Pod undocumented
202 0 my $this_function = (caller(0))[3];
203 0 my ($out_fn,$sout_fn,$out,$sout,$sum);
204 0 my ($rscape_out,$rscape_sout,$rscape_sum);
205 0 my $tag = "";
206 0 ▶ if ($self->has_statistic){$tag = ".".$self->statistic};
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_statistic) 0 0 0
207  
208 0 ▶ if ($self->has_basename){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_basename) { } 0 0 0
elsif ($self->has_ifilebn) { } 0 0 0
209 0 $out_fn = $self->basename.$tag."."."rscape.out";
210 0 $sout_fn = $self->basename.$tag."."."rscape.sorted.out";
211 }
212 elsif ($self->has_ifilebn){
213 0 $out_fn = $self->ifilebn.$tag."."."rscape.out";
214 0 $sout_fn = $self->ifilebn.$tag."."."rscape.sorted.out";
215 }
216 else{
217 0 $out_fn = $tag."rscape.out";
218 0 $sout_fn = $tag."rscape.sorted.out";
219 }
220 0 $out = file($oodir,$out_fn); # R-scape stdout
221 0 $sout = file($oodir,$sout_fn); # R-scape sorted stdout
222  
223 0 $rscape_out = "rscape.out";
224 0 $rscape_sout = $rscape_out.".sorted";
225  
226 0 my $rscape_options = " -o $rscape_out --rna --outdir $oodir ";
227 0 ▶ if ($self->has_nofigures && $self->nofigures == 1){$rscape_options.=" --nofigures "};
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_nofigures and $self->nofigures == 1) 0 0 0
Condition0%0/3
$self->has_nofigures and $self->nofigures == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_nofigures and $self->nofigures == 1
$self->has_nofigures $self->nofigures == 1
Truth table0%0/3
A and B
A= $self->has_nofiguresB= $self->nofigures == 1
ABresult
0X0
100
111
228 0 ▶ if ($self->has_statistic){$rscape_options.=" --".$self->statistic." " }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_statistic) 0 0 0
229 0 my $cmd = $rscape.$rscape_options.$self->ifile;
230  
231 0 my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
232 run( command => $cmd, verbose => 0 );
233 0 ▶ if( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
234 0 print STDERR "ERROR [$this_function] Call to $rscape unsuccessful\n";
235 0 print STDERR "ERROR: $cmd\n";
236 0 print STDERR "ERROR: this is what the command printed:\n";
237 0 print join "", @$full_buf;
238 0 croak $!;
239 }
240  
241 0 $self->_parse_rscape($rscape_out);
242  
243 0 rename $rscape_out, $out;
244 0 rename $rscape_sout, $sout;
245 }
246  
247 # parse R-scape output
248 # canonical R-scape output is expected to look like this:
249 # MSA myaln nseq 9 (9) alen 45 (45) avgid 79.51 (79.51) nbpairs 11 (11)
250 # contacts 11 (11 bpairs 11 wc bpairs)
251 # maxD 8.00
252 # mind 1
253 # Method Target_E-val [cov_min,conv_max] [FP | TP True Found | Sen PPV F]
254 # RAFS 0.05 [-1.00,0.83] [0 | 3 11 3 | 27.27 100.00 42.86]
255 # left_pos right_pos score E-value
256 # ------------------------------------------------------------
257 #* 20 36 0.83 0.000328218
258 #* 21 35 0.83 0.000328218
259 #* 22 34 0.33 0.0497359
260  
261 sub _parse_rscape {
262 0 ▶ my ($self,$out) = @_;
Subroutine _parse_rscape: not called CC=12 SCAR=50.5
263 0 my $this_function = (caller(0))[3];
264 0 my @buffer = ();
265 0 my $parse1 = 0;
266 0 my $parse2 = 0;
267 0 my $nosbp = 0;
268 0 ▶ open my $file, "<", $out or croak "ERROR: [$this_function] Cannot open file $out";
Branch: 1 branch
BranchTrueFalseTotal
open my $file, "<", $out or croak("ERROR: [$this_function] Cannot open file $out") 0 0 0
269 0 ▶ while(<$file>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $file)) 0 0 0
270 0 chomp;
271 0 ▶ if (m/^#\s+MSA\s+([a-zA-Z0-9_.|]+)\s+nseq\s+(\d+)\s+\(\d+\)\s+alen\s+(\d+)\s+\(\d+\)\s+avgid\s+(\d+\.\d+)\s+\(\d+\.\d+\)\s+nbpairs\s+(\d+)\s+\(\d+\)/g){
Branch: 1 branch
BranchTrueFalseTotal
if (/^#\s+MSA\s+([a-zA-Z0-9_.|]+)\s+nseq\s+(\d+)\s+\(\d+\)\s+alen\s+(\d+)\s+\(\d+\)\s+avgid\s+(\d+\.\d+)\s+\(\d+\.\d+\)\s+nbpairs\s+(\d+)\s+\(\d+\)/g) 0 0 0
272 0 $self->nseq($2);
273 0 $self->alen($3);
274 0 $self->nbpairs($5);
275 0 $parse1 = 1;
276 0 next;
277 }
278 0 ▶ if (m/^#\s+([a-zA-Z0-9]+)\s+(\d+\.\d+)\s+\[(\-?\d+\.\d+),(-?\d+\.\d+)\]\s+\[(\d+)\s+\|\s+(\d+)\s+(\d+)\s+(\d+)\s+\|\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\]/g){
Branch: 1 branch
BranchTrueFalseTotal
if (/^#\s+([a-zA-Z0-9]+)\s+(\d+\.\d+)\s+\[(\-?\d+\.\d+),(-?\d+\.\d+)\]\s+\[(\d+)\s+\|\s+(\d+)\s+(\d+)\s+(\d+)\s+\|\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\]/g) 0 0 0
279 0 $self->evalue($2);
280 0 $self->FP($5);
281 0 $self->TP($6);
282 0 $self->T($7);
283 0 $self->F($8);
284 0 $self->Sen($9);
285 0 $self->PPV($10);
286 0 $self->Fmeasure($11);
287 0 $parse2 = 1;
288 0 next;
289 }
290 0 ▶ if (m/^\*\s+(\d+)\s+(\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)$/){
Branch: 1 branch
BranchTrueFalseTotal
if (/^\*\s+(\d+)\s+(\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)$/) 0 0 0
291 0 my %bp = (i => $1,
292 j => $2,
293 score => $3,
294 evalue => $4);
295 0 push @{$self->sigBP}, \%bp;
296 }
297 0 ▶ if (m/^no significant pairs$/){
Branch: 1 branch
BranchTrueFalseTotal
if (/^no significant pairs$/) 0 0 0
298 0 $nosbp=1;
299 }
300 }
301 0 close ($file);
302 #carp "INFO: [$this_function] parse1:".eval($parse1);
303 #carp "INFO: [$this_function] parse2:".eval($parse2);
304  
305 0 ▶ if ($nosbp == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($nosbp == 1) 0 0 0
306 0 $self->status(1); # no significant basepairs
307 0 $self->_check_attributes();
308 0 return;
309 }
310 0 ▶ if ($parse1 == 1 && $parse2 == 1){
Branch: 2 branches
BranchTrueFalseTotal
if ($parse1 == 1 and $parse2 == 1) { } 0 0 0
elsif ($parse1 == 1 and $parse2 == 0) { } 0 0 0
Condition0%0/3
$parse1 == 1 and $parse2 == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$parse1 == 1 and $parse2 == 1
$parse1 == 1 $parse2 == 1
Truth table0%0/3
A and B
A= $parse1 == 1B= $parse2 == 1
ABresult
0X0
100
111
311 0 $self->_check_attributes();
312 0 $self->status(0); # all OK
313 0 return;
314 }
315 ▶ elsif ($parse1 == 1 && $parse2 == 0){
Condition0%0/3
$parse1 == 1 and $parse2 == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$parse1 == 1 and $parse2 == 0
$parse1 == 1 $parse2 == 0
Truth table0%0/3
A and B
A= $parse1 == 1B= $parse2 == 0
ABresult
0X0
100
111
316 0 $self->status(2); # covariation scores are almost constant, no further analysis
317 0 return;
318 }
319 else{
320 0 croak "ERROR: [$this_function] ambiguous status when parsing rscape output file. parse1:".eval($parse1)." parse2:".eval($parse2)." This shouldn't happen ...";
321 0 $self->status(3);
322 0 return;
323 }
324 }
325  
326 sub _check_attributes {
327 0 ▶ my ($self,$out) = @_;
Subroutine _check_attributes: not called CC=13 SCAR=52.0
328 0 my $this_function = (caller(0))[3];
329  
330 0 ▶ $self->has_cseq ? 1 : croak "ERROR [$this_function] \$self->cseq not set ". $self->ifile;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_cseq ? : 0 0 0
331 0 ▶ $self->has_nseq ? 1 : croak "ERROR [$this_function] \$self->nseq not set ". $self->ifile;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_nseq ? : 0 0 0
332 0 ▶ $self->has_alen ? 1 : croak "ERROR [$this_function] \$self->alen not set". $self->ifile;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_alen ? : 0 0 0
333 0 ▶ $self->has_nbpairs ? 1 : croak "ERROR [$this_function] \$self->nbpairs not set". $self->ifile;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_nbpairs ? : 0 0 0
334 0 ▶ $self->has_evalue ? 1 : croak "ERROR [$this_function] \$self->evalue not set". $self->ifile;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_evalue ? : 0 0 0
335 0 ▶ $self->has_FP ? 1 : croak "ERROR [$this_function] \$self->FP not set". $self->ifile;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_FP ? : 0 0 0
336 0 ▶ $self->has_TP ? 1 : croak "ERROR [$this_function] \$self->TP not set". $self->ifile;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_TP ? : 0 0 0
337 0 ▶ $self->has_T ? 1 : croak "ERROR [$this_function] \$self->T not set". $self->ifile;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_T ? : 0 0 0
338 0 ▶ $self->has_F ? 1 : croak "ERROR [$this_function] \$self->F not set". $self->ifile;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_F ? : 0 0 0
339 0 ▶ $self->has_Sen ? 1 : croak "ERROR [$this_function] \$self->Sen not set". $self->ifile;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_Sen ? : 0 0 0
340 0 ▶ $self->has_PPV ? 1 : croak "ERROR [$this_function] \$self->PPV not set". $self->ifile;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_PPV ? : 0 0 0
341 0 ▶ $self->has_Fmeasure ? 1 : croak "ERROR [$this_function] \$self->Fmeasure not set". $self->ifile;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_Fmeasure ? : 0 0 0
342 }
343  
344 1 1;
« lib/Bio/RNA/RNAaliSplit/WrapRNAz.pm ↑ Summary script/RNAalisplit.pl »