| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
# Do a piece of a rmark benchmark, for nhmmer-filtered cmsearch. |
| 4 |
# |
| 5 |
# This script is normally called by rmark-master.pl; its command line |
| 6 |
# syntax is tied to rmark-master.pl. |
| 7 |
# x-nhmmer2cmsearch doesn't use the <msafile>, but it's passed in so that |
| 8 |
# rmark-master.pl can use a consistent command line structure for |
| 9 |
# all search programs (BLAST uses it, for example). |
| 10 |
# |
| 11 |
# Usage: x-nhmmer-cmsearch -C <hmmfile> -X <cmfile> <execdir> <scriptdir> <modeldir> <resultdir> <optsfile> <tblfile> <msafile> <posfile> <fafile> <outfile> |
| 12 |
# Example: ./x-nhmmer-cmsearch -C rmark3.hmm -X rmark3.cm ../hmmer/src/ ../rmark/ models testdir h3-df.opts test.tbl rmark3.msa rmark3.pos test.fa test.out |
| 13 |
# |
| 14 |
# Command-line options: |
| 15 |
# -C <f> : REQUIRED; fetch HMMs from existing file <f> |
| 16 |
# -X <f> : REQUIRED; fetch CMs f rom existing file <f> |
| 17 |
# -M <n> : run MPI with <n> <= 8 processors, only valid if --mpi exists in the $optsfile |
| 18 |
# |
| 19 |
# SRE, Tue Apr 20 10:32:49 2010 [Janelia] |
| 20 |
# SVN $Id$ |
| 21 |
# |
| 22 |
use Getopt::Std; |
| 23 |
getopts('M:C:X:'); |
| 24 |
$mpi_nprocs = 8; |
| 25 |
if (defined $opt_C) { $master_hmm = $opt_C; } |
| 26 |
else { die "ERROR -C is required."; } |
| 27 |
if (defined $opt_X) { $master_cm = $opt_X; } |
| 28 |
else { die "ERROR -X is required."; } |
| 29 |
if (defined $opt_M) { |
| 30 |
$mpi_nprocs = $opt_M; |
| 31 |
if($mpi_nprocs < 2 || $mpi_nprocs > 8) { die "ERROR, with -M <n>, <n> must be between 2 and 8"; } |
| 32 |
} |
| 33 |
|
| 34 |
$usage = "Usage: x-nhmmer2cmsearch [options]\n\t<execdir>\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"; |
| 35 |
$options_usage = "Options:\n\t"; |
| 36 |
$options_usage .= " -C <f> : REQUIRED; fetch HMMs from <f>\n\t"; |
| 37 |
$options_usage .= " -X <f> : REQUIRED; fetch CMs from <f>\n\t"; |
| 38 |
$options_usage .= " -M <n> : run MPI with <n> <= 8 processors, only valid if --mpi exists in the <optsfile>\n\n"; |
| 39 |
|
| 40 |
if(scalar(@ARGV) != 10) { printf("$usage\n$options_usage"); exit(1); } |
| 41 |
|
| 42 |
($execdir, $scriptdir, $modeldir, $resultdir, $optsfile, $tblfile, $msafile, $posfile, $fafile, $outfile) = @ARGV; |
| 43 |
$tmpoutfile = $outfile . ".tmp"; |
| 44 |
$sorttmpoutfile = $outfile . ".tmp.sort"; |
| 45 |
|
| 46 |
$idscript = "$scriptdir/rmark-idpositives.pl"; |
| 47 |
$nhmmer = "$execdir/nhmmer"; |
| 48 |
$cmsearch = "$execdir/cmsearch"; |
| 49 |
$cmfetch = "$execdir/cmfetch"; |
| 50 |
$hmmfetch = "$execdir/hmmfetch"; |
| 51 |
$cmstat = "$execdir/cmstat"; |
| 52 |
$sfetch = "$execdir/esl-sfetch"; |
| 53 |
$seqstat = "$execdir/esl-seqstat"; |
| 54 |
|
| 55 |
if (! -d $execdir) { die "didn't find executable directory $execdir"; } |
| 56 |
if (! -d $scriptdir) { die "didn't find script directory $scriptdir"; } |
| 57 |
if (! -x $cmsearch) { die "didn't find executable $cmsearch"; } |
| 58 |
if (! -x $cmstat) { die "didn't find executable $cmstat"; } |
| 59 |
if (! -x $nhmmer) { die "didn't find executable $nhmmer"; } |
| 60 |
if (! -e $resultdir) { die "$resultdir doesn't exist"; } |
| 61 |
if (! -e $posfile) { die "$posfile doesn't exist"; } |
| 62 |
if (! -e $idscript) { die "positive identification script $idscript doesn't exist"; } |
| 63 |
if (! -e $optsfile) { die "options file $optsfile doesn't exist"; } |
| 64 |
if (! -e $cmfetch) { die "$cmfetch does not exist"; } |
| 65 |
if (! -e $hmmfetch) { die "$hmmfetch does not exist"; } |
| 66 |
if (! -e $master_cm) { die "master CM file $master_cm does not exist"; } |
| 67 |
if (! -e $master_hmm) { die "master HMM file $master_hmm does not exist"; } |
| 68 |
|
| 69 |
if(! -e $sfetch) { |
| 70 |
$sfetch = "$execdir/../easel/miniapps/esl-sfetch"; |
| 71 |
if(! -e $sfetch) { die "$sfetch does not exist, nor $execdir/$sfetch"; } |
| 72 |
} |
| 73 |
if(! -e $seqstat) { |
| 74 |
$seqstat = "$execdir/../easel/miniapps/esl-seqstat"; |
| 75 |
if(! -e $seqstat) { die "$seqstat does not exist"; } |
| 76 |
} |
| 77 |
|
| 78 |
# read options file, determine if we're using mpi or not |
| 79 |
# first options line is for nhmmer, second is for cmsearch |
| 80 |
# all remaining lines are <fam> <fam-specific-options> |
| 81 |
$do_mpi = 0; |
| 82 |
open(OPTS, $optsfile) || die "couldn't open options file $optsfile"; |
| 83 |
$nhmmer_searchopts = <OPTS>; |
| 84 |
$cmsearch_searchopts = <OPTS>; |
| 85 |
if($cmsearch_searchopts =~ m/\-\-mpi/) { $do_mpi = 1; } |
| 86 |
while($line = <OPTS>) { |
| 87 |
chomp $line; |
| 88 |
$fam = $line; |
| 89 |
$fam =~ s/\s+.+$//; |
| 90 |
$opts = $line; |
| 91 |
$opts =~ s/^\S+\s+//; |
| 92 |
$fam_searchopts_H{$fam} = $opts; |
| 93 |
} |
| 94 |
close(OPTS); |
| 95 |
chomp $nhmmer_searchopts; |
| 96 |
chomp $cmsearch_searchopts; |
| 97 |
|
| 98 |
# index the fafile if nec |
| 99 |
if(! -e "$fafile.ssi") { |
| 100 |
$status = system("$sfetch --index $fafile"); |
| 101 |
if ($status != 0) { die "FAILED: $sfetch --index $fafile"; } |
| 102 |
} |
| 103 |
|
| 104 |
# get lengths of all seqs in $fafile, so we can edit |
| 105 |
# the sfetch input file so it doesn't contain invalid coords |
| 106 |
# greater than the length of the seqs |
| 107 |
$output = `$seqstat -a $fafile`; |
| 108 |
%falensH = (); |
| 109 |
@lines = split(/\n/, $output); |
| 110 |
foreach $line (@lines) { |
| 111 |
chomp $line; |
| 112 |
if($line =~ /^\=\s+(\S+)\s+(\d+)/) { |
| 113 |
$falensH{$1} = $2; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
$modeldir = $resultdir; # we'll fetch the models and read them from the $resultdir/ |
| 118 |
|
| 119 |
open(OUTFILE,">$outfile") || die "failed to open $outfile"; |
| 120 |
open(TABLE, "$tblfile") || die "failed to open $tblfile"; |
| 121 |
open(TMPOUTFILE,">$tmpoutfile") || die "failed to open $tmpoutfile"; |
| 122 |
while (<TABLE>) |
| 123 |
{ |
| 124 |
($msaname) = split; |
| 125 |
|
| 126 |
$command = "$hmmfetch -o $resultdir/$msaname.hmm $master_hmm $msaname > /dev/null"; |
| 127 |
$status = system("$command"); |
| 128 |
if ($status != 0) { die "FAILED: $command"; } |
| 129 |
|
| 130 |
$command = "$cmfetch -o $resultdir/$msaname.cm $master_cm $msaname > /dev/null"; |
| 131 |
$status = system("$command"); |
| 132 |
if ($status != 0) { die "FAILED: $command"; } |
| 133 |
|
| 134 |
# get W of CM from cmstat |
| 135 |
$cmstat_output = `$cmstat $resultdir/$msaname.cm`; |
| 136 |
@lines = split(/\n/, $cmstat_output); |
| 137 |
$W = ""; |
| 138 |
foreach $line (@lines) { |
| 139 |
if($line !~ /^\#/) { |
| 140 |
chomp $line; |
| 141 |
$line =~ s/^\s+//; |
| 142 |
@fields = split(/\s+/, $line); |
| 143 |
$W = $fields[7]; |
| 144 |
} |
| 145 |
} |
| 146 |
if($W eq "") { die "ERROR, failed to get W using cmstat for $msaname"; } |
| 147 |
|
| 148 |
$sfetch_in = "$resultdir/$msaname.sfetch"; |
| 149 |
open(SFETCHIN,">$sfetch_in") || die "failed to open $sfetch_in"; |
| 150 |
|
| 151 |
# run nhmmer first |
| 152 |
$status = system("$nhmmer $nhmmer_searchopts $fam_searchopts_H{$msaname} --tblout $resultdir/$msaname.tmp $modeldir/$msaname.hmm $fafile > $resultdir/$msaname.search1"); |
| 153 |
if ($status != 0) { die "FAILED: $nhmmer $nhmmer_searchopts $fam_searchopts_H{$msaname} --tblout $resultdir/$msaname.tmp $resultdir/$msaname.hmm $fafile > $resultdir/$msaname.search1"; } |
| 154 |
|
| 155 |
# create a filtered database of only the hits reported by nhmmer using esl-sfetch to fetch sequences |
| 156 |
$at_least_one_hit_survived_the_filter = 0; |
| 157 |
open(OUTPUT, "$resultdir/$msaname.tmp") || die "FAILED: to open $resultdir/$msaname.tmp tabular output file"; |
| 158 |
while (<OUTPUT>) |
| 159 |
{ |
| 160 |
if (/^\#/) { next; } |
| 161 |
@fields = split(' ', $_, 12); |
| 162 |
$target = $fields[0]; |
| 163 |
$target_from = $fields[6]; |
| 164 |
$target_to = $fields[7]; |
| 165 |
$pval = $fields[8]; |
| 166 |
$bitscore = $fields[9]; |
| 167 |
|
| 168 |
# pad out hit to j-W+1..i+W-1 |
| 169 |
$reversed = 0; |
| 170 |
if($target_from > $target_to) { # reverse direction |
| 171 |
$reversed = 1; |
| 172 |
} |
| 173 |
if($reversed) { #swap |
| 174 |
$tmp = $target_from; $target_from = $target_to; $target_to = $tmp; |
| 175 |
} |
| 176 |
# pad |
| 177 |
$tmp = $target_from; |
| 178 |
$target_from = $target_to - $W + 1; |
| 179 |
$target_to = $tmp + $W - 1; |
| 180 |
# check edge cases |
| 181 |
if($target_from < 1) { $target_from = 1; } |
| 182 |
if($target_to > $falensH{$target}) { $target_to = $falensH{$target}; } |
| 183 |
|
| 184 |
if($reversed) { #swap back |
| 185 |
$tmp = $target_from; $target_from = $target_to; $target_to = $tmp; |
| 186 |
} |
| 187 |
printf SFETCHIN ("%s/%d-%d %d %d %s\n", $target, $target_from, $target_to, $target_from, $target_to, $target); |
| 188 |
$at_least_one_hit_survived_the_filter = 1; |
| 189 |
} |
| 190 |
close(SFETCHIN); |
| 191 |
|
| 192 |
if($at_least_one_hit_survived_the_filter) { |
| 193 |
# extract sequences into filtered database |
| 194 |
$filtered_db = "$resultdir/$msaname.sub.fa"; |
| 195 |
if(! -e "$fafile.ssi") { |
| 196 |
$status = system("$sfetch --index $fafile"); |
| 197 |
if ($status != 0) { die "FAILED: $sfetch --index $fafile"; } |
| 198 |
} |
| 199 |
$status = system("$sfetch -Cf $fafile $sfetch_in > $filtered_db"); |
| 200 |
if ($status != 0) { die "FAILED: $sfetch -Cf $fafile $sfetch_in > $filtered_db"; } |
| 201 |
|
| 202 |
# run cmsearch on filtered_db |
| 203 |
if($do_mpi) { |
| 204 |
$command = "mpirun -np $mpi_nprocs $cmsearch --toponly $cmsearch_searchopts --tabfile $resultdir/$msaname.tmp $modeldir/$msaname.cm $filtered_db > $resultdir/$msaname.search"; |
| 205 |
$status = system("$command"); |
| 206 |
if ($status != 0) { die "FAILED: $command"; } |
| 207 |
} |
| 208 |
else { |
| 209 |
$command = "$cmsearch --toponly $cmsearch_searchopts --tabfile $resultdir/$msaname.tmp $modeldir/$msaname.cm $filtered_db > $resultdir/$msaname.search"; |
| 210 |
$status = system("$command"); |
| 211 |
if ($status != 0) { die "FAILED: $command"; } |
| 212 |
} |
| 213 |
|
| 214 |
# parse output, being careful to add positions correctly to get original coordinates in $fafile |
| 215 |
open(OUTPUT, "$resultdir/$msaname.tmp") || die "FAILED: to open $resultdir/$msaname.tmp tabular output file"; |
| 216 |
while (<OUTPUT>) |
| 217 |
{ |
| 218 |
if (/^\#/) { next; } |
| 219 |
@fields = split(' ', $_, 9); |
| 220 |
$target = $fields[1]; |
| 221 |
$target_from = $fields[2]; |
| 222 |
$target_to = $fields[3]; |
| 223 |
$bitscore = $fields[6]; |
| 224 |
$pval = $fields[7]; |
| 225 |
|
| 226 |
# $target includes number of positions to add/subtract from $target_from/$target_to to get |
| 227 |
# coordinates in original $fafile. |
| 228 |
# Example: |
| 229 |
|
| 230 |
$offset_range = $target; |
| 231 |
$target =~ s/\/.+$//; # remove everything after first "/" |
| 232 |
$offset_range =~ s/^.+\///; # remove everything up to final "/" |
| 233 |
($offset1, $offset2) = split("-", $offset_range); |
| 234 |
if($offset1 < $offset2) { # original hit from nhmmer filter was on forward strand |
| 235 |
$target_from += ($offset1-1); |
| 236 |
$target_to += ($offset1-1); |
| 237 |
} |
| 238 |
else { # original hit from nhmmer was on reverse strand |
| 239 |
$target_from = $offset1 - $target_from + 1; |
| 240 |
$target_to = $offset1 - $target_to + 1; |
| 241 |
} |
| 242 |
|
| 243 |
printf TMPOUTFILE "%10g %10.1f %10d %10d %20s %35s\n", $pval, $bitscore, $target_from, $target_to, $target, $msaname; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
unlink "$resultdir/$msaname.tmp"; |
| 248 |
unlink "$filtered_db"; |
| 249 |
#unlink "$sfetch_in"; |
| 250 |
if($at_least_one_hit_survived_the_filter) { |
| 251 |
system("grep \"CPU time\" $resultdir/$msaname.search1 > $resultdir/$msaname.time"); |
| 252 |
system("grep \"CPU time\" $resultdir/$msaname.search >> $resultdir/$msaname.time"); |
| 253 |
sleep(0.1); |
| 254 |
unlink "$resultdir/$msaname.search"; |
| 255 |
} |
| 256 |
#unlink "$resultdir/$msaname.search1"; |
| 257 |
unlink "$resultdir/$msaname.cm"; |
| 258 |
unlink "$resultdir/$msaname.hmm"; |
| 259 |
} |
| 260 |
close TABLE; |
| 261 |
close OUTFILE; |
| 262 |
close TMPOUTFILE; |
| 263 |
|
| 264 |
# Use 'rmark-idpositives.pl' to identify positives in the temporary output file to |
| 265 |
# create the permanent output file. First, we need to sort by score. |
| 266 |
|
| 267 |
$command = "sort -g $tmpoutfile > $sorttmpoutfile"; |
| 268 |
$status = system("$command"); |
| 269 |
if ($status != 0) { die "FAILED: $command"; } |
| 270 |
|
| 271 |
$command = "perl $idscript $posfile $sorttmpoutfile > $outfile"; |
| 272 |
$status = system("$command"); |
| 273 |
if ($status != 0) { die "FAILED: $command"; } |