lib/Bio/RNA/RNAaliSplit/WrapRscape.pm untested

filter: Statementstmt 0.0% 0 / 164 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 267 CC 32 SCAR 44.1
CC
32
Cov
0%
_check_attributes
182.0
_parse_rscape
72.0
run_rscape
56.0
CRAP 82.2
1 # -*-CPerl-*-
2 # Last changed Time-stamp: <2019-08-25 19:51:47 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 use version; our $VERSION = qv('0.11');
13 use Carp;
14 use Data::Dumper;
15 use Moose;
16 use Path::Class;
17 use IPC::Cmd qw(can_run run);
18 use File::Path qw(make_path);
19  
20 my ($rscape,$oodir);
21 my $exe = "R-scape";
22  
23 has 'basename' => (
24 is => 'rw',
25 isa => 'Str',
26 predicate => 'has_basename',
27 documentation => q(Set this to override output basename),
28 );
29  
30 has 'statistic' => (
31 is => 'rw',
32 isa => 'Str',
33 predicate => 'has_statistic',
34 default => 'RAFS',
35 documentation => q(Covariation statistic),
36 );
37 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 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 has 'alen' => (
54 is => 'rw',
55 isa => 'Int',
56 predicate => 'has_alen',
57 documentation => q(Alignment length),
58 init_arg => undef,
59 );
60  
61 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 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 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 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 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 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 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 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 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 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 default => sub { [] },
144 traits => ['Array'],
145 predicate => 'has_data',
146 handles => {
147 all => 'elements',
148 count => 'count',
149 add => 'push',
150 pop => 'pop',
151 },
152 );
153  
154 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 with 'FileDirUtil';
163  
164 sub BUILD {
165 my $self = shift;
166 my $this_function = (caller(0))[3];
167 confess "ERROR [$this_function] \$self->ifile not available"
168 unless ($self->has_ifile);
169 $rscape = can_run($exe) or
170 croak "ERROR [$this_function] $exe not found";
171 unless($self->has_odir){
172 my $odir_name = "as";
173 $self->odir( [$self->ifile->dir,$odir_name] );
174 }
175 $oodir = $self->odir->subdir("rscape");
176 my @created = make_path($oodir, {error => \my $err});
177 confess "ERROR [$this_function] could not create output directory $self->oodir"
178 if (@$err);
179 $self->set_ifilebn;
180 $self->_count_seq();
181 if ($self->cseq > 1){ $self->run_rscape() }
182 }
183  
184 sub _count_seq {
185 my $self = shift;
186 my $this_function = (caller(0))[3];
187 my $count = 0;
188 open my $stk, "<", $self->ifile or croak "ERROR [$this_function] Cannot open Stockholm file $self->ifile for reading";
189 while (<$stk>){
190 next if (/^#/);
191 next if (/^\/\//);
192 $count++;
193 }
194 close ($stk);
195 $self->cseq($count);
196 }
197  
198 sub run_rscape {
199 my $self = shift;
200 my $this_function = (caller(0))[3];
201 my ($out_fn,$sout_fn,$out,$sout,$sum);
202 my ($rscape_out,$rscape_sout,$rscape_sum);
203 my $tag = "";
204 if ($self->has_statistic){$tag = ".".$self->statistic};
205  
206 if ($self->has_basename){
207 $out_fn = $self->basename.$tag."."."rscape.out";
208 $sout_fn = $self->basename.$tag."."."rscape.sorted.out";
209 }
210 elsif ($self->has_ifilebn){
211 $out_fn = $self->ifilebn.$tag."."."rscape.out";
212 $sout_fn = $self->ifilebn.$tag."."."rscape.sorted.out";
213 }
214 else{
215 $out_fn = $tag."rscape.out";
216 $sout_fn = $tag."rscape.sorted.out";
217 }
218 $out = file($oodir,$out_fn); # R-scape stdout
219 $sout = file($oodir,$sout_fn); # R-scape sorted stdout
220  
221 $rscape_out = "rscape.out";
222 $rscape_sout = $rscape_out.".sorted";
223  
224 my $rscape_options = " -o $rscape_out --rna --outdir $oodir ";
225 if ($self->has_nofigures && $self->nofigures == 1){$rscape_options.=" --nofigures "};
226 if ($self->has_statistic){$rscape_options.=" --".$self->statistic." " }
227 my $cmd = $rscape.$rscape_options.$self->ifile;
228  
229 my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
230 run( command => $cmd, verbose => 0 );
231 if( !$success ) {
232 print STDERR "ERROR [$this_function] Call to $rscape unsuccessful\n";
233 print STDERR "ERROR: $cmd\n";
234 print STDERR "ERROR: this is what the command printed:\n";
235 print join "", @$full_buf;
236 croak $!;
237 }
238  
239 $self->_parse_rscape($rscape_out);
240  
241 rename $rscape_out, $out;
242 rename $rscape_sout, $sout;
243 }
244  
245 # parse R-scape output
246 # canonical R-scape output is expected to look like this:
247 # MSA myaln nseq 9 (9) alen 45 (45) avgid 79.51 (79.51) nbpairs 11 (11)
248 # contacts 11 (11 bpairs 11 wc bpairs)
249 # maxD 8.00
250 # mind 1
251 # Method Target_E-val [cov_min,conv_max] [FP | TP True Found | Sen PPV F]
252 # RAFS 0.05 [-1.00,0.83] [0 | 3 11 3 | 27.27 100.00 42.86]
253 # left_pos right_pos score E-value
254 # ------------------------------------------------------------
255 #* 20 36 0.83 0.000328218
256 #* 21 35 0.83 0.000328218
257 #* 22 34 0.33 0.0497359
258  
259 sub _parse_rscape {
260 my ($self,$out) = @_;
261 my $this_function = (caller(0))[3];
262 my @buffer = ();
263 my $parse1 = 0;
264 my $parse2 = 0;
265 my $nosbp = 0;
266 open my $file, "<", $out or croak "ERROR: [$this_function] Cannot open file $out";
267 while(<$file>){
268 chomp;
269 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){
270 $self->nseq($2);
271 $self->alen($3);
272 $self->nbpairs($5);
273 $parse1 = 1;
274 next;
275 }
276 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){
277 $self->evalue($2);
278 $self->FP($5);
279 $self->TP($6);
280 $self->T($7);
281 $self->F($8);
282 $self->Sen($9);
283 $self->PPV($10);
284 $self->Fmeasure($11);
285 $parse2 = 1;
286 next;
287 }
288 if (m/^\*\s+(\d+)\s+(\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)$/){
289 my %bp = (i => $1,
290 j => $2,
291 score => $3,
292 evalue => $4);
293 push @{$self->sigBP}, \%bp;
294 }
295 if (m/^no significant pairs$/){
296 $nosbp=1;
297 }
298 }
299 close ($file);
300 #carp "INFO: [$this_function] parse1:".eval($parse1);
301 #carp "INFO: [$this_function] parse2:".eval($parse2);
302  
303 if ($nosbp == 1){
304 $self->status(1); # no significant basepairs
305 $self->_check_attributes();
306 return;
307 }
308 if ($parse1 == 1 && $parse2 == 1){
309 $self->_check_attributes();
310 $self->status(0); # all OK
311 return;
312 }
313 elsif ($parse1 == 1 && $parse2 == 0){
314 $self->status(2); # covariation scores are almost constant, no further analysis
315 return;
316 }
317 else{
318 croak "ERROR: [$this_function] ambiguous status when parsing rscape output file. parse1:".eval($parse1)." parse2:".eval($parse2)." This shouldn't happen ...";
319 $self->status(3);
320 return;
321 }
322 }
323  
324 sub _check_attributes {
325 my ($self,$out) = @_;
326 my $this_function = (caller(0))[3];
327  
328 $self->has_cseq ? 1 : croak "ERROR [$this_function] \$self->cseq not set ". $self->ifile;
329 $self->has_nseq ? 1 : croak "ERROR [$this_function] \$self->nseq not set ". $self->ifile;
330 $self->has_alen ? 1 : croak "ERROR [$this_function] \$self->alen not set". $self->ifile;
331 $self->has_nbpairs ? 1 : croak "ERROR [$this_function] \$self->nbpairs not set". $self->ifile;
332 $self->has_evalue ? 1 : croak "ERROR [$this_function] \$self->evalue not set". $self->ifile;
333 $self->has_FP ? 1 : croak "ERROR [$this_function] \$self->FP not set". $self->ifile;
334 $self->has_TP ? 1 : croak "ERROR [$this_function] \$self->TP not set". $self->ifile;
335 $self->has_T ? 1 : croak "ERROR [$this_function] \$self->T not set". $self->ifile;
336 $self->has_F ? 1 : croak "ERROR [$this_function] \$self->F not set". $self->ifile;
337 $self->has_Sen ? 1 : croak "ERROR [$this_function] \$self->Sen not set". $self->ifile;
338 $self->has_PPV ? 1 : croak "ERROR [$this_function] \$self->PPV not set". $self->ifile;
339 $self->has_Fmeasure ? 1 : croak "ERROR [$this_function] \$self->Fmeasure not set". $self->ifile;
340 }
341  
342 1;