pypsicquic 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pypsicquic-0.1.0/PKG-INFO +15 -0
- pypsicquic-0.1.0/pyproject.toml +17 -0
- pypsicquic-0.1.0/pypsicquic/__init__.py +233 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pypsicquic
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary:
|
|
5
|
+
Author: glaunay
|
|
6
|
+
Author-email: pitooon@gmail.com
|
|
7
|
+
Requires-Python: >=3.9,<4.0
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Requires-Dist: networkx (>=2.8.7,<3.0.0)
|
|
14
|
+
Requires-Dist: requests (>=2.28.1,<3.0.0)
|
|
15
|
+
Requires-Dist: scipy (>=1.9.3,<2.0.0)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "pypsicquic"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = ""
|
|
5
|
+
authors = ["glaunay <pitooon@gmail.com>"]
|
|
6
|
+
|
|
7
|
+
[tool.poetry.dependencies]
|
|
8
|
+
python = ">=3.9, <4.0"
|
|
9
|
+
requests = "^2.28.1"
|
|
10
|
+
networkx = "^2.8.7"
|
|
11
|
+
scipy = "^1.9.3"
|
|
12
|
+
|
|
13
|
+
[tool.poetry.dev-dependencies]
|
|
14
|
+
|
|
15
|
+
[build-system]
|
|
16
|
+
requires = ["poetry-core>=1.0.0"]
|
|
17
|
+
build-backend = "poetry.core.masonry.api"
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import xml.etree.ElementTree as ET
|
|
2
|
+
import requests
|
|
3
|
+
from sys import stderr
|
|
4
|
+
import networkx as nx
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
class Dtree:
|
|
8
|
+
def __init__(self):
|
|
9
|
+
self.data = {}
|
|
10
|
+
def add(self, a, b):
|
|
11
|
+
for _ in (a,b):
|
|
12
|
+
if not _ in self.data:
|
|
13
|
+
self.data[_] = set([])
|
|
14
|
+
self.data[a].add(b)
|
|
15
|
+
self.data[b].add(a)
|
|
16
|
+
def __getitem__(self, k):
|
|
17
|
+
return self.data[k]
|
|
18
|
+
|
|
19
|
+
def __bool__(self):
|
|
20
|
+
return bool(self.data)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class PsicquicStore:
|
|
24
|
+
def __init__(self, xml_file="registry.xml", proxy = None):
|
|
25
|
+
self._registry_url = url_registry = 'http://www.ebi.ac.uk/Tools/webservices/psicquic/registry/registry?action=ACTIVE&format=xml'
|
|
26
|
+
|
|
27
|
+
self.proxies = proxy
|
|
28
|
+
|
|
29
|
+
tree = ET.parse(xml_file)
|
|
30
|
+
root = tree.getroot()
|
|
31
|
+
ns = '{http://hupo.psi.org/psicquic/registry}'
|
|
32
|
+
self.services = { s.find(f"{ns}name").text : s.find(f"{ns}restUrl").text.replace(' ', '') \
|
|
33
|
+
for s in root.findall(f"{ns}service")\
|
|
34
|
+
}
|
|
35
|
+
self.curr_endpoint = self.services['IntAct']
|
|
36
|
+
def set_service(self, service_id, mute=False):
|
|
37
|
+
if not service_id in self.services:
|
|
38
|
+
raise ValueError(f"{service_id} not a valid provider")
|
|
39
|
+
|
|
40
|
+
self.curr_endpoint = self.services[service_id]
|
|
41
|
+
if not mute :
|
|
42
|
+
print(f"Active url set to {self.curr_endpoint}", file=stderr)
|
|
43
|
+
|
|
44
|
+
def find_source(self, **kwargs):
|
|
45
|
+
""" Test each service for a non empty response on asked miql query
|
|
46
|
+
"""
|
|
47
|
+
furbished_service = []
|
|
48
|
+
for service_id in self.services:
|
|
49
|
+
self.set_service(service_id, mute=True)
|
|
50
|
+
maybe_psq_view = self.get(mute=True, **kwargs)
|
|
51
|
+
if maybe_psq_view:
|
|
52
|
+
furbished_service.append( (service_id, len(maybe_psq_view)) )
|
|
53
|
+
return furbished_service
|
|
54
|
+
|
|
55
|
+
def _urlget(self, url):
|
|
56
|
+
resp = requests.get(url, proxies=self.proxies)
|
|
57
|
+
if not resp.ok:
|
|
58
|
+
raise ValueError(f"Url Fetching error {resp.status_code} at {url}")
|
|
59
|
+
return resp
|
|
60
|
+
|
|
61
|
+
def get(self, mute = False, **kwargs):
|
|
62
|
+
if "pmid" in kwargs:
|
|
63
|
+
url = f"{self.curr_endpoint}query/pubmed:{kwargs['pmid']}"
|
|
64
|
+
if not mute:
|
|
65
|
+
print(url, file=stderr)
|
|
66
|
+
ans = self._urlget(url)
|
|
67
|
+
if not ans.text:
|
|
68
|
+
return None
|
|
69
|
+
return PsicquicView(raw=ans.text)
|
|
70
|
+
## too long
|
|
71
|
+
"""
|
|
72
|
+
if "species" in kwargs:
|
|
73
|
+
url = f"{self.curr_endpoint}query/species:{kwargs['species']}"
|
|
74
|
+
print(url, file=stderr)
|
|
75
|
+
ans = self._urlget(url)
|
|
76
|
+
return self.viewer(ans.text)
|
|
77
|
+
"""
|
|
78
|
+
def get_from(self, mol_ids, steps=1):
|
|
79
|
+
""" Build the PsicquicView for the partners of the provided proteins
|
|
80
|
+
The steps parameter controls the diameter of the partners shells, the depth of the search.
|
|
81
|
+
By default steps equals 1, which retuns only the 1st neighbours of the input proteins.
|
|
82
|
+
"""
|
|
83
|
+
mitab_set = self._get_from(set(mol_ids), set(), set(), steps - 1)
|
|
84
|
+
return PsicquicView(psq_datum_iter=mitab_set)
|
|
85
|
+
|
|
86
|
+
def _get_from(self, mol_ids, prev_mol_ids, psq_datum_set, steps):
|
|
87
|
+
def get_psq_datum_set(url):
|
|
88
|
+
print(url)
|
|
89
|
+
ans = self._urlget(url)
|
|
90
|
+
return set([ PsicquicDatum(l.split("\t")) for l in ans.text.split("\n") if l ])
|
|
91
|
+
def chunks(xs, n):
|
|
92
|
+
xs = list(xs)
|
|
93
|
+
n = max(1, n)
|
|
94
|
+
return (xs[i:i+n] for i in range(0, len(xs), n))
|
|
95
|
+
|
|
96
|
+
# exclude partners already known
|
|
97
|
+
black = Dtree()
|
|
98
|
+
for psq_datum in psq_datum_set:
|
|
99
|
+
e = psq_datum.interactors
|
|
100
|
+
black.add(*e)
|
|
101
|
+
# collect new interactions
|
|
102
|
+
maybe_new_psq_datum = set()
|
|
103
|
+
for mol_id in mol_ids:
|
|
104
|
+
if not black:
|
|
105
|
+
url = f"{self.curr_endpoint}query/id:{mol_id}"
|
|
106
|
+
maybe_new_psq_datum |= get_psq_datum_set(url)
|
|
107
|
+
continue
|
|
108
|
+
|
|
109
|
+
for black_mol_ids in chunks(black[mol_id], 40):
|
|
110
|
+
url = f"{self.curr_endpoint}query/id:{mol_id} AND NOT ({' OR '.join(black_mol_ids)})"
|
|
111
|
+
maybe_new_psq_datum |= get_psq_datum_set(url)
|
|
112
|
+
|
|
113
|
+
# They should all be new
|
|
114
|
+
#new_psq_datum = maybe_new_psq_datum - psq_datum_set
|
|
115
|
+
new_psq_datum = maybe_new_psq_datum
|
|
116
|
+
if not new_psq_datum or steps == 0:
|
|
117
|
+
return new_psq_datum
|
|
118
|
+
|
|
119
|
+
# We could circle back to already knew molecules
|
|
120
|
+
maybe_new_mol_ids = set()
|
|
121
|
+
for psq_datum in new_psq_datum:
|
|
122
|
+
maybe_new_mol_ids.update(psq_datum.interactors)
|
|
123
|
+
new_mol_ids = maybe_new_mol_ids - mol_ids
|
|
124
|
+
print(f"search countdown {steps}, new molecule/interaction counts = {len(list(new_mol_ids))} {len(list(new_psq_datum))}")
|
|
125
|
+
next_new_psq_datum = \
|
|
126
|
+
self._get_from(new_mol_ids, prev_mol_ids | mol_ids, new_psq_datum | psq_datum_set, steps - 1)
|
|
127
|
+
return next_new_psq_datum | new_psq_datum
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
class PsicquicView:
|
|
131
|
+
def __init__(self, raw=None, io_wrapper=None, psq_datum_iter = None):
|
|
132
|
+
assert raw or psq_datum_iter or io_wrapper
|
|
133
|
+
if io_wrapper:
|
|
134
|
+
self.data = [ PsicquicDatum(l.replace("\n", "").split("\t")) for l in io_wrapper ]
|
|
135
|
+
elif raw:
|
|
136
|
+
self.data = [ PsicquicDatum(l.split("\t")) for l in raw.split("\n") ]
|
|
137
|
+
else:
|
|
138
|
+
self.data = [ _ for _ in psq_datum_iter]
|
|
139
|
+
|
|
140
|
+
def __iter__(self):
|
|
141
|
+
for psq_datum in self.data:
|
|
142
|
+
yield(psq_datum)
|
|
143
|
+
|
|
144
|
+
def __str__(self):
|
|
145
|
+
return '\n'.join([str(_) for _ in self.data])
|
|
146
|
+
|
|
147
|
+
def __getitem__(self, _slice):
|
|
148
|
+
_ = self.data[_slice]
|
|
149
|
+
return PsicquicView(psq_datum_iter=_)
|
|
150
|
+
|
|
151
|
+
def __repr__(self):
|
|
152
|
+
return str(self)
|
|
153
|
+
def __len__(self):
|
|
154
|
+
return len(self.data)
|
|
155
|
+
def filter(self, physical_only=True):
|
|
156
|
+
_ = [ psicquic_datum for psicquic_datum in self.data if psicquic_datum.is_physical ]
|
|
157
|
+
return PsicquicView(psq_datum_iter=_)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class PsicquicDatum:
|
|
161
|
+
def __init__(self, mitab_line_array):
|
|
162
|
+
self._data = mitab_line_array
|
|
163
|
+
def __getitem__(self, i):
|
|
164
|
+
return self._data[i]
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def interactors(self):
|
|
168
|
+
return (self._data[0], self._data[1]) \
|
|
169
|
+
if self._data[0] > self._data[1] \
|
|
170
|
+
else (self._data[1], self._data[0])
|
|
171
|
+
def __hash__(self):
|
|
172
|
+
#(iA, iB) = self.interactors
|
|
173
|
+
#return hash( iA + iB )
|
|
174
|
+
return hash(''.join(self._data))
|
|
175
|
+
def __eq__(self, other):
|
|
176
|
+
return hash(self) == hash(other)
|
|
177
|
+
def __ne__(self, other):
|
|
178
|
+
return hash(self) != hash(other)
|
|
179
|
+
def __str__(self):
|
|
180
|
+
return "\t".join(self._data)
|
|
181
|
+
def __repr__(self):
|
|
182
|
+
return str(self)
|
|
183
|
+
@property
|
|
184
|
+
def is_physical(self):
|
|
185
|
+
if len(self._data) >= 12:
|
|
186
|
+
return self._data[11] == "psi-mi:\"MI:0915\"(physical association)"
|
|
187
|
+
return False
|
|
188
|
+
|
|
189
|
+
class PsicquicNetwork:
|
|
190
|
+
def __init__(self):
|
|
191
|
+
pass
|
|
192
|
+
|
|
193
|
+
def load_psq(self, psq_datum_iter):
|
|
194
|
+
self.G = nx.Graph()
|
|
195
|
+
for psq_datum in psq_datum_iter:
|
|
196
|
+
node_pair = psq_datum.interactors
|
|
197
|
+
if not self.G.has_edge(*node_pair):
|
|
198
|
+
self.G.add_edge(*node_pair, psq_data=[])
|
|
199
|
+
self.G[ node_pair[0] ][ node_pair[1] ]['psq_data'].append(psq_datum)
|
|
200
|
+
|
|
201
|
+
def load_nx(self, G):
|
|
202
|
+
self.G = G
|
|
203
|
+
|
|
204
|
+
def find_nodes(self, expr):
|
|
205
|
+
re_pattern = re.compile(expr)
|
|
206
|
+
return [ n for n in self.G.nodes if re_pattern.match(str(n)) ]
|
|
207
|
+
|
|
208
|
+
def _subgraph(self, prev_nodes, c_nodes, left):
|
|
209
|
+
new_nodes = []
|
|
210
|
+
# get the new neighbourhood
|
|
211
|
+
for nc in c_nodes:
|
|
212
|
+
for nn in self.G.neighbors(nc):
|
|
213
|
+
if not (nn in prev_nodes or nn in c_nodes):
|
|
214
|
+
new_nodes.append(nn)
|
|
215
|
+
# stop condition
|
|
216
|
+
if not new_nodes or left == 1:
|
|
217
|
+
return new_nodes
|
|
218
|
+
# calling next expansion, w/ curr_nodes passed into previous and new neighbours as seed for next search
|
|
219
|
+
next_new = self._subgraph( prev_nodes + c_nodes, new_nodes, left-1)
|
|
220
|
+
return new_nodes + next_new
|
|
221
|
+
|
|
222
|
+
def subgraph(self, central_node_list, degree=1):
|
|
223
|
+
# could use https://networkx.org/documentation/stable/reference/classes/generated/networkx.Graph.degree.html
|
|
224
|
+
# to ctrl degree value
|
|
225
|
+
seed_nodes = [ _ for _ in central_node_list ]
|
|
226
|
+
new_nodes = self._subgraph([], seed_nodes, degree )
|
|
227
|
+
new_self = PsicquicNetwork()
|
|
228
|
+
new_self.load_nx(self.G.subgraph( seed_nodes + new_nodes) )
|
|
229
|
+
return new_self
|
|
230
|
+
|
|
231
|
+
def del_nodes(self, nx_nodes):
|
|
232
|
+
self.G.remove_nodes_from(nx_nodes)
|
|
233
|
+
|