File Coverage

blib/lib/Bio/RNA/RNAaliSplit/WrapRNAalifold.pm
Criterion Covered Total %
statement 30 107 28.0
branch 0 22 0.0
condition n/a
subroutine 10 13 76.9
pod 0 2 0.0
total 40 144 27.7


line stmt bran cond sub pod time code
1             # -*-CPerl-*-
2             # Last changed Time-stamp: <2017-03-09 16:16:14 michl>
3              
4             # Bio::RNA::RNAaliSplit::WrapRNAz.pm: A versatile object-oriented
5             # wrapper for RNAalifold
6             #
7             # Requires RNAalifold executable from the ViennaRNA package available
8             # to the Perl interpreter.
9              
10             package Bio::RNA::RNAaliSplit::WrapRNAalifold;
11              
12 1     1   1472 use version; our $VERSION = qv('0.04');
  1         1  
  1         8  
13 1     1   92 use Carp;
  1         2  
  1         82  
14 1     1   6 use Data::Dumper;
  1         1  
  1         37  
15 1     1   4 use Moose;
  1         0  
  1         7  
16 1     1   4933 use Moose::Util::TypeConstraints;
  1         1  
  1         10  
17 1     1   1290 use Path::Class::File;
  1         2  
  1         25  
18 1     1   4 use Path::Class::Dir;
  1         1  
  1         16  
19 1     1   3 use Path::Class;
  1         1  
  1         51  
20 1     1   5 use File::Basename;
  1         2  
  1         53  
21 1     1   3 use IPC::Cmd qw(can_run run);
  1         2  
  1         982  
