File: test_expansion.py

package info (click to toggle)
pybel 0.15.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,496 kB
  • sloc: python: 29,392; javascript: 246; makefile: 226; sh: 20
file content (99 lines) | stat: -rw-r--r-- 3,389 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-

"""Tests for expansion functions."""

import unittest

from pybel import BELGraph
from pybel.examples.sialic_acid_example import (
    cd33,
    cd33_phosphorylated,
    shp1,
    shp2,
    sialic_acid,
    sialic_acid_cd33_complex,
    sialic_acid_graph,
    syk,
)
from pybel.struct.mutation.expansion.neighborhood import (
    expand_node_neighborhood,
    expand_node_predecessors,
    expand_node_successors,
    expand_nodes_neighborhoods,
)


class TestExpansion(unittest.TestCase):
    """Test expansion functions."""

    def test_neighborhood(self):
        """Test expansion around the neighborhood of CD33 in the sialic acid graph given node."""
        graph = BELGraph()
        graph.add_node_from_data(cd33)
        self.assertEqual(1, graph.number_of_nodes())

        _sialic_acid_graph = sialic_acid_graph.copy()
        expand_node_neighborhood(graph=graph, universe=_sialic_acid_graph, node=cd33)

        self.assertEqual(
            {cd33, sialic_acid, sialic_acid_cd33_complex, cd33_phosphorylated},
            set(graph),
        )

    def test_neighborhood_with_predecessors(self):
        """Test expansion on the predecessors of a given node."""
        graph = BELGraph()
        graph.add_node_from_data(cd33)
        graph.add_node_from_data(sialic_acid_cd33_complex)
        self.assertEqual(3, graph.number_of_nodes())

        _sialic_acid_graph = sialic_acid_graph.copy()
        expand_node_predecessors(universe=_sialic_acid_graph, graph=graph, node=cd33)

        self.assertEqual(4, graph.number_of_nodes())
        self.assertIn(sialic_acid, graph)
        self.assertIn(sialic_acid_cd33_complex, graph)
        self.assertIn(cd33_phosphorylated, graph)

    def test_neighborhood_with_successors(self):
        """Test expansion on the successors of a given node."""
        graph = BELGraph()
        graph.add_node_from_data(cd33)
        graph.add_node_from_data(cd33_phosphorylated)
        self.assertEqual(2, graph.number_of_nodes())

        _sialic_acid_graph = sialic_acid_graph.copy()
        expand_node_successors(universe=_sialic_acid_graph, graph=graph, node=cd33)

        self.assertEqual(
            {sialic_acid_cd33_complex, sialic_acid, cd33_phosphorylated, cd33},
            set(graph),
        )

    def test_neighborhoods(self):
        """Test expansion on the neighborhood of given nodes.

        The edge between PTPN6/CD33ph should not be added.
        """
        graph = BELGraph()
        graph.add_node_from_data(cd33)
        graph.add_node_from_data(syk)
        self.assertEqual(2, graph.number_of_nodes())

        _sialic_acid_graph = sialic_acid_graph.copy()
        expand_nodes_neighborhoods(universe=_sialic_acid_graph, graph=graph, nodes=[cd33, syk])

        self.assertNotIn(shp1, graph[cd33_phosphorylated])
        self.assertNotIn(shp2, graph[cd33_phosphorylated])

        self.assertEqual(
            9,
            graph.number_of_nodes(),
            msg="wrong number of nodes: {}".format(list(graph)),
        )
        self.assertEqual(8, graph.number_of_edges(), msg="wrong number of edges")
        self.assertIn(sialic_acid, graph)
        self.assertIn(sialic_acid_cd33_complex, graph)
        self.assertIn(cd33_phosphorylated, graph)

    # TODO test that if new nodes with metadata that's missing (namespace_url definition, etc) then that gets added too