def test_make_new_otu_counts(self):
        """make_new_otu_counts works
        """
        mapping_lines = """#SampleID\tindividual\ttimepoint_zero\ttimepoint
AT0\tA\t1\t0
AT1\tA\t0\t1
AT2\tA\t0\t2
BT0\tB\t1\t0
BT1\tB\t0\t1
BT2\tB\t0\t2
""".split('\n')
        mapping_data, header, comments = parse_mapping_file(mapping_lines)
        samples_from_subject, sample_to_subtract = \
            get_sample_individual_info(mapping_data, header, 'individual', \
            'timepoint_zero')
        otu_lines = """# QIIME v1.2.0-dev OTU table
#OTU ID\tAT0\tAT1\tS1\tAT2\tBT0\tBT1\tBT2
0\t0.5\t0.3\t99\t0.2\t0.0\t0.0\t0.0
1\t0.0\t0.0\t99\t0.0\t0.4\t0.5\t0.6
2\t0.1\t0.4\t99\t0.7\t0.5\t0.6\t0.8
3\t0.0\t0.1\t99\t0.0\t0.4\t0.0\t0.0
""".split('\n')
        otu_table = parse_otu_table(otu_lines, float)
        sample_ids, otu_ids, otu_counts, consensus = otu_table
        converted_otu_table = make_new_otu_counts(otu_ids, sample_ids, otu_counts, consensus, sample_to_subtract, samples_from_subject)
        converted_otu_table = converted_otu_table.split('\n')
        self.assertEqual(converted_otu_table[1], "#OTU ID\tAT0\tAT1\tAT2\tBT0\tBT1\tBT2")
        self.assertEqual(converted_otu_table[2], "0\t0.0\t-0.2\t-0.3\t999999999.0\t999999999.0\t999999999.0")
        self.assertEqual(converted_otu_table[3], "1\t999999999.0\t999999999.0\t999999999.0\t0.0\t0.1\t0.2")
        self.assertEqual(converted_otu_table[4], "2\t0.0\t0.3\t0.6\t0.0\t0.1\t0.3")
        self.assertEqual(converted_otu_table[5], "3\t0.0\t0.1\t0.0\t0.0\t-0.4\t-0.4")
    def test_get_sample_individual_info(self):
        """get_sample_individual_info works
        """
        mapping_lines = """#SampleID\tindividual\ttimepoint_zero\ttimepoint
AT0\tA\t1\t0
AT1\tA\t0\t1
AT2\tA\t0\t2
BT0\tB\t1\t0
BT1\tB\t0\t1
BT2\tB\t0\t2
""".split('\n')
        mapping_data, header, comments = parse_mapping_file(mapping_lines)
        samples_from_subject, samples_to_subtract = \
            get_sample_individual_info(mapping_data, header, 'individual', \
                'timepoint_zero')
        self.assertEqual(
            samples_from_subject, {
                'BT1': ['BT0', 'BT1', 'BT2'],
                'BT0': ['BT0', 'BT1', 'BT2'],
                'BT2': ['BT0', 'BT1', 'BT2'],
                'AT2': ['AT0', 'AT1', 'AT2'],
                'AT0': ['AT0', 'AT1', 'AT2'],
                'AT1': ['AT0', 'AT1', 'AT2']
            })
        self.assertEqual(
            samples_to_subtract, {
                'BT1': 'BT0',
                'BT0': 'BT0',
                'BT2': 'BT0',
                'AT2': 'AT0',
                'AT0': 'AT0',
                'AT1': 'AT0'
            })
    def test_get_sample_individual_info(self):
        """get_sample_individual_info works
        """
        mapping_lines = """#SampleID\tindividual\ttimepoint_zero\ttimepoint
AT0\tA\t1\t0
AT1\tA\t0\t1
AT2\tA\t0\t2
BT0\tB\t1\t0
BT1\tB\t0\t1
BT2\tB\t0\t2
""".split('\n')
        mapping_data, header, comments = parse_mapping_file(mapping_lines)
        samples_from_subject, samples_to_subtract = \
            get_sample_individual_info(mapping_data, header, 'individual', \
                'timepoint_zero')
        self.assertEqual(samples_from_subject, {'BT1': ['BT0', 'BT1', 'BT2'], 'BT0': ['BT0', 'BT1', 'BT2'], 'BT2': ['BT0', 'BT1', 'BT2'], 'AT2': ['AT0', 'AT1', 'AT2'], 'AT0': ['AT0', 'AT1', 'AT2'], 'AT1': ['AT0', 'AT1', 'AT2']})
        self.assertEqual(samples_to_subtract, {'BT1': 'BT0', 'BT0': 'BT0', 'BT2': 'BT0', 'AT2': 'AT0', 'AT0': 'AT0', 'AT1': 'AT0'})
