File Coverage

blib/lib/Bio/RNA/RNAaliSplit/WrapRNAz.pm
Criterion Covered Total %
statement 30 103 29.1
branch 0 54 0.0
condition n/a
subroutine 10 13 76.9
pod 0 2 0.0
total 40 172 23.2


line stmt bran cond sub pod time code
1             # -*-CPerl-*-
2             # Last changed Time-stamp: <2017-03-09 16:16:26 michl>
3              
4             # Bio::RNA::RNAaliSplit::WrapRNAz.pm: A versatile object-oriented
5             # wrapper for RNAz
6             #
7             # Requires RNAz executable available to the Perl interpreter.
8             # This package contains code fragments from the original RNAz Perl module
9              
10             package Bio::RNA::RNAaliSplit::WrapRNAz;
11              
12 1     1   1387 use version; our $VERSION = qv('0.04');
  1         2  
  1         8  
13 1     1   78 use Carp;
  1         1  
  1         61  
14 1     1   5 use Data::Dumper;
  1         1  
  1         33  
15 1     1   4 use Moose;
  1         1  
  1         8  
16 1     1   4795 use Moose::Util::TypeConstraints;
  1         1  
  1         9  
17 1     1   1275 use Path::Class::File;
  1         1  
  1         36  
18 1     1   4 use Path::Class::Dir;
  1         1  
  1         17  
19 1     1   4 use Path::Class;
  1         1  
  1         54  
20 1     1   4 use File::Basename;
  1         1  
  1         85  
21 1     1   7 use IPC::Cmd qw(can_run run);
  1         2  
  1         1083  
