-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneScraper.py
184 lines (144 loc) · 5.4 KB
/
GeneScraper.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from Bio import SeqFeature, Entrez
from collections import UserDict
from xml.etree import ElementTree
from typing import Union, Sized
def _deconstruct(item: Union[list, set, tuple]) -> Union[object, list, set, tuple]:
"""
Deconstruct iterable containers into single value if there is only one member of list,
otherwise, preserve iterable container.
Meant to break out values from containers when there is only one object. It cannot be assumed
that parsed files will always have one object in container for any 1 datatype. In `SeqFeature`,
containerization of a single object seems to only occur in the `qualifiers` container.
Args:
item: Any iterable container (i.e: tuple, list, set)
Returns:
sole member of container, or the container is untouched
"""
if len(item) == 1:
return item[0]
else:
return item
class GeneScraper(UserDict):
def __init__(self, gene: SeqFeature):
super().__init__()
# store for debugging
self.gene = gene
if gene.type == 'gene':
self.data = self._from_gene(gene)
elif gene.type == 'CDS':
self.data = self._from_cds(gene)
elif gene.type == 'ncRNA':
self.data = self._from_nc_rna(gene)
elif gene.type == 'mobile_element':
self.data = self._from_mobile_element(gene)
else:
self.data = self._from_gene(gene)
def __repr__(self):
return self.id
@property
def id(self) -> Union[str, int]:
if 'gene' in self.data.keys():
return self.data['gene']
elif 'name' in self.data.keys():
return self.data['name']
elif self.data['type'] == 'mobile_element':
return self.data['mobile_element_type']
@staticmethod
def _qualifiers(gene: SeqFeature) -> dict:
"""
Get generic `qualifiers` data
Keys include 'locus_tag', 'db_xref', 'name', 'product', and 'note'.
If any key is not found, it is skipped.
"""
keys = ('gene', 'gene_synonym', 'locus_tag', 'db_xref', 'name', 'product', 'note', 'mol_type')
data = {}
for key in keys:
if key in gene.qualifiers:
data[key] = _deconstruct(gene.qualifiers[key])
return data
@staticmethod
def _minimal_data(gene: SeqFeature) -> dict:
"""
Get generic `location` data and `type`
"""
data = {
'strand': gene.location.strand,
'type': gene.type,
'start': int(gene.location.start),
'end': int(gene.location.end),
}
return data
@staticmethod
def _from_gene(gene: SeqFeature) -> dict:
"""
Extract data from a feature.
This is called by default for any `SeqFeature` not caught by `__init__`
(i.e: 'misc_feature', 'tRNA', 'rRNA').
Args:
gene: feature to extract data from
Returns:
dict containing keys pulled from `SeqFeature`.
Serves as transient datatype before being added to `self.data`
"""
if gene: # get rid of static check warning
data = {}
data.update(GeneScraper._minimal_data(gene))
data.update(GeneScraper._qualifiers(gene))
return data
@staticmethod
def _from_cds(gene: SeqFeature) -> dict:
"""
Extract data from `cds` feature type
Args:
gene: feature to extract data from
Returns:
`CdsData` pulled from `SeqFeature`.
Serves as transient datatype before being added to `self.data`
"""
# TODO: in what case would there be more than one codon_start/codon_end/product?
keys = ('codon_start', 'transl_table', 'protein_id', 'translation')
data = {}
for key in keys:
if key in gene.qualifiers:
data[key] = _deconstruct(gene.qualifiers[key])
data.update(GeneScraper._from_gene(gene))
return data
@staticmethod
def _from_nc_rna(gene: SeqFeature) -> dict:
data = {
'ncRNA_class': _deconstruct(gene.qualifiers['ncRNA_class']),
}
data.update(GeneScraper._from_gene(gene))
return data
@staticmethod
def _from_mobile_element(gene: SeqFeature) -> dict:
data = {'mobile_element_type': _deconstruct(gene.qualifiers['mobile_element_type'])}
data.update(GeneScraper._from_gene(gene))
return data
@staticmethod
def _gene_metadata(gid: str) -> dict[str, str]:
"""
Get non-essential metadata not found in GenBank files by using ASN.1 spec.
Since querying NCBI is time-consuming, this function is not called by `__init__`
Args:
gid: GeneID
Returns:
dict containing `summary` and `title`
Todo:
Return a namedtuple
"""
with Entrez.efetch(db='gene', id=gid, retmode='xml') as handle:
parsed = ElementTree.parse(handle)
root = parsed.getroot()
try:
prot_desc = root[0].find('Entrezgene_prot').find('Prot-ref').find('Prot-ref_desc').text
except ValueError:
prot_desc = None
try:
summary = root[0].find('Entrezgene_summary').text
except ValueError:
summary = None
return {
'summary': summary,
'prot_desc': prot_desc,
}