-
Notifications
You must be signed in to change notification settings - Fork 28
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
Util #12
Merged
Merged
Util #12
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
362fe9e
ENH: Adding util functions
mortonjt a1b9cef
STY: pep8
mortonjt 2908c93
STY: Flake8 to the rescue
mortonjt e2e2e63
STY: Addressing @antgonza and @josenavas comments
mortonjt d6a037b
STY: pep8 flake8
mortonjt d197605
DOC: Changing method name
mortonjt 2ded931
STY: clean up pep8/flake8
mortonjt a54d051
Merge branch 'master' of https://github.com/biocore/gneiss into util
mortonjt 477d196
DOC: Updating changelog
mortonjt 371cc16
ENH: Adding inplace option
mortonjt 6d70cf9
STY: Code dereplication
mortonjt 9003db8
DOC: cleaning up docstrings
mortonjt b049ced
pep8
mortonjt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,322 @@ | ||
# ---------------------------------------------------------------------------- | ||
# Copyright (c) 2016--, gneiss development team. | ||
# | ||
# Distributed under the terms of the GPLv3 License. | ||
# | ||
# The full license is in the file COPYING.txt, distributed with this software. | ||
# ---------------------------------------------------------------------------- | ||
|
||
import unittest | ||
import pandas as pd | ||
import pandas.util.testing as pdt | ||
from skbio import TreeNode | ||
from gneiss.util import match, match_tips, rename_internal_nodes | ||
|
||
|
||
class TestUtil(unittest.TestCase): | ||
|
||
def test_match(self): | ||
table = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 2, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['o1', 'o2', 'o3', 'o4']) | ||
metadata = pd.DataFrame([['a', 'control'], | ||
['b', 'control'], | ||
['c', 'diseased'], | ||
['d', 'diseased']], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['Barcode', 'Treatment']) | ||
exp_table, exp_metadata = table, metadata | ||
res_table, res_metadata = match(table, metadata) | ||
pdt.assert_frame_equal(exp_table, res_table) | ||
pdt.assert_frame_equal(exp_metadata, res_metadata) | ||
|
||
def test_match_immutable(self): | ||
# tests to make sure that the original tables don't change. | ||
table = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 2, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['o1', 'o2', 'o3', 'o4']) | ||
metadata = pd.DataFrame([['a', 'control'], | ||
['c', 'diseased'], | ||
['b', 'control']], | ||
index=['s1', 's3', 's2'], | ||
columns=['Barcode', 'Treatment']) | ||
|
||
exp_table = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 2, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['o1', 'o2', 'o3', 'o4']) | ||
exp_metadata = pd.DataFrame([['a', 'control'], | ||
['c', 'diseased'], | ||
['b', 'control']], | ||
index=['s1', 's3', 's2'], | ||
columns=['Barcode', 'Treatment']) | ||
match(table, metadata, intersect=True) | ||
pdt.assert_frame_equal(table, exp_table) | ||
pdt.assert_frame_equal(metadata, exp_metadata) | ||
|
||
def test_match_duplicate(self): | ||
table1 = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 2, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s2', 's2', 's3', 's4'], | ||
columns=['o1', 'o2', 'o3', 'o4']) | ||
metadata1 = pd.DataFrame([['a', 'control'], | ||
['b', 'control'], | ||
['c', 'diseased'], | ||
['d', 'diseased']], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['Barcode', 'Treatment']) | ||
|
||
table2 = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 2, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['o1', 'o2', 'o3', 'o4']) | ||
metadata2 = pd.DataFrame([['a', 'control'], | ||
['b', 'control'], | ||
['c', 'diseased'], | ||
['d', 'diseased']], | ||
index=['s1', 's1', 's3', 's4'], | ||
columns=['Barcode', 'Treatment']) | ||
|
||
with self.assertRaises(ValueError): | ||
match(table1, metadata1) | ||
with self.assertRaises(ValueError): | ||
match(table2, metadata2) | ||
|
||
def test_match_scrambled(self): | ||
table = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 2, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['o1', 'o2', 'o3', 'o4']) | ||
metadata = pd.DataFrame([['a', 'control'], | ||
['c', 'diseased'], | ||
['b', 'control'], | ||
['d', 'diseased']], | ||
index=['s1', 's3', 's2', 's4'], | ||
columns=['Barcode', 'Treatment']) | ||
exp_table = table | ||
exp_metadata = pd.DataFrame([['a', 'control'], | ||
['b', 'control'], | ||
['c', 'diseased'], | ||
['d', 'diseased']], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['Barcode', 'Treatment']) | ||
|
||
res_table, res_metadata = match(table, metadata) | ||
pdt.assert_frame_equal(exp_table, res_table) | ||
pdt.assert_frame_equal(exp_metadata, res_metadata) | ||
|
||
def test_match_intersect(self): | ||
table = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 2, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['o1', 'o2', 'o3', 'o4']) | ||
metadata = pd.DataFrame([['a', 'control'], | ||
['c', 'diseased'], | ||
['b', 'control']], | ||
index=['s1', 's3', 's2'], | ||
columns=['Barcode', 'Treatment']) | ||
|
||
exp_table = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 2, 4, 4], | ||
[5, 5, 3, 3]], | ||
index=['s1', 's2', 's3'], | ||
columns=['o1', 'o2', 'o3', 'o4']) | ||
|
||
exp_metadata = pd.DataFrame([['a', 'control'], | ||
['b', 'control'], | ||
['c', 'diseased']], | ||
index=['s1', 's2', 's3'], | ||
columns=['Barcode', 'Treatment']) | ||
|
||
res_table, res_metadata = match(table, metadata, intersect=True) | ||
pdt.assert_frame_equal(exp_table, res_table) | ||
pdt.assert_frame_equal(exp_metadata, res_metadata) | ||
|
||
def test_match_mismatch(self): | ||
table = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 2, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['o1', 'o2', 'o3', 'o4']) | ||
metadata = pd.DataFrame([['a', 'control'], | ||
['c', 'diseased'], | ||
['b', 'control']], | ||
index=['s1', 's3', 's2'], | ||
columns=['Barcode', 'Treatment']) | ||
with self.assertRaises(ValueError): | ||
match(table, metadata) | ||
|
||
def test_match_tips(self): | ||
table = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 2, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['a', 'b', 'c', 'd']) | ||
tree = TreeNode.read([u"(((a,b)f, c),d)r;"]) | ||
exp_table, exp_tree = table, tree | ||
res_table, res_tree = match_tips(table, tree) | ||
pdt.assert_frame_equal(exp_table, res_table) | ||
self.assertEqual(str(exp_tree), str(res_tree)) | ||
|
||
def test_match_tips_scrambled_tips(self): | ||
table = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 3, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['a', 'b', 'c', 'd']) | ||
tree = TreeNode.read([u"(((b,a)f, c),d)r;"]) | ||
exp_tree = tree | ||
exp_table = pd.DataFrame([[0, 0, 1, 1], | ||
[3, 2, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['b', 'a', 'c', 'd']) | ||
|
||
res_table, res_tree = match_tips(table, tree) | ||
pdt.assert_frame_equal(exp_table, res_table) | ||
self.assertEqual(str(exp_tree), str(res_tree)) | ||
|
||
def test_match_tips_scrambled_columns(self): | ||
table = pd.DataFrame([[0, 0, 1, 1], | ||
[3, 2, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['b', 'a', 'c', 'd']) | ||
tree = TreeNode.read([u"(((a,b)f, c),d)r;"]) | ||
exp_tree = tree | ||
exp_table = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 3, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['a', 'b', 'c', 'd']) | ||
|
||
res_table, res_tree = match_tips(table, tree) | ||
pdt.assert_frame_equal(exp_table, res_table) | ||
self.assertEqual(str(exp_tree), str(res_tree)) | ||
|
||
def test_match_tips_intersect_tips(self): | ||
# there are less tree tips than table columns | ||
table = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 3, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['a', 'b', 'c', 'd']) | ||
tree = TreeNode.read([u"((a,b)f,d)r;"]) | ||
exp_table = pd.DataFrame([[0, 0, 1], | ||
[2, 3, 4], | ||
[5, 5, 3], | ||
[0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['a', 'b', 'd']) | ||
exp_tree = tree | ||
res_table, res_tree = match_tips(table, tree, intersect=True) | ||
pdt.assert_frame_equal(exp_table, res_table) | ||
self.assertEqual(str(exp_tree), str(res_tree)) | ||
|
||
def test_match_tips_intersect_columns(self): | ||
# table has less columns than tree tips | ||
table = pd.DataFrame([[0, 0, 1], | ||
[2, 3, 4], | ||
[5, 5, 3], | ||
[0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['a', 'b', 'd']) | ||
tree = TreeNode.read([u"(((a,b)f, c),d)r;"]) | ||
exp_table = pd.DataFrame([[1, 0, 0], | ||
[4, 2, 3], | ||
[3, 5, 5], | ||
[1, 0, 0]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['d', 'a', 'b']) | ||
exp_tree = TreeNode.read([u"(d,(a,b)f)r;"]) | ||
res_table, res_tree = match_tips(table, tree, intersect=True) | ||
pdt.assert_frame_equal(exp_table, res_table) | ||
self.assertEqual(str(exp_tree), str(res_tree)) | ||
|
||
def test_match_tips_intersect_tree_immutable(self): | ||
# tests to see if tree chnages. | ||
table = pd.DataFrame([[0, 0, 1], | ||
[2, 3, 4], | ||
[5, 5, 3], | ||
[0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['a', 'b', 'd']) | ||
tree = TreeNode.read([u"(((a,b)f, c),d)r;"]) | ||
match_tips(table, tree, intersect=True) | ||
self.assertEqual(str(tree), u"(((a,b)f,c),d)r;\n") | ||
|
||
def test_match_tips_mismatch(self): | ||
# table has less columns than tree tips | ||
table = pd.DataFrame([[0, 0, 1], | ||
[2, 3, 4], | ||
[5, 5, 3], | ||
[0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['a', 'b', 'd']) | ||
tree = TreeNode.read([u"(((a,b)f, c),d)r;"]) | ||
with self.assertRaises(ValueError): | ||
match_tips(table, tree) | ||
|
||
table = pd.DataFrame([[0, 0, 1, 1], | ||
[2, 3, 4, 4], | ||
[5, 5, 3, 3], | ||
[0, 0, 0, 1]], | ||
index=['s1', 's2', 's3', 's4'], | ||
columns=['a', 'b', 'c', 'd']) | ||
tree = TreeNode.read([u"((a,b)f,d)r;"]) | ||
with self.assertRaises(ValueError): | ||
match_tips(table, tree) | ||
|
||
def test_rename_internal_nodes(self): | ||
tree = TreeNode.read([u"(((a,b), c),d)r;"]) | ||
exp_tree = TreeNode.read([u"(((a,b)y2, c)y1,d)y0;"]) | ||
res_tree = rename_internal_nodes(tree) | ||
self.assertEqual(str(exp_tree), str(res_tree)) | ||
|
||
def test_rename_internal_nodes_names(self): | ||
tree = TreeNode.read([u"(((a,b), c),d)r;"]) | ||
exp_tree = TreeNode.read([u"(((a,b)ab, c)abc,d)r;"]) | ||
res_tree = rename_internal_nodes(tree, ['r', 'abc', 'ab']) | ||
self.assertEqual(str(exp_tree), str(res_tree)) | ||
|
||
def test_rename_internal_nodes_names_mismatch(self): | ||
tree = TreeNode.read([u"(((a,b), c),d)r;"]) | ||
with self.assertRaises(ValueError): | ||
rename_internal_nodes(tree, ['r', 'abc']) | ||
|
||
def test_rename_internal_nodes_warning(self): | ||
tree = TreeNode.read([u"(((a,b)y2, c),d)r;"]) | ||
with self.assertWarns(Warning): | ||
rename_internal_nodes(tree) | ||
|
||
def test_rename_internal_nodes_immutable(self): | ||
tree = TreeNode.read([u"(((a,b)y2, c),d)r;"]) | ||
rename_internal_nodes(tree) | ||
self.assertEqual(str(tree), "(((a,b)y2,c),d)r;\n") | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized that none of this files have the copyright notice on top. Should it be added?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