| 1 | #!/usr/bin/env perl | ||
| 2 | # Last changed Time-stamp: <2019-01-07 00:40:19 mtw> | ||
| 3 | # -*-CPerl-*- | ||
| 4 | # | ||
| 5 | # A structural alignment evaluator | ||
| 6 | # | ||
| 7 | # usage: eval_alignment.pl -a myaln.stk --statistics RAFS | ||
| 8 | # | ||
| 9 | |||
| 10 | use version; our $VERSION = qv('0.09'); | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | use File::Basename; | ||
| 14 | use Bio::AlignIO; | ||
| 15 | use Bio::RNA::RNAaliSplit::WrapRscape; | ||
| 16 | use Bio::RNA::RNAaliSplit::WrapRNAz; | ||
| 17 | use Getopt::Long qw( :config posix_default bundling no_ignore_case ); | ||
| 18 | use Data::Dumper; | ||
| 19 | use Pod::Usage; | ||
| 20 | use Path::Class; | ||
| 21 | use Carp; | ||
| 22 | use Cwd; | ||
| 23 | |||
| 24 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 25 | #^^^^^^^^^^ Variables ^^^^^^^^^^^# | ||
| 26 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 27 | |||
| 28 | my $show_version = 0; | ||
| 29 | my ($id,$stkfile,$alnfile,$logfile,$in,$out,$cwd,$bn); | ||
| 30 | my @odir = (); | ||
| 31 | my $handle=undef; | ||
| 32 | my $nofigures = 0; | ||
| 33 | my $outdir = "eval_result"; | ||
| 34 | my $stat = "GTp"; | ||
| 35 | my $have_logfile=0; | ||
| 36 | use diagnostics; | ||
| 37 | |||
| 38 | Getopt::Long::config('no_ignore_case'); | ||
| 39 | pod2usage(-verbose => 1) unless | ||
| 40 | GetOptions("a|aln=s" => \$stkfile, | ||
| 41 | "s|statistic=s" => \$stat, | ||
| 42 | "o|out=s" => \$outdir, | ||
| 43 | "l|log=s" => \&set_logfile, | ||
| 44 | "n|nofigures" => sub{$nofigures = 1}, | ||
| 45 | "version" => sub{$show_version = 1}, | ||
| 46 | "man" => sub{pod2usage(-verbose => 2)}, | ||
| 47 | "help|h" => sub{pod2usage(1)} | ||
| 48 | ); | ||
| 49 | |||
| 50 | if ($show_version == 1){ | ||
| 51 | print "eval_alignment.pl $VERSION\n"; | ||
| 52 | exit(0); | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | if ($have_logfile == 1){ | ||
| 57 | open($handle, ">", $logfile) | ||
| 58 | || die "$0: can't open $logfile for appending: $!"; | ||
| 59 | } | ||
| 60 | else{ | ||
| 61 | open($handle, ">&", STDOUT) | ||
| 62 | || die "$0: can't open handle for STDOUT: $!"; | ||
| 63 | } | ||
| 64 | |||
| 65 | @odir = split /\//, $outdir; | ||
| 66 | $cwd = getcwd(); | ||
| 67 | unless (-f $stkfile){ | ||
| 68 | warn "Could not find input file provided via -a|-aln option"; | ||
| 69 | pod2usage(-verbose => 0); | ||
| 70 | } | ||
| 71 | $bn = basename($stkfile, ".stk"); | ||
| 72 | $alnfile = file($cwd,$bn.".aln"); | ||
| 73 | # convert Stockholm -> Clustal for RNAz | ||
| 74 | $in = Bio::AlignIO->new(-file => $stkfile , | ||
| 75 | -format => "Stockholm"); | ||
| 76 | |||
| 77 | $out = Bio::AlignIO->new(-file => ">$alnfile" , | ||
| 78 | -format => "ClustalW"); | ||
| 79 | |||
| 80 | while ( my $aln = $in->next_aln ) { | ||
| 81 | my $clean = remove_gaponly($aln); | ||
| 82 | $out->write_aln($clean); | ||
| 83 | } | ||
| 84 | |||
| 85 | my $r = Bio::RNA::RNAaliSplit::WrapRscape->new(ifile => $stkfile, | ||
| 86 | statistic => $stat, | ||
| 87 | nofigures => $nofigures, | ||
| 88 | odir => \@odir, | ||
| 89 | ); | ||
| 90 | #print Dumper($r); | ||
| 91 | |||
| 92 | my $z = Bio::RNA::RNAaliSplit::WrapRNAz->new(ifile => $alnfile, | ||
| 93 | odir => \@odir); | ||
| 94 | #print Dumper($z); | ||
| 95 | |||
| 96 | if ($r->cseq <= 1){ # stk file had only one sequence | ||
| 97 | print $handle join("\t","-",$stkfile,"n/a"),"\n"; | ||
| 98 | } | ||
| 99 | else{ # normal stk file | ||
| 100 | my $hint = "-"; | ||
| 101 | my $prob=sprintf("%6.4f",$z->P); | ||
| 102 | my $str; | ||
| 103 | if ($r->status == 0){ # R-scape went through and gave results | ||
| 104 | ($prob>0.9 && $r->TP>1) ? ($hint = "*") : ($hint = "-"); | ||
| 105 | $str = join("\t",$hint,$stkfile,$prob,$r->statistic,$r->TP,$r->alen,$r->nbpairs,$r->nseq)."\n"; | ||
| 106 | } | ||
| 107 | else{ | ||
| 108 | $str = join("\t",$hint,$stkfile,$prob,"nodata")."\n"; | ||
| 109 | } | ||
| 110 | print $handle $str; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | sub set_logfile { | ||
| 115 | $logfile=$_[1]; | ||
| 116 | $have_logfile=1; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub remove_gaponly { | ||
| 120 | my $a = shift; | ||
| 121 | $a->set_displayname_flat(); | ||
| 122 | my $l = $a->length; | ||
| 123 | my $dim = $a->num_sequences; | ||
| 124 | #print "+++ $dim sequences with length $l in alignment +++\n"; | ||
| 125 | my $gapstring = ("-"x$l); | ||
| 126 | #print "gapstring\n$gapstring\n\n\n"; | ||
| 127 | my @keep = (); | ||
| 128 | |||
| 129 | foreach my $i (1..$dim){ | ||
| 130 | my $alnT = $a->get_seq_by_pos($i); | ||
| 131 | my $seq = $alnT->seq(); | ||
| 132 | # check if we have a gap-only substring of length $l | ||
| 133 | index($seq,$gapstring) == 0 ? next : 1; | ||
| 134 | push @keep, $i; | ||
| 135 | #print "keep $seq\n"; | ||
| 136 | } | ||
| 137 | return ( $a->select_noncont(@keep) ); | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | __END__ | ||
| 142 | |||
| 143 | =head1 NAME | ||
| 144 | |||
| 145 | eval_alignment.pl - A structural alignment evaluator | ||
| 146 | |||
| 147 | =head1 SYNOPSIS | ||
| 148 | |||
| 149 | eval_alignment.pl [--aln|-a I<FILE>] [-out|-o I<DIR>] [--log|-l I<FILE>] | ||
| 150 | [--statistic|-s I<STRING>] [--nofigures] [options] | ||
| 151 | |||
| 152 | =head1 DESCRIPTION | ||
| 153 | |||
| 154 | This is a semi-automatic evaluator for RNA alignemnts. In employs | ||
| 155 | R-scape (http://eddylab.org/R-scape/) and RNAz | ||
| 156 | (https://www.tbi.univie.ac.at/software/RNAz) to classify structural | ||
| 157 | RNA alignments. It accepts a single multiple sequence alignment in | ||
| 158 | Stockholm format and runs R-scape and RNAz on it, determining | ||
| 159 | statistically significant covarying base pairs (SSCBP) and RNAz | ||
| 160 | classification parameters. | ||
| 161 | |||
| 162 | This tool prints the RNAz class probability, the applied R-scape | ||
| 163 | covariance statistic, the number of SSCBP, the lenth of the alignment, | ||
| 164 | as well as the number of base pairs and sequences to stdout. R-scapoe | ||
| 165 | and RNAz output files are written to a user-defined directory. | ||
| 166 | |||
| 167 | Note that this script removes gap-only sequences from the alignment | ||
| 168 | since RNAz does not accept them. | ||
| 169 | |||
| 170 | =head1 DISCLAIMER | ||
| 171 | |||
| 172 | This script employs a simple R-scape/RNAz warpper. As such, it does | ||
| 173 | not implement (and pass through) all R-scape/RNAz options. | ||
| 174 | |||
| 175 | Please ensure that R-scape v0.6.1 is installed on your machine and | ||
| 176 | available for your Perl interpreter. | ||
| 177 | |||
| 178 | =head1 OPTIONS | ||
| 179 | |||
| 180 | =over | ||
| 181 | |||
| 182 | =item B<--aln|-a> | ||
| 183 | |||
| 184 | A multiple sequence alignment in Stockholm format. | ||
| 185 | |||
| 186 | =item B<--statistic|-s> | ||
| 187 | |||
| 188 | The covariation statistic used by R-scape. Allowed values are: 'GT', | ||
| 189 | 'MI', 'MIr', 'MIg', 'CHI', 'OMES', 'RAF', 'RAFS'. Appending either 'p' | ||
| 190 | or 'a' to any of them calculates its average product correction and | ||
| 191 | average sum correction, respctively (e.g. GTp or GTa). See the R-scape | ||
| 192 | manual for details. | ||
| 193 | |||
| 194 | =item B<--nofigures|-n> | ||
| 195 | |||
| 196 | Turn off production of graphical R-scape output | ||
| 197 | |||
| 198 | =item B<--log|-l> | ||
| 199 | |||
| 200 | Specify log file; output is written to STDOUT unless specified | ||
| 201 | |||
| 202 | =item B<--version> | ||
| 203 | |||
| 204 | Show wrap-R-scape.pl version and exit | ||
| 205 | |||
| 206 | =back | ||
| 207 | |||
| 208 | =head1 SEE ALSO | ||
| 209 | |||
| 210 | The L<R-scape | ||
| 211 | manual|http://eddylab.org/software/rscape/R-scape_userguide.pdf> and | ||
| 212 | the L<R-scape | ||
| 213 | paper|http://eddylab.org/publications/RivasEddy16/RivasEddy16-preprint.pdf>. | ||
| 214 | |||
| 215 | =head1 AUTHOR | ||
| 216 | |||
| 217 | Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt> and | ||
| 218 | E<lt>michael.wolfinger@univie.ac.atE<gt> | ||
| 219 | |||
| 220 | =cut |