cellmaps-utils 0.4.0__tar.gz → 0.5.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.
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/HISTORY.rst +9 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/PKG-INFO +14 -3
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/README.rst +4 -2
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/__init__.py +1 -1
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/constants.py +1 -1
- cellmaps_utils-0.5.0/cellmaps_utils/ddotconverter.py +285 -0
- cellmaps_utils-0.5.0/cellmaps_utils/hcx_utils.py +92 -0
- cellmaps_utils-0.5.0/cellmaps_utils/hidefconverter.py +245 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils.egg-info/PKG-INFO +15 -4
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils.egg-info/SOURCES.txt +9 -0
- cellmaps_utils-0.5.0/docs/cellmaps_utils.rst +129 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/usage.rst +1 -1
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/setup.py +0 -4
- cellmaps_utils-0.5.0/tests/.DS_Store +0 -0
- cellmaps_utils-0.5.0/tests/data/ddot_output.ont +10 -0
- cellmaps_utils-0.5.0/tests/data/hidef_output.edges +1 -0
- cellmaps_utils-0.5.0/tests/data/hidef_output.nodes +2 -0
- cellmaps_utils-0.5.0/tests/data/parent_edgelist +7 -0
- cellmaps_utils-0.5.0/tests/data/reactome_ddot.txt +7 -0
- cellmaps_utils-0.5.0/tests/test_ddotconverter.py +46 -0
- cellmaps_utils-0.5.0/tests/test_hidefconverter.py +102 -0
- cellmaps_utils-0.4.0/cellmaps_utils/hidefconverter.py +0 -126
- cellmaps_utils-0.4.0/docs/cellmaps_utils.rst +0 -55
- cellmaps_utils-0.4.0/tests/test_hidefconverter.py +0 -44
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/AUTHORS.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/CONTRIBUTING.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/LICENSE +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/MANIFEST.in +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/apms_readme.txt +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/apmstool.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/basecmdtool.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/cellmaps_utilscmd.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/crispr_readme.txt +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/crisprtool.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/exceptions.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/ifimage_readme.txt +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/iftool.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/logutils.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/music_utils.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/ndexupload.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/provenance.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils/tabletool.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils.egg-info/dependency_links.txt +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils.egg-info/not-zip-safe +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils.egg-info/requires.txt +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/cellmaps_utils.egg-info/top_level.txt +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/Makefile +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/_build/html/_static/file.png +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/_build/html/_static/minus.png +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/_build/html/_static/plus.png +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/authors.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/conf.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/contributing.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/devbranches.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/developer.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/history.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/index.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/installation.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/integrationtesting.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/make.bat +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/modules.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/newrelease.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/outputs.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/pypircfile.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/docs/versioningscheme.rst +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/setup.cfg +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/__init__.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/data/images/blue.jpg +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/data/images/green.jpg +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/data/images/red.jpg +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/data/images/yellow.jpg +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/data/ro-crate-metadata.json +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/data/test_password +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/test_apmstool.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/test_basecommandlinetool.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/test_crisprtool.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/test_helloworldcommand.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/test_iftool.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/test_integation_provenance.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/test_logutils.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/test_ndexupload.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/test_provenance.py +0 -0
- {cellmaps_utils-0.4.0 → cellmaps_utils-0.5.0}/tests/test_tabletool.py +0 -0
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
History
|
|
3
3
|
=======
|
|
4
4
|
|
|
5
|
+
0.5.0 (2024-09-05)
|
|
6
|
+
------------------
|
|
7
|
+
|
|
8
|
+
* Add ``HiDeFToHierarchyConverter``, a class to convert a edge list and node list
|
|
9
|
+
in HiDeF format to hierarchy in HCX.
|
|
10
|
+
* Add ``InteractomeToDDOTConverter`` and ``DDOTToInteractomeConverter``, classes to convert network in
|
|
11
|
+
CX2 format to DDOT format and vice versa, ``HierarchyToDDOTConverter`` and ``DDOTToHierarchyConverter``,
|
|
12
|
+
classes to convert hierarchy network in HCX format to DDOT and vice versa.
|
|
13
|
+
|
|
5
14
|
0.4.0 (2024-07-02)
|
|
6
15
|
-------------------
|
|
7
16
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: cellmaps_utils
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: Contains utilities needed by Cell Maps tools
|
|
5
5
|
Home-page: https://github.com/idekerlab/cellmaps_utils
|
|
6
6
|
Author: Cell Maps team
|
|
@@ -47,7 +47,7 @@ Cell Maps Pipeline Utilities
|
|
|
47
47
|
:target: https://zenodo.org/doi/10.5281/zenodo.10607322
|
|
48
48
|
:alt: Zenodo DOI badge
|
|
49
49
|
|
|
50
|
-
Contains utilities needed by various tools in the cell maps toolkit.
|
|
50
|
+
Contains utilities needed by various tools in the cell maps toolkit.
|
|
51
51
|
|
|
52
52
|
|
|
53
53
|
* Free software: MIT license
|
|
@@ -80,7 +80,7 @@ Installation
|
|
|
80
80
|
pip install dist/cellmaps_utils*whl
|
|
81
81
|
|
|
82
82
|
|
|
83
|
-
Run **make** command with no arguments to see other build/deploy options including creation of Docker image
|
|
83
|
+
Run **make** command with no arguments to see other build/deploy options including creation of Docker image
|
|
84
84
|
|
|
85
85
|
.. code-block::
|
|
86
86
|
|
|
@@ -105,6 +105,8 @@ Output:
|
|
|
105
105
|
dist builds source and wheel package
|
|
106
106
|
install install the package to the active Python's site-packages
|
|
107
107
|
|
|
108
|
+
Before running tests, please install ``pip install -r requirements_dev``
|
|
109
|
+
|
|
108
110
|
For developers
|
|
109
111
|
-------------------------------------------
|
|
110
112
|
|
|
@@ -143,6 +145,15 @@ This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypack
|
|
|
143
145
|
History
|
|
144
146
|
=======
|
|
145
147
|
|
|
148
|
+
0.5.0 (2024-09-05)
|
|
149
|
+
------------------
|
|
150
|
+
|
|
151
|
+
* Add ``HiDeFToHierarchyConverter``, a class to convert a edge list and node list
|
|
152
|
+
in HiDeF format to hierarchy in HCX.
|
|
153
|
+
* Add ``InteractomeToDDOTConverter`` and ``DDOTToInteractomeConverter``, classes to convert network in
|
|
154
|
+
CX2 format to DDOT format and vice versa, ``HierarchyToDDOTConverter`` and ``DDOTToHierarchyConverter``,
|
|
155
|
+
classes to convert hierarchy network in HCX format to DDOT and vice versa.
|
|
156
|
+
|
|
146
157
|
0.4.0 (2024-07-02)
|
|
147
158
|
-------------------
|
|
148
159
|
|
|
@@ -17,7 +17,7 @@ Cell Maps Pipeline Utilities
|
|
|
17
17
|
:target: https://zenodo.org/doi/10.5281/zenodo.10607322
|
|
18
18
|
:alt: Zenodo DOI badge
|
|
19
19
|
|
|
20
|
-
Contains utilities needed by various tools in the cell maps toolkit.
|
|
20
|
+
Contains utilities needed by various tools in the cell maps toolkit.
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
* Free software: MIT license
|
|
@@ -50,7 +50,7 @@ Installation
|
|
|
50
50
|
pip install dist/cellmaps_utils*whl
|
|
51
51
|
|
|
52
52
|
|
|
53
|
-
Run **make** command with no arguments to see other build/deploy options including creation of Docker image
|
|
53
|
+
Run **make** command with no arguments to see other build/deploy options including creation of Docker image
|
|
54
54
|
|
|
55
55
|
.. code-block::
|
|
56
56
|
|
|
@@ -75,6 +75,8 @@ Output:
|
|
|
75
75
|
dist builds source and wheel package
|
|
76
76
|
install install the package to the active Python's site-packages
|
|
77
77
|
|
|
78
|
+
Before running tests, please install ``pip install -r requirements_dev``
|
|
79
|
+
|
|
78
80
|
For developers
|
|
79
81
|
-------------------------------------------
|
|
80
82
|
|
|
@@ -194,7 +194,7 @@ CX2 format hierarchy filename
|
|
|
194
194
|
|
|
195
195
|
HIERARCHY_PARENT_NETWORK_PREFIX = 'hierarchy_parent'
|
|
196
196
|
"""
|
|
197
|
-
CX2 format hierarchy parent (
|
|
197
|
+
CX2 format hierarchy parent (interactome) filename
|
|
198
198
|
"""
|
|
199
199
|
|
|
200
200
|
HIERARCHY_NODES_FILE = 'hierarchy_node_attributes.tsv'
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os.path
|
|
3
|
+
|
|
4
|
+
from ndex2.cx2 import CX2Network, RawCX2NetworkFactory
|
|
5
|
+
import ndex2.constants as constants
|
|
6
|
+
|
|
7
|
+
from cellmaps_utils import hcx_utils
|
|
8
|
+
from cellmaps_utils.exceptions import CellMapsError
|
|
9
|
+
|
|
10
|
+
logger = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class InteractomeToDDOTConverter:
|
|
14
|
+
|
|
15
|
+
def __init__(self, output_dir, interactome_path, ddot_file_name='interactome_ddot.txt'):
|
|
16
|
+
"""
|
|
17
|
+
Initializes the converter with the path to the interactome data in CX2 format, output directory, and file name.
|
|
18
|
+
|
|
19
|
+
:param output_dir: Directory where the output file will be saved
|
|
20
|
+
:type output_dir: str
|
|
21
|
+
:param interactome_path: Path to the interactome file
|
|
22
|
+
:type interactome_path: str
|
|
23
|
+
:param ddot_file_name: Name of the output file to write DDOT formatted data
|
|
24
|
+
:type ddot_file_name: str
|
|
25
|
+
"""
|
|
26
|
+
self._interactome_path = interactome_path
|
|
27
|
+
self._output_file = os.path.join(output_dir, ddot_file_name)
|
|
28
|
+
|
|
29
|
+
def generate_ddot_format_file(self):
|
|
30
|
+
"""
|
|
31
|
+
Reads an interactome from a specified path and writes it out in DDOT format.
|
|
32
|
+
The output includes nodes and their interactions.
|
|
33
|
+
"""
|
|
34
|
+
factory = RawCX2NetworkFactory()
|
|
35
|
+
interactome = factory.get_cx2network(self._interactome_path)
|
|
36
|
+
with open(self._output_file, 'w') as f:
|
|
37
|
+
for edge_id, edge_values in interactome.get_edges().items():
|
|
38
|
+
source = edge_values[constants.EDGE_SOURCE]
|
|
39
|
+
target = edge_values[constants.EDGE_TARGET]
|
|
40
|
+
if constants.EDGE_INTERACTION_EXPANDED in edge_values[constants.ASPECT_VALUES]:
|
|
41
|
+
interaction = edge_values[constants.ASPECT_VALUES][constants.EDGE_INTERACTION_EXPANDED]
|
|
42
|
+
f.write(f"{source}\t{target}\t{interaction}\n")
|
|
43
|
+
else:
|
|
44
|
+
f.write(f"{source}\t{target}\n")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class DDOTToInteractomeConverter:
|
|
48
|
+
|
|
49
|
+
def __init__(self, output_dir, interactome_ddot_path, interactome_file_name='interactome.cx2'):
|
|
50
|
+
"""
|
|
51
|
+
Initializes the converter to transform a DDOT formatted file to an CX2 format.
|
|
52
|
+
|
|
53
|
+
:param output_dir: Directory where the output file will be saved
|
|
54
|
+
:type output_dir: str
|
|
55
|
+
:param interactome_ddot_path: Path to the DDOT formatted file
|
|
56
|
+
:type interactome_ddot_path: str
|
|
57
|
+
:param interactome_file_name: Name of the output file for the interactome
|
|
58
|
+
:type interactome_file_name: str
|
|
59
|
+
"""
|
|
60
|
+
self._interactome_ddot_path = interactome_ddot_path
|
|
61
|
+
self._output_file = os.path.join(output_dir, interactome_file_name)
|
|
62
|
+
|
|
63
|
+
def generate_interactome_file(self):
|
|
64
|
+
"""
|
|
65
|
+
Converts a DDOT formatted file to an interactome CX2 network file. It parses the DDOT file,
|
|
66
|
+
constructs nodes and edges in the interactome, and saves the output.
|
|
67
|
+
"""
|
|
68
|
+
interactome = CX2Network()
|
|
69
|
+
with open(self._interactome_ddot_path, 'r') as f:
|
|
70
|
+
for line in f:
|
|
71
|
+
parts = line.strip().split('\t')
|
|
72
|
+
id_source_node = interactome.lookup_node_id_by_name(parts[0])
|
|
73
|
+
source_node = id_source_node if id_source_node is not None else interactome.add_node(
|
|
74
|
+
attributes={'name': parts[0]})
|
|
75
|
+
id_target_node = interactome.lookup_node_id_by_name(parts[1])
|
|
76
|
+
target_node = id_target_node if id_target_node is not None else interactome.add_node(
|
|
77
|
+
attributes={'name': parts[1]})
|
|
78
|
+
id_edge = interactome.add_edge(source=source_node, target=target_node)
|
|
79
|
+
if len(parts) >= 3:
|
|
80
|
+
interactome.add_edge_attribute(id_edge, constants.EDGE_INTERACTION_EXPANDED, parts[3])
|
|
81
|
+
interactome.write_as_raw_cx2(self._output_file)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class HierarchyToDDOTConverter:
|
|
85
|
+
|
|
86
|
+
def __init__(self, output_dir, hierarchy_path, ontology_file_name='ontology.ont'):
|
|
87
|
+
"""
|
|
88
|
+
Initializes the converter to transform a hierarchy data file in CX2 format into a DDOT ontology file.
|
|
89
|
+
|
|
90
|
+
:param output_dir: Directory where the output ontology file will be saved
|
|
91
|
+
:type output_dir: str
|
|
92
|
+
:param hierarchy_path: Path to the hierarchy data file
|
|
93
|
+
:type hierarchy_path: str
|
|
94
|
+
:param ontology_file_name: Name of the output ontology file
|
|
95
|
+
:type ontology_file_name: str
|
|
96
|
+
"""
|
|
97
|
+
self._hierarchy_path = hierarchy_path
|
|
98
|
+
self._output_file = os.path.join(output_dir, ontology_file_name)
|
|
99
|
+
|
|
100
|
+
def generate_ontology_ddot_file(self):
|
|
101
|
+
"""
|
|
102
|
+
Converts a hierarchy network file into a DDOT ontology file format. This method extracts hierarchy
|
|
103
|
+
edges and node information to create an ontology representation suitable for DDOT applications.
|
|
104
|
+
"""
|
|
105
|
+
factory = RawCX2NetworkFactory()
|
|
106
|
+
hierarchy = factory.get_cx2network(self._hierarchy_path)
|
|
107
|
+
with open(self._output_file, 'w') as f:
|
|
108
|
+
for _, edge_values in hierarchy.get_edges().items():
|
|
109
|
+
source_id = edge_values[constants.EDGE_SOURCE]
|
|
110
|
+
source = hierarchy.get_node(source_id)[constants.ASPECT_VALUES].get(constants.NODE_NAME_EXPANDED,
|
|
111
|
+
source_id)
|
|
112
|
+
target_id = edge_values[constants.EDGE_TARGET]
|
|
113
|
+
target = hierarchy.get_node(target_id)[constants.ASPECT_VALUES].get(constants.NODE_NAME_EXPANDED,
|
|
114
|
+
target_id)
|
|
115
|
+
f.write(f"{source}\t{target}\tdefault\n")
|
|
116
|
+
for node_id, node_values in hierarchy.get_nodes().items():
|
|
117
|
+
node_attr = node_values[constants.ASPECT_VALUES]
|
|
118
|
+
node = node_attr.get(constants.NODE_NAME_EXPANDED, node_id)
|
|
119
|
+
genes = node_attr.get('CD_MemberList', '').split(' ')
|
|
120
|
+
for gene in genes:
|
|
121
|
+
f.write(f"{node}\t{gene}\tgene\n")
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class DDOTToHierarchyConverter:
|
|
125
|
+
|
|
126
|
+
def __init__(self, output_dir, ontology_ddot_path, parent_ddot_path=None, parent_ndex_url=None,
|
|
127
|
+
host='ndexbio.org', parent_uuid=None, ndex_user=None, ndex_password=None,
|
|
128
|
+
hierarchy_filename='hierarchy.cx2', interactome_filename='hierarchy_parent.cx2'):
|
|
129
|
+
"""
|
|
130
|
+
Initializes the converter to create a hierarchy in CX2 from DDOT ontology file.
|
|
131
|
+
|
|
132
|
+
:param output_dir: Directory where the output files will be saved
|
|
133
|
+
:type output_dir: str
|
|
134
|
+
:param ontology_ddot_path: Path to the DDOT formatted ontology file
|
|
135
|
+
:type ontology_ddot_path: str
|
|
136
|
+
:param parent_ddot_path: Optional path to the parent interactome DDOT file
|
|
137
|
+
:type parent_ddot_path: str, optional
|
|
138
|
+
:param parent_ndex_url: Optional URL to the parent interactome in NDEx
|
|
139
|
+
:type parent_ndex_url: str, optional
|
|
140
|
+
:param host: Hostname for the NDEx server
|
|
141
|
+
:type host: str
|
|
142
|
+
:param parent_uuid: UUID of the parent network in NDEx
|
|
143
|
+
:type parent_uuid: str, optional
|
|
144
|
+
:param ndex_user: Username for NDEx server authentication
|
|
145
|
+
:type ndex_user: str, optional
|
|
146
|
+
:param ndex_password: Password for NDEx server authentication
|
|
147
|
+
:type ndex_password: str, optional
|
|
148
|
+
:param hierarchy_filename: Name for the output hierarchy file
|
|
149
|
+
:type hierarchy_filename: str
|
|
150
|
+
:param interactome_filename: Name for the output interactome file (parent network)
|
|
151
|
+
:type interactome_filename: str
|
|
152
|
+
"""
|
|
153
|
+
self._output_dir = output_dir
|
|
154
|
+
self._hierarchy_file = os.path.join(output_dir, hierarchy_filename)
|
|
155
|
+
self._interactome_filename = interactome_filename
|
|
156
|
+
self._ontology_ddot_path = ontology_ddot_path
|
|
157
|
+
if parent_ndex_url is None and parent_ddot_path is None:
|
|
158
|
+
raise CellMapsError("Specifying either url of parent interactome in NDEx, or edge list of interactome"
|
|
159
|
+
" is required!")
|
|
160
|
+
if parent_ddot_path is not None and (parent_ndex_url is not None or parent_uuid is not None):
|
|
161
|
+
raise CellMapsError("You need to specify parent network as edge list in file, or as network in NDEx, "
|
|
162
|
+
"not both!")
|
|
163
|
+
self._parent_url = parent_ndex_url
|
|
164
|
+
self._parent_edgelist = parent_ddot_path
|
|
165
|
+
self._host = host
|
|
166
|
+
self._uuid = parent_uuid
|
|
167
|
+
self._username = ndex_user
|
|
168
|
+
self._password = ndex_password
|
|
169
|
+
|
|
170
|
+
def generate_hierarchy_hcx_file(self):
|
|
171
|
+
"""
|
|
172
|
+
Constructs a hierarchy network from a DDOT formatted ontology file, utilizing a parent interactome.
|
|
173
|
+
The hierarchy is enriched with hierarchical context (HCX) information and saved as CX2.
|
|
174
|
+
"""
|
|
175
|
+
interactome = hcx_utils.get_interactome(self._host, self._uuid, self._username, self._password,
|
|
176
|
+
self._parent_edgelist)
|
|
177
|
+
hierarchy = self._get_hierarchy(interactome)
|
|
178
|
+
hcx_utils.add_hcx_network_annotations(hierarchy, interactome, self._output_dir, self._interactome_filename,
|
|
179
|
+
self._host, self._uuid)
|
|
180
|
+
hierarchy.write_as_raw_cx2(self._hierarchy_file)
|
|
181
|
+
|
|
182
|
+
def _get_hierarchy(self, interactome):
|
|
183
|
+
"""
|
|
184
|
+
Constructs the hierarchy based on the DDOT edge list and node list and uses interactome to add HCX information.
|
|
185
|
+
|
|
186
|
+
:param interactome: The interactome network.
|
|
187
|
+
:type interactome: CX2Network
|
|
188
|
+
:return: A CX2Network object representing the hierarchy.
|
|
189
|
+
:rtype: CX2Network
|
|
190
|
+
"""
|
|
191
|
+
hierarchy = CX2Network()
|
|
192
|
+
|
|
193
|
+
with open(self._ontology_ddot_path, 'r') as f:
|
|
194
|
+
for line in f:
|
|
195
|
+
source, target, edge_type = line.strip().split('\t')
|
|
196
|
+
if 'gene' in edge_type.lower():
|
|
197
|
+
source_id = hierarchy.lookup_node_id_by_name(source)
|
|
198
|
+
if source_id is not None:
|
|
199
|
+
node_attr = hierarchy.get_node(source_id)[constants.ASPECT_VALUES]
|
|
200
|
+
target_id = interactome.lookup_node_id_by_name(target)
|
|
201
|
+
if 'CD_MemberList' not in node_attr:
|
|
202
|
+
hierarchy.add_node_attribute(source_id, 'CD_MemberList', target)
|
|
203
|
+
hierarchy.add_node_attribute(source_id, 'HCX::members', [target_id])
|
|
204
|
+
else:
|
|
205
|
+
hierarchy.add_node_attribute(source_id, 'CD_MemberList',
|
|
206
|
+
node_attr['CD_MemberList'] + ' ' + target)
|
|
207
|
+
node_attr['HCX::members'].append(target_id)
|
|
208
|
+
hierarchy.add_node_attribute(source_id, 'HCX::members', node_attr['HCX::members'])
|
|
209
|
+
else:
|
|
210
|
+
source_id = hierarchy.lookup_node_id_by_name(source)
|
|
211
|
+
if source_id is None:
|
|
212
|
+
source_id = hierarchy.add_node(attributes={constants.NODE_NAME_EXPANDED: source})
|
|
213
|
+
target_id = hierarchy.lookup_node_id_by_name(target)
|
|
214
|
+
if target_id is None:
|
|
215
|
+
target_id = hierarchy.add_node(attributes={constants.NODE_NAME_EXPANDED: target})
|
|
216
|
+
hierarchy.add_edge(source=source_id, target=target_id)
|
|
217
|
+
|
|
218
|
+
self._update_memberlist(hierarchy=hierarchy, interactome=interactome)
|
|
219
|
+
root_nodes = hcx_utils.get_root_nodes(hierarchy)
|
|
220
|
+
hcx_utils.add_isroot_node_attribute(hierarchy, root_nodes=root_nodes)
|
|
221
|
+
|
|
222
|
+
return hierarchy
|
|
223
|
+
|
|
224
|
+
def _update_memberlist(self, hierarchy=None, interactome=None):
|
|
225
|
+
"""
|
|
226
|
+
Iterates through all nodes of network updating the CD_MemberList and HCX::members by
|
|
227
|
+
aggregating all the gene lists from the node and its children
|
|
228
|
+
|
|
229
|
+
:param hierarchy:
|
|
230
|
+
:type hierarchy: :py:class:`~ndex2.cx2.CX2Network`
|
|
231
|
+
"""
|
|
232
|
+
for node_id, node_obj in hierarchy.get_nodes().items():
|
|
233
|
+
children_nodes = self._get_children_of_node(hierarchy, node_id)
|
|
234
|
+
children_nodes.add(node_id)
|
|
235
|
+
member_list, member_ids = self._get_memberlists_from_nodes(hierarchy, interactome, children_nodes)
|
|
236
|
+
hierarchy.add_node_attribute(node_id, 'CD_MemberList', ' '.join(member_list))
|
|
237
|
+
hierarchy.add_node_attribute(node_id, 'HCX::members', member_ids)
|
|
238
|
+
|
|
239
|
+
def _get_children_of_node(self, network=None, node_id=None):
|
|
240
|
+
"""
|
|
241
|
+
Gets children of node as set. It is assumed the child node is the target
|
|
242
|
+
of the edge.
|
|
243
|
+
|
|
244
|
+
:param network:
|
|
245
|
+
:type network: :py:class:`~ndex2.cx2.CX2Network`
|
|
246
|
+
:param node_id: node whose children should be found
|
|
247
|
+
:return: id of children nodes
|
|
248
|
+
:rtype: set
|
|
249
|
+
"""
|
|
250
|
+
child_set = set()
|
|
251
|
+
for edge_id, edge_obj in network.get_edges().items():
|
|
252
|
+
if edge_obj[constants.EDGE_SOURCE] == node_id:
|
|
253
|
+
if edge_obj[constants.EDGE_TARGET] not in child_set:
|
|
254
|
+
child_set.add(edge_obj[constants.EDGE_TARGET])
|
|
255
|
+
child_set.update(self._get_children_of_node(network=network,
|
|
256
|
+
node_id=edge_obj[constants.EDGE_TARGET]))
|
|
257
|
+
return child_set
|
|
258
|
+
|
|
259
|
+
@staticmethod
|
|
260
|
+
def _get_memberlists_from_nodes(hierarchy=None, interactome=None, node_set=None):
|
|
261
|
+
"""
|
|
262
|
+
Gets set of genes from all nodes in **node_set**
|
|
263
|
+
|
|
264
|
+
:param hierarchy:
|
|
265
|
+
:type hierarchy: :py:class:`~ndex2.cx2.CX2Network`
|
|
266
|
+
:param interactome:
|
|
267
|
+
:type interactome: :py:class:`~ndex2.cx2.CX2Network`
|
|
268
|
+
:param node_set:
|
|
269
|
+
:type node_set: set
|
|
270
|
+
:return: gene_set
|
|
271
|
+
:rtype: list
|
|
272
|
+
:return: hcx_set
|
|
273
|
+
:rtype: list
|
|
274
|
+
"""
|
|
275
|
+
gene_set = set()
|
|
276
|
+
hcx_set = set()
|
|
277
|
+
for node_id in node_set:
|
|
278
|
+
member_list = hierarchy.get_node(node_id).get(constants.ASPECT_VALUES, {}).get('CD_MemberList', None)
|
|
279
|
+
if member_list is None:
|
|
280
|
+
continue
|
|
281
|
+
member_set = set(member_list.split(' '))
|
|
282
|
+
gene_set.update(member_set)
|
|
283
|
+
for member in list(member_set):
|
|
284
|
+
hcx_set.add(interactome.lookup_node_id_by_name(member))
|
|
285
|
+
return list(gene_set), list(hcx_set)
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
import ndex2
|
|
5
|
+
from ndex2.cx2 import CX2Network, RawCX2NetworkFactory
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_host_and_uuid_from_network_url(network_url):
|
|
9
|
+
if network_url is None:
|
|
10
|
+
return None, None
|
|
11
|
+
parts = network_url.replace('https://', '').replace('http:/', '').split('/')
|
|
12
|
+
host, uuid = parts[0], parts[-1]
|
|
13
|
+
return host, uuid
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_interactome(host, uuid, username, password, parent_edgelist):
|
|
17
|
+
"""
|
|
18
|
+
Retrieves the interactome either from NDEx or from a local edge list.
|
|
19
|
+
|
|
20
|
+
:return: A CX2Network object representing the interactome.
|
|
21
|
+
:rtype: CX2Network
|
|
22
|
+
"""
|
|
23
|
+
interactome = CX2Network()
|
|
24
|
+
if parent_edgelist is not None:
|
|
25
|
+
with open(parent_edgelist, 'r') as f:
|
|
26
|
+
for line in f:
|
|
27
|
+
parts = line.split()
|
|
28
|
+
id_source_node = interactome.lookup_node_id_by_name(parts[0])
|
|
29
|
+
source_node = id_source_node if id_source_node is not None else interactome.add_node(
|
|
30
|
+
attributes={'name': parts[0]})
|
|
31
|
+
id_target_node = interactome.lookup_node_id_by_name(parts[1])
|
|
32
|
+
target_node = id_target_node if id_target_node is not None else interactome.add_node(
|
|
33
|
+
attributes={'name': parts[1]})
|
|
34
|
+
interactome.add_edge(source=source_node, target=target_node)
|
|
35
|
+
elif host is not None and uuid is not None:
|
|
36
|
+
client = ndex2.client.Ndex2(host=host, username=username, password=password)
|
|
37
|
+
factory = RawCX2NetworkFactory()
|
|
38
|
+
client_resp = client.get_network_as_cx2_stream(uuid)
|
|
39
|
+
interactome = factory.get_cx2network(json.loads(client_resp.content))
|
|
40
|
+
return interactome
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def get_root_nodes(hierarchy):
|
|
44
|
+
"""
|
|
45
|
+
In CDAPS the root node has only source edges to children
|
|
46
|
+
so this function counts up number of target edges for each node
|
|
47
|
+
and the one with 0 is the root
|
|
48
|
+
|
|
49
|
+
:return: root node ids
|
|
50
|
+
:rtype: set
|
|
51
|
+
"""
|
|
52
|
+
all_nodes = set()
|
|
53
|
+
for node_id, node_obj in hierarchy.get_nodes().items():
|
|
54
|
+
all_nodes.add(node_id)
|
|
55
|
+
|
|
56
|
+
nodes_with_targets = set()
|
|
57
|
+
for edge_id, edge_obj in hierarchy.get_edges().items():
|
|
58
|
+
nodes_with_targets.add(edge_obj['t'])
|
|
59
|
+
return all_nodes.difference(nodes_with_targets)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def add_isroot_node_attribute(hierarchy, root_nodes=None):
|
|
63
|
+
"""
|
|
64
|
+
Using the **root_nodes** set or list, add
|
|
65
|
+
``HCX::isRoot`` to
|
|
66
|
+
every node setting value to ``True``
|
|
67
|
+
if node id is in **root_nodes**
|
|
68
|
+
otherwise set the value to ``False``
|
|
69
|
+
"""
|
|
70
|
+
attr_name = 'HCX::isRoot'
|
|
71
|
+
for node_id, node_obj in hierarchy.get_nodes().items():
|
|
72
|
+
if node_id in root_nodes:
|
|
73
|
+
hierarchy.add_node_attribute(node_id, attr_name, True)
|
|
74
|
+
else:
|
|
75
|
+
hierarchy.add_node_attribute(node_id, attr_name, False)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def add_hcx_network_annotations(hierarchy, interactome, output_dir, interactome_name, host, uuid):
|
|
79
|
+
hierarchy.add_network_attribute('ndexSchema', 'hierarchy_v0.1',
|
|
80
|
+
datatype='string')
|
|
81
|
+
hierarchy.add_network_attribute('HCX::modelFileCount', '2',
|
|
82
|
+
datatype='integer')
|
|
83
|
+
|
|
84
|
+
if host and uuid:
|
|
85
|
+
hierarchy.add_network_attribute('HCX::interactionNetworkHost', host)
|
|
86
|
+
hierarchy.add_network_attribute('HCX::interactionNetworkUUID', uuid)
|
|
87
|
+
else:
|
|
88
|
+
interactome_path = os.path.join(output_dir, interactome_name)
|
|
89
|
+
interactome.write_as_raw_cx2(interactome_path)
|
|
90
|
+
hierarchy.add_network_attribute('HCX::interactionNetworkName', interactome_name,
|
|
91
|
+
datatype='string')
|
|
92
|
+
return hierarchy
|