Exemplo n.º 4
0
def get_single_paired_T_values(OTU, mapping_data, header, individual_column,
                               timepoint_zero_column, otu_table, ignore_val):
    """gets the values for running a paired T test
   
    the timepoint_zero column should all be zero. The other column from that
    individual should be either negative or positive depending. If the
    value is the ignore value, it should be ignored. If the number of values
    do not pass the filter, the OTU should be ignored
    """
    timepoint_zero_vals = []
    after_treatment_vals = []
    #sample_to_subtract is the sample_names mapped to the timepoint zero sample
    #names
    samples_from_subject, sample_to_subtract = get_sample_individual_info(\
        mapping_data, header, individual_column, timepoint_zero_column)
    for sample in samples_from_subject:
        num_samples = len(samples_from_subject[sample])
        if num_samples != 2:
            raise ValueError(
                "Each individual/site must contain exactly 2 samples")
    timepoint_zeros = []
    for sample in sample_to_subtract:
        #if the sample is mapped to itself it is timepoint zero
        if sample == sample_to_subtract[sample]:
            timepoint_zeros.append(sample)
    for sample in timepoint_zeros:
        if sample in otu_table.SampleIds:
            timepoint_zero_val = otu_table.getValueByIds(OTU, sample)
        else:
            timepoint_zero_val = None
        if timepoint_zero_val != ignore_val:
            for sample2 in sample_to_subtract:
                if sample2 != sample and sample_to_subtract[sample2] == sample:
                    after_treatment_sample_id = sample2
                    if after_treatment_sample_id in otu_table.SampleIds:
                        if timepoint_zero_val != None:
                            after_treatment_vals.append(
                                otu_table.getValueByIds(
                                    OTU, after_treatment_sample_id))
                            #print "OTU: %s" % OTU
                            #print "sample id: %s" % after_treatment_sample_id
                            #print "value: %s" % otu_table.getValueByIds(OTU, after_treatment_sample_id)
                            timepoint_zero_vals.append(timepoint_zero_val)
    return timepoint_zero_vals, after_treatment_vals
Exemplo n.º 5
0
def get_single_paired_T_values(OTU, mapping_data, header, individual_column,
    timepoint_zero_column, otu_table, ignore_val):
    """gets the values for running a paired T test
   
    the timepoint_zero column should all be zero. The other column from that
    individual should be either negative or positive depending. If the
    value is the ignore value, it should be ignored. If the number of values
    do not pass the filter, the OTU should be ignored
    """
    timepoint_zero_vals = []
    after_treatment_vals = []
    #sample_to_subtract is the sample_names mapped to the timepoint zero sample
    #names
    samples_from_subject, sample_to_subtract = get_sample_individual_info(\
        mapping_data, header, individual_column, timepoint_zero_column)
    for sample in samples_from_subject:
        num_samples = len(samples_from_subject[sample])
        if num_samples != 2:
            raise ValueError("Each individual/site must contain exactly 2 samples")
    timepoint_zeros = []
    for sample in sample_to_subtract:
        #if the sample is mapped to itself it is timepoint zero
        if sample == sample_to_subtract[sample]:
            timepoint_zeros.append(sample)
    for sample in timepoint_zeros:
        if sample in otu_table.SampleIds:
            timepoint_zero_val = otu_table.getValueByIds(OTU, sample)
        else:
            timepoint_zero_val = None
        if timepoint_zero_val != ignore_val:
            for sample2 in sample_to_subtract:
                if sample2 != sample and sample_to_subtract[sample2] == sample:
                    after_treatment_sample_id = sample2
                    if after_treatment_sample_id in otu_table.SampleIds:
                        if timepoint_zero_val != None:
                            after_treatment_vals.append(
                                    otu_table.getValueByIds(
                                        OTU, after_treatment_sample_id))
                            #print "OTU: %s" % OTU
                            #print "sample id: %s" % after_treatment_sample_id
                            #print "value: %s" % otu_table.getValueByIds(OTU, after_treatment_sample_id)
                            timepoint_zero_vals.append(timepoint_zero_val)
    return timepoint_zero_vals, after_treatment_vals
