-
Notifications
You must be signed in to change notification settings - Fork 0
/
complexComponents.py
338 lines (293 loc) · 15.6 KB
/
complexComponents.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
#! /usr/bin/env python3
import argparse
import collections
import distutils.util
import graphviz
import pathlib
import rdflib
import rdflib.namespace
from SPARQLWrapper import SPARQLWrapper, JSON
import uri_utils
def getComponentsDirectStoichiometry(dataSource, complexIdent, prefixesDict={}):
"""
Compute a list of dictionnaries of the complex direct components and their stoichiometric coefficients.
Parameters
----------
dataSource : str
Either the URL of the SPARQL endpoint or the path of the file.
complexIdent : str
Identifier (either CURIe or full URI) of the molecular complex.
prefixesDict : dict of {str : str}, default {}
Prefixes names and values.
Returns
-------
listComponentStoichios : list of {'complex':???, 'complexPart':???, 'stoichioValue':???, 'stoichioIdent':???}
List of dictionnaries of the complex components' URIs and their stoichiometric coefficients.
"""
queryPath = 'queries/template-complexComponentsWithStoichiometricCoefficient.rq'
sparqlQuery = pathlib.Path(queryPath).read_text().replace('$supplementaryPrefixes$',uri_utils.convertPrefixesDictToSPARQL(prefixesDict))
if complexIdent.startswith('http'):
sparqlQuery = sparqlQuery.replace('$complexValue$','<' + complexIdent + '>')
else:
sparqlQuery = sparqlQuery.replace('$complexValue$',complexIdent)
solutions = []
if dataSource.startswith("http"):
sparql = SPARQLWrapper(dataSource)
sparql.setQuery(sparqlQuery)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
currentSolution = {}
currentSolution['complex'] = uri_utils.getFullURI(result['complex']['value'], prefixesDict)
currentSolution['complexPart'] = uri_utils.getFullURI(result['complexPart']['value'], prefixesDict)
if 'stoichioValue' in result.keys():
currentSolution['stoichioValue'] = result['stoichioValue']['value']
else:
currentSolution['stoichioValue'] = "1"
if 'stoichioIdent' in result.keys():
currentSolution['stoichioIdent'] = result['stoichioIdent']['value']
else:
currentSolution['stoichioIdent'] = "UnknownStoichiometry"
solutions.append(currentSolution)
else:
# TODO: guess format from file extension
g = rdflib.Graph().parse(dataSource, format="text/turtle")
for result in g.query(sparqlQuery):
currentSolution = {}
currentSolution['complex'] = uri_utils.getFullURI(str(result['complex']), prefixesDict)
currentSolution['complexPart'] = uri_utils.getFullURI(str(result['complexPart']), prefixesDict)
currentSolution['stoichioValue'] = result['stoichioValue']
currentSolution['stoichioIdent'] = result['stoichioIdent']
solutions.append(currentSolution)
return solutions
def getComponentsStoichiometry(dataSource, complexIdent, prefixesDict={}):
"""
Compute a dictionnary of the complex (direct and indirect) components and their stoichiometric coefficients.
Parameters
----------
dataSource : str
Either the URL of the SPARQL endpoint or the path of the file.
complexIdent : str
Identifier (either CURIe or full URI) of the molecular complex.
prefixesDict : dict of {str : str}, default {}
Prefixes names and values.
Returns
-------
dictComponents : dict of {str : float}
Dictionnary of the complex components' URIs and their stoichiometric coefficients.
"""
solutions = getComponentsDirectStoichiometry(dataSource, complexIdent, prefixesDict)
if len(solutions) == 0:
return {uri_utils.getFullURI(complexIdent, prefixesDict): 1.0}
dictComponents = collections.defaultdict(int)
for currentSolution in solutions:
componentIdent = currentSolution['complexPart']
componentStoichio = 1.0
if 'stoichioValue' in currentSolution.keys():
componentStoichio = float(currentSolution['stoichioValue'])
for (k,v) in getComponentsStoichiometry(dataSource, componentIdent, prefixesDict).items():
dictComponents[k] += componentStoichio * v
return dictComponents
def getRepresentationBiopaxOriginal(dataSource, complexIdent, prefixesDict={}, targetGraph=None, rdfFormat="turtle", biopaxFilePath="", recursiveComplexComponents=False):
"""
Retrieve an RDF representation of a molecular complex consisting of all the triples having the complex as subject.
Parameters
----------
dataSource : str
Either the URL of the SPARQL endpoint or the path of the file.
complexIdent : str
Identifier (either CURIe or full URI) of the molecular complex.
prefixesDict : dict of {str : str}, default {}
Prefixes names and values.
targetGraph : rdflib.Graph
Graph in which to write the RDF representation. A new graph is created if None, default None.
rdfFormat : str
Output RDF format, default "turtle".
biopaxFilePath : str
File path in which to write the RDF representation if different from empty string, default "".
recursiveComplexComponents : boolean
recursively add to the BioPAX representation the molecular complex components that are also molecular complexes, default: False
Returns
-------
string with RDF serialization.
"""
if targetGraph == None:
resultGraph = rdflib.Graph()
else:
resultGraph = targetGraph
queryPath = 'queries/template-complexAllRelationsAndValues-construct.rq'
sparqlQuery = pathlib.Path(queryPath).read_text().replace('$supplementaryPrefixes$',uri_utils.convertPrefixesDictToSPARQL(prefixesDict))
if complexIdent.startswith('http'):
sparqlQuery = sparqlQuery.replace('$complexValue$','<' + complexIdent + '>')
else:
sparqlQuery = sparqlQuery.replace('$complexValue$',complexIdent)
if dataSource.startswith("http"):
sparql = SPARQLWrapper(dataSource)
sparql.setQuery(sparqlQuery)
sparql.setReturnFormat(rdfFormat)
results = sparql.query().convert()
resultGraph += rdflib.Graph().parse(data=results, format=rdfFormat)
resultGraph.bind("bp3","http://www.biopax.org/release/biopax-level3.owl#")
for (k,v) in prefixesDict.items():
resultGraph.bind(k,v)
else:
# TODO: guess format from file extension
sourceGraph = rdflib.Graph().parse(dataSource, format="text/turtle")
results = sourceGraph.query(sparqlQuery, initNs=prefixesDict)
resultGraph += rdflib.Graph().parse(data=results.serialize(format='xml'))
resultGraph.bind("bp3","http://www.biopax.org/release/biopax-level3.owl#")
for (k,v) in prefixesDict.items():
resultGraph.bind(k,v)
stoichios = getComponentsDirectStoichiometry(dataSource, complexIdent, prefixesDict)
bp3NS = rdflib.Namespace("http://www.biopax.org/release/biopax-level3.owl#")
for currentStochio in stoichios:
resultGraph.add((rdflib.URIRef(currentStochio['stoichioIdent']), rdflib.namespace.RDF['type'], bp3NS['Stoichiometry']))
resultGraph.add((rdflib.URIRef(currentStochio['stoichioIdent']), bp3NS['physicalEntity'], rdflib.URIRef(currentStochio['complexPart'])))
resultGraph.add((rdflib.URIRef(currentStochio['stoichioIdent']), bp3NS['stoichiometricCoefficient'], rdflib.Literal(currentStochio['stoichioValue'], datatype=rdflib.namespace.XSD.float)))
if recursiveComplexComponents:
queryPath = 'queries/template-complexComponentsComplexes.rq'
sparqlQuery = pathlib.Path(queryPath).read_text().replace('$supplementaryPrefixes$',uri_utils.convertPrefixesDictToSPARQL(prefixesDict))
if complexIdent.startswith('http'):
sparqlQuery = sparqlQuery.replace('$complexValue$','<' + complexIdent + '>')
else:
sparqlQuery = sparqlQuery.replace('$complexValue$',complexIdent)
if dataSource.startswith("http"):
#sparql = SPARQLWrapper(dataSource)
sparql.setQuery(sparqlQuery)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
subComplex = result['complexPart']['value']
getRepresentationBiopaxOriginal(dataSource, subComplex, prefixesDict, targetGraph=resultGraph, rdfFormat=rdfFormat, biopaxFilePath="", recursiveComplexComponents=recursiveComplexComponents)
else:
for result in sourceGraph.query(sparqlQuery, initNs=prefixesDict):
subComplex = result['complexPart']['value']
getRepresentationBiopaxOriginal(dataSource, subComplex, prefixesDict, targetGraph=resultGraph, rdfFormat=rdfFormat, biopaxFilePath="", recursiveComplexComponents=recursiveComplexComponents)
if biopaxFilePath != "":
with open(biopaxFilePath, 'w') as rdfFile:
rdfFile.write(resultGraph.serialize(format=rdfFormat).encode().decode('UTF-8'))
return resultGraph.serialize(format=rdfFormat).encode().decode('UTF-8')
def addToGraphBiopaxOriginal(dataSource, complexIdent, prefixesDict={}, rdfFormat="turtle", targetGraph=None, recursiveComplexComponents=False):
"""
Add to a graph the RDF representation of a molecular complex consisting of all the triples having the complex as subject.
Parameters
----------
dataSource : str
Either the URL of the SPARQL endpoint or the path of the file.
complexIdent : str
Identifier (either CURIe or full URI) of the molecular complex.
prefixesDict : dict of {str : str}, default {}
Prefixes names and values.
rdfFormat : str
Output RDF format, default "turtle".
targetGraph : rdflib.Graph
Graph in which to write the RDF representation. A new graph is created if None, default None.
Returns
-------
string with RDF serialization.
"""
queryPath = 'queries/template-complexAllRelationsAndValues-construct.rq'
sparqlQuery = pathlib.Path(queryPath).read_text().replace('$supplementaryPrefixes$',uri_utils.convertPrefixesDictToSPARQL(prefixesDict))
if complexIdent.startswith('http'):
sparqlQuery = sparqlQuery.replace('$complexValue$','<' + complexIdent + '>')
else:
sparqlQuery = sparqlQuery.replace('$complexValue$',complexIdent)
if targetGraph == None:
resultGraph = rdflib.Graph()
else:
resultGraph = targetGraph
if dataSource.startswith("http"):
sparql = SPARQLWrapper(dataSource)
sparql.setQuery(sparqlQuery)
sparql.setReturnFormat(rdfFormat)
results = sparql.query().convert()
resultGraph.parse(data=results, format=rdfFormat)
else:
# TODO: guess format from file extension
sourceGraph = rdflib.Graph().parse(dataSource, format="text/turtle")
results = sourceGraph.query(sparqlQuery, initNs=prefixesDict)
resultGraph.parse(data=results.serialize(format='xml'))
resultGraph.bind("bp3","http://www.biopax.org/release/biopax-level3.owl#")
for (k,v) in prefixesDict.items():
resultGraph.bind(k,v)
return resultGraph
def getRepresentationBiopaxValid(dataSource, complexIdent, prefixesDict={}, targetGraph=None, rdfFormat="turtle", biopaxFilePath=""):
"""
Generate a BioPAX-compliant RDF representation of a molecular complex such that none of its components that is also a complex has components of its own (i.e. so that all its complex components are black box complexes, cf. BioPAX specification p48).
Parameters
----------
dataSource : str
Either the URL of the SPARQL endpoint or the path of the file.
complexIdent : str
Identifier (either CURIe or full URI) of the molecular complex.
prefixesDict : dict of {str : str}, default {}
Prefixes names and values.
targetGraph : rdflib.Graph
Graph in which to write the RDF representation. A new graph is created if None, default None.
rdfFormat : str
Output RDF format, default "turtle".
biopaxFilePath : str
File path in which to write the RDF representation if different from empty string, default "".
Returns
-------
string with RDF serialization.
"""
if targetGraph == None:
resultGraph = rdflib.Graph()
else:
resultGraph = targetGraph
queryPath = 'queries/template-complexAllRelationsAndValuesExceptComponents-construct.rq'
sparqlQuery = pathlib.Path(queryPath).read_text().replace('$supplementaryPrefixes$',uri_utils.convertPrefixesDictToSPARQL(prefixesDict))
if complexIdent.startswith('http'):
sparqlQuery = sparqlQuery.replace('$complexValue$','<' + complexIdent + '>')
else:
sparqlQuery = sparqlQuery.replace('$complexValue$',complexIdent)
if dataSource.startswith("http"):
sparql = SPARQLWrapper(dataSource)
sparql.setQuery(sparqlQuery)
sparql.setReturnFormat(rdfFormat)
results = sparql.query().convert()
resultGraph += rdflib.Graph().parse(data=results, format=rdfFormat)
else:
# TODO: guess format from file extension
sourceGraph = rdflib.Graph().parse(dataSource, format="text/turtle")
results = sourceGraph.query(sparqlQuery, initNs=prefixesDict)
resultGraph += rdflib.Graph().parse(data=results.serialize(format='xml'))
resultGraph.bind("bp3","http://www.biopax.org/release/biopax-level3.owl#")
for (k,v) in prefixesDict.items():
resultGraph.bind(k,v)
bp3NS = rdflib.Namespace("http://www.biopax.org/release/biopax-level3.owl#")
complexIdentURI = rdflib.URIRef(uri_utils.getFullURI(complexIdent, prefixesDict))
if '' in prefixesDict.keys():
defaultNS = rdflib.Namespace(prefixesDict[''])
else:
defaultNS = rdflib.Namespace(uri_utils.getBaseURI(complexIdent))
for (currentComponent, currentStoichiometricCoeff) in getComponentsStoichiometry(dataSource, complexIdent, prefixesDict).items():
if currentComponent == complexIdent:
continue
getRepresentationBiopaxValid(dataSource, currentComponent, prefixesDict, resultGraph, rdfFormat="turtle", biopaxFilePath="")
resultGraph.add((complexIdentURI, bp3NS['component'], rdflib.URIRef(currentComponent)))
stoichiometryNodeID = 'Stoichiometry-' + uri_utils.getLocalName(complexIdent, prefixesDict) + '-' + uri_utils.getLocalName(currentComponent, prefixesDict)
resultGraph.add((complexIdentURI, bp3NS['componentStoichiometry'], defaultNS[stoichiometryNodeID]))
resultGraph.add((defaultNS[stoichiometryNodeID], rdflib.namespace.RDF['type'], bp3NS['Stoichiometry']))
resultGraph.add((defaultNS[stoichiometryNodeID], bp3NS['physicalEntity'], rdflib.URIRef(currentComponent)))
resultGraph.add((defaultNS[stoichiometryNodeID], bp3NS['stoichiometricCoefficient'], rdflib.Literal(currentStoichiometricCoeff, datatype=rdflib.namespace.XSD.float)))
if biopaxFilePath != "":
with open(biopaxFilePath, 'w') as rdfFile:
rdfFile.write(resultGraph.serialize(format=rdfFormat).encode().decode('UTF-8'))
return(resultGraph.serialize(format=rdfFormat).encode().decode('UTF-8'))
def main():
parser = argparse.ArgumentParser(description='Generate a BioPAX-compliant representation of a BioPAX-level3 molecular complex')
parser.add_argument("dataSource", help="either URL (starting with 'http') of the SPARQL endpoint having molecular complexes in BioPAX-level3 format, or file path (starting with anything but 'http') of the RDF file") # 'http://localhost:3030/reactome/query'
parser.add_argument("complexIdentifier", help="identifier of the molecular complex (CURIE or full URI)") # 'reactome:Complex1'
parser.add_argument("jsonPrefixesFilePath", help="path for the json file containing a dictionnary which keys are prefixes and associated values are URIs (default: empty)", nargs='?', default="") # defaultPrefixes-release77.json
parser.add_argument("dotFilePath", help="path for the .dot file to be generated (default: guess from complexIdentifier)", nargs='?', default="")
parser.add_argument('--recursiveComplexComponents', help='recursively add to the BioPAX representation the molecular complex components that are also molecular complexes (default: False)', nargs='?', default=False, choices=('True','False'))
args = parser.parse_args()
prefixesDict = {}
if args.jsonPrefixesFilePath != "":
prefixesDict = uri_utils.readPrefixesFromFile(args.jsonPrefixesFilePath)
getRepresentationBiopaxOriginal(args.dataSource, args.complexIdentifier, prefixesDict=prefixesDict, targetGraph=None, rdfFormat="turtle", biopaxFilePath=args.complexIdentifier.replace("reactome:", "")+"-orig.ttl", recursiveComplexComponents=bool(distutils.util.strtobool(str(args.recursiveComplexComponents))))
getRepresentationBiopaxValid(args.dataSource, args.complexIdentifier, prefixesDict=prefixesDict, targetGraph=None, rdfFormat="turtle", biopaxFilePath=args.complexIdentifier.replace("reactome:", "")+"-valid.ttl")
if __name__ == "__main__":
main()