Exemple #1
0
    def test_get_log_transform(self):
        orig_data = array([[0, 1, 2], [1000, 0, 0]])

        orig_otu_table = table_factory(orig_data,
                                       ['Sample1', 'Sample2', 'Sample3'],
                                       ['OTU1', 'OTU2'], [None, None, None], [{
                                           "taxonomy": ["Bacteria"]
                                       }, {
                                           "taxonomy": ["Archaea"]
                                       }])

        exp_data = array([[0, 0.69314718, 1.38629436], [7.60090246, 0, 0]])
        exp_otu_table = table_factory(exp_data,
                                      ['Sample1', 'Sample2', 'Sample3'],
                                      ['OTU1', 'OTU2'], [None, None, None], [{
                                          "taxonomy": ["Bacteria"]
                                      }, {
                                          "taxonomy": ["Archaea"]
                                      }])

        log_otu_table = get_log_transform(orig_otu_table, eps=None)

        # comparing directly log_otu_table against exp_otu_table doesn't work,
        #  needs to be modified in the otu table object
        self.assertFloatEqual(list(log_otu_table.iterSampleData()),
                              list(exp_otu_table.iterSampleData()))
    def test_get_log_transform(self):
        data = array([[0,1,2],[1000,0,0]])
        logdata = get_log_transform(data,eps=None)

        # set zeros to 1/2s
        exp = log(array([[.5,1,2],[1000,.5,.5]]))
        # translate to 0
        exp -= exp.min()

        self.assertFloatEqual(logdata, exp)
    def test_get_log_transform(self):
        #data = array([[0,1,2],[1000,0,0]])
        #logdata = get_log_transform(data,eps=None)

        # set zeros to 1/2s
        #exp = log(array([[.5,1,2],[1000,.5,.5]]))
        # translate to 0
        #exp -= exp.min()

        #self.assertFloatEqual(logdata, exp)

        orig_data = array([[0, 1, 2], [1000, 0, 0]])
        #{(0,1):1.0, (0,2):2,(1,0):1000.0}

        orig_otu_table = table_factory(orig_data,
                                       ['Sample1', 'Sample2', 'Sample3'],
                                       ['OTU1', 'OTU2'], [None, None, None], [{
                                           "taxonomy": ["Bacteria"]
                                       }, {
                                           "taxonomy": ["Archaea"]
                                       }])

        exp_data = array([[0, 0.69314718, 1.38629436], [7.60090246, 0, 0]])
        #{(0,1):0.69314718, (0,2):1.38629436,(1,0):7.60090246}
        exp_otu_table = table_factory(exp_data,
                                      ['Sample1', 'Sample2', 'Sample3'],
                                      ['OTU1', 'OTU2'], [None, None, None], [{
                                          "taxonomy": ["Bacteria"]
                                      }, {
                                          "taxonomy": ["Archaea"]
                                      }])

        log_otu_table = get_log_transform(orig_otu_table, eps=None)

        # comparing directly log_otu_table against exp_otu_table doesn't work,
        #  needs to be modified in the otu table object
        self.assertFloatEqual(log_otu_table._data.items(),
                              exp_otu_table._data.items())
    def test_get_log_transform(self):
        orig_data = array([[0, 1, 2], [1000, 0, 0]])

        orig_otu_table = table_factory(orig_data,
                                       ['Sample1', 'Sample2', 'Sample3'],
                                       ['OTU1', 'OTU2'],
                                       [None, None, None],
                                       [{"taxonomy": ["Bacteria"]},
                                        {"taxonomy": ["Archaea"]}])

        exp_data = array([[0, 0.69314718, 1.38629436], [7.60090246, 0, 0]])
        exp_otu_table = table_factory(exp_data,
                                      ['Sample1', 'Sample2', 'Sample3'],
                                      ['OTU1', 'OTU2'],
                                      [None, None, None],
                                      [{"taxonomy": ["Bacteria"]},
                                       {"taxonomy": ["Archaea"]}])

        log_otu_table = get_log_transform(orig_otu_table, eps=None)

        # comparing directly log_otu_table against exp_otu_table doesn't work,
        #  needs to be modified in the otu table object
        assert_almost_equal(list(log_otu_table.iterSampleData()),
                              list(exp_otu_table.iterSampleData()))
    def test_get_log_transform(self):
        #data = array([[0,1,2],[1000,0,0]])
        #logdata = get_log_transform(data,eps=None)

        # set zeros to 1/2s
        #exp = log(array([[.5,1,2],[1000,.5,.5]]))
        # translate to 0
        #exp -= exp.min()

        #self.assertFloatEqual(logdata, exp)

        orig_data = array([[0,1,2],[1000,0,0]])
        #{(0,1):1.0, (0,2):2,(1,0):1000.0}

        orig_otu_table = table_factory(orig_data,
                                        ['Sample1', 'Sample2', 'Sample3'],
                                        ['OTU1', 'OTU2'],
                                        [None, None, None],
                                        [{"taxonomy": ["Bacteria"]},
                                         {"taxonomy": ["Archaea"]}])

        exp_data = array([[0,0.69314718,1.38629436],[7.60090246,0,0]])
        #{(0,1):0.69314718, (0,2):1.38629436,(1,0):7.60090246}
        exp_otu_table = table_factory(exp_data,
                                       ['Sample1', 'Sample2', 'Sample3'],
                                       ['OTU1', 'OTU2'],
                                       [None, None, None],
                                       [{"taxonomy": ["Bacteria"]},
                                        {"taxonomy": ["Archaea"]}])

        log_otu_table = get_log_transform(orig_otu_table, eps = None)

        # comparing directly log_otu_table against exp_otu_table doesn't work,
        #  needs to be modified in the otu table object
        self.assertFloatEqual(list(log_otu_table.iterSampleData()),
                              list(exp_otu_table.iterSampleData()))
