Skip to content

Commit

Permalink
Merge pull request #28 from sjanssen2/skbio
Browse files Browse the repository at this point in the history
flake8 + skbio 0.4.2
  • Loading branch information
wasade authored Jul 18, 2018
2 parents 99f19be + fdd44eb commit 7398a25
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 158 deletions.
18 changes: 10 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# Modified from https://github.com/biocore/biom-format/
language: python
env:
- PYTHON_VERSION=2.7 NUMPY_VERSION=1.7
- PYTHON_VERSION=2.7
before_install:
- wget http://repo.continuum.io/miniconda/Miniconda-2.2.2-Linux-x86_64.sh -O miniconda.sh
- chmod +x miniconda.sh
- ./miniconda.sh -b
- export PATH=/home/travis/anaconda/bin:$PATH
- wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh -O miniconda.sh
- export MINICONDA_PREFIX="$HOME/miniconda"
- bash miniconda.sh -b -p $MINICONDA_PREFIX
- export PATH="$MINICONDA_PREFIX/bin:$PATH"
# Update conda itself
- conda update --yes conda
- conda config --set always_yes yes
- conda update -q conda
- conda info -a
install:
- conda create --yes -n env_name python=$PYTHON_VERSION pip numpy=$NUMPY_VERSION scipy matplotlib nose pep8 Sphinx
- conda create --yes -n env_name python=$PYTHON_VERSION pip numpy scipy matplotlib nose pep8 Sphinx
- source activate env_name
- pip install future==0.13.1 coveralls natsort pyflakes flake8 python-dateutil scikit-bio click
- pip install future coveralls natsort pyflakes flake8 python-dateutil scikit-bio==0.4.2 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
1 change: 0 additions & 1 deletion INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ $ python setup.py install --prefix=$HOME
and tax2tree will be placed into your path. To run, just call:

$ t2t

68 changes: 41 additions & 27 deletions scripts/t2t
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ def print_version(ctx, param, value):
click.echo('Version %s' % t2t.__version__)
ctx.exit()

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])

@click.group()

@click.group(context_settings=CONTEXT_SETTINGS)
@click.option('--version', is_flag=True, callback=print_version,
expose_value=False, is_eager=True)
@click.pass_context
Expand All @@ -32,16 +34,28 @@ def cli(ctx):
@click.option('--output', '-o', required=True, help='Output basename')
@click.option('--tree', '-t', required=True, help='Input tree',
type=click.File('U'))
def decorate(tree, consensus_map, output):
@click.option('--no-suffix', '-n',
help="Don't append suffixes (e.g. _1, _2) to polyphyletic " +
"groups",
is_flag=True, default=False)
@click.option('--suffix-char', '-s',
help="Use a different char (instead of underscore) for " +
"polyphyletic group suffixes",
required=False, default="_", type=str)

def decorate(tree, consensus_map, output, no_suffix, suffix_char):
"""Decorate a taxonomy onto a tree"""
append_rank = False
seed_con = consensus_map.readline().strip().split('\t')[1]

# get desired ranks from first line of consensus map
seed_con = consensus_map.readline().strip().split('\t')[1]
nl.determine_rank_order(seed_con)
consensus_map.seek(0)

tipname_map = nl.load_consensus_map(consensus_map, append_rank)
tree_ = nl.load_tree(tree, tipname_map)
counts = nl.collect_names_at_ranks_counts(tree_)

nl.decorate_ntips(tree_)
nl.decorate_name_relative_freqs(tree_, counts, 2)
nl.set_ranksafe(tree_)
Expand All @@ -53,7 +67,9 @@ def decorate(tree, consensus_map, output):
contree, contree_lookup = nl.make_consensus_tree(tipname_map.values())
nl.backfill_names_gap(tree_, contree_lookup)
nl.commonname_promotion(tree_)
nl.make_names_unique(tree_)

if not no_suffix:
nl.make_names_unique(tree_, suffix_glue_char=suffix_char)

constrings = nl.pull_consensus_strings(tree_)

Expand All @@ -62,9 +78,7 @@ def decorate(tree, consensus_map, output):
f.close()

nl.save_bootstraps(tree_)
f = open(output, 'w')
f.write(tree_.to_newick(with_distances=True))
f.close()
tree_.write(output)


@cli.command()
Expand All @@ -79,8 +93,7 @@ def reroot(tree, tips, output):
tipnames = set([l.strip() for l in tips])
tree_ = TreeNode.from_newick(tree)
rerooted = ut.reroot(tree_, tipnames)

output.write(rerooted.to_newick(with_distances=True))
rerooted.write(output)


@cli.command()
Expand Down Expand Up @@ -110,7 +123,7 @@ def fetch(tree, output):
"""Fetch the taxonomy off the tree"""
result, error = t2tcli.fetch(tree)
if error:
print '\n'.join(result)
click.echo('\n'.join(result))
else:
output.write('\n'.join(result))

Expand All @@ -126,7 +139,8 @@ def validate(taxonomy, limit, flat_errors, hierarchy_errors):
lines = taxonomy.readlines()
result, err = t2t.cli.validate(lines, limit, flat_errors, hierarchy_errors)

print '\n'.join(result)
click.echo('\n'.join(result))
click.echo('Validation complete.')

