closurizer 0.5.1__tar.gz → 0.7.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.
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: closurizer
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.7.0
|
4
4
|
Summary: Add closure expansion fields to kgx files following the Golr pattern
|
5
5
|
Author: Kevin Schaper
|
6
6
|
Author-email: kevin@tislab.org
|
@@ -11,6 +11,7 @@ Classifier: Programming Language :: Python :: 3.9
|
|
11
11
|
Classifier: Programming Language :: Python :: 3.10
|
12
12
|
Classifier: Programming Language :: Python :: 3.11
|
13
13
|
Classifier: Programming Language :: Python :: 3.12
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
14
15
|
Requires-Dist: SQLAlchemy (>=1.4.37,<2.0.0)
|
15
16
|
Requires-Dist: click (>=8,<9)
|
16
|
-
Requires-Dist: duckdb
|
17
|
+
Requires-Dist: duckdb
|
@@ -11,6 +11,7 @@ from closurizer.closurizer import add_closure
|
|
11
11
|
@click.option('--additional-node-constraints', required=False,
|
12
12
|
help='additional where clause constraints to apply to the generation of the denormalized nodes output')
|
13
13
|
@click.option('--edge-fields', multiple=True, help='edge fields to expand with closure IDs, labels, etc')
|
14
|
+
@click.option('--edge-fields-to-label', multiple=True, help='edge fields to with category, label, etc but not full closure exansion')
|
14
15
|
@click.option('--node-fields', multiple=True, help='node fields to expand with closure IDs, labels, etc')
|
15
16
|
@click.option('--grouping-fields', multiple=True, help='fields to populate a single value grouping_key field')
|
16
17
|
@click.option('--dry-run', is_flag=True, help='A dry run will not write the output file, but will print the SQL query')
|
@@ -21,11 +22,13 @@ def main(kg: str,
|
|
21
22
|
additional_node_constraints: str = None,
|
22
23
|
dry_run: bool = False,
|
23
24
|
edge_fields: List[str] = None,
|
25
|
+
edge_fields_to_label: List[str] = None,
|
24
26
|
node_fields: List[str] = None,
|
25
27
|
grouping_fields: List[str] = None):
|
26
28
|
add_closure(kg_archive=kg,
|
27
29
|
closure_file=closure,
|
28
30
|
edge_fields=edge_fields,
|
31
|
+
edge_fields_to_label=edge_fields_to_label,
|
29
32
|
node_fields=node_fields,
|
30
33
|
edges_output_file=edges_output,
|
31
34
|
nodes_output_file=nodes_output,
|
@@ -1,10 +1,10 @@
|
|
1
|
-
from typing import List
|
1
|
+
from typing import List, Optional
|
2
2
|
|
3
3
|
import os
|
4
4
|
import tarfile
|
5
5
|
import duckdb
|
6
6
|
|
7
|
-
def edge_columns(field):
|
7
|
+
def edge_columns(field: str, include_closure_fields: bool =True):
|
8
8
|
column_text = f"""
|
9
9
|
{field}.name as {field}_label,
|
10
10
|
{field}.category as {field}_category,
|
@@ -19,14 +19,14 @@ def edge_columns(field):
|
|
19
19
|
"""
|
20
20
|
return column_text
|
21
21
|
|
22
|
-
def edge_joins(field):
|
22
|
+
def edge_joins(field: str, include_closure_joins: bool =True):
|
23
23
|
return f"""
|
24
24
|
left outer join nodes as {field} on edges.{field} = {field}.id
|
25
25
|
left outer join closure_id as {field}_closure on {field}.id = {field}_closure.id
|
26
26
|
left outer join closure_label as {field}_closure_label on {field}.id = {field}_closure_label.id
|
27
27
|
"""
|
28
28
|
|
29
|
-
def evidence_sum(evidence_fields):
|
29
|
+
def evidence_sum(evidence_fields: List[str]):
|
30
30
|
""" Sum together the length of each field after splitting on | """
|
31
31
|
evidence_count_sum = "+".join([f"ifnull(len(split({field}, '|')),0)" for field in evidence_fields])
|
32
32
|
return f"{evidence_count_sum} as evidence_count,"
|
@@ -57,26 +57,29 @@ def node_joins(predicate):
|
|
57
57
|
on {field}_edges.object = {field}_closure_label.id
|
58
58
|
"""
|
59
59
|
|
60
|
+
|
60
61
|
def grouping_key(grouping_fields):
|
61
62
|
fragments = []
|
62
63
|
for field in grouping_fields:
|
63
64
|
if field == 'negated':
|
64
|
-
fragments.append(f"coalesce({field}.replace('
|
65
|
+
fragments.append(f"coalesce(cast({field} as varchar).replace('true','NOT'), '')")
|
65
66
|
else:
|
66
67
|
fragments.append(field)
|
67
68
|
grouping_key_fragments = ", ".join(fragments)
|
68
69
|
return f"concat_ws('|', {grouping_key_fragments}) as grouping_key"
|
69
70
|
|
71
|
+
|
70
72
|
def add_closure(kg_archive: str,
|
71
73
|
closure_file: str,
|
72
74
|
nodes_output_file: str,
|
73
75
|
edges_output_file: str,
|
74
|
-
node_fields: List[str] =
|
76
|
+
node_fields: List[str] = [],
|
75
77
|
edge_fields: List[str] = ['subject', 'object'],
|
76
|
-
|
78
|
+
edge_fields_to_label: List[str] = [],
|
79
|
+
additional_node_constraints: Optional[str] = None,
|
77
80
|
dry_run: bool = False,
|
78
|
-
evidence_fields: List[str] =
|
79
|
-
grouping_fields: List[str] =
|
81
|
+
evidence_fields: List[str] = ['has_evidence', 'publications'],
|
82
|
+
grouping_fields: List[str] = ['subject', 'negated', 'predicate', 'object']
|
80
83
|
):
|
81
84
|
print("Generating closure KG...")
|
82
85
|
print(f"kg_archive: {kg_archive}")
|
@@ -84,16 +87,6 @@ def add_closure(kg_archive: str,
|
|
84
87
|
|
85
88
|
db = duckdb.connect(database='monarch-kg.duckdb')
|
86
89
|
|
87
|
-
if edge_fields is None or len(edge_fields) == 0:
|
88
|
-
edge_fields = ['subject', 'object']
|
89
|
-
|
90
|
-
if evidence_fields is None or len(evidence_fields) == 0:
|
91
|
-
evidence_fields = ['has_evidence', 'publications']
|
92
|
-
|
93
|
-
if grouping_fields is None or len(grouping_fields) == 0:
|
94
|
-
grouping_fields = ['subject', 'negated', 'predicate', 'object']
|
95
|
-
|
96
|
-
|
97
90
|
if not dry_run:
|
98
91
|
print(f"fields: {','.join(edge_fields)}")
|
99
92
|
print(f"output_file: {edges_output_file}")
|
@@ -137,21 +130,24 @@ def add_closure(kg_archive: str,
|
|
137
130
|
create or replace table denormalized_edges as
|
138
131
|
select edges.*,
|
139
132
|
{"".join([edge_columns(field) for field in edge_fields])}
|
133
|
+
{"".join([edge_columns(field, include_closure_fields=False) for field in edge_fields_to_label])}
|
140
134
|
{evidence_sum(evidence_fields)}
|
141
135
|
{grouping_key(grouping_fields)}
|
142
136
|
from edges
|
143
137
|
{"".join([edge_joins(field) for field in edge_fields])}
|
138
|
+
{"".join([edge_joins(field, include_closure_joins=False) for field in edge_fields_to_label])}
|
144
139
|
"""
|
145
140
|
|
146
141
|
print(edges_query)
|
147
142
|
|
143
|
+
additional_node_constraints = f"where {additional_node_constraints}" if additional_node_constraints else ""
|
148
144
|
nodes_query = f"""
|
149
145
|
create or replace table denormalized_nodes as
|
150
146
|
select nodes.*,
|
151
147
|
{"".join([node_columns(node_field) for node_field in node_fields])}
|
152
148
|
from nodes
|
153
149
|
{node_joins('has_phenotype')}
|
154
|
-
|
150
|
+
{additional_node_constraints}
|
155
151
|
group by nodes.*
|
156
152
|
"""
|
157
153
|
print(nodes_query)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
[tool.poetry]
|
2
2
|
name = "closurizer"
|
3
|
-
version = "0.
|
3
|
+
version = "0.7.0"
|
4
4
|
description = "Add closure expansion fields to kgx files following the Golr pattern"
|
5
5
|
authors = ["Kevin Schaper <kevin@tislab.org>"]
|
6
6
|
|
@@ -8,13 +8,16 @@ authors = ["Kevin Schaper <kevin@tislab.org>"]
|
|
8
8
|
python = "^3.8"
|
9
9
|
click = "^8"
|
10
10
|
SQLAlchemy = "^1.4.37"
|
11
|
-
duckdb = "
|
11
|
+
duckdb = "*"
|
12
12
|
|
13
13
|
[tool.poetry.dev-dependencies]
|
14
14
|
|
15
15
|
[tool.poetry.scripts]
|
16
16
|
closurizer = "closurizer.cli:main"
|
17
17
|
|
18
|
+
[tool.poetry.group.dev.dependencies]
|
19
|
+
pytest = "^8.1.1"
|
20
|
+
|
18
21
|
[build-system]
|
19
22
|
requires = ["poetry-core>=1.0.0"]
|
20
23
|
build-backend = "poetry.core.masonry.api"
|