| 1 | #!/usr/bin/env perl | ||
| 2 | # Last changed Time-stamp: <2019-04-24 01:07:23 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.11'); | ||
| 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 $method = "dHn"; # SCI | dHn | dHx | dBp | dBc | dHB | ||
| 32 | my $rscape_stat = "GTp"; | ||
| 33 | my $outdir = "as"; | ||
| 34 | my $verbose = undef; | ||
| 35 | my $rnaz=undef; | ||
| 36 | my @nseqs=(); | ||
| 37 | my ($alifile,$fi); | ||
| 38 | my $scaleH = 1.; | ||
| 39 | my $scaleB = 1.; | ||
| 40 | my $ribosum = 1; | ||
| 41 | my $constraint=undef; | ||
| 42 | my $show_version = 0; | ||
| 43 | my $do_rscape = 1; | ||
| 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 | Getopt::Long::config('no_ignore_case'); | ||
| 58 | pod2usage(-verbose => 1) unless GetOptions("aln|a=s" => \$alifile, | ||
| 59 | "constraint|c=s" => \$constraint, | ||
| 60 | "method|m=s" => \$method, | ||
| 61 | "noribosum" => sub{$ribosum=0}, | ||
| 62 | "norscape" => sub{$do_rscape=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 $format = Bio::AlignIO->_guess_format($alnfile); | ||
| 116 | print STDERR "Guess input format $format\n"; | ||
| 117 | my $AlignSplitObject = Bio::RNA::RNAaliSplit->new(ifile => $alnfile, | ||
| 118 | format => $format, | ||
| 119 | odir => $odirn); | ||
| 120 | #print Dumper($AlignSplitObject); | ||
| 121 | my $dim = $AlignSplitObject->next_aln->num_sequences; | ||
| 122 | my $stkfile = $AlignSplitObject->alignment_stk; | ||
| 123 | my $dmfile = make_distance_matrix($AlignSplitObject,$method,$odirn); | ||
| 124 | |||
| 125 | # compute Neighbor Joining tree and do split decomposition | ||
| 126 | print STDERR "Perform Split Decomposition ...\n"; | ||
| 127 | my $sd = Bio::RNA::RNAaliSplit::WrapAnalyseDists->new(ifile => $dmfile, | ||
| 128 | odir => $AlignSplitObject->odir); | ||
| 129 | print STDERR "Identified ".$sd->count." splits\n"; | ||
| 130 | if ($do_rscape){ | ||
| 131 | print join "\t", ("#hint","RNAz prob","z-score","SCI","seqs","statistic","SSCBP","consensus structure","alignment"), "\n"; | ||
| 132 | } | ||
| 133 | else { | ||
| 134 | print join "\t", ("#hint","RNAz prob","z-score","SCI","seqs","consensus structure","alignment"), "\n"; | ||
| 135 | } | ||
| 136 | |||
| 137 | # run RNAalifold for the input alignment | ||
| 138 | $alifold = Bio::RNA::RNAaliSplit::WrapRNAalifold->new(ifile => $AlignSplitObject->alignment_aln, | ||
| 139 | odir => $AlignSplitObject->odir, | ||
| 140 | format => 'C', | ||
| 141 | ribosum => $ribosum); | ||
| 142 | |||
| 143 | # run RNAz for the input alignment | ||
| 144 | $rnaz = Bio::RNA::RNAaliSplit::WrapRNAz->new(ifile => $AlignSplitObject->alignment_aln, | ||
| 145 | odir => $AlignSplitObject->odir); | ||
| 146 | |||
| 147 | # run R-scape for the input alignment | ||
| 148 | if ($do_rscape){ | ||
| 149 | $rscape = Bio::RNA::RNAaliSplit::WrapRscape->new(ifile => $alifold->alignment_stk, # use RNAalifold-generated stk | ||
| 150 | odir => $AlignSplitObject->odir, | ||
| 151 | statistic => $rscape_stat, | ||
| 152 | nofigures => 1); | ||
| 153 | $rscape->status == 0 ? $what = $rscape->TP : $what = "n/a"; | ||
| 154 | |||
| 155 | print join "\t", "-",$rnaz->P,$rnaz->z,$alifold->sci,$dim,$rscape->statistic,$what, | ||
| 156 | $alifold->consensus_struc,$alnfile."\n"; | ||
| 157 | } | ||
| 158 | else { | ||
| 159 | print join "\t", "-",$rnaz->P,$rnaz->z,$alifold->sci,$dim, | ||
| 160 | $alifold->consensus_struc,$alnfile."\n"; | ||
| 161 | } | ||
| 162 | |||
| 163 | # extract split sets and run RNAz/RNAalifold/R-scape on each of them | ||
| 164 | my $splitnr=1; | ||
| 165 | while (my $sets = $sd->pop()){ | ||
| 166 | my ($sa1_c,$sa1_s,$sa2_c,$sa2_s); # subalignments in Clustal and Stockholm | ||
| 167 | my $set1 = $$sets{S1}; | ||
| 168 | my $set2 = $$sets{S2}; | ||
| 169 | my $token = "split".$splitnr; | ||
| 170 | #print "set1: @$set1\n"; #print "set2: @$set2\n"; | ||
| 171 | ($sa1_c,$sa1_s) = $AlignSplitObject->dump_subalignment("splits", $token.".set1", $set1); | ||
| 172 | ($sa2_c,$sa2_s) = $AlignSplitObject->dump_subalignment("splits", $token.".set2", $set2); | ||
| 173 | if( scalar(@$set1) > 1){evaluate_alignment($sa1_c,$sa1_s,$AlignSplitObject->odir,scalar(@$set1))} | ||
| 174 | if( scalar(@$set2) > 1){evaluate_alignment($sa2_c,$sa2_s,$AlignSplitObject->odir,scalar(@$set2))} | ||
| 175 | $splitnr++; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | sub evaluate_alignment { | ||
| 180 | my ($aln, $stk, $odir, $count) = @_; | ||
| 181 | my ($hint,$rnazo,$alifoldo,$rscapeo,$cs,$what); | ||
| 182 | |||
| 183 | $rnazo = Bio::RNA::RNAaliSplit::WrapRNAz->new(ifile => $aln, | ||
| 184 | odir => $odir); | ||
| 185 | ($rnazo->P > $rnaz->P) ? ($hint = "?") : ($hint = "-"); | ||
| 186 | if($rnazo->P > 1.2*$rnaz->P){$hint = "x"}; | ||
| 187 | if($rnazo->P > 1.3*$rnaz->P){$hint = "X"}; | ||
| 188 | $alifoldo = Bio::RNA::RNAaliSplit::WrapRNAalifold->new(ifile => $aln, | ||
| 189 | odir => $odir, | ||
| 190 | ribosum => $ribosum); | ||
| 191 | $cs = $alifoldo->consensus_struc; | ||
| 192 | if ($do_rscape) { | ||
| 193 | $rscapeo = Bio::RNA::RNAaliSplit::WrapRscape->new(ifile => $alifoldo->alignment_stk, # use RNAalifold-generated stk | ||
| 194 | odir => $odir, | ||
| 195 | statistic => $rscape_stat, | ||
| 196 | nofigures => 1); | ||
| 197 | if ($rscapeo->status == 0){ # all OK | ||
| 198 | $what = $rscapeo->TP; | ||
| 199 | } | ||
| 200 | elsif ($rscapeo->status == 1){ # no significant basepairs | ||
| 201 | $what = "0:no_sign"; | ||
| 202 | } | ||
| 203 | elsif ($rscapeo->status == 2){ # covariation scores are almost constant, no further analysis | ||
| 204 | $what = "0:no_data"; | ||
| 205 | } | ||
| 206 | else { $what = "n/a" } | ||
| 207 | |||
| 208 | print join "\t",($hint,$rnazo->P,$rnazo->z,$alifoldo->sci,$count,$rscapeo->statistic,$what,$cs,$aln), "\n"; | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | print join "\t",($hint,$rnazo->P,$rnazo->z,$alifoldo->sci,$count,$cs,$aln), "\n"; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | sub make_distance_matrix { | ||
| 216 | my ($ASO,$m,$od) = @_; | ||
| 217 | my $this_function = (caller(0))[3]; | ||
| 218 | my ($i,$j,$Dfile,$dHn,$dHx,$dBp,$dHB); | ||
| 219 | my @pw_alns = (); | ||
| 220 | my @D = (); | ||
| 221 | my $check = 1; | ||
| 222 | my $dim = $ASO->next_aln->num_sequences; | ||
| 223 | |||
| 224 | # extract all pairwise alignments | ||
| 225 | print STDERR "Extracting pairwise alignments ...\n"; | ||
| 226 | for ($i=1;$i<$dim;$i++){ | ||
| 227 | for($j=$i+1;$j<=$dim;$j++){ | ||
| 228 | my $token = join "_", "pw",$i,$j; | ||
| 229 | my ($sa_clustal,$sa_stockholm) = $ASO->dump_subalignment("pairwise", $token, [$i,$j]); | ||
| 230 | push @pw_alns, $sa_clustal->stringify; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | # initialize distance matrix | ||
| 235 | for($i=0;$i<$dim;$i++){ | ||
| 236 | for ($j=0;$j<$dim;$j++){ | ||
| 237 | $D[$dim*$i+$j] = 0.; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | # build distance matrix based on pairwise alignments | ||
| 242 | print STDERR "Constructing distance matrix based on pairwise alignments ...\n"; | ||
| 243 | foreach my $ali (@pw_alns){ | ||
| 244 | my $pw_aso = Bio::RNA::RNAaliSplit->new(ifile => $ali, | ||
| 245 | format => "ClustalW", | ||
| 246 | odir => $od); | ||
| 247 | my ($i,$j) = sort split /_/, $pw_aso->ifilebn; | ||
| 248 | |||
| 249 | $dHn = $pw_aso->hammingdistN; | ||
| 250 | $dHx = $pw_aso->hammingdistX; | ||
| 251 | my $id1 = $pw_aso->next_aln->get_seq_by_pos(1)->display_id; | ||
| 252 | my $id2 = $pw_aso->next_aln->get_seq_by_pos(2)->display_id; | ||
| 253 | |||
| 254 | if ($m eq "SCI"){ | ||
| 255 | # distance = -log( [normalized] pairwise SCI) | ||
| 256 | my ($sci,$dsci); | ||
| 257 | if($pw_aso->sci > 1){$sci = 1} | ||
| 258 | elsif ($pw_aso->sci == 0.){$sci = 0.000001} | ||
| 259 | else { $sci = $pw_aso->sci; } | ||
| 260 | $dsci = -1*log($sci)/log(10); | ||
| 261 | $D[$dim*($i-1)+($j-1)] = $D[$dim*($j-1)+($i-1)] = $dsci; | ||
| 262 | # print "$i -> $j : $sci\t$dsci\n"; | ||
| 263 | } | ||
| 264 | elsif ($m eq "dHn") { # hamming dist with gaps replaced by Ns | ||
| 265 | $D[$dim*($i-1)+($j-1)] = $D[$dim*($j-1)+($i-1)] = $dHn; | ||
| 266 | } | ||
| 267 | elsif ($m eq "dHx") { # hamming dist with all gap columns removed | ||
| 268 | $D[$dim*($i-1)+($j-1)] = $D[$dim*($j-1)+($i-1)] = $dHx; | ||
| 269 | } | ||
| 270 | elsif ($m eq "dBp") { # base pair distance | ||
| 271 | carp "ERROR [$this_function] sequence \'display_id\' not found in input alignment" | ||
| 272 | unless (exists $$fi{$id1} && exists $$fi{$id2}); | ||
| 273 | my $gss1 = $$fi{$id1}->{gss}; | ||
| 274 | my $gss2 = $$fi{$id2}->{gss}; | ||
| 275 | my $dBp = RNA::bp_distance($gss1,$gss2); | ||
| 276 | $D[$dim*($i-1)+($j-1)] = $D[$dim*($j-1)+($i-1)] = $dBp; | ||
| 277 | } | ||
| 278 | elsif ($m eq "dBc") { # constrained base pair distance | ||
| 279 | carp "ERROR [$this_function] sequence \'display_id\' not found in input alignment" | ||
| 280 | unless (exists $$fi{$id1} && exists $$fi{$id2}); | ||
| 281 | my $gss1 = $$fi{$id1}->{gss}; | ||
| 282 | my $gss2 = $$fi{$id2}->{gss}; | ||
| 283 | my $dBp = RNA::bp_distance($gss1,$gss2); | ||
| 284 | $D[$dim*($i-1)+($j-1)] = $D[$dim*($j-1)+($i-1)] = $dBp; | ||
| 285 | } | ||
| 286 | elsif($m eq "dHB") { # combined hamming + basepair distance | ||
| 287 | carp "ERROR [$this_function] sequence \'display_id\' not found in input alignment" | ||
| 288 | unless (exists $$fi{$id1} && exists $$fi{$id2}); | ||
| 289 | my $gss1 = $$fi{$id1}->{gss}; | ||
| 290 | my $gss2 = $$fi{$id2}->{gss}; | ||
| 291 | my $dBp = RNA::bp_distance($gss1,$gss2); | ||
| 292 | my $dHB = $scaleH*$dHn + $scaleB*$dBp; | ||
| 293 | $D[$dim*($i-1)+($j-1)] = $D[$dim*($j-1)+($i-1)] = $dHB; | ||
| 294 | } | ||
| 295 | else {croak "Method $method not available ..please use any of SCI|dHn|dHx|dBp|dBc|dHB"} | ||
| 296 | } | ||
| 297 | |||
| 298 | # write matrix to file | ||
| 299 | print STDERR "Writing distance matrix to file ...\n"; | ||
| 300 | if ($m eq "SCI"){ $Dfile = dump_matrix(\@D,$dim,1,1,"S",$ASO,$verbose)} | ||
| 301 | elsif ($m eq "dHn"){$Dfile = dump_matrix(\@D,$dim,1,1,"dHn",$ASO,$verbose)} | ||
| 302 | elsif ($m eq "dHx"){$Dfile = dump_matrix(\@D,$dim,1,1,"dHx",$ASO,$verbose)} | ||
| 303 | elsif ($m eq "dBp"){$Dfile = dump_matrix(\@D,$dim,1,1,"dBp",$ASO,$verbose)} | ||
| 304 | elsif ($m eq "dBc"){$Dfile = dump_matrix(\@D,$dim,1,1,"dBc",$ASO,$verbose)} | ||
| 305 | elsif ($m eq "dHB"){$Dfile = dump_matrix(\@D,$dim,1,1,"dHB",$ASO,$verbose)} | ||
| 306 | else { croak "Method $m not available ..please use SCI|dHn|dHx|dBp|dHB"} | ||
| 307 | |||
| 308 | # check triangle inequality | ||
| 309 | if ($check == 1){ | ||
| 310 | print STDERR "Checking triangle inequality ...\n"; | ||
| 311 | my $result = check_triangle($dim,\@D); | ||
| 312 | } | ||
| 313 | return $Dfile; | ||
| 314 | } | ||
| 315 | |||
| 316 | |||
| 317 | sub dump_matrix { | ||
| 318 | my ($M,$d,$ad,$pd,$what,$aso,$verbos) = @_; | ||
| 319 | my ($i,$j,$info); | ||
| 320 | my $ad_mx = file($aso->odir,"ld.mx"); # AnalyseDists lower diagoinal distance matrix | ||
| 321 | my $pd_mx = file($aso->odir,"phylip.dst"); # Phylip distance matrix | ||
| 322 | if(defined($verbos)){ | ||
| 323 | print STDERR "Analysedists matrix \$ad_mx is $ad_mx\n"; | ||
| 324 | print STDERR "Phylip matrix \$pd_mx is $pd_mx\n"; | ||
| 325 | } | ||
| 326 | if ($what eq "S"){$info="> S (SCI distance)"} | ||
| 327 | elsif($what eq "dHn"){$info="> H (Hamming distance with gap Ns)"} | ||
| 328 | elsif($what eq "dHx"){$info="> H (Hamming distance with gaps removed)"} | ||
| 329 | elsif($what eq "dBp"){$info="> B (Base pair distance)"} | ||
| 330 | elsif($what eq "dBc"){$info="> B (Base pair distance with constraints)"} | ||
| 331 | else{$info="> H (unknown)"} | ||
| 332 | |||
| 333 | if (defined $ad){ # print lower triangle matrix input for AanalyseDists | ||
| 334 | open my $matrix, ">", $ad_mx or die $!; | ||
| 335 | print $matrix $info,"\n"; | ||
| 336 | print $matrix "> X $d\n"; | ||
| 337 | for ($i=1;$i<$d;$i++){ | ||
| 338 | for($j=0;$j<$i;$j++){ | ||
| 339 | printf $matrix "%6.4f ", @$M[$d*$i+$j]; | ||
| 340 | } | ||
| 341 | print $matrix "\n"; | ||
| 342 | } | ||
| 343 | close $matrix; | ||
| 344 | } | ||
| 345 | else{ # print entire matrix | ||
| 346 | for ($i=0;$i<$d;$i++){ | ||
| 347 | for($j=0;$j<$d;$j++){ | ||
| 348 | printf "%6.4f ", @$M[$d*$i+$j]; | ||
| 349 | } | ||
| 350 | print "\n"; | ||
| 351 | } | ||
| 352 | } | ||
| 353 | if (defined $pd){ # print phylip distance matrix | ||
| 354 | open my $matrix, ">", $pd_mx or die $!; | ||
| 355 | print $matrix "$d\n"; | ||
| 356 | for ($i=0;$i<$d;$i++){ | ||
| 357 | my $val = ${$aso->next_aln}{_order}->{$i}; | ||
| 358 | $val=~s/\//_/g; | ||
| 359 | my $id = join '_', eval($i+1),$val; | ||
| 360 | print $matrix $id." "; | ||
| 361 | for($j=0;$j<$d;$j++){ | ||
| 362 | printf $matrix "%6.4f ", @$M[$d*$i+$j]; | ||
| 363 | } | ||
| 364 | print $matrix "\n"; | ||
| 365 | } | ||
| 366 | close $matrix; | ||
| 367 | } | ||
| 368 | return $ad_mx; | ||
| 369 | } | ||
| 370 | |||
| 371 | sub check_triangle { | ||
| 372 | my ($d,$dref) = @_; | ||
| 373 | my $count = 0; | ||
| 374 | my @M = @$dref; | ||
| 375 | my ($i,$j,$k,$d_ij,$d_jk,$d_ik); | ||
| 376 | for ($i=0;$i<$d;$i++){ | ||
| 377 | for($j=0;$j<$d;$j++){ | ||
| 378 | $d_ij = $M[$d*$i+$j]; | ||
| 379 | for($k=0;$k<$d;$k++){ | ||
| 380 | $d_jk = $M[$d*$j+$k]; | ||
| 381 | $d_ik = $M[$d*$i+$k]; | ||
| 382 | $count++; | ||
| 383 | croak "ERROR triangle inequation not satisfied i:$i j:$j k:$k" | ||
| 384 | unless ($d_ij + $d_jk >= $d_ik); | ||
| 385 | } | ||
| 386 | } | ||
| 387 | } | ||
| 388 | print STDERR "Checked $count triangles ...\n"; | ||
| 389 | } | ||
| 390 | |||
| 391 | sub fold_input_alignment { | ||
| 392 | my $aln = shift; | ||
| 393 | my %foldin = (); | ||
| 394 | |||
| 395 | my $input_AlignIO = Bio::AlignIO->new(-file => $alifile, | ||
| 396 | -format => 'ClustalW' | ||
| 397 | ); | ||
| 398 | my $input_aln = $input_AlignIO->next_aln; | ||
| 399 | foreach my $element ($input_aln->each_seq) { | ||
| 400 | my @gappos = (); | ||
| 401 | my $gseq = $element->seq; | ||
| 402 | for (my $i=0;$i<length($gseq);$i++){ | ||
| 403 | push @gappos, $i if (substr($gseq,$i,1) eq "-"); # store gap positions | ||
| 404 | } | ||
| 405 | my $seq = $gseq; | ||
| 406 | $seq =~ s/-//g; # get gap-free sequence | ||
| 407 | my ($ss,$mfe) = RNA::fold($seq); | ||
| 408 | my $gss = $ss; | ||
| 409 | for (my $i=0;$i<=$#gappos;$i++){ | ||
| 410 | my $gaps_inserted = 0; | ||
| 411 | substr($gss,$gappos[$i]+$gaps_inserted,0) = "."; | ||
| 412 | $gaps_inserted++; | ||
| 413 | } | ||
| 414 | print $element->display_id."\n$gseq\n$gss\n$seq\n$ss\n"; | ||
| 415 | print join ",", @gappos,"\n---\n"; | ||
| 416 | unless (exists($foldin{$element->display_id})){ | ||
| 417 | $foldin{$element->display_id} = { | ||
| 418 | gseq => $gseq, | ||
| 419 | seq => $seq, | ||
| 420 | ss => $ss, | ||
| 421 | gss =>$gss, | ||
| 422 | mfe => $mfe, | ||
| 423 | } | ||
| 424 | } | ||
| 425 | else{ | ||
| 426 | croak "ERROR: duplicate sequence identifier in input: ".$element->display_id; | ||
| 427 | } | ||
| 428 | } | ||
| 429 | return \%foldin; | ||
| 430 | } | ||
| 431 | |||
| 432 | sub fold_input_alignment_constrained { | ||
| 433 | my ($aln,$constr) = @_; | ||
| 434 | my %foldin = (); | ||
| 435 | my @pt = make_pair_table($constr); | ||
| 436 | my $input_AlignIO = Bio::AlignIO->new(-file => $alifile, | ||
| 437 | -format => 'ClustalW' | ||
| 438 | ); | ||
| 439 | my $input_aln = $input_AlignIO->next_aln; | ||
| 440 | foreach my $element ($input_aln->each_seq) { | ||
| 441 | my @gappos = (); | ||
| 442 | my $cons=$constr; | ||
| 443 | my $gseq = $element->seq; | ||
| 444 | my $id = $element->display_id; | ||
| 445 | if (length($constraint) != length($gseq)){ | ||
| 446 | print STDERR "ERROR: constraint length does not match alignment length ...\n$constraint \n $gseq"; | ||
| 447 | croak(); | ||
| 448 | } | ||
| 449 | print STDERR "\n>$id\n"; | ||
| 450 | print STDERR "$gseq\n"; | ||
| 451 | for (my $p=0; $p<length($gseq);$p++) { | ||
| 452 | # remove non-compatible pairs as well as pairs to a gap position | ||
| 453 | my $c = substr($gseq,$p,1); | ||
| 454 | if ($c eq '-') { | ||
| 455 | push @gappos, $p; | ||
| 456 | substr($cons,$p,1) = 'x'; # mark for removal | ||
| 457 | substr($cons,$pt[$p],1) = '.' | ||
| 458 | if $pt[$p]>0 && substr($cons,$pt[$p],1) ne 'x'; # open pair | ||
| 459 | } | ||
| 460 | elsif ($pt[$p]>$p) { | ||
| 461 | substr($cons,$p,1) = substr($cons,$pt[$p],1) = '.' | ||
| 462 | unless exists $pair{$c . substr($gseq,$pt[$p],1)}; | ||
| 463 | } | ||
| 464 | } # end for | ||
| 465 | |||
| 466 | print STDERR "$cons\n$gseq\n"; | ||
| 467 | # print STDERR length($seq), length($cons), "\n"; | ||
| 468 | $cons =~ s/x//g; | ||
| 469 | my $seq = $gseq; | ||
| 470 | $seq =~ s/-//g; | ||
| 471 | print STDERR "$cons\n$seq\n"; | ||
| 472 | |||
| 473 | # do constraint folding | ||
| 474 | $RNA::fold_constrained = 1; | ||
| 475 | my ($css,$cmfe) = RNA::fold($seq, $cons); | ||
| 476 | print STDERR "$css\t$cmfe\n"; | ||
| 477 | $RNA::fold_constrained = 0; | ||
| 478 | |||
| 479 | # re-insert gaps into constraint-folded strcuture | ||
| 480 | my $gcss = $css; | ||
| 481 | for (my $i=0;$i<=$#gappos;$i++){ | ||
| 482 | my $gaps_inserted = 0; | ||
| 483 | substr($gcss,$gappos[$i]+$gaps_inserted,0) = "."; | ||
| 484 | $gaps_inserted++; | ||
| 485 | } | ||
| 486 | print "$gcss\n"; | ||
| 487 | print STDERR join ",", @gappos,"\n"; | ||
| 488 | |||
| 489 | unless (exists($foldin{$element->display_id})){ | ||
| 490 | $foldin{$element->display_id} = { | ||
| 491 | gseq => $gseq, | ||
| 492 | seq => $seq, | ||
| 493 | ss => $css, | ||
| 494 | gss =>$gcss, | ||
| 495 | mfe => $cmfe, | ||
| 496 | } | ||
| 497 | } | ||
| 498 | else{ | ||
| 499 | croak "ERROR: duplicate sequence identifier in input: ".$element->display_id; | ||
| 500 | } | ||
| 501 | print STDERR Dumper( $foldin{$element->display_id} ); | ||
| 502 | print STDERR "---\n"; | ||
| 503 | } | ||
| 504 | return \%foldin; | ||
| 505 | } | ||
| 506 | |||
| 507 | sub make_pair_table { | ||
| 508 | #indices start at 0 in this version! | ||
| 509 | my $structure = shift; | ||
| 510 | print ">>$structure<<\n"; | ||
| 511 | my (@olist, @table); | ||
| 512 | my ($hx,$i) = (0,0); | ||
| 513 | |||
| 514 | foreach my $c (split(//,$structure)) { | ||
| 515 | if ($c eq '.') { | ||
| 516 | $table[$i]= -1; | ||
| 517 | } elsif ($c eq '(') { | ||
| 518 | $olist[$hx++]=$i; | ||
| 519 | } elsif ($c eq ')') { | ||
| 520 | my $j = $olist[--$hx]; | ||
| 521 | die ("unbalanced brackets in make_pair_table") if ($hx<0); | ||
| 522 | $table[$i]=$j; | ||
| 523 | $table[$j]=$i; | ||
| 524 | } | ||
| 525 | $i++; | ||
| 526 | } | ||
| 527 | carp ("too few closed brackets in make_pair_table") if ($hx!=0); | ||
| 528 | return @table; | ||
| 529 | } | ||
| 530 | |||
| 531 | |||
| 532 | __END__ |