Skip to content

Commit 783b42e

Browse files
committed
Update testing
1 parent c5da62f commit 783b42e

File tree

10 files changed

+84
-110
lines changed

10 files changed

+84
-110
lines changed

src/bio2bel_hgnc/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
"""Exports HGNC Equivalences"""
3+
"""A package for converting HGNC to BEL."""
44

55
from .manager import Manager
66

src/bio2bel_hgnc/constants.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
MODULE_NAME = 'hgnc'
88
#: The default directory where PyBEL files, including logs and the default cache, are stored. Created if not exists.
99
DATA_DIR = get_data_dir(MODULE_NAME)
10-
DEFAULT_CACHE_CONNECTION = get_connection(MODULE_NAME)
1110

1211
GENE_FAMILY_KEYWORD = 'GFAM'
1312

src/bio2bel_hgnc/enrich.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
__all__ = [
1010
'get_node',
11-
'add_metadata',
1211
'add_node_orthologies',
1312
'add_orthologies',
1413
'add_node_central_dogma',
@@ -28,16 +27,6 @@ def get_node(graph, node, connection=None):
2827
return manager.get_node(graph, node)
2928

3029

31-
def add_metadata(graph, node, manager=None):
32-
"""Add to the node based on PyHGNC's database
33-
34-
:param pybel.BELGraph graph: A BEL Graph
35-
:param tuple node: A PyBEL node tuple
36-
:param Optional[str or bio2bel_hgnc.Manager] manager: A database manager
37-
"""
38-
raise NotImplementedError
39-
40-
4130
def add_node_orthologies(graph, node, manager=None, add_leaves=False):
4231
"""Given a node that's HGNC, add orthology relationships
4332

src/bio2bel_hgnc/manager.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
"""Bio2BEL HGNC Manager."""
44

5+
import logging
56
from collections import Counter
67

78
import click
8-
import logging
9-
from tqdm import tqdm
10-
11-
from bio2bel.namespace_manager import NamespaceManagerMixin
129
from pybel import BELGraph
1310
from pybel.constants import FUNCTION, GENE, IDENTIFIER, NAME, NAMESPACE, NAMESPACE_DOMAIN_GENE, PROTEIN, RNA
1411
from pybel.dsl import gene as gene_dsl, protein as protein_dsl, rna as rna_dsl
1512
from pybel.manager.models import NamespaceEntry
13+
from pybel.resources import write_namespace
14+
from tqdm import tqdm
15+
16+
from bio2bel.namespace_manager import NamespaceManagerMixin
1617
from .constants import GENE_FAMILY_KEYWORD, MODULE_NAME, encodings
1718
from .gfam_manager import Manager as GfamManager
18-
from .model_utils import *
19+
from .model_utils import (
20+
family_to_bel, gene_to_bel, gene_to_mirna_to_bel, gene_to_protein_to_bel, gene_to_rna_to_bel,
21+
uniprot_to_pybel,
22+
)
1923
from .models import Base, GeneFamily, HumanGene, MouseGene, RatGene, UniProt
2024
from .wrapper import BaseManager
2125

@@ -51,7 +55,6 @@ def _write_gene_bel_namespace_helper(values, file):
5155
:param dict[str,str] values:
5256
:param file:
5357
"""
54-
from pybel.resources import write_namespace
5558
write_namespace(
5659
namespace_name='HGNC Human Gene Names',
5760
namespace_keyword='HGNC',
@@ -69,7 +72,6 @@ def _write_gene_families_bel_namespace_helper(values, file):
6972
:param list[str] values:
7073
:param file:
7174
"""
72-
from pybel.resources import write_namespace
7375
write_namespace(
7476
namespace_name='HGNC Gene Families',
7577
namespace_keyword='GFAM',
@@ -196,15 +198,6 @@ def get_gene_by_mgi_id(self, mgi_id):
196198

197199
return human_genes[0]
198200

199-
def get_gene_by_mgi_symbol(self, mgi_symbol):
200-
"""Gets a HGNC gene by an orthologous MGI gene symbol
201-
202-
:param str mgi_symbol: MGI gene symbol
203-
:rtype: Optional[bio2bel_hgnc.models.HumanGene]
204-
"""
205-
# TODO this data is not in HGNC...
206-
raise NotImplementedError
207-
208201
def get_gene_by_rgd_id(self, rgd_id):
209202
"""Gets a HGNC gene by an orthologous RGD identifier
210203
@@ -225,15 +218,6 @@ def get_gene_by_rgd_id(self, rgd_id):
225218

226219
return human_genes[0]
227220

228-
def get_gene_by_rgd_symbol(self, rgd_symbol):
229-
"""Gets a HGNC gene by an orthologous RGD identifier
230-
231-
:param str rgd_symbol: RGD gene symbol
232-
:rtype: Optional[bio2bel_hgnc.models.HumanGene]
233-
"""
234-
# TODO this data is not in HGNC...
235-
raise NotImplementedError
236-
237221
def get_node(self, graph, node):
238222
"""Gets a node from the database, whether it has a HGNC, RGD, MGI, or EG identifier.
239223
@@ -591,20 +575,33 @@ def get_all_hgnc_symbols_family(self):
591575
return set(res)
592576

593577
def add_central_dogma(self, graph, node):
594-
"""
578+
"""Add the central dogma of biology.
595579
596580
:param graph:
597581
:param node:
598-
:return:
599582
"""
600583
if node not in graph:
601584
raise ValueError
602585

603586
human_gene = self.get_node(graph, node)
604587
encoding = encodings.get(human_gene.locus_type, 'GRP')
605588

606-
if 'R' in encoding:
607-
graph.add_unqualified_edge(node, )
589+
if 'M' in encoding:
590+
rna = gene_to_rna_to_bel(human_gene)
591+
graph.add_transcription(gene_to_bel(human_gene), rna)
592+
elif 'R' in encoding:
593+
rna = gene_to_mirna_to_bel(human_gene)
594+
graph.add_transcription(gene_to_bel(human_gene), rna)
595+
else:
596+
return
597+
598+
if 'P' not in encoding:
599+
return rna
600+
else:
601+
protein = gene_to_protein_to_bel(human_gene)
602+
graph.add_translation(rna, protein)
603+
return protein
604+
608605

609606
def add_node_equivalencies(self, graph, node, add_leaves=True):
610607
"""Given an HGNC node, add equivalencies found in the database.

src/bio2bel_hgnc/model_utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
"""Bio2BEL HGNC Model utilities."""
44

5-
from pybel.dsl import gene as gene_dsl, protein as protein_dsl, rna as rna_dsl
5+
from pybel.dsl import gene as gene_dsl, mirna as mirna_dsl, protein as protein_dsl, rna as rna_dsl
66

77
__all__ = [
88
'gene_to_bel',
99
'gene_to_rna_to_bel',
10+
'gene_to_mirna_to_bel',
1011
'gene_to_protein_to_bel',
1112
'family_to_bel',
1213
'uniprot_to_pybel',
@@ -39,6 +40,19 @@ def gene_to_rna_to_bel(gene):
3940
)
4041

4142

43+
def gene_to_mirna_to_bel(gene):
44+
"""Converts a Gene to a PyBEL miRNA
45+
46+
:param bio2bel_hgnc.models.HumanGene gene: A Gene model
47+
:rtype: pybel.dsl.mirna
48+
"""
49+
return mirna_dsl(
50+
namespace='HGNC',
51+
name=str(gene.symbol),
52+
identifier=str(gene.identifier)
53+
)
54+
55+
4256
def gene_to_protein_to_bel(gene):
4357
"""Converts a Gene to a PyBEL gene
4458

src/bio2bel_hgnc/wrapper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# -*- coding: utf-8 -*-
22

3+
"""Wrap PyHGNC functionality."""
4+
35
from pyhgnc.manager.database import DbManager
46
from pyhgnc.manager.query import QueryManager
57

68

79
class BaseManager(DbManager, QueryManager):
8-
"""Wraps the relevant PyHGNC functions"""
10+
"""Wraps the PyHGNC managers."""

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-

tests/cases.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""Test cases for Bio2BEL HGNC."""
4+
5+
from bio2bel.testing import AbstractTemporaryCacheClassMixin
6+
from bio2bel_hgnc import Manager
7+
from tests.constants import hcop_test_path, hgnc_test_path
8+
9+
10+
class TemporaryCacheMixin(AbstractTemporaryCacheClassMixin):
11+
Manager = Manager
12+
13+
@classmethod
14+
def populate(cls):
15+
cls.manager.populate(
16+
hgnc_file_path=hgnc_test_path,
17+
hcop_file_path=hcop_test_path,
18+
)

tests/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
"""Test constants for Bio2BEL HGNC."""
4+
35
import logging
46
import os
57

tests/test_enrich_metadata.py

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
# -*- coding: utf-8 -*-
22

33
import logging
4-
import os
5-
import tempfile
64
import unittest
75

8-
import pyhgnc
9-
from bio2bel_hgnc import Manager
10-
from bio2bel_hgnc.constants import GENE_FAMILY_KEYWORD
11-
from bio2bel_hgnc.enrich import (
12-
add_metadata, add_node_central_dogma, add_node_equivalencies, add_node_orthologies, get_node,
13-
)
146
from pybel import BELGraph
157
from pybel.constants import EQUIVALENT_TO, ORTHOLOGOUS, RELATION, TRANSCRIBED_TO, TRANSLATED_TO, unqualified_edge_code
168
from pybel.dsl import gene, mirna, protein, rna
179
from pybel.tokens import node_to_tuple
18-
from tests.constants import hcop_test_path, hgnc_test_path
10+
11+
from bio2bel_hgnc.constants import GENE_FAMILY_KEYWORD
12+
from bio2bel_hgnc.enrich import (
13+
add_node_equivalencies, add_node_orthologies, get_node,
14+
)
15+
from tests.cases import TemporaryCacheMixin
1916

2017
log = logging.getLogger(__name__)
2118

@@ -24,37 +21,6 @@
2421
equivalence_code = unqualified_edge_code[EQUIVALENT_TO]
2522

2623

27-
class TemporaryCacheMixin(unittest.TestCase):
28-
"""
29-
:type file_handle: int
30-
:type path: str
31-
:type manager: pyhgnc.manager.query.QueryManager
32-
"""
33-
file_handle, path, manager = None, None, None
34-
35-
@classmethod
36-
def setUpClass(cls):
37-
"""Create and populate temporary PyHGNC cache"""
38-
cls.file_handle, cls.path = tempfile.mkstemp()
39-
cls.connection = 'sqlite:///' + cls.path
40-
log.info('Test generated connection string %s', cls.connection)
41-
42-
pyhgnc.update(
43-
connection=cls.connection,
44-
hgnc_file_path=hgnc_test_path,
45-
hcop_file_path=hcop_test_path,
46-
)
47-
48-
cls.manager = Manager(connection=cls.connection)
49-
50-
@classmethod
51-
def tearDownClass(cls):
52-
"""Deletes the temporary PyHGNC cache"""
53-
cls.manager.session.close()
54-
os.close(cls.file_handle)
55-
os.remove(cls.path)
56-
57-
5824
class TestEnrich(TemporaryCacheMixin):
5925
def help_check_cd33_model(self, model):
6026
"""Checks if the given model is CD33
@@ -138,22 +104,6 @@ def test_get_entrez_node(self):
138104
cd33_model = get_node(graph, cd33_tuple, connection=self.manager)
139105
self.help_check_cd33_model(cd33_model)
140106

141-
def test_add_metadata(self):
142-
graph = BELGraph()
143-
144-
cd33_test = protein(namespace='HGNC', name='CD33')
145-
graph.add_node_from_data(cd33_test)
146-
147-
self.assertIn(cd33_test.as_tuple(), graph, msg='Graph is missing CD33 protein node')
148-
self.assertIsNone(graph.get_node_label(cd33_test.as_tuple()), msg='CD33 should not have label information')
149-
self.assertIsNone(graph.get_node_identifier(cd33_test.as_tuple()), msg='CD33 should not have identifier information')
150-
151-
add_metadata(graph, cd33_test.as_tuple(), manager=self.manager)
152-
153-
self.assertIn(cd33_test.as_tuple(), graph, msg='Graph somehow lost CD33 protein node')
154-
155-
self.assertEqual('1659', graph.get_node_identifier(cd33_test.as_tuple()), msg='Graph should be enriched with identifier')
156-
157107
def test_add_equivalency(self):
158108
graph = BELGraph()
159109
cd33_hgnc_tuple = graph.add_node_from_data(protein(name='CD33', namespace='HGNC'))
@@ -162,7 +112,7 @@ def test_add_equivalency(self):
162112
self.assertEqual(2, graph.number_of_nodes())
163113
self.assertEqual(0, graph.number_of_edges())
164114

165-
add_node_equivalencies(graph, cd33_hgnc_tuple, connection=self.manager, add_leaves=False)
115+
self.manager.add_node_equivalencies(graph, cd33_hgnc_tuple, add_leaves=False)
166116

167117
self.assertEqual(2, graph.number_of_nodes())
168118
self.assertEqual(2, graph.number_of_edges())
@@ -250,8 +200,9 @@ def test_add_mirna(self):
250200
self.assertEqual(1, graph.number_of_nodes())
251201
self.assertEqual(0, graph.number_of_edges())
252202

253-
add_node_central_dogma(graph, mir489_gene_tuple, connection=self.manager)
254-
203+
result = self.manager.add_central_dogma(graph, mir489_gene_tuple)
204+
self.assertIsNotNone(result)
205+
self.assertIsInstance(result, mirna)
255206
self.assertEqual(2, graph.number_of_nodes())
256207
self.assertEqual(1, graph.number_of_edges())
257208

@@ -265,12 +216,14 @@ def test_add_mirna(self):
265216
def test_add_rna(self):
266217
graph = BELGraph()
267218
mir503hg_gene = gene(namespace='HGNC', name='MIR503HG', identifier='28258')
268-
graph.add_node_from_data(mir503hg_gene)
219+
mir503hg_gene_tuple = graph.add_node_from_data(mir503hg_gene)
269220

270221
self.assertEqual(1, graph.number_of_nodes())
271222
self.assertEqual(0, graph.number_of_edges())
272223

273-
add_node_central_dogma(graph, mir503hg_gene.as_tuple(), connection=self.manager)
224+
result = self.manager.add_central_dogma(graph, mir503hg_gene_tuple)
225+
self.assertIsNotNone(result)
226+
self.assertIsInstance(result, mirna)
274227

275228
self.assertEqual(2, graph.number_of_nodes())
276229
self.assertEqual(1, graph.number_of_edges())
@@ -290,7 +243,9 @@ def test_add_protein(self):
290243
self.assertEqual(1, graph.number_of_nodes())
291244
self.assertEqual(0, graph.number_of_edges())
292245

293-
add_node_central_dogma(graph, cd33_gene_tuple, connection=self.manager)
246+
result = self.manager.add_central_dogma(graph, cd33_gene_tuple)
247+
self.assertIsNotNone(result)
248+
self.assertIsInstance(result, protein)
294249

295250
self.assertEqual(3, graph.number_of_nodes())
296251
self.assertEqual(2, graph.number_of_edges())
@@ -329,6 +284,7 @@ def test_enrich_genes_with_families(self):
329284
self.assertIn(g.as_tuple(), graph.edge[cd33_gene_tuple])
330285

331286
def test_enrich_family_with_genes(self):
287+
"""Test enrichment of genes members on gene families."""
332288
graph = BELGraph()
333289
f = gene(name="CD molecules", namespace=GENE_FAMILY_KEYWORD)
334290
graph.add_node_from_data(f)
@@ -345,7 +301,3 @@ def test_enrich_family_with_genes(self):
345301
g = gene(namespace='HGNC', name=x)
346302
self.assertTrue(graph.has_node_with_data(g))
347303
self.assertIn(f.as_tuple(), graph.edge[g.as_tuple()])
348-
349-
def test_enrich_families_with_genes(self):
350-
"""For gene families, adds their member genes"""
351-
raise NotImplementedError

0 commit comments

Comments
 (0)