示例#1
0
def load_tree_files(tree_dir):
    """Load trees from filepaths

    checks if  filenames indicate that trees are from different
    distance methods.  If so, warns user.
    loads trees into phylonode objects
    returns [trees]
    raises a RuntimeError if no  trees are loaded
    """
    tree_file_names = os.listdir(tree_dir)
    # ignore invisible files like .DS_Store
    tree_file_names = [
        fname for fname in tree_file_names if not fname.startswith('.')
    ]

    # try to warn user if using multiple types of trees {
    try:
        base_names = []
        for fname in tree_file_names:
            base_names.append(parse_rarefaction_fname(fname)[0])
    except ValueError:
        pass
    else:
        if len(set(base_names)) > 1:
            warnstr = """
warning: trees are named differently, please be sure you're not
comparing trees generated in different manners, unless you're quite sure
that's what you intend to do.  types: """ + str(set(base_names)) + """
continuing anyway..."""
            warn(warnstr)
    # }
    trees = []
    for fname in tree_file_names:
        try:
            f = open(os.path.join(tree_dir, fname), 'U')
            tree = TreeNode.from_newick(f)
            tree.filepath = fname
            trees.append(tree)
            f.close()
        except IOError as err:
            sys.stderr.write('error loading tree ' + fname + '\n')
            exit(1)
    if len(trees) == 0:
        raise RuntimeError('Error: no trees loaded' +
                           ', check that tree directory has has valid trees')
    return trees
示例#2
0
def load_tree_files(tree_dir):
    """Load trees from filepaths

    checks if  filenames indicate that trees are from different
    distance methods.  If so, warns user.
    loads trees into phylonode objects
    returns [trees]
    raises a RuntimeError if no  trees are loaded
    """
    tree_file_names = os.listdir(tree_dir)
    # ignore invisible files like .DS_Store
    tree_file_names = [fname for fname in tree_file_names if not
                       fname.startswith('.')]

    # try to warn user if using multiple types of trees {
    try:
        base_names = []
        for fname in tree_file_names:
            base_names.append(parse_rarefaction_fname(fname)[0])
    except ValueError:
        pass
    else:
        if len(set(base_names)) > 1:
            warnstr = """
warning: trees are named differently, please be sure you're not
comparing trees generated in different manners, unless you're quite sure
that's what you intend to do.  types: """ + str(set(base_names)) + """
continuing anyway..."""
            warn(warnstr)
    # }
    trees = []
    for fname in tree_file_names:
        try:
            f = open(os.path.join(tree_dir, fname), 'U')
            tree = TreeNode.from_newick(f)
            tree.filepath = fname
            trees.append(tree)
            f.close()
        except IOError as err:
            sys.stderr.write('error loading tree ' + fname + '\n')
            exit(1)
    if len(trees) == 0:
        raise RuntimeError('Error: no trees loaded' +
                           ', check that tree directory has has valid trees')
    return trees
示例#3
0
def make_output_row(f_metrics, metric, f_samples, f_data, fname, num_cols, all_samples):
    f_col = f_metrics.index(metric)

    # first 3 cols are fname, seqs/sample, iteration
    try:
        base, seqs, iter, ext = parse_rarefaction_fname(fname)
    except:
        seqs, iter = "n/a", "n/a"
    output_row = [fname] + ["n/a"] * num_cols
    for f_row, sample in enumerate(f_samples):
        try:
            output_row[all_samples.index(sample) + 1] = str(f_data[f_row, f_col])
        except IndexError:
            print("warning, didn't find sample in example file." + "exiting", sample, fname, metric)
            raise  # re-raise error

    output_row.insert(1, seqs)
    output_row.insert(2, iter)
    return output_row
示例#4
0
def make_output_row(f_metrics, metric, f_samples, f_data, fname, num_cols, all_samples):
    f_col = f_metrics.index(metric)

    # first 3 cols are fname, seqs/sample, iteration
    try:
        base, seqs, iter, ext = parse_rarefaction_fname(fname)
    except:
        seqs, iter = 'n/a', 'n/a'
    output_row = [fname] + ['n/a']*num_cols
    for f_row, sample in enumerate(f_samples):
        try:
            output_row[all_samples.index(sample)+1] = \
                str(f_data[f_row,f_col])
        except IndexError:
            print("warning, didn't find sample in example file."+\
             "exiting", sample, fname, metric)
            raise # re-raise error
            

    output_row.insert(1, seqs)
    output_row.insert(2, iter)
    return output_row
示例#5
0
 def test_parse_rarefaction_fname(self):
     """ parse_rarefaction_fname should return base, seqs/sam, iters, etc."""
     fname = "alpha_rarefaction_900_3.txt"
     base, seqs, iter, ext = parse_rarefaction_fname(fname)
     self.assertEqual((base, seqs, iter, ext),
         ("alpha_rarefaction", 900, 3, ".txt"))
示例#6
0
 def test_parse_rarefaction_fname(self):
     """ parse_rarefaction_fname should return base, seqs/sam, iters, etc."""
     fname = "alpha_rarefaction_900_3.txt"
     base, seqs, iter, ext = parse_rarefaction_fname(fname)
     self.assertEqual((base, seqs, iter, ext),
                      ("alpha_rarefaction", 900, 3, ".txt"))