def main(newick: str, output_newick: str, samples: Iterable[Sample]):
    sample_name_by_workflow_run_id = {
        str(s["workflow_run_id"]): s["sample_name"]
        for s in samples
    }
    with open(newick) as i, open(output_newick, "w") as o:
        tree = next(NewickIO.parse(i))
        for node in tree.find_clades(order="level"):
            node.name = sample_name_by_workflow_run_id.get(
                node.name, node.name)
        NewickIO.write([tree], o)
Ejemplo n.º 2
0
Archivo: phylo.py Proyecto: xzy3/QuaSim
def read_fasta_or_newick_and_return_tree(path, nwk_path=None, patt=None):
    global NUM_OF_VIRIONS
    if any(path.name.endswith(x) for x in FASTA_EXTENSIONS):
        seqs = AlignIO.read(path, FASTA)
        seqs._records = [x for x in seqs if get_count(x, patt) > MIN_COUNT]
        NUM_OF_VIRIONS = int(sum(get_count(x, patt) for x in seqs))

        if len(seqs) <= 2: return None
        tree = build_phylogenetic_tree(seqs)
        if nwk_path is not None and tree is not None:
            NewickIO.write([tree], nwk_path)
    elif any(path.name.endswith(x) for x in NEWICK_EXTENSIONS):
        tree = NewickIO.parse(path).next()

    # Root the tree if necessary
    if not tree.rooted:
        tree.root_at_midpoint()

    return tree
Ejemplo n.º 3
0
def main():
    prog = sys.argv[0]
    description = ('Parse newick tree an perform action on'
                   'each non-root node')
    parser = argparse.ArgumentParser(prog=prog, description=description)
    parser.add_argument('infile', nargs='?', type=argparse.FileType(),
                        help='a Newick treefile')
    parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
                        help='changed Newick outfile')
    parser.add_argument('--cutoff', dest='cutoff', nargs='?', type=int, default=75,
                        help='value at or beneath which no inner node'
                             'confidences are snown any more')
    options = parser.parse_args()

    infile = options.infile or sys.stdin
    outfile = options.outfile or sys.stdout
    cutoff = options.cutoff
    newick = NewickCutoff(infile, outfile, cutoff)
    trees = newick.readtrees()
    trees = newick.relabeltree(trees)

    NewickIO.write(trees, outfile)
                        code)
    if len(mappings) <= 0:
        log.warning("empty mappings file")
    return mappings


def printtrees(trees):
    for tree in trees:
        Phylo.draw_ascii(tree)


stdout_handler = logging.StreamHandler(sys.stderr)
handlers = [stdout_handler]
logging.basicConfig(level=logging.INFO,
                    format='[%(levelname)s - %(message)s]',
                    handlers=handlers)
log = logging.getLogger('LOGGER_NAME')

mappingfile = sys.argv[2]
mappings = readmappings(mappingfile)
mappedstrings = {}

treefile = sys.argv[1]
trees = readtrees(treefile)

trees = relabeltree(trees)
reportMappings(mappedstrings)
#printtees(trees)

NewickIO.write(trees, sys.stdout)