| 1 | # -*-CPerl-*- | ||
| 2 | # Last changed Time-stamp: <2019-08-25 21:19:37 mtw> | ||
| 3 | |||
| 4 | # Bio::RNA::RNAaliSplit::WrapRNAalifold.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 | 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 ($rnaalifold,$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 'ribosum' => ( | ||
| 31 | is => 'rw', | ||
| 32 | isa => 'Num', | ||
| 33 | predicate => 'has_ribosum', | ||
| 34 | documentation => q(Ribosum scoring), | ||
| 35 | ); | ||
| 36 | |||
| 37 | has 'SS_cons' => ( | ||
| 38 | is => 'rw', | ||
| 39 | isa => 'Num', | ||
| 40 | predicate => 'has_sscons', | ||
| 41 | documentation => q(Use consensus structures from Stockholm file), | ||
| 42 | ); | ||
| 43 | |||
| 44 | has 'sci' => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => 'Num', | ||
| 47 | init_arg => undef, | ||
| 48 | documentation => q(Structure conservation index), | ||
| 49 | ); | ||
| 50 | |||
| 51 | has 'format' => ( | ||
| 52 | is => 'rw', | ||
| 53 | isa => 'Str', | ||
| 54 | default => 'C', | ||
| 55 | predicate => 'has_format', | ||
| 56 | documentation => q(File format of the input multiple sequence alignment (MSA)), | ||
| 57 | ); | ||
| 58 | |||
| 59 | has 'consensus_struc' => ( | ||
| 60 | is => 'rw', | ||
| 61 | isa => 'Str', | ||
| 62 | predicate => 'has_consensus_struc', | ||
| 63 | init_arg => undef, | ||
| 64 | ); | ||
| 65 | |||
| 66 | has 'consensus_mfe' => ( | ||
| 67 | is => 'rw', | ||
| 68 | isa => 'Num', | ||
| 69 | predicate => 'has_consensus_mfe', | ||
| 70 | init_arg => undef, | ||
| 71 | ); | ||
| 72 | |||
| 73 | has 'consensus_energy' => ( | ||
| 74 | is => 'rw', | ||
| 75 | isa => 'Num', | ||
| 76 | predicate => 'has_consensus_energy', | ||
| 77 | init_arg => undef, | ||
| 78 | ); | ||
| 79 | |||
| 80 | has 'consensus_covar_terms' => ( | ||
| 81 | is => 'rw', | ||
| 82 | isa => 'Num', | ||
| 83 | predicate => 'has_consensus_covar_terms', | ||
| 84 | init_arg => undef, | ||
| 85 | ); | ||
| 86 | |||
| 87 | has 'RNAalifold_version' => ( | ||
| 88 | is => 'rw', | ||
| 89 | isa => 'String', | ||
| 90 | init_arg => undef, | ||
| 91 | ); | ||
| 92 | |||
| 93 | has 'alignment_stk' => ( | ||
| 94 | is => 'rw', | ||
| 95 | isa => 'Path::Class::File', | ||
| 96 | predicate => 'has_stk', | ||
| 97 | init_arg => undef, | ||
| 98 | ); | ||
| 99 | |||
| 100 | with 'FileDirUtil'; | ||
| 101 | |||
| 102 | sub BUILD { | ||
| 103 | my $self = shift; | ||
| 104 | my $this_function = (caller(0))[3]; | ||
| 105 | confess "ERROR [$this_function] \$self->ifile not available" | ||
| 106 | unless ($self->has_ifile); | ||
| 107 | $rnaalifold = can_run('RNAalifold') or | ||
| 108 | croak "ERROR [$this_function] RNAalifold not found"; | ||
| 109 | unless($self->has_odir){ | ||
| 110 | my $odir_name = "as"; | ||
| 111 | $self->odir( [$self->ifile->dir,$odir_name] ); | ||
| 112 | } | ||
| 113 | $oodir = $self->odir->subdir("alifold"); | ||
| 114 | my @created = make_path($oodir, {error => \my $err}); | ||
| 115 | confess "ERROR [$this_function] could not create output directory $self->oodir" | ||
| 116 | if (@$err); | ||
| 117 | $self->set_ifilebn; | ||
| 118 | $self->run_rnaalifold(); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub run_rnaalifold { | ||
| 122 | my $self = shift; | ||
| 123 | my $this_function = (caller(0))[3]; | ||
| 124 | my ($out_fn,$out,$alnps_fn,$alnps,$alirnaps_fn,$stk_fn); | ||
| 125 | my ($alirnaps,$alidotps_fn,$alidotps,$alifoldstk); | ||
| 126 | my $tag = ""; | ||
| 127 | if ($self->has_ribosum){$tag = ".risobum"} | ||
| 128 | if ($self->has_basename){ | ||
| 129 | $out_fn = $self->bn.$tag."."."alifold.out"; | ||
| 130 | $alnps_fn = $self->bn.$tag."."."aln.ps"; | ||
| 131 | $alirnaps_fn = $self->bn.$tag."."."alirna.ps"; | ||
| 132 | $alidotps_fn = $self->bn.$tag."."."alidot.ps"; | ||
| 133 | $stk_fn = $self->bn.$tag."."."alifold.stk"; | ||
| 134 | } | ||
| 135 | elsif ($self->has_ifilebn){ | ||
| 136 | $out_fn = $self->ifilebn.$tag."."."alifold.out"; | ||
| 137 | $alnps_fn = $self->ifilebn.$tag."."."aln.ps"; | ||
| 138 | $alirnaps_fn = $self->ifilebn.$tag."."."alirna.ps"; | ||
| 139 | $alidotps_fn = $self->ifilebn.$tag."."."alidot.ps"; | ||
| 140 | $stk_fn = $self->ifilebn.$tag."."."alifold.stk"; | ||
| 141 | } | ||
| 142 | else{ | ||
| 143 | $out_fn = $tag."alifold.out"; | ||
| 144 | $alnps_fn = $tag."aln.ps"; | ||
| 145 | $alirnaps_fn = $tag."alirna.ps"; | ||
| 146 | $alidotps_fn = $tag."alidot.ps"; | ||
| 147 | } | ||
| 148 | $out = file($oodir,$out_fn); # RNAalifold stdout | ||
| 149 | $alnps = file($oodir,$alnps_fn); # RNAalifold aln.ps | ||
| 150 | $alirnaps = file($oodir,$alirnaps_fn); # RNAalifold alirna.ps | ||
| 151 | $alidotps = file($oodir,$alidotps_fn); # RNAalifold alidot.ps | ||
| 152 | $alifoldstk = file($oodir,$stk_fn); # RNAalifold generated Stockholm file with new CS | ||
| 153 | |||
| 154 | open my $fh, ">", $out; | ||
| 155 | my $alifold_options = " --aln --color -p --sci --aln-stk "; | ||
| 156 | #$alifold_options .= " --cfactor 0.6 --nfactor 0.5 "; | ||
| 157 | $alifold_options .= " -f ".$self->format." "; | ||
| 158 | if ($self->has_ribosum){$alifold_options.=" -r "} | ||
| 159 | if ($self->has_sscons){$alifold_options.=" --SS_cons "} | ||
| 160 | my $cmd = $rnaalifold.$alifold_options.$self->ifile; | ||
| 161 | my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) = | ||
| 162 | run( command => $cmd, verbose => 0 ); | ||
| 163 | if( !$success ) { | ||
| 164 | print STDERR "ERROR [$this_function] Call to $rnaalifold unsuccessful\n"; | ||
| 165 | print STDERR "ERROR: $cmd\n"; | ||
| 166 | print STDERR "ERROR: this is what the command printed:\n"; | ||
| 167 | print join "", @$full_buf; | ||
| 168 | croak $!; | ||
| 169 | } | ||
| 170 | my $stdout_buffer = join "", @$stdout_buf; | ||
| 171 | my @rnaalifoldout = split /\n/, $stdout_buffer; | ||
| 172 | foreach my $line( @rnaalifoldout){ | ||
| 173 | print $fh $line,"\n"; | ||
| 174 | } | ||
| 175 | close($fh); | ||
| 176 | |||
| 177 | $self->_parse_rnaalifold($stdout_buffer); | ||
| 178 | $self->alignment_stk($alifoldstk); | ||
| 179 | rename "aln.ps", $alnps; | ||
| 180 | rename "alirna.ps", $alirnaps; | ||
| 181 | rename "alidot.ps", $alidotps; | ||
| 182 | rename "RNAalifold_results.stk", $alifoldstk; | ||
| 183 | unlink "alifold.out"; | ||
| 184 | } | ||
| 185 | |||
| 186 | # parse RNAalifold output | ||
| 187 | sub _parse_rnaalifold { | ||
| 188 | my ($self,$out) = @_; | ||
| 189 | my $this_function = (caller(0))[3]; | ||
| 190 | my @buffer=split(/^/, $out); | ||
| 191 | |||
| 192 | foreach my $i (0..$#buffer){ | ||
| 193 | next unless ($i == 1); # just parse consensus structure | ||
| 194 | unless ($buffer[$i] =~ m/([\(\)\.]+)\s+\(\s*(-?\d+\.\d+)\s+=\s+(-?\d+\.\d+)\s+\+\s+(-?\d+\.\d+)\)\s+\[sci\s+=\s+(-?\d+\.\d+)\]/){ | ||
| 195 | carp "ERROR [$this_function] cannot parse RNAalifold output:"; | ||
| 196 | croak $self->ifile.":\n$buffer[$i]"; | ||
| 197 | } | ||
| 198 | $self->consensus_struc($1); | ||
| 199 | $self->consensus_mfe($2); | ||
| 200 | $self->consensus_energy($3); | ||
| 201 | $self->consensus_covar_terms($4); | ||
| 202 | $self->sci($5); | ||
| 203 | last; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | 1; |