biocypher 0.9.2__py3-none-any.whl → 0.12.3__py3-none-any.whl
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.
- biocypher/__init__.py +8 -0
- biocypher/_core.py +10 -4
- biocypher/_get.py +129 -46
- biocypher/_graph.py +819 -0
- biocypher/_metadata.py +32 -12
- biocypher/_translate.py +5 -8
- biocypher/_workflow.py +798 -0
- biocypher/output/connect/_get_connector.py +24 -7
- biocypher/output/connect/_neo4j_driver.py +55 -9
- biocypher/output/connect/_neo4j_driver_wrapper.py +1317 -0
- biocypher/output/in_memory/_airr.py +499 -0
- biocypher/output/in_memory/_get_in_memory_kg.py +25 -13
- biocypher/output/in_memory/_in_memory_kg.py +57 -0
- biocypher/output/in_memory/_pandas.py +1 -59
- biocypher/output/templates/powershell_template.ps1 +60 -0
- biocypher/output/write/_batch_writer.py +4 -4
- biocypher/output/write/_get_writer.py +5 -0
- biocypher/output/write/graph/_airr.py +32 -0
- biocypher/output/write/graph/_neo4j.py +91 -5
- biocypher/output/write/relational/_postgresql.py +37 -27
- {biocypher-0.9.2.dist-info → biocypher-0.12.3.dist-info}/METADATA +32 -28
- {biocypher-0.9.2.dist-info → biocypher-0.12.3.dist-info}/RECORD +31 -24
- {biocypher-0.9.2.dist-info → biocypher-0.12.3.dist-info}/WHEEL +1 -1
- biocypher-0.12.3.dist-info/licenses/LICENSE +202 -0
- biocypher-0.12.3.dist-info/licenses/NOTICE +9 -0
- biocypher-0.9.2.dist-info/LICENSE +0 -21
|
@@ -6,7 +6,6 @@ Handles the connecting and writing a Knowledge Graph to a database.
|
|
|
6
6
|
from biocypher._config import config as _config
|
|
7
7
|
from biocypher._logger import logger
|
|
8
8
|
from biocypher._translate import Translator
|
|
9
|
-
from biocypher.output.connect._neo4j_driver import _Neo4jDriver
|
|
10
9
|
|
|
11
10
|
logger.debug(f"Loading module {__name__}.")
|
|
12
11
|
|
|
@@ -17,16 +16,32 @@ def get_connector(
|
|
|
17
16
|
dbms: str,
|
|
18
17
|
translator: Translator,
|
|
19
18
|
):
|
|
20
|
-
"""
|
|
21
|
-
Function to return the connector class.
|
|
19
|
+
"""Return the connector class.
|
|
22
20
|
|
|
23
|
-
Returns
|
|
21
|
+
Returns
|
|
22
|
+
-------
|
|
24
23
|
class: the connector class
|
|
25
|
-
"""
|
|
26
24
|
|
|
25
|
+
Raises
|
|
26
|
+
------
|
|
27
|
+
NotImplementedError: if the DBMS is not supported
|
|
28
|
+
ImportError: if Neo4j driver is not installed when using Neo4j
|
|
29
|
+
|
|
30
|
+
"""
|
|
27
31
|
dbms_config = _config(dbms)
|
|
28
32
|
|
|
29
33
|
if dbms == "neo4j":
|
|
34
|
+
# Import Neo4j driver only when needed
|
|
35
|
+
try:
|
|
36
|
+
from biocypher.output.connect._neo4j_driver import _Neo4jDriver
|
|
37
|
+
except ImportError as e:
|
|
38
|
+
msg = (
|
|
39
|
+
"Neo4j driver is not installed. Install it with: "
|
|
40
|
+
"pip install biocypher[neo4j] or pip install neo4j>=5.0"
|
|
41
|
+
)
|
|
42
|
+
logger.error(msg)
|
|
43
|
+
raise ImportError(msg) from e
|
|
44
|
+
|
|
30
45
|
return _Neo4jDriver(
|
|
31
46
|
database_name=dbms_config["database_name"],
|
|
32
47
|
wipe=dbms_config["wipe"],
|
|
@@ -36,5 +51,7 @@ def get_connector(
|
|
|
36
51
|
multi_db=dbms_config["multi_db"],
|
|
37
52
|
translator=translator,
|
|
38
53
|
)
|
|
39
|
-
|
|
40
|
-
|
|
54
|
+
|
|
55
|
+
msg = f"Online mode is not supported for the DBMS {dbms}."
|
|
56
|
+
logger.error(msg)
|
|
57
|
+
raise NotImplementedError(msg)
|
|
@@ -6,12 +6,11 @@ import itertools
|
|
|
6
6
|
|
|
7
7
|
from collections.abc import Iterable
|
|
8
8
|
|
|
9
|
-
import neo4j_utils
|
|
10
|
-
|
|
11
9
|
from biocypher import _misc
|
|
12
10
|
from biocypher._create import BioCypherEdge, BioCypherNode
|
|
13
11
|
from biocypher._logger import logger
|
|
14
12
|
from biocypher._translate import Translator
|
|
13
|
+
from biocypher.output.connect._neo4j_driver_wrapper import Neo4jDriver
|
|
15
14
|
|
|
16
15
|
logger.debug(f"Loading module {__name__}.")
|
|
17
16
|
__all__ = ["_Neo4jDriver"]
|
|
@@ -20,7 +19,7 @@ __all__ = ["_Neo4jDriver"]
|
|
|
20
19
|
class _Neo4jDriver:
|
|
21
20
|
"""
|
|
22
21
|
Manages a BioCypher connection to a Neo4j database using the
|
|
23
|
-
``
|
|
22
|
+
internal ``Neo4jDriver`` class.
|
|
24
23
|
|
|
25
24
|
Args:
|
|
26
25
|
|
|
@@ -55,10 +54,12 @@ class _Neo4jDriver:
|
|
|
55
54
|
wipe: bool = False,
|
|
56
55
|
fetch_size: int = 1000,
|
|
57
56
|
increment_version: bool = True,
|
|
57
|
+
force_enterprise: bool = False,
|
|
58
|
+
**kwargs,
|
|
58
59
|
):
|
|
59
60
|
self.translator = translator
|
|
60
61
|
|
|
61
|
-
self._driver =
|
|
62
|
+
self._driver = Neo4jDriver(
|
|
62
63
|
db_name=database_name,
|
|
63
64
|
db_uri=uri,
|
|
64
65
|
db_user=user,
|
|
@@ -67,6 +68,7 @@ class _Neo4jDriver:
|
|
|
67
68
|
wipe=wipe,
|
|
68
69
|
multi_db=multi_db,
|
|
69
70
|
raise_errors=True,
|
|
71
|
+
force_enterprise=force_enterprise,
|
|
70
72
|
)
|
|
71
73
|
|
|
72
74
|
# check for biocypher config in connected graph
|
|
@@ -79,6 +81,18 @@ class _Neo4jDriver:
|
|
|
79
81
|
self._update_meta_graph()
|
|
80
82
|
|
|
81
83
|
def _update_meta_graph(self):
|
|
84
|
+
"""Update the BioCypher meta graph with version information.
|
|
85
|
+
|
|
86
|
+
Requires APOC to be installed. If APOC is not available, this
|
|
87
|
+
operation is skipped with a warning.
|
|
88
|
+
"""
|
|
89
|
+
if not self._driver.has_apoc:
|
|
90
|
+
logger.warning(
|
|
91
|
+
"APOC plugin is not available. Skipping meta graph update. "
|
|
92
|
+
"Install APOC to enable version tracking: https://neo4j.com/labs/apoc/"
|
|
93
|
+
)
|
|
94
|
+
return
|
|
95
|
+
|
|
82
96
|
logger.info("Updating Neo4j meta graph.")
|
|
83
97
|
|
|
84
98
|
# find current version node
|
|
@@ -102,8 +116,8 @@ class _Neo4jDriver:
|
|
|
102
116
|
def init_db(self):
|
|
103
117
|
"""
|
|
104
118
|
Used to initialise a property graph database by setting up new
|
|
105
|
-
constraints. Wipe has been performed by the ``
|
|
106
|
-
class
|
|
119
|
+
constraints. Wipe has been performed by the ``Neo4jDriver``
|
|
120
|
+
class already.
|
|
107
121
|
|
|
108
122
|
Todo:
|
|
109
123
|
- set up constraint creation interactively depending on the
|
|
@@ -137,7 +151,12 @@ class _Neo4jDriver:
|
|
|
137
151
|
self._driver.query(s)
|
|
138
152
|
|
|
139
153
|
def _get_neo4j_version(self):
|
|
140
|
-
"""Get neo4j version.
|
|
154
|
+
"""Get neo4j version.
|
|
155
|
+
|
|
156
|
+
Returns the Neo4j server version. If detection fails, defaults to
|
|
157
|
+
"5.0.0" to use the newer syntax (which is more likely for new
|
|
158
|
+
installations).
|
|
159
|
+
"""
|
|
141
160
|
try:
|
|
142
161
|
neo4j_version = self._driver.query(
|
|
143
162
|
"""
|
|
@@ -149,8 +168,12 @@ class _Neo4jDriver:
|
|
|
149
168
|
)[0][0]["version"]
|
|
150
169
|
return neo4j_version
|
|
151
170
|
except Exception as e:
|
|
152
|
-
logger.warning(
|
|
153
|
-
|
|
171
|
+
logger.warning(
|
|
172
|
+
f"Error detecting Neo4j version: {e}. "
|
|
173
|
+
"Defaulting to version 5.0.0 syntax. "
|
|
174
|
+
"If you're using Neo4j 4.x, this may cause errors."
|
|
175
|
+
)
|
|
176
|
+
return "5.0.0"
|
|
154
177
|
|
|
155
178
|
def add_nodes(self, id_type_tuples: Iterable[tuple]) -> tuple:
|
|
156
179
|
"""
|
|
@@ -237,6 +260,9 @@ class _Neo4jDriver:
|
|
|
237
260
|
|
|
238
261
|
Returns:
|
|
239
262
|
True for success, False otherwise.
|
|
263
|
+
|
|
264
|
+
Raises:
|
|
265
|
+
RuntimeError: If APOC is not available and required for the operation.
|
|
240
266
|
"""
|
|
241
267
|
|
|
242
268
|
try:
|
|
@@ -250,6 +276,16 @@ class _Neo4jDriver:
|
|
|
250
276
|
|
|
251
277
|
raise ValueError(msg)
|
|
252
278
|
|
|
279
|
+
# Check if APOC is available
|
|
280
|
+
if not self._driver.has_apoc:
|
|
281
|
+
msg = (
|
|
282
|
+
"APOC plugin is required for adding nodes. "
|
|
283
|
+
"Please install APOC in your Neo4j instance. "
|
|
284
|
+
"See: https://neo4j.com/labs/apoc/"
|
|
285
|
+
)
|
|
286
|
+
logger.error(msg)
|
|
287
|
+
raise RuntimeError(msg)
|
|
288
|
+
|
|
253
289
|
logger.info(f"Merging {len(entities)} nodes.")
|
|
254
290
|
|
|
255
291
|
entity_query = (
|
|
@@ -336,6 +372,16 @@ class _Neo4jDriver:
|
|
|
336
372
|
self.add_biocypher_nodes(nodes)
|
|
337
373
|
logger.info(f"Merging {len(rels)} edges.")
|
|
338
374
|
|
|
375
|
+
# Check if APOC is available
|
|
376
|
+
if not self._driver.has_apoc:
|
|
377
|
+
msg = (
|
|
378
|
+
"APOC plugin is required for adding edges. "
|
|
379
|
+
"Please install APOC in your Neo4j instance. "
|
|
380
|
+
"See: https://neo4j.com/labs/apoc/"
|
|
381
|
+
)
|
|
382
|
+
logger.error(msg)
|
|
383
|
+
raise RuntimeError(msg)
|
|
384
|
+
|
|
339
385
|
# cypher query
|
|
340
386
|
|
|
341
387
|
# merging only on the ids of the entities, passing the
|