22              
23             my ($rnaz,$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 'P' => (
40             is => 'rw',
41             isa => 'Num',
42             init_arg => undef,
43             documentation => q(SVM RNA-class probability),
44             );
45              
46             has 'z' => (
47             is => 'rw',
48             isa => 'Num',
49             init_arg => undef,
50             documentation => q(Mean z-score),
51             );
52              
53             has 'sci' => (
54             is => 'rw',
55             isa => 'Num',
56             init_arg => undef,
57             documentation => q(Structure conservation index),
58             );
59              
60             with 'Bio::RNA::RNAaliSplit::FileDir';
61              
62             sub BUILD {
63 0     0 0   my $self = shift;
64 0           my $this_function = (caller(0))[3];
65 0 0         confess "ERROR [$this_function] \$self->ifile not available"
66             unless ($self->has_ifile);
67 0 0         $rnaz = can_run('RNAz') or
68             croak "ERROR [$this_function] RNAz not found";
69 0 0         unless($self->has_odir){
70 0 0         unless($self->has_odirn){self->odirname("as")}
  0            
71 0           $self->odir( [$self->ifile->dir,$self->odirn] );
72 0           mkdir($self->odir);
73             }
74 0           $oodir = $self->odir->subdir("rnaz");
75 0           mkdir($oodir);
76 0           $self->alnfilebasename(fileparse($self->ifile->basename, qr/\.[^.]*/));
77              
78 0           $self->run_rnaz();
79             }
80              
81             sub run_rnaz {
82 0     0 0   my $self = shift;
83 0           my $this_function = (caller(0))[3];
84 0           my ($rnaz_outfilename,$rnaz_out);
85 0 0         if ($self->has_alnfilebasename){$rnaz_outfilename = $self->alnfilebasename.".rnaz.out"}
  0 0          
86 0           elsif ($self->has_basename){$rnaz_outfilename = $self->bn.".rnaz.out"}
87 0           else{$rnaz_outfilename = "rnaz.out"}
88 0           $rnaz_out = file($oodir,$rnaz_outfilename);
89 0           open my $fh, ">", $rnaz_out;
90 0           my $rnaz_cmd = $rnaz." -l -d < ".$self->ifile;
91 0           my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
92             run( command => $rnaz_cmd, verbose => 0 );
93 0 0         if( !$success ) {
94 0           print STDERR "ERROR [$this_function] Call to $rnaz unsuccessful\n";
95 0           print STDERR "ERROR: this is what the command printed:\n";
96 0           print join "", @$full_buf;
97 0           croak $!;
98             }
99 0           my $stdout_buffer = join "", @$stdout_buf;
100 0           my @out = split /\n/, $stdout_buffer;
101 0           foreach my $line( @out){print $fh $line,"\n"}
  0            
102 0           close($fh);
103              
104 0           $self->_parse_rnaz($stdout_buffer);
105             }
106              
107             # parse RNAz output
108             sub _parse_rnaz {
109 0     0     my ($self,$rnaz) = @_;
110 0           my $this_function = (caller(0))[3];
111 0           my @rnaz=split(/^/, $rnaz);
112 0           my ($N,$identity,$columns,$decValue,$P,$z,$sci,$energy,$strand,
113             $covariance,$combPerPair,$meanMFE,$consensusMFE,$consensusSeq,
114             $consensusFold, $GCcontent, $ShannonEntropy);
115 0           my @aln=();
116              
117 0           foreach my $i (0..$#rnaz){
118 0           my $line=$rnaz[$i];
119 0 0         $identity=$1 if ($line=~/Mean pairwise identity:\s*(-?\d+.\d+)/);
120 0 0         $N=$1 if ($line=~/Sequences:\s*(\d+)/);
121 0 0         if ($line=~/Reading direction:\s*(forward|reverse)/){
122 0 0         $strand=($1 eq 'forward')?'+':'-';
123             }
124 0 0         $columns=$1 if ($line=~/Columns:\s*(\d+)/);
125 0 0         $decValue=$1 if ($line=~/SVM decision value:\s*(-?\d+.\d+)/);
126 0 0         $P=$1 if ($line=~/SVM RNA-class probability:\s*(-?\d+.\d+)/);
127 0 0         $z=$1 if ($line=~/Mean z-score:\s*(-?\d+.\d+)/);
128 0 0         $sci=$1 if ($line=~/Structure conservation index:\s*(-?\d+.\d+)/);
129 0 0         $energy=$1 if ($line=~/Energy contribution:\s*(-?\d+.\d+)/);
130 0 0         $covariance=$1 if ($line=~/Covariance contribution:\s*(-?\d+.\d+)/);
131 0 0         $combPerPair=$1 if ($line=~/Combinations\/Pair:\s*(-?\d+.\d+)/);
132 0 0         $consensusMFE=$1 if ($line=~/Consensus MFE:\s*(-?\d+.\d+)/);
133 0 0         $meanMFE=$1 if ($line=~/Mean single sequence MFE:\s*(-?\d+.\d+)/);
134 0 0         $GCcontent=$1 if ($line=~/G\+C content:\s(\d+.\d+)/);
135 0 0         $ShannonEntropy=$1 if ($line=~/Shannon entropy:\s*(\d+.\d+)/);
136              
137 0 0         if ($line=~/^>/){
138 0           chomp($rnaz[$i+1]);
139 0           chomp($rnaz[$i+2]);
140 0 0         if ($line=~/^>consensus/){
141 0           $consensusSeq=$rnaz[$i+1];
142 0           $consensusFold=substr($rnaz[$i+2],0,length($rnaz[$i+1]));
143 0           last;
144             } else {
145 0 0         if ($line=~/>(.*?) (\d+) (\d+) (\+|\-) (\d+)/){
    0          
146 0           push @aln, {name=>$1,
147             start=>$2,
148             end=>$2+$3,
149             strand=>$4,
150             fullLength=>$5,
151             seq=>$rnaz[$i+1],
152             fold=>substr($rnaz[$i+2],0,length($rnaz[$i+1]))};
153 0           $i+=2;
154             } elsif ($line=~/^(.*)\/(\d+)-(\d+)$/){
155 0           push @aln, {name=>$1,
156             start=>$2,
157             end=>$3,
158             strand=>$strand,
159             fullLength=>'',
160             seq=>$rnaz[$i+1],
161             fold=>substr($rnaz[$i+2],0,length($rnaz[$i+1]))};
162 0           $i+=2;
163             }
164             }
165             }
166             }
167              
168 0           $self->P($P);
169 0           $self->z($z);
170 0           $self->sci($sci);
171             }
172              
173             1;