-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparse_clinvar_xml.py
More file actions
169 lines (150 loc) · 8.07 KB
/
parse_clinvar_xml.py
File metadata and controls
169 lines (150 loc) · 8.07 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import lxml.etree as ET
import csv
import os
from tqdm import tqdm
# Define TSV column headers
columns = [
'MeasureSetID', 'MeasureSetAcc', 'MeasureSetVersion', 'AlleleID', 'MeasureType', 'CanonicalSPDI',
'GRCh37_Chromosome', 'GRCh37_PositionVCF', 'GRCh37_ReferenceAlleleVCF', 'GRCh37_AlternateAlleleVCF', 'GRCh37_Start', 'GRCh37_Stop',
'GRCh38_Chromosome', 'GRCh38_PositionVCF', 'GRCh38_ReferenceAlleleVCF', 'GRCh38_AlternateAlleleVCF', 'GRCh38_Start', 'GRCh38_Stop',
'MolecularConsequence', 'FunctionalConsequence',
'ModeOfInheritance', 'PreferredValues', 'Citations', 'Comments', 'FamilyData',
'RecordStatus', 'ClinicalSignificance', 'ReviewStatus', 'Description', 'DateLastEvaluated'
]
def extract_variant_data(clinvar_set):
"""Extract all required fields from a ClinVarSet element into a dictionary."""
ref_assertion = clinvar_set.find('ReferenceClinVarAssertion')
if ref_assertion is None:
print("Warning: No ReferenceClinVarAssertion found in ClinVarSet")
return None
# Basic fields from ReferenceClinVarAssertion
record_status = ref_assertion.findtext('RecordStatus', default='')
# Classification details
germline_classification = ref_assertion.find('Classifications/GermlineClassification')
if germline_classification is not None:
review_status = germline_classification.findtext('ReviewStatus', default='')
description_elem = germline_classification.find('Description')
description = description_elem.text if description_elem is not None else ''
date_last_evaluated = description_elem.get('DateLastEvaluated', '') if description_elem is not None else ''
clinical_significance = description
else:
review_status, description, date_last_evaluated, clinical_significance = '', '', '', ''
# MeasureSet details
measure_set = ref_assertion.find('MeasureSet[@Type="Variant"]')
if measure_set is None:
print("Warning: No MeasureSet with Type='Variant' found")
return None
measure_set_id = measure_set.get('ID', '')
measure_set_acc = measure_set.get('Acc', '')
measure_set_version = measure_set.get('Version', '')
measure = measure_set.find('Measure')
if measure is None:
print("Warning: No Measure found in MeasureSet")
return None
allele_id = measure.get('ID', '')
measure_type = measure.get('Type', '')
canonical_spdi = measure.findtext('CanonicalSPDI', default='')
# Sequence Locations for GRCh37 and GRCh38
sequence_locations = {}
for seq_loc in measure.findall('SequenceLocation'):
assembly = seq_loc.get('Assembly')
if assembly in ['GRCh37', 'GRCh38']:
sequence_locations[assembly] = {
'Chromosome': seq_loc.get('Chr', ''),
'PositionVCF': seq_loc.get('positionVCF', ''),
'ReferenceAlleleVCF': seq_loc.get('referenceAlleleVCF', ''),
'AlternateAlleleVCF': seq_loc.get('alternateAlleleVCF', ''),
'Start': seq_loc.get('start', ''),
'Stop': seq_loc.get('stop', '')
}
# Attributes (MolecularConsequence, FunctionalConsequence)
attributes = {}
for attr in measure.findall('AttributeSet/Attribute'):
attr_type = attr.get('Type')
if attr_type in ['MolecularConsequence', 'FunctionalConsequence']:
attributes[attr_type] = attr.text or ''
# Lists joined with semicolons
mode_of_inheritance = [attr.text for attr in ref_assertion.findall('AttributeSet/Attribute[@Type="ModeOfInheritance"]') if attr.text]
preferred_values = [elem.text for elem in measure.findall('Name/ElementValue[@Type="Preferred"]') if elem.text]
citations = [cit.findtext('ID') for cit in clinvar_set.findall('.//Citation') if cit.find('ID') is not None]
comments = [com.text for com in clinvar_set.findall('.//Comment') if com.text]
family_data = [fd.get('NumFamilies', '') for fd in clinvar_set.findall('.//FamilyData') if fd.get('NumFamilies')]
# Construct the row dictionary
row = {
'MeasureSetID': measure_set_id,
'MeasureSetAcc': measure_set_acc,
'MeasureSetVersion': measure_set_version,
'AlleleID': allele_id,
'MeasureType': measure_type,
'CanonicalSPDI': canonical_spdi,
'GRCh37_Chromosome': sequence_locations.get('GRCh37', {}).get('Chromosome', ''),
'GRCh37_PositionVCF': sequence_locations.get('GRCh37', {}).get('PositionVCF', ''),
'GRCh37_ReferenceAlleleVCF': sequence_locations.get('GRCh37', {}).get('ReferenceAlleleVCF', ''),
'GRCh37_AlternateAlleleVCF': sequence_locations.get('GRCh37', {}).get('AlternateAlleleVCF', ''),
'GRCh37_Start': sequence_locations.get('GRCh37', {}).get('Start', ''),
'GRCh37_Stop': sequence_locations.get('GRCh37', {}).get('Stop', ''),
'GRCh38_Chromosome': sequence_locations.get('GRCh38', {}).get('Chromosome', ''),
'GRCh38_PositionVCF': sequence_locations.get('GRCh38', {}).get('PositionVCF', ''),
'GRCh38_ReferenceAlleleVCF': sequence_locations.get('GRCh38', {}).get('ReferenceAlleleVCF', ''),
'GRCh38_AlternateAlleleVCF': sequence_locations.get('GRCh38', {}).get('AlternateAlleleVCF', ''),
'GRCh38_Start': sequence_locations.get('GRCh38', {}).get('Start', ''),
'GRCh38_Stop': sequence_locations.get('GRCh38', {}).get('Stop', ''),
'MolecularConsequence': attributes.get('MolecularConsequence', ''),
'FunctionalConsequence': attributes.get('FunctionalConsequence', ''),
'ModeOfInheritance': ';'.join(mode_of_inheritance),
'PreferredValues': ';'.join(preferred_values),
'Citations': ';'.join(citations),
'Comments': ';'.join(comments),
'FamilyData': ';'.join(family_data),
'RecordStatus': record_status,
'ClinicalSignificance': clinical_significance,
'ReviewStatus': review_status,
'Description': description,
'DateLastEvaluated': date_last_evaluated
}
return row
def parse_variant_xml(xml_file, tsv_file):
"""Parse the XML file and write variants to a TSV with progress tracking."""
print(f"Starting to parse XML file: {xml_file}")
print(f"File size: {os.path.getsize(xml_file) / (1024**3):.2f} GB")
with open(xml_file, 'rb') as f, open(tsv_file, 'w', newline='') as tsvfile:
# Set up TSV writer
writer = csv.DictWriter(tsvfile, fieldnames=columns, delimiter='\t')
writer.writeheader()
# Get total file size for progress tracking
total_size = os.path.getsize(xml_file)
context = ET.iterparse(f, events=('end',), tag='ClinVarSet')
count = 0
# Wrap the parsing loop with tqdm for a progress bar
with tqdm(total=total_size, unit='B', unit_scale=True, desc="Parsing ClinVar XML") as pbar:
for event, elem in context:
if elem.tag == 'ClinVarSet':
count += 1
if count % 10000 == 0:
print(f"Processed {count} ClinVarSet elements")
row = extract_variant_data(elem)
if row:
writer.writerow(row)
# Clear memory
elem.clear()
while elem.getprevious() is not None:
del elem.getparent()[0]
# Update progress bar based on current file position
current_pos = f.tell()
pbar.n = current_pos # Update to current position
pbar.refresh()
print(f"\nFinished parsing. Total ClinVarSet elements processed: {count}")
def main():
xml_file = 'ClinVarRCVRelease_00-latest.xml'
tsv_file = 'output.tsv'
try:
parse_variant_xml(xml_file, tsv_file)
print(f"Parsing completed successfully. Output written to {tsv_file}")
except FileNotFoundError:
print(f"Error: The file '{xml_file}' was not found. Please check the file path.")
except ET.ParseError as e:
print(f"Error parsing XML: {e}. The file may be corrupted or not valid XML.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == '__main__':
main()