Skip to content

Commit

Permalink
Update to bio2bel v0.2.0 (#16)
Browse files Browse the repository at this point in the history
* Update setup

* Fix flake8

* Update src/bio2bel_mirtarbase/manager.py

* Update constants.py

* Update constants.py

* Update iteration and tests

* Add tests for counting the human genes with Bio2BEL HGNC

* Create .readthedocs.yml

* Fix tests

* Bump version: 0.1.3-dev → 0.2.0
  • Loading branch information
cthoyt authored Dec 3, 2018
1 parent 10a41c7 commit ff2a712
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.1.3-dev
current_version = 0.2.0
commit = True
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(?:-(?P<release>[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+(?P<build>[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?
Expand Down
8 changes: 8 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# See: https://docs.readthedocs.io/en/latest/yaml-config.html
build:
image: latest
python:
version: 3.6
pip_install: true
extra_requirements:
- docs
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
- env: TOXENV=flake8
- env: TOXENV=pyroma
- env: TOXENV=xenon
- env: TOXENV=vulture
# docs stage
- stage: docs
env: TOXENV=doc8
Expand All @@ -31,7 +30,6 @@ jobs:
matrix:
allow_failures:
- env: TOXENV=xenon
- env: TOXENV=vulture

install:
- sh -c 'if [ "$TOXENV" = "py" ]; then pip install tox codecov; else pip install tox; fi'
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
# built documents.
#
# The full version, including alpha/beta/rc tags.
release = '0.1.3-dev'
release = '0.2.0'

# The short X.Y version.
parsed_version = re.match(
Expand Down
19 changes: 16 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
'Intended Audience :: Science/Research',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Topic :: Scientific/Engineering :: Bio-Informatics',
'License :: OSI Approved :: MIT License',
]
INSTALL_REQUIRES = [
'pybel>=0.12.1',
'bio2bel>=0.1.5',
'pybel>=0.12.0,<0.13.0',
'bio2bel>=0.2.0,<0.3.0',
'bio2bel_hgnc>=0.1.0',
'bio2bel_entrez>=0.1.0',
'bio2bel_mirbase',
Expand All @@ -35,7 +37,18 @@
'tqdm',
]
EXTRAS_REQUIRE = {
'web': ['flask', 'flask_admin'],
'web': [
'flask',
'flask_admin',
],
'docs': [
'flask',
'flask_admin',
'sphinx',
'sphinx-rtd-theme',
'sphinx-click',
'sphinx-autodoc-typehints',
],
}
ENTRY_POINTS = {
'bio2bel': [
Expand Down
2 changes: 1 addition & 1 deletion src/bio2bel_mirtarbase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .manager import Manager # noqa: F401

__version__ = '0.1.3-dev'
__version__ = '0.2.0'

__title__ = 'bio2bel_mirtarbase'
__description__ = "A package for converting miRTarBase to BEL"
Expand Down
2 changes: 1 addition & 1 deletion src/bio2bel_mirtarbase/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from bio2bel.utils import get_data_dir

VERSION = '0.1.3-dev'
VERSION = '0.2.0'

MODULE_NAME = 'mirtarbase'
DATA_DIR = get_data_dir(MODULE_NAME)
Expand Down
22 changes: 14 additions & 8 deletions src/bio2bel_mirtarbase/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
"""Manager for Bio2BEL miRTarBase."""

import logging
import time
from typing import List, Mapping, Optional

import bio2bel_entrez
import bio2bel_mirbase
import time
from bio2bel_entrez.manager import VALID_ENTREZ_NAMESPACES
from tqdm import tqdm

import bio2bel_entrez
import bio2bel_hgnc
import bio2bel_mirbase
from bio2bel import AbstractManager
from bio2bel.manager.bel_manager import BELManagerMixin
from bio2bel.manager.flask_manager import FlaskMixin
Expand All @@ -22,8 +21,14 @@
from .models import Base, Evidence, Interaction, Mirna, Species, Target
from .parser import get_data

__all__ = [
'Manager',
]

log = logging.getLogger(__name__)

VALID_ENTREZ_NAMESPACES = {'egid', 'eg', 'entrez', 'ncbigene'}


def _build_entrez_map(hgnc_manager: bio2bel_hgnc.Manager) -> Mapping[str, HumanGene]:
"""Build a mapping from entrez gene identifiers to their database models from :py:mod:`bio2bel_hgnc.models`."""
Expand Down Expand Up @@ -266,14 +271,14 @@ def enrich_rnas(self, graph: BELGraph):
log.debug('enriching inhibitors of RNA')
count = 0

for node in graph:
for node in list(graph):
if node[FUNCTION] != RNA:
continue

if NAMESPACE not in node:
namespace = node.get(NAMESPACE)
if namespace is None:
continue

namespace = node[NAMESPACE]
identifier = node.get(IDENTIFIER)
name = node.get(NAME)

Expand All @@ -292,7 +297,7 @@ def enrich_rnas(self, graph: BELGraph):
for interaction in target.interactions:
for evidence in interaction.evidences:
count += 1
evidence.add_to_graph(graph)
evidence._add_to_graph(graph, evidence.interaction.mirna.as_bel(), node)

log.debug('added %d MTIs', count)

Expand Down Expand Up @@ -334,6 +339,7 @@ def enrich_mirnas(self, graph: BELGraph):
log.debug('added %d MTIs', count)

def get_mirna_interaction_evidences(self):
"""Get interaction evidences."""
return self.session \
.query(Mirna, Interaction, Evidence) \
.join(Interaction) \
Expand Down
43 changes: 24 additions & 19 deletions src/bio2bel_mirtarbase/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship

import pybel.dsl
from pybel import BELGraph
from pybel.dsl import mirna, rna
from .constants import MODULE_NAME
Expand All @@ -16,11 +17,11 @@
MIRBASE = 'mirbase'
HGNC = 'hgnc'

MIRNA_TABLE_NAME = '{}_mirna'.format(MODULE_NAME)
TARGET_TABLE_NAME = '{}_target'.format(MODULE_NAME)
SPECIES_TABLE_NAME = '{}_species'.format(MODULE_NAME)
EVIDENCE_TABLE_NAME = '{}_evidence'.format(MODULE_NAME)
INTERACTION_TABLE_NAME = '{}_interaction'.format(MODULE_NAME)
MIRNA_TABLE_NAME = f'{MODULE_NAME}_mirna'
TARGET_TABLE_NAME = f'{MODULE_NAME}_target'
SPECIES_TABLE_NAME = f'{MODULE_NAME}_species'
EVIDENCE_TABLE_NAME = f'{MODULE_NAME}_evidence'
INTERACTION_TABLE_NAME = f'{MODULE_NAME}_interaction'

# create base class
Base = declarative_base()
Expand All @@ -42,7 +43,7 @@ def to_json(self, include_id: bool = True) -> Mapping:
:param include_id: Include the database identifier?
"""
rv = {
'name': str(self.name)
'name': str(self.name),
}

if include_id:
Expand Down Expand Up @@ -98,7 +99,7 @@ class Target(Base):
hgnc_symbol = Column(String(32), nullable=True, unique=True, index=True, doc="HGNC gene symbol")
hgnc_id = Column(String(32), nullable=True, unique=True, index=True, doc="HGNC gene identifier")

species_id = Column(Integer, ForeignKey('{}.id'.format(SPECIES_TABLE_NAME)), nullable=False, doc='The host species')
species_id = Column(Integer, ForeignKey(f'{SPECIES_TABLE_NAME}.id'), nullable=False, doc='The host species')
species = relationship('Species')

def __str__(self): # noqa: D105
Expand All @@ -115,7 +116,7 @@ def serialize_to_entrez_node(self) -> rna:
def serialize_to_hgnc_node(self) -> rna:
"""Serialize to PyBEL node data dictionary."""
if self.hgnc_id is None:
raise ValueError('missing HGNC information for Entrez Gene {}'.format(self.entrez_id))
raise ValueError(f'missing HGNC information for Entrez Gene {self.entrez_id}')

return rna(
namespace=HGNC,
Expand All @@ -129,7 +130,7 @@ def to_json(self, include_id=True) -> Mapping:
'species': self.species.to_json(),
'identifiers': [
self.serialize_to_entrez_node(),
self.serialize_to_hgnc_node()
self.serialize_to_hgnc_node(),
]
}

Expand All @@ -149,11 +150,11 @@ class Interaction(Base):
mirtarbase_id = Column(String(64), nullable=False, unique=True, index=True,
doc="miRTarBase interaction identifier which is unique for a pair of miRNA and RNA targets")

mirna_id = Column(Integer, ForeignKey("{}.id".format(MIRNA_TABLE_NAME)), nullable=False, index=True,
mirna_id = Column(Integer, ForeignKey(f'{MIRNA_TABLE_NAME}.id'), nullable=False, index=True,
doc='The miRTarBase identifier of the interacting miRNA')
mirna = relationship(Mirna, backref="interactions")

target_id = Column(Integer, ForeignKey("{}.id".format(TARGET_TABLE_NAME)), nullable=False, index=True,
target_id = Column(Integer, ForeignKey(f'{TARGET_TABLE_NAME}.id'), nullable=False, index=True,
doc='The Entrez gene identifier of the interacting RNA')
target = relationship(Target, backref="interactions")

Expand All @@ -163,7 +164,7 @@ class Interaction(Base):
)

def __str__(self): # noqa: D105
return '{} =| {}'.format(self.mirna.name, self.target.name)
return f'{self.mirna.name} =| {self.target.name}'


class Evidence(Base):
Expand All @@ -179,7 +180,7 @@ class Evidence(Base):
doc="Type and strength of the miRNA - target interaction. E.g. 'Functional MTI (Weak)'")
reference = Column(String(255), nullable=False, doc="Reference PubMed Identifier")

interaction_id = Column(Integer, ForeignKey("{}.id".format(INTERACTION_TABLE_NAME)),
interaction_id = Column(Integer, ForeignKey(f'{INTERACTION_TABLE_NAME}.id'),
doc='The interaction for which this evidence was captured')
interaction = relationship(Interaction, backref="evidences")

Expand All @@ -188,14 +189,18 @@ def __str__(self): # noqa: D105

def add_to_graph(self, graph: BELGraph) -> str:
"""Add this edge to the BEL graph and return the ket for that edge."""
try:
target_node = self.interaction.target.serialize_to_hgnc_node()
except ValueError:
target_node = self.interaction.target.serialize_to_entrez_node()
return self._add_to_graph(
graph,
self.interaction.mirna.as_bel(),
self.interaction.target.serialize_to_entrez_node(),

)

def _add_to_graph(self, graph: BELGraph, source: pybel.dsl.MicroRna, target: pybel.dsl.Rna) -> str:
"""Add this edge to the BEL graph and return the ket for that edge."""
return graph.add_directly_decreases(
self.interaction.mirna.as_bel(),
target_node,
source,
target,
evidence=str(self.support),
citation=str(self.reference),
annotations={
Expand Down
28 changes: 11 additions & 17 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import json
import os

import bio2bel_mirbase

import bio2bel_hgnc
import bio2bel_mirbase
import bio2bel_mirtarbase
from bio2bel.testing import AbstractTemporaryCacheClassMixin

Expand All @@ -25,21 +24,6 @@ class TemporaryFilledCacheMixin(AbstractTemporaryCacheClassMixin):
hgnc_manager: bio2bel_hgnc.Manager
mirbase_manager: bio2bel_mirbase.Manager

@classmethod
def setUpClass(cls):
"""Create temporary file and populate database."""
super().setUpClass()

cls.hgnc_manager = bio2bel_hgnc.Manager(connection=cls.connection)
cls.hgnc_manager._create_tables()
json_data = cls.hgnc_manager.load_hgnc_json(hgnc_file_path=TEST_HGNC_JSON)
cls.hgnc_manager.insert_hgnc(hgnc_dict=json_data, silent=True, low_memory=False)

cls.mirbase_manager = bio2bel_mirbase.Manager(connection=cls.connection)
with open(TEST_MIRBASE_JSON) as file:
mirbase_list = json.load(file)
cls.mirbase_manager._populate_list(mirbase_list)

@classmethod
def populate(cls):
"""Fill the HGNC and mirTarBase databases.
Expand All @@ -58,4 +42,14 @@ def populate(cls):
MIRT000006 hsa-miR-146a-5p Homo sapiens CXCR4 7852 Homo sapiens Microarray Functional MTI (Weak) 20375304
MIRT000012 hsa-miR-122-5p Homo sapiens CYP7A1 1581 Homo sapiens qRT-PCR//Luciferase reporter assay Functional MTI 20351063
"""
cls.hgnc_manager = bio2bel_hgnc.Manager(connection=cls.connection)
cls.hgnc_manager._create_tables()
json_data = cls.hgnc_manager.load_hgnc_json(hgnc_file_path=TEST_HGNC_JSON)
cls.hgnc_manager.insert_hgnc(hgnc_dict=json_data, silent=True, low_memory=False)

cls.mirbase_manager = bio2bel_mirbase.Manager(connection=cls.connection)
with open(TEST_MIRBASE_JSON) as file:
mirbase_list = json.load(file)
cls.mirbase_manager._populate_definitions_helper(mirbase_list)

cls.manager.populate(TEST_MIRTARBASE_EXCEL)
Loading

0 comments on commit ff2a712

Please sign in to comment.