Debian Codegraph @

src/impl_sse/cmsearch_dual

raw
1 #! /usr/bin/perl -W
2
3 # It is possible, but discouraged, to set additional search options here.
4 #
5 # Avoid using the below three options at all for script compatibility.
6 # -Z is accepted at script command line and passed to both executables,
7 # so it should not be set here.
8 # --tblout for cmsearch is used internally to process results list, and
9 # should not be altered either here or where it is set below.
10 # --glbf for sse_cmsearch is used internally to process results list, and
11 # should not be altered either here or where it is set below.
12
13 $cmsearch_options = "";
14 $sse_cmsearch_options = "";
15
16 ##############################################################################
17 # Do not edit below this divider!
18 ##############################################################################
19
20 # Parse command line
21 use Getopt::Std;
22 $Getopt::Std::STANDARD_HELP_VERSION = TRUE;
23
24 our($opt_P, $opt_Q, $opt_R, $opt_Z);
25 getopts('P:Q:R:Z:') or die;
26
27 if (scalar @ARGV != 2) {
28 die("Incorrect number of command-line arguments; requires CM file and FASTA sequence file.");
29 }
30 $cmfile = $ARGV[0];
31 $fafile = $ARGV[1];
32 #print STDERR $opt_Z?"Z = $opt_Z, ":'',"\$cmfile = $cmfile, \$fafile = $fafile\n";
33
34 # FIXME Check to make sure there's only one CM in cmfile (parsing won't work on multiple-CM files)
35
36 # Run scalar cmsearch
37 $cmsearch = $opt_P?$opt_P:'cmsearch';
38 $tbl_out = $cmfile;
39 $tbl_out =~ s/.*\///;
40 $tbl_out =~ s/cm$//;
41 $tbl_out .= $fafile;
42 $tbl_out =~ s/\..*\//\./;
43 $tbl_out =~ s/fa//;
44 $tbl_out .= "1\.tbl";
45 $logfile = $tbl_out;
46 $logfile =~ s/tbl$/log/;
47 @args = ($cmsearch, $opt_Z?"-Z $opt_Z":'', "--noali", "--tblout $tbl_out", $cmsearch_options, $cmfile, $fafile, "> $logfile");
48 $status = system("@args"); # FIXME check status
49
50 # Read cmsearch results
51 @cms_hit_lines = `grep -v "^#" $tbl_out`;
52 $num_hits = 0;
53 foreach $line (@cms_hit_lines) {
54 # example:
55 # mrum-tRNA.5 - tRNA RF00005 cm 1 71 1 74 + no 1 0.53 0.1 63.6 1.2e-17 ! -
56 # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
57 ($seq,undef,undef,undef,undef,undef,undef,$start,$stop,undef,undef,undef,undef,undef,undef,$e_val,undef,undef) = split ' ', $line;
58 if ($start > $stop ) {
59 $orient = 1;
60 $tmp = $stop;
61 $stop = $start;
62 $start = $tmp;
63 } else {
64 $orient = 0;
65 }
66 $hit[$num_hits]{seq} = $seq;
67 $hit[$num_hits]{start} = $start;
68 $hit[$num_hits]{stop} = $stop;
69 $hit[$num_hits]{orient} = $orient;
70 $hit[$num_hits]{e_val} = $e_val;
71 $num_hits++;
72 }
73
74 # Clean up cmsearch results
75
76 #printf("running sse_cmsearch\n");
77 # Run sse_cmsearch
78 $sse_cmsearch = $opt_Q?$opt_Q:'sse_cmsearch';
79 $logfile =~ s/1\.log/2\.log/;
80 $glbf = $logfile;
81 $glbf =~ s/log$/glbf/;
82 @args = ($sse_cmsearch, $opt_Z?"-Z $opt_Z":'', "--glbf 3", $sse_cmsearch_options, $cmfile, $fafile, "> $glbf", "2> $logfile");
83 $status = system("@args"); # FIXME check status
84
85 # Read sse_cmsearch results
86 @sse_hit_lines = `cat $glbf`;
87 foreach $line (@sse_hit_lines) {
88 chomp($line);
89 ($tmp_h{seq}, $tmp_h{e_val}, $tmp_h{start}, $tmp_h{stop}, $tmp_h{orient}) = split ' ',$line;
90 if ($tmp_h{start} > $tmp_h{stop} ) {
91 $tmp = $tmp_h{stop};
92 $tmp_h{stop} = $tmp_h{start};
93 $tmp_h{start} = $tmp;
94 }
95
96 if (! (main::already_have(\%tmp_h, \@hit))) {
97 $hit[$num_hits]{seq} = $tmp_h{seq};
98 $hit[$num_hits]{start} = $tmp_h{start};
99 $hit[$num_hits]{stop} = $tmp_h{stop};
100 $hit[$num_hits]{orient} = $tmp_h{orient};
101 $hit[$num_hits]{e_val} = $tmp_h{e_val};
102 $num_hits++;
103 }
104 }
105
106 #printf("retrieving\n");
107 # Retrieve sequences
108 $sfetch = $opt_R?$opt_R:'esl-sfetch';
109 $fa2 = $logfile;
110 $fa2 =~ s/2\.log/hits/;
111 if (-e $fa2) { system("rm $fa2"); }
112 unless (-e "$fafile\.ssi") {
113 system("$sfetch --index $fafile");
114 }
115 for $href (@hit) {
116 #@args = ($sfetch, "-c",$href->{start},'\.\.',$href->{stop},$href->{orient}?"-r":"",$fafile,$href->{seq});
117 @args = ($sfetch, "-c",$href->{start}.'\.\.'.$href->{stop}, $fafile, $href->{seq}, ">> $fa2");
118 $status = system("@args"); # FIXME check status
119 }
120
121 #printf("rerunning\n");
122 # Re-run cmsearch (no filters (--max) (could do top strand only but currently don't) on combined hit list,
123 # output sent to stdout
124 @args = ($cmsearch, $opt_Z?"-Z $opt_Z":'', "--max", $cmfile, $fa2);
125 $status = system("@args"); # FIXME check status
126
127 exit;
128
129 #################################################################################
130 # Subroutines
131 #################################################################################
132
133 # Help/usage message
134
135 sub main::HELP_MESSAGE {
136 print "cmsearch_dual - a combination of two pipelines to use both sequence and structural filters\n";
137 print "Usage: cmsearch [options] cmfile seqfile\n";
138 print "\t-Z <x>\tSet database size to <x> megabases, where <x> is a real number\n";
139 print "\t\tRecommended if the seqfile contains only part of the database to be searched\n";
140 print "\t-P <n>\tSpecify path <n> for cmsearch\n";
141 print "\t-Q <n>\tSpecify path <n> for sse_cmsearch\n";
142 print "\t-R <n>\tSpecify path <n> for esl-sfetch (included with Infernal under easel/mini-apps)\n";
143 }
144 # Version message
145 sub main::VERSION_MESSAGE {
146
147 }
148
149 # Check for overlapping or redundant hits
150 sub main::already_have{
151 my ($href, $aref) = @_;
152 foreach $hit (@$aref) {
153 if ($href->{seq} =~ $hit->{seq}) {
154 if ($href->{orient} == $hit->{orient}) {
155 if (($href->{start} <= $hit->{stop}) && ($hit->{start} <= $href->{stop})) {
156 # Hit overlap detected
157 # FIXME Sticks with score/coordinate combo from scalar cmsearch
158 # FIXME We could check scores and pick the better one, but for that
159 # FIXME we would need directly comparable e-values (cmsearch uses
160 # FIXME Inside scores, sse_cmsearch uses CYK scores).
161 return 1;
162 }
163 }
164 }
165 }
166
167 return 0;
168 }