예제 #1
0
def main():
    args = handle_program_options()

    try:
        # Load biom format file
        biomf = biom.load_table(args.input_biom_fp)
    except TypeError as te:
        sys.exit(
            "The data in the path does not appear to be a BIOM format table. "
            "Error: {}.".format(te))

    # Determine OTUIDs present in each sample
    sample_otus = oc.assign_otu_membership(biomf)

    try:
        # Parse mapping file
        header, imap = util.parse_map_file(args.mapping_file)
    except ValueError as ve:
        sys.exit("Error: {}.".format(ve))

    # Get relevant category information
    group_data = util.gather_categories(imap, header, [args.category_column])

    # Initialize results dict in group_data with {"otuids": set()} for each category
    for group in group_data:
        group_data[group].results["otuids"] = set()

    # Collect all OTUIDs present in each category
    for sid in sample_otus:
        group = sample_group(sid, group_data)
        group_data[group].results["otuids"].update(sample_otus[sid])

    if args.reverse:
        # Get shared OTUIDs
        shared = shared_otuids(group_data)
        # Write out shared OTUIDs results
        shared_df = pd.DataFrame.from_dict(shared, orient="index").T
        shared_df.to_csv(args.reverse, sep="\t", index=False)
    # Create input for unique_otus
    group_otuids = {
        group: group_data[group].results["otuids"]
        for group in group_data
    }
    # Write out unique OTUIDs to file
    write_uniques(args.output_dir, args.prefix, unique_otuids(group_otuids))
예제 #2
0
    def test_fuzzy_lookup(self):
        """
        Testing fuzzy_lookup() function of otu_calc.py.

        :return: Returns OK if the test goals were achieved, otherwise
                raises error.
        """
        self.result1 = oc.assign_otu_membership(self.biom)['Sample2']
        self.result = oc.fuzzy_lookup(self.result1, ['Escherichia_spp.'])

        # Obtaining manual result to be compared against.
        hand_calc = 'Escherichia_spp.'

        # Testing the validity of fuzzy_lookup() function.
        self.assertIn(
            hand_calc, str(self.result1),
            msg='Error! Output is not a subset of input.'
        )
예제 #3
0
    def test_sdi(self):
        """
        Testing sdi() function of otu_calc.py.

        :return: Returns OK if the test goals were achieved, otherwise
                 raises error.
        """
        self.result1 = oc.assign_otu_membership(self.biom)
        fset = self.result1['Sample4']
        self.result = oc.sdi(fset)

        # Manual calculation of SDI
        hand_calc = 0.636513937

        # Testing the validity of sdi() function
        self.assertAlmostEqual(
            self.result, hand_calc, places=6,
            msg='Error! SDI was calculated inacccurately.'
        )
예제 #4
0
    def test_print_membership(self):
        """
        Testing print_membership() function of otu_calc.py. Output of this
        function is written to a temporary file. The contents of the temporary
        file are accessed and used to compare with expected output.

        :return: Returns OK if the test goals were achieved, otherwise
                 raises error.
        """
        self.result1 = oc.assign_otu_membership(self.biom)

        # Writing the output of print_membership() to temporary file.
        saveout = sys.stdout
        temp_file2 = tempfile.NamedTemporaryFile(delete=False)
        with open(temp_file2.name, 'w') as f:
            sys.stdout = f
            self.result = oc.print_membership(self.result1['Sample1'])
        sys.stdout = saveout

        # Obtaining the function output from output.txt file.
        func_calc = []
        with open(temp_file2.name, 'r') as out:
            while True:
                statement = out.readline()
                func_calc.append(statement)
                if not statement:
                    break

        # Writing manual outputs to compare
        hand_calc = [
            'Halanaerobium_Halanaerobiumsaccharolyticum: 28.57%\n',
            'Dolichospermum_spp.: 71.43%\n'
            ]

        # Testing the validity of print_membership() function
        self.assertListEqual(
            func_calc[0:2], hand_calc,
            msg='Error! Output does not print out the right information correctly.'
        )

        # Deleting temporary file.
        os .unlink(temp_file2.name)
