Debian Codegraph @

easel/demotic/demotic_infernal_tab.pm

raw
1 ############################################################################
2 # demotic_infernal_tab package
3 # Parses "tabfile" output generated by cmsearch rc1.0
4 # (TAJ 6/2008) based on demotic_search3.pm; itself based on SRE demotic_wublast.pm
5 ############################################################################
6
7 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
8 # TODO List:
9 # (1) Parse "Post-search" number of hits (expected, actual) and surv fraction (expected, actual)
10 # (2) Possibly offload gff output functionality from calling script to this pm.
11 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
12
13 # Example output line (ignoring the leading "#"):
14 # ////////////////////////////////////////////////////////////////////////////////////////////
15 ## target name start stop start stop bit sc E-value GC%
16 ## ----------------------- ---------- ---------- ----- ----- -------- -------- ---
17 # Contig10/506498-506590 1 65 1 80 35.79 1.00e-08 40
18 # ////////////////////////////////////////////////////////////////////////////////////////////
19 #
20 # Example "post_search" content:
21 # ////////////////////////////////////////////////////////////////////////////////////////////
22 # Post-search info for CM 1: SmY
23 #
24 # number of hits surv fraction
25 # ------------------- -----------------
26 # rnd mod alg cfg beta expected actual expected actual
27 # --- --- --- --- ----- ---------- ------- -------- -------
28 # 1 cm ins glc 1e-15 1.000 15 0.0601 0.7829
29 #
30 # expected time actual time
31 # ------------- -------------
32 # 00:00:01.04 00:00:06.00
33 # ////////////////////////////////////////////////////////////////////////////////////////////
34
35
36 package demotic_infernal_tab;
37 # Don't have "parse(\*STDIN)" option, since "tabfile" output always written to file
38
39 sub parse (*) {
40 my $fh = shift; # if this isn't a file handle to a tabfile, some ass needs kickin'
41 my $parsing_header = 1;
42 my $parsing_presearch = 0;
43 my $parsing_hits = 0;
44 my $post_search = 0;
45 my $nhits = 0; # temp counter for number of hits in current model; (tabulated)
46 my $index = 0; # indexes CM number; ie 1st model is index 0
47
48 ### NOTE:
49 ### Some arrays (0..(N-1)) contain values from each search (1..N). Recall there is 1 search per model.
50 ### Ex: @cm_name # lists names of all CMs used, say: (SmY.1, SmY.2, SmY.3)
51 ###
52 ### Other arrays are a "list of lists" (see LoL note below). In these cases, the Nth position
53 ### in the array, will be an anonymous reference to a list of features for _all_ hits to model (N+1).
54 ###
55 ### Ex: @t_name # position N in the list corresponds to one model (N+1) in the search,
56 ### # which is an anonymous ref to a list of ctgs for all hits from that model.
57 ### Ex: @{$t_name[1]} # lists ctgs for all hits to the 2nd query model; eg (ChrI, ChrI, ChrIII, ChrV)
58
59
60 # Document all the variables available in the package; initialize everything
61 #
62 # =========== Header details (once/report) ================================
63 $command = ""; # command line used to run search
64 $date = ""; # date search was run
65 $db_recs = ""; # number of records in target DB
66 $db_size = ""; # DB size (in MB)
67
68 # =========== Pre-search details (once/CM) =================================
69 # Note: details of the "pre-search" filtering rounds not captured currently
70 @cm_name = (); # Covariation Model(s) name; 1st model is 0th in array
71
72 # =========== Details from each *hit* (0-many/CM) ==========================
73 ########################################################################################################
74 # NOTE: (LoL) == List of Lists; the (N)th position in the following arrays contains a list #
75 # of that feature for all hits from the (N+1)th CM in the report. The order of #
76 # features in the anonymous array @{$foo[$i]} corresponds to the order in the report. #
77 # EXAMPLE: #
78 # @GC # list of anonymous refs, one per CM; refs list all GCs in search #
79 # @{$GC[0]} # contains the list of GC values for all hits to the 1st CM #
80 # ${$GC[1]}->[3] # GC value of 4th (3+1) hit in the 2nd (1+1) search #
81 ########################################################################################################
82 @t_name = (); # (LoL); target fasta record name
83 @t_start = (); # (LoL); start location in fasta target
84 @t_stop = (); # (LoL); stop location in fasta target
85 @q_start = (); # (LoL); start location in CM query
86 @q_stop = (); # (LoL); stop location in CM query
87 @bitscore = (); # (LoL); bit score
88 @Eval = (); # (LoL); E-value // can be in scientific notation
89 @GC = (); # (LoL); %GC
90
91 # ===================== "Post-search info" (each CM report) ==================
92 # NOTE: I'm not capturing all available information here, because I believe it can
93 # be quite variable. I'm not expert on all possible output that can be generated
94 @time_expect = (); # expected run time (quoted, not converting into hr:min)
95 @time_actual = (); # actual run time (quoted, not converting into hr:min)
96 @num_hits = (); # tracks number of hits for each model
97
98 # ====================== Cumulative Data (all query outputs) ================
99 $model_num = 0; # number of CM's used in the search
100
101
102 # Loop over tabfile input, and parse it (there can be >1 CM/report in each tabfile)
103 while (<$fh>) {
104 if ($parsing_header) { # only one header section, even if multiple CM's in search
105 if (/^\#\sPre\-search\sinfo\sfor\sCM/) { # NB: model name here is picked up later
106 $parsing_header = 0;
107 $parsing_presearch = 1;
108 next;
109 }
110 elsif (/^\#\scommand\:\s+(\S.+)$/) {
111 $command = $1;
112 }
113 elsif (/^\#\sdate:\s+(\S.+)$/) {
114 $date = $1;
115 }
116 elsif (/^\#\snum\sseqs:\s+(\d+)/) { # could get fucked if commas appear in the number
117 $db_recs = $1;
118 }
119 elsif (/^\#\sdbsize\S+:\s+(\S+)/) { # $1 has a decimal; \d just don't cut it
120 $db_size = $1;
121 }
122 }
123 elsif ($parsing_presearch) { # Each CM has it's own "pre-search"
124 if (/^\#\starget\sname/) { # signals end of "Pre-search"; occurs once per CM
125 $parsing_presearch = 0;
126 $parsing_hits = 1;
127 <$fh>; # Discard next line (composed of dashes); then hitlist begins
128 next;
129 }
130 elsif (/^\#\sCM:\s(\S+)/) { # If multiple models/CM; format is "CM-NAME.n" (1..n)
131 $model_num++; # NB: correctly tallys models; use $index for array indices!
132 push (@cm_name, $1);
133 }
134 }
135 elsif ($parsing_hits) { # every line not starting with "#" should be a "hit"
136 # Example hit [sans leading "#"]:
137 # Contig10 1001 1065 1 80 35.79 1.00e-08 40
138 if (/^\s*(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\d+)\s*$/) {
139 push (@{$t_name[$index]} , $1);
140 push (@{$t_start[$index]} , $2);
141 push (@{$t_stop[$index]} , $3);
142 push (@{$q_start[$index]} , $4);
143 push (@{$q_stop[$index]} , $5);
144 push (@{$bitscore[$index]}, $6);
145 push (@{$Eval[$index]} , $7);
146 push (@{$GC[$index]} , $8);
147 $nhits++;
148 }
149 elsif (/^\#/) { # end of "hitlist" for this CM
150 $parsing_hits = 0;
151 $post_search = 1;
152 push (@num_hits, $nhits);
153 $nhits = 0; # clear hit counter for next model
154 next;
155 }
156 else { die "Parsing 'hitlist' -- should never get here! Current line is:\n$_"; }
157 }
158 elsif ($post_search) { # Each CM has it's own "post-search"
159 if (/^\#\sexpected\stime/) { # Effectively marking the end of a single query's report
160 <$fh>; # discard next line of dashes
161 $_ = <$fh>; # get next line listing the times
162 if (/^\#\s+(\S+)\s+(\S+)\s*$/) {
163 push (@time_expect, $1); # \__ not going to try parsing hours/minutes/seconds
164 push (@time_actual, $2); # /
165 $post_search = 0; # \__ if looping over multiple query CMs, next up is
166 $parsing_presearch = 1; # / "pre-search" for next model, or soon: EOF
167 $index++; # LoL index; end of report i, prepare for report i+1
168 }
169 }
170 }
171 else { die "Should never get here! Dropped through WHILE loop, parsing line:\n$_"; }
172 } # this closes the loop over lines in the input stream.
173 }
174
175 1;
176 __END__
177