| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
# Do a piece of a rmark benchmark, using wu-blast to filter for cmsearch |
| 4 |
# the same way that Rfam uses. |
| 5 |
# |
| 6 |
# This script is normally called by rmark-master.pl; its command line |
| 7 |
# syntax is tied to rmark-master.pl. |
| 8 |
# x-rfamscan doesn't use <modeldir>, but it's passed in so that |
| 9 |
# rmark-master.pl can use a consistent command line structure for |
| 10 |
# all search programs (cmsearch uses it, for example). |
| 11 |
# |
| 12 |
# Usage: x-rfamscan <execdir> <scriptdir> <modeldir> <resultdir> <optsfile> <tblfile> <msafile> <posfile> <fafile> <outfile> |
| 13 |
# Example: ./x-rfamscan /usr/local/blast/bin/ ../rmark/ models testdir blastall-w7.opts test.tbl rmark3.msa rmark3.pos test.fa test.out |
| 14 |
# |
| 15 |
# Command-line options: |
| 16 |
# -P : run a positive-only benchmark, only each family's positive sequences will be searched |
| 17 |
# -M <n> : run MPI with <n> <= 8 processors, only valid if --mpi exists in the $optsfile |
| 18 |
# -C <f> : fetch models from existing file <f> |
| 19 |
# |
| 20 |
# SRE, Tue Apr 20 10:32:49 2010 [Janelia] |
| 21 |
# SVN $Id$ |
| 22 |
# |
| 23 |
use Getopt::Std; |
| 24 |
getopts('M:PC:'); |
| 25 |
$do_posonly = 0; |
| 26 |
$do_fetch_models = 0; |
| 27 |
if (defined $opt_P) { $do_posonly = 1; } |
| 28 |
$mpi_nprocs = 8; |
| 29 |
if (defined $opt_C) { $do_fetch_models = 1; $master_model = $opt_C; if($do_build_models) { die "-B and -C are incompatible"; } } |
| 30 |
if (defined $opt_M) { |
| 31 |
$mpi_nprocs = $opt_M; |
| 32 |
if($mpi_nprocs < 2 || $mpi_nprocs > 8) { die "ERROR, with -M <n>, <n> must be between 2 and 8"; } |
| 33 |
} |
| 34 |
|
| 35 |
#wublast e-value cutoff defaults: |
| 36 |
$eval_sens = 100; |
| 37 |
$eval_spec = 10; |
| 38 |
|
| 39 |
#lcmask defaults: |
| 40 |
$gap_thresh = 0.5; #Gap threshold for lcmask |
| 41 |
$nuc_freq_thresh = 0.1; #Nucleotide frequency threshold for lcmask |
| 42 |
$max_pid_thresh = 0.95; |
| 43 |
|
| 44 |
$usage = "Usage: x-rfamscan [options]\n\t<blastall executable>\n\t<scriptdir>\n\t<modeldir>\n\t<resultdir>\n\t<optsfile>\n\t<tblfile>\n\t<msafile>\n\t<posfile>\n\t<fafile>\n\t<outfile>\n"; |
| 45 |
$options_usage = "Options:\n\t"; |
| 46 |
$options_usage .= " -P : run a positive-only benchmark, only each family's positive sequences will be searched\n\t"; |
| 47 |
$options_usage .= " -M <n> : run MPI with <n> <= 8 processors, only valid if --mpi exists in the <optsfile>\n\n"; |
| 48 |
if(scalar(@ARGV) != 10) { printf("$usage\n$options_usage"); exit(1); } |
| 49 |
|
| 50 |
($execdir, $scriptdir, $modeldir, $resultdir, $optsfile, $tblfile, $msafile, $posfile, $fafile, $outfile) = @ARGV; |
| 51 |
$tmpoutfile = $outfile . ".tmp"; |
| 52 |
$sorttmpoutfile = $outfile . ".tmp.sort"; |
| 53 |
|
| 54 |
$idscript = "$scriptdir/rmark-idpositives.pl"; |
| 55 |
$wublastn = "$execdir/blastn"; |
| 56 |
$cmsearch = "$execdir/cmsearch"; |
| 57 |
$afetch = "$execdir/esl-afetch"; |
| 58 |
$sfetch = "$execdir/esl-sfetch"; |
| 59 |
$seqstat = "$execdir/esl-seqstat"; |
| 60 |
$reformat = "$execdir/esl-reformat"; |
| 61 |
$cmfetch = "$execdir/cmfetch"; |
| 62 |
if(! -e $afetch) { |
| 63 |
$afetch = "$execdir/../easel/miniapps/esl-afetch"; |
| 64 |
if(! -e $afetch) { die "$afetch does not exist, nor $execdir/$afetch"; } |
| 65 |
} |
| 66 |
if(! -e $sfetch) { |
| 67 |
$sfetch = "$execdir/../easel/miniapps/esl-sfetch"; |
| 68 |
if(! -e $sfetch) { die "$sfetch does not exist, nor $execdir/$sfetch"; } |
| 69 |
} |
| 70 |
if(! -e $reformat) { |
| 71 |
$reformat = "$execdir/../easel/miniapps/esl-reformat"; |
| 72 |
if(! -e $reformat) { die "$reformat does not exist"; } |
| 73 |
} |
| 74 |
if(! -e $seqstat) { |
| 75 |
$seqstat = "$execdir/../easel/miniapps/esl-seqstat"; |
| 76 |
if(! -e $seqstat) { die "$reformat does not exist"; } |
| 77 |
} |
| 78 |
if(! -e $wublastn) { |
| 79 |
$wublastn = "/usr/local/wublast/blastn"; |
| 80 |
if(! -e $wublastn) { die "$wublastn does not exist"; } |
| 81 |
} |
| 82 |
if($do_fetch_models) { |
| 83 |
if(! -e $cmfetch) { die "$cmfetch does not exist"; } |
| 84 |
} |
| 85 |
|
| 86 |
if (! -d $execdir) { die "didn't find executable directory $execdir"; } |
| 87 |
if (! -d $scriptdir) { die "didn't find script directory $scriptdir"; } |
| 88 |
if (! -e $resultdir) { die "$resultdir doesn't exist"; } |
| 89 |
if (! -e $posfile) { die "$posfile doesn't exist"; } |
| 90 |
if (! -e $idscript) { die "positive identification script $idscript doesn't exist"; } |
| 91 |
if (! -e $optsfile) { die "options file $optsfile doesn't exist"; } |
| 92 |
if (! -e $cmsearch) { die "executable $cmsearch doesn't exist"; } |
| 93 |
|
| 94 |
# read options file |
| 95 |
# first options line is SENSITIVITY stage wublast options, used for all families |
| 96 |
# second options line is SPECIFICITY stage wublast options, used for all families |
| 97 |
# third options line is cmsearch options, used for all families |
| 98 |
# all remaining lines are <fam> <fam-specific-options> for both sets of WUBLAST searches |
| 99 |
|
| 100 |
$do_mpi = 0; |
| 101 |
open(OPTS, $optsfile) || die "couldn't open options file $optsfile"; |
| 102 |
$senssearchopts = <OPTS>; |
| 103 |
chomp $senssearchopts; |
| 104 |
$specsearchopts = <OPTS>; |
| 105 |
chomp $specsearchopts; |
| 106 |
$cmsearchopts = <OPTS>; |
| 107 |
chomp $cmsearchopts; |
| 108 |
if($cmsearchopts =~ m/\-\-mpi/) { $do_mpi = 1; } |
| 109 |
while($line = <OPTS>) { |
| 110 |
chomp $line; |
| 111 |
$fam = $line; |
| 112 |
$fam =~ s/\s+.+$//; |
| 113 |
$opts = $line; |
| 114 |
$opts =~ s/^\S+\s+//; |
| 115 |
$fam_searchopts_H{$fam} = $opts; |
| 116 |
} |
| 117 |
close(OPTS); |
| 118 |
|
| 119 |
# index the fafile if nec |
| 120 |
if(! -e "$fafile.ssi") { |
| 121 |
$status = system("$sfetch --index $fafile"); |
| 122 |
if ($status != 0) { die "FAILED: $sfetch --index $fafile"; } |
| 123 |
} |
| 124 |
|
| 125 |
# get lengths of all seqs in $fafile, so we can edit |
| 126 |
# the sfetch input file so it doesn't contain invalid coords |
| 127 |
# greater than the length of the seqs |
| 128 |
$output = `$seqstat -a $fafile`; |
| 129 |
%falensH = (); |
| 130 |
@lines = split(/\n/, $output); |
| 131 |
foreach $line (@lines) { |
| 132 |
chomp $line; |
| 133 |
if($line =~ /^\=\s+(\S+)\s+(\d+)/) { |
| 134 |
$falensH{$1} = $2; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
open(OUTFILE,">$outfile") || die "failed to open $outfile"; |
| 139 |
open(TMPOUTFILE,">$tmpoutfile") || die "failed to open $tmpoutfile"; |
| 140 |
open(TABLE, "$tblfile") || die "failed to open $tblfile"; |
| 141 |
|
| 142 |
if($do_fetch_models) { |
| 143 |
$modeldir = $resultdir; # we'll fetch the models and read them from the $resultdir/ |
| 144 |
} |
| 145 |
|
| 146 |
$orig_fafile = $fafile; |
| 147 |
while (<TABLE>) |
| 148 |
{ |
| 149 |
$runtime = -1 * time(); |
| 150 |
|
| 151 |
($msaname) = split; |
| 152 |
|
| 153 |
if($do_fetch_models) { |
| 154 |
$status = system("$cmfetch -o $resultdir/$msaname.cm $master_model $msaname > /dev/null"); |
| 155 |
if ($status != 0) { die "FAILED: $cmfetch -o $resultdir/$msaname.cm $master_model > /dev/null"; } |
| 156 |
} |
| 157 |
|
| 158 |
# determine window size for this family, we require that this |
| 159 |
# was specified in the options file, as the argument for the 'hspsepSmax' option |
| 160 |
if(! exists($fam_searchopts_H{$msaname})) { die "ERROR $msaname family specific options not read from $searchopts"; } |
| 161 |
if($fam_searchopts_H{$msaname} =~ /^.*hspsepSmax=(\d+).*$/) { |
| 162 |
$window = $1; |
| 163 |
} |
| 164 |
else { die "ERROR $msaname family specific options do not include hspsepSmax=\d+"; } |
| 165 |
|
| 166 |
# fetch the msa and format it as pfam |
| 167 |
$command = "$afetch $msafile $msaname | $reformat --informat stockholm -o $resultdir/$msaname.sto pfam -"; |
| 168 |
$status = system("$command"); |
| 169 |
if ($status != 0) { die "FAILED: $command"; } |
| 170 |
|
| 171 |
# generate the query fasta file, with some residues lower-cased, like Rfam does |
| 172 |
# 1. any nucleotides in a column with >50% gap get lower-cased. |
| 173 |
# 2. any nucleotides in a column with frequency <10% also get lower-cased. |
| 174 |
# 3. filter any sequences that are >95% identical to any other sequence in the alignment. |
| 175 |
# |
| 176 |
my %seedseqlist = (); |
| 177 |
make_lcmask_file("$resultdir/$msaname.sto", "$resultdir/$msaname.query", \%seedseqlist); |
| 178 |
|
| 179 |
# run wublast, stage 1, specificity |
| 180 |
$command = "$wublastn $fafile $resultdir/$msaname.query mformat=3 cpus=1 $specsearchopts $fam_searchopts_H{$fam} > $resultdir/$msaname.search1"; |
| 181 |
$status = system("$command"); |
| 182 |
if ($status != 0) { die "FAILED: $command"; } |
| 183 |
|
| 184 |
# run wublast, stage 2, sensitivity |
| 185 |
$command = "$wublastn $fafile $resultdir/$msaname.query mformat=3 cpus=1 $senssearchopts $fam_searchopts_H{$fam} >> $resultdir/$msaname.search1"; |
| 186 |
$status = system("$command"); |
| 187 |
if ($status != 0) { die "FAILED: $command"; } |
| 188 |
|
| 189 |
# create sfetch input file with list of merged/de-overlapped hits from wublast |
| 190 |
$sfetch_in = "$resultdir/$msaname.sfetch"; |
| 191 |
$printNum = wublast2minidb( $window, "$resultdir/$msaname.search1", $sfetch_in); |
| 192 |
|
| 193 |
# revise sfetch file so that no seq indices are less than 1 or greater than the length of each sequence |
| 194 |
$new_sfetch_in = $sfetch_in . ".new"; |
| 195 |
open(SFETCH, $sfetch_in); |
| 196 |
open(NSFETCH, ">" . $new_sfetch_in); |
| 197 |
while($line = <SFETCH>) { |
| 198 |
chomp $line; |
| 199 |
if($line !~ m/\w/) { next; } |
| 200 |
if($line =~ /(\S+)\/(\d+)-(\d+)\s+(\d+)\s+(\d+)\s+(\S+)/) { |
| 201 |
$t = $1; |
| 202 |
$i11 = $2; |
| 203 |
$i21 = $3; |
| 204 |
$i12 = $4; |
| 205 |
$i22 = $5; |
| 206 |
if(! exists($falensH{$t})) { die "ERROR falensH{$t} does not exist\n"; } |
| 207 |
if($i11 != $i12) { die "ERROR sfetch file wrong1: $line\n"; } |
| 208 |
if($i21 != $i22) { die "ERROR sfetch file wrong2: $line\n"; } |
| 209 |
if($i11 > $falensH{$t}) { $i11 = $falensH{$t}; } |
| 210 |
if($i21 > $falensH{$t}) { $i21 = $falensH{$t}; } |
| 211 |
printf NSFETCH ("%s/%d-%d %d %d %s\n", $t, $i11, $i21, $i11, $i21, $t); |
| 212 |
} |
| 213 |
else { die "ERROR sfetch file wrong3: $line\n"; } |
| 214 |
} |
| 215 |
close(SFETCH); |
| 216 |
close(NSFETCH); |
| 217 |
$command = "mv $new_sfetch_in $sfetch_in"; |
| 218 |
$status = system("$command"); |
| 219 |
if ($status != 0) { die "FAILED: $command"; } |
| 220 |
|
| 221 |
# extract sequences into filtered database |
| 222 |
$filtered_db = "$resultdir/$msaname.sub.fa"; |
| 223 |
$status = system("$sfetch -Cf $fafile $sfetch_in > $filtered_db"); |
| 224 |
if ($status != 0) { die "FAILED: $sfetch -Cf $fafile $sfetch_in > $filtered_db"; } |
| 225 |
|
| 226 |
# run cmsearch on filtered_db |
| 227 |
if($do_mpi) { |
| 228 |
$command = "mpirun -np $mpi_nprocs $cmsearch --noalign --toponly $cmsearchopts --tabfile $resultdir/$msaname.tmp $modeldir/$msaname.cm $filtered_db > $resultdir/$msaname.search"; |
| 229 |
$status = system("$command"); |
| 230 |
if ($status != 0) { die "FAILED: $command"; } |
| 231 |
} |
| 232 |
else { |
| 233 |
$command = "$cmsearch --noalign --toponly $cmsearchopts --tabfile $resultdir/$msaname.tmp $modeldir/$msaname.cm $filtered_db > $resultdir/$msaname.search"; |
| 234 |
$status = system("$command"); |
| 235 |
if ($status != 0) { die "FAILED: $command"; } |
| 236 |
} |
| 237 |
|
| 238 |
# parse output, being careful to add positions correctly to get original coordinates in $fafile |
| 239 |
open(OUTPUT, "$resultdir/$msaname.tmp") || die "FAILED: to open $resultdir/$msaname.tmp tabular output file"; |
| 240 |
while (<OUTPUT>) |
| 241 |
{ |
| 242 |
if (/^\#/) { next; } |
| 243 |
@fields = split(' ', $_, 9); |
| 244 |
$target = $fields[1]; |
| 245 |
$target_from = $fields[2]; |
| 246 |
$target_to = $fields[3]; |
| 247 |
$bitscore = $fields[6]; |
| 248 |
$pval = $fields[7]; |
| 249 |
|
| 250 |
# $target includes number of positions to add/subtract from $target_from/$target_to to get |
| 251 |
# coordinates in original $fafile. |
| 252 |
# Example: |
| 253 |
|
| 254 |
$offset_range = $target; |
| 255 |
$target =~ s/\/.+$//; # remove everything after first "/" |
| 256 |
$offset_range =~ s/^.+\///; # remove everything up to final "/" |
| 257 |
($offset1, $offset2) = split("-", $offset_range); |
| 258 |
if($offset1 < $offset2) { # original hit from wublast filter was on forward strand |
| 259 |
$target_from += ($offset1-1); |
| 260 |
$target_to += ($offset1-1); |
| 261 |
} |
| 262 |
else { # original hit from nhmmer was on reverse strand |
| 263 |
$target_from = $offset1 - $target_from + 1; |
| 264 |
$target_to = $offset1 - $target_to + 1; |
| 265 |
} |
| 266 |
|
| 267 |
printf TMPOUTFILE "%10g %10.1f %10d %10d %20s %35s\n", $pval, $bitscore, $target_from, $target_to, $target, $msaname; |
| 268 |
} |
| 269 |
unlink "$resultdir/$msaname.tmp"; |
| 270 |
unlink "$resultdir/$msaname.search1"; |
| 271 |
unlink "$resultdir/$msaname.search"; |
| 272 |
unlink "$resultdir/$msaname.query"; |
| 273 |
unlink "$resultdir/$msaname.sto"; |
| 274 |
unlink "$filtered_db"; |
| 275 |
unlink "$sfetch_in"; |
| 276 |
|
| 277 |
if($do_fetch_models) { |
| 278 |
unlink "$resultdir/$msaname.cm"; |
| 279 |
} |
| 280 |
|
| 281 |
$runtime += time(); |
| 282 |
open(TIME, ">" . "$resultdir/$msaname.time"); |
| 283 |
printf TIME "$msaname $runtime seconds\n"; |
| 284 |
close(TIME); |
| 285 |
} |
| 286 |
close TABLE; |
| 287 |
close OUTFILE; |
| 288 |
close TMPOUTFILE; |
| 289 |
|
| 290 |
# Use 'rmark-idpositives.pl' to identify positives in the temporary output file to |
| 291 |
# create the permanent output file. First, we need to sort by score. |
| 292 |
|
| 293 |
$command = "sort -g $tmpoutfile > $sorttmpoutfile"; |
| 294 |
$status = system("$command"); |
| 295 |
if ($status != 0) { die "FAILED: $command"; } |
| 296 |
|
| 297 |
$command = "perl $idscript $posfile $sorttmpoutfile > $outfile"; |
| 298 |
$status = system("$command"); |
| 299 |
if ($status != 0) { die "FAILED: $command"; } |
| 300 |
|
| 301 |
|
| 302 |
####################################### |
| 303 |
# |
| 304 |
# Below is from Paul's rfam_blast_filter.pl, originating in |
| 305 |
# cp ~/notebook/9_0306_inf_rfamscan_rmark/orig_files/rfam_blast_filter.pl ./ |
| 306 |
# |
| 307 |
# |
| 308 |
# |
| 309 |
###################################################################### |
| 310 |
#wublast2minidb: takes all the blast hits, merges overlaps and creates a nice list of seqs and coordinates |
| 311 |
sub wublast2minidb { |
| 312 |
my $window = shift; |
| 313 |
my $blastfile = shift; |
| 314 |
my $sfetchfile = shift; |
| 315 |
|
| 316 |
my (%seq_list); |
| 317 |
|
| 318 |
open(SFETCH, ">" . $sfetchfile); |
| 319 |
open(BLAST,"$blastfile") || die "cannot open $blastfile\n[$!]"; |
| 320 |
while (my $line = <BLAST>){ |
| 321 |
next if( !defined($line) || $line =~ /^\#/ ); |
| 322 |
|
| 323 |
chomp($line); |
| 324 |
my @bline = split(/\t/,$line); |
| 325 |
|
| 326 |
if(scalar(@bline) == 22 ){ |
| 327 |
my ($name, $strand, $start, $end); |
| 328 |
if ($bline[1] =~ /\S+/){$name = $bline[1];} else {printf STDERR "name error =\'$bline[1]\' in $blastfile \n"}; |
| 329 |
if ($bline[16] =~ /\S+/ && $bline[19] =~ /\S+/){$strand = $bline[16]*$bline[19];} else {printf STDERR "strand error =\'$bline[16]\' and \'$bline[19]\' \n"}; |
| 330 |
if ($bline[20] =~ /\d+/){$start = $bline[20];} else {printf STDERR "sstart error =\'$bline[20]\' in $blastfile \n"}; |
| 331 |
if ($bline[21] =~ /\d+/){$end = $bline[21];} else {printf STDERR "send error =\'$bline[21]\' in $blastfile \n"}; |
| 332 |
my $already = 0; |
| 333 |
|
| 334 |
#print "$name\t$start\t$end\t$strand\n"; |
| 335 |
|
| 336 |
# BLAST HIT |
| 337 |
#-------------------XXXXXXXXXXXXXX---------------------------- |
| 338 |
# |<----------------------| |
| 339 |
# window |
| 340 |
# |---------------------->| |
| 341 |
# window |
| 342 |
|
| 343 |
my $tmp = $end; |
| 344 |
$end = max($start + $window,$end); #This looks strange but is correct. See diagram above. |
| 345 |
$start = min($tmp - $window,$start); #Ditto. |
| 346 |
$start = 1 if( $start < 1 ); |
| 347 |
|
| 348 |
#Merge overlaps: |
| 349 |
if( exists($seq_list{$name}) ) { |
| 350 |
|
| 351 |
foreach my $se ( @{ $seq_list{$name} } ){ |
| 352 |
my $start2 = $se->{'start'}; |
| 353 |
my $end2 = $se->{'end'}; |
| 354 |
|
| 355 |
my $ov = overlap($start, $end, $start2, $end2); |
| 356 |
if ($ov && $strand == $se->{'strand'}){ |
| 357 |
my $mn0 = min($start, $start2); |
| 358 |
my $mn1 = min($end, $end2); |
| 359 |
my $mx0 = max($start, $start2); |
| 360 |
my $mx1 = max($end, $end2); |
| 361 |
|
| 362 |
$se->{'start'} = min($mn0, $mn1); |
| 363 |
$se->{'end'} = max($mx0, $mx1); |
| 364 |
$already = 1; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
if( !$already ) { |
| 370 |
|
| 371 |
push( @{ $seq_list{$name} }, { 'start' => $start, |
| 372 |
'end' => $end, |
| 373 |
'strand' => $strand} ); |
| 374 |
|
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
my $printNum=0; |
| 380 |
#Final paranoid merging of overlaps that may have grown together: |
| 381 |
foreach my $n ( keys %seq_list ){ |
| 382 |
my $iindx=0; |
| 383 |
foreach my $se ( @{ $seq_list{$n} } ){ |
| 384 |
my $start = $se->{'start'}; |
| 385 |
my $end = $se->{'end'}; |
| 386 |
my $strand = $se->{'strand'}; |
| 387 |
my @temp = @{ $seq_list{$n} }; |
| 388 |
for (my $j = $iindx+1; $j < scalar( @{ $seq_list{$n} } ); $j++ ){ |
| 389 |
last if !defined(${ $seq_list{$n} }[$j]); |
| 390 |
my $se2 = ${ $seq_list{$n} }[$j]; |
| 391 |
my $start2 = $se2->{'start'}; |
| 392 |
my $end2 = $se2->{'end'}; |
| 393 |
my $strand2 = $se2->{'strand'}; |
| 394 |
|
| 395 |
my $ov = overlap($start, $end, $start2, $end2); |
| 396 |
if ($ov && $strand == $strand2){ |
| 397 |
my $mn0 = min($start, $start2); |
| 398 |
my $mn1 = min($end, $end2); |
| 399 |
my $mx0 = max($start, $start2); |
| 400 |
my $mx1 = max($end, $end2); |
| 401 |
|
| 402 |
$start = min($mn0, $mn1); |
| 403 |
$end = max($mx0, $mx1); |
| 404 |
${ $seq_list{$n} }[$iindx] = $se; |
| 405 |
splice(@{ $seq_list{$n} }, $j, 1); |
| 406 |
$j--; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
if($strand eq "-1") { # swap |
| 411 |
my $tmp = $start; |
| 412 |
$start = $end; |
| 413 |
$end = $tmp; |
| 414 |
} |
| 415 |
|
| 416 |
printf SFETCH ("%s/%d-%d %d %d %s\n", $n, $start, $end, $start, $end, $n); |
| 417 |
$printNum++; |
| 418 |
$iindx++; #EPN; change from Paul's version, this removes self check for overlaps, which causes |
| 419 |
# half the hits to be removed erroneously in this paranoid checking loop |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
close(SFETCH); |
| 425 |
return $printNum; |
| 426 |
|
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
###################################################################### |
| 432 |
########## |
| 433 |
#make_lcmask_file: takes as input a stockholm formatted filename and an output filename. |
| 434 |
# Writes the sequences from the stockholm file to the output file in |
| 435 |
# fasta format. Gappy columns and low frequency nucleotides are masked. |
| 436 |
# Also, too similar sequences are filtered. |
| 437 |
sub make_lcmask_file { |
| 438 |
my $stk_file = shift; # stockholm file to use |
| 439 |
my $out_file = shift; # output file |
| 440 |
my $list = shift; |
| 441 |
|
| 442 |
if(!defined($out_file)){ |
| 443 |
my @stk_file = split(/\./, $stk_file); |
| 444 |
my $out_file = pop(@stk_file); # |
| 445 |
$out_file = $out_file . ".fa"; |
| 446 |
} |
| 447 |
|
| 448 |
#Read sequences into hashes: |
| 449 |
open( STK, "$stk_file" ) or die ("FATAL: Couldn't open $stk_file \n[$!]"); |
| 450 |
my $seed = read_stockholm( \*STK ); |
| 451 |
close(STK); |
| 452 |
|
| 453 |
my @list = keys %{$seed}; |
| 454 |
|
| 455 |
my $length = length($seed->{$list[0]}); |
| 456 |
my $noseqs = @list; |
| 457 |
my @aofh_nuc_counts; #array of hashes to store nuc-counts. |
| 458 |
|
| 459 |
foreach my $seqname ( @list ) { |
| 460 |
my $seq = uc($seed->{$seqname}); #Must be uppercase,U->T! |
| 461 |
$seq =~ tr/U/T/; |
| 462 |
my @seq = split(//,$seq); |
| 463 |
for (my $i=0; $i<$length; $i++){ |
| 464 |
if (is_nucleotide($seq[$i])){ |
| 465 |
if (defined($aofh_nuc_counts[$i]{$seq[$i]})){ |
| 466 |
$aofh_nuc_counts[$i]{$seq[$i]}++; |
| 467 |
} |
| 468 |
else { |
| 469 |
$aofh_nuc_counts[$i]{$seq[$i]}=1; |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
#Calculate the per site nucleotide frequencies: |
| 476 |
my @fract_nucs; |
| 477 |
for (my $ii=0; $ii<$length; $ii++){ |
| 478 |
$fract_nucs[$ii]=0; |
| 479 |
foreach my $n ( keys %{ $aofh_nuc_counts[$ii] } ) { |
| 480 |
$fract_nucs[$ii]+=$aofh_nuc_counts[$ii]{$n}; |
| 481 |
$aofh_nuc_counts[$ii]{$n} = $aofh_nuc_counts[$ii]{$n}/$noseqs; |
| 482 |
} |
| 483 |
$fract_nucs[$ii]=$fract_nucs[$ii]/$noseqs; |
| 484 |
} |
| 485 |
|
| 486 |
my @accepted_seqs; |
| 487 |
open( OUT, ">$out_file" ) or die ("FATAL: Couldn't open $out_file \n[$!]"); |
| 488 |
foreach my $seqname2 ( @list ) { |
| 489 |
|
| 490 |
my $seq2 = uc($seed->{$seqname2});#Must be uppercase! |
| 491 |
$seq2 =~ tr/U/T/; |
| 492 |
|
| 493 |
my $max_pid = 0; |
| 494 |
foreach my $aseq (@accepted_seqs){ |
| 495 |
my $p = pid($aseq, $seq2); |
| 496 |
if ($max_pid < $p){ |
| 497 |
$max_pid = $p; |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
if ($max_pid<$max_pid_thresh && length($seqname2)>0 && length($seq2)>0){ |
| 502 |
push(@accepted_seqs, $seq2 ); |
| 503 |
my @seq2 = split(//,$seq2); |
| 504 |
printf OUT ">$seqname2\n"; |
| 505 |
for (my $iii=0; $iii<$length; $iii++){ |
| 506 |
if (is_nucleotide($seq2[$iii])){ |
| 507 |
if (defined($aofh_nuc_counts[$iii]{$seq2[$iii]}) && $aofh_nuc_counts[$iii]{$seq2[$iii]}>$nuc_freq_thresh && $fract_nucs[$iii]>$gap_thresh){ |
| 508 |
printf OUT "$seq2[$iii]"; |
| 509 |
} |
| 510 |
else { |
| 511 |
$seq2[$iii] = lc($seq2[$iii]); |
| 512 |
printf OUT "$seq2[$iii]"; |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
printf OUT "\n"; |
| 517 |
} |
| 518 |
} |
| 519 |
close(OUT); |
| 520 |
|
| 521 |
return 1; |
| 522 |
|
| 523 |
} |
| 524 |
|
| 525 |
###################################################################### |
| 526 |
#returns true if input character is a nucleotide (IUPAC codes): |
| 527 |
sub is_nucleotide { |
| 528 |
my $a = shift; |
| 529 |
|
| 530 |
if (defined($a)){ |
| 531 |
$a =~ tr/a-z/A-Z/; |
| 532 |
} |
| 533 |
|
| 534 |
if (defined($a) && length($a) && ($a =~ /[ACGUTRYWSMKBDHVN]/) ){ |
| 535 |
return 1; |
| 536 |
} |
| 537 |
else { |
| 538 |
return 0; |
| 539 |
} |
| 540 |
|
| 541 |
} |
| 542 |
|
| 543 |
###################################################################### |
| 544 |
|
| 545 |
sub read_stockholm { |
| 546 |
my $infile = shift; |
| 547 |
|
| 548 |
my $aln = ""; |
| 549 |
my %accession = (); |
| 550 |
my %align = (); |
| 551 |
my $name = ""; |
| 552 |
my $seq = ""; |
| 553 |
my $count = 0; |
| 554 |
my %seqnames = (); |
| 555 |
my %GFfeatures = (); ##=GF <feature> <Generic per-File annotation, free text> |
| 556 |
my %GCfeatures = (); ##=GC <feature> <Generic per-Column annotation, exactly 1 char per column> |
| 557 |
my %GSfeatures = (); ##=GS <seqname> <feature> <Generic per-Sequence annotation, free text> |
| 558 |
my %GRfeatures = (); ##=GR <seqname> <feature> <Generic per-Sequence AND per-Column markup, exactly 1 char per column> |
| 559 |
|
| 560 |
# open (DF, "<$infile") || die "cant open $infile\n"; |
| 561 |
while(my $entry = <$infile>) { |
| 562 |
# Largely stolen by PPG from the stockholm parser from BioPerl: |
| 563 |
# Double slash (//) signals end of file. The flat Pfam-A data from |
| 564 |
# ftp://ftp.sanger.ac.uk/pub/databases/Pfam/Pfam-A.full.gz consists |
| 565 |
# of several concatenated Stockholm-formatted files. The following |
| 566 |
# line makes it possible to parse it without this module trying to |
| 567 |
# read the whole file into memory. Andreas Kähäri 10/3/2003. |
| 568 |
last if $entry =~ /^\/\//; |
| 569 |
|
| 570 |
if ($entry =~ /^\#=GF\s+(\S+)\s+(\S+)/) { |
| 571 |
$GFfeatures{$1} .= $2; |
| 572 |
next; |
| 573 |
} |
| 574 |
|
| 575 |
if ($entry =~ /^\#=GC\s+(\S+)\s+(\S+)/) { |
| 576 |
$GCfeatures{$1} .= $2; |
| 577 |
next; |
| 578 |
} |
| 579 |
|
| 580 |
if ($entry =~ /^\#=GS\s+(\S+)\s+(\S+)\s+(\S+)/) { |
| 581 |
my $id1 = $1 . ":" . $2; |
| 582 |
$GSfeatures{$id1} .= $3; |
| 583 |
next; |
| 584 |
} |
| 585 |
|
| 586 |
if ($entry =~ /^\#=GF\s+(\S+)\s+(\S+)\s+(\S+)/) { |
| 587 |
my $id2 = $1 . ":" . $2; |
| 588 |
$GRfeatures{$id2} .= $3; |
| 589 |
next; |
| 590 |
} |
| 591 |
|
| 592 |
$entry =~ /^([A-Za-z\.\-]+)$/ && ( $align{$name} .= $1 ) && next; |
| 593 |
$entry !~ /^([^\#]\S+)\s+([A-Za-z\.\-]+)\s*/ && next; |
| 594 |
|
| 595 |
|
| 596 |
$name = $1; |
| 597 |
$seq = $2; |
| 598 |
|
| 599 |
if( ! defined $align{$name} ) { |
| 600 |
$count++; |
| 601 |
$seqnames{$count} = $name; |
| 602 |
|
| 603 |
} |
| 604 |
$align{$name} .= $seq; |
| 605 |
} |
| 606 |
#close(DF); |
| 607 |
|
| 608 |
#Check sequence lengths: |
| 609 |
my $len = length($align{$name}); |
| 610 |
my $lenO = $len; |
| 611 |
my $nameO = $name; |
| 612 |
my $seq_count = 0; |
| 613 |
foreach my $no1 ( sort { $a <=> $b } keys %seqnames ) { |
| 614 |
$name = $seqnames{$no1}; |
| 615 |
$len = length($align{$name}); |
| 616 |
$seq_count++; |
| 617 |
|
| 618 |
if ($len != $lenO){ |
| 619 |
die "Length of $name is $len\nLength of $nameO is $lenO\n"; |
| 620 |
} |
| 621 |
$lenO = $len; |
| 622 |
$nameO = $name; |
| 623 |
} |
| 624 |
|
| 625 |
return \%align; |
| 626 |
} |
| 627 |
|
| 628 |
###################################################################### |
| 629 |
|
| 630 |
#pid: compute the identity between two sequences. |
| 631 |
sub pid { |
| 632 |
my $a = shift; |
| 633 |
my $b = shift; |
| 634 |
|
| 635 |
my @a = split(//, $a); |
| 636 |
my @b = split(//, $b); |
| 637 |
my ($sim, $lena, $lenb) = (0, 0, 0); |
| 638 |
|
| 639 |
if (scalar(@a) != scalar(@b)){ |
| 640 |
return 0; |
| 641 |
} |
| 642 |
else { |
| 643 |
|
| 644 |
for (my $i=0; $i<scalar(@b); $i++){ |
| 645 |
if ( (is_nucleotide($a[$i]) || is_nucleotide($b[$i])) && $a[$i] eq $b[$i] ){ |
| 646 |
$sim++; |
| 647 |
} |
| 648 |
|
| 649 |
if ( is_nucleotide($a[$i]) ){ |
| 650 |
$lena++; |
| 651 |
} |
| 652 |
|
| 653 |
if ( is_nucleotide($b[$i]) ){ |
| 654 |
$lenb++; |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
my $maxlen = max($lena, $lenb); |
| 660 |
if ($maxlen>0){ |
| 661 |
return $sim/$maxlen; |
| 662 |
} |
| 663 |
else { |
| 664 |
return 0; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
sub max { |
| 669 |
my $a = shift; |
| 670 |
my $b = shift; |
| 671 |
if($a > $b) { return $a; } |
| 672 |
else { return $b; } |
| 673 |
} |
| 674 |
|
| 675 |
sub min { |
| 676 |
my $a = shift; |
| 677 |
my $b = shift; |
| 678 |
if($a < $b) { return $a; } |
| 679 |
else { return $b; } |
| 680 |
} |
| 681 |
|
| 682 |
###################################################################### |
| 683 |
sub overlap { |
| 684 |
my($x1, $y1, $x2, $y2) = @_; |
| 685 |
|
| 686 |
if ( ($x1<=$x2 && $x2<=$y1) || ($x1<=$y2 && $y2<=$y1) || ($x2<=$x1 && $x1<=$y2) || ($x2<=$y1 && $y1<=$y2) ){ |
| 687 |
return 1; |
| 688 |
} |
| 689 |
else { |
| 690 |
return 0; |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
#max |
| 695 |
sub max { |
| 696 |
return $_[0] if @_ == 1; |
| 697 |
$_[0] > $_[1] ? $_[0] : $_[1] |
| 698 |
} |
| 699 |
|
| 700 |
#min |
| 701 |
sub min { |
| 702 |
return $_[0] if @_ == 1; |
| 703 |
$_[0] < $_[1] ? $_[0] : $_[1] |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
###################################################################### |