Skip to content

Commit

Permalink
Merge pull request #29 from sjanssen2/py35
Browse files Browse the repository at this point in the history
Py35
  • Loading branch information
wasade authored Apr 12, 2021
2 parents d1e9f9f + 7f54076 commit 9b3814f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Modified from https://github.com/biocore/biom-format/
language: python
env:
- PYTHON_VERSION=2.7
- PYTHON_VERSION=3.6
before_install:
- wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh -O miniconda.sh
- export MINICONDA_PREFIX="$HOME/miniconda"
Expand All @@ -14,7 +14,7 @@ before_install:
install:
- conda create --yes -n env_name python=$PYTHON_VERSION pip numpy scipy matplotlib nose pep8 Sphinx
- source activate env_name
- pip install future coveralls natsort pyflakes flake8 python-dateutil scikit-bio==0.4.2 click
- pip install future coveralls natsort pyflakes flake8 python-dateutil scikit-bio click
- pip install -e . --no-deps
script:
- if [ ${WITH_DOCTEST} ]; then PYTHONWARNINGS=ignore nosetests --with-doctest --with-coverage -I DONOTIGNOREANYTHING; else PYTHONWARNINGS=ignore nosetests --with-coverage -I DONOTIGNOREANYTHING; fi
Expand Down
13 changes: 9 additions & 4 deletions t2t/consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
make_consensus_tree
etc...
"""
from nlevel import RANK_ORDER
from t2t.nlevel import RANK_ORDER
from numpy import zeros, where, logical_or, long


Expand All @@ -25,7 +25,7 @@ def taxa_score(master, reps):
for rep in reps:
n_reps += 1

for id_, con in rep.iteritems():
for id_, con in rep.items():
if id_ not in master_ids:
raise KeyError("Unknown key %s in replicate" % id_)

Expand All @@ -49,7 +49,7 @@ def taxa_score(master, reps):

def merge_taxa_strings_and_scores(master, scores):
"""Merge taxa strings and their scores, return {id_:(taxa,score)}"""
return {k: zip(v, scores[k]) for k, v in master.items()}
return {k: list(zip(v, scores[k])) for k, v in master.items()}


def taxa_score_hash(master, reps):
Expand Down Expand Up @@ -108,7 +108,12 @@ def get_consensus_stats(consensus_map):
cons = consensus_map.values()
total_cons = len(cons)

rank_names = [c[0] for c in cons[0]]
rank_names = None
for co in cons:
if co is None:
continue
rank_names = [c[0] for c in co if c is not None]
break
for idx, rank in enumerate(rank_names):
# collect all cons that are classified (ie more info than k__)
klassed = [c[idx].lower()
Expand Down
6 changes: 3 additions & 3 deletions t2t/consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def calculate(self, tree, rooted):
consistency_index = {i: defaultdict(int) for i in range(self.n_ranks)}
for n in tree.traverse(include_self=True):
for rank in range(self.n_ranks):
for name, total_taxa_cnt in self.taxa_counts[rank].iteritems():
for name, total_taxa_cnt in self.taxa_counts[rank].items():
node_taxa_count = n.TaxaCount[rank].get(name, 0)
incongruent_taxa = n.NumTipsRank[rank] - node_taxa_count

Expand Down Expand Up @@ -93,7 +93,7 @@ def write_taxon_consistency(self, output_file, consistency_index):
fout = open(output_file, 'w')
fout.write('Taxon\tCount\tConsistency\n')
for rank in range(self.n_ranks):
for name, consistency in consistency_index[rank].iteritems():
for name, consistency in consistency_index[rank].items():
fout.write('%s\t%d\t%.3f\n' % (name,
self.taxa_counts[rank][name],
consistency))
Expand All @@ -116,7 +116,7 @@ def write_rank_consistency(self, output_file, consistency_index, min_taxa,
fout.write('Rank #\tRank prefix\t# taxon\tAverage consistency\n')
for rank in range(self.n_ranks):
val = []
for name, consistency in consistency_index[rank].iteritems():
for name, consistency in consistency_index[rank].items():
if self.taxa_counts[rank][name] >= min_taxa:
val.append(consistency)

Expand Down
4 changes: 2 additions & 2 deletions t2t/nlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from skbio import TreeNode
from t2t.util import unzip
import re
import os
import io

__author__ = "Daniel McDonald"
__copyright__ = "Copyright 2011, The tax2tree project"
Expand Down Expand Up @@ -517,6 +515,7 @@ def name_node_score_fold(tree, score_f=fmeasure, tiebreak_f=min_tips,

return used_scores


def score_tree(tree, verbose=False):
"""Scores the tree based on RankNameScores and tip coverage
Expand Down Expand Up @@ -572,6 +571,7 @@ def make_consensus_tree(cons_split, check_for_rank=True, tips=None):
god_node.Rank = None

