File: test_struct_summary_edges.py

package info (click to toggle)
pybel 0.15.5-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,504 kB
  • sloc: python: 29,392; javascript: 246; makefile: 226; sh: 20
file content (139 lines) | stat: -rw-r--r-- 4,337 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
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
# -*- coding: utf-8 -*-

"""Test summary functions for edges."""

import unittest
from collections import Counter

from pybel import BELGraph
from pybel.dsl import protein
from pybel.examples import sialic_acid_graph
from pybel.language import Entity
from pybel.struct.summary.edge_summary import (
    count_annotations,
    get_annotation_values,
    get_annotation_values_by_annotation,
    get_annotations,
    get_unused_annotations,
    get_unused_list_annotation_values,
    iter_annotation_value_pairs,
    iter_annotation_values,
)
from pybel.testing.utils import n


class TestEdgeSummary(unittest.TestCase):
    """Test summary functions for edges."""

    def test_1(self):
        """Test iterating over annotation/value pairs."""
        graph = BELGraph()
        graph.annotation_list.update(
            {
                "A": set("1234"),
                "B": set("XYZ"),
                "C": set("abcde"),
            }
        )
        u = protein("HGNC", name="U")
        v = protein("HGNC", name="V")
        w = protein("HGNC", name="W")

        graph.add_increases(u, v, evidence=n(), citation=n(), annotations={"A": {"1", "2"}, "B": {"X"}})

        graph.add_increases(
            u,
            w,
            evidence=n(),
            citation=n(),
            annotations={
                "A": {"1", "3"},
                "C": {"a"},
            },
        )

        graph.add_increases(
            w,
            v,
            evidence=n(),
            citation=n(),
        )

        x = dict(Counter((key, entity.identifier) for key, entity in iter_annotation_value_pairs(graph)))

        self.assertEqual(
            {
                ("A", "1"): 2,
                ("A", "2"): 1,
                ("A", "3"): 1,
                ("B", "X"): 1,
                ("C", "a"): 1,
            },
            x,
        )

        y = Counter(iter_annotation_values(graph, "A"))
        self.assertEqual(x["A", "1"] + x["A", "2"] + x["A", "3"], sum(y.values()))

        y = Counter(iter_annotation_values(graph, "B"))
        self.assertEqual(x["B", "X"], sum(y.values()))

        y = Counter(iter_annotation_values(graph, "C"))
        self.assertEqual(x["C", "a"], sum(y.values()))

    def test_get_annotation_values(self):
        """Test getting annotation values."""
        expected = {
            "Confidence": {
                Entity(namespace="Confidence", identifier="High"),
                Entity(namespace="Confidence", identifier="Low"),
            },
            "Species": {
                Entity(namespace="Species", identifier="9606"),
            },
        }

        self.assertEqual({"Confidence", "Species"}, get_annotations(sialic_acid_graph))
        self.assertEqual({"Confidence": 8, "Species": 8}, dict(count_annotations(sialic_acid_graph)))

        annotation_values_by_annotation = get_annotation_values_by_annotation(sialic_acid_graph)
        self.assertEqual(expected, annotation_values_by_annotation)

        annotation_values = get_annotation_values(sialic_acid_graph, "Confidence")
        self.assertEqual(expected["Confidence"], annotation_values)

    def test_get_unused_annotation_url(self):
        graph = BELGraph()
        name = n()
        graph.annotation_url[name] = n()
        self.assertEqual({name}, get_unused_annotations(graph))

    def test_get_unused_annotation_pattern(self):
        graph = BELGraph()
        name = n()
        graph.annotation_pattern[name] = n()
        self.assertEqual({name}, get_unused_annotations(graph))

    def test_get_unused_annotation_list(self):
        graph = BELGraph()
        name = n()
        graph.annotation_pattern[name] = {n(), n(), n()}
        self.assertEqual({name}, get_unused_annotations(graph))

    def test_get_unused_annotation_list_values(self):
        """Test getting unused annotation list values."""
        graph = BELGraph()
        annotation_key = "test"
        graph.annotation_list[annotation_key] = set("abc")
        graph.add_increases(
            protein(n(), n()),
            protein(n(), n()),
            citation=n(),
            evidence=n(),
            annotations={annotation_key: {"a"}},
        )

        rv = get_unused_list_annotation_values(graph)
        self.assertIsInstance(rv, dict)

        self.assertEqual({annotation_key: set("bc")}, rv)