|
#!/usr/bin/env python |
|
# File created on 03 Dec 2012 |
|
from __future__ import division |
|
|
|
__author__ = "Greg Caporaso" |
|
__copyright__ = "Copyright 2011, The QIIME project" |
|
__credits__ = ["Greg Caporaso"] |
|
__license__ = "GPL" |
|
__version__ = "1.5.0-dev" |
|
__maintainer__ = "Greg Caporaso" |
|
__email__ = "[email protected]" |
|
__status__ = "Development" |
|
|
|
|
|
from shutil import rmtree |
|
from os.path import exists, join |
|
from cogent.util.unit_test import TestCase, main |
|
from cogent.util.misc import remove_files, create_dir |
|
from biom.parse import parse_biom_table |
|
from qiime.util import (get_qiime_temp_dir, |
|
get_tmp_filename) |
|
from qiime.test import initiate_timeout, disable_timeout |
|
from filter_control_otus import (filter_otu_table_to_control_samples, |
|
identify_otus_to_filter, filter_control_otus) |
|
|
|
class IdentifyControlOtusTests(TestCase): |
|
|
|
def setUp(self): |
|
|
|
self.mapping_f = mapping_f.split("\n") |
|
self.otu_table = parse_biom_table(otu_table.split("\n")) |
|
self.otu_table_control = parse_biom_table(otu_table_control.split("\n")) |
|
self.otu_table_data = parse_biom_table(otu_table_data.split("\n")) |
|
self.otu_table_control_no_tax = parse_biom_table(otu_table_control_no_tax.split("\n")) |
|
self.control_filtered_otu_table = parse_biom_table(control_filtered_otu_table.split("\n")) |
|
|
|
|
|
def test_filter_otu_table_to_control_samples(self): |
|
"""filter_otu_table_to_control_samples functions as expected""" |
|
self.assertEqual( |
|
filter_otu_table_to_control_samples(self.otu_table,self.mapping_f,"Control:Yes"), |
|
self.otu_table_control) |
|
self.assertEqual( |
|
filter_otu_table_to_control_samples(self.otu_table,self.mapping_f,"Control:No"), |
|
self.otu_table_data) |
|
|
|
def test_identify_otus_to_filter(self): |
|
"""identify_otus_to_filter functions as expected""" |
|
self.assertEqual( |
|
identify_otus_to_filter(self.otu_table_control,0.05), |
|
[('o2','a;b;d'),('o3','a;b;e')]) |
|
self.assertEqual( |
|
identify_otus_to_filter(self.otu_table_control,0.5), |
|
[('o3','a;b;e')]) |
|
self.assertEqual( |
|
identify_otus_to_filter(self.otu_table_control_no_tax,0.05), |
|
[('o2',None),('o3',None)]) |
|
|
|
def test_filter_control_otus(self): |
|
"""filter_control_otus functions as expected""" |
|
self.assertEqual(filter_control_otus(self.otu_table,['o2','o3']), |
|
self.control_filtered_otu_table) |
|
|
|
mapping_f = """#SampleID Control Description |
|
s1 No s1 |
|
s2 No s2 |
|
s3 No s3 |
|
c1 Yes c1 |
|
c2 Yes c2""" |
|
|
|
otu_table = """{"rows": [{"id": "o1", "metadata": {"taxonomy": "a;b;c"}}, {"id": "o2", "metadata": {"taxonomy": "a;b;d"}}, {"id": "o3", "metadata": {"taxonomy": "a;b;e"}}, {"id": "o4", "metadata": {"taxonomy": "a;b;f"}}], "format": "Biological Observation Matrix 1.0.0", "data": [[0, 0, 66.0], [0, 1, 65.0], [0, 2, 2.0], [0, 3, 3.0], [0, 4, 6.0], [1, 0, 5.0], [1, 1, 6.0], [1, 3, 12.0], [1, 4, 3.0], [2, 0, 1.0], [2, 3, 85.0], [2, 4, 91.0], [3, 0, 28.0], [3, 1, 29.0], [3, 2, 98.0]], "columns": [{"id": "s1", "metadata": null}, {"id": "s2", "metadata": null}, {"id": "s3", "metadata": null}, {"id": "c1", "metadata": null}, {"id": "c2", "metadata": null}], "generated_by": "BIOM-Format 1.0.0c", "matrix_type": "sparse", "shape": [4, 5], "format_url": "http://biom-format.org", "date": "2012-12-03T12:42:38.166092", "type": "OTU table", "id": null, "matrix_element_type": "float"}""" |
|
|
|
otu_table_control = """{"rows": [{"id": "o1", "metadata": {"taxonomy": "a;b;c"}}, {"id": "o2", "metadata": {"taxonomy": "a;b;d"}}, {"id": "o3", "metadata": {"taxonomy": "a;b;e"}}, {"id": "o4", "metadata": {"taxonomy": "a;b;f"}}], "format": "Biological Observation Matrix 1.0.0", "data": [[0, 0, 3.0], [0, 1, 6.0], [1, 0, 12.0], [1, 1, 3.0], [2, 0, 85.0], [2, 1, 91.0]], "columns": [{"id": "c1", "metadata": null}, {"id": "c2", "metadata": null}], "generated_by": "BIOM-Format 1.0.0c", "matrix_type": "sparse", "shape": [4, 2], "format_url": "http://biom-format.org", "date": "2012-12-03T12:56:49.581524", "type": "OTU table", "id": null, "matrix_element_type": "float"}""" |
|
|
|
otu_table_control_no_tax = """{"rows": [{"id": "o1", "metadata":null}, {"id": "o2", "metadata": null}, {"id": "o3", "metadata": null}, {"id": "o4", "metadata": null}], "format": "Biological Observation Matrix 1.0.0", "data": [[0, 0, 3.0], [0, 1, 6.0], [1, 0, 12.0], [1, 1, 3.0], [2, 0, 85.0], [2, 1, 91.0]], "columns": [{"id": "c1", "metadata": null}, {"id": "c2", "metadata": null}], "generated_by": "BIOM-Format 1.0.0c", "matrix_type": "sparse", "shape": [4, 2], "format_url": "http://biom-format.org", "date": "2012-12-03T12:56:49.581524", "type": "OTU table", "id": null, "matrix_element_type": "float"}""" |
|
|
|
otu_table_data = """{"rows": [{"id": "o1", "metadata": {"taxonomy": "a;b;c"}}, {"id": "o2", "metadata": {"taxonomy": "a;b;d"}}, {"id": "o3", "metadata": {"taxonomy": "a;b;e"}}, {"id": "o4", "metadata": {"taxonomy": "a;b;f"}}], "format": "Biological Observation Matrix 1.0.0", "data": [[0, 0, 66.0], [0, 1, 65.0], [0, 2, 2.0], [1, 0, 5.0], [1, 1, 6.0], [2, 0, 1.0], [3, 0, 28.0], [3, 1, 29.0], [3, 2, 98.0]], "columns": [{"id": "s1", "metadata": null}, {"id": "s2", "metadata": null}, {"id": "s3", "metadata": null}], "generated_by": "BIOM-Format 1.0.0c", "matrix_type": "sparse", "shape": [4, 3], "format_url": "http://biom-format.org", "date": "2012-12-03T12:49:46.439032", "type": "OTU table", "id": null, "matrix_element_type": "float"}""" |
|
|
|
control_filtered_otu_table = """{"rows": [{"id": "o1", "metadata": {"taxonomy": "a;b;c"}}, {"id": "o4", "metadata": {"taxonomy": "a;b;f"}}], "format": "Biological Observation Matrix 1.0.0", "data": [[0, 0, 66.0], [0, 1, 65.0], [0, 2, 2.0], [0, 3, 3.0], [0, 4, 6.0], [1, 0, 28.0], [1, 1, 29.0], [1, 2, 98.0]], "columns": [{"id": "s1", "metadata": null}, {"id": "s2", "metadata": null}, {"id": "s3", "metadata": null}, {"id": "c1", "metadata": null}, {"id": "c2", "metadata": null}], "generated_by": "BIOM-Format 1.0.0c", "matrix_type": "sparse", "shape": [2, 5], "format_url": "http://biom-format.org", "date": "2012-12-03T13:26:19.896552", "type": "OTU table", "id": null, "matrix_element_type": "float"}""" |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |