Skip to content

Commit

Permalink
Merge pull request HadrienG#25 from HadrienG/lineage
Browse files Browse the repository at this point in the history
Lineage
  • Loading branch information
HadrienG authored Sep 21, 2018
2 parents a9721dc + 7056de9 commit db78db5
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 25 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
language: python
dist: xenial
sudo: true
python:
- '3.5'
- '3.6'
- '3.7'
install:
- pip install -r requirements.txt
- pip install nose nose-testconfig
- pip install codecov
- pip install -e .
Expand Down
17 changes: 17 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[packages]
requests = "*"
peewee = "==2.8.1"
tqdm = "*"

[dev-packages]
nose = "*"
nose-testconfig = "*"

[scripts]
tests = "nosetests --tc-file taxadb.ini --with-coverage --cover-package=taxadb"
taxadb = "python -m taxadb"
89 changes: 89 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions requirements.txt

This file was deleted.

6 changes: 3 additions & 3 deletions taxadb/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ def main():
parser_create.add_argument(
'--division',
'-d',
choices=['full', 'nucl', 'prot', 'gb', 'wgs', 'gss', 'est'],
choices=['taxa', 'full', 'nucl', 'prot', 'gb', 'wgs', 'gss', 'est'],
default='full',
metavar='[full|nucl|prot|gb|wgs|gss|est]',
metavar='[taxa|full|nucl|prot|gb|wgs|gss|est]',
help='division to build (default: %(default)s))'
)
parser_create.add_argument(
Expand Down Expand Up @@ -355,4 +355,4 @@ def main():
logger = logging.getLogger(__name__)
logger.debug(e)
parser.print_help()
raise # extra traceback to uncomment for extra debugging powers
# raise # extra traceback to uncomment for extra debugging powers
40 changes: 28 additions & 12 deletions taxadb/taxid.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ def sci_name(self, taxid):
except Taxa.DoesNotExist:
return None

def lineage_id(self, taxid, reverse=False):
def lineage_id(self, taxid, ranks=False, reverse=False):
"""Get lineage for a taxonomic id
Given a taxid, return its associated lineage (in the form of a list of
taxids, each parents of each others)
Args:
taxid (:obj:`int`): a taxid
ranks (:obj:`bool`): Wether to return a dict with the tax ranks or
not. Default False
reverse (:obj:`bool`): Inverted lineage, from top to bottom
taxonomy hierarchy. Default False
Returns:
Expand All @@ -54,30 +56,39 @@ def lineage_id(self, taxid, reverse=False):
"""
try:
lineage_list = []
lineages = {} if ranks else []
# lineage_list = []
current_lineage = Taxa.get(Taxa.ncbi_taxid == taxid).tax_name
current_lineage_id = Taxa.get(Taxa.ncbi_taxid == taxid).ncbi_taxid
parent = Taxa.get(Taxa.ncbi_taxid == taxid).parent_taxid
rank = Taxa.get(Taxa.ncbi_taxid == taxid).lineage_level
while current_lineage != 'root':
lineage_list.append(current_lineage_id)
if ranks:
lineages[rank] = current_lineage_id
else:
lineages.append(current_lineage_id)

new_query = Taxa.get(Taxa.ncbi_taxid == parent)

current_lineage = new_query.tax_name
current_lineage_id = new_query.ncbi_taxid
parent = new_query.parent_taxid
if reverse is True:
lineage_list.reverse()
return lineage_list
rank = new_query.lineage_level
if reverse is True and ranks is False:
lineages.reverse()
return lineages
except Taxa.DoesNotExist:
return None

def lineage_name(self, taxid, reverse=False):
def lineage_name(self, taxid, ranks=False, reverse=False):
"""Get a lineage name for a taxonomic id
Given a taxid, return its associated lineage
Arguments:
taxid (:obj:`int`): a taxid
ranks (:obj:`bool`): Wether to return a dict with the tax ranks or
not. Default False
reverse (:obj:`bool`): Inverted lineage, from top to bottom
taxonomy hierarchy. Default False
Expand All @@ -87,18 +98,23 @@ def lineage_name(self, taxid, reverse=False):
"""
try:
lineage_list = []
lineages = {} if ranks else []
current_lineage = Taxa.get(Taxa.ncbi_taxid == taxid).tax_name
parent = Taxa.get(Taxa.ncbi_taxid == taxid).parent_taxid
rank = Taxa.get(Taxa.ncbi_taxid == taxid).lineage_level
while current_lineage != 'root':
lineage_list.append(current_lineage)
if ranks:
lineages[rank] = current_lineage
else:
lineages.append(current_lineage)
new_query = Taxa.get(Taxa.ncbi_taxid == parent)

current_lineage = new_query.tax_name
parent = new_query.parent_taxid
if reverse is True:
lineage_list.reverse()
return lineage_list
rank = new_query.lineage_level
if reverse is True and ranks is False:
lineages.reverse()
return lineages
except Taxa.DoesNotExist:
return None

Expand Down
14 changes: 8 additions & 6 deletions taxadb/test/test_taxadb.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,15 @@ def test_taxid_sci_name_None(self):
self.assertIsNone(name)

@attr('taxid')
def test_taxid_lineage_id(self):
def test_taxid_lineage_id_ranks(self):
taxid = self._buildTaxaDBObject(TaxID)
lineage = taxid.lineage_id(9986)
self.assertListEqual(lineage, [
9986, 9984, 9979, 9975, 314147, 314146, 1437010, 9347, 32525,
40674, 32524, 32523, 1338369, 8287, 117571, 117570, 7776, 7742,
89593, 7711, 33511, 33213, 6072, 33208, 33154, 2759, 131567])
lineage = taxid.lineage_id(9986, ranks=True)
self.assertDictEqual(lineage,
{'species': 9986, 'genus': 9984, 'family': 9979,
'order': 9975, 'no rank': 131567,
'superorder': 314146, 'class': 40674,
'subphylum': 89593, 'phylum': 7711,
'kingdom': 33208, 'superkingdom': 2759})

@attr('taxid')
def test_taxid_lineage_id_reverse(self):
Expand Down
2 changes: 1 addition & 1 deletion taxadb/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.8.0'
__version__ = '0.9.0'

0 comments on commit db78db5

Please sign in to comment.