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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
# -*- coding: utf-8 -*-
"""Tests for canonicalization functions."""
import unittest
from typing import Iterable
from pybel import BELGraph
from pybel.canonicalize import _to_bel_lines_body, postpend_location
from pybel.constants import CITATION_TYPE_PUBMED, EXTRACELLULAR, INTRACELLULAR, MODIFIER
from pybel.dsl import (
Abundance,
BiologicalProcess,
ComplexAbundance,
CompositeAbundance,
EnumeratedFusionRange,
Fragment,
Gene,
GeneFusion,
GeneModification,
Hgvs,
MicroRna,
NamedComplexAbundance,
Pathology,
Protein,
ProteinModification,
ProteinSubstitution,
Reaction,
Rna,
RnaFusion,
activity,
degradation,
secretion,
translocation,
)
from pybel.language import Entity
from pybel.testing.utils import n
from pybel.utils import canonicalize_edge
class TestCanonicalize(unittest.TestCase):
def test_postpend_location_failure(self):
with self.assertRaises(ValueError):
postpend_location("", dict(name="failure"))
def test_canonicalize_variant_dsl(self):
"""Use the __str__ functions in the DSL to create BEL instead of external pybel.canonicalize."""
self.assertEqual('var("p.Val600Glu")', str(Hgvs("p.Val600Glu")))
self.assertEqual('var("p.Val600Glu")', str(ProteinSubstitution("Val", 600, "Glu")))
self.assertEqual(
'pmod(go:0006468 ! "protein phosphorylation")',
str(ProteinModification("Ph")),
)
self.assertEqual("pmod(TEST:Ph)", str(ProteinModification("Ph", namespace="TEST")))
self.assertEqual(
"pmod(TEST:Ph, Ser)",
str(ProteinModification("Ph", namespace="TEST", code="Ser")),
)
self.assertEqual(
"pmod(TEST:Ph, Ser, 5)",
str(ProteinModification("Ph", namespace="TEST", code="Ser", position=5)),
)
self.assertEqual(
'pmod(GO:"protein phosphorylation", Thr, 308)',
str(
ProteinModification(
name="protein phosphorylation",
namespace="GO",
code="Thr",
position=308,
)
),
)
self.assertEqual('frag("?")', str(Fragment()))
self.assertEqual('frag("672_713")', str(Fragment(start=672, stop=713)))
self.assertEqual('frag("?", "descr")', str(Fragment(description="descr")))
self.assertEqual(
'frag("672_713", "descr")',
str(Fragment(start=672, stop=713, description="descr")),
)
self.assertEqual('gmod(go:0006306 ! "DNA methylation")', str(GeneModification("Me")))
self.assertEqual("gmod(TEST:Me)", str(GeneModification("Me", namespace="TEST")))
self.assertEqual(
'gmod(GO:"DNA Methylation")',
str(GeneModification("DNA Methylation", namespace="GO")),
)
def test_canonicalize_fusion_range_dsl(self):
"""Test canonicalization of enumerated fusion ranges."""
self.assertEqual("p.1_15", str(EnumeratedFusionRange("p", 1, 15)))
self.assertEqual("p.*_15", str(EnumeratedFusionRange("p", "*", 15)))
def test_Abundance(self):
"""Test canonicalization of abundances."""
short = Abundance(namespace="CHEBI", name="water")
self.assertEqual("a(CHEBI:water)", str(short))
long = Abundance(namespace="CHEBI", name="test name")
self.assertEqual('a(CHEBI:"test name")', str(long))
def test_protein_reference(self):
self.assertEqual("p(HGNC:AKT1)", str(Protein(namespace="HGNC", name="AKT1")))
def test_gene_reference(self):
node = Gene(namespace="EGID", name="780")
self.assertEqual("g(EGID:780)", str(node))
def test_protein_pmod(self):
node = Protein(
name="PLCG1",
namespace="HGNC",
variants=[ProteinModification(name="Ph", code="Tyr")],
)
self.assertEqual(
'p(HGNC:PLCG1, pmod(go:0006468 ! "protein phosphorylation", Tyr))',
str(node),
)
def test_protein_fragment(self):
node = Protein(name="APP", namespace="HGNC", variants=[Fragment(start=672, stop=713)])
self.assertEqual('p(HGNC:APP, frag("672_713"))', str(node))
def test_mirna_reference(self):
self.assertEqual("m(HGNC:MIR1)", str(MicroRna(namespace="HGNC", name="MIR1")))
def test_rna_fusion_specified(self):
node = RnaFusion(
partner_5p=Rna(namespace="HGNC", name="TMPRSS2"),
range_5p=EnumeratedFusionRange("r", 1, 79),
partner_3p=Rna(namespace="HGNC", name="ERG"),
range_3p=EnumeratedFusionRange("r", 312, 5034),
)
self.assertEqual('r(fus(HGNC:TMPRSS2, "r.1_79", HGNC:ERG, "r.312_5034"))', str(node))
def test_rna_fusion_unspecified(self):
node = RnaFusion(
partner_5p=Rna(namespace="HGNC", name="TMPRSS2"),
partner_3p=Rna(namespace="HGNC", name="ERG"),
)
self.assertEqual('r(fus(HGNC:TMPRSS2, "?", HGNC:ERG, "?"))', str(node))
def test_gene_fusion_specified(self):
node = GeneFusion(
partner_5p=Gene(namespace="HGNC", name="TMPRSS2"),
range_5p=EnumeratedFusionRange("c", 1, 79),
partner_3p=Gene(namespace="HGNC", name="ERG"),
range_3p=EnumeratedFusionRange("c", 312, 5034),
)
self.assertEqual('g(fus(HGNC:TMPRSS2, "c.1_79", HGNC:ERG, "c.312_5034"))', str(node))
def test_pathology(self):
node = Pathology(namespace="DO", name="Alzheimer disease")
self.assertEqual('path(DO:"Alzheimer disease")', str(node))
def test_bioprocess(self):
node = BiologicalProcess(namespace="GO", name="apoptosis")
self.assertEqual("bp(GO:apoptosis)", str(node))
def test_named_complex_abundance(self):
node = NamedComplexAbundance(namespace="SCOMP", name="Calcineurin Complex")
self.assertEqual('complex(SCOMP:"Calcineurin Complex")', str(node))
def test_complex_abundance(self):
node = ComplexAbundance(
members=[
Protein(namespace="HGNC", name="FOS"),
Protein(namespace="HGNC", name="JUN"),
]
)
self.assertEqual("complex(p(HGNC:FOS), p(HGNC:JUN))", str(node))
def test_composite_abundance(self):
node = CompositeAbundance(
members=[
Protein(namespace="HGNC", name="FOS"),
Protein(namespace="HGNC", name="JUN"),
]
)
self.assertEqual("composite(p(HGNC:FOS), p(HGNC:JUN))", str(node))
def test_reaction(self):
node = Reaction(
reactants=[Abundance(namespace="CHEBI", name="A")],
products=[Abundance(namespace="CHEBI", name="B")],
)
self.assertEqual("rxn(reactants(a(CHEBI:A)), products(a(CHEBI:B)))", str(node))
class TestCanonicalizeEdge(unittest.TestCase):
"""This class houses all testing for the canonicalization of edges such that the relation/modifications can be used
as a second level hash"""
def setUp(self):
self.g = BELGraph()
self.g.annotation_pattern["Species"] = r"\d+"
self.u = Protein(name="u", namespace="TEST")
self.v = Protein(name="v", namespace="TEST")
self.g.add_node_from_data(self.u)
self.g.add_node_from_data(self.v)
def get_data(self, k):
return self.g[self.u][self.v][k]
def add_edge(self, source_modifier=None, target_modifier=None, annotations=None):
key = self.g.add_increases(
self.u,
self.v,
evidence=n(),
citation=n(),
source_modifier=source_modifier,
target_modifier=target_modifier,
annotations=annotations,
)
return canonicalize_edge(self.get_data(key))
def test_failure(self):
with self.assertRaises(ValueError):
self.add_edge(source_modifier={MODIFIER: "nope"})
def test_canonicalize_edge_info(self):
c1 = self.add_edge(annotations={"Species": "9606"})
c2 = self.add_edge(annotations={"Species": "9606"})
c3 = self.add_edge(
source_modifier=activity("tport"),
)
c4 = self.add_edge(
source_modifier=activity(namespace="go", name="transporter activity", identifier="0005215"),
)
self.assertEqual(c1, c2)
self.assertNotEqual(c1, c3)
self.assertEqual(c3, c4)
def test_subject_degradation_location(self):
self.assertEqual(
self.add_edge(source_modifier=degradation()),
self.add_edge(source_modifier=degradation()),
)
self.assertEqual(
self.add_edge(source_modifier=degradation(location=Entity(name="somewhere", namespace="GO"))),
self.add_edge(source_modifier=degradation(location=Entity(name="somewhere", namespace="GO"))),
)
self.assertNotEqual(
self.add_edge(source_modifier=degradation()),
self.add_edge(source_modifier=degradation(location=Entity(name="somewhere", namespace="GO"))),
)
def test_translocation(self):
self.assertEqual(
self.add_edge(source_modifier=secretion()),
self.add_edge(source_modifier=secretion()),
)
self.assertEqual(
self.add_edge(source_modifier=secretion()),
self.add_edge(source_modifier=translocation(INTRACELLULAR, EXTRACELLULAR)),
)
class TestSerializeBEL(unittest.TestCase):
def setUp(self):
self.citation = n()
self.evidence = n()
self.url = n()
self.graph = BELGraph()
self.graph.namespace_url["HGNC"] = self.url
def _help_check_lines(self, lines: Iterable[str]):
"""Check the given lines match the graph built during the tests."""
self.assertEqual(lines, list(_to_bel_lines_body(self.graph)))
def test_simple(self):
"""Test a scenario with a qualified edge, but no annotations."""
self.graph.add_increases(
Protein(namespace="HGNC", name="YFG1"),
Protein(namespace="HGNC", name="YFG"),
citation=self.citation,
evidence=self.evidence,
)
self.assertEqual(2, self.graph.number_of_nodes())
self.assertEqual(1, self.graph.number_of_edges())
expected_lines = [
f'SET Citation = {{"{CITATION_TYPE_PUBMED}", "{self.citation}"}}\n',
'SET SupportingText = "{}"'.format(self.evidence),
"p(HGNC:YFG1) increases p(HGNC:YFG)",
"UNSET SupportingText",
"UNSET Citation\n",
"#" * 80,
]
self._help_check_lines(expected_lines)
def test_different_key_and_namespace(self):
key, namespace, value = map(lambda _: n(), range(3))
self.graph.annotation_curie.add(key)
self.graph.add_increases(
Protein(namespace="HGNC", name="YFG1"),
Protein(namespace="HGNC", name="YFG"),
citation=self.citation,
evidence=self.evidence,
annotations={
key: Entity(namespace=namespace, identifier=value),
},
)
self.assertEqual(2, self.graph.number_of_nodes())
self.assertEqual(1, self.graph.number_of_edges())
expected_lines = [
f'SET Citation = {{"{CITATION_TYPE_PUBMED}", "{self.citation}"}}\n',
f'SET SupportingText = "{self.evidence}"',
f'SET {key} = "{namespace}:{value}"',
"p(HGNC:YFG1) increases p(HGNC:YFG)",
f"UNSET {key}",
"UNSET SupportingText",
"UNSET Citation\n",
("#" * 80),
]
self._help_check_lines(expected_lines)
def test_single_annotation(self):
"""Test a scenario with a qualified edge, but no annotations."""
a1, v1 = map(lambda _: n(), range(2))
self.graph.annotation_list[a1] = {v1}
self.graph.add_increases(
Protein(namespace="HGNC", name="YFG1"),
Protein(namespace="HGNC", name="YFG"),
citation=self.citation,
evidence=self.evidence,
annotations={
a1: {v1},
},
)
self.assertEqual(2, self.graph.number_of_nodes())
self.assertEqual(1, self.graph.number_of_edges())
# Means that only the identifier needs to be written out
self.assertNotIn(a1, self.graph.annotation_curie)
expected_lines = [
f'SET Citation = {{"{CITATION_TYPE_PUBMED}", "{self.citation}"}}\n',
f'SET SupportingText = "{self.evidence}"',
f'SET {a1} = "{v1}"',
"p(HGNC:YFG1) increases p(HGNC:YFG)",
f"UNSET {a1}",
"UNSET SupportingText",
"UNSET Citation\n",
"#" * 80,
]
self._help_check_lines(expected_lines)
def test_multiple_annotations(self):
a1, v1, v2 = map(lambda _: n(), range(3))
v1, v2 = sorted([v1, v2])
self.graph.annotation_list[a1] = {v1, v2}
self.graph.add_increases(
Protein(namespace="HGNC", name="YFG1"),
Protein(namespace="HGNC", name="YFG"),
citation=self.citation,
evidence=self.evidence,
annotations={
a1: {v1, v2},
},
)
self.assertEqual(2, self.graph.number_of_nodes())
self.assertEqual(1, self.graph.number_of_edges())
expected_lines = [
f'SET Citation = {{"{CITATION_TYPE_PUBMED}", "{self.citation}"}}\n',
f'SET SupportingText = "{self.evidence}"',
f'SET {a1} = {{"{v1}", "{v2}"}}',
"p(HGNC:YFG1) increases p(HGNC:YFG)",
f"UNSET {a1}",
"UNSET SupportingText",
"UNSET Citation\n",
("#" * 80),
]
self._help_check_lines(expected_lines)
|