-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenes.py
284 lines (228 loc) · 8.66 KB
/
Genes.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/python
import MySQLdb
import warnings
from MySQLdb import connections
class Gene:
__output_key = "eid"
def __init__(self, eid, db):
try:
self.eid = int(eid)
except ValueError:
raise ValueError("Entrez ID must be an integer")
if not isinstance(db, GeneDB):
raise TypeError("db must be a GeneDB object, not %s" % type(db))
self.db = db
self.cross = {}
@staticmethod
def set_output_source(source):
"""Defines what should be displayed when converting the gene to a
string.
Parameters
----------
source : The key to use for the Gene's output. Same as __get__
"""
Gene.__output_key = source
def __repr__(self):
return str(self[Gene.__output_key])
__str__ = __repr__
def __getitem__(self, key):
if key == "eid":
return self.eid
elif key == "symbol":
try:
return self.symbol
except AttributeError:
if self.db is not None:
self.symbol = self.db.get_symbol(self.eid)
return self.symbol
else:
raise KeyError("Gene does not have a database " + \
"connection to look up its symbol")
else:
if key not in self.cross:
if self.db is not None:
self.cross[key] = self.db.get_cross_id(self.eid, key)
else:
raise KeyError("Gene does not have a database " + \
"connection to look up its symbol")
return self.cross[key]
def __eq__(self, other):
return self["eid"] == other["eid"]
def __ne__(self, other):
return self["eid"] != other["eid"]
def __hash__(self):
return hash((type(self), self["eid"]))
class GeneDB:
def __init__(self, *args, **kwargs):
"""Stores a MySQLdb connection statically within the class. This is
a shortcut for users so they don't have to constantly pass connection/
cursor objects to methods over and over.
Paramters
---------
conn : a MySQLdb connection
OR
host : the host location of the database
db : the name of the database
user : the username for database access
passwd : the password associated with the use
"""
if len(args) == 1 or "conn" in kwargs:
conn = args[0] if len(args) == 1 else kwargs["conn"]
if type(args[0]) is connections.Connection:
self.conn = conn
else:
raise TypeError("connect() requires a MySQLdb.conn object")
elif len(args) == 4:
self.conn = MySQLdb.connect(
host=args[0],
db=args[1],
user=args[2],
passwd=args[3])
else:
try:
self.conn = MySQLdb.connect(
host=kwargs["host"],
db=kwargs["db"],
user=kwargs["user"],
passwd=kwargs["passwd"])
except KeyError:
raise TypeError("You must pass host, db, user, and passwd " + \
"to connect()")
self.cursor = self.conn.cursor()
self.gene_cache = {}
self.source_cache = {}
def get_gene(self, gene_id, source="eid"):
"""Returns a Gene object from the given ID and type.
Parameters
----------
gene_id : A gene identifier
source : The source of the identifier. Valid options are eid,
symbol, or any cross database name in the gene_xrefs table.
Returns
-------
A Gene object
"""
gene_id = str(gene_id)
try:
valid_gene = self.gene_cache[self.source_cache[source][gene_id]]
return valid_gene
except KeyError:
pass
valid_eid = None
if source == "eid":
try:
eid = int(gene_id)
except ValueError:
raise ValueError("gene_id must be an integer if source " + \
"is \"Entrez\"")
self.cursor.execute("""
SELECT EXISTS(
SELECT *
FROM genes
WHERE entrez_id = %(eid)s
)""", {'eid': eid})
if self.cursor.fetchone()[0] == 1:
valid_eid = eid
else:
self.cursor.execute("""
SELECT entrez_id
FROM discontinued_genes
WHERE discontinued_id = %(eid)s""", {'eid': eid})
row = self.cursor.fetchone()
if row is not None:
valid_eid = row[0]
else:
raise KeyError("Entrez ID %d was not found in the database" % eid)
elif source == "symbol":
args = {"symbol": gene_id}
self.cursor.execute("""
SELECT entrez_id
FROM genes
WHERE symbol = %(symbol)s""", args)
row = self.cursor.fetchone()
if row is not None:
valid_eid = row[0]
else:
self.cursor.execute("""
SELECT entrez_id
FROM discontinued_genes
WHERE discontinued_symbol = %(symbol)s""", args)
row = self.cursor.fetchone()
if row is not None:
valid_eid = row[0]
else:
self.cursor.execute("""
SELECT entrez_id
FROM gene_synonyms
WHERE symbol = %(symbol)s""", args)
row = self.cursor.fetchone()
if row is not None:
valid_eid = row[0]
else:
raise KeyError("Symbol %s not found in the database" % gene_id)
else:
self.cursor.execute("""
SELECT entrez_id
FROM gene_xrefs
WHERE Xref_db = %(db)s
AND Xref_id = %(id)s""", {'db': source, 'id': gene_id})
row = self.cursor.fetchone()
if row is not None:
valid_eid = row[0]
else:
raise KeyError(("Gene ID %s from source %s was not found " + \
"in the database") % (gene_id, source))
if valid_eid is None:
raise KeyError("Unable to find a valid Entrez ID for %s from %s" % (gene_id, source))
valid_eid = int(valid_eid)
if source not in self.source_cache:
self.source_cache[source] = {}
self.source_cache[source][gene_id] = valid_eid
self.gene_cache[valid_eid] = Gene(valid_eid, self)
return self.gene_cache[valid_eid]
def get_cross_id(self, entrez_id, xref_db):
"""Finds a gene's identifier from an outside database based on its
Entrez ID.
Paramters
---------
entrez_id : An Entrez ID for a gene
xref_db : The name of the external database to query from
Returns
-------
A string containing the foreign identifier
"""
try:
entrez_id = int(entrez_id)
except ValueError:
raise ValueError("entrez_id must be an integer")
self.cursor.execute("""
SELECT entrez_id
FROM gene_xrefs
WHERE Xref_db = %(db)s
AND entrez_id = %(eid)s""", {'db': xref_db, 'eid': entrez_id})
row = self.cursor.fetchone()
if row is not None:
return row[0]
raise KeyError("Unable to find an external identifer for database " + \
"%s using Entrez ID %d" % (xref_db, entrez_id))
def get_symbol(self, entrez_id):
"""Finds a gene's official symbol (if it has one) given its Entrez ID.
Parameters
----------
entrez_id : A gene's Entrez ID
Returns
-------
The gene's symbol (if it has one)
"""
try:
entrez_id = int(entrez_id)
except ValueError:
raise ValueError("entrez_id must be an integer")
self.cursor.execute("""
SELECT symbol
FROM genes
WHERE entrez_id = %(eid)s""", {'eid': entrez_id})
row = self.cursor.fetchone()
if row is not None:
return row[0]
raise KeyError("Entrez ID %d was not found in the database" % entrez_id)