def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)

    data = {}

    #Open and get coord data
    #data['otu_counts'] = list(get_otu_counts(opts.otu_table_fp, data))
    otu_table = get_otu_counts(opts.otu_table_fp)
    # determine whether fractional values are present in OTU table
    num_otu_hits = opts.num_otu_hits
    if opts.log_transform:
        if not opts.log_eps is None and opts.log_eps <= 0:
            print "Parameter 'log_eps' must be positive. Value was", opts.log_eps
            exit(1)
        #data['otu_counts'][2] = get_log_transform(data['otu_counts'][2], opts.log_eps)
        otu_table = get_log_transform(otu_table, opts.log_eps)
        num_otu_hits = 0

    fractional_values = False
    max_val = -1
    for val in otu_table.iterObservationData():
        max_val = maximum(max_val, val.max())

    # the data cannot be of mixed types: if one is float, all are float
    fractional_values = (max_val.dtype.name == 'float32'
                         or max_val.dtype.name == 'float64')

    if fractional_values and max_val <= 1:
        if num_otu_hits > 0:
            print "Warning: OTU table appears to be using relative abundances",\
                    "and num_otu_hits was set to %d. Setting num_otu_hits to 0."\
                    %(num_otu_hits)
            num_otu_hits = 0

    filepath = opts.otu_table_fp
    filename = filepath.strip().split('/')[-1].split('.')[0]

    dir_path = opts.output_dir
    create_dir(dir_path)

    js_dir_path = os.path.join(dir_path, 'js')
    create_dir(js_dir_path)

    qiime_dir = get_qiime_project_dir()

    js_path = os.path.join(qiime_dir, 'qiime/support_files/js')

    shutil.copyfile(os.path.join(js_path, 'overlib.js'),
                    os.path.join(js_dir_path, 'overlib.js'))
    shutil.copyfile(os.path.join(js_path, 'otu_count_display.js'),
                    os.path.join(js_dir_path, 'otu_count_display.js'))
    shutil.copyfile(os.path.join(js_path, 'jquery.js'),
                    os.path.join(js_dir_path, 'jquery.js'))
    shutil.copyfile(os.path.join(js_path, 'jquery.tablednd_0_5.js'),
                    os.path.join(js_dir_path, 'jquery.tablednd_0_5.js'))

    # load tree for sorting OTUs
    ordered_otu_names = None
    if not opts.tree is None:
        try:
            f = open(opts.tree, 'U')
        except (TypeError, IOError):
            raise TreeMissingError, \
                "Couldn't read tree file at path: %s" % tree_source
        tree = parse_newick(f, PhyloNode)
        f.close()
        ordered_otu_names = [tip.Name for tip in tree.iterTips()]
    ordered_sample_names = None

    # load tree for sorting Samples
    if not opts.sample_tree is None:
        try:
            f = open(opts.sample_tree, 'U')
        except (TypeError, IOError):
            raise TreeMissingError, \
                "Couldn't read tree file at path: %s" % tree_source
        tree = parse_newick(f, PhyloNode)
        f.close()
        ordered_sample_names = [tip.Name for tip in tree.iterTips()]
    # if there's no sample tree, load sample map for sorting samples
    elif not opts.map_fname is None:
        lines = open(opts.map_fname, 'U').readlines()
        map = parse_mapping_file(lines)[0]
        ordered_sample_names = [row[0] for row in map]

    #data['otu_order'] = ordered_otu_names
    #data['sample_order'] = ordered_sample_names

    try:
        action = generate_heatmap_plots
    except NameError:
        action = None

    #Place this outside try/except so we don't mask NameError in action
    if action:
        action(num_otu_hits, otu_table, ordered_otu_names,
               ordered_sample_names, dir_path, js_dir_path, filename,
               fractional_values)
