Skip to content

Commit

Permalink
Make newick import optional for conda package.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromekelleher committed Mar 3, 2021
1 parent 848c9cc commit 1ee6d0a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ jobs:
python setup.py check
python -m twine check dist/*.tar.gz --strict
python -m build
- run:
name: Install from the distribution tarball
command: |
python -m venv venv
source venv/bin/activate
pip install --upgrade setuptools pip
pip install dist/*.tar.gz
python -c 'import msprime; print(msprime.__version__)'
# We should still be able to import without the newick module
python -m pip uninstall -y newick
python -c 'import msprime; print(msprime.__version__)'
25 changes: 22 additions & 3 deletions msprime/species_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,36 @@
import collections
import re

import newick
try:
_newick_imported = False
import newick

_newick_imported = True
except ImportError: # pragma: no cover
pass

from . import demography as demog


def check_newick_import():
if not _newick_imported:
raise ImportError(
"The 'newick' module is required for species tree parsing. "
"If you installed msprime using conda, please install the "
"newick module using `conda install -c bioconda newick` or "
"'pip install newick'. If you installed msprime using pip "
"newick should have been automatically installed; please "
"open an issue on GitHub with details of your installation."
)


def parse_starbeast(tree, generation_time, time_units="myr"):
"""
Parse a nexusencoded species tree into a Demography object. See the
Parse a nexus encoded species tree into a Demography object. See the
documentation of :class:`.Demography.from_starbeast` (the public interface)
for details.
"""

check_newick_import()
# Make sure that branch length units are either "myr" or "yr".
allowed_branch_lenth_units = ["myr", "yr"]
if time_units not in allowed_branch_lenth_units:
Expand Down Expand Up @@ -99,6 +117,7 @@ def parse_species_tree(
documentation of :class:`.Demography.from_species_tree` (the public interface)
for details.
"""
check_newick_import()
# Make sure that branch length units are either "myr", "yr", or "gen".
allowed_branch_lenth_units = ["myr", "yr", "gen"]
if time_units not in allowed_branch_lenth_units:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_species_tree_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,12 @@ def test_12_species(self):
assert len(spec.events) == 11
for mm in spec.events:
assert isinstance(mm, msprime.PopulationSplit)


def test_newick_import():
try:
species_trees._newick_imported = False
with pytest.raises(ImportError, match="The 'newick' module"):
species_trees.check_newick_import()
finally:
species_trees._newick_imported = True

0 comments on commit 1ee6d0a

Please sign in to comment.