| 1 | #!/usr/bin/env perl | ||
| 2 | # Last changed Time-stamp: <2019-04-05 22:54:35 mtw> | ||
| 3 | # -*-CPerl-*- | ||
| 4 | # | ||
| 5 | # usage: RNAalisplit.pl -a myfile.aln | ||
| 6 | # | ||
| 7 | # NB: Display ID handling in Bio::AlignIO is broken for Stockholm | ||
| 8 | # format. Use ClustalW format instead !!! | ||
| 9 | |||
| 10 | use version; our $VERSION = qv('0.10'); | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | use Bio::RNA::RNAaliSplit; | ||
| 14 | use Bio::RNA::RNAaliSplit::WrapRNAz; | ||
| 15 | use Bio::RNA::RNAaliSplit::WrapRscape; | ||
| 16 | use Bio::RNA::RNAaliSplit::WrapRNAalifold; | ||
| 17 | use Bio::RNA::RNAaliSplit::WrapAnalyseDists; | ||
| 18 | use Getopt::Long qw( :config posix_default bundling no_ignore_case ); | ||
| 19 | use Data::Dumper; | ||
| 20 | use Pod::Usage; | ||
| 21 | use Path::Class; | ||
| 22 | use File::Basename; | ||
| 23 | use Carp; | ||
| 24 | use RNA; | ||
| 25 | use diagnostics; | ||
| 26 | |||
| 27 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 28 | #^^^^^^^^^^ Variables ^^^^^^^^^^^# | ||
| 29 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 30 | |||
| 31 | my $format = "ClustalW"; | ||
| 32 | my $method = "dHn"; # SCI | dHn | dHx | dBp | dBc | dHB | ||
| 33 | my $rscape_stat = "GTp"; | ||
| 34 | my $outdir = "as"; | ||
| 35 | my $verbose = undef; | ||
| 36 | my $rnaz=undef; | ||
| 37 | my @nseqs=(); | ||
| 38 | my ($alifile,$fi); | ||
| 39 | my $scaleH = 1.; | ||
| 40 | my $scaleB = 1.; | ||
| 41 | my $ribosum = 1; | ||
| 42 | my $constraint=undef; | ||
| 43 | my $show_version = 0; | ||
| 44 | my %foldme = (); # HoH of folded input sequences for dBp/dBc computation | ||
| 45 | |||
| 46 | my %pair = ("AU" => 5, | ||
| 47 | "GC" => 1, | ||
| 48 | "CG" => 2, | ||
| 49 | "UA" => 6, | ||
| 50 | "GU" => 3, | ||
| 51 | "GT" => 3, | ||
| 52 | "TG" => 4, | ||
| 53 | "UG" => 4, | ||
| 54 | "AT" => 5, | ||
| 55 | "TA" => 6); | ||
| 56 | |||
| 57 | |||
| 58 | Getopt::Long::config('no_ignore_case'); | ||
| 59 | pod2usage(-verbose => 1) unless GetOptions("aln|a=s" => \$alifile, | ||
| 60 | "constraint|c=s" => \$constraint, | ||
| 61 | "method|m=s" => \$method, | ||
| 62 | "noribosum" => sub{$ribosum=0}, | ||
| 63 | "out|o=s" => \$outdir, | ||
| 64 | "rscapestat=s" => \$rscape_stat, | ||
| 65 | "scaleH" => \$scaleH, | ||
| 66 | "scaleB" => \$scaleB, | ||
| 67 | "verbose|v" => sub{$verbose = 1}, | ||
| 68 | "version" => sub{$show_version = 1}, | ||
| 69 | "man" => sub{pod2usage(-verbose => 2)}, | ||
| 70 | "help|h" => sub{pod2usage(1)} | ||
| 71 | ); | ||
| 72 | |||
| 73 | if ($show_version == 1){ | ||
| 74 | print "RNAalisplit $VERSION\n"; | ||
| 75 | exit(0); | ||
| 76 | } | ||
| 77 | |||
| 78 | unless (-f $alifile){ | ||
| 79 | warn "Could not find input file provided via --aln|-a option"; | ||
| 80 | pod2usage(-verbose => 0); | ||
| 81 | } | ||
| 82 | |||
| 83 | croak "ERROR: method dBc must be selected when using constraints, exiting ..." | ||
| 84 | if (defined $constraint && $method ne "dBc"); | ||
| 85 | |||
| 86 | if($method eq "dBp" || $method eq "dHB"){ | ||
| 87 | $fi = fold_input_alignment($alifile); # for later computation of BP dist | ||
| 88 | } | ||
| 89 | if($method eq "dBc"){ | ||
| 90 | $fi = fold_input_alignment_constrained($alifile,$constraint); | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | my $round = 1; | ||
| 95 | my $done = 0; | ||
| 96 | while ($done != 1){ | ||
| 97 | my $lround = sprintf("%03d", $round); | ||
| 98 | my $current_round_name = "round_".$lround; | ||
| 99 | my $odirname = dir($outdir,$current_round_name); | ||
| 100 | print STDERR "Computing round $lround ...\n"; | ||
| 101 | alisplit($alifile,$odirname); | ||
| 102 | $round++; | ||
| 103 | $done = 1; | ||
| 104 | #TODO sort output by RNAz SVM prob and re-run with the first few as input alignments | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | ############### | ||
| 109 | # subroutines # | ||
| 110 | ############### | ||
| 111 | |||
| 112 | sub alisplit { | ||
| 113 | my ($alnfile,$odirn) = @_; | ||
| 114 | my ($what,$alifold,$rscape); | ||
| 115 | my $AlignSplitObject = Bio::RNA::RNAaliSplit->new(ifile => $alnfile, | ||
| 116 | format => $format, | ||
| 117 | odir => $odirn, | ||
| 118 | dump => 1); | ||
| 119 | #print Dumper($AlignSplitObject); | ||
| 120 | my $dim = $AlignSplitObject->next_aln->num_sequences; | ||
| 121 | my $stkfile = $AlignSplitObject->alignment_stk; | ||
| 122 | my $dmfile = make_distance_matrix($AlignSplitObject,$method,$odirn); | ||
| 123 | |||
| 124 | # compute Neighbor Joining tree and do split decomposition | ||
| 125 | print STDERR "Perform Split Decomposition ...\n"; | ||
| 126 | my $sd = Bio::RNA::RNAaliSplit::WrapAnalyseDists->new(ifile => $dmfile, | ||
| 127 | odir => $AlignSplitObject->odir); | ||
| 128 | print STDERR "Identified ".$sd->count." splits\n"; | ||
| 129 | print join "\t", ("#hint","RNAz prob","z-score","SCI","seqs","statistic","SSCBP","consensus structure","alignment"), "\n"; | ||
| 130 | |||
| 131 | # run RNAalifold for the input alignment | ||
| 132 | $alifold = Bio::RNA::RNAaliSplit::WrapRNAalifold->new(ifile => $alnfile, | ||
| 133 | odir => $AlignSplitObject->odir, | ||
| 134 | ribosum => $ribosum); | ||
| 135 | |||
| 136 | # run RNAz for the input alignment | ||
| 137 | $rnaz = Bio::RNA::RNAaliSplit::WrapRNAz->new(ifile => $alnfile, | ||
| 138 | odir => $AlignSplitObject->odir); | ||
| 139 | |||
| 140 | # run R-scape for the input alignment | ||
| 141 | $rscape = Bio::RNA::RNAaliSplit::WrapRscape->new(ifile => $alifold->alignment_stk, # use RNAalifold-generated stk | ||
| 142 | odir => $AlignSplitObject->odir, | ||
| 143 | statistic => $rscape_stat, | ||
| 144 | nofigures => 1); | ||
| 145 | $rscape->status == 0 ? $what = $rscape->TP : $what = "n/a"; | ||
| 146 | |||
| 147 | print join "\t", "-",$rnaz->P,$rnaz->z,$alifold->sci,$dim,$rscape->statistic,$what, | ||
| 148 | $alifold->consensus_struc,$alnfile."\n"; | ||
| 149 | |||
| 150 | # extract split sets and run RNAz/RNAalifold/R-scape on each of them | ||
| 151 | my $splitnr=1; | ||
| 152 | while (my $sets = $sd->pop()){ | ||
| 153 | my ($sa1_c,$sa1_s,$sa2_c,$sa2_s); # subalignments in Clustal and Stockholm | ||
| 154 | my $set1 = $$sets{S1}; | ||
| 155 | my $set2 = $$sets{S2}; | ||
| 156 | my $token = "split".$splitnr; | ||
| 157 | #print "set1: @$set1\n"; #print "set2: @$set2\n"; | ||
| 158 | ($sa1_c,$sa1_s) = $AlignSplitObject->dump_subalignment("splits", $token.".set1", $set1); | ||
| 159 | ($sa2_c,$sa2_s) = $AlignSplitObject->dump_subalignment("splits", $token.".set2", $set2); | ||
| 160 | if( scalar(@$set1) > 1){evaluate_alignment($sa1_c,$sa1_s,$AlignSplitObject->odir,scalar(@$set1))} | ||
| 161 | if( scalar(@$set2) > 1){evaluate_alignment($sa2_c,$sa2_s,$AlignSplitObject->odir,scalar(@$set2))} | ||
| 162 | $splitnr++; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | sub evaluate_alignment { | ||
| 167 | my ($aln, $stk, $odir, $count) = @_; | ||
| 168 | my ($hint,$rnazo,$alifoldo,$rscapeo,$cs,$what); | ||
| 169 | |||
| 170 | $rnazo = Bio::RNA::RNAaliSplit::WrapRNAz->new(ifile => $aln, | ||
| 171 | odir => $odir); | ||
| 172 | ($rnazo->P > $rnaz->P) ? ($hint = "?") : ($hint = "-"); | ||
| 173 | if($rnazo->P > 1.2*$rnaz->P){$hint = "x"}; | ||
| 174 | if($rnazo->P > 1.3*$rnaz->P){$hint = "X"}; | ||
| 175 | $alifoldo = Bio::RNA::RNAaliSplit::WrapRNAalifold->new(ifile => $aln, | ||
| 176 | odir => $odir, | ||
| 177 | ribosum => $ribosum); | ||
| 178 | $cs = $alifoldo->consensus_struc; | ||
| 179 | $rscapeo = Bio::RNA::RNAaliSplit::WrapRscape->new(ifile => $alifoldo->alignment_stk, # use RNAalifold-generated stk | ||
| 180 | odir => $odir, | ||
| 181 | statistic => $rscape_stat, | ||
| 182 | nofigures => 1); | ||
| 183 | if ($rscapeo->status == 0){ # all OK | ||
| 184 | $what = $rscapeo->TP; | ||
| 185 | } | ||
| 186 | elsif ($rscapeo->status == 1){ # no significant basepairs | ||
| 187 | $what = "0:no_sign"; | ||
| 188 | } | ||
| 189 | elsif ($rscapeo->status == 2){ # covariation scores are almost constant, no further analysis | ||
| 190 | $what = "0:no_data"; | ||
| 191 | } | ||
| 192 | else { $what = "n/a" } | ||
| 193 | |||
| 194 | print join "\t",($hint,$rnazo->P,$rnazo->z,$alifoldo->sci,$count,$rscapeo->statistic,$what,$cs,$aln), "\n"; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub make_distance_matrix { | ||
| 198 | my ($ASO,$m,$od) = @_; | ||
| 199 | my $this_function = (caller(0))[3]; | ||
| 200 | my ($i,$j,$Dfile,$dHn,$dHx,$dBp,$dHB); | ||
| 201 | my @pw_alns = (); | ||
| 202 | my @D = (); | ||
| 203 | my $check = 1; | ||
| 204 | my $dim = $ASO->next_aln->num_sequences; | ||
| 205 | |||
| 206 | # extract all pairwise alignments | ||
| 207 | print STDERR "Extracting pairwise alignments ...\n"; | ||
| 208 | for ($i=1;$i<$dim;$i++){ | ||
| 209 | for($j=$i+1;$j<=$dim;$j++){ | ||
| 210 | my $token = join "_", "pw",$i,$j; | ||
| 211 | my ($sa_clustal,$sa_stockholm) = $ASO->dump_subalignment("pairwise", $token, [$i,$j]); | ||
| 212 | push @pw_alns, $sa_clustal->stringify; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | # initialize distance matrix | ||
| 217 | for($i=0;$i<$dim;$i++){ | ||
| 218 | for ($j=0;$j<$dim;$j++){ | ||
| 219 | $D[$dim*$i+$j] = 0.; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | # build distance matrix based on pairwise alignments | ||
| 224 | print STDERR "Constructing distance matrix based on pairwise alignments ...\n"; | ||
| 225 | foreach my $ali (@pw_alns){ | ||
| 226 | my $pw_aso = Bio::RNA::RNAaliSplit->new(ifile => $ali, | ||
| 227 | format => "ClustalW", | ||
| 228 | odir => $od); | ||
| 229 | my ($i,$j) = sort split /_/, $pw_aso->ifilebn; | ||
| 230 | |||
| 231 | $dHn = $pw_aso->hammingdistN; | ||
| 232 | $dHx = $pw_aso->hammingdistX; | ||
| 233 | my $id1 = $pw_aso->next_aln->get_seq_by_pos(1)->display_id; | ||
| 234 | my $id2 = $pw_aso->next_aln->get_seq_by_pos(2)->display_id; | ||
| 235 | # my $seq1 = $so1->seq; | ||
| 236 | # my $seq2 = $so2->seq; | ||
| 237 | # my ($ss1,$mfe1) = RNA::fold($seq1); | ||
| 238 | # my ($ss2,$mfe2) = RNA::fold($seq2); | ||
| 239 | #print ">> \$seq1: $seq1\n $ss1\n>> \$seq2: $seq2\n $ss2\n\n"; | ||
| 240 | # print Dumper($so1); | ||
| 241 | |||
| 242 | if ($m eq "SCI"){ | ||
| 243 | # distance = -log( [normalized] pairwise SCI) | ||
| 244 | my ($sci,$dsci); | ||
| 245 | if($pw_aso->sci > 1){$sci = 1} | ||
| 246 | elsif ($pw_aso->sci == 0.){$sci = 0.000001} | ||
| 247 | else { $sci = $pw_aso->sci; } | ||
| 248 | $dsci = -1*log($sci)/log(10); | ||
| 249 | $D[$dim*($i-1)+($j-1)] = $D[$dim*($j-1)+($i-1)] = $dsci; | ||
| 250 | # print "$i -> $j : $sci\t$dsci\n"; | ||
| 251 | } | ||
| 252 | elsif ($m eq "dHn") { # hamming dist with gaps replaced by Ns | ||
| 253 | $D[$dim*($i-1)+($j-1)] = $D[$dim*($j-1)+($i-1)] = $dHn; | ||
| 254 | } | ||
| 255 | elsif ($m eq "dHx") { # hamming dist with all gap columns removed | ||
| 256 | $D[$dim*($i-1)+($j-1)] = $D[$dim*($j-1)+($i-1)] = $dHx; | ||
| 257 | } | ||
| 258 | elsif ($m eq "dBp") { # base pair distance | ||
| 259 | carp "ERROR [$this_function] sequence \'display_id\' not found in input alignment" | ||
| 260 | unless (exists $$fi{$id1} && exists $$fi{$id2}); | ||
| 261 | my $gss1 = $$fi{$id1}->{gss}; | ||
| 262 | my $gss2 = $$fi{$id2}->{gss}; | ||
| 263 | my $dBp = RNA::bp_distance($gss1,$gss2); | ||
| 264 | $D[$dim*($i-1)+($j-1)] = $D[$dim*($j-1)+($i-1)] = $dBp; | ||
| 265 | } | ||
| 266 | elsif ($m eq "dBc") { # constrained base pair distance | ||
| 267 | carp "ERROR [$this_function] sequence \'display_id\' not found in input alignment" | ||
| 268 | unless (exists $$fi{$id1} && exists $$fi{$id2}); | ||
| 269 | my $gss1 = $$fi{$id1}->{gss}; | ||
| 270 | my $gss2 = $$fi{$id2}->{gss}; | ||
| 271 | my $dBp = RNA::bp_distance($gss1,$gss2); | ||
| 272 | $D[$dim*($i-1)+($j-1)] = $D[$dim*($j-1)+($i-1)] = $dBp; | ||
| 273 | } | ||
| 274 | elsif($m eq "dHB") { # combined hamming + basepair distance | ||
| 275 | carp "ERROR [$this_function] sequence \'display_id\' not found in input alignment" | ||
| 276 | unless (exists $$fi{$id1} && exists $$fi{$id2}); | ||
| 277 | my $gss1 = $$fi{$id1}->{gss}; | ||
| 278 | my $gss2 = $$fi{$id2}->{gss}; | ||
| 279 | my $dBp = RNA::bp_distance($gss1,$gss2); | ||
| 280 | my $dHB = $scaleH*$dHn + $scaleB*$dBp; | ||
| 281 | $D[$dim*($i-1)+($j-1)] = $D[$dim*($j-1)+($i-1)] = $dHB; | ||
| 282 | } | ||
| 283 | else {croak "Method $method not available ..please use any of SCI|dHn|dHx|dBp|dBc|dHB"} | ||
| 284 | } | ||
| 285 | |||
| 286 | # write matrix to file | ||
| 287 | print STDERR "Writing distance matrix to file ...\n"; | ||
| 288 | if ($m eq "SCI"){ $Dfile = dump_matrix(\@D,$dim,1,1,"S",$ASO,$verbose)} | ||
| 289 | elsif ($m eq "dHn"){$Dfile = dump_matrix(\@D,$dim,1,1,"dHn",$ASO,$verbose)} | ||
| 290 | elsif ($m eq "dHx"){$Dfile = dump_matrix(\@D,$dim,1,1,"dHx",$ASO,$verbose)} | ||
| 291 | elsif ($m eq "dBp"){$Dfile = dump_matrix(\@D,$dim,1,1,"dBp",$ASO,$verbose)} | ||
| 292 | elsif ($m eq "dBc"){$Dfile = dump_matrix(\@D,$dim,1,1,"dBc",$ASO,$verbose)} | ||
| 293 | elsif ($m eq "dHB"){$Dfile = dump_matrix(\@D,$dim,1,1,"dHB",$ASO,$verbose)} | ||
| 294 | else { croak "Method $m not available ..please use SCI|dHn|dHx|dBp|dHB"} | ||
| 295 | |||
| 296 | # check triangle inequality | ||
| 297 | if ($check == 1){ | ||
| 298 | print STDERR "Checking triangle inequality ...\n"; | ||
| 299 | my $result = check_triangle($dim,\@D); | ||
| 300 | } | ||
| 301 | return $Dfile; | ||
| 302 | } | ||
| 303 | |||
| 304 | |||
| 305 | sub dump_matrix { | ||
| 306 | my ($M,$d,$ad,$pd,$what,$aso,$verbos) = @_; | ||
| 307 | my ($i,$j,$info); | ||
| 308 | my $ad_mx = file($aso->odir,"ld.mx"); # AnalyseDists lower diagoinal distance matrix | ||
| 309 | my $pd_mx = file($aso->odir,"phylip.dst"); # Phylip distance matrix | ||
| 310 | if(defined($verbos)){ | ||
| 311 | print STDERR "Analysedists matrix \$ad_mx is $ad_mx\n"; | ||
| 312 | print STDERR "Phylip matrix \$pd_mx is $pd_mx\n"; | ||
| 313 | } | ||
| 314 | if ($what eq "S"){$info="> S (SCI distance)"} | ||
| 315 | elsif($what eq "dHn"){$info="> H (Hamming distance with gap Ns)"} | ||
| 316 | elsif($what eq "dHx"){$info="> H (Hamming distance with gaps removed)"} | ||
| 317 | elsif($what eq "dBp"){$info="> B (Base pair distance)"} | ||
| 318 | elsif($what eq "dBc"){$info="> B (Base pair distance with constraints)"} | ||
| 319 | else{$info="> H (unknown)"} | ||
| 320 | |||
| 321 | if (defined $ad){ # print lower triangle matrix input for AanalyseDists | ||
| 322 | open my $matrix, ">", $ad_mx or die $!; | ||
| 323 | print $matrix $info,"\n"; | ||
| 324 | print $matrix "> X $d\n"; | ||
| 325 | for ($i=1;$i<$d;$i++){ | ||
| 326 | for($j=0;$j<$i;$j++){ | ||
| 327 | printf $matrix "%6.4f ", @$M[$d*$i+$j]; | ||
| 328 | } | ||
| 329 | print $matrix "\n"; | ||
| 330 | } | ||
| 331 | close $matrix; | ||
| 332 | } | ||
| 333 | else{ # print entire matrix | ||
| 334 | for ($i=0;$i<$d;$i++){ | ||
| 335 | for($j=0;$j<$d;$j++){ | ||
| 336 | printf "%6.4f ", @$M[$d*$i+$j]; | ||
| 337 | } | ||
| 338 | print "\n"; | ||
| 339 | } | ||
| 340 | } | ||
| 341 | if (defined $pd){ # print phylip distance matrix | ||
| 342 | open my $matrix, ">", $pd_mx or die $!; | ||
| 343 | print $matrix "$d\n"; | ||
| 344 | for ($i=0;$i<$d;$i++){ | ||
| 345 | my $val = ${$aso->next_aln}{_order}->{$i}; | ||
| 346 | $val=~s/\//_/g; | ||
| 347 | my $id = join '_', eval($i+1),$val; | ||
| 348 | print $matrix $id." "; | ||
| 349 | for($j=0;$j<$d;$j++){ | ||
| 350 | printf $matrix "%6.4f ", @$M[$d*$i+$j]; | ||
| 351 | } | ||
| 352 | print $matrix "\n"; | ||
| 353 | } | ||
| 354 | close $matrix; | ||
| 355 | } | ||
| 356 | return $ad_mx; | ||
| 357 | } | ||
| 358 | |||
| 359 | sub check_triangle { | ||
| 360 | my ($d,$dref) = @_; | ||
| 361 | my $count = 0; | ||
| 362 | my @M = @$dref; | ||
| 363 | my ($i,$j,$k,$d_ij,$d_jk,$d_ik); | ||
| 364 | for ($i=0;$i<$d;$i++){ | ||
| 365 | for($j=0;$j<$d;$j++){ | ||
| 366 | $d_ij = $M[$d*$i+$j]; | ||
| 367 | for($k=0;$k<$d;$k++){ | ||
| 368 | $d_jk = $M[$d*$j+$k]; | ||
| 369 | $d_ik = $M[$d*$i+$k]; | ||
| 370 | $count++; | ||
| 371 | croak "ERROR triangle inequation not satisfied i:$i j:$j k:$k" | ||
| 372 | unless ($d_ij + $d_jk >= $d_ik); | ||
| 373 | } | ||
| 374 | } | ||
| 375 | } | ||
| 376 | print STDERR "Checked $count triangles ...\n"; | ||
| 377 | } | ||
| 378 | |||
| 379 | sub fold_input_alignment { | ||
| 380 | my $aln = shift; | ||
| 381 | my %foldin = (); | ||
| 382 | |||
| 383 | my $input_AlignIO = Bio::AlignIO->new(-file => $alifile, | ||
| 384 | -format => 'ClustalW' | ||
| 385 | ); | ||
| 386 | my $input_aln = $input_AlignIO->next_aln; | ||
| 387 | foreach my $element ($input_aln->each_seq) { | ||
| 388 | my @gappos = (); | ||
| 389 | my $gseq = $element->seq; | ||
| 390 | for (my $i=0;$i<length($gseq);$i++){ | ||
| 391 | push @gappos, $i if (substr($gseq,$i,1) eq "-"); # store gap positions | ||
| 392 | } | ||
| 393 | my $seq = $gseq; | ||
| 394 | $seq =~ s/-//g; # get gap-free sequence | ||
| 395 | my ($ss,$mfe) = RNA::fold($seq); | ||
| 396 | my $gss = $ss; | ||
| 397 | for (my $i=0;$i<=$#gappos;$i++){ | ||
| 398 | my $gaps_inserted = 0; | ||
| 399 | substr($gss,$gappos[$i]+$gaps_inserted,0) = "."; | ||
| 400 | $gaps_inserted++; | ||
| 401 | } | ||
| 402 | print $element->display_id."\n$gseq\n$gss\n$seq\n$ss\n"; | ||
| 403 | print join ",", @gappos,"\n---\n"; | ||
| 404 | unless (exists($foldin{$element->display_id})){ | ||
| 405 | $foldin{$element->display_id} = { | ||
| 406 | gseq => $gseq, | ||
| 407 | seq => $seq, | ||
| 408 | ss => $ss, | ||
| 409 | gss =>$gss, | ||
| 410 | mfe => $mfe, | ||
| 411 | } | ||
| 412 | } | ||
| 413 | else{ | ||
| 414 | croak "ERROR: duplicate sequence identifier in input: ".$element->display_id; | ||
| 415 | } | ||
| 416 | } | ||
| 417 | return \%foldin; | ||
| 418 | } | ||
| 419 | |||
| 420 | sub fold_input_alignment_constrained { | ||
| 421 | my ($aln,$constr) = @_; | ||
| 422 | my %foldin = (); | ||
| 423 | my @pt = make_pair_table($constr); | ||
| 424 | my $input_AlignIO = Bio::AlignIO->new(-file => $alifile, | ||
| 425 | -format => 'ClustalW' | ||
| 426 | ); | ||
| 427 | my $input_aln = $input_AlignIO->next_aln; | ||
| 428 | foreach my $element ($input_aln->each_seq) { | ||
| 429 | my @gappos = (); | ||
| 430 | my $cons=$constr; | ||
| 431 | my $gseq = $element->seq; | ||
| 432 | my $id = $element->display_id; | ||
| 433 | if (length($constraint) != length($gseq)){ | ||
| 434 | print STDERR "ERROR: constraint length does not match alignment length ...\n$constraint \n $gseq"; | ||
| 435 | croak(); | ||
| 436 | } | ||
| 437 | print STDERR "\n>$id\n"; | ||
| 438 | print STDERR "$gseq\n"; | ||
| 439 | for (my $p=0; $p<length($gseq);$p++) { | ||
| 440 | # remove non-compatible pairs as well as pairs to a gap position | ||
| 441 | my $c = substr($gseq,$p,1); | ||
| 442 | if ($c eq '-') { | ||
| 443 | push @gappos, $p; | ||
| 444 | substr($cons,$p,1) = 'x'; # mark for removal | ||
| 445 | substr($cons,$pt[$p],1) = '.' | ||
| 446 | if $pt[$p]>0 && substr($cons,$pt[$p],1) ne 'x'; # open pair | ||
| 447 | } | ||
| 448 | elsif ($pt[$p]>$p) { | ||
| 449 | substr($cons,$p,1) = substr($cons,$pt[$p],1) = '.' | ||
| 450 | unless exists $pair{$c . substr($gseq,$pt[$p],1)}; | ||
| 451 | } | ||
| 452 | } # end for | ||
| 453 | |||
| 454 | print STDERR "$cons\n$gseq\n"; | ||
| 455 | # print STDERR length($seq), length($cons), "\n"; | ||
| 456 | $cons =~ s/x//g; | ||
| 457 | my $seq = $gseq; | ||
| 458 | $seq =~ s/-//g; | ||
| 459 | print STDERR "$cons\n$seq\n"; | ||
| 460 | |||
| 461 | # do constraint folding | ||
| 462 | $RNA::fold_constrained = 1; | ||
| 463 | my ($css,$cmfe) = RNA::fold($seq, $cons); | ||
| 464 | print STDERR "$css\t$cmfe\n"; | ||
| 465 | $RNA::fold_constrained = 0; | ||
| 466 | |||
| 467 | # re-insert gaps into constraint-folded strcuture | ||
| 468 | my $gcss = $css; | ||
| 469 | for (my $i=0;$i<=$#gappos;$i++){ | ||
| 470 | my $gaps_inserted = 0; | ||
| 471 | substr($gcss,$gappos[$i]+$gaps_inserted,0) = "."; | ||
| 472 | $gaps_inserted++; | ||
| 473 | } | ||
| 474 | print "$gcss\n"; | ||
| 475 | print STDERR join ",", @gappos,"\n"; | ||
| 476 | |||
| 477 | unless (exists($foldin{$element->display_id})){ | ||
| 478 | $foldin{$element->display_id} = { | ||
| 479 | gseq => $gseq, | ||
| 480 | seq => $seq, | ||
| 481 | ss => $css, | ||
| 482 | gss =>$gcss, | ||
| 483 | mfe => $cmfe, | ||
| 484 | } | ||
| 485 | } | ||
| 486 | else{ | ||
| 487 | croak "ERROR: duplicate sequence identifier in input: ".$element->display_id; | ||
| 488 | } | ||
| 489 | print STDERR Dumper( $foldin{$element->display_id} ); | ||
| 490 | print STDERR "---\n"; | ||
| 491 | } | ||
| 492 | return \%foldin; | ||
| 493 | } | ||
| 494 | |||
| 495 | sub make_pair_table { | ||
| 496 | #indices start at 0 in this version! | ||
| 497 | my $structure = shift; | ||
| 498 | print ">>$structure<<\n"; | ||
| 499 | my (@olist, @table); | ||
| 500 | my ($hx,$i) = (0,0); | ||
| 501 | |||
| 502 | foreach my $c (split(//,$structure)) { | ||
| 503 | if ($c eq '.') { | ||
| 504 | $table[$i]= -1; | ||
| 505 | } elsif ($c eq '(') { | ||
| 506 | $olist[$hx++]=$i; | ||
| 507 | } elsif ($c eq ')') { | ||
| 508 | my $j = $olist[--$hx]; | ||
| 509 | die ("unbalanced brackets in make_pair_table") if ($hx<0); | ||
| 510 | $table[$i]=$j; | ||
| 511 | $table[$j]=$i; | ||
| 512 | } | ||
| 513 | $i++; | ||
| 514 | } | ||
| 515 | carp ("too few closed brackets in make_pair_table") if ($hx!=0); | ||
| 516 | return @table; | ||
| 517 | } | ||
| 518 | |||
| 519 | |||
| 520 | __END__ | ||
| 521 | |||
| 522 | =head1 NAME | ||
| 523 | |||
| 524 | RNAalisplit - Split and decompose RNA multiple sequence alignments | ||
| 525 | |||
| 526 | =head1 SYNOPSIS | ||
| 527 | |||
| 528 | RNAalisplit.pl [--aln|-a I<FILE>] [--method|-m I<OPTION>] [options] | ||
| 529 | |||
| 530 | =head1 DESCRIPTION | ||
| 531 | |||
| 532 | This tool splits multiple sequence alignments horizontally, thereby | ||
| 533 | extracting sets of sequences that group together according to a | ||
| 534 | decision value. The most natural decision value is the RNAz SVM | ||
| 535 | RNA-classs probability. | ||
| 536 | |||
| 537 | A neighbour joining tree is reconstructed from pairwise distances of | ||
| 538 | sequences in the input alignment and subsets of the alignment are | ||
| 539 | derived by splitting at each edge of the NJ tree as well as performing | ||
| 540 | a split decomposition of the matrix of pairwise distances. These | ||
| 541 | subsets/subalignments are then evaluated according to the same | ||
| 542 | decision value and a decision is made whether a subalignment performs | ||
| 543 | better than the original alignment.This can be used to discriminate | ||
| 544 | sequences that to not 'fit' in the input alignment. | ||
| 545 | |||
| 546 | Output is written to STDOUT and a directory containing all temporary | ||
| 547 | RNAalifold / RNAz / R-scape output files is created. Inside this | ||
| 548 | directory, the 'phylip.dst' file contains the distance matrix computed | ||
| 549 | from pairwise distances. It can be visualized e.g. with SplitsTree. | ||
| 550 | |||
| 551 | =head1 OPTIONS | ||
| 552 | |||
| 553 | =over | ||
| 554 | |||
| 555 | =item B<--aln|-a> | ||
| 556 | |||
| 557 | A multiple sequence alignment in ClustalW format | ||
| 558 | |||
| 559 | =item B<--constraint|-c> | ||
| 560 | |||
| 561 | Constraint structure, overriding the consensus structure of the | ||
| 562 | underlying alignment in case B<--method|-m dBc> is selected. | ||
| 563 | |||
| 564 | =item B<--method|-m> | ||
| 565 | |||
| 566 | Method to compute pairwise ditances. Available options are 'dHn', | ||
| 567 | 'dHx', 'dBp', 'dBc, 'dHB', and 'SCI'. The first and second compute | ||
| 568 | pairwise Hamming distances of sequences, where 'dHn' replaces gaps | ||
| 569 | with 'N', whereas 'dHx' removes all gap columns (not yet | ||
| 570 | implemented). 'dBp' folds RNA sequences into their MFE structures and | ||
| 571 | computes pairwise base pair distances. 'dBc' computes base pair | ||
| 572 | distances on constraint-folded RNA sequences. Here, the default is to | ||
| 573 | use the consensus structure of the underlying alignment as a | ||
| 574 | constraint, however, an alternative constraint structure can be | ||
| 575 | provided via the B<--constraint> option. 'SCI' computes the distance | ||
| 576 | as 1-log(SCI), based on a truncated strucure conservation index of two | ||
| 577 | sequences. The latter, however, is not a metric and therefore often | ||
| 578 | results in negative branch lengths in Neighbor Joining trees. Use with | ||
| 579 | caution. [default: 'dHn'] | ||
| 580 | |||
| 581 | =item B<--noribosum> | ||
| 582 | |||
| 583 | Turn off ribosum scoring for RNAalifold computation. Default: ribosum | ||
| 584 | scoring on | ||
| 585 | |||
| 586 | =item B<--rscapestat> | ||
| 587 | |||
| 588 | R-scape covariation statistic. Allowed values are: 'GT', 'MI', 'MIr', | ||
| 589 | 'MIg', 'CHI', 'OMES', 'RAF', 'RAFS'. Appending either 'p' or 'a' to | ||
| 590 | any of them calculates its average product correction and average sum | ||
| 591 | correction, respctively (e.g. GTp or GTa). See the R-scape manual for | ||
| 592 | details. | ||
| 593 | |||
| 594 | =item B<--out|-o> | ||
| 595 | |||
| 596 | Output base directory. Temporary data and results will be written to | ||
| 597 | this directory | ||
| 598 | |||
| 599 | =item B<--version> | ||
| 600 | |||
| 601 | Show RNAalisplit version and exit | ||
| 602 | |||
| 603 | =back | ||
| 604 | |||
| 605 | =head1 AUTHOR | ||
| 606 | |||
| 607 | Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt> and | ||
| 608 | E<lt>michael.wolfinger@univie.ac.atE<gt> | ||
| 609 | |||
| 610 | =cut | ||
| 611 | (END) |