forked from macarthur-lab/clinvar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grab_interesting_variations.py
63 lines (55 loc) · 1.72 KB
/
grab_interesting_variations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import sys
import re
import gzip
"""
Helper script to grab some variations by their ID from the master XML for
testing purposes.
Usage:
python grab_interesting_variations.py \
<ClinVarFullRelease.xml.gz> \
<comma-separated list of variation IDs> \
<out.xml.gz>
"""
in_xml = sys.argv[1] # e.g. ClinVarFullRelease.xml.gz
interesting_variations = set(sys.argv[2].split(","))
# ^ comma-separated list of interesting variation IDs, e.g. 187175,188901
out_xml = sys.argv[3] # where to write, e.g. interesting.xml.gz
variations_id_regex = re.compile(r'ID="(\d+)"')
# input file could be gzipped or not, output file will have same status
if in_xml.endswith(".gz"):
in_f = gzip.open(in_xml)
if not out_xml.endswith(".gz"):
out_xml += ".gz"
out_f = gzip.open(out_xml, 'w')
else:
in_f = open(in_xml)
assert not out_xml.endswith(".gz")
out_f = open(out_xml, 'w')
in_clinvarset = False
interesting = False
clinvarset = []
out_f.write(next(in_f)) # <?xml>
out_f.write(next(in_f)) # <RelaseSet>
out_f.write("\n")
for line in in_f:
if line.startswith("<ClinVarSet"):
in_clinvarset = True
elif line.startswith("</ClinVarSet>"):
if interesting:
out_f.write("".join(clinvarset))
out_f.write(line)
out_f.write("\n")
clinvarset = []
in_clinvarset = False
interesting = False
continue
else:
if line.startswith(" <MeasureSet"):
m = variations_id_regex.search(line)
interesting = (interesting or
(m and m.group(1) in interesting_variations))
if in_clinvarset:
clinvarset.append(line)
out_f.write("</ReleaseSet>\n")
in_f.close()
out_f.close()