Debian Codegraph @

hmmer/easel/demotic/infernal_tab2gff.pl

raw
1 #!/usr/bin/perl -w -I/groups/eddy/home/jonest/Demotic
2
3 # TAJ 6/23/08 last mod 7/10/08
4 # Purpose: flexibly convert "cmsearch --tabfile TAB.out" output to GFF format
5 # Based upon my "blastn2gff.pl" script.
6 #
7 # OUTPUT GFF2 format:
8 # ------------------
9 # CONTIG METHOD TYPE START END SCORE ORI FRAME GENE "genename"; note "free text"
10 # $contig $method $type $GFFstart $GFFend $score $ori "." $gene_name $note
11 # na "Infernal" "Infernal_hit" na na na na "." ::$cm_name[i] "" # Defaults
12 # na -m -t na na na na na -g -n # Options
13 #
14 # Todo List
15 # -----------------------------------------------------
16 # (1) Allow for GFF3 format output
17 # (2) Possibly all switch to produce separate GFF files for each report
18 # -----------------------------------------------------
19
20 use strict;
21 use demotic_infernal_tab;
22 use Getopt::Std;
23 getopts ('E:s:G:l:m:t:g:n:u:d:b');
24 our ($opt_E, $opt_s, $opt_G, $opt_l, $opt_m, $opt_t, $opt_g, $opt_n, $opt_b, $opt_u, $opt_d);
25
26 (my $script = $0) =~ s/^.*\///;
27 my $USAGE = "
28 Parse cmsearch tabfile output, filter hits on user-supplied cutoffs, and output hits
29 in GFF2 format.
30 ======================================================================================
31 USAGE: $script <options> tabfile.out > foo.gff
32 ======================================================================================
33 tabfile.out ==> Output file created by using cmsearch switch '--tabfile tab.out'
34 from Infernal rc1.0
35
36 OPTIONS
37 --------------------------------------------------------------------------------------
38 -E Eval_cutoff (E)-value cutoff -- reject hits with E-value > Eval_cutoff
39 -s score_cutoff (s)score cutoff -- reject hits with bitscore < score_cutoff
40 -G GC_cutoff (G)C percent cutoff (0..100) -- reject hits with GC < GC_cutoff
41 -l len_cutoff (l)ength cutoff -- reject hits with length < len_cutoff
42 -m method (m)ethod. Default is 'Infernal' # for GFF output
43 -t type (t)ype. Default is 'Infernal_hit' # for GFF output
44 -g gene_name (g)ene name. Default is CM query name # for GFF output (1)
45 -n \"a short note\" (n)ote. No default # for GFF output
46 -u X (u)pstream pad -- add X NTs to BEGINNING of all hits (2)
47 -d Y (d)ownstream pad -- add Y Nts to END of all hits (2)
48 --------------------------------------------------------------------------------------
49
50 NOTES:
51 (1) == Default behavior obtains a (non-unique) 'gene name' from the CM name. For
52 multiple CM queries having the same name, Infernal differentiates the models
53 by adding a '.N' version (eg CM.1, CM.2, etc). Specifying the gene name by '-g'
54 results in the same gene name being used for all hits in all reports contained
55 in that tabfile.
56 (2) == The values always add to the length of the feature. X and Y cannot be negative!
57 It's highly recommended that you use the '-n NOTE' feature to annotate the
58 changes made to the GFF start and end sites due to these flags. Script warns
59 when padding beyond the 5' end of the contig (and sets it to 1), but cannot edge
60 detect for the 3' end of the contig.
61
62 ************* While this script can parse 'tabfile' files with output from
63 * WARNING * multiple queries (possibly containing very different CMs), only one
64 ************* GENE name, METHOD and TYPE will be added to all GFF lines created!
65 ";
66
67 my $CMs = 0; # number of CMs in tabfile
68 my $hits = 0; # number of hits within current CM
69 my $Eval_cutoff = 10000; # arbitrarily large E-value default cutoff
70 my $score_cutoff = -1000; # arbitrarily low bitscore default cutoff
71 my $GC_cutoff = 0; # minimum %GC cutoff
72 my $len_cutoff = 0; # minimum length cutoff
73 my $up_pad = 0; # add X NTs to upstream/start of hit NB: applies to all hits!
74 my $down_pad = 0; # add Y NTs to downstream/end of hit NB: applies to all hits!
75 my $pass_filter = 0;
76
77 # get cutoff & coord padding options (NB: opt's m,t,g,n obtained during sub print_GFF2)
78 $Eval_cutoff = $opt_E if ($opt_E);
79 $score_cutoff = $opt_s if ($opt_s);
80 $GC_cutoff = $opt_G if ($opt_G);
81 $len_cutoff = $opt_l if ($opt_l);
82 $up_pad = $opt_u if ($opt_u);
83 $down_pad = $opt_d if ($opt_d);
84 if ($up_pad !~ /^\+?\d+$/) { die "Illegal pad: '$up_pad'; must be a whole positive number."; }
85 if ($down_pad !~ /^\+?\d+$/) { die "Illegal pad: '$down_pad'; must be a whole positive number."; }
86 if (($up_pad > 100000) || ($down_pad > 100000)) {
87 die "Whoa, whoa! Slow down their Feyman. You're being a bit excessive with your pads. --mgmt";
88 }
89
90
91 # =========================================================
92 # | demotic_infernal_tab -- Parameters |
93 # =========================================================
94 #
95 # Returns SINGLE STRING; applies to entire tabfile (regardless of no. of CMs)
96 # ----------------------------------------------------------------------------
97 # $model_num # number of CM's used in the search # check
98 # $command # command line used to run search # check
99 # $date # date search was run # check
100 # $db_recs # number of records in target DB # check
101 # $db_size # DB size (in MB) # check
102 #
103 # Returns LIST; 0th position in list corresponds to 1st CM in the tabfile
104 # EX: $cm_name[2] # name of the CM query used in 3rd report
105 # ----------------------------------------------------------------------------
106 # @cm_name # Covariation Model(s) name; 1st model is 0th in array # check
107 # @time_expect # expected run time (quoted, not converting into hr:min) # check
108 # @time_actual # actual run time (quoted, not converting into hr:min) # check
109 # @num_hits # tracks number of hits for each model # check
110 #
111 # Returns 2D ARRAY [i][j];
112 # "i" = list of CMs in report (as above); frequently only 1 report, or i=0
113 # "j" = list of _hits_ for each CM (0th -> 1st hit), in order appearing in report
114 # EX: ${$GC[1]}->[3] # GC value of 4th (3+1) hit in the 2nd (1+1) search
115 # ----------------------------------------------------------------------------
116 # @t_name # (2D); target fasta record name # check
117 # @t_start # (2D); start location in fasta target \___ For "-" ori hits, # check
118 # @t_stop # (2D); stop location in fasta target / t_start > t_stop # check
119 # @q_start # (2D); start location in CM query \___ Regardless of ori, # check
120 # @q_stop # (2D); stop location in CM query / q_start < q_stop # check
121 # @bitscore # (2D); bit score # check
122 # @Eval # (2D); E-value // can be in scientific notation # check
123 # @GC # (2D); %GC # check
124 # =========================================================
125
126
127 # Parse infernal 'tabfile'; possibly involving multiple query CMs
128 die $USAGE unless (@ARGV == 1);
129 my $tabfile = shift;
130 open (TABFILE, "$tabfile") || die "Can't open $tabfile. You fuckin' wif me?";
131 &demotic_infernal_tab::parse(\*TABFILE);
132 close TABFILE;
133
134 $CMs = $demotic_infernal_tab::model_num; # number of query CMs
135
136 # For all hits, for all CMs in tabfile report -> print in GFF2 formt if passes all cutoffs
137 foreach my $rep_num (0..$CMs-1) { # looping over i CMs
138 $hits = $demotic_infernal_tab::num_hits[$rep_num];
139 foreach my $hit_num (0..$hits-1) { # looping over j hits from ith CM
140 $pass_filter = &filter_hit($rep_num, $hit_num);
141 if ($pass_filter) {
142 if ($pass_filter) {
143 &print_GFF2 ($rep_num, $hit_num);
144 }
145 else { next; }
146 }
147 }
148 }
149
150
151 ############# Validation -- Make sure I'm parsing hit list for all CMs properly #############
152 if ($opt_b) { # Thouroughly embarassing parse dump, for manual validation // not complete
153 foreach my $rep (0..($CMs-1)) {
154 my $name = $demotic_infernal_tab::cm_name[$rep];
155 $hits = $demotic_infernal_tab::num_hits[$rep];
156 print "------------------------------------------------\n";
157 print "Model \#", ($rep+1), " ==> cm_name: [$name] containing $hits hits\n";
158 print "------------------------------------------------\n";
159 foreach my $j (0..$hits-1) { # looping over all hits for that CM
160 my $contig = $demotic_infernal_tab::t_name[$rep]->[$j];
161 my $start_t = $demotic_infernal_tab::t_start[$rep]->[$j];
162 my $stop_t = $demotic_infernal_tab::t_stop[$rep]->[$j];
163 my $start_q = $demotic_infernal_tab::q_start[$rep]->[$j];
164 my $stop_q = $demotic_infernal_tab::q_stop[$rep]->[$j];
165 my $score = $demotic_infernal_tab::bitscore[$rep]->[$j];
166 my $e_val = $demotic_infernal_tab::Eval[$rep]->[$j];
167 my $gc = $demotic_infernal_tab::GC[$rep]->[$j];
168 print "#", ($j+1), ": ", $contig,
169 " Tar:S-E: ", $start_t, "-", $stop_t,
170 " Que:S-E: ", $start_q, "-", $stop_q,
171 " Score: ", $score,
172 " E-val: ", $e_val,
173 " %GC: ", $gc,
174 "\n";
175 }
176 }
177 }
178
179 ################# Subroutines ######################
180
181 sub filter_hit {
182 # INPUT: Index to _i_th CM in tabfile, _j_th hit for given CM
183 # OUTPUT: 1 if hit passes command line criteria, 0 if it fails the filter
184 my $CM_i = shift;
185 my $hit_j = shift;
186 my $evalue = $demotic_infernal_tab::Eval[$CM_i]->[$hit_j];
187 my $score = $demotic_infernal_tab::bitscore[$CM_i]->[$hit_j];
188 my $gc = $demotic_infernal_tab::GC[$CM_i]->[$hit_j];
189 my $begin = $demotic_infernal_tab::t_start[$CM_i]->[$hit_j];
190 my $end = $demotic_infernal_tab::t_stop[$CM_i]->[$hit_j];
191 my $len = 0;
192 if ($begin < $end ) { $len = $end - $begin + 1; }
193 else { $len = $begin - $end + 1; }
194 if (($evalue <= $Eval_cutoff) &&
195 ($score >= $score_cutoff) &&
196 ($gc >= $GC_cutoff) &&
197 ($len >= $len_cutoff)) {
198 return 1; # passes all cutoff criteria
199 }
200 else {
201 return 0; # does not pass all cutoff criteria
202 }
203 }
204
205 sub print_GFF2 {
206 # INPUT: Index to _i_th CM in tabfile, _j_th hit for given CM
207 # e.g. $demotic_infernal_tab::foo[$i]->[$j]
208 # OUTPUT: Infernal hit in GFF2 format (see following)
209 # contig method type start end score ori frame gene "genename"; note "blah blah"
210 # $ctg $method $type $GFFstart $GFFstop $score $ori $frame $gene $note
211
212 my $CM_i = shift;
213 my $hit_j = shift;
214 my $ctg = $demotic_infernal_tab::t_name[$CM_i]->[$hit_j];
215 my $start = $demotic_infernal_tab::t_start[$CM_i]->[$hit_j];
216 my $stop = $demotic_infernal_tab::t_stop[$CM_i]->[$hit_j];
217 my $score = $demotic_infernal_tab::bitscore[$CM_i]->[$hit_j];
218 my $frame = "."; # used only for CDS
219 my $ori = "";
220 my $gene = "gene \"$demotic_infernal_tab::cm_name[$CM_i]\""; # default
221 my $method = "Infernal"; # default
222 my $type = "Infernal_hit"; # default
223 my $note = ""; # default
224 my $GFFstart = 0;
225 my $GFFstop = 0;
226 my $over = 0;
227 if ($opt_g) { $gene = "gene \"$opt_g\""; } # change default?
228 if ($opt_m) { $method = "$opt_m"; } # change default?
229 if ($opt_t) { $type = "$opt_t"; } # change default?
230 if ($opt_n) { $note = "; note \"$opt_n\""; } # change default?
231 # Note: Unlike blastn, target start/stop dictates ori; while query_start is always <= query_stop.
232 ###########################################
233 # ========>>>>>>>>>>>>>=========
234 # | |
235 # (target) start stop
236 ###########################################
237 if ($start <= $stop ) {
238 $ori = "+";
239 $GFFstart = $start - $up_pad;
240 if ($GFFstart < 1) {
241 $over = ($GFFstart * -1) + 1; # distance beyond 5' end of contig
242 $GFFstart = 1;
243 warn "(${ctg}:${start}-${stop}) couldn't be padded the full $up_pad NTs! START set to 1.";
244 print "\# For following hit; pad overshot $over NTs beyond 5' end of hit; START set to 1\n";
245 }
246 $GFFstop = $stop + $down_pad; # Can't warn against overpadding; I don't know end of ctg!
247 print ("$ctg\t$method\t$type\t$GFFstart\t$GFFstop\t$score\t$ori\t$frame\t${gene}$note\n");
248 }
249 ###########################################
250 # ========<<<<<<<<<<<<<=========
251 # | |
252 # (target) stop start
253 ###########################################
254 elsif ($start > $stop) {
255 $ori = "-";
256 $GFFstart = $stop - $down_pad;
257 if ($GFFstart < 1) {
258 $over = ($GFFstart * -1) + 1; # distance beyond 5' end of contig
259 $GFFstart = 1;
260 warn "(${ctg}:${start}-${stop}) couldn't be padded the full $up_pad NTs! START set to 1.";
261 print "\# For following hit; pad overshot $over NTs beyond 3' end of hit; START set to 1\n";
262 }
263 $GFFstop = $start + $up_pad; # Can't warn against overpadding; I don't know end of ctg!
264 print ("$ctg\t$method\t$type\t$GFFstart\t$GFFstop\t$score\t$ori\t$frame\t${gene}$note\n");
265 }
266 if ($GFFstart > $GFFstop) { # meager error checking...
267 die "Illegal GFF coords: GFFstart ($GFFstart) > GFFstop ($GFFstop)\n!";
268 }
269 }