base = list(cons_split)[0]
# SMJ base = next(iter(cons_split))
cur_node = god_node

# create a base path in the tree
Expand Down
8 changes: 4 additions & 4 deletions t2t/remap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
def parse_otu_map(lines):
"""Returns {rep: [members]}, members include rep"""
res = {}
for l in lines:
fields = l.strip().split('\t')
for line in lines:
fields = line.strip().split('\t')
rep = fields[1]
members = fields[1:]
res[rep] = members
Expand All @@ -15,7 +15,7 @@ def parse_otu_map(lines):
def members_to_rep(otus):
"""Provides a lookup for a cluster member to its rep"""
res = {}
for rep, members in otus.iteritems():
for rep, members in otus.items():
for member in members:
res[member] = rep
return res
Expand All @@ -26,7 +26,7 @@ def remap_taxonomy(mapping, taxa):
res = {}
reps = members_to_rep(mapping)

for tax_id, tax_str in taxa.iteritems():
for tax_id, tax_str in taxa.items():
rep = reps.get(tax_id, None)

if rep is None:
Expand Down
24 changes: 12 additions & 12 deletions tests/test_consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,20 @@ def test_hash_cons(self):
'c': [None, None, None, None, None, None, None],
'd': ['k__k2', 'p__x', 'c__c1', 'o__o10', 'f__f2', 'g__g2',
's__s3']}
exp = array([map(hash, input['a']),
map(hash, input['d']),
map(hash, input['c']),
map(hash, input['b'])])
exp = array([*map(hash, input['a']),
*map(hash, input['d']),
*map(hash, input['c']),
*map(hash, input['b'])])
obs = hash_cons(input, ['a', 'd', 'c', 'b'], 7)
self.assertTrue(array_equal(obs, exp))
self.assertTrue(array_equal(obs.flatten(), exp))

exp = array([map(hash, input['a']),
map(hash, input['d']),
[0, 0, 0, 0, 0, 0, 0],
map(hash, input['c']),
map(hash, input['b'])])
exp = array([*map(hash, input['a']),
*map(hash, input['d']),
*map(hash, [0, 0, 0, 0, 0, 0, 0]),
*map(hash, input['c']),
*map(hash, input['b'])])
obs = hash_cons(input, ['a', 'd', 'e', 'c', 'b'], 7)
self.assertTrue(array_equal(obs, exp))
self.assertTrue(array_equal(obs.flatten(), exp))

def test_get_consensus_stats(self):
"""Produces the correct stats"""
Expand All @@ -176,7 +176,7 @@ def test_get_consensus_stats(self):
'f': set(['f__f1', 'f__f2']), 'g': set(['g__g1',
'g__g2']),
's': set(['s__s1', 's__s2', 's__s3'])}
#exp_names = {'k':2,'p':1,'c':2,'o':1,'f':2,'g':2,'s':3}
# exp_names = {'k':2,'p':1,'c':2,'o':1,'f':2,'g':2,'s':3}
obs_nseqs, obs_names = get_consensus_stats(input)
self.assertEqual(obs_nseqs, exp_nseqs)
self.assertEqual(obs_names, exp_names)
Expand Down

0 comments on commit 9b3814f

Please sign in to comment.