-
Notifications
You must be signed in to change notification settings - Fork 43
/
cellphonedb.py
359 lines (270 loc) · 8.77 KB
/
cellphonedb.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of the `pypath` python module
#
# Copyright 2014-2023
# EMBL, EMBL-EBI, Uniklinik RWTH Aachen, Heidelberg University
#
# Authors: see the file `README.rst`
# Contact: Dénes Türei (turei.denes@gmail.com)
#
# Distributed under the GPLv3 License.
# See accompanying file LICENSE.txt or copy at
# https://www.gnu.org/licenses/gpl-3.0.html
#
# Website: https://pypath.omnipathdb.org/
#
from future.utils import iteritems
from past.builtins import xrange, range
import re
import csv
import collections
import itertools
import pypath.share.curl as curl
import pypath.resources.urls as urls
import pypath.utils.mapping as mapping
import pypath.internals.intera as intera
import pypath.share.common as common
import pypath.share.session as session
import pypath.inputs.uniprot_db as uniprot_db
_logger = session.Logger(name = 'cellphonedb_input')
_log = _logger._log
CellPhoneDBAnnotation = collections.namedtuple(
'CellPhoneDBAnnotation',
(
'receptor',
'receptor_class',
'peripheral',
'secreted',
'secreted_class',
'transmembrane',
'integrin',
)
)
def cellphonedb_ligands_receptors():
"""
Retrieves the set of ligands and receptors from CellPhoneDB.
Returns tuple of sets.
"""
receptors = set()
ligands = set()
proteins = cellphonedb_protein_annotations()
complexes = cellphonedb_complex_annotations()
for _id, annot in itertools.chain(
iteritems(proteins),
iteritems(complexes)
):
if annot.receptor:
receptors.add(_id)
if annot.secreted or (
not annot.receptor and (
annot.transmembrane or
annot.peripheral
)
):
ligands.add(_id)
return ligands, receptors
def _cellphonedb_annotations(url, name_method):
replacements = {
'_add': '',
' | ': ',',
' ': '_',
}
def get_bool(rec, attr):
return attr in rec and rec[attr] == 'True'
def get_desc(rec, attr):
desc = '%s_desc' % attr
value = (
''
if (
attr in rec and rec[attr] == 'False' or
attr not in rec and not rec[desc]
) else
rec[desc]
if rec[desc] else
attr
)
for pattern, repl in iteritems(replacements):
value = value.replace(pattern, repl)
value = value.lower().split(',') if value else None
return tuple(sorted(common.to_set(value)))
record = CellPhoneDBAnnotation
annot = {}
c = curl.Curl(url, large = True, silent = False)
tab = list(csv.DictReader(c.result))
for rec in tab:
names = name_method(rec)
if isinstance(names, (str, intera.Complex)):
names = (names,)
for name in names:
annot[name] = record(
receptor = get_bool(rec, 'receptor'),
receptor_class = get_desc(rec, 'receptor'),
peripheral = get_bool(rec, 'peripheral'),
secreted = get_bool(rec, 'secreted'),
secreted_class = get_desc(rec, 'secreted'),
transmembrane = get_bool(rec, 'transmembrane'),
integrin = get_bool(rec, 'integrin'),
)
return annot
def cellphonedb_protein_annotations(add_complex_annotations = False):
"""
:arg bool add_complex_annotations:
Deprecated because results wrong annotations.
Copy the annotations of complexes to each of their member proteins.
"""
def name_method(rec):
uniprot = rec['uniprot']
uniprot = _cellphonedb_hla(uniprot)
uniprot = mapping.map_names(uniprot, 'uniprot', 'uniprot')
return uniprot
protein_annotations = _cellphonedb_annotations(
url = urls.urls['cellphonedb_git']['proteins'],
name_method = name_method,
)
return protein_annotations
def _cellphonedb_hla(uniprot):
"""
Returns *set*.
"""
uniprots = None
# for HLA genes in the uniprot column we have "HLA..." gene symbols
# but not always in the standard format (with dash)
if uniprot.startswith('HLA') and '-' not in uniprot:
genesymbol = 'HLA-%s' % uniprot[3:]
uniprots = mapping.map_name(genesymbol, 'genesymbol', 'uniprot')
return uniprots or {uniprot}
def cellphonedb_complex_annotations():
def get_uniprots(rec):
return tuple(
uniprot
for uniprot in
(rec['uniprot_%u' % i] for i in xrange(1, 5))
if uniprot
)
def get_stoichiometry(rec):
if not rec['stoichiometry']:
return get_uniprots(rec)
return tuple(
mapping.map_name0(genesymbol, 'genesymbol', 'uniprot')
for genesymbol in rec['stoichiometry'].split(';')
)
def name_method(rec):
comp = get_stoichiometry(rec)
cplex = intera.Complex(
name = rec['complex_name'],
components = comp,
sources = 'CellPhoneDB',
ids = rec['complex_name'],
)
return cplex
return _cellphonedb_annotations(
url = urls.urls['cellphonedb_git']['complexes'],
name_method = name_method,
)
def _cellphonedb_get_entity(name, complexes):
if name in complexes:
return (complexes[name],)
if '_by' in name:
_log(f'Ignoring entity: `{name}`.')
return ()
if ':' in name:
name = name.split(':')[1]
if '_' in name:
name = mapping.map_name0(name, 'name-entry', 'name')
if not uniprot_db.is_uniprot(name):
uniprot = mapping.map_name0(name, 'genesymbol', 'uniprot')
name = uniprot or name
name = _cellphonedb_hla(name)
return (name,) if isinstance(name, str) else name
def cellphonedb_interactions():
"""
Interactions between ligands and receptors from CellPhoneDB.
Yields:
Named tuples representing interactions.
"""
def get_type(entity):
return (
'ligand'
if entity in ligands else
'receptor'
if entity in receptors else
'unknown'
)
CellphonedbInteraction = collections.namedtuple(
'CellphonedbInteraction',
[
'id_a',
'id_b',
'sources',
'references',
'interaction_type',
'type_a',
'type_b',
]
)
repmid = re.compile(r'PMID: ([0-9]+)')
recomma = re.compile(r'[,;]')
ligands, receptors = cellphonedb_ligands_receptors()
complexes = dict(
(
_id,
cplex
)
for cplex in cellphonedb_complexes().values()
for _id in cplex.ids['CellPhoneDB']
)
url = urls.urls['cellphonedb_git']['interactions']
c = curl.Curl(url, silent = False, large = True)
reader = csv.DictReader(c.result)
for rec in reader:
_partner_a = _cellphonedb_get_entity(
rec['partner_a'],
complexes = complexes,
)
_partner_b = _cellphonedb_get_entity(
rec['partner_b'],
complexes = complexes,
)
for partner_a, partner_b in itertools.product(_partner_a, _partner_b):
type_a = get_type(partner_a)
type_b = get_type(partner_b)
rev = type_b == 'ligand' and type_a == 'receptor'
_type_a = type_b if rev else type_a
_type_b = type_a if rev else type_b
sources = (
'CellPhoneDB'
if rec['annotation_strategy'] == 'curated' else
'%s;CellPhoneDB' % (
';'.join(
recomma.split(
rec['annotation_strategy'].replace(
'guidetopharmacology.org',
'Guide2Pharma'
)
)
)
)
)
refs = ';'.join(repmid.findall(rec['source']))
yield (
CellphonedbInteraction(
id_a = partner_b if rev else partner_a,
id_b = partner_a if rev else partner_b,
sources = sources,
references = refs,
interaction_type = '%s-%s' % (_type_a, _type_b),
type_a = _type_a,
type_b = _type_b,
)
)
def cellphonedb_complexes():
annot = cellphonedb_complex_annotations()
complexes = {}
for cplex in annot.keys():
key = cplex.__str__()
if key in annot:
cplex.add_attr('CellPhoneDB', annot[key])
complexes[key] = cplex
return complexes