How to use pybel - 10 common examples

To help you get started, we’ve selected a few pybel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pybel / pybel / tests / test_manager / test_manager_drop.py View on Github external
def setUp(self):
        super(TestCascades, self).setUp()

        self.n1 = Node._start_from_base_entity(yfg1)
        self.n2 = Node._start_from_base_entity(yfg2)
        self.n3 = Node._start_from_base_entity(yfg3)
        self.e1 = make_increase_edge(self.n1, self.n2)
        self.e2 = make_increase_edge(self.n2, self.n3)
        self.e3 = make_increase_edge(self.n1, self.n3)
        self.g1 = Network(name=n(), version=n(), edges=[self.e1, self.e2, self.e3])
        self.g2 = Network(name=n(), version=n(), edges=[self.e1])

        self.manager.session.add_all([self.n1, self.n2, self.n3, self.e1, self.e2, self.e3, self.g1, self.g2])
        self.manager.session.commit()

        self.assertEqual(3, self.manager.count_nodes())
        self.assertEqual(3, self.manager.count_edges())
        self.assertEqual(2, self.manager.count_networks())
github pybel / pybel / tests / test_manager_graph.py View on Github external
def help_reconstitute(self, node_data, namespace_dict, number_nodes, number_edges):
        """Helps test the round-trip conversion from PyBEL data dictionary to node model, then back to PyBEL node
        data dictionary and PyBEL node tuple.

        :param dict node_data: PyBEL node data dictionary
        """
        graph = BELGraph(name='test', version='0.0.0')
        make_dummy_namespaces(self.manager, graph, namespace_dict)

        node_tuple = graph.add_node_from_data(node_data)

        self.manager.insert_graph(graph, store_parts=True)
        self.assertEqual(number_nodes, self.manager.count_nodes())
        self.assertEqual(number_edges, self.manager.count_edges())

        node = self.manager.get_or_create_node(graph, node_tuple)
        self.manager.session.commit()

        self.assertEqual(node_data, node.to_json())
        self.assertEqual(node_tuple, node.to_tuple())

        self.assertEqual(node, self.manager.get_node_by_tuple(node_tuple))
github pybel / pybel / tests / test_struct / test_transformations / test_induction.py View on Github external
def test_get_upstream_causal_subgraph(self):
        """Test get_upstream_causal_subgraph."""
        a, b, c, d, e, f = [protein(namespace='test', name=n()) for _ in range(6)]

        universe = BELGraph()
        universe.namespace_pattern['test'] = 'test-url'
        universe.add_increases(a, b, citation=n(), evidence=n())
        universe.add_increases(b, c, citation=n(), evidence=n())
        universe.add_association(d, a, citation=n(), evidence=n())
        universe.add_increases(e, a, citation=n(), evidence=n())
        universe.add_decreases(f, b, citation=n(), evidence=n())

        subgraph = get_upstream_causal_subgraph(universe, [a, b])

        self.assertIsInstance(subgraph, BELGraph)
        self.assert_all_nodes_are_base_entities(subgraph)

        self.assertIn('test', subgraph.namespace_pattern)
        self.assertEqual('test-url', subgraph.namespace_pattern['test'])

        self.assertIn(a, subgraph)
        self.assertIn(b, subgraph)
        self.assertNotIn(c, subgraph)
        self.assertNotIn(d, subgraph)
        self.assertIn(e, subgraph)
github pybel / pybel / tests / test_io / test_tsv.py View on Github external
def help_test_convert(
        self,
        converter: Type[Converter],
        u: BaseEntity,
        v: BaseEntity,
        edge_data: EdgeData,
        triple: Tuple[str, str, str],
    ) -> None:
        """Test a converter class."""
        self.assertTrue(issubclass(converter, Converter), msg='Not a Converter: {}'.format(converter.__name__))
        key = n()
        self.assertTrue(
            converter.predicate(u, v, key, edge_data),
            msg='Predicate failed: {}'.format(converter.__name__),
        )
        self.assertEqual(
            triple,
            converter.convert(u, v, key, edge_data),
            msg='Conversion failed: {}'.format(converter.__name__),
        )
        graph = BELGraph()
        graph.add_edge(u, v, key=key, **edge_data)
        self.assertEqual(
            triple,
            get_triple(graph, u, v, key),
            msg='get_triple failed: {}'.format(converter.__name__),
        )
github pybel / pybel / tests / test_struct / test_summary / test_summary_nodes.py View on Github external
def test_get_top_hubs(self):
        """Test counting pathologies in the graph."""
        graph = BELGraph()
        a, b, c = protein(n(), n()), protein(n(), n()), pathology(n(), n())

        graph.add_association(a, b, citation=n(), evidence=n())
        graph.add_association(a, c, citation=n(), evidence=n())

        top_hubs = get_top_hubs(graph, n=1)
        self.assertEqual(1, len(top_hubs))
        node, degree = top_hubs[0]
        self.assertEqual(a, node)
        self.assertEqual(4, degree)  # higher than expected because association edges