예제 #5
0
    def test_assign_otu_membership(self):
        """
        Testing assign_otu_membership() function of otu_calc.py.

        :return: Returns OK if the test goals were achieved, otherwise
                 raises error.
        """
        self.result = oc.assign_otu_membership(self.biom)

        # Obtaining the values to be tested
        result1 = bc.relative_abundance(self.biom, ['Sample1'])
        hand_calc = result1.values()[0].values()
        func_calc = [0.714286, 0.285714]

        # Testing the validity of assign_otu_membership() function
        for hand, func in zip(hand_calc, func_calc):
            self.assertAlmostEqual(
                hand, func, places=5,
                msg='Error! OTU membership calculations are inaccurate!'
            )
def main():
    args = handle_program_options()

    try:
        # Load biom format file
        biomf = biom.load_table(args.input_biom_fp)
    except TypeError as te:
        sys.exit("The data in the path does not appear to be a BIOM format table. "
                 "Error: {}.".format(te))

    # Determine OTUIDs present in each sample
    sample_otus = oc.assign_otu_membership(biomf)

    try:
        # Parse mapping file
        header, imap = util.parse_map_file(args.mapping_file)
    except ValueError as ve:
        sys.exit("Error: {}.".format(ve))

    # Get relevant category information
    group_data = util.gather_categories(imap, header, [args.category_column])

    # Initialize results dict in group_data with {"otuids": set()} for each category
    for group in group_data:
        group_data[group].results["otuids"] = set()

    # Collect all OTUIDs present in each category
    for sid in sample_otus:
        group = sample_group(sid, group_data)
        group_data[group].results["otuids"].update(sample_otus[sid])

    if args.reverse:
        # Get shared OTUIDs
        shared = shared_otuids(group_data)
        # Write out shared OTUIDs results
        shared_df = pd.DataFrame.from_dict(shared, orient="index").T
        shared_df.to_csv(args.reverse, sep="\t", index=False)
    # Create input for unique_otus
    group_otuids = {group: group_data[group].results["otuids"] for group in group_data}
    # Write out unique OTUIDs to file
    write_uniques(args.output_dir, args.prefix, unique_otuids(group_otuids))
예제 #7
0
    def test_assign_otu_membership(self):
        """
        Testing assign_otu_membership() function of otu_calc.py.

        :return: Returns OK if the test goals were achieved, otherwise
                 raises error.
        """
        self.biomf = biom.load_table("phylotoast/test/test.biom")
        self.result = oc.assign_otu_membership(self.biomf)

        # Obtaining the values to be tested
        hand_calc = {"S9": ["GG_OTU_2", "GG_OTU_3", "GG_OTU_5"],
                     "S3": ["GG_OTU_1", "GG_OTU_2", "GG_OTU_4", "GG_OTU_5"],
                     "S6": ["GG_OTU_1", "GG_OTU_2", "GG_OTU_3", "GG_OTU_4", "GG_OTU_5"]}

        # Testing the validity of assign_otu_membership() function
        for sid in ["S3", "S6", "S9"]:
            self.assertListEqual(
                sorted(hand_calc[sid]), sorted(self.result[sid]),
                msg="Error! OTU membership calculations are inaccurate!"
            )
예제 #8
0
    def test_assign_otu_membership(self):
        """
        Testing assign_otu_membership() function of otu_calc.py.

        :return: Returns OK if the test goals were achieved, otherwise
                 raises error.
        """
        self.biomf = biom.load_table("phylotoast/test/test.biom")
        self.result = oc.assign_otu_membership(self.biomf)

        # Obtaining the values to be tested
        hand_calc = {
            "S9": ["GG_OTU_2", "GG_OTU_3", "GG_OTU_5"],
            "S3": ["GG_OTU_1", "GG_OTU_2", "GG_OTU_4", "GG_OTU_5"],
            "S6": ["GG_OTU_1", "GG_OTU_2", "GG_OTU_3", "GG_OTU_4", "GG_OTU_5"]
        }

        # Testing the validity of assign_otu_membership() function
        for sid in ["S3", "S6", "S9"]:
            self.assertListEqual(
                sorted(hand_calc[sid]),
                sorted(self.result[sid]),
                msg="Error! OTU membership calculations are inaccurate!")