Debian Codegraph @

hmmer/easel/demotic/demotic_hmmer.pm

raw
1 # Demotic parser for HMMER3 output
2 #
3 # Works for phmmer, hmmsearch output
4 # Does not parse env coords nor alignments yet,
5 # but is suitable for use in profmark.
6 #
7 # SRE, Tue Apr 22 13:13:51 2008
8
9 package demotic_hmmer;
10
11 # parse(\*STDIN) would parse HMMER output
12 # coming in via stdin.
13 #
14 sub parse (*) {
15 my $fh = shift;
16 my $parsing_header = 1;
17 my $parsing_seqs = 0;
18 my $parsing_domains = 0;
19 my $parsing_alis = 0;
20
21 # Initialize everything... so we can call the parser
22 # repeatedly, one call per hmmsearch output.
23 #
24 # This section also documents what's available in
25 # variables within the package.
26 #
27 # Arrays are indexed [0..nhits-1] or [0..nali-1]
28 #
29 $queryname = ""; # Name of the query sequence
30 $querydesc = ""; # Description line for the query (or "")
31 $querylen = 0; # Length of the query in residues
32 $db = ""; # Name of the target database file
33 $db_nseq = 0; # Number of sequences in the target database
34 $db_nletters = ""; # Number of residues in the target database
35 # (stored as a string so we can have large #'s)
36
37 # The top hit list (still in rank order)
38 $nhits = 0; # Number of entries in the hit list
39 @hit_target = (); # Target sequence name (by rank)
40 %target_desc = (); # Target sequence description (by targ name)
41 @hit_bitscore = (); # Raw score (by rank)
42 @hit_Eval = (); # P-val or E-val (by rank)
43
44 # The alignment output (in order it came in)
45 # all indexed by # of alignment [0..nali-1]
46 $nali = 0; # Number of alignments
47 @ali_target = (); # Target sequence name
48 @ali_bitscore = (); # bit score
49 @ali_evalue = (); # E-value
50 @ali_qstart = (); # Start position on query
51 @ali_qend = (); # End position on query
52 @ali_tstart = (); # Start position on target
53 @ali_tend = (); # End position on target
54 @ali_qali = (); # Aligned string from query
55 @ali_tali = (); # Aligned string from target (subject)
56 @ali_hitidx = (); # index of hit
57
58 # Now, loop over the lines of our input, and parse 'em.
59 #
60 while (<$fh>) {
61 if ($parsing_header) {
62 if (/^Scores for complete sequences /) { # seq section starts with this line
63 $parsing_header = 0;
64 $parsing_seqs = 1;
65 <$fh>; # This discards the next line: --- full sequence ---- ---- single best dom ----
66 <$fh>; # and discard another line: E-value score bias score bias
67 <$fh>; # and discard another line: ------- ------- ------ ------- ------ ----------
68 next;
69 }
70 elsif (/Query:\s*(\S+)\s*\[[LM]=(\d+)\]\s*$/) {
71 $queryname = $1;
72 $querylen = $2;
73 }
74 elsif (/Description:\s*(.*)$/) {
75 $querydesc = $1;
76 }
77 }
78
79 elsif ($parsing_seqs) {
80 if (/^\s*$/) { # seq section ends with a blank line
81 $parsing_seqs = 0;
82 $parsing_domains = 1;
83 next;
84 } elsif (/^\s*(\S+)\s+(\S+)\s+(\S+)\s+\S+\s+\S+\s+\S+\s+(\S+)\s+(\d+)\s+(\S+)\s+(.+)$/) {
85 # evalue score bias evalue score bias exp N target desc
86 # 1 2 3 4 5 6 7
87 $hit_target[$nhits] = $6;
88 $target_desc{$6} = $7;
89 $hit_bitscore[$nhits] = $2;
90 $hit_Eval[$nhits] = $1;
91 $nhits++;
92 }
93 }
94
95 elsif ($parsing_domains) {
96 if (/^\s*$/) { }
97
98 elsif (/^\s*Domain annotation/) { }
99
100 elsif (/^\>\>\s*(\S+)\s*/) { $curr_ali_target = $1; }
101
102 elsif (/^\s*\d+\s+\S\s+(\S+)\s+\S+\s+(\S+)\s+\S+\s+(\d+)\s+(\d+)\s+\S\S\s+(\d+)\s+(\d+)\s+\S\S\s+(\d+)\s+(\d+)\s+\S\S\s+\S+\s*$/)
103 # # score bias c-Evalue i-Evalue hmmfrom hmm to alifrom ali to envfrom env to acc
104 # --- ------ ----- --------- --------- ------- ------- ------- ------- ------- ------- ----
105 # 1 ! 387.6 1.4 7.5e-120 2.1e-115 1 181 [] 1 181 [] 1 181 [] 1.00
106 {
107 $ali_target[$nali] = $curr_ali_target;
108 $ali_bitscore[$nali] = $1;
109 $ali_evalue[$nali] = $2;
110 $ali_qstart[$nali] = $3;
111 $ali_qend[$nali] = $4;
112 $ali_tstart[$nali] = $5;
113 $ali_tend[$nali] = $6;
114 $ali_hitidx[$nali] = $nhits-1;
115 $nali ++;
116 }
117 elsif (/^\s+Alignments for each domain/) {
118 $parsing_domains = 0;
119 $parsing_alis = 1;
120 }
121 }
122
123 elsif ($parsing_alis) {
124 if (/^\s*$/) { }
125
126 elsif (/^\s*\=\=\s+domain\s+(\d+)\s+/) {
127 $whichali = $1-1;
128 }
129 elsif (/^\s+$queryname\s+\d+\s+(\S+)\s+\d+\s*$/) {
130 $ali_qali[$whichali] .= $1;
131 }
132 elsif (/^\s+$ali_target[$whichali]\s+\d+\s+(\S+)\s+\d+\s*$/) {
133 $ali_tali[$whichali] .= $1;
134 }
135 elsif (/\/\//) { return 1; } # normal return after each query.
136 else { next; }
137 }
138 }
139
140 if ($parsing_alis) { return 1; } else { return 0; }
141 }
142
143 sub exblxout {
144 my $ofh = shift;
145 my $i;
146
147 for ($i = 0; $i <= $nali-1; $i++) {
148 printf $ofh "%s\t%d\t%d\t%d\t%s\t%d\t%d\t%s\n",
149 $ali_evalue[$i],
150 $ali_identity[$i],
151 $ali_tstart[$i],
152 $ali_tend[$i],
153 $ali_target[$i],
154 $ali_qstart[$i],
155 $ali_qend[$i],
156 $queryname;
157 }
158
159 }
160
161
162 sub tblout {
163 my $ofh = shift;
164 my $i;
165
166 for ($i = 0; $i <= $nali-1; $i++) {
167 printf $ofh "%s\t%d\t%d\t%d\t%s\t%d\t%d\t%s\t%s\n",
168 $ali_evalue[$i],
169 $ali_identity[$i],
170 $ali_qstart[$i],
171 $ali_qend[$i],
172 $queryname,
173 $ali_tstart[$i],
174 $ali_tend[$i],
175 $ali_target[$i],
176 $target_desc{$ali_target[$i]};
177 }
178 }
179
180
181 sub gffout {
182 my $ofh = shift;
183 my $source = shift;
184 my $feature = shift;
185 my $i;
186 my $strand;
187
188 for ($i = 0; $i <= $nali-1; $i++) {
189 if ($ali_qstart[$i] > $ali_qend[$i]) { $strand = "-"; }
190 else { $strand = "+"; }
191
192 printf $ofh "%s\t%s\t%s\t%d\t%d\t%.1f\t%s\t.\tgene \"%s\"\n",
193 $ali_target[$i],
194 $source,
195 $feature,
196 $ali_tstart[$i],
197 $ali_tend[$i],
198 $ali_bitscore[$i],
199 $strand,
200 $queryname;
201 }
202 }
203
204
205 sub profmarkout {
206 my $ofh = shift;
207 my $i;
208
209 for ($i = 0; $i < $nhits; $i++) {
210 printf $ofh "%g\t%.1f\t%s\t%s\n", $hit_Eval[$i], $hit_bitscore[$i], $hit_target[$i], $queryname;
211 }
212 }
213 1;
214 __END__
215
216 |more
217