22              
23             my ($rnaalifold,$oodir);
24              
25             has 'alnfilebasename' => (
26             is => 'rw',
27             isa => 'Str',
28             predicate => 'has_alnfilebasename',
29             init_arg => undef, # make this unsettable via constructor
30             );
31              
32             has 'bn' => (
33             is => 'rw',
34             isa => 'Str',
35             predicate => 'has_basename',
36             documentation => q(Set this to override output basename),
37             );
38              
39             has 'ribosum' => (
40             is => 'rw',
41             isa => 'Num',
42             predicate => 'has_ribosum',
43             documentation => q(Ribosum scoring),
44             );
45              
46             has 'sci' => (
47             is => 'rw',
48             isa => 'Num',
49             init_arg => undef,
50             documentation => q(Structure conservation index),
51             );
52              
53             has 'consensus_struc' => (
54             is => 'rw',
55             isa => 'Str',
56             predicate => 'has_consensus_struc',
57             init_arg => undef,
58             );
59              
60             has 'consensus_mfe' => (
61             is => 'rw',
62             isa => 'Num',
63             predicate => 'has_consensus_mfe',
64             init_arg => undef,
65             );
66              
67             has 'consensus_energy' => (
68             is => 'rw',
69             isa => 'Num',
70             predicate => 'has_consensus_energy',
71             init_arg => undef,
72             );
73              
74             has 'consensus_covar_terms' => (
75             is => 'rw',
76             isa => 'Num',
77             predicate => 'has_consensus_covar_terms',
78             init_arg => undef,
79             );
80              
81             has 'RNAalifold_version' => (
82             is => 'rw',
83             isa => 'String',
84             init_arg => undef,
85             );
86              
87             with 'Bio::RNA::RNAaliSplit::FileDir';
88              
89             #sub BUILDARGS {
90             # my $self = shift;
91             # print Dumper($self);
92             #}
93              
94             sub BUILD {
95 0     0 0   my $self = shift;
96 0           my $this_function = (caller(0))[3];
97 0 0         confess "ERROR [$this_function] \$self->ifile not available"
98             unless ($self->has_ifile);
99 0 0         $rnaalifold = can_run('RNAalifold') or
100             croak "ERROR [$this_function] RNAalifold not found";
101 0 0         unless($self->has_odir){
102 0 0         unless($self->has_odirn){self->odirname("as")}
  0            
103 0           $self->odir( [$self->ifile->dir,$self->odirn] );
104 0           mkdir($self->odir);
105             }
106 0           $oodir = $self->odir->subdir("alifold");
107 0           mkdir($oodir);
108 0           $self->alnfilebasename(fileparse($self->ifile->basename, qr/\.[^.]*/));
109              
110 0           $self->run_rnaalifold();
111             }
112              
113             sub run_rnaalifold {
114 0     0 0   my $self = shift;
115 0           my $this_function = (caller(0))[3];
116 0           my ($out_fn,$out,$alnps_fn,$alnps,$alirnaps_fn,$stk_fn);
117 0           my ($alirnaps,$alidotps_fn,$alidotps,$alifoldstk);
118 0           my $tag = "";
119 0 0         if ($self->has_ribosum){$tag = ".risobum"}
  0            
120 0 0         if ($self->has_alnfilebasename){
    0          
121 0           $out_fn = $self->alnfilebasename.$tag."."."alifold.out";
122 0           $alnps_fn = $self->alnfilebasename.$tag."."."aln.ps";
123 0           $alirnaps_fn = $self->alnfilebasename.$tag."."."alirna.ps";
124 0           $alidotps_fn = $self->alnfilebasename.$tag."."."alidot.ps";
125 0           $stk_fn = $self->alnfilebasename.$tag."."."alifold.stk";
126             }
127             elsif ($self->has_basename){
128 0           $out_fn = $self->bn.$tag."."."alifold.out";
129 0           $alnps_fn = $self->bn.$tag."."."aln.ps";
130 0           $alirnaps_fn = $self->bn.$tag."."."alirna.ps";
131 0           $alidotps_fn = $self->bn.$tag."."."alidot.ps";
132 0           $stk_fn = $self->bn.$tag."."."alifold.stk";
133             }
134             else{
135 0           $out_fn = $tag."alifold.out";
136 0           $alnps_fn = $tag."aln.ps";
137 0           $alirnaps_fn = $tag."alirna.ps";
138 0           $alidotps_fn = $tag."alidot.ps";
139             }
140 0           $out = file($oodir,$out_fn); # RNAalifold stdout
141 0           $alnps = file($oodir,$alnps_fn); # RNAalifold aln.ps
142 0           $alirnaps = file($oodir,$alirnaps_fn); # RNAalifold alirna.ps
143 0           $alidotps = file($oodir,$alidotps_fn); # RNAalifold alidot.ps
144 0           $alifoldstk = file($oodir,$stk_fn); # RNAalifold generated Stockholm file with new CS
145              
146 0           open my $fh, ">", $out;
147 0           my $alifold_options = " --aln --color --cfactor 0.6 --nfactor 0.5 -p --sci --aln-stk ";
148 0 0         if ($self->has_ribosum){$alifold_options.=" -r "}
  0            
149 0           my $cmd = $rnaalifold.$alifold_options.$self->ifile;
150 0           my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
151             run( command => $cmd, verbose => 0 );
152 0 0         if( !$success ) {
153 0           print STDERR "ERROR [$this_function] Call to $rnaalifold unsuccessful\n";
154 0           print STDERR "ERROR: $cmd\n";
155 0           print STDERR "ERROR: this is what the command printed:\n";
156 0           print join "", @$full_buf;
157 0           croak $!;
158             }
159 0           my $stdout_buffer = join "", @$stdout_buf;
160 0           my @rnaalifoldout = split /\n/, $stdout_buffer;
161 0           foreach my $line( @rnaalifoldout){
162 0           print $fh $line,"\n";
163             }
164 0           close($fh);
165              
166 0           $self->_parse_rnaalifold($stdout_buffer);
167 0           rename "aln.ps", $alnps;
168 0           rename "alirna.ps", $alirnaps;
169 0           rename "alidot.ps", $alidotps;
170 0           rename "RNAalifold_results.stk", $alifoldstk;
171 0           unlink "alifold.out";
172             }
173              
174             # parse RNAalifold output
175             sub _parse_rnaalifold {
176 0     0     my ($self,$out) = @_;
177 0           my $this_function = (caller(0))[3];
178 0           my @buffer=split(/^/, $out);
179              
180 0           foreach my $i (0..$#buffer){
181 0 0         next unless ($i == 1); # just parse consensus structure
182 0 0         unless ($buffer[$i] =~ m/([\(\)\.]+)\s+\(\s*(-?\d+\.\d+)\s+=\s+(-?\d+\.\d+)\s+\+\s+(-?\d+\.\d+)\)\s+\[sci\s+=\s+(-?\d+\.\d+)\]/){
183 0           carp "ERROR [$this_function] cannot parse RNAalifold output:";
184 0           croak $self->ifile.":\n$buffer[$i]";
185             }
186 0           $self->consensus_struc($1);
187 0           $self->consensus_mfe($2);
188 0           $self->consensus_energy($3);
189 0           $self->consensus_covar_terms($4);
190 0           $self->sci($5);
191 0           last;
192             }
193             }
194              
195             1;