Skip to content

Commit

Permalink
Refactor modules to top-level & add guard to Unifrac
Browse files Browse the repository at this point in the history
  • Loading branch information
Roderick Bovee committed Aug 16, 2017
1 parent de86bac commit 8130b32
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 12 deletions.
10 changes: 6 additions & 4 deletions onecodex/lib/distance_metrics.py → onecodex/distance.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import skbio.diversity

from onecodex.lib.taxonomy import generate_skbio_tree, prune_to_rank
from onecodex.taxonomy import generate_skbio_tree, prune_to_rank


ACCEPTABLE_FIELDS = ['abundance', 'readcount_w_children', 'readcount']
Expand All @@ -16,7 +16,7 @@ def alpha_counts(classification, field='readcount_w_children', rank='species'):


def beta_counts(classifications, field='readcount_w_children', rank='species'):
from onecodex.viz.helpers import normalize_analyses, collate_analysis_results
from onecodex.helpers import normalize_analyses, collate_analysis_results
normed_analyses, _ = normalize_analyses(classifications)
df = collate_analysis_results(normed_analyses, field=field)
df = df.loc[:, [i[2] == rank for i in df.columns]]
Expand Down Expand Up @@ -73,17 +73,19 @@ def unifrac(classifications, weighted=True,
A beta diversity metric that takes into account the relative relatedness of community members.
Weighted UniFrac looks at abundances, unweighted UniFrac looks at presence
"""
assert field in ACCEPTABLE_FIELDS
from skbio.tree import TreeNode

assert field in ACCEPTABLE_FIELDS
counts, tax_ids, ids = beta_counts(classifications, field=field, rank=rank)

tree = None
for c in classifications:
assert c.job.id == classifications[0].job.id, "All classifications must " \
"have same job for Unifrac"
tree = generate_skbio_tree(c, existing_tree=tree)

# there's a bug (?) in skbio where it expects the root to only have
# one child, so we do a little faking here
from skbio.tree import TreeNode
new_tree = TreeNode(name='fake root')
new_tree.rank = 'no rank'
new_tree.append(tree)
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions onecodex/viz/distance.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pandas as pd

from onecodex.exceptions import OneCodexException
from onecodex.viz.helpers import normalize_analyses
from onecodex.lib.distance_metrics import braycurtis, cityblock, jaccard
from onecodex.helpers import normalize_analyses
from onecodex.distance import braycurtis, cityblock, jaccard


def plot_distance(analyses, title=None, distance_metric='bray-curtis',
Expand Down
2 changes: 1 addition & 1 deletion onecodex/viz/heatmap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from onecodex.exceptions import OneCodexException
from onecodex.viz.helpers import collate_analysis_results, normalize_analyses
from onecodex.helpers import collate_analysis_results, normalize_analyses


def plot_heatmap(analyses, title=None, top_n=20, threshold=None, field='readcount_w_children'):
Expand Down
4 changes: 2 additions & 2 deletions onecodex/viz/metadata.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pandas as pd

from onecodex.exceptions import OneCodexException
from onecodex.viz.helpers import normalize_analyses, collate_analysis_results
from onecodex.lib.distance_metrics import simpson, chao1
from onecodex.helpers import normalize_analyses, collate_analysis_results
from onecodex.distance import simpson, chao1


def plot_metadata(analyses, title=None, metadata='created_at', statistic=None,
Expand Down
2 changes: 1 addition & 1 deletion onecodex/viz/pca.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pandas as pd

from onecodex.viz.helpers import collate_analysis_results, normalize_analyses
from onecodex.helpers import collate_analysis_results, normalize_analyses


def plot_pca(analyses, title=None, threshold=None, field='readcount_w_children',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_taxonomy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from onecodex.lib.taxonomy import generate_skbio_tree, prune_to_rank
from onecodex.taxonomy import generate_skbio_tree, prune_to_rank


def test_tree_generation(ocx, api_data):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_viz.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from onecodex.viz.helpers import collate_analysis_results
from onecodex.helpers import collate_analysis_results


def test_result_collation(ocx, api_data):
Expand Down

0 comments on commit 8130b32

Please sign in to comment.