Debian Codegraph @

hmmer/documentation/userguide/inclusions/gen-inclusions.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # gen-inclusions.py : generate .tex inclusions for user guide
4 #
5 # usage: gen-inclusions.py
6 # or: gen-inclusions.py <top_builddir> <top_srcdir>
7 #
8 # Runs the tutorial examples in the user guide and extracts output
9 # snippets for inclusion in .tex files. This saves having to manually
10 # update the user guide to be consistent with small changes to outputs
11 # (including version numbers and such).
12 #
13 # Each example is run in the current working directory, with input
14 # and output files in the cwd, some of them symlinked from
15 # `<top_srcdir>/tutorial`.
16 #
17 # Intended to be run infrequently by developers, not users, to update
18 # the user guide source files for a new release. The resulting .tex
19 # snippets are checked into git. It can assume that it's working on a
20 # development machine in a git repo, not in distribution code in the
21 # field.
22 #
23 # Script looks for HMMER3 executables in `<top_builddir>/src`, and
24 # tutorial files in `<top_srcdir>/tutorial`. The top_builddir and
25 # top_srcdir path prefixes default to `../../..`, because it is
26 # normally run in `documentation/userguide/inclusions`.
27 #
28 # If provided, <top_builddir> and <top_srcdir> can be either relative
29 # or absolute paths.
30 #
31 # Options:
32 # -t, --tutupdate : update tutorial files from Pfam, UniProt
33 # --noclean : don't clean up the files it generates
34
35
36 import sys
37 import os
38 import shutil
39 import subprocess
40 import re
41 import argparse
42
43 # Parse command line options and arguments
44 #
45 parser = argparse.ArgumentParser(description='generate .tex inclusions for user guide')
46 parser.add_argument('-t', '--tutupdate', action='store_true')
47 parser.add_argument( '--noclean', action='store_true')
48 parser.add_argument('top_builddir', nargs='?', default='../../..')
49 parser.add_argument('top_srcdir', nargs='?', default='../../..')
50 args = parser.parse_args()
51 top_builddir = os.path.abspath(args.top_builddir) # absolute to top of build tree e.g. '/home/seddy/hmmer3/build-osx'
52 top_srcdir = args.top_srcdir # relative to top of source, e.g. '../../..'
53
54 # Check that <top_builddir>/src seems to contain compiled programs.
55 #
56 if (not os.path.exists('{0}/src/hmmscan'.format(top_builddir))) or (not os.path.exists('{0}/src/hmmsearch'.format(top_builddir))):
57 sys.exit('no programs found? ./configure; make first')
58
59
60 print('gen-inclusions.py : a fiddly script to generate .tex inclusions for user guide')
61 print('cross your fingers...')
62
63
64 # optionally, update tutorial files that come from Pfam, UniProt.
65 # don't update MADE1.sto from Dfam. It's a derivative, a 100-seq subset of the 2000-seq Dfam alignment.
66 if args.tutupdate:
67 subprocess.run('wget -O fn3.sto https://pfam.xfam.org/family/fn3/alignment/seed', shell=True, cwd='{0}/tutorial'.format(top_srcdir))
68 subprocess.run('wget -O Pkinase.sto https://pfam.xfam.org/family/Pkinase/alignment/seed', shell=True, cwd='{0}/tutorial'.format(top_srcdir))
69 subprocess.run('wget -O 7LESS_DROME http://www.uniprot.org/uniprot/P13368.txt', shell=True, cwd='{0}/tutorial'.format(top_srcdir))
70
71
72
73 # make symlinks to tutorial files in cwd (i.e. in inclusions/ in the source directory)
74 # exception: globins4.sto is included verbatim in userguide, so copy it.
75 #
76 tutorial_files = [ '7LESS_DROME', 'HBB_HUMAN', 'MADE1.sto', 'Pkinase.sto', 'dna_target.fa', 'fn3.sto', 'globins45.fa' ]
77 for file in tutorial_files:
78 if not os.path.exists(file): os.symlink('{0}/tutorial/{1}'.format(top_srcdir, file), file)
79 for file in ['globins4.sto']:
80 if not os.path.exists(file): shutil.copyfile('{0}/tutorial/{1}'.format(top_srcdir, file), file)
81
82
83
84 # get a symlink or copy of uniprot_swissprot and its relnotes.txt in cwd too.
85 # Default: I keep a copy on my development machine in my ~/data/.
86 # Else: Attempt to download a copy from UniProt to cwd.
87 swissprot_dir = os.path.expanduser('~/data/Uniprot')
88 if os.path.exists(swissprot_dir):
89 if not os.path.exists('uniprot_sprot.fasta'): os.symlink('{0}/uniprot_sprot.fasta'.format(swissprot_dir), 'uniprot_sprot.fasta')
90 if not os.path.exists('relnotes.txt'): os.symlink('{0}/relnotes.txt'.format(swissprot_dir), 'relnotes.txt')
91 else:
92 subprocess.run('wget ftp://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/complete/uniprot_sprot.fasta.gz', shell=True)
93 subprocess.run('wget ftp://ftp.uniprot.org/pub/databases/uniprot/current_release/relnotes.txt', shell=True)
94 subprocess.run('gunzip -f uniprot_sprot.fasta.gz', shell=True)
95
96
97
98 # uniprot_release = '2018_02' or some such, from relnotes.txt
99 # uniprot_nseq = '556,825' or some such (as string, with commas)
100 #
101 def uniprot_relnotes(deffp):
102 print('parsing uniprot release info...')
103
104 with open('relnotes.txt', 'r') as f:
105 content = f.read()
106 m = re.search(r'UniProt Release (\S+)', content)
107 if not m: sys.exit('failed to identify UniProt release code')
108 uniprot_release = m.group(1)
109
110 m = re.search(r'UniProtKB/Swiss-Prot:\s*(\S+) entries', content)
111 if not m: sys.exit('failed to identify UniProt/Swiss-Prot nseq')
112 uniprot_nseq = m.group(1)
113
114 print(r'\newcommand{{\UNIrelease}}{{{0}}}'.format(re.sub('_', r'\_', uniprot_release)), file=deffp)
115 print(r'\newcommand{{\UNInseq}}{{{0}}}'.format(uniprot_nseq), file=deffp)
116 print('', file=deffp)
117
118
119
120 # intro uses hmmscan -h header
121 # this also gets version and date, as \HMMERversion, \HMMERdate
122 #
123 def hmmscan_noargs(deffp):
124 print('running hmmscan (with no args)...')
125 r = subprocess.run('hmmscan -h',
126 shell=True, stdout=subprocess.PIPE, encoding='utf-8', # don't check=True; hmmscan -h returns 1
127 env={"PATH": "{0}/src".format(top_builddir)})
128
129 m = re.match(r'((?:#.*\n)+)', r.stdout)
130 with open('hmmscan-noargs.out', 'w') as f:
131 print(m.group(1), file=f, end='')
132
133 m = re.search('# HMMER\s+(\S+)\s+\((.+?)\);', r.stdout)
134 print(r'\newcommand{{\HMMERversion}}{{{0}}}'.format(m.group(1)), file=deffp)
135 print(r'\newcommand{{\HMMERdate}}{{{0}}}'.format(m.group(2)), file=deffp)
136 print('', file=deffp)
137
138
139
140
141 # 0. hmmbuild (with no arguments)
142 # Don't set check=True, because hmmbuild w/ no args returns 1 exit code.
143 # hmmbuild-noargs.out : stdout, complete
144 #
145 def hmmbuild_noargs():
146 print('running hmmbuild (with no args)...')
147 r = subprocess.run('hmmbuild > hmmbuild-noargs.out',
148 shell=True, stdout=subprocess.PIPE, encoding='utf-8',
149 env={"PATH": "{0}/src".format(top_builddir)})
150
151
152
153
154 # 1. hmmbuild globins4.hmm globins4.sto
155 #
156 def hmmbuild_globins(deffp):
157 print('running hmmbuild globins4.hmm globins4.sto...')
158 r = subprocess.run('hmmbuild globins4.hmm globins4.sto',
159 shell=True, stdout=subprocess.PIPE, check=True, encoding='utf-8',
160 env={"PATH": "{0}/src".format(top_builddir)})
161
162 with open("hmmbuild-globins.out", "w") as f:
163 print(r.stdout, file=f, end='')
164
165 m = re.search(r'^\d+\s+\S+\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)', r.stdout, flags=re.MULTILINE)
166 if not m: sys.exit("bad pattern match to hmmbuild output")
167 print(r"\newcommand{{\BGLnseq}}{{{0}}}".format(m.group(1)), file=deffp) # LaTeX macro names only allow letters
168 print(r"\newcommand{{\BGLalen}}{{{0}}}".format(m.group(2)), file=deffp) # {{, }} are escapes for {,}
169 print(r"\newcommand{{\BGLmlen}}{{{0}}}".format(m.group(3)), file=deffp)
170 print(r"\newcommand{{\BGLgaps}}{{{0}}}".format(int(m.group(2))-int(m.group(3))), file=deffp)
171 print(r"\newcommand{{\BGLeffn}}{{{0}}}".format(m.group(4)), file=deffp)
172 print(r"\newcommand{{\BGLre}}{{{0}}}".format(m.group(5)), file=deffp)
173 print('', file=deffp)
174
175 with open('globins4.hmm', 'r') as f:
176 content = f.read()
177 # elide both horizontally and vertically. Among the regexp trickery here:
178 # \n matches newline if pattern is not a raw string; but then other regexp elems have to be \\S, etc
179 # (?:) is a non-capturing group, useful when you need the group for a {} repetition operator
180 content = re.sub('(\nHMM\\s+A\\s+C.+\n(?:.+\n){7})(?s:.+)\n((?:.+\n){3}//)', '\\1...\n\\2', content) # Cuts every line between 1st and last state.
181 content = re.sub(r'(HMM(?:\s+\S\s{3}){7})(?:\s+\S\s{3}){11}(.+)', r'\1 ... \2', content) # Cuts columns in the HMM line
182 content = re.sub(r'^(\s+COMPO(?:\s+\S+){7})(?:\s+\S+){11}(.+)$', r'\1 ... \2', content, flags=re.MULTILINE) # Cuts columns in the COMPO line
183 content = re.sub(r'^(\s+\d+(?:\s+\S+){7})(?:\s+\S+){11}(.+)$', r'\1 ... \2', content, flags=re.MULTILINE) # Cuts columns in the match lines
184 content = re.sub(r'^((?:\s+[\d\.*]+){7})(?:\s+\S+){11}((?:\s+[\d\.*]+){2})$', r'\1 ... \2', content, flags=re.MULTILINE) # Cuts columns in the insert, transition lines
185 with open('hmmbuild-globins.out2', 'w') as f:
186 print(content, file=f, end='')
187
188 # the formats chapter uses this profile too
189 with open('globins4.hmm', 'r') as f:
190 content = f.read()
191 m = re.match(r'HMMER3/(\S)\s+(\[.+\])', content)
192 print(r'\newcommand{{\HMMERfmtversion}}{{{0}}}'.format(m.group(1)), file=deffp)
193 print(r'\newcommand{{\HMMERsavestamp}}{{{0}}}'.format(m.group(2)), file=deffp)
194 print('', file=deffp)
195
196
197 # 2. hmmsearch globins4.hmm uniprot_sprot.fasta
198 #
199 def hmmsearch_globins(deffp):
200 print('running hmmsearch globins4.hmm uniprot_sprot.fasta...')
201 r = subprocess.run('hmmsearch globins4.hmm uniprot_sprot.fasta',
202 shell=True, stdout=subprocess.PIPE, check=True, encoding='utf-8',
203 env={"PATH": "{0}/src".format(top_builddir)})
204
205 m = re.match('(.+Scores for complete sequences.+?)\n', r.stdout, flags=re.DOTALL)
206 if not m: sys.exit('bad first pattern match to hmmsearch output')
207 with open("hmmsearch-globins.out", "w") as f:
208 print(m.group(1), file=f)
209
210 m = re.search('\n(\\s+--- full sequence ---(.+\n){9})', r.stdout) # matches 9 lines of output starting with --- full sequence --- line
211 if not m: sys.exit('bad top hits pattern match to hmmsearch output')
212 with open("hmmsearch-globins.out2", "w") as f:
213 print(m.group(1), file=f, end='') # end='' because pattern match included a final \n
214
215 m = re.search(r'\n(Internal pipeline statistics.+)', r.stdout, flags=re.DOTALL)
216 if not m: sys.exit('bad xpipeline stats pattern match to hmmsearch output')
217 with open("hmmsearch-globins.out3", "w") as f:
218 print(m.group(1), file=f, end='')
219
220 # e-value score bias e-val sc bias exp N seqname
221 m = re.search(r'^\s*([0-9\.e-]+)\s+([0-9\.]+)\s+([0-9\.]+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+)\s+sp\|\S+\|(\S+)', r.stdout, flags=re.MULTILINE)
222 if not m: sys.exit('bad third pattern match to hmmsearch output')
223 print(r'\newcommand{{\SGUevalue}}{{{0}}}'.format(m.group(1)), file=deffp)
224 print(r'\newcommand{{\SGUbitscore}}{{{0}}}'.format(m.group(2)), file=deffp)
225 print(r'\newcommand{{\SGUbias}}{{{0}}}'.format(m.group(3)), file=deffp)
226 print(r'\newcommand{{\SGUorigscore}}{{{0:.1f}}}'.format(float(m.group(2)) + float(m.group(3))), file=deffp)
227 print(r'\newcommand{{\SGUdombitscore}}{{{0}}}'.format(m.group(5)), file=deffp)
228 print(r'\newcommand{{\SGUseqname}}{{{0}}}'.format(re.sub('_', r'\_', m.group(9))), file=deffp) # protect the \ in the swissprot name
229 # part of the text assumes that the "full sequence bit score" and "best 1 domain bit score"
230 # for the top globin hit aren't identical. They're already .1f strings and can be compared directly.
231 assert m.group(2) != m.group(5), "guide assumes that full vs. best 1 domain bit scores for top globin differ slightly"
232
233 m = re.search(r'Passed MSV filter:\s+\d+\s+\((\S+?)\)', r.stdout)
234 print(r'\newcommand{{\SGUmsvpass}}{{{0:.1f}}}'.format(100 * round(float(m.group(1)), 3)), file=deffp)
235
236 m = re.search(r'Passed bias filter:\s+(\d+)', r.stdout)
237 print(r'\newcommand{{\SGUbiaspass}}{{{0}}}'.format(m.group(1)), file=deffp)
238
239 m = re.search(r'Passed Vit filter:\s+(\d+)', r.stdout)
240 print(r'\newcommand{{\SGUvitpass}}{{{0}}}'.format(m.group(1)), file=deffp)
241
242 m = re.search(r'Passed Fwd filter:\s+(\d+)', r.stdout)
243 print(r'\newcommand{{\SGUfwdpass}}{{{0}}}'.format(m.group(1)), file=deffp)
244
245 m = re.search(r'Elapsed: \d+:\d+:(\S+)', r.stdout)
246 print(r'\newcommand{{\SGUelapsed}}{{{0:.1f}}}'.format(float(m.group(1))), file=deffp)
247 print('', file=deffp)
248
249
250
251
252 # 3. hmmsearch fn3.hmm 7LESS_DROME
253 # hmmsearch-fn3-sevenless.out : first lines of top hits table
254 # hmmsearch-fn3-sevenless.out2 : domain annotation section's starting line
255 # hmmsearch-fn3-sevenless.out3 : domain annotation section, table
256 #
257 def hmmsearch_fn3_sevenless(deffp):
258 print('running hmmbuild, then hmmsearch fn3.hmm 7LESS_DROME...')
259
260 r = subprocess.run('hmmbuild fn3.hmm fn3.sto',
261 shell=True, stdout=subprocess.PIPE, check=True, encoding='utf-8',
262 env={"PATH": "{0}/src".format(top_builddir)})
263
264 r = subprocess.run('hmmsearch fn3.hmm 7LESS_DROME',
265 shell=True, stdout=subprocess.PIPE, check=True, encoding='utf-8',
266 env={"PATH": "{0}/src".format(top_builddir)})
267
268 m = re.search('\n(\\s+--- full sequence ---(?:.+\n){4})', r.stdout) # .out: per-seq hit table (one hit) from 7LESS_DROME
269 if not m: sys.exit('bad first pattern match to fn3 hmmsearch output')
270 with open('hmmsearch-fn3-sevenless.out', 'w') as f:
271 print(m.group(1), file=f, end='')
272
273 # append parsed variables from per-seq hit table line to .def
274 # e-value score bias e-val sc bias exp N seqname
275 m = re.search(r'^\s*([0-9\.e-]+)\s+([0-9\.]+)\s+([0-9\.]+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\S+)', r.stdout, flags=re.MULTILINE)
276 if not m: sys.exit('bad second pattern match to fn3 hmmsearch output')
277 print(r'\newcommand{{\SFSevalue}}{{{0}}}'.format(m.group(1)), file=deffp)
278 print(r'\newcommand{{\SFSbitscore}}{{{0}}}'.format(m.group(2)), file=deffp)
279 print(r'\newcommand{{\SFSdomevalue}}{{{0}}}'.format(m.group(4)), file=deffp)
280 print(r'\newcommand{{\SFSdombitscore}}{{{0}}}'.format(m.group(5)), file=deffp)
281 print(r'\newcommand{{\SFSexpdom}}{{{0}}}'.format(m.group(7)), file=deffp)
282 print(r'\newcommand{{\SFSndom}}{{{0}}}'.format(m.group(8)), file=deffp)
283 print('', file=deffp)
284
285 m = re.search(r'^(Domain annotation.+:\s*)$', r.stdout, flags=re.MULTILINE) # .out2: "Domain annotation" header line
286 if not m: sys.exit('bad domain section line pattern match to fn3 hmmsearch output')
287 with open('hmmsearch-fn3-sevenless.out2', 'w') as f:
288 print(m.group(1), file=f, end='')
289
290 m = re.search('\n(>> 7LESS_DROME.+\n.+\n.+\n((?:\\s*\\d+.+\n)+))', r.stdout) # .out3: per-domain hit table (9 domains) for 7LESS_DROME
291 if not m: sys.exit('bad domain table pattern match to fn3 hmmsearch output')
292 with open('hmmsearch-fn3-sevenless.out3', 'w') as f:
293 print(m.group(1), file=f, end='')
294 tbl = m.group(2) # <tbl> = multiline string containing the 7LESS_DROME domain hits table
295
296 m = re.search(r'\n( ==\s+domain\s+2.+? PP\s*)\n', r.stdout, flags=re.DOTALL) # .out4: alignment of domain 2
297 if not m: sys.exit('bad alignment pattern match to fn3 hmmsearch output')
298 with open('hmmsearch-fn3-sevenless.out4', 'w') as f:
299 print(m.group(1), file=f, end='')
300
301 # parse and store info on (9) fn3 domains identified in 7LESS_DROME
302 domtbl1 = []
303 for line in tbl.splitlines():
304 fields = line.split()
305 dom = { 'idx' : int(fields[0]),
306 'sigchar' : fields[1],
307 'bitscore' : float(fields[2]),
308 'c-evalue' : float(fields[4]),
309 'i-evalue' : float(fields[5]),
310 'hmmfrom' : int(fields[6]),
311 'hmmto' : int(fields[7]),
312 'envfrom' : int(fields[12]),
313 'envto' : int(fields[13]) }
314 domtbl1.append(dom)
315
316 return domtbl1
317
318
319
320 # 4. hmmsearch fn3.hmm uniprot_sprot
321 #
322 def hmmsearch_fn3_uniprot(deffp, domtbl1):
323 print('running hmmsearch fn3.hmm uniprot_sprot.fasta...')
324
325 r = subprocess.run('hmmsearch fn3.hmm uniprot_sprot.fasta',
326 shell=True, stdout=subprocess.PIPE, check=True, encoding='utf-8',
327 env={"PATH": "{0}/src".format(top_builddir)})
328
329 # uniprot name is sp|P13368|7LESS_DROME
330 m = re.search('\n(>> \S+\|7LESS_DROME.+\n.+\n.+\n((?:\\s*\\d+.+\n)+))', r.stdout) # .out: per-domain hit table (9 domains) for 7LESS_DROME, Uniprot search version
331 if not m: sys.exit('bad pattern match to fn3 hmmsearch output')
332 with open('hmmsearch-fn3-uniprot.out', 'w') as f:
333 print(m.group(1), file=f, end='')
334 tbl = m.group(2) # <tbl> = multiline string containing the domain hits table
335
336 # parse and store info on (7) fn3 domains identified in 7LESS_DROME in full UniProt search
337 # domtbl1 is from just searching 7LESS_DROME alone, which gives more domains, because
338 # some aren't statistically significant in the larger search.
339 domtbl2 = []
340 for line in tbl.splitlines():
341 fields = line.split()
342 dom = { 'idx' : int(fields[0]),
343 'sigchar' : fields[1],
344 'bitscore' : float(fields[2]),
345 'c-evalue' : float(fields[4]),
346 'i-evalue' : float(fields[5]),
347 'hmmfrom' : int(fields[6]),
348 'hmmto' : int(fields[7]),
349 'envfrom' : int(fields[12]),
350 'envto' : int(fields[13]) }
351 domtbl2.append(dom)
352
353 # parse out <domZ> from the trailer
354 # we need it to construct numbers used to explain the conditional E-value calculation
355 m = re.search(r'^Domain search space\s+\(domZ\):\s+(\d+)\s+\[number of targets reported over threshold\]', r.stdout, flags=re.MULTILINE)
356 if not m: sys.exit('bad second pattern match to fn3 hmmsearch output')
357 fn3_domZ = int(m.group(1))
358
359 # capture information about best-scoring domain, from domtbl1 and domtbl2
360 bestdom1 = sorted(domtbl1, key=lambda k: k['bitscore'], reverse=True)[0]
361 bestdom2 = sorted(domtbl2, key=lambda k: k['bitscore'], reverse=True)[0]
362
363 # construct list of domains in 7LESS_DROME single seq search but not in full Uniprot search
364 lostdoms = []
365 insigdoms = []
366 i = 0
367 for d in range(len(domtbl2)):
368 while (domtbl1[i]['envfrom'] != domtbl2[d]['envfrom']):
369 lostdoms.append(domtbl1[i])
370 i += 1
371 if (domtbl1[i]['sigchar'] == '!' and domtbl2[d]['sigchar'] == '?'):
372 insigdoms.append(domtbl1[i])
373 i += 1
374 assert len(lostdoms) == 2, "userguide assumes two domains are lost from single 7LESS_DROME seq vs. UniProt search"
375 assert len(insigdoms) == 2, "userguide assumes two domains changed from ! to ? in single seq vs. UniProt search"
376
377
378 # There's a section that says that domains 1,4,5 and maybe 6 are strong;
379 # 2 is weak; 3,7 are insignificant, in the Uniprot version of the search.
380 # Just check that this is true.
381 assert len(domtbl2) == 7
382 assert domtbl2[0]['i-evalue'] < 0.01
383 assert domtbl2[3]['i-evalue'] < 0.01
384 assert domtbl2[4]['i-evalue'] < 0.01
385 assert domtbl2[5]['i-evalue'] < 0.2
386 assert domtbl2[1]['i-evalue'] > 0.1 and domtbl2[1]['c-evalue'] < 0.01
387 assert domtbl2[6]['i-evalue'] > 0.1 and domtbl2[6]['c-evalue'] < 0.1
388 assert domtbl2[2]['c-evalue'] > 0.1
389
390 print(r'\newcommand{{\SFSmaxdom}}{{{0}}}'.format(bestdom1['idx']), file=deffp)
391 print(r'\newcommand{{\SFSmaxdomu}}{{{0}}}'.format(bestdom2['idx']), file=deffp)
392 print(r'\newcommand{{\SFSmaxsc}}{{{0:.1f}}}'.format(bestdom1['bitscore']), file=deffp)
393 print(r'\newcommand{{\SFSievalue}}{{{0}}}'.format(bestdom1['i-evalue']), file=deffp)
394 print(r'\newcommand{{\SFSuievalue}}{{{0:.1e}}}'.format(bestdom2['i-evalue']), file=deffp)
395 print(r'\newcommand{{\SFSdomZ}}{{{0}}}'.format(fn3_domZ), file=deffp)
396 print(r'\newcommand{{\SFSucevalue}}{{{0:.1e}}}'.format(bestdom2['c-evalue']), file=deffp)
397 print(r'\newcommand{{\SFSaidx}}{{{0}}}'.format(lostdoms[0]['idx']), file=deffp)
398 print(r'\newcommand{{\SFSascore}}{{{0:.1f}}}'.format(lostdoms[0]['bitscore']), file=deffp)
399 print(r'\newcommand{{\SFSaevalue}}{{{0:.2g}}}'.format(lostdoms[0]['i-evalue']), file=deffp)
400 print(r'\newcommand{{\SFSauevalue}}{{{0:.0f}}}'.format(lostdoms[0]['i-evalue'] * fn3_domZ), file=deffp)
401 print(r'\newcommand{{\SFSacoords}}{{{0}-{1}}}'.format(lostdoms[0]['envfrom'], lostdoms[0]['envto']), file=deffp)
402 print(r'\newcommand{{\SFSbidx}}{{{0}}}'.format(lostdoms[1]['idx']), file=deffp)
403 print(r'\newcommand{{\SFSbscore}}{{{0:.1f}}}'.format(lostdoms[1]['bitscore']), file=deffp)
404 print(r'\newcommand{{\SFSbevalue}}{{{0:.2g}}}'.format(lostdoms[1]['i-evalue']), file=deffp)
405 print(r'\newcommand{{\SFSbuevalue}}{{{0:.1f}}}'.format(lostdoms[1]['i-evalue'] * fn3_domZ), file=deffp)
406 print(r'\newcommand{{\SFSbcoords}}{{{0}-{1}}}'.format(lostdoms[1]['envfrom'], lostdoms[1]['envto']), file=deffp)
407 print(r'\newcommand{{\SFSainsig}}{{{0:.1f}}}'.format(insigdoms[0]['bitscore']), file=deffp)
408 print(r'\newcommand{{\SFSbinsig}}{{{0:.1f}}}'.format(insigdoms[1]['bitscore']), file=deffp)
409 print('', file=deffp)
410
411
412
413 def sevenless_table():
414 print('parsing domain annotation in 7LESS_DROME...')
415 with open('7LESS_DROME', 'r') as f:
416 content = f.read()
417 domtbl = []
418 for m in re.finditer(r'^(FT\s+DOMAIN\s+\d+\s+\d+\s+Fibronectin.+)\nFT\s+(.+)\n', content, flags=re.MULTILINE):
419 domtbl.append('{0} {1}'.format(m.group(1), m.group(2)))
420
421 assert len(domtbl) == 7, 'guide assumes that uniprot annotates seven domains in 7LESS_DROME'
422
423 with open('sevenless_domains.out', 'w') as f:
424 for domline in domtbl:
425 print(domline, file=f)
426
427
428
429
430 def jackhmmer_hbb_uniprot(deffp):
431 print('running jackhmmer HBB_HUMAN uniprot_sprot.fasta...')
432
433 r = subprocess.run('jackhmmer HBB_HUMAN uniprot_sprot.fasta',
434 shell=True, stdout=subprocess.PIPE, check=True, encoding='utf-8',
435 env={"PATH": "{0}/src".format(top_builddir)})
436
437 m = re.match(r'((?s:.+?)Scores for complete sequences(?:.*\n){10})', r.stdout)
438 with open('jackhmmer-hbb-uniprot.out', 'w') as f:
439 print(m.group(1), file=f, end='')
440 print('...', file=f)
441
442 m = re.search(r'\n((?:.*\n){2}\s*------ inclusion threshold ------(?:.*\n){3})', r.stdout)
443 with open('jackhmmer-hbb-uniprot.out2', 'w') as f:
444 print(m.group(1), file=f, end='')
445
446 m = re.search(r'\n((?:@@.*\n)+(?:.*\n)(?:@@.*\n)+)', r.stdout)
447 with open('jackhmmer-hbb-uniprot.out3', 'w') as f:
448 print(m.group(1), file=f, end='')
449
450 m = re.search(r'@@ Included in MSA:\s+(\d+)\s+subsequences\s+\(query\s+\+\s+(\d+) subseqs from \d+ targets', r.stdout)
451 print(r'\newcommand{{\JHUninc}}{{{0}}}'.format(m.group(1)), file=deffp)
452 print(r'\newcommand{{\JHUnsig}}{{{0}}}'.format(m.group(1)), file=deffp)
453
454 m = re.search(r'@@\s+Round:\s+2\s*\n(?s:.+?)(Scores for complete sequences(?:.*\n){8})', r.stdout)
455 with open('jackhmmer-hbb-uniprot.out4', 'w') as f:
456 print(m.group(1), file=f, end='')
457 print('...', file=f)
458
459 m = re.search(r"""
460 @@\s+Round:\s+2.* # anchor on round 2 part of the output
461 (?s: .+?) # accept anything until...
462 \n
463 ( # start what we're grabbing...
464 (?: .+\n) {2} # match exactly two preceding lines
465 \+.+\n # first + line
466 (?: [^\+].+\n ) {1,4}? # up to 1-4 intervening lines
467 \+.+\n # a second + line
468 (?: [^\+].+\n ) {1,7}? # up to 1-7 intervening lines
469 \+.+\n # a third + line
470 .+\n # and one trailing line
471 ) # end of our grab
472 """, r.stdout, flags=re.VERBOSE)
473 with open('jackhmmer-hbb-uniprot.out5', 'w') as f:
474 print('...', file=f)
475 print(m.group(1), file=f, end='')
476 print('...', file=f)
477
478 m = re.search(r"""
479 @@\s+Round:\s+2 # anchor on round 2 part of the output
480 (?s:.+?) # accept anything until...
481 \n
482 ( # start grab:
483 (?:@@.*\n)+ # a block of @@ info
484 (?:.*\n) # blank line(s)
485 (?:@@.*\n)+ # a second block of @@ info
486 ) # end grab.
487 """, r.stdout, flags=re.VERBOSE)
488 with open('jackhmmer-hbb-uniprot.out6', 'w') as f:
489 print(m.group(1), file=f, end='')
490
491 m = re.search(r"""
492 @@\s+Round:\s+4
493 (?s:.+?)
494 (
495 @@\s+New\s+targets\s+included:
496 (?s:.+?)
497 //\n
498 \[ok\]
499 )
500 $
501 """, r.stdout, flags=re.VERBOSE)
502 with open('jackhmmer-hbb-uniprot.out7', 'w') as f:
503 print(m.group(1), file=f, end='')
504
505 print('', file=deffp)
506
507
508
509 def hmmscan_sevenless():
510 print('building minifam and running hmmscan on 7LESS_DROME...')
511
512 subprocess.run('hmmbuild globins4.hmm globins4.sto', shell=True, stdout=subprocess.DEVNULL, check=True, env={"PATH": "{0}/src".format(top_builddir)})
513 subprocess.run('hmmbuild fn3.hmm fn3.sto', shell=True, stdout=subprocess.DEVNULL, check=True, env={"PATH": "{0}/src".format(top_builddir)})
514 subprocess.run('hmmbuild Pkinase.hmm Pkinase.sto', shell=True, stdout=subprocess.DEVNULL, check=True, env={"PATH": "{0}/src".format(top_builddir)})
515 subprocess.run('cat globins4.hmm fn3.hmm Pkinase.hmm > minifam', shell=True, stdout=subprocess.DEVNULL, check=True)
516
517 r = subprocess.run('hmmpress -f minifam',
518 shell=True, stdout=subprocess.PIPE, check=True, encoding='utf-8',
519 env={"PATH": "{0}/src".format(top_builddir)})
520 with open('hmmpress-minifam.out', 'w') as f:
521 print(r.stdout, file=f, end='')
522
523 r = subprocess.run('hmmscan minifam 7LESS_DROME',
524 shell=True, stdout=subprocess.PIPE, check=True, encoding='utf-8',
525 env={"PATH": "{0}/src".format(top_builddir)})
526
527 m = re.match(r'((?s:.+?)Scores for complete sequence.*\n(?:.*\n){5})', r.stdout)
528 with open('hmmscan-minifam-sevenless.out', 'w') as f:
529 print(m.group(1), file=f, end='')
530
531 m = re.search(r'(Domain annotation.+\n>> fn3.+\n.+\n.+\n(?:\s*\d+.+\n)+)', r.stdout)
532 with open('hmmscan-minifam-sevenless.out2', 'w') as f:
533 print(m.group(1), file=f, end='')
534
535 m = re.search(r'\n( ==\s+domain\s+2(?s:.+?) PP\s*)\n', r.stdout)
536 with open('hmmscan-minifam-sevenless.out3', 'w') as f:
537 print(m.group(1), file=f, end='')
538
539
540
541
542 def hmmstat_minifam():
543 print('running hmmstat minifam...')
544
545 r = subprocess.run('hmmstat minifam',
546 shell=True, stdout=subprocess.PIPE, check=True, encoding='utf-8',
547 env={"PATH": "{0}/src".format(top_builddir)})
548 with open('hmmstat-minifam.out', 'w') as f:
549 print(r.stdout, file=f, end='')
550
551
552
553
554
555 def hmmalign_globins():
556 print('running hmmalign globins4.hmm globins45.fa...')
557
558 r = subprocess.run('hmmalign globins4.hmm globins45.fa',
559 shell=True, stdout=subprocess.PIPE, check=True, encoding='utf-8',
560 env={"PATH": "{0}/src".format(top_builddir)})
561
562 m = re.match(r'(# STOCKHOLM(?:.*\n){13})', r.stdout)
563 content = re.sub('^(.{80}).*$', r'\1 ...', m.group(1), flags=re.MULTILINE)
564 with open('hmmalign-globins.out', 'w') as f:
565 print(content, file=f, end='')
566 print('...', file=f)
567
568 m = re.search(r'\n((?:.+\n){4}#=GC PP_cons.+\n#=GC RF.+\n)', r.stdout)
569 content = re.sub('^(.{80}).*$', r'\1 ...', m.group(1), flags=re.MULTILINE)
570 with open('hmmalign-globins.out2', 'w') as f:
571 print('...', file=f)
572 print(content, file=f, end='')
573
574
575 # Text uses MYG_HORSE's unaligned leading G, PP=8 as an example.
576 assert re.search(r'^MYG_HORSE\s+g', r.stdout, flags=re.MULTILINE)
577 assert re.search(r'^#=GR\s+MYG_HORSE\s+PP\s+8', r.stdout, flags=re.MULTILINE)
578
579
580
581 def hmmbuild_made1():
582 print('running hmmbuild MADE1.hmm MADE1.sto')
583
584 r = subprocess.run('hmmbuild MADE1.hmm MADE1.sto',
585 shell=True, stdout=subprocess.PIPE, check=True, encoding='utf-8',
586 env={"PATH": "{0}/src".format(top_builddir)})
587
588 content = re.sub(r'(MADE1 \(MAriner Derived Element 1\), a TcMar-Mariner).+\n', r'\1 ...\n', r.stdout)
589 with open('hmmbuild-made1.out', 'w') as f:
590 print(content, file=f, end='')
591
592
593
594 def nhmmer_made1(deffp):
595 print('running nhmmer MADE1.hmm dna_target.fa')
596
597 r = subprocess.run('nhmmer MADE1.hmm dna_target.fa',
598 shell=True, stdout=subprocess.PIPE, check=True, encoding='utf-8',
599 env={"PATH": "{0}/src".format(top_builddir)})
600
601 m = re.search(r'\n(\s*E-value\s*score.+\n(?:.+\n)+\s*-+\s*inclusion threshold.+\n.+\n)', r.stdout)
602 with open('nhmmer-made1.out', 'w') as f:
603 print(m.group(1), file=f, end='')
604
605 m = re.search(r'humanchr1.+\s+(3\d+)\s+(3\d+)(?s:.+?)humanchr1.+\s+(3\d+)\s+(3\d+)', r.stdout)
606 print(r'\newcommand{{\NMHafrom}}{{{0}}}'.format(m.group(1)), file=deffp)
607 print(r'\newcommand{{\NMHato}}{{{0}}}'.format(m.group(2)), file=deffp)
608 print(r'\newcommand{{\NMHbfrom}}{{{0}}}'.format(m.group(3)), file=deffp)
609 print(r'\newcommand{{\NMHbto}}{{{0}}}'.format(m.group(4)), file=deffp)
610
611 assert int(m.group(1)) < int(m.group(3)) # "one on the forward strand"
612
613 m = re.search(r'^(Annotation.+)\n>>', r.stdout, flags=re.MULTILINE)
614 with open('nhmmer-made1.out2', 'w') as f:
615 print(m.group(1), file=f)
616
617 m = re.search(r'\n(>> humanchr1(?:.+\n){4})', r.stdout)
618 with open('nhmmer-made1.out3', 'w') as f:
619 print(m.group(1), file=f)
620
621 m = re.search(r'\n\n(\s+Alignment.+\n\s*score.+\n(?:.*\n)+?)\n>>', r.stdout)
622 with open('nhmmer-made1.out4', 'w') as f:
623 print(m.group(1), file=f, end='')
624
625 m = re.search(r'\n(Internal pipeline statistics.+)', r.stdout, flags=re.DOTALL)
626 with open("nhmmer-made1.out5", "w") as f:
627 print(m.group(1), file=f, end='')
628
629 m = re.search(r'^Target sequences:\s+\d+\s+\((\d+)\s+residues', r.stdout, flags=re.MULTILINE)
630 print(r'\newcommand{{\NMHnres}}{{{0}}}'.format(m.group(1)), file=deffp)
631 print(r'\newcommand{{\NMHntop}}{{{0}}}'.format(int(m.group(1))//2), file=deffp)
632
633 m = re.search(r'^Residues passing SSV filter:\s+(\d+)\s+\((\S+?)\)', r.stdout, flags=re.MULTILINE)
634 print(r'\newcommand{{\NMHnssv}}{{{0}}}'.format(m.group(1)), file=deffp)
635 print(r'\newcommand{{\NMHfracssv}}{{{0:.1f}}}'.format(float(m.group(2))*100), file=deffp)
636
637 m = re.search(r'^Residues passing bias filter:\s+(\d+)\s+\((\S+?)\)', r.stdout, flags=re.MULTILINE)
638 print(r'\newcommand{{\NMHnbias}}{{{0}}}'.format(m.group(1)), file=deffp)
639 print(r'\newcommand{{\NMHfracbias}}{{{0:.1f}}}'.format(float(m.group(2))*100), file=deffp)
640
641 m = re.search(r'^Residues passing Vit filter:\s+(\d+)\s+\((\S+?)\)', r.stdout, flags=re.MULTILINE)
642 print(r'\newcommand{{\NMHnvit}}{{{0}}}'.format(m.group(1)), file=deffp)
643 print(r'\newcommand{{\NMHfracvit}}{{{0:.1f}}}'.format(float(m.group(2))*100), file=deffp)
644
645 m = re.search(r'^Residues passing Fwd filter:\s+(\d+)\s+\((\S+?)\)', r.stdout, flags=re.MULTILINE)
646 print(r'\newcommand{{\NMHnfwd}}{{{0}}}'.format(m.group(1)), file=deffp)
647
648
649
650 with open("inclusions.def", "w") as deffp:
651 uniprot_relnotes(deffp)
652 hmmscan_noargs(deffp)
653 hmmbuild_noargs()
654 hmmbuild_globins(deffp)
655 hmmsearch_globins(deffp)
656 domtbl1 = hmmsearch_fn3_sevenless(deffp)
657 hmmsearch_fn3_uniprot(deffp, domtbl1)
658 sevenless_table()
659 jackhmmer_hbb_uniprot(deffp)
660 hmmscan_sevenless()
661 hmmstat_minifam()
662 hmmalign_globins()
663 hmmbuild_made1()
664 nhmmer_made1(deffp)
665
666 if not args.noclean:
667 for fname in [ 'relnotes.txt', 'uniprot_sprot.fasta',
668 'fn3.hmm', 'globins4.hmm', 'Pkinase.hmm', 'MADE1.hmm',
669 'minifam', 'minifam.h3f', 'minifam.h3i', 'minifam.h3m', 'minifam.h3p' ]:
670 if os.path.exists(fname): os.remove(fname)
671 for fname in tutorial_files:
672 if os.path.exists(fname): os.remove(fname)