Exemplo n.º 6
0
    def test_make_new_otu_counts(self):
        """make_new_otu_counts works
        """
        mapping_lines = """#SampleID\tindividual\ttimepoint_zero\ttimepoint
AT0\tA\t1\t0
AT1\tA\t0\t1
AT2\tA\t0\t2
BT0\tB\t1\t0
BT1\tB\t0\t1
BT2\tB\t0\t2
""".split('\n')
        mapping_data, header, comments = parse_mapping_file(mapping_lines)
        samples_from_subject, sample_to_subtract = \
            get_sample_individual_info(mapping_data, header, 'individual',
                                       'timepoint_zero')
        otu_table_str = """{"rows": [{"id": "0", "metadata": null}, {"id": "1",
        "metadata": null}, {"id": "2", "metadata": null}, {"id": "3",
        "metadata": null}], "format": "Biological Observation Matrix v0.9",
        "data": [[0, 0, 0.5], [0, 1, 0.29999999999999999], [0, 2, 99.0],
        [0, 3, 0.20000000000000001], [1, 2, 99.0], [1, 4, 0.40000000000000002],
        [1, 5, 0.5], [1, 6, 0.59999999999999998], [2, 0, 0.10000000000000001],
        [2, 1, 0.40000000000000002], [2, 2, 99.0], [2, 3, 0.69999999999999996],
        [2, 4, 0.5], [2, 5, 0.59999999999999998], [2, 6, 0.80000000000000004],
        [3, 1, 0.10000000000000001], [3, 2, 99.0],
        [3, 4, 0.40000000000000002]], "columns": [{"id": "AT0", "metadata":
        null}, {"id": "AT1", "metadata": null}, {"id": "S1", "metadata": null},
        {"id": "AT2", "metadata": null}, {"id": "BT0", "metadata": null},
        {"id": "BT1", "metadata": null}, {"id": "BT2", "metadata": null}],
        "generated_by": "QIIME 1.4.0-dev, svn revision 2570", "matrix_type":
        "sparse", "shape": [4, 7], "format_url":
        "http://www.qiime.org/svn_documentation/documentation/biom_format.html",
        "date": "2011-12-21T21:35:19.499263", "type": "OTU table", "id": null,
        "matrix_element_type": "float"}"""
        otu_table = parse_biom_table_str(otu_table_str)

        converted_otu_table_object = make_new_otu_counts(otu_table,
                                                         sample_to_subtract, samples_from_subject)
        expected_otu_table_str = """{"rows": [{"id": "0", "metadata": null},
        {"id": "1", "metadata": null}, {"id": "2", "metadata": null}, {"id":
        "3", "metadata": null}], "format":
        "Biological Observation Matrix 0.9.0-dev", "data": [[0, 1,
        -0.20000000000000001], [0, 2, -0.29999999999999999],
        [0, 3, 999999999.0], [0, 4, 999999999.0], [0, 5, 999999999.0], [1, 0,
        999999999.0], [1, 1, 999999999.0], [1, 2, 999999999.0], [1, 4,
        0.10000000000000001], [1, 5, 0.20000000000000001], [2, 1,
        0.29999999999999999], [2, 2, 0.59999999999999998], [2, 4,
        0.10000000000000001], [2, 5, 0.29999999999999999], [3, 1,
        0.10000000000000001], [3, 4, -0.40000000000000002], [3, 5,
        -0.40000000000000002]], "columns": [{"id": "AT0", "metadata": null},
        {"id": "AT1", "metadata": null}, {"id": "AT2", "metadata": null},
        {"id": "BT0", "metadata": null}, {"id": "BT1", "metadata": null},
        {"id": "BT2", "metadata": null}], "generated_by":
        "QIIME 1.4.0-dev, svn revision 2570", "matrix_type": "sparse",
        "shape": [4, 6], "format_url":
        "http://biom-format.org",
        "date": "2011-12-21T21:43:06.809380", "type": "OTU table", "id": null,
        "matrix_element_type": "float"}"""
        expected_otu_table_object = parse_biom_table_str(
            expected_otu_table_str)
        self.assertEqual(converted_otu_table_object.ObservationIds,
                         expected_otu_table_object.ObservationIds)
        self.assertEqual(converted_otu_table_object.SampleIds,
                         expected_otu_table_object.SampleIds)
        self.assertEqual(converted_otu_table_object.SampleMetadata,
                         expected_otu_table_object.SampleMetadata)
        self.assertEqual(converted_otu_table_object.ObservationMetadata,
                         expected_otu_table_object.ObservationMetadata)
        self.assertFloatEqual(sorted(converted_otu_table_object._data.items()),
                              sorted(expected_otu_table_object._data.items()))