@cli.command()
@click.option('--consensus-map', '-m', required=True,
Expand All @@ -135,35 +149,35 @@ def validate(taxonomy, limit, flat_errors, hierarchy_errors):
@click.option('--tree', '-t', required=True, help='Input tree',
type=click.File('U'))
@click.option('--rooted/--unrooted', default=True, help='Treat tree as rooted or unrooted')
@click.option('--quiet', is_flag=True, default=False, help='Suppress output')
def consistency(tree, consensus_map, output_file, rooted, quiet):
@click.option('--verbose', is_flag=True, default=False, help='Provide detailed output')
def consistency(tree, consensus_map, output_file, rooted, verbose):
"""Consistency of a tree relative to taxonomy"""
if not quiet:
print 'Determining taxonomic consistency of: '
print ' tree = ' + tree.name
print ' consensus-map = ' + consensus_map.name
print ' rooted = ' + str(rooted)
print ''

if verbose:
click.echo('Determining taxonomic consistency of: ')
click.echo(' tree = ' + tree.name)
click.echo(' consensus-map = ' + consensus_map.name)
click.echo(' rooted = ' + str(rooted))
click.echo('')

# dynamically determine taxonomic ranks
seed_con = consensus_map.readline().strip().split('\t')[1]
nl.determine_rank_order(seed_con)

tipname_map = nl.load_consensus_map(consensus_map, append_rank = False)
tipname_map = nl.load_consensus_map(consensus_map, append_rank=False)
tree = nl.load_tree(tree, tipname_map)

counts = nl.collect_names_at_ranks_counts(tree)
nl.decorate_ntips_rank(tree)
nl.decorate_name_counts(tree)

# determine taxonomic consistency of tree
c = con.Consistency(counts, len(nl.RANK_ORDER))
consistency_index = c.calculate(tree, rooted)
c.write(output_file, consistency_index)
if not quiet:
print 'Consistency written to: ' + output_file

if verbose:
click.echo('Consistency written to: ' + output_file)


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
if on_rtd:
install_requires = []
else:
install_requires = ['numpy >= 1.7', 'future==0.13.1', 'scikit-bio',
install_requires = ['numpy >= 1.7', 'future', 'scikit-bio',
'Click']

setup(name='tax2tree',
Expand Down
13 changes: 7 additions & 6 deletions t2t/consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
etc...
"""
from nlevel import RANK_ORDER
from numpy import zeros, where, logical_or
from numpy import zeros, where, logical_or, long


def taxa_score(master, reps):
Expand Down Expand Up @@ -111,8 +111,9 @@ def get_consensus_stats(consensus_map):
rank_names = [c[0] for c in cons[0]]
for idx, rank in enumerate(rank_names):
# collect all cons that are classified (ie more info than k__)
klassed = [c[idx].lower() for c in cons if c[idx]
and c[idx][0] == rank]
klassed = [c[idx].lower()
for c
in cons if c[idx] and c[idx][0] == rank]

n_classified = len(klassed)

Expand All @@ -126,7 +127,7 @@ def get_consensus_stats(consensus_map):

def pretty_print_consensus_stats(stats):
seqs, names = stats
print '\t'.join(['rank', 'num_classified', 'num_unclassified',
'num_names'])
print('\t'.join(['rank', 'num_classified', 'num_unclassified',
'num_names']))
for k in RANK_ORDER:
print '\t'.join(map(str, [k, seqs[k][0], seqs[k][1], len(names[k])]))
print('\t'.join(map(str, [k, seqs[k][0], seqs[k][1], len(names[k])])))
18 changes: 9 additions & 9 deletions t2t/consistency.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env python

from collections import defaultdict

from numpy import mean

__author__ = "Donovan Park"
__copyright__ = "Copyright 2014, The tax2tree project"
__credits__ = ["Donovan Park"]
Expand All @@ -9,10 +13,6 @@
__email__ = "donovan.parks@gmail.com"
__status__ = "Development"

from collections import defaultdict

from numpy import mean


class Consistency(object):
"""Calculates the consistency of taxonomic groups within a reference tree.
Expand Down Expand Up @@ -50,13 +50,13 @@ def calculate(self, tree, rooted):
# determine total number of informative tips in tree for each rank
total_informative_tips = defaultdict(int)
for n in tree.tips():
for rank in xrange(self.n_ranks):
for rank in range(self.n_ranks):
total_informative_tips[rank] += n.NumTipsRank[rank]

# determine highest consistency node for each taxa
consistency_index = {i: defaultdict(int) for i in xrange(self.n_ranks)}
consistency_index = {i: defaultdict(int) for i in range(self.n_ranks)}
for n in tree.traverse(include_self=True):
for rank in xrange(self.n_ranks):
for rank in range(self.n_ranks):
for name, total_taxa_cnt in self.taxa_counts[rank].iteritems():
node_taxa_count = n.TaxaCount[rank].get(name, 0)
incongruent_taxa = n.NumTipsRank[rank] - node_taxa_count
Expand Down Expand Up @@ -92,7 +92,7 @@ def write_taxon_consistency(self, output_file, consistency_index):

fout = open(output_file, 'w')
fout.write('Taxon\tCount\tConsistency\n')
for rank in xrange(self.n_ranks):
for rank in range(self.n_ranks):
for name, consistency in consistency_index[rank].iteritems():
fout.write('%s\t%d\t%.3f\n' % (name,
self.taxa_counts[rank][name],
Expand All @@ -114,7 +114,7 @@ def write_rank_consistency(self, output_file, consistency_index, min_taxa,

fout = open(output_file, 'w')
fout.write('Rank #\tRank prefix\t# taxon\tAverage consistency\n')
for rank in xrange(self.n_ranks):
for rank in range(self.n_ranks):
val = []
for name, consistency in consistency_index[rank].iteritems():
if self.taxa_counts[rank][name] >= min_taxa:
Expand Down
Loading

0 comments on commit 7398a25

Please sign in to comment.