Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to new format metadata gtdb #22

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion multitax/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.3.1"
__version__ = "1.3.2"

__all__ = (
'CustomTx',
Expand Down
86 changes: 40 additions & 46 deletions multitax/gtdbtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,56 +31,50 @@ def _build_translation(self, target_tax, files: list = None, urls: list = None):
if files:
fhs = open_files(files)
else:
_urls = ["https://data.gtdb.ecogenomic.org/releases/latest/ar53_metadata.tar.gz",
"https://data.gtdb.ecogenomic.org/releases/latest/bac120_metadata.tar.gz"]
_urls = ["https://data.gtdb.ecogenomic.org/releases/latest/ar53_metadata.tsv.gz",
"https://data.gtdb.ecogenomic.org/releases/latest/bac120_metadata.tsv.gz"]
fhs = download_files(
urls=urls if urls else _urls, retry_attempts=3)

accession_col = 0
gtdb_taxonomy_col = 19
ncbi_taxid_col = 80

for source, fh in fhs.items():
for file in fh.getmembers():
with fh.extractfile(file) as ext:
for line in ext:
try:
fields = line.rstrip().split('\t')
except:
fields = line.decode().rstrip().split('\t')

# skip header
if fields[0] == "accession":
continue

# 0 accession
# 16 gtdb_taxonomy
# 77 ncbi_taxid
# 78 ncbi_taxonomy
# 79 ncbi_taxonomy_unfiltered

# Create lineage from leaf node based on target tax (instead of using field 78)
# to accomodate changes in newest versions of the NCBI tax
ncbi_leaf_node = target_tax.latest(fields[77])
if ncbi_leaf_node != target_tax.undefined_node:
ncbi_nodes = target_tax.lineage(ncbi_leaf_node, ranks=[
"superkingdom", "phylum", "class",
"order", "family", "genus", "species"])
else:
continue

# Build GTDB lineage from leaf (species on given lineage)
# to accomodate possible changes in the loaded tax
gtdb_leaf_node = fields[16].split(";")[-1]
if gtdb_leaf_node != self.undefined_node:
gtdb_nodes = self.lineage(gtdb_leaf_node, ranks=[
"domain", "phylum", "class", "order",
"family", "genus", "species"])
else:
continue

# Match ranks
for i, gtdb_n in enumerate(gtdb_nodes):
if ncbi_nodes[i] != target_tax.undefined_node and gtdb_n != self.undefined_node:
if gtdb_n not in translated_nodes:
translated_nodes[gtdb_n] = set()
translated_nodes[gtdb_n].add(ncbi_nodes[i])
for line in fh:
try:
fields = line.rstrip().split('\t')
except:
fields = line.decode().rstrip().split('\t')

# skip header
if fields[accession_col] == "accession":
continue

ncbi_leaf_node = target_tax.latest(fields[ncbi_taxid_col])
if ncbi_leaf_node != target_tax.undefined_node:
ncbi_nodes = target_tax.lineage(ncbi_leaf_node, ranks=[
"superkingdom", "phylum", "class",
"order", "family", "genus", "species"])
else:
continue

# Build GTDB lineage from leaf (species on given lineage)
# to accomodate possible changes in the loaded tax
gtdb_leaf_node = fields[gtdb_taxonomy_col].split(";")[-1]
if gtdb_leaf_node != self.undefined_node:
gtdb_nodes = self.lineage(gtdb_leaf_node, ranks=[
"domain", "phylum", "class", "order",
"family", "genus", "species"])
else:
continue

# Match ranks
for i, gtdb_n in enumerate(gtdb_nodes):
if ncbi_nodes[i] != target_tax.undefined_node and gtdb_n != self.undefined_node:
if gtdb_n not in translated_nodes:
translated_nodes[gtdb_n] = set()
translated_nodes[gtdb_n].add(ncbi_nodes[i])

else:
warnings.warn("Translation between taxonomies [" + self.__class__.__name__ +
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="multitax",
version="1.3.1",
version="1.3.2",
url="https://www.github.com/pirovc/multitax",
license="MIT",
author="Vitor C. Piro",
Expand Down