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
|
#!/usr/bin/perl
################################################################
# copyright (c) 2014,2015 by William R. Pearson and The Rector &
# Visitors of the University of Virginia */
################################################################
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under this License is distributed on an "AS
# IS" BASIS, WITHOUT WRRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language
# governing permissions and limitations under the License.
################################################################
################################################################
# m8_btop_msa.pl --query query.file blast_tab_btop_file
################################################################
# m8_btop_msa.pl takes a query sequence and a blast tabular format
# file with a BTOP field, and constructs a query-driven multiple
# sequence alignment of the subject sequences that can be used as
# input to psiblast with the "--in_msa msa.file" option.
#
# (because BLAST BTOP encoding provides the mismatched residues, the
# library sequences are not required to produce the MSA -- they are
# available in the BTOP string)
#
# The BTOP alignment encoding file generated from "blastp/n" or
# "blast_formatter" using the command: blast_formatter -archive
# blast_output.asn -outfmt '7 qseqid sseqid pident length mismatch
# gapopen qstart qend sstart send evalue bitscore score btop' >
# blast_output.tab_annot
#
# the raw score shown above is used by the annot_blast_btop2.pl
# program, if present, but is not required by m8_btop_msa.pl
#
################################################################
use warnings;
use strict;
use IPC::Open2;
use Pod::Usage;
use Getopt::Long;
# use Data::Dumper;
# read lines of the form:
# gi|121694|sp|P20432.1|GSTT1_DROME gi|121694|sp|P20432|GSTT1_DROME 100.00 209 0 0 1 209 1 209 6e-156 433 1113 209
# gi|121694|sp|P20432.1|GSTT1_DROME gi|1170090|sp|P04907|GSTF3_MAIZE 26.77 198 123 7 4 185 6 197 2e-08 51.2 121 FL1YG ... 1NKRA1YW1
# gi|121694|sp|P20432.1|GSTT1_DROME gi|81174731|sp|P0ACA5|SSPA_ECO57 39.66 58 32 2 43 100 49 103 8e-06 43.9 102 EDFLLI ... V-I-NEQS3FM
# gi|121694|sp|P20432.1|GSTT1_DROME gi|121695|sp|P12653|GSTF1_MAIZE 27.62 181 107 7 32 203 34 199 9e-05 40.8 94 LI1LF ... N-1AS1CLLM1
# and report the domain content ala -m 8CC
my ($shelp, $help, $evalue, $percid) = (0, 0, 0.001, 0.0);
my ($query_file, $bound_file, $have_qslen, $label_coords) = ("","",0,0);
my $query_lib_r = 0;
GetOptions(
"query=s" => \$query_file,
"query_file=s" => \$query_file,
"have_qslen" => \$have_qslen,
"evalue=f" => \$evalue,
"percid=f" => \$percid,
"coords!" => \$label_coords,
"bound_file=s" => \$bound_file,
"bound=s" => \$bound_file,
"seqbdr=s" => \$bound_file,
"h|?" => \$shelp,
"help" => \$help,
);
pod2usage(1) if $shelp;
pod2usage(exitstatus => 0, verbose => 2) if $help;
unless (-f STDIN || -p STDIN || @ARGV) {
pod2usage(1);
}
my @hit_list = ();
my @multi_align = ();
my @multi_names = ();
################
# get query sequence, and insert into MSA
#
my ($query_acc, $query_seq_r, $query_len);
if ($query_file) {
($query_acc, $query_seq_r) = parse_query_lib($query_file);
$query_len = scalar(@$query_seq_r)-1; # -1 for ' ' 1: offset
}
if (! $query_file || !$query_len) {
die "query sequence required";
}
push @multi_names, $query_acc;
push @multi_align, btop2alignment($query_seq_r, $query_len, {BTOP=>$query_len, q_start=>1, q_end=>$query_len});
my $max_sseqid_len = length($query_acc);
################
# get sequence boundaries if available
#
my $seq_bound_hr = 0;
if ($bound_file) {
$seq_bound_hr = parse_bound_file($bound_file)
}
my @tab_fields = qw(q_seqid s_seqid percid alen mismatch gopen q_start q_end s_start s_end evalue bits score BTOP);
if ($have_qslen) {
@tab_fields = qw(q_seqid q_len s_seqid s_len percid alen mismatch gopen q_start q_end s_start s_end evalue bits score BTOP);
}
while (my $line = <>) {
if ($line =~ m/^# Fields:/ && $line !~ m/bit score, score, BTOP/) {
# raw score missing, edit @tab_fields
pop @tab_fields;
pop @tab_fields;
push @tab_fields, "BTOP";
next;
}
next if ($line =~ m/^#/);
chomp $line;
next unless $line;
my %hit_data = ();
@hit_data{@tab_fields} = split(/\t/,$line);
next if ($hit_data{evalue} > $evalue);
next if ($hit_data{percid} < $percid);
# push @hit_list, \%hit_data;
if (length($hit_data{s_seqid}) > $max_sseqid_len) {
$max_sseqid_len = length($hit_data{s_seqid});
}
if ($bound_file) {
if (defined($seq_bound_hr->{$hit_data{subj_acc}})) {
push @multi_names, $hit_data{s_seqid};
push @multi_align, bound_btop2alignment($query_seq_r, $query_len, \%hit_data, @{$seq_bound_hr->{$hit_data{subj_acc}}}{qw(start end)});
}
}
else { # no sequence boundaries
push @multi_names, $hit_data{s_seqid};
push @multi_align, btop2alignment($query_seq_r, $query_len, \%hit_data);
}
}
# final MSA output
$max_sseqid_len += 4;
print "SSEARCHm8 multiple sequence alignment\n\n\n";
my $i_pos = 0;
my $del_cnt = 0;
for (my $j = 0; $j < $query_len/60; $j++) {
my $i_end = $i_pos + 59;
if ($i_end > $query_len) {$i_end = $query_len-1;}
my $label_str = "%-".$max_sseqid_len."s";
my $coord_str = " " x ($max_sseqid_len);
# build a coordinate line using the first sequence
if ($label_coords) {
my $cline = '';
for (my $ip=$i_pos; $ip <= $i_end; $ip++) {
$del_cnt++ if (${multi_align[0]}[$ip]eq'-');
my $ci_pos = $ip - $del_cnt;
last if $ci_pos > $query_len;
if ($ci_pos % 10 == 9) {
$cline .= sprintf(" %8d",$ci_pos+1);
}
}
print("$coord_str $cline\n");
}
for (my $n = 0; $n < scalar(@multi_names); $n++) {
printf("$label_str %s\n",$multi_names[$n],join("",@{$multi_align[$n]}[$i_pos .. $i_end]));
}
$i_pos += 60;
print "\n";
print "\n" unless ($label_coords);
}
# input: a blast BTOP string of the form: "1VA160TS7KG10RK27"
# returns a list_ref of tokens: (1, "VA", 60, "TS", 7, "KG, 10, "RK", 27)
#
sub decode_btop {
my ($btop_str) = @_;
my @tokens = split(/(\d+)/,$btop_str);
shift @tokens unless $tokens[0];
my @out_tokens = ();
for my $token (@tokens) {
if ($token =~ m/^\d+$/) {
push @out_tokens, $token
}
else {
my @mis_tokens = split(/(..)/,$token);
for my $mis (@mis_tokens) {
if ($mis) {push @out_tokens, $mis};
}
}
}
return \@out_tokens;
}
sub btop2alignment {
my ($query_seq_r, $query_len, $hit_data_hr) = @_;
# $query_seq_r is 1: based
my @alignment = ();
# make a local copy
# my @query_seq = @{$query_seq_r};
# the left unaligned region gets " ";
for (my $i=1; $i < $hit_data_hr->{q_start}; $i++) {
push @alignment, "-";
}
my $btop_align_r = decode_btop($hit_data_hr->{BTOP});
my ($seq0, $seq1) = ("","");
my $qix = $hit_data_hr->{q_start};
for my $btop (@{$btop_align_r}) {
if ($btop =~ m/^\d+$/) { # matching query sequence, add it up
for (my $i=0; $i < $btop; $i++) {
push @alignment, $query_seq_r->[$qix++];
}
}
else { # could be: TS/-S/T-
($seq0, $seq1) = split(//,$btop);
if ($seq0 ne '-') {
push @alignment, $seq1;
$qix++;
}
}
}
# all done with alignment, double check that $qix = $hit_data_hr->{q_end}
unless ($qix == $hit_data_hr->{q_end}+1) {
warn "$qix != ".$hit_data_hr->{q_end}+1;
}
for (my $i = $hit_data_hr->{q_end}+1; $i <= $query_len; $i++) {
push @alignment, "-";
}
return \@alignment;
}
sub bound_btop2alignment {
my ($query_seq_r, $query_len, $hit_data_hr, $sb_start, $sb_end) = @_;
# $query_seq_r is 1: based
my @alignment = ();
# the left unaligned region gets " ";
for (my $i=1; $i < $hit_data_hr->{q_start}; $i++) {
push @alignment, "-";
}
my $btop_align_r = decode_btop($hit_data_hr->{BTOP});
my ($seq0, $seq1) = ("","");
my ($qix, $six) = @{$hit_data_hr}{qw(q_start s_start)};
for my $btop (@{$btop_align_r}) {
if ($btop =~ m/^\d+$/) { # matching query sequence, add it up
for (my $i=0; $i < $btop; $i++) {
if ($six >= $sb_start && $six <= $sb_end) {
push @alignment, $query_seq_r->[$qix];
}
else {
push @alignment, '-';
}
$qix++; $six++;
}
}
else { # could be: TS/-S/T-
($seq0, $seq1) = split(//,$btop);
if ($seq1 eq '-') { # gap in subject
push @alignment, '-';
$qix++;
}
elsif ($seq0 ne '-') { # mismatch
if ($six >= $sb_start && $six <= $sb_end) {
push @alignment, $seq1;
}
else {
push @alignment, '-';
}
$qix++;
$six++;
}
else { # gap in query, consume $six
$six++;
}
}
}
# all done with alignment, double check that $qix = $hit_data_hr->{q_end}
unless ($qix == $hit_data_hr->{q_end}+1) {
warn $qix." != ".$hit_data_hr->{q_end}+1;
}
for (my $i = $hit_data_hr->{q_end}+1; $i <= $query_len; $i++) {
push @alignment, "-";
}
return \@alignment;
}
sub parse_query_lib {
my ($query_file) = @_;
my %query_seqs = ();
open(my $qfd, $query_file);
my ($header, $sequence) = ("","");
while (my $entry = <$qfd>) { # returns an entire fasta entry
chomp $entry;
if ($entry =~ m/^>/) {
$header = $entry;
}
else {
$sequence .= $entry
}
}
$sequence =~ s/[^A-Za-z\*]//g; # remove everything but letters
$sequence = uc($sequence);
$header =~ s/^>//;
$header =~ s/\s.*$//;
my @seq = split(//,$sequence);
unshift @seq,""; # @seq is now 1-based
return ($header, \@seq);
}
sub parse_query_file {
my ($query_file) = @_;
my $seq_data = "";
open(my $qfd, $query_file);
while (my $line = <$qfd>) {
next if $line =~ m/^>/;
next if $line =~ m/^;/;
chomp $line;
$line =~ s/[^A-Za-z\*]//g;
$seq_data .= $line
}
$seq_data = uc($seq_data);
my @seq = split(//,$seq_data);
return \@seq;
}
__END__
=pod
=head1 NAME
m8_btop_msa.pl
=head1 SYNOPSIS
m8_btop_msa.pl --query ../seq/mgstm1.aa blast_tabular_file
=head1 OPTIONS
-h short help
--help include description
--query -- fasta query sequence
-- same as --query_file
(can contain multiple sequences for multi-sequence search)
--have_qslen -- blast tabular file includes q_len, s_len
--evalue -- only include alignments better than E()-value
--bound_file -- also --bound, --seqbdr -- file specifying regions to be included
--coords -- provide coordinates based on query sequence
=head1 DESCRIPTION
C<m8_btop_msa.pl> takes a Blast tabular output file with a BTOP
encoded alignment (or a fasta -m8CB output file) and produces a
Clustal-like MSA from the implied alignments to the query sequence.
=head1 AUTHOR
William R. Pearson, wrp@virginia.edu
=cut
|