def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)
      
    data = {}

    #Open and get coord data
    #data['otu_counts'] = list(get_otu_counts(opts.otu_table_fp, data))
    otu_table = get_otu_counts(opts.otu_table_fp)
    # determine whether fractional values are present in OTU table
    num_otu_hits=opts.num_otu_hits
    if opts.log_transform:
        if not opts.log_eps is None and opts.log_eps <= 0:
            print "Parameter 'log_eps' must be positive. Value was", opts.log_eps
            exit(1)
        #data['otu_counts'][2] = get_log_transform(data['otu_counts'][2], opts.log_eps)
        otu_table = get_log_transform(otu_table, opts.log_eps)
        num_otu_hits = 0

    fractional_values = False
    max_val = -1
    for val in otu_table.iterObservationData():
        max_val = maximum(max_val, val.max())

    # the data cannot be of mixed types: if one is float, all are float
    fractional_values = (max_val.dtype.name == 'float32' or max_val.dtype.name == 'float64')

    if fractional_values and max_val <= 1:
        if num_otu_hits > 0:
            print "Warning: OTU table appears to be using relative abundances",\
                    "and num_otu_hits was set to %d. Setting num_otu_hits to 0."\
                    %(num_otu_hits)
            num_otu_hits = 0

    filepath=opts.otu_table_fp
    filename=filepath.strip().split('/')[-1].split('.')[0]
    
    dir_path = opts.output_dir
    create_dir(dir_path)

    js_dir_path = os.path.join(dir_path,'js')
    create_dir(js_dir_path)
    

    qiime_dir=get_qiime_project_dir()

    js_path=os.path.join(qiime_dir,'qiime/support_files/js')

    shutil.copyfile(os.path.join(js_path,'overlib.js'), os.path.join(js_dir_path,'overlib.js'))
    shutil.copyfile(os.path.join(js_path,'otu_count_display.js'), os.path.join(js_dir_path,'otu_count_display.js'))
    shutil.copyfile(os.path.join(js_path,'jquery.js'), os.path.join(js_dir_path,'jquery.js'))
    shutil.copyfile(os.path.join(js_path,'jquery.tablednd_0_5.js'), os.path.join(js_dir_path,'jquery.tablednd_0_5.js'))

    # load tree for sorting OTUs
    ordered_otu_names = None
    if not opts.tree is None:
        try:
            f = open(opts.tree, 'U')
        except (TypeError, IOError):
            raise TreeMissingError, \
                "Couldn't read tree file at path: %s" % tree_source
        tree = parse_newick(f, PhyloNode) 
        f.close()
        ordered_otu_names = [tip.Name for tip in tree.iterTips()]
    ordered_sample_names = None
    
    # load tree for sorting Samples
    if not opts.sample_tree is None:
        try:
            f = open(opts.sample_tree, 'U')
        except (TypeError, IOError):
            raise TreeMissingError, \
                "Couldn't read tree file at path: %s" % tree_source
        tree = parse_newick(f, PhyloNode) 
        f.close()
        ordered_sample_names = [tip.Name for tip in tree.iterTips()]
    # if there's no sample tree, load sample map for sorting samples
    elif not opts.map_fname is None:
        lines = open(opts.map_fname,'U').readlines()
        map = parse_mapping_file(lines)[0]
        ordered_sample_names = [row[0] for row in map]

    #data['otu_order'] = ordered_otu_names
    #data['sample_order'] = ordered_sample_names
    
    try:
        action = generate_heatmap_plots
    except NameError:
        action = None
        
    #Place this outside try/except so we don't mask NameError in action
    if action:
        action(num_otu_hits,otu_table, ordered_otu_names, ordered_sample_names,
               dir_path,js_dir_path,filename, fractional_values)
