Debian Codegraph @

hmmer/easel/demotic/demotic_h2.pm

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