Skip to content

Commit

Permalink
MRG: replace lca_utils.LineagePair with tax_utils.LineagePair (#2441
Browse files Browse the repository at this point in the history
)

Note: PR into #2437

Wanted to try out the new code for myself!

Co-authored-by: N. Tessa Pierce-Ward <ntpierce@gmail.com>
Co-authored-by: Tessa Pierce Ward <bluegenes@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 10, 2023
1 parent 474e4b3 commit f2fabef
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/sourmash/lca/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .lca_db import LCA_Database
from .lca_utils import (taxlist, zip_lineage, build_tree, find_lca,
gather_assignments, LineagePair, display_lineage,
gather_assignments, display_lineage,
count_lca_for_assignments)

from .command_index import index
Expand Down
2 changes: 1 addition & 1 deletion src/sourmash/lca/command_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from sourmash.sourmash_args import load_file_as_signatures
from sourmash.logging import notify, error, debug, set_quiet
from . import lca_utils
from .lca_utils import LineagePair
from .lca_db import LCA_Database
from sourmash.sourmash_args import DEFAULT_LOAD_K

Expand All @@ -26,6 +25,7 @@ def load_taxonomy_assignments(filename, *, delimiter=',', start_column=2,
The 'assignments' dictionary that's returned maps identifiers to
lineage tuples.
"""
from sourmash.tax.tax_utils import LineagePair
# parse spreadsheet!
# CTB note: can't easily switch to FileInputCSV, because of
# janky way we do/don't handle headers here. See issue #2198.
Expand Down
5 changes: 3 additions & 2 deletions src/sourmash/lca/lca_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ def load(cls, db_name):
Method specific to this class.
"""
from .lca_utils import taxlist, LineagePair
from .lca_utils import taxlist
from sourmash.tax.tax_utils import LineagePair

if not os.path.isfile(db_name):
raise ValueError(f"'{db_name}' is not a file and cannot be loaded as an LCA database")
Expand Down Expand Up @@ -325,7 +326,7 @@ def load(cls, db_name):
lid_to_lineage = {}
lineage_to_lid = {}
for k, v in lid_to_lineage_2.items():
v = dict(v)
v = dict( ((x[0], x[1]) for x in v) )
vv = []
for rank in taxlist():
name = v.get(rank, '')
Expand Down
12 changes: 1 addition & 11 deletions src/sourmash/lca/lca_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,6 @@ def display_lineage(lineage, include_strain=True, truncate_empty=True):
null_names = set(['[Blank]', 'na', 'null'])


def make_lineage(lineage_str):
"Turn a ; or ,-separated set of lineages into a tuple of LineagePair objs."
lin = lineage_str.split(';')
if len(lin) == 1:
lin = lineage.split(',')
lin = [ LineagePair(rank, n) for (rank, n) in zip(taxlist(), lin) ]
lin = tuple(lin)

return lin


def build_tree(assignments, initial=None):
"""
Builds a tree of dictionaries from lists of LineagePair objects
Expand Down Expand Up @@ -249,6 +238,7 @@ def pop_to_rank(lin, rank):

def make_lineage(lineage):
"Turn a ; or ,-separated set of lineages into a tuple of LineagePair objs."
from sourmash.tax.tax_utils import LineagePair
lin = lineage.split(';')
if len(lin) == 1:
lin = lineage.split(',')
Expand Down
4 changes: 3 additions & 1 deletion src/sourmash/tax/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,9 @@ def summarize(args):
name_seen = set()
for v in tax_assign.values():
sofar = []
for rank, name in v:
for vv in v:
name = vv.name
rank = vv.rank
if name not in name_seen:
rank_counts[rank] += 1
name_seen.add(name)
Expand Down
3 changes: 2 additions & 1 deletion src/sourmash/tax/tax_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def _init_from_lineage_tuples(self):
new_lineage[rank_idx] = LineagePair(rank=lin_tup.rank, name=lin_tup.name)
else:
new_lineage[rank_idx] = lin_tup

# build list of filled ranks
filled_ranks = [a.rank for a in new_lineage if a.name]
# set lineage and filled_ranks
Expand Down Expand Up @@ -293,7 +294,7 @@ def pop_to_rank(self, rank):
return new

def lineage_at_rank(self, rank):
# non-destructive pop_to_rank. Returns tuple of LineagePairs
"non-destructive pop_to_rank. Returns tuple of LineagePairs"
"Returns tuple of LineagePairs at given rank."
# are we already above rank?
if not self.rank_is_filled(rank):
Expand Down
Loading

0 comments on commit f2fabef

Please sign in to comment.