-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathorthology_adapter.py
More file actions
505 lines (415 loc) · 16.2 KB
/
orthology_adapter.py
File metadata and controls
505 lines (415 loc) · 16.2 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
from __future__ import annotations
from pypath.share import curl, settings
from pypath.inputs import oma, uniprot, pharos
from pypath.utils import taxonomy
from typing import Union
from contextlib import ExitStack
from bioregistry import normalize_curie
from time import time
import os
import pandas as pd
import numpy as np
from enum import Enum, EnumMeta, IntEnum
from pydantic import BaseModel, DirectoryPath, validate_call
from biocypher._logger import logger
from tqdm import tqdm
logger.debug(f"Loading module {__name__}.")
class OrthologyEnumMeta(EnumMeta):
def __contains__(cls, item):
return item in cls.__members__.keys()
class OrthologyEdgeField(Enum, metaclass=OrthologyEnumMeta):
SOURCE = "source"
RELATION_TYPE = "relation_type"
OMA_ORTHOLOGY_SCORE = "oma_orthology_score"
@classmethod
def _missing_(cls, value: str):
value = value.lower()
for member in cls.__members__.values():
if member.value.lower() == value:
return member
return None
class OMAOrganismField(IntEnum, metaclass=OrthologyEnumMeta):
TAX_4932 = 4932 # s. cerevisiae
TAX_10090 = 10090 # mouse
TAX_3702 = 3702
TAX_10116 = 10116 # rat
TAX_559292 = 559292
TAX_9913 = 9913 # cow
TAX_1264690 = 1264690
TAX_83333 = 83333
TAX_6239 = 6239 # c. elegans
TAX_1423 = 1423
TAX_39947 = 39947
TAX_44689 = 44689
TAX_7227 = 7227 # drosophila
TAX_8355 = 8355 # Xenopus laevis
TAX_7955 = 7955 # zebrafish
TAX_9031 = 9031 # chicken
TAX_1773 = 1773
TAX_9598 = 9598 # chimp - APES TOGETHER STRONG
TAX_9544 = 9544 # Macaca - APES TOGETHER STRONG
TAX_9595 = 9595 # GORILLA GORILLA GORILLA - APES TOGETHER STRONG
TAX_9601 = 9601 # orangutan - APES TOGETHER STRONG
@classmethod
def _missing_(cls, value: str):
value = value.lower()
for member in cls.__members__.values():
if member.value.lower() == value:
return member
return None
class PharosOrganismField(Enum, metaclass=OrthologyEnumMeta):
MOUSE = "Mouse"
COW = "Cow"
XENOPUS = "Xenopus"
ZEBRAFISH = "Zebrafish"
RAT = "Rat"
C_ELEGANS = "C. elegans"
S_CEREVISIA = "S.cerevisiae"
CHICKEN = "Chicken"
CHIMP = "Chimp" # APES TOGETHER STRONG
FRUITFLY = "Fruitfly"
DOG = "Dog"
MACAQUE = "Macaque"
PIG = "Pig"
HORSE = "Horse"
@classmethod
def _missing_(cls, value: str):
value = value.lower()
for member in cls.__members__.values():
if member.value.lower() == value:
return member
return None
class OrthologyModel(BaseModel):
edge_fields: Union[list[OrthologyEdgeField], None] = None
oma_organisms: Union[list[OMAOrganismField], None] = None
pharos_organisms: Union[list[PharosOrganismField], None] = None
merge_with_pypath_taxids: bool = True
add_prefix: bool = True
test_mode: bool = False
export_csv: bool = False
output_dir: DirectoryPath | None = None
class Orthology:
"""
Class that downloads orthology data using pypath and reformats it to be ready
for import into a BioCypher database.
"""
def __init__(
self,
edge_fields: Union[list[OrthologyEdgeField], None] = None,
oma_organisms: Union[list[OMAOrganismField], None] = None,
pharos_organisms: Union[list[PharosOrganismField], None] = None,
merge_with_pypath_taxids: bool = True,
add_prefix: bool = True,
test_mode: bool = False,
export_csv: bool = False,
output_dir: DirectoryPath | None = None,
):
"""
Args:
edge_fields: Gene-gene orthology edge fields that will be included in graph, if defined it must be values of elements from OrthologyEdgeField enum class (not the names)
oma_organisms: list of taxanomy ids of organisms that will be compared against human to extract orthology relations, if defined it must be values of elements from OMAOrganismField enum class (not the names)
pharos_organisms: list of taxanomy names of organisms that will be compared against human to extract orthology relations, if defined it must be values of elements from PharosOrganismField enum class (not the names)
merge_with_pypath_taxids: Whether to merge `oma_organisms` list with list of pypath's tax ids
add_prefix: if True, add prefix to database identifiers
test_mode: if True, limits amount of output data
export_csv: if True, export data as csv
output_dir: Location of csv export, if not defined it will be current director. `export_csv` should be True for this arg.
"""
model = OrthologyModel(
edge_fields=edge_fields,
oma_organisms=oma_organisms,
pharos_organisms=pharos_organisms,
merge_with_pypath_taxids=merge_with_pypath_taxids,
add_prefix=add_prefix,
test_mode=test_mode,
export_csv=export_csv,
output_dir=output_dir,
).model_dump()
self.add_prefix = model["add_prefix"]
self.export_csv = model["export_csv"]
self.output_dir = model["output_dir"]
# set edge fields
self.set_edge_fields(edge_fields=model["edge_fields"])
# set organism lists
self.set_organism_lists(
oma_organisms=model["oma_organisms"],
pharos_organisms=model["pharos_organisms"],
merge_with_pypath_taxids=model["merge_with_pypath_taxids"],
)
# set early_stopping, if test_mode true
self.early_stopping = None
if model["test_mode"]:
self.early_stopping = 100
@validate_call
def download_orthology_data(
self,
cache: bool = False,
debug: bool = False,
retries: int = 3,
):
"""
Wrapper function to download orthology data from various databases using pypath.
Args
cache: if True, it uses the cached version of the data, otherwise
forces download.
debug: if True, turns on debug mode in pypath.
retries: number of retries in case of download error.
"""
with ExitStack() as stack:
stack.enter_context(settings.context(retries=retries))
if debug:
stack.enter_context(curl.debug_on())
if not cache:
stack.enter_context(curl.cache_off())
self.download_oma_data()
self.download_pharos_data()
def download_oma_data(self):
"""
Downloads orthology data from OMA against human.
"""
self.entry_name_to_uniprot = uniprot.uniprot_data(
field="id", reviewed=True, organism="*"
)
self.entry_name_to_uniprot = {
v: k for k, v in self.entry_name_to_uniprot.items()
}
uniprot_to_entrez = uniprot.uniprot_data(
field="xref_geneid", reviewed=True, organism="*"
)
self.uniprot_to_entrez = {}
for k, v in uniprot_to_entrez.items():
self.uniprot_to_entrez[k] = v.strip(";").split(";")[0]
logger.debug("Started downloading OMA orthology data")
t0 = time()
self.oma_orthology = []
for t in tqdm(self.oma_organisms):
tax_orthology = oma.oma_orthologs(organism_a=9606, organism_b=t)
tax_orthology = [
i
for i in tax_orthology
if i.a.id in self.entry_name_to_uniprot
and i.b.id in self.entry_name_to_uniprot
]
tax_orthology = [
i
for i in tax_orthology
if self.uniprot_to_entrez.get(
self.entry_name_to_uniprot[i.a.id], None
)
and self.uniprot_to_entrez.get(
self.entry_name_to_uniprot[i.b.id], None
)
]
self.oma_orthology.extend(tax_orthology)
logger.debug(f"Orthology data of tax id {t} is downloaded")
t1 = time()
logger.info(
f"OMA orthology data is downloaded in {round((t1-t0) / 60, 2)} mins"
)
def process_oma_data(self) -> pd.DataFrame:
"""
Processes orthology data from OMA.
"""
if not hasattr(self, "oma_orthology"):
self.download_oma_data()
logger.debug("Started processing OMA orthology data")
t0 = time()
df_list = [
(
self.uniprot_to_entrez[
self.entry_name_to_uniprot[ortholog.a.id]
],
self.uniprot_to_entrez[
self.entry_name_to_uniprot[ortholog.b.id]
],
ortholog.rel_type,
round(ortholog.score),
)
for ortholog in self.oma_orthology
]
oma_orthology_df = pd.DataFrame(
df_list,
columns=[
"entrez_a",
"entrez_b",
"relation_type",
"oma_orthology_score",
],
)
oma_orthology_df["source"] = "OMA"
oma_orthology_df.sort_values(
by="oma_orthology_score", ascending=False, inplace=True
)
oma_orthology_df = oma_orthology_df[
~oma_orthology_df[["entrez_a", "entrez_b"]]
.apply(frozenset, axis=1)
.duplicated()
].reset_index(drop=True)
t1 = time()
logger.info(
f"OMA orthology data is processed in {round((t1-t0) / 60, 2)} mins"
)
return oma_orthology_df
def download_pharos_data(self):
"""
Downloads orthology data from Pharos.
"""
logger.debug("Started downloading Pharos orthology data")
t0 = time()
uniprot_to_entrez = uniprot.uniprot_data(
field="xref_geneid", reviewed=True, organism="*"
)
self.entrez_to_uniprot = {}
for k, v in uniprot_to_entrez.items():
for entrez in v.strip(";").split(";"):
if entrez:
self.entrez_to_uniprot[entrez] = k
self.pharos_orthology_init = pharos.pharos_targets(orthologs=True)
t1 = time()
logger.info(
f"Pharos orthology data is downloaded in {round((t1-t0) / 60, 2)} mins"
)
def process_pharos_data(self) -> pd.DataFrame:
"""
Processes orthology data from Pharos.
"""
if not hasattr(self, "pharos_orthology_init"):
self.download_pharos_data()
logger.debug("Started processing Pharos orthology data")
t0 = time()
df_list = []
for protein in self.pharos_orthology_init:
if protein["orthologs"]:
for ortholog in protein["orthologs"]:
if (
ortholog["geneid"]
and str(ortholog["geneid"]) in self.entrez_to_uniprot
and str(ortholog["species"]) in self.pharos_organisms
and protein["uniprot"] in self.uniprot_to_entrez
):
df_list.append(
(
self.uniprot_to_entrez[protein["uniprot"]],
str(ortholog["geneid"]),
)
)
pharos_orthology_df = pd.DataFrame(
df_list, columns=["entrez_a", "entrez_b"]
)
pharos_orthology_df["source"] = "Pharos"
pharos_orthology_df = pharos_orthology_df[
~pharos_orthology_df[["entrez_a", "entrez_b"]]
.apply(frozenset, axis=1)
.duplicated()
].reset_index(drop=True)
t1 = time()
logger.info(
f"Pharos orthology data is processed in {round((t1-t0) / 60, 2)} mins"
)
return pharos_orthology_df
def merge_orthology_data(self) -> pd.DataFrame:
"""
Merges orthology data from OMA and Pharos
"""
logger.debug("Started merged OMA and Pharos orthology data")
t0 = time()
pharos_orthology_df = self.process_pharos_data()
oma_orthology_df = self.process_oma_data()
oma_plus_pharos_orthology_df = oma_orthology_df.merge(
pharos_orthology_df, how="outer", on=["entrez_a", "entrez_b"]
)
oma_plus_pharos_orthology_df["source"] = oma_plus_pharos_orthology_df[
["source_x", "source_y"]
].apply(self.merge_source_column, axis=1)
oma_plus_pharos_orthology_df.drop(
columns=["source_x", "source_y"], inplace=True
)
t1 = time()
logger.info(
f"OMA and Pharos orthology data is merged in {round((t1-t0) / 60, 2)} mins"
)
if self.export_csv:
if self.output_dir:
full_path = os.path.join(self.output_dir, "Orthology.csv")
else:
full_path = os.path.join(os.getcwd(), "Orthology.csv")
oma_plus_pharos_orthology_df.to_csv(full_path, index=False)
logger.info(f"Orthology data is written: {full_path}")
return oma_plus_pharos_orthology_df
@validate_call
def get_orthology_edges(
self, label: str = "gene_is_orthologous_with_gene"
) -> list[tuple]:
"""
Reformats orthology data to be ready for import into a BioCypher database.
"""
all_orthology_df = self.merge_orthology_data()
# define edge list
edge_list = []
logger.info("Preparing orthology edges.")
for index, row in tqdm(
all_orthology_df.iterrows(), total=all_orthology_df.shape[0]
):
_dict = row.to_dict()
source = self.add_prefix_to_id("ncbigene", _dict["entrez_a"])
target = self.add_prefix_to_id("ncbigene", _dict["entrez_b"])
del _dict["entrez_a"], _dict["entrez_b"]
props = {}
for k, v in _dict.items():
if k in self.edge_fields and str(v) != "nan":
if isinstance(v, str) and "|" in v:
props[str(k).replace(" ", "_").lower()] = v.replace(
"'", "^"
).split("|")
else:
props[str(k).replace(" ", "_").lower()] = str(
v
).replace("'", "^")
edge_list.append((None, source, target, label, props))
if self.early_stopping and (index + 1) == self.early_stopping:
break
return edge_list
def merge_source_column(self, element, joiner="|"):
"""
Merges source columns' entries in selected dataframes
"""
_list = []
for e in list(element.dropna().values):
if joiner in e:
_list.extend(iter(e.split(joiner)))
else:
_list.append(e)
return joiner.join(list(dict.fromkeys(_list).keys()))
@validate_call
def add_prefix_to_id(
self, prefix: str, identifier: str, sep: str = ":"
) -> str:
"""
Adds prefix to ids
"""
if self.add_prefix and identifier:
return normalize_curie(prefix + sep + identifier)
return identifier
def set_edge_fields(self, edge_fields):
if edge_fields:
self.edge_fields = edge_fields
else:
self.edge_fields = [field.value for field in OrthologyEdgeField]
def set_organism_lists(
self, oma_organisms, pharos_organisms, merge_with_pypath_taxids
):
# define oma organisms
if oma_organisms:
self.oma_organisms = {field.value for field in oma_organisms}
else:
self.oma_organisms = {field.value for field in OMAOrganismField}
# merge organisms with list of pypath taxids
if merge_with_pypath_taxids:
self.oma_organisms = self.oma_organisms.union(
set(taxonomy.taxids.keys())
)
# define pharos organisms
if pharos_organisms:
self.pharos_organisms = [field.value for field in pharos_organisms]
else:
self.pharos_organisms = [field.value for field in PharosOrganismField]