def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)
      
    data = {}

    #Open and get coord data
    data['otu_counts'] = list(get_otu_counts(opts.otu_table_fp, data))
    # determine whether fractional values are present in OTU table

    if opts.log_transform:
        if not opts.log_eps is None and opts.log_eps <= 0:
            print "Parameter 'log_eps' must be positive. Value was", opts.log_eps
            exit(1)
        data['otu_counts'][2] = get_log_transform(data['otu_counts'][2], opts.log_eps)
        opts.num_otu_hits = 0
        
    # test: if using relative abundances, and opts.num_otu_hits > 0
    # print warning and set to 0
    fractional_values = ((data['otu_counts'][2] > 0) & \
                         (data['otu_counts'][2] < 1)).any()
    if fractional_values and (data['otu_counts'][2]).max() <= 1:
        if opts.num_otu_hits > 0:
            print "Warning: OTU table appears to be using relative abundances",\
                    "and num_otu_hits was set to %d. Setting num_otu_hits to 0."\
                    %(opts.num_otu_hits)
            opts.num_otu_hits = 0

    filepath=opts.otu_table_fp
    filename=filepath.strip().split('/')[-1].split('.')[0]

    if opts.output_dir:
        if os.path.exists(opts.output_dir):
            dir_path=opts.output_dir
        else:
            try:
                os.mkdir(opts.output_dir)
                dir_path=opts.output_dir
            except OSError:
                pass
    else:
        dir_path='./'

    js_dir_path = os.path.join(dir_path,'js')
    
    try:
        os.mkdir(js_dir_path)
    except OSError:
        pass

    qiime_dir=get_qiime_project_dir()

    js_path=os.path.join(qiime_dir,'qiime/support_files/js')

    shutil.copyfile(os.path.join(js_path,'overlib.js'), os.path.join(js_dir_path,'overlib.js'))
    shutil.copyfile(os.path.join(js_path,'otu_count_display.js'), os.path.join(js_dir_path,'otu_count_display.js'))
    shutil.copyfile(os.path.join(js_path,'jquery.js'), os.path.join(js_dir_path,'jquery.js'))
    shutil.copyfile(os.path.join(js_path,'jquery.tablednd_0_5.js'), os.path.join(js_dir_path,'jquery.tablednd_0_5.js'))

    # load tree for sorting OTUs
    ordered_otu_names = None
    if not opts.tree is None:
        try:
            f = open(opts.tree, 'U')
        except (TypeError, IOError):
            raise TreeMissingError, \
                "Couldn't read tree file at path: %s" % tree_source
        tree = parse_newick(f, PhyloNode) 
        f.close()
        ordered_otu_names = [tip.Name for tip in tree.iterTips()]
    ordered_sample_names = None
    
    # load tree for sorting Samples
    if not opts.sample_tree is None:
        try:
            f = open(opts.sample_tree, 'U')
        except (TypeError, IOError):
            raise TreeMissingError, \
                "Couldn't read tree file at path: %s" % tree_source
        tree = parse_newick(f, PhyloNode) 
        f.close()
        ordered_sample_names = [tip.Name for tip in tree.iterTips()]
    # if there's no sample tree, load sample map for sorting samples
    elif not opts.map_fname is None:
        lines = open(opts.map_fname,'U').readlines()
        map = parse_mapping_file(lines)[0]
        ordered_sample_names = [row[0] for row in map]

    data['otu_order'] = ordered_otu_names
    data['sample_order'] = ordered_sample_names
    
    try:
        action = generate_heatmap_plots
    except NameError:
        action = None
    #Place this outside try/except so we don't mask NameError in action
    if action:
        action(opts,data, dir_path,js_dir_path,filename, fractional_values)