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
|
#!/usr/bin/perl
################################################################################
#
# Name: block2prfl.pl
# Project: Gene Prediction with Protein Family Patterns
# Author: Oliver Keller
# Date: 2011-01-07
# Version: 0.2 (rev.374)
#
#
# block2prfl.pl [<options>] <MSA>
#
# Converts a BLOCKS database flat file into a protein block profile
# that can be used as input for AUGUSTUS-PPX.
#
# Uses a BLOSUM matrix to calculate pseudocounts. Ensure that
# the variable AUGUSTUS_CONFIG_PATH is set in order to make that work.
#
use strict;
use warnings;
use File::Basename;
use List::Util ("min","max","sum");
use POSIX;
use Getopt::Long;
my $DISABLE_WEIGHTS = 0;
my $AMINO_ACIDS = "GDERKNQSTAVLIFYWHMCP";
my $EXT_AAS = $AMINO_ACIDS."BZU";
my @AA_LIST = split "", $AMINO_ACIDS;
my $SHOW_ENTROPY = 0;
my $MIN_BLOCK_WIDTH = 6;
my ($MIN_CONSERVE, $MAX_ENTROPY);
my $LOG20 = log(scalar @AA_LIST);
my ($SKIPSTR, $USESTR, $name, $desc);
my $RELAX_AROUND_SKIPPED = 0; # whether to allow arbitrary distances around explicitly skipped blocks
my $RELAX_TERM; # allow arbitrary distance to sequence start and end
my $INITARGS = join(" ",@ARGV);
my $OWNSCOREFILE;
my $GLOB_WEIGHT = 20;
my $WEIGHTING_MODE = "none";
GetOptions("show_entropy" => \$SHOW_ENTROPY,
"max_entropy=f" => \$MAX_ENTROPY,
"min_conserve=f" => \$MIN_CONSERVE,
"skip=s" => \$SKIPSTR,
"useonly=s" => \$USESTR,
"relax_around_skipped" => \$RELAX_AROUND_SKIPPED,
"relax_terminal" => \$RELAX_TERM,
"setname=s" => \$name,
"setdesc=s" => \$desc,
"min_blockwidth=i" => \$MIN_BLOCK_WIDTH,
"ownscorefile=s" => \$OWNSCOREFILE,
"mode=s" => \$WEIGHTING_MODE,
"regweight=f" => \$GLOB_WEIGHT);
if (!defined $MAX_ENTROPY && defined $MIN_CONSERVE) {
$MAX_ENTROPY = 1 - $MIN_CONSERVE;
}
if ($WEIGHTING_MODE eq "blimps") {
$GLOB_WEIGHT /= 4;
}
my $SMfilename = $ENV{"AUGUSTUS_CONFIG_PATH"}."/profile/default.qij" if defined $ENV{"AUGUSTUS_CONFIG_PATH"};
$SMfilename = $ENV{"QIJ"} if (defined $ENV{"QIJ"});
# try "./default.qij" if QIJ is not specified
$SMfilename = "default.qij" unless defined $SMfilename;
if (! -e $SMfilename && $0 =~ /^(.*\/)/ && -e "$1/$SMfilename") {
$SMfilename="$1/$SMfilename";
}
my $fh;
open QIJFILE, $SMfilename or die "Could not open '$SMfilename'\n";
my %matrix;
my %marg;
my @SM_AAS=();
my $sm_count=0;
sub cut_sides { # rightcut MUST be <= 0 !!!
my ($array, $leftcut, $rightcut) = @_;
splice(@$array, 0, $leftcut);
splice(@$array, $rightcut) if $rightcut < 0;
}
sub block_to_pssm {
my $blockref = shift;
my ($size, $seq_count) =
@$blockref{"width", "num_sequences"};
my $result = { "lines" => [] };
my @probs;
my $total_weight = 0;
foreach (0..$size-1) {
$probs[$_] = { map { ($_ => 0) } split("", $EXT_AAS) };
}
foreach my $arrayref (values %{$blockref->{sequences}})
{
my ($seq, $weight) = @$arrayref[1,2];
# print STDERR "sequence: $seq ($weight)\n";
$weight = 1 / $seq_count if (!defined $weight || $DISABLE_WEIGHTS);
$total_weight += $weight;
for (0..$size-1)
{
my $key = substr($seq,0,1,"");
$probs[$_]{$key} += $weight;
}
}
my @entropies = ();
foreach my $pos (0..$size-1) {
my $total=1;
foreach ( ["U","C"], ["B","D"], ["Z","E"] ) {
$probs[$pos]{$_->[1]} += $probs[$pos]{$_->[0]};
}
my $rem_weight = 0;
foreach (grep(/[^$EXT_AAS]/,keys %{$probs[$pos]})) {
print STDERR "Unknown char '$_' found in column $pos in block $blockref->{number}.\n";
$rem_weight += $probs[$pos]{$_};
}
if ($rem_weight > 0) {
printf STDERR "Equally distributing a fraction of %5.3f\n", $rem_weight / $total_weight;
$_ += $rem_weight/20 foreach (@{$probs[$pos]}{@AA_LIST})
}
# calculate regularised counts (used as pseudo counts)
my %reg = ();
foreach my $aa (@AA_LIST) {
$reg{$aa} =
sum ( map { $probs[$pos]{$_} * $matrix{$_}{$aa} / $marg{$_} }
@AA_LIST );
}
my $res_count = grep { $probs[$pos]{$_} != 0 } @AA_LIST;
my $reg_weight = $GLOB_WEIGHT / $seq_count;
$reg_weight *= $res_count if $WEIGHTING_MODE eq "blimps";
my $entropy = 0;
my $line = "";
foreach (@AA_LIST)
{
my $val = ($probs[$pos]{$_} + $reg_weight*$reg{$_}) / $total_weight / (1+$reg_weight);
$entropy -= $val * log($val);
$total-=$val;
$line .= sprintf ($val < 1e-4 ? "\t%.2g" : "\t%.5f", $val);
}
$entropy /= $LOG20;
if (abs($total) > 1e-4) {
$line .= " # Rounding error: $total" ;
print STDERR "Warning: line $pos in block $blockref->{number} does not add up to 1!\n";
print STDERR "Deviation is $total\n";
}
push @entropies, $entropy;
push @{$result->{lines}}, $line;
}
my ($leftcut, $rightcut) = (0, 0);
if (defined $MAX_ENTROPY) {
while ($leftcut < @entropies && $entropies[$leftcut] > $MAX_ENTROPY) {
$leftcut++;
}
while ($rightcut > $leftcut-@entropies && $entropies[$rightcut-1] > $MAX_ENTROPY) {
$rightcut--;
}
if (($leftcut - $rightcut) > 0) {
&cut_sides(\@entropies, $leftcut, $rightcut);
if (@entropies < $MIN_BLOCK_WIDTH || sum(@entropies)/@entropies > $MAX_ENTROPY) {
$leftcut = $size;
$size = $rightcut = 0;
@entropies = ();
$result->{lines} = [];
print STDERR "Removed block $blockref->{number} from profile (entropy too high)\n";
} else {
$size -= ($leftcut - $rightcut);
&cut_sides($result->{lines}, $leftcut, $rightcut);
printf STDERR ("Removed %d columns (%d+%d) from block $blockref->{number} (entropy too high)\n", $leftcut - $rightcut, $leftcut, -$rightcut);
}
}
}
$result->{entropies} = [@entropies];
$result->{size} = $size;
@$result{"left_cutoff", "right_cutoff"} = ($leftcut, -$rightcut);
return $result;
}
sub read_a_block {
my $result = {};
my $status = 0;
while (<>) {
last if /^\/\/$/;
if ($status == 0) {
if (/^ID (.+); BLOCK$/) {
$result->{id}=$1;
$status = 1;
}
} elsif ($status == 1) {
if (s/; distance from previous block=\((-?\d+),(\d+)\).*$//) {
@{$result}{qw/min_prev max_prev/} = ($1, $2);
}
if (/^AC (.+)/) {
$result->{number} = $1;
} elsif (/^DE (.+)$/) {
$result->{de}=$1;
} elsif (/^BL (.+)width=(\d+);? seqs=(\d+)/) {
@{$result}{qw/motif width num_sequences/} = ($1, $2, $3);
$status = 2;
}
} elsif ($status == 2) {
next unless /^ *(\S+) +\( *(-?\d+)\) (\S*) *([\d|\.]+)?.*$/;
my $seq_name = "$1:$2";
next if (exists $result->{sequences}{$seq_name});
push (@{$result->{sequences}{$seq_name}}, $2, $3) ;
# save sequence weight if it was found
if (defined $4){
push (@{$result->{sequences}{$seq_name}}, $4) ;
}
}
}
return $result;
}
# Reading in substitution matrix Q_ij
while (<QIJFILE>) {
chomp;
s/\#.*$//;
s/^\s*|\s*$//g;
next if (/^$/);
unless (@SM_AAS) {
@SM_AAS=split (/\s+/, uc, 20);
if (@SM_AAS == 20 ) {
my $patt = '^['.join("",@SM_AAS).']*$';
next if ($AMINO_ACIDS =~ /$patt/);
}
@SM_AAS = @AA_LIST;
}
my @line = split(/\s+/);
if (@line <= $sm_count || @line > 20 || grep { !/^\d+\.\d*$|^\.\d+$/ } @line) {
die "Not a valid frequency file";
}
@{$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;
}
$marg{$_} = sum(@{$matrix{$_}}{@SM_AAS});
}
#@{$marg}{@SM_AAS} = map { sum(@{$matrix{$_}}{@SM_AAS}) } @SM_AAS;
my $blkcount=0;
my ($totalminp, $totalmaxp) = (0,0);
my $endwithempty;
my $blset_inverted = !defined $USESTR;
my %blset;
if (!$blset_inverted) {
$blset{$_} = 1 foreach (split(/,/, $USESTR));
}
if (defined $SKIPSTR) {
my @skiplist = split (/,/, $SKIPSTR);
if ($blset_inverted) {
$blset{$_} = 1 foreach (@skiplist);
} else {
delete $blset{$_} foreach (@skiplist);
}
}
my $relax = $RELAX_TERM;
until (eof()) {
my $blockref = read_a_block();
next unless defined $blockref;
my ($minp, $maxp)
= @$blockref{"min_prev", "max_prev"};
$minp = max (0, $minp);
if (defined $maxp) {
$maxp = max ($minp, $maxp);
} else {
$maxp="*";
}
# if the last block is empty, we assume it to be the sequence end
# in this case we finish after printing the distance to the previous block
$endwithempty = $blockref->{width} == 0;
$totalminp += $minp;
if ($totalmaxp ne "*") {
if ($maxp eq "*" ) {
$totalmaxp = "*";
} else {
$totalmaxp += $maxp;
}
}
if ($blset_inverted == exists $blset{$blockref->{number}}) {
# this block is skipped explicitly
# in case we remove the last block, we treat it as empty
$totalminp += $blockref->{width};
$totalmaxp += $blockref->{width} unless $totalmaxp eq "*";
$relax = $RELAX_AROUND_SKIPPED unless $relax;
next;
}
my $pssm = &block_to_pssm($blockref);
$totalminp += $pssm->{left_cutoff};
$totalmaxp += $pssm->{left_cutoff} unless $totalmaxp eq "*";
unless (defined $name) {
$name = $blockref->{id};
$name = "" unless defined $name;
} elsif ($name && defined $blockref->{id} && $blockref->{id} ne $name) {
print STDERR "Block name is '".$blockref->{id}."', while pattern will be named '$name'\n";
}
# do not print anything if the block was deleted due to high entropy
next if ($pssm->{size} == 0);
if ($name) {
$desc = $blockref->{de} unless defined $desc;
if (defined $desc) {
$desc = "# $desc\n";
} else {
$desc = "";
}
print "[name]\n$name\n$desc";
$name = "";
}
print "\n[dist]\n# distance from previous block\n# <min> <max>\n";
print ($relax ? "0\t*\n" : "$totalminp\t$totalmaxp\n");
print
"\n[block]\n# block no. ".($blkcount++)." follows, $blockref->{num_sequences} sequences, length $pssm->{size}\n";
my $blockname = $blockref->{number};
print "name=$blockname\n" if defined $blockname;
if ($pssm->{left_cutoff}) {
print "# note: PSSM column 0 corresponds to original block column $pssm->{left_cutoff}\n";
}
print "#\n".
"# <colnr> <probs for $AMINO_ACIDS>\n".
"# G D E R K N Q S T ".
"A V L I F Y W H M C P\n";
foreach my $pos (0..$pssm->{size}-1) {
print $pos.$pssm->{lines}[$pos]."\n";
if ($SHOW_ENTROPY) {
printf ("# conservedness in previous line (1-normed entropy): %.3f\n", 1-$pssm->{entropies}[$pos]);
}
}
($totalmaxp, $totalminp) = ($pssm->{right_cutoff}) x 2;
$relax=0;
}
if (($totalminp>0 || $endwithempty) && !$RELAX_TERM) {
$totalmaxp = "*" unless $endwithempty;
print "\n[dist]\n# distance from previous block\n# <min> <max>\n$totalminp\t$totalmaxp\n";
}
print "\n# created by:\n";
print "# $0 $INITARGS\n";
|