-
Notifications
You must be signed in to change notification settings - Fork 25
/
PointFinderIO.py
68 lines (57 loc) · 2.13 KB
/
PointFinderIO.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
64
65
66
67
68
#!/usr/bin/env python
import csv
from .Interfaces import hAMRonizedResultIterator
from hAMRonization.constants import NUCLEOTIDE_VARIANT, AMINO_ACID_VARIANT
required_metadata = [
"analysis_software_version",
"reference_database_version",
"input_file_name",
]
class PointFinderIterator(hAMRonizedResultIterator):
"""
Updated for ResFinder v4.1 using the `PointFinder_results.txt` output
file
"""
# Mutation
# Nucleotide change
# Amino acid change
# Resistance
# PMID
def __init__(self, source, metadata):
metadata["reference_database_name"] = "pointfinder"
metadata["analysis_software_name"] = "pointfinder"
# even though resfinderv4 runs pointfinder
# parsing mutational resistance requires parsing a different file
# to get gene presence absence
self.metadata = metadata
self.field_mapping = {
"Mutation": "reference_accession",
"Nucleotide change": "nucleotide_mutation",
"Amino acid change": "amino_acid_mutation",
"Resistance": "drug_class",
"PMID": None,
"_type": "genetic_variation_type",
"_gene_symbol": "gene_symbol",
"_gene_name": "gene_name",
}
super().__init__(source, self.field_mapping, self.metadata)
def parse(self, handle):
"""
Read each and return it
"""
reader = csv.DictReader(handle, delimiter="\t")
for result in reader:
gene, mutation = result["Mutation"].split()
result["_gene_symbol"] = gene
result["_gene_name"] = gene
if mutation.startswith("r."):
result["_type"] = NUCLEOTIDE_VARIANT
result["Nucleotide change"] = gene
result["Amino acid change"] = None
elif mutation.startswith("p."):
result["_type"] = AMINO_ACID_VARIANT
result["Amino acid change"] = mutation
else:
raise ValueError(f"Mutation type of {result} not recognised")
yield self.hAMRonize(result, self.metadata)
result = {}