File: test_collapse.py

package info (click to toggle)
pybel 0.15.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 16,492 kB
  • sloc: python: 29,392; javascript: 246; makefile: 226; sh: 20
file content (148 lines) | stat: -rw-r--r-- 4,313 bytes parent folder | download | duplicates (3)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-

"""Tests for collapse functions."""

import unittest

from pybel import BELGraph
from pybel.constants import DIRECTLY_INCREASES
from pybel.dsl import gene, mirna, pathology, pmod, protein, rna
from pybel.struct.mutation.collapse import (
    collapse_all_variants,
    collapse_nodes,
    collapse_to_genes,
    surviors_are_inconsistent,
)
from pybel.testing.utils import n

HGNC = "HGNC"
GO = "GO"
CHEBI = "CHEBI"

g1 = gene(HGNC, "1")
r1 = rna(HGNC, "1")
p1 = protein(HGNC, "1")
p1_phosphorylated = protein(HGNC, "1", variants=[pmod("Ph")])

g2 = gene(HGNC, "2")
r2 = rna(HGNC, "2")
p2 = protein(HGNC, "2")

g3 = gene(HGNC, "3")
r3 = rna(HGNC, "3")
p3 = protein(HGNC, "3")

g4 = gene(HGNC, "4")
m4 = mirna(HGNC, "4")

p5 = pathology(GO, "5")


class TestCollapse(unittest.TestCase):
    """Tests for collapse functions."""

    def test_check_survivors_consistent(self):
        """Test the survivor mapping is consistent."""
        inconsistencies = surviors_are_inconsistent(
            {
                1: {2},
                3: {4},
            }
        )
        self.assertEqual(0, len(inconsistencies))
        self.assertFalse(inconsistencies)

        inconsistencies = surviors_are_inconsistent(
            {
                1: {2},
                2: {3},
                3: {4},
                5: {4},
            }
        )
        self.assertEqual(2, len(inconsistencies))
        self.assertIn(2, inconsistencies)
        self.assertIn(3, inconsistencies)

    def test_collapse_by_dict(self):
        """Test collapsing nodes by a dictionary."""
        graph = BELGraph()
        graph.add_node_from_data(p1)
        graph.add_node_from_data(p2)
        graph.add_node_from_data(p3)

        graph.add_increases(p1, p3, citation=n(), evidence=n())
        graph.add_qualified_edge(p2, p3, relation=DIRECTLY_INCREASES, citation=n(), evidence=n())

        self.assertEqual(3, graph.number_of_nodes())
        self.assertEqual(2, graph.number_of_edges())

        d = {p1: {p2}}

        collapse_nodes(graph, d)

        self.assertEqual({p1, p3}, set(graph))
        self.assertEqual({(p1, p3), (p1, p3)}, set(graph.edges()))
        self.assertEqual(2, graph.number_of_edges(), msg=graph.edges(data=True, keys=True))

    def test_collapse_dogma_1(self):
        """Test collapsing to genes, only with translations."""
        graph = BELGraph()
        graph.add_translation(r1, p1)

        self.assertEqual(2, graph.number_of_nodes())
        self.assertEqual(1, graph.number_of_edges())

        collapse_to_genes(graph)

        self.assertIn(g1, graph)
        self.assertEqual(1, graph.number_of_nodes())
        self.assertEqual(0, graph.number_of_edges())

    def test_collapse_dogma_2(self):
        """Test collapsing to genes with translations and transcriptions."""
        graph = BELGraph()
        graph.add_transcription(g1, r1)
        graph.add_translation(r1, p1)

        self.assertEqual(3, graph.number_of_nodes())
        self.assertEqual(2, graph.number_of_edges())

        collapse_to_genes(graph)

        self.assertIn(g1, graph)
        self.assertEqual(1, graph.number_of_nodes())
        self.assertEqual(0, graph.number_of_edges())

    def test_collapse_dogma_3(self):
        """Test collapsing to genes, only with transcriptions."""
        graph = BELGraph()
        graph.add_transcription(g1, r1)

        self.assertEqual(2, graph.number_of_nodes())
        self.assertEqual(1, graph.number_of_edges())

        collapse_to_genes(graph)

        self.assertIn(g1, graph)
        self.assertEqual(1, graph.number_of_nodes())
        self.assertEqual(0, graph.number_of_edges())

    def test_collapse_all_variants(self):
        """Test collapsing all variants to their reference nodes."""
        graph = BELGraph()
        graph.add_node_from_data(p1_phosphorylated)

        graph.add_increases(p1_phosphorylated, p2, citation=n(), evidence=n())

        self.assertEqual(3, graph.number_of_nodes())
        self.assertEqual(2, graph.number_of_edges())

        collapse_all_variants(graph)

        self.assertEqual(2, graph.number_of_nodes())
        self.assertEqual(1, graph.number_of_edges())

        self.assertIn(p1, graph)
        self.assertNotIn(p1_phosphorylated, graph)
        self.assertIn(p2, graph)