def get_graphs1(): """ Returns instances of defined graphs. """ g1 = NxGraph() g1.add_edge('B', 'A', **{'predicate': 'biolink:sub_class_of'}) g1.add_edge('C', 'B', **{'predicate': 'biolink:sub_class_of'}) g1.add_edge('D', 'C', **{'predicate': 'biolink:sub_class_of'}) g1.add_edge('D', 'A', **{'predicate': 'biolink:related_to'}) g1.add_edge('E', 'D', **{'predicate': 'biolink:sub_class_of'}) g1.add_edge('F', 'D', **{'predicate': 'biolink:sub_class_of'}) g2 = NxGraph() g2.name = 'Graph 1' g2.add_node( 'HGNC:12345', id='HGNC:12345', name='Test Gene', category=['biolink:NamedThing'], alias='NCBIGene:54321', same_as='UniProtKB:54321', ) g2.add_node('B', id='B', name='Node B', category=['biolink:NamedThing'], alias='Z') g2.add_node('C', id='C', name='Node C', category=['biolink:NamedThing']) g2.add_edge( 'C', 'B', edge_key='C-biolink:subclass_of-B', subject='C', object='B', predicate='biolink:subclass_of', relation='rdfs:subClassOf', provided_by='Graph 1', publications=[1], pubs=['PMID:123456'], ) g2.add_edge( 'B', 'A', edge_key='B-biolink:subclass_of-A', subject='B', object='A', predicate='biolink:subclass_of', relation='rdfs:subClassOf', provided_by='Graph 1', ) g2.add_edge( 'C', 'c', edge_key='C-biolink:exact_match-B', subject='C', object='c', predicate='biolink:exact_match', relation='skos:exactMatch', provided_by='Graph 1', ) return [g1, g2]
def get_graphs(): """ Returns instances of defined graphs. """ g1 = NxGraph() g1.add_edge("B", "A", **{"predicate": "biolink:sub_class_of"}) g1.add_edge("C", "B", **{"predicate": "biolink:sub_class_of"}) g1.add_edge("D", "C", **{"predicate": "biolink:sub_class_of"}) g1.add_edge("D", "A", **{"predicate": "biolink:related_to"}) g1.add_edge("E", "D", **{"predicate": "biolink:sub_class_of"}) g1.add_edge("F", "D", **{"predicate": "biolink:sub_class_of"}) g2 = NxGraph() g2.name = "Graph 1" g2.add_node( "HGNC:12345", id="HGNC:12345", name="Test Gene", category=["biolink:NamedThing"], alias="NCBIGene:54321", same_as="UniProtKB:54321", ) g2.add_node("B", id="B", name="Node B", category=["biolink:NamedThing"], alias="Z") g2.add_node("C", id="C", name="Node C", category=["biolink:NamedThing"]) g2.add_edge( "C", "B", edge_key="C-biolink:subclass_of-B", subject="C", object="B", predicate="biolink:subclass_of", relation="rdfs:subClassOf", provided_by="Graph 1", publications=[1], pubs=["PMID:123456"], ) g2.add_edge( "B", "A", edge_key="B-biolink:subclass_of-A", subject="B", object="A", predicate="biolink:subclass_of", relation="rdfs:subClassOf", provided_by="Graph 1", ) g2.add_edge( "C", "c", edge_key="C-biolink:exact_match-B", subject="C", object="c", predicate="biolink:exact_match", relation="skos:exactMatch", provided_by="Graph 1", ) return [g1, g2]
def test_validator_explicit_biolink_version(): """ A fake test to establish a success condition for validation. """ G = NxGraph() G.add_node( "CHEMBL.COMPOUND:1222250", id="CHEMBL.COMPOUND:1222250", name="Dextrose", category=["Carbohydrate"], ) G.add_node( "UBERON:0000001", id="UBERON:0000001", name="fake", category=["NamedThing"] ) G.add_edge( "CHEMBL.COMPOUND:1222250", "UBERON:0000001", id="CHEMBL.COMPOUND:1222250-part_of-UBERON:0000001", relation="RO:1", predicate="part_of", subject="CHEMBL.COMPOUND:1222250", object="UBERON:0000001", category=["biolink:Association"], ) Validator.set_biolink_model(version="1.8.2") validator = Validator(verbose=True) validator.validate(G) print(validator.get_errors()) assert len(validator.get_errors()) == 0
def test_validator_good(): """ A fake test to establish a success condition for validation. """ G = NxGraph() G.add_node( "UniProtKB:P123456", id="UniProtKB:P123456", name="fake", category=["Protein"] ) G.add_node( "UBERON:0000001", id="UBERON:0000001", name="fake", category=["NamedThing"] ) G.add_node( "UBERON:0000002", id="UBERON:0000002", name="fake", category=["NamedThing"] ) G.add_edge( "UBERON:0000001", "UBERON:0000002", id="UBERON:0000001-part_of-UBERON:0000002", relation="RO:1", predicate="part_of", subject="UBERON:0000001", object="UBERON:0000002", category=["biolink:Association"], ) validator = Validator(verbose=True) validator.validate(G) print(validator.get_errors()) assert len(validator.get_errors()) == 0
def test_validator_good(): """ A fake test to establish a success condition for validation. """ G = NxGraph() G.add_node('UniProtKB:P123456', id='UniProtKB:P123456', name='fake', category=['Protein']) G.add_node('UBERON:0000001', id='UBERON:0000001', name='fake', category=['NamedThing']) G.add_node('UBERON:0000002', id='UBERON:0000002', name='fake', category=['NamedThing']) G.add_edge( 'UBERON:0000001', 'UBERON:0000002', id='UBERON:0000001-part_of-UBERON:0000002', relation='RO:1', predicate='part_of', subject='UBERON:0000001', object='UBERON:0000002', category=['biolink:Association'], ) validator = Validator(verbose=True) e = validator.validate(G) print(validator.report(e)) assert len(e) == 0
def test_clique_merge8(): """ Test for clique merge where same_as appear as both node and edge properties. """ ppm = {"biolink:Gene": ["HGNC", "NCBIGene", "ENSEMBL", "OMIM"]} g1 = NxGraph() g1.add_node("HGNC:1", **{"category": ["biolink:Gene"]}) g1.add_node("OMIM:2", **{"category": ["biolink:Gene"], "same_as": ["HGNC:1"]}) g1.add_node("NCBIGene:3", **{"category": ["biolink:NamedThing"]}) g1.add_node("ENSEMBL:4", **{"category": ["biolink:Gene"], "same_as": ["HGNC:1"]}) g1.add_node( "ENSEMBL:6", **{"category": ["biolink:Gene"], "same_as": ["NCBIGene:8"]} ) g1.add_node("HGNC:7", **{"category": ["biolink:Gene"]}) g1.add_node("NCBIGene:8", **{"category": ["biolink:Gene"]}) g1.add_edge( "NCBIGene:3", "HGNC:1", edge_key=generate_edge_key("NCBIGene:3", "biolink:same_as", "HGNC:1"), **{"predicate": "biolink:same_as", "relation": "owl:equivalentClass"} ) g1.add_edge( "ENSEMBL:6", "NCBIGene:8", edge_key=generate_edge_key("ENSEMBL:6", "biolink:same_as", "NCBIGene:8"), **{"predicate": "biolink:same_as", "relation": "owl:equivalentClass"} ) g1.add_edge( "HGNC:7", "NCBIGene:8", edge_key=generate_edge_key("HGNC:7", "biolink:same_as", "NCBIGene:8"), **{"predicate": "biolink:same_as", "relation": "owl:equivalentClass"} ) updated_graph, clique_graph = clique_merge( target_graph=g1, prefix_prioritization_map=ppm ) assert updated_graph.number_of_nodes() == 2 assert updated_graph.number_of_edges() == 0 assert updated_graph.has_node("HGNC:1") assert updated_graph.has_node("HGNC:7") n1 = updated_graph.nodes()["HGNC:1"] assert "OMIM:2" in n1["same_as"] assert "NCBIGene:3" in n1["same_as"] assert "ENSEMBL:4" in n1["same_as"] n2 = updated_graph.nodes()["HGNC:7"] assert "ENSEMBL:6" in n2["same_as"] assert "NCBIGene:8" in n2["same_as"] assert not updated_graph.has_node("OMIM:2") assert not updated_graph.has_node("NCBIGene:3") assert not updated_graph.has_node("ENSEMBL:4") assert not updated_graph.has_node("ENSEMBL:6") assert not updated_graph.has_node("NCBIGene:8")
def test_add_node(): """ Test adding a node to an NxGraph. """ g = NxGraph() g.add_node('A') g.add_node('A', name='Node A', description='Node A') assert g.has_node('A')
def test_add_edge_attribute(): """ Test adding an edge attribute to an NxGraph. """ g = NxGraph() g.add_edge("A", "B") g.add_edge_attribute("A", "B", "edge_ab", "predicate", "biolink:related_to")
def test_add_edge_attribute(): """ Test adding an edge attribute to an NxGraph. """ g = NxGraph() g.add_edge('A', 'B') g.add_edge_attribute('A', 'B', 'edge_ab', 'predicate', 'biolink:related_to')
def test_add_node(): """ Test adding a node to an NxGraph. """ g = NxGraph() g.add_node("A") g.add_node("A", name="Node A", description="Node A") assert g.has_node("A")
def test_set_node_attributes(): """ Test setting node attributes in bulk. """ g = NxGraph() g.add_node("X:1", alias="A:1") g.add_node("X:2", alias="B:2") d = {"X:1": {"alias": "ABC:1"}, "X:2": {"alias": "DEF:2"}} NxGraph.set_node_attributes(g, d)
def test_add_node_attribute(): """ Test adding a node attribute to an NxGraph. """ g = NxGraph() g.add_node("A") g.add_node_attribute("A", "provided_by", "test") n = g.get_node("A") assert "provided_by" in n and n["provided_by"] == "test"
def test_set_node_attributes(): """ Test setting node attributes in bulk. """ g = NxGraph() g.add_node("X:1", alias="A:1") g.add_node("X:2", alias="B:2") d = {'X:1': {'alias': 'ABC:1'}, 'X:2': {'alias': 'DEF:2'}} NxGraph.set_node_attributes(g, d)
def test_update_edge_attribute(): """ Test updating an edge attribute for an edge in an NxGraph. """ g = NxGraph() g.add_edge('A', 'B', 'edge_ab') g.update_edge_attribute('A', 'B', 'edge_ab', 'source', 'test') e = g.get_edge('A', 'B', 'edge_ab') assert 'source' in e and e['source'] == 'test'
def test_add_node_attribute(): """ Test adding a node attribute to an NxGraph. """ g = NxGraph() g.add_node('A') g.add_node_attribute('A', 'provided_by', 'test') n = g.get_node('A') assert 'provided_by' in n and n['provided_by'] == 'test'
def test_update_edge_attribute(): """ Test updating an edge attribute for an edge in an NxGraph. """ g = NxGraph() g.add_edge("A", "B", "edge_ab") g.update_edge_attribute("A", "B", "edge_ab", "source", "test") e = g.get_edge("A", "B", "edge_ab") assert "source" in e and e["source"] == "test"
def test_update_node_attribute(): """ Test updating a node attribute for a node in an NxGraph. """ g = NxGraph() g.add_node('A', name='A', description='Node A') g.update_node_attribute('A', 'description', 'Modified description') n = g.get_node('A') assert 'name' in n and n['name'] == 'A' assert 'description' in n and n['description'] == 'Modified description'
def test_merge_graphs(): """ Test for merging 3 graphs into one, while not preserving conflicting node and edge properties. """ graphs = get_graphs() merged_graph = merge_graphs(NxGraph(), graphs) assert merged_graph.number_of_nodes() == 6 assert merged_graph.number_of_edges() == 6 assert merged_graph.name not in [x.name for x in graphs]
def test_update_node_attribute(): """ Test updating a node attribute for a node in an NxGraph. """ g = NxGraph() g.add_node("A", name="A", description="Node A") g.update_node_attribute("A", "description", "Modified description") n = g.get_node("A") assert "name" in n and n["name"] == "A" assert "description" in n and n["description"] == "Modified description"
def test_validator_bad(): """ A fake test to establish a fail condition for validation. """ G = NxGraph() G.add_node('x', foo=3) G.add_node('ZZZ:3', **{'nosuch': 1}) G.add_edge('x', 'y', **{'baz': 6}) validator = Validator(verbose=True) e = validator.validate(G) assert len(e) > 0
def test_add_edge(): """ Test adding an edge to an NxGraph. """ g = NxGraph() g.add_node("A") g.add_node("B") g.add_edge("A", "B", predicate="biolink:related_to", provided_by="test") assert g.has_edge("A", "B") g.add_edge("B", "C", edge_key="B-biolink:related_to-C", provided_by="test") assert g.has_edge("B", "C")
def test_validator_bad(): """ A fake test to establish a fail condition for validation. """ G = NxGraph() G.add_node("x", foo=3) G.add_node("ZZZ:3", **{"nosuch": 1}) G.add_edge("x", "y", **{"baz": 6}) validator = Validator(verbose=True) validator.validate(G) assert len(validator.get_errors()) > 0
def test_add_edge(): """ Test adding an edge to an NxGraph. """ g = NxGraph() g.add_node('A') g.add_node('B') g.add_edge('A', 'B', predicate='biolink:related_to', provided_by='test') assert g.has_edge('A', 'B') g.add_edge('B', 'C', edge_key='B-biolink:related_to-C', provided_by='test') assert g.has_edge('B', 'C')
def test_set_edge_attributes(): """ Test setting edge attributes in bulk. """ g = NxGraph() g.add_node("X:1", alias="A:1") g.add_node("X:2", alias="B:2") g.add_edge("X:2", 'X:1', edge_key='edge1', source="Source 1") d = {('X:2', 'X:1', 'edge1'): {'source': 'Modified Source 1'}} NxGraph.set_edge_attributes(g, d) e = list(g.edges(keys=True, data=True))[0] assert e[3]['source'] == 'Modified Source 1'
def test_set_edge_attributes(): """ Test setting edge attributes in bulk. """ g = NxGraph() g.add_node("X:1", alias="A:1") g.add_node("X:2", alias="B:2") g.add_edge("X:2", "X:1", edge_key="edge1", source="Source 1") d = {("X:2", "X:1", "edge1"): {"source": "Modified Source 1"}} NxGraph.set_edge_attributes(g, d) e = list(g.edges(keys=True, data=True))[0] assert e[3]["source"] == "Modified Source 1"
def test_remove_singleton_nodes(): """ Test the remove singleton nodes operation. """ g = NxGraph() g.add_edge("A", "B") g.add_edge("B", "C") g.add_edge("C", "D") g.add_edge("B", "D") g.add_node("X") g.add_node("Y") assert g.number_of_nodes() == 6 assert g.number_of_edges() == 4 remove_singleton_nodes(g) assert g.number_of_nodes() == 4 assert g.number_of_edges() == 4
def test_remove_singleton_nodes(): """ Test the remove singleton nodes operation. """ g = NxGraph() g.add_edge('A', 'B') g.add_edge('B', 'C') g.add_edge('C', 'D') g.add_edge('B', 'D') g.add_node('X') g.add_node('Y') assert g.number_of_nodes() == 6 assert g.number_of_edges() == 4 remove_singleton_nodes(g) assert g.number_of_nodes() == 4 assert g.number_of_edges() == 4
def test_write_tsv2(): """ Write a graph to a TSV archive using TsvSink. """ graph = NxGraph() graph.add_node('A', id='A', **{'name': 'Node A'}) graph.add_node('B', id='B', **{'name': 'Node B'}) graph.add_node('C', id='C', **{'name': 'Node C'}) graph.add_node('D', id='D', **{'name': 'Node D'}) graph.add_node('E', id='E', **{'name': 'Node E'}) graph.add_node('F', id='F', **{'name': 'Node F'}) graph.add_edge('B', 'A', **{'subject': 'B', 'object': 'A', 'predicate': 'biolink:sub_class_of'}) graph.add_edge('C', 'B', **{'subject': 'C', 'object': 'B', 'predicate': 'biolink:sub_class_of'}) graph.add_edge('D', 'C', **{'subject': 'D', 'object': 'C', 'predicate': 'biolink:sub_class_of'}) graph.add_edge('D', 'A', **{'subject': 'D', 'object': 'A', 'predicate': 'biolink:related_to'}) graph.add_edge('E', 'D', **{'subject': 'E', 'object': 'D', 'predicate': 'biolink:sub_class_of'}) graph.add_edge('F', 'D', **{'subject': 'F', 'object': 'D', 'predicate': 'biolink:sub_class_of'}) s = TsvSink( filename=os.path.join(TARGET_DIR, 'test_graph'), format='tsv', compression='tar', node_properties={'id', 'name'}, edge_properties={'subject', 'predicate', 'object', 'relation'}, ) for n, data in graph.nodes(data=True): s.write_node(data) for u, v, k, data in graph.edges(data=True, keys=True): s.write_edge(data) s.finalize() assert os.path.exists(os.path.join(TARGET_DIR, 'test_graph.tar')) s = TsvSink( filename=os.path.join(TARGET_DIR, 'test_graph'), format='tsv', compression='tar.gz', node_properties={'id', 'name'}, edge_properties={'subject', 'predicate', 'object', 'relation'}, ) for n, data in graph.nodes(data=True): s.write_node(data) for u, v, k, data in graph.edges(data=True, keys=True): s.write_edge(data) s.finalize() assert os.path.exists(os.path.join(TARGET_DIR, 'test_graph.tar.gz'))
def test_read_graph2(): """ Read from an NxGraph using GraphSource. This test also supplies the provided_by parameter. """ graph = NxGraph() graph.add_node("A", **{"id": "A", "name": "node A"}) graph.add_node("B", **{"id": "B", "name": "node B"}) graph.add_node("C", **{"id": "C", "name": "node C"}) graph.add_edge( "A", "C", **{ "subject": "A", "predicate": "biolink:related_to", "object": "C", "relation": "biolink:related_to", }) t = Transformer() s = GraphSource(t) g = s.parse(graph=graph, provided_by="Test Graph", knowledge_source="Test Graph") nodes = {} edges = {} for rec in g: if rec: if len(rec) == 4: edges[(rec[0], rec[1], rec[2])] = rec[3] else: nodes[rec[0]] = rec[1] assert len(nodes.keys()) == 3 n1 = nodes["A"] assert n1["id"] == "A" assert n1["name"] == "node A" assert "Test Graph" in n1["provided_by"] assert len(edges.keys()) == 1 e1 = list(edges.values())[0] assert e1["subject"] == "A" assert e1["predicate"] == "biolink:related_to" assert e1["object"] == "C" assert e1["relation"] == "biolink:related_to" assert "Test Graph" in e1["knowledge_source"]
def test_read_graph2(): """ Read from an NxGraph using GraphSource. This test also supplies the provided_by parameter. """ graph = NxGraph() graph.add_node('A', **{'id': 'A', 'name': 'node A'}) graph.add_node('B', **{'id': 'B', 'name': 'node B'}) graph.add_node('C', **{'id': 'C', 'name': 'node C'}) graph.add_edge( 'A', 'C', **{ 'subject': 'A', 'predicate': 'biolink:related_to', 'object': 'C', 'relation': 'biolink:related_to', } ) s = GraphSource() g = s.parse(graph=graph, provided_by='Test Graph') nodes = {} edges = {} for rec in g: if rec: if len(rec) == 4: edges[(rec[0], rec[1], rec[2])] = rec[3] else: nodes[rec[0]] = rec[1] assert len(nodes.keys()) == 3 n1 = nodes['A'] assert n1['id'] == 'A' assert n1['name'] == 'node A' assert 'Test Graph' in n1['provided_by'] assert len(edges.keys()) == 1 e1 = list(edges.values())[0] assert e1['subject'] == 'A' assert e1['predicate'] == 'biolink:related_to' assert e1['object'] == 'C' assert e1['relation'] == 'biolink:related_to' assert 'Test Graph' in e1['provided_by']