Debian Codegraph @

rmark/x-wublast

raw
1 #! /usr/bin/perl
2
3 # Do a piece of a rmark benchmark, for wu-blast's blastn.
4 #
5 # This script is normally called by rmark-master.pl; its command line
6 # syntax is tied to rmark-master.pl.
7 # x-wublast doesn't use <modeldir>, but it's passed in so that
8 # rmark-master.pl can use a consistent command line structure for
9 # all search programs (cmsearch uses it, for example).
10 #
11 # Usage: x-wublast <execdir> <scriptdir> <modeldir> <resultdir> <optsfile> <tblfile> <msafile> <posfile> <fafile> <outfile>
12 # Example: ./x-wublast /usr/local/blast/bin/ ../rmark/ models testdir blastall-w7.opts test.tbl rmark3.msa rmark3.pos test.fa test.out
13 #
14 # Command-line options:
15 # -P : run a positive-only benchmark, only each family's positive sequences will be searched
16 #
17 # SRE, Tue Apr 20 10:32:49 2010 [Janelia]
18 # SVN $Id$
19 #
20 use Getopt::Std;
21 getopts('P');
22 $do_posonly = 0;
23 if (defined $opt_P) { $do_posonly = 1; }
24
25 $usage = "Usage: x-wublast [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";
26 $options_usage = "Options:\n\t";
27 $options_usage .= " -P : run a positive-only benchmark, only each family's positive sequences will be searched\n\t";
28 if(scalar(@ARGV) != 10) { printf("$usage\n$options_usage"); exit(1); }
29
30 ($execdir, $scriptdir, $modeldir, $resultdir, $optsfile, $tblfile, $msafile, $posfile, $fafile, $outfile) = @ARGV;
31 $tmpoutfile = $outfile . ".tmp";
32 $sorttmpoutfile = $outfile . ".tmp.sort";
33
34 $idscript = "$scriptdir/rmark-idpositives.pl";
35 $blastn = "$execdir/blastn";
36 $afetch = "$execdir/esl-afetch";
37 $sfetch = "$execdir/esl-sfetch";
38 $reformat = "$execdir/esl-reformat";
39 if(! -e $afetch) {
40 $afetch = "$execdir/../easel/miniapps/esl-afetch";
41 if(! -e $afetch) { die "$afetch does not exist, nor $execdir/$afetch"; }
42 }
43 if(! -e $sfetch) {
44 $sfetch = "$execdir/../easel/miniapps/esl-sfetch";
45 if(! -e $sfetch) { die "$sfetch does not exist, nor $execdir/$sfetch"; }
46 }
47 if(! -e $reformat) {
48 $reformat = "$execdir/../easel/miniapps/esl-reformat";
49 if(! -e $reformat) { die "$reformat does not exist"; }
50 }
51 if(! -e $blastn) {
52 $blastn = "/usr/local/wublast/blastn";
53 if(! -e $blastn) { die "$blastn does not exist"; }
54 }
55
56 if (! -d $execdir) { die "didn't find executable directory $execdir"; }
57 if (! -d $scriptdir) { die "didn't find script directory $scriptdir"; }
58 if (! -e $resultdir) { die "$resultdir doesn't exist"; }
59 if (! -e $posfile) { die "$posfile doesn't exist"; }
60 if (! -e $idscript) { die "positive identification script $idscript doesn't exist"; }
61 if (! -e $optsfile) { die "options file $optsfile doesn't exist"; }
62
63 # read options file
64 # first options line is general wublast options, used for all families
65 # all remaining lines are <fam> <fam-specific-options>
66 open(OPTS, $optsfile) || die "couldn't open options file $optsfile";
67 $searchopts = <OPTS>;
68 chomp $searchopts;
69 while($line = <OPTS>) {
70 chomp $line;
71 $fam = $line;
72 $fam =~ s/\s+.+$//;
73 $opts = $line;
74 $opts =~ s/^\S+\s+//;
75 $fam_searchopts_H{$fam} = $opts;
76 }
77 close(OPTS);
78
79 open(OUTFILE,">$outfile") || die "failed to open $outfile";
80 open(TMPOUTFILE,">$tmpoutfile") || die "failed to open $tmpoutfile";
81 open(TABLE, "$tblfile") || die "failed to open $tblfile";
82 $orig_fafile = $fafile;
83 while (<TABLE>)
84 {
85 ($msaname) = split;
86
87 # fetch the msa
88 $command = "$afetch -o $resultdir/$msaname.sto $msafile $msaname > /dev/null";
89 $status = system("$command");
90 if ($status != 0) { die "FAILED: $command"; }
91
92 # generate the query fasta file
93 $command = "$reformat fasta $resultdir/$msaname.sto > $resultdir/$msaname.query";
94 $status = system("$command");
95 if ($status != 0) { die "FAILED: $command"; }
96
97 # run wublast
98 $runtime = -1 * time();
99 $command = "$blastn $fafile $resultdir/$msaname.query cpus=1 $searchopts $fam_searchopts_H{$fam} topcomboN=100000 mformat=3 > $resultdir/$msaname.tmp";
100 $status = system("$command");
101 if ($status != 0) { die "FAILED: $command"; }
102 $runtime += time();
103
104 open(OUTPUT, "$resultdir/$msaname.tmp") || die "FAILED: to open $resultdir/$msaname.tmp tabular output file";
105
106 while (<OUTPUT>)
107 {
108 if (/^\#/) { next; }
109 @fields = split(' ', $_, 23);
110 $query = $fields[0];
111 $target = $fields[1];
112 $evalue = $fields[2];
113 $nhsps = $fields[3];
114 $bitscore = $fields[4];
115 $query_from = $fields[17];
116 $query_to = $fields[18];
117 $target_from = $fields[20];
118 $target_to = $fields[21];
119 $group = $fields[22];
120
121 if($target_from > $target_to) { die "ERROR: WUBLAST output unexpectedly has target_from > target_to"; }
122
123 if($nhsps > 1) {
124 # This hsp is the first of $nhsps that make up the full hit,
125 # read the others, and update $target_from and $target_to.
126 # Once they're all read, $target_from..$target_to should span all hsps.
127 for($h = 2; $h <= $nhsps; $h++) {
128 $line = <OUTPUT>;
129 if ($line =~ /^\#/) { die "ERROR: WUBLAST output unexpected comment line"; }
130 @fields2 = split(' ', $line, 23);
131 $query2 = $fields2[0];
132 $target2 = $fields2[1];
133 $evalue2 = $fields2[2];
134 $nhsps2 = $fields2[3];
135 $bitscore2 = $fields2[4];
136 $query_from2 = $fields2[17];
137 $query_to2 = $fields2[18];
138 $target_from2 = $fields2[20];
139 $target_to2 = $fields2[21];
140 $group2 = $fields2[22];
141 if($target_from2 > $target_to2) { die "ERROR: WUBLAST output unexpectedly has target_from2 > target_to2"; }
142 if(($group2 ne $group) || ($query2 ne $query) || ($target2 ne $target)) {
143 die "ERROR: WUBLAST output is not sorted by 'groups'";
144 }
145 if($target_from2 < $target_from) { $target_from = $target_from2; }
146 if($target_to2 > $target_to) { $target_to = $target_to2; }
147 }
148 }
149
150 # If the query coords are reversed (query_from > query_to) we've got a hit on the other strand
151 if($query_from > $query_to) {
152 $tmp = $target_from;
153 $target_from = $target_to;
154 $target_to = $tmp;
155 }
156
157 # report hit
158 printf TMPOUTFILE "%10g %10.1f %10d %10d %20s %35s\n", $evalue, $bitscore, $target_from, $target_to, $target, $msaname;
159 }
160
161 #unlink "$resultdir/$msaname.tmp";
162 unlink "$resultdir/$msaname.sto";
163 unlink "$resultdir/$msaname.query";
164
165 open(TIME, ">" . "$resultdir/$msaname.time");
166 printf TIME "$msaname $runtime seconds\n";
167 close(TIME);
168
169 }
170 close TABLE;
171 close OUTFILE;
172 close TMPOUTFILE;
173
174 # Use 'rmark-idpositives.pl' to identify positives in the temporary output file to
175 # create the permanent output file
176 # First, we need to sort by score.
177
178 $command = "sort -g $tmpoutfile > $sorttmpoutfile";
179 $status = system("$command");
180 if ($status != 0) { die "FAILED: $command"; }
181
182 $command = "perl $idscript $posfile $sorttmpoutfile > $outfile";
183 $status = system("$command");
184 if ($status != 0) { die "FAILED: $command"; }
185