github pybel / pybel / tests / test_struct / test_transformations / test_induction.py View on Github external
def test_build_author_inclusion_filter(self):
        """Test getting a sub-graph by a single author."""
        a, b, c, d = [protein(namespace='test', name=n()) for _ in range(4)]
        a1, a2, a3, a4, a5 = n(), n(), n(), n(), n()

        c1 = {
            CITATION_TYPE: CITATION_TYPE_PUBMED,
            CITATION_REFERENCE: n(),
            CITATION_AUTHORS: [a1, a2, a3]
        }
        c2 = {
            CITATION_TYPE: CITATION_TYPE_PUBMED,
            CITATION_REFERENCE: n(),
            CITATION_AUTHORS: [a1, a4]
        }

        graph = BELGraph()
        keyword, url = n(), n()
        graph.namespace_url[keyword] = url
        graph.add_increases(a, b, evidence=n(), citation=c1)
        graph.add_increases(a, b, evidence=n(), citation=c2)
        graph.add_increases(b, c, evidence=n(), citation=c1)
        graph.add_increases(c, d, evidence=n(), citation=c2)

        subgraph1 = get_subgraph_by_authors(graph, a1)

        self.assertIsInstance(subgraph1, BELGraph)
        self.assert_all_nodes_are_base_entities(subgraph1)
github pybel / pybel / tests / test_struct / test_transformations / test_induction.py View on Github external
def test_build_author_set_inclusion_filter(self):
        """Test getting a sub-graph by a set of authors."""
        a, b, c, d = [protein(namespace='test', name=n()) for _ in range(4)]
        a1, a2, a3, a4 = n(), n(), n(), n()

        c1 = {
            CITATION_TYPE: CITATION_TYPE_PUBMED,
            CITATION_REFERENCE: n(),
            CITATION_AUTHORS: [a1, a2, a3]
        }
        c2 = {
            CITATION_TYPE: CITATION_TYPE_PUBMED,
            CITATION_REFERENCE: n(),
            CITATION_AUTHORS: [a1, a4]
        }

        graph = BELGraph()
        keyword, url = n(), n()
        graph.namespace_url[keyword] = url
        graph.add_increases(a, b, evidence=n(), citation=c1)
github pybel / pybel / tests / test_manager / test_manager_graph.py View on Github external
def test_pmod_default_full(self, mock):
        dummy_namespace = n()
        dummy_name = n()

        node_data = Protein(namespace=dummy_namespace, name=dummy_name,
                            variants=[ProteinModification('Me', code='Ser', position=5)])
        self._help_reconstitute(node_data, 2, 1)
github pybel / pybel / tests / test_struct / test_filters / test_struct_filters.py View on Github external
def test_all_filter_no_query(self):
        """Test that the all filter returns true when there's no argument"""
        graph = BELGraph()
        graph.add_increases(Protein(n(), n()), Protein(n(), n()), citation=n(), evidence=n())
        self.assertEqual(1, count_passed_edge_filter(graph, build_annotation_dict_all_filter({})))
github pybel / pybel / tests / test_manager / test_manager_graph.py View on Github external
from pybel.manager import models
from pybel.manager.models import Author, Citation, Edge, Evidence, NamespaceEntry, Node
from pybel.testing.cases import FleetingTemporaryCacheMixin, TemporaryCacheClsMixin, TemporaryCacheMixin
from pybel.testing.constants import test_bel_simple
from pybel.testing.mocks import mock_bel_resources
from pybel.testing.utils import make_dummy_annotations, make_dummy_namespaces, n
from tests.constants import (
    BelReconstitutionMixin, akt1, casp8, egfr, expected_test_simple_metadata, fadd, test_citation_dict,
    test_evidence_text,
)

fos = hgnc(name='FOS')
jun = hgnc(name='JUN')
mirna_1 = mirbase(name=n())
mirna_2 = mirbase(name=n())
pathology_1 = Pathology('DO', n())
ap1_complex = ComplexAbundance([fos, jun])

egfr_dimer = ComplexAbundance([egfr, egfr])

yfg_data = hgnc(name='YFG')
e2f4_data = hgnc(name='E2F4')
bound_ap1_e2f4 = ComplexAbundance([ap1_complex, e2f4_data])

superoxide = chebi(name='superoxide')
hydrogen_peroxide = chebi(name='hydrogen peroxide')
oxygen = chebi(name='oxygen')
superoxide_decomposition = Reaction(reactants=[superoxide], products=[hydrogen_peroxide, oxygen])


def assert_unqualified_edge(test_case, u: BaseEntity, v: BaseEntity, rel: str) -> None:
    """Assert there's only one edge and get the data for it"""