| 1 | # -*-CPerl-*- | ||
| 2 | # Last changed Time-stamp: <2019-08-25 21:19:14 mtw> | ||
| 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 | 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 | #use diagnostics; | ||
| 20 | |||
| 21 | my ($rnaz,$oodir); | ||
| 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 'P' => ( | ||
| 31 | is => 'rw', | ||
| 32 | isa => 'Num', | ||
| 33 | init_arg => undef, | ||
| 34 | documentation => q(SVM RNA-class probability), | ||
| 35 | ); | ||
| 36 | |||
| 37 | has 'z' => ( | ||
| 38 | is => 'rw', | ||
| 39 | isa => 'Num', | ||
| 40 | init_arg => undef, | ||
| 41 | documentation => q(Mean z-score), | ||
| 42 | ); | ||
| 43 | |||
| 44 | has 'sci' => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => 'Num', | ||
| 47 | init_arg => undef, | ||
| 48 | documentation => q(Structure conservation index), | ||
| 49 | ); | ||
| 50 | |||
| 51 | with 'FileDirUtil'; | ||
| 52 | |||
| 53 | sub BUILD { | ||
| 54 | my $self = shift; | ||
| 55 | my $this_function = (caller(0))[3]; | ||
| 56 | confess "ERROR [$this_function] \$self->ifile not available" | ||
| 57 | unless ($self->has_ifile); | ||
| 58 | $rnaz = can_run('RNAz') or | ||
| 59 | croak "ERROR [$this_function] RNAz not found"; | ||
| 60 | unless($self->has_odir){ | ||
| 61 | my $odir_name = "as"; | ||
| 62 | $self->odir( [$self->ifile->dir,$odir_name] ); | ||
| 63 | } | ||
| 64 | $oodir = $self->odir->subdir("rnaz"); | ||
| 65 | my @created = make_path($oodir, {error => \my $err}); | ||
| 66 | confess "ERROR [$this_function] could not create output directory $self->oodir" | ||
| 67 | if (@$err); | ||
| 68 | $self->set_ifilebn; | ||
| 69 | $self->run_rnaz(); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub run_rnaz { | ||
| 73 | my $self = shift; | ||
| 74 | my $this_function = (caller(0))[3]; | ||
| 75 | my ($rnaz_outfilename,$rnaz_out); | ||
| 76 | if ($self->has_basename){$rnaz_outfilename = $self->basename.".rnaz.out"} | ||
| 77 | elsif ($self->has_ifilebn){$rnaz_outfilename = $self->ifilebn.".rnaz.out"} | ||
| 78 | else{$rnaz_outfilename = "rnaz.out"} | ||
| 79 | $rnaz_out = file($oodir,$rnaz_outfilename); | ||
| 80 | open my $fh, ">", $rnaz_out; | ||
| 81 | my $rnaz_cmd = $rnaz." -l -d < ".$self->ifile; | ||
| 82 | my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) = | ||
| 83 | run( command => $rnaz_cmd, verbose => 0 ); | ||
| 84 | if( !$success ) { | ||
| 85 | print STDERR "ERROR [$this_function] Call to $rnaz unsuccessful\n"; | ||
| 86 | print STDERR "ERROR: this is what the command printed:\n"; | ||
| 87 | print join "", @$full_buf; | ||
| 88 | croak $!; | ||
| 89 | } | ||
| 90 | my $stdout_buffer = join "", @$stdout_buf; | ||
| 91 | my @out = split /\n/, $stdout_buffer; | ||
| 92 | foreach my $line( @out){print $fh $line,"\n"} | ||
| 93 | close($fh); | ||
| 94 | $self->_parse_rnaz($stdout_buffer); | ||
| 95 | } | ||
| 96 | |||
| 97 | # parse RNAz output | ||
| 98 | sub _parse_rnaz { | ||
| 99 | my ($self,$rnaz) = @_; | ||
| 100 | my $this_function = (caller(0))[3]; | ||
| 101 | my @rnaz=split(/^/, $rnaz); | ||
| 102 | my ($N,$identity,$columns,$decValue,$P,$z,$sci,$energy,$strand, | ||
| 103 | $covariance,$combPerPair,$meanMFE,$consensusMFE,$consensusSeq, | ||
| 104 | $consensusFold, $GCcontent, $ShannonEntropy); | ||
| 105 | my @aln=(); | ||
| 106 | |||
| 107 | foreach my $i (0..$#rnaz){ | ||
| 108 | my $line=$rnaz[$i]; | ||
| 109 | $identity=$1 if ($line=~/Mean pairwise identity:\s*(-?\d+.\d+)/); | ||
| 110 | $N=$1 if ($line=~/Sequences:\s*(\d+)/); | ||
| 111 | if ($line=~/Reading direction:\s*(forward|reverse)/){ | ||
| 112 | $strand=($1 eq 'forward')?'+':'-'; | ||
| 113 | } | ||
| 114 | $columns=$1 if ($line=~/Columns:\s*(\d+)/); | ||
| 115 | $decValue=$1 if ($line=~/SVM decision value:\s*(-?\d+.\d+)/); | ||
| 116 | $P=$1 if ($line=~/SVM RNA-class probability:\s*(-?\d+.\d+)/); | ||
| 117 | $z=$1 if ($line=~/Mean z-score:\s*(-?\d+.\d+)/); | ||
| 118 | $sci=$1 if ($line=~/Structure conservation index:\s*(-?\d+.\d+)/); | ||
| 119 | $energy=$1 if ($line=~/Energy contribution:\s*(-?\d+.\d+)/); | ||
| 120 | $covariance=$1 if ($line=~/Covariance contribution:\s*(-?\d+.\d+)/); | ||
| 121 | $combPerPair=$1 if ($line=~/Combinations\/Pair:\s*(-?\d+.\d+)/); | ||
| 122 | $consensusMFE=$1 if ($line=~/Consensus MFE:\s*(-?\d+.\d+)/); | ||
| 123 | $meanMFE=$1 if ($line=~/Mean single sequence MFE:\s*(-?\d+.\d+)/); | ||
| 124 | $GCcontent=$1 if ($line=~/G\+C content:\s(\d+.\d+)/); | ||
| 125 | $ShannonEntropy=$1 if ($line=~/Shannon entropy:\s*(\d+.\d+)/); | ||
| 126 | |||
| 127 | if ($line=~/^>/){ | ||
| 128 | chomp($rnaz[$i+1]); | ||
| 129 | chomp($rnaz[$i+2]); | ||
| 130 | if ($line=~/^>consensus/){ | ||
| 131 | $consensusSeq=$rnaz[$i+1]; | ||
| 132 | $consensusFold=substr($rnaz[$i+2],0,length($rnaz[$i+1])); | ||
| 133 | last; | ||
| 134 | } else { | ||
| 135 | if ($line=~/>(.*?) (\d+) (\d+) (\+|\-) (\d+)/){ | ||
| 136 | push @aln, {name=>$1, | ||
| 137 | start=>$2, | ||
| 138 | end=>$2+$3, | ||
| 139 | strand=>$4, | ||
| 140 | fullLength=>$5, | ||
| 141 | seq=>$rnaz[$i+1], | ||
| 142 | fold=>substr($rnaz[$i+2],0,length($rnaz[$i+1]))}; | ||
| 143 | $i+=2; | ||
| 144 | } elsif ($line=~/^(.*)\/(\d+)-(\d+)$/){ | ||
| 145 | push @aln, {name=>$1, | ||
| 146 | start=>$2, | ||
| 147 | end=>$3, | ||
| 148 | strand=>$strand, | ||
| 149 | fullLength=>'', | ||
| 150 | seq=>$rnaz[$i+1], | ||
| 151 | fold=>substr($rnaz[$i+2],0,length($rnaz[$i+1]))}; | ||
| 152 | $i+=2; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | $self->P($P); | ||
| 159 | $self->z($z); | ||
| 160 | $self->sci($sci); | ||
| 161 | } | ||
| 162 | |||
| 163 | 1; |