1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
|
#!/usr/bin/perl
################################################################################
#
# Name: msa2prfl.pl
# Project: Gene Prediction with Protein Family Patterns
# Author: Oliver Keller
# Date: 2011-01-07
# Version: 0.2 (rev.374)
#
#
# msa2prfl.pl [<options>] <MSA>
#
# Converts a Multiple Sequence Alignment in FASTA or CLUSTAL format into
# a protein block profile that can be used as input for AUGUSTUS-PPX.
#
# Uses a BLOSUM q_ij matrix to calculate pseudocounts which is searched for
# in a subdirectory of $AUGUSTUS_CONFIG_PATH. Ensure that the environment
# variable AUGUSTUS_CONFIG_PATH is set.
#
# Options:
# --width=i minimal block width (default: 6)
# --qij=s alternative file containing BLOSUM q_ij matrix
# --max_entropy=f maximal entropy of a block column (default: disabled)
# --keep_empty do not remove empty columns from alignment
# --prefix_from_seqnames if MSA contains partial sequences, determine
# sequence offset from sequence name as in XXXX/<from>-<to>
# --relax consider MSA as partial (allow arbitrary distance at
# beginning and end)
# --blockscorefile=s create a log file with blocks from the MSA and their scores
# --setname=s set a name for the profile
# --setdesc=s set a description for the profile
# --setacc=s set an accession id for the profile
# --info=s provide a text file containing the meta information
use strict;
use warnings;
use Getopt::Long;
use List::Util qw/sum min max/;
my @seqnames;
my %sequences;
my $MIN_WIDTH=6;
my $REG_MATR_FILE;
my $WEIGHTING_MODE="none";
my $DEFAULT_GLOB_WEIGHT=20;
my $GLOB_WEIGHT;
my $AMINO_ACIDS = "GDERKNQSTAVLIFYWHMCP";
my $QIJ_ORDER = "ARNDCQEGHILKMFPSTWYV";
my @AA_LIST = split "", $AMINO_ACIDS;
my $SETNAME;
my $SETDESC;
my $SETACC;
my $INFOFILE;
my $KEEP_EMPTY;
my $USE_MSA_DIST;
my $MAX_ENTROPY;
my $MIN_CONSERVE;
my $DISABLE_WEIGHTS;
my $PREFIX_FROM_SEQNAMES;
my $RELAX;
my $AA_COUNT = length($AMINO_ACIDS);
my $LOG20 = log($AA_COUNT);
my $INITARGS = join(" ",@ARGV);
my $blockscorefile;
my %backcol;
@backcol{@AA_LIST} = ( 0.07088, 0.05268, 0.06270, 0.05256, 0.05807,
0.04439, 0.04037, 0.07068, 0.05837, 0.07689,
0.06538, 0.09200, 0.05527, 0.03995, 0.03224,
0.01312, 0.02253, 0.02353, 0.01793, 0.05046 );
my $minFreq = 0.0001;
if (defined $ENV{"minFreq"}) {
$minFreq = $ENV{"minFreq"};
}
# set this to 1 if the same behavious as block2prfl.pl is desired
my $COMPATIBILITY_MODE = 0;
if ($COMPATIBILITY_MODE) {
$WEIGHTING_MODE="blimps";
}
# note: if this is changed,
# block dist calculation has to be modified
my $MAX_GAP_COUNT = 0;
### subroutines
sub usage {
print STDERR "
Usage:
msa2prfl.pl [<options>] <MSA>
Converts a Multiple Sequence Alignment in FASTA or CLUSTAL format into
a protein block profile that can be used as input for AUGUSTUS-PPX.
Uses a BLOSUM q_ij matrix to calculate pseudocounts which is searched for
in a subdirectory of \$AUGUSTUS_CONFIG_PATH. Ensure that the environment
variable AUGUSTUS_CONFIG_PATH is set.
Options:
--width=i minimal block width (default: 6)
--qij=s alternative file containing BLOSUM q_ij matrix
--max_entropy=f maximal entropy of a block column (default: disabled)
--keep_empty do not remove empty columns from alignment
--prefix_from_seqnames if MSA contains partial sequences, determine
sequence offset from sequence name as in XXXX/<from>-<to>
--relax consider MSA as partial (allow arbitrary distance at
beginning and end)
--blockscorefile=s create a log file with blocks from the MSA and their scores
--setname=s set a name for the profile
--setdesc=s set a description for the profile
--setacc=s set an accession id for the profile
--info=s provide a text file containing the meta information
--help show this message
";
}
#
# merge a block into a interblock region
#
sub merge_ibr {
my ($ibr, $bl) = @_;
foreach (0..$#$ibr) {
$ibr->[$_] +=
$bl->{ibr}[$_] + scalar @{$bl->{columns}};
}
}
#
# read in a clustal file
#
sub read_clustal {
my ($seqref, $seqnameref) = @_;
my $partlen=0;
my $newpartlen=0;
my $seqcount=0;
my $status=0;
while (<>) {
last if /^\/\/$/;
if (/^(\s*\S+)\s+([A-Za-z. -]+)/) {
$status=1;
my ($seqname, $newseq) = ($1, $2);
# Be tolerant with sequence names, but require
# at least one character not used in the conservedness line
# ignore line if it only contains conservedness characters
if ($seqname=~s/^\s+//) {
next unless $seqname=~/[^:.* ]/;
}
$newseq =~ s/ //g;
if ($newpartlen == 0) {
$newpartlen = length($newseq);
} elsif ($newpartlen != length($newseq)) {
die "CLUSTAL format error: Sequence length mismatch";
}
unless (exists $seqref->{$seqname}) {
push @$seqnameref, $seqname;
$seqref->{$seqname} = "-" x $partlen;
}
my $to_add = $partlen - length($seqref->{$seqname});
if ($to_add < 0) {
die "CLUSTAL format error: Sequence names not unique";
} else {
$seqref->{$seqname} .= $newseq;
}
} elsif (/^\s*$/ && $status==1) {
$partlen += $newpartlen;
$newpartlen=0;
foreach (values %$seqref) {
### DEBUG part
if (length($_) > $partlen) {
die "Internal bug in CLUSTAL parser. Please report";
}
###
$_ .= "-" x ($partlen - length($_));
}
$status=0;
}
}
}
#
# read in the qij (regularisation) matrix
#
sub read_qij {
my $fh = shift;
my @SM_AAS = ();
my $matrix = {};
my $sm_count=0;
while (<$fh>) {
chomp;
s/\#.*$//;
s/^\s*|\s*$//g;
next if (/^$/);
unless (@SM_AAS) {
my $FILE_AAS=uc;
$FILE_AAS =~ s/[^A-Z]//g;
my $patt = '^['.$FILE_AAS.']*$';
if (length($FILE_AAS)==20 && $AMINO_ACIDS =~ /$patt/) {
@SM_AAS = split "", $FILE_AAS;
next;
} else {
@SM_AAS = split "", $QIJ_ORDER;
}
}
my @line = split(/\s+/);
if (@line <= $sm_count || @line > 20 || grep { !/^\d+\.\d*$|^\.\d+$/ } @line) {
print STDERR "Not a valid frequency file!\n";
return undef;
}
@{$matrix->{$SM_AAS[$sm_count]}}{@SM_AAS} = @line;
$sm_count++;
last if ($sm_count == 20);
}
# normalizing - the substitution matrix (SM) is scaled the following way:
# - it is made symmetric by substituting SM_ij and SM_ji with (SM_ij+SM_ji/2)
# - every entry is divided by the total sum of all entries
my $sm_sum=0;
foreach (0..$#SM_AAS) {
my $aa1 = $SM_AAS[$_];
foreach my $aa2 (@SM_AAS[0..$_-1]) {
my $ref1 = \$matrix->{$aa1}{$aa2};
unless (defined $matrix->{$aa2}{$aa1}) {
$matrix->{$aa2}{$aa1} = $$ref1;
} else {
my $ref2 = \$matrix->{$aa2}{$aa1};
my $val = ($$ref1 + $$ref2)/2;
$$ref1 = $$ref2 = $val;
}
$sm_sum += 2 * $$ref1;
}
$sm_sum += $matrix->{$aa1}{$aa1};
}
foreach (@SM_AAS) {
foreach my $aa2 (@SM_AAS) {
$matrix->{$_}{$aa2} /= $sm_sum;
}
$matrix->{"*"}{$_} = sum(@{$matrix->{$_}}{@SM_AAS});
}
return $matrix;
}
sub output_format {
return sprintf(($_[0] < 1e-4 ? "%.2g" : $COMPATIBILITY_MODE ? "%.5f" : "%7.5f" ), @_);
}
sub format_collist {
my @pos = map { $_->{num} } @{$_[0]};
my $firstpos = shift @pos;
my @output = ( [$firstpos, $firstpos] );
foreach (@pos) {
if ($_ == $output[-1][1] + 1) {
$output[-1][1]++;
} else {
push @output, [ $_, $_ ];
}
}
foreach (@output) {
if ($_->[0] == $_->[1]) {
$_ = $_->[0];
} elsif ($_->[0]+1 == $_->[1]) {
$_ = join(",",@$_);
} else {
$_ = join("-", @$_);
}
}
return join(",",@output);
}
############ main ###############
my $HELP;
GetOptions("width=i" => \$MIN_WIDTH,
"qij=s" => \$REG_MATR_FILE,
"mode=s" => \$WEIGHTING_MODE,
"regweight=f" => \$GLOB_WEIGHT,
"max_entropy=f" => \$MAX_ENTROPY,
"min_conserve=f" => \$MIN_CONSERVE,
"keep_empty" => \$KEEP_EMPTY,
"use_msa_dist" => \$USE_MSA_DIST,
"prefix_from_seqnames" => \$PREFIX_FROM_SEQNAMES,
"relax" => \$RELAX,
"setname=s" => \$SETNAME,
"setdesc=s" => \$SETDESC,
"setacc=s" => \$SETACC,
"noweights" => \$DISABLE_WEIGHTS,
"blockscorefile=s" => \$blockscorefile,
"info=s" => \$INFOFILE,
"help" => \$HELP,
);
if ($HELP) {
&usage();
exit(0);
}
unless (defined $GLOB_WEIGHT) {
$GLOB_WEIGHT = $DEFAULT_GLOB_WEIGHT;
$GLOB_WEIGHT /= 4 if $WEIGHTING_MODE eq "blimps";
}
if (!defined $MAX_ENTROPY && defined $MIN_CONSERVE) {
$MAX_ENTROPY = 1 - $MIN_CONSERVE;
}
if (defined $INFOFILE && -f $INFOFILE && open (INFOFILE, $INFOFILE)) {
while (<INFOFILE>) {
$SETACC = $1 if /^Accession number:\s*(\S+)/ && !defined $SETACC;
$SETNAME = $1 if /^Name:\s*(\S+)/ && !defined $SETNAME;
$SETDESC = $1 if /^Description:\s*(\S.*)/ && !defined $SETDESC;
}
close INFOFILE;
}
foreach ($SETNAME, $SETACC) {
$_ = "unknown" unless defined;
}
# directories for config files
# this is the method to be used for all AUGUSTUS-PPX scripts
my @CONFIG_DIRS = ("");
if (defined $ENV{"AUGUSTUS_CONFIG_PATH"}) {
push (@CONFIG_DIRS, $ENV{"AUGUSTUS_CONFIG_PATH"}."/profile/");
}
if ($0 =~ /^(.*\/)/ && $1 ne "./") {
push (@CONFIG_DIRS, $1);
}
#
# look for regularisation matrix file
#
my $regmatrix;
unless (defined $REG_MATR_FILE) {
$REG_MATR_FILE="default.qij";
} elsif ($REG_MATR_FILE =~ s/^((~.*?|)\/)//) {
# absolute directory given: do not check other dirs
@CONFIG_DIRS=($1);
}
foreach (@CONFIG_DIRS) {
my $fh;
if (-e "$_$REG_MATR_FILE" && open $fh, "$_$REG_MATR_FILE") {
$regmatrix = read_qij($fh);
last;
}
}
unless (defined $regmatrix) {
print STDERR "Qij file '$REG_MATR_FILE' could not be read.\nCheck the parameter --qij=<filename>.\n\n";
die;
}
#
# read in fasta/clustal file
#
while (<>) {
if ($. == 1 && /^CLUSTAL/) {
&read_clustal(\%sequences, \@seqnames);
last;
}
chomp;
if (s/^>//) {
chomp;
while (exists $sequences{$_}) {
if (s/_\((\d+)\)$//) {
$_ .= sprintf("_(%d)", $1+1);
} else {
$_ .= "_(1)";
}
}
push @seqnames, $_;
} elsif (@seqnames) {
$sequences{$seqnames[-1]} .= $_;
}
}
my $width=0;
my $height = @seqnames;
foreach (values %sequences) {
s/[^A-Za-z.-]//g;
$width=length if $width<length;
}
foreach (values %sequences) {
my $rem = $width - length;
if ($rem) {
print STDERR "Warning: sequence lengths differ. Filled up with gaps.\n";
$_ .= "-" x $rem;
}
}
my @blocks;
my $last_ibr = $PREFIX_FROM_SEQNAMES ?
[ map { /\/(\d+)-(\d+)$/ && $1 > 0 ? $1-1 : 0 } @seqnames ] :
[ (0) x ($height+1) ];
push @$last_ibr, max @$last_ibr if ($PREFIX_FROM_SEQNAMES);
print STDERR "Determining block columns...\n";
foreach my $i (0..($width-1)) {
my $val={ str=>[], num=>$i, frq=>{ "-" => 0, "." => 0} };
my $percentage = int(20*$i/$width)*5;
foreach (@seqnames) {
my $c = substr($sequences{$_}, $i, 1);
push @{$val->{str}}, $c;
$val->{frq}{$c}++;
}
my $gapcount = sum(@{$val->{frq}}{"-","."});
# if ($i == 180) {
# print STDERR "\n".join("", @{$val->{str}})."\n";
# }
if ($gapcount <= $MAX_GAP_COUNT && !grep { /[a-z]/ } keys %{$val->{frq}}) {
# print STDERR "*";
# a new block column
if (defined $last_ibr || @blocks == 0) {
# begin new block
push @blocks, { columns => [ $val ], ibr => $last_ibr };
undef $last_ibr;
} else {
# add column to existing block
push @{$blocks[-1]{columns}}, $val;
}
} elsif ($gapcount < $height || $KEEP_EMPTY) {
# print STDERR ".";
unless (defined $last_ibr) {
$last_ibr = [ (0) x ($height+1) ];
if (@blocks && scalar @{$blocks[-1]{columns}} < $MIN_WIDTH) {
merge_ibr ($last_ibr, pop @blocks);
}
}
foreach (0..($height-1)) {
$last_ibr->[$_]++ if $val->{str}[$_] !~ /[.-]/;
}
$last_ibr->[$height]++;
} else {
if ($gapcount > $height) {
die "gaps=$gapcount height=$height";
}
# print STDERR " ";
}
# print STDERR "\n" unless ($i % 80);
printf STDERR "\r%2d%%", $percentage;
}
print STDERR "\rdone\n";
unless (defined $last_ibr) {
# profile ends with a block
my $lastbl = $blocks[-1];
my $blw = scalar @{$lastbl->{columns}};
$last_ibr= [(0) x ($height+1)];
if ($blw < $MIN_WIDTH) {
merge_ibr($last_ibr, $lastbl);
pop @blocks;
}
}
foreach (@blocks) {
$_->{width} = scalar @{$_->{columns}};
}
unless ($DISABLE_WEIGHTS) {
foreach my $bl (@blocks) {
next if $bl->{width}==0;
# calculate the weights
my $weights = [ (0) x $height ];
foreach (@{$bl->{columns}}){
my $frq = $_->{frq};
my @res = grep { /^[$AMINO_ACIDS]$/ } keys %$frq;
my %colweights = map { ( $_ => 0 ) } keys %$frq;
@colweights{@res} = map { 1/$frq->{$_}/(scalar @res) } @res;
my $i=0;
foreach my $c (@{$_->{str}}) {
$weights->[$i++] += $colweights{$c};
}
}
### DEBUG part
#
if ($COMPATIBILITY_MODE) {
$bl->{compweights} = [ map { sprintf("%.3f", $_ * $height / $bl->{width}) } @$weights ];
}
###
$_ /= $bl->{width} foreach (@$weights);
$bl->{weights} = $weights;
}
}
my @suffices = ("A".."Z", "AA".."ZZ");
foreach my $bl (@blocks) {
my @entropies;
foreach (@{$bl->{columns}}) {
my $freq = $_->{frq};
# apply weights
unless ($DISABLE_WEIGHTS) {
%$freq = ();
my $comptotal = sum (@{$bl->{compweights}}) if $COMPATIBILITY_MODE;
foreach my $i (0..$height-1) {
$freq->{$_->{str}[$i]} += ($COMPATIBILITY_MODE ?
$bl->{compweights}[$i]/$comptotal :
$bl->{weights}[$i]);
}
} else {
$_ /= $height foreach values %$freq;
delete $freq->{$_} foreach grep { /[^$AMINO_ACIDS]/ } keys %$freq;
}
### DEBUG
if ($COMPATIBILITY_MODE) {
foreach ( ["U","C"], ["B","D"], ["Z","E"] ) {
if (exists $freq->{$_->[0]}) {
$freq->{$_->[1]} += $freq->{$_->[0]};
delete $freq->{$_->[0]};
}
}
my $rem_weight = 0;
foreach (grep { /[^$AMINO_ACIDS]/ } keys %$freq ) {
$rem_weight += $freq->{$_};
delete $freq->{$_};
}
if ($rem_weight>0) {
$freq->{$_} += $rem_weight/$AA_COUNT foreach @AA_LIST;
}
} else {
###
# substitute non-standard keys
# U -> [C] O->[K]
# B -> [DN] J->[IL]
# Z -> [EQ] X->(any)
foreach ( ["U","C"], ["B","D","N"], ["Z","E","Q"],
["O", "K"], ["J", "I", "L"], ["X", @AA_LIST] ) {
next unless exists $freq->{$_->[0]};
my $rem = $freq->{$_->[0]};
delete $freq->{shift @$_};
my @freqs = @{$regmatrix->{"*"}}{@$_};
my $factor = $rem / sum(@freqs);
foreach (@$_) {
$freq->{$_} += (shift @freqs)*$factor;
}
}
### DEBUG
}
if (keys %$freq > $AA_COUNT) {
die "Have keys '".join("", sort keys %$freq,)."'\nshould have '".join("", sort @AA_LIST)."'";
}
if (abs(sum (values %$freq) - 1) > 1e-4) {
die sprintf("col %d: sum is not 1 but %f ", $_->{num}, sum(values %$freq));
}
###
# calculate regularised counts (used as pseudo counts)
my $res_count = scalar grep { $freq->{$_} != 0 } keys %$freq;
my $regweight = $GLOB_WEIGHT / $height;
$regweight *= $res_count if $WEIGHTING_MODE eq "blimps";
my %reg = ();
foreach my $aa (@AA_LIST) {
$reg{$aa} =
sum ( map { $freq->{$_} * $regmatrix->{$_}{$aa} / $regmatrix->{"*"}{$_} }
keys %$freq );
}
foreach my $aa (@AA_LIST) {
$freq->{$aa} += $regweight * $reg{$aa};
$freq->{$aa} /= (1 + $regweight);
}
if (defined $MAX_ENTROPY) {
my $entropy=0;
my $total=1;
foreach my $val (values %$freq) {
$entropy -= $val * log($val);
$total -= $val;
}
$entropy /= $LOG20;
if (abs($total) > 1e-4) {
print STDERR "Warning: column $_->{num} does not add up to 1!";
print STDERR "Deviation is $total.\n";
}
push @entropies, $entropy;
}
} # end for each column
# mark entropy based cutoffs
last unless @{$bl->{columns}};
@{$bl}{"from", "to"} = (0,$bl->{width});
if (defined $MAX_ENTROPY) {
while ($bl->{to} - $bl->{from} >= $MIN_WIDTH) {
if ($entropies[$bl->{from}]>$MAX_ENTROPY) {
$bl->{from}++;
} elsif ($entropies[$bl->{to}-1]>$MAX_ENTROPY) {
$bl->{to}--;
} else {
last;
}
}
# high-entropy blocks to be removed
if (sum(@entropies[$bl->{from}..$bl->{to}-1]) > $MAX_ENTROPY*($bl->{to}-$bl->{from})) {
$bl->{from} = $bl->{to};
}
}
}
push @blocks, { width=>0, ibr => $last_ibr };
for (my $i = 0; $i < $#blocks; $i++) {
my ($bl, $nextbl) = @blocks[$i, $i+1];
# set name to AC, plus consecutive suffix
$bl->{name} = $SETACC."_".$suffices[$i];
# remove cut off and high-entropy blocks
my $cutoff = $bl->{width} - $bl->{to};
$bl->{width} = $bl->{to} - $bl->{from};
if ($bl->{width} < $MIN_WIDTH) {
merge_ibr($nextbl->{ibr}, $bl);
splice(@blocks, $i, 1);
last if $i >= $#blocks;
redo;
}
# apply right cutoff
splice(@{$bl->{columns}}, $bl->{to});
$_ += $cutoff foreach (@{$nextbl->{ibr}});
# apply left cutoff
splice(@{$bl->{columns}}, 0, $bl->{from});
$_ += $bl->{from} foreach (@{$bl->{ibr}});
}
if (sum (map{$_->{width}} @blocks)==0) {
print STDERR "No blocks found in MSA. Use \"prepareAlign\" to eliminate sequences.\n";
exit;
}
# calculate interblock ranges
foreach (map { $_->{ibr} } @blocks) {
my $maxdist=pop @$_;
$maxdist=max @$_ unless $USE_MSA_DIST;
@$_ = ( min(@$_), $maxdist );
}
if ($RELAX) {
$blocks[0]{ibr}[1] = $blocks[-1]{ibr}[1] = "*";
} elsif ($PREFIX_FROM_SEQNAMES && grep { /\/\d+-\d+$/ } @seqnames) {
$blocks[-1]{ibr}[1] = "*";
}
### output the profile
print "[name]\n$SETNAME\n";
print "# $SETDESC\n" if defined $SETDESC;
my $blcount=0;
my $colcount=0;
my $minlen=0;
foreach (@blocks) {
print "\n[dist]\n# distance from previous block\n";
print "# <min> <max>\n";
print join("\t", @{$_->{ibr}})."\n";
$minlen += $_->{ibr}[0];
if (defined $_->{columns} && @{$_->{columns}}) {
print "\n[block]\n# block no. $blcount follows, $height sequences, length $_->{width}\n";
print "# corresponding to MSA columns:\n";
print "# ".&format_collist($_->{columns})."\n";
print "name=$_->{name}\n";
$blcount++;
$colcount += $_->{width};
$minlen += $_->{width};
print "#\n# <colnr> <probs for $AMINO_ACIDS>\n";
print "#\t".join("\t", @AA_LIST)."\n";
$_->{mu} = $_->{var} = 0;
for (my $colno = 0; $colno < $_->{width}; $colno++) {
my $freq = $_->{columns}[$colno]{frq};
my $mu = 0;
foreach my $aa (@AA_LIST) {
my $oddlog = log($freq->{$aa} / $backcol{$aa});
$mu += $oddlog * $backcol{$aa};
$_->{var} += $oddlog * $oddlog * $backcol{$aa};
}
$_->{mu} += $mu;
$_->{var} -= $mu * $mu;
print "$colno\t".join("\t", map { output_format($freq->{$_}) } @AA_LIST)."\n";
}
}
}
print "\n# created by:\n";
print "# $0 $INITARGS\n";
if (defined $blockscorefile && open OUTFILE, ">$blockscorefile") {
my %output;
my @outseqs;
foreach my $seqname (@seqnames) {
my $seq = $sequences{$seqname};
my $pos=0;
my $netcount=0;
my $offset = ($PREFIX_FROM_SEQNAMES && $seqname=~s/\/(\d+)-\d+$//) ? $1-1 : 0;
unless (exists $output{$seqname}) {
push @outseqs, $seqname ;
$output{$seqname} = "$seqname:\n";
}
foreach (@blocks) {
next unless $_->{width};
my $startpos=$_->{columns}[0]{num};
my $endpos=$_->{columns}[-1]{num};
my $netdiff = $offset;
$offset=0;
while ($pos < $startpos) {
$netdiff++ if substr($seq, $pos, 1) =~ /^[A-Za-z]$/;
$pos++;
}
$netcount += $netdiff;
my $score=1;
my $outseq="";
foreach my $col (@{$_->{columns}}) {
my $c=substr($seq, $col->{num}, 1);
$outseq .= $c;
$c =~ tr/a-z/A-Z/;
if ($c =~ /^[$AMINO_ACIDS]$/) {
$score *= $col->{frq}{$c} / $backcol{$c};
} else {
$score = 0;
last;
}
}
my $spec;
if ($score==0) { $spec=0 } else {
$score=log($score) ;
$spec = ($score - $_->{mu})/sqrt($_->{var});
}
$output{$seqname}.="$netdiff\t$_->{name}\t";
$output{$seqname}.=sprintf("%8.5f\t%8.5f\t%5d %s", exp($score/$_->{width}), $spec, $netcount, $outseq);
$pos = $endpos+1;
$netcount+=$_->{width};
$output{$seqname}.=sprintf(" %d\n", $netcount);
}
}
print OUTFILE $output{$_}."--\n" foreach (@outseqs);
close OUTFILE;
}
print STDERR "Profile has $blcount blocks with $colcount columns.\n";
print STDERR "Minimum admissible sequence length: $minlen\n\n";
|