def build_nj_phylip(alignment, outfile, outgroup, work_dir="."):
    """
    build neighbor joining tree of DNA seqs with PHYLIP in EMBOSS

    PHYLIP manual
    http://evolution.genetics.washington.edu/phylip/doc/
    """

    phy_file = op.join(work_dir, "work", "aln.phy")
    try:
        AlignIO.write(alignment, file(phy_file, "w"), "phylip")
    except ValueError:
        print("Repeated seq name, possibly due to truncation. NJ tree not built.", file=sys.stderr)
        return None

    seqboot_out = phy_file.rsplit(".",1)[0] + ".fseqboot"
    seqboot_cl = FSeqBootCommandline(FPHYLIP_BIN("fseqboot"), \
        sequence=phy_file, outfile=seqboot_out, \
        seqtype="d", reps=100, seed=12345)
    stdout, stderr = seqboot_cl()
    logging.debug("Resampling alignment: %s" % seqboot_cl)

    dnadist_out = phy_file.rsplit(".",1)[0] + ".fdnadist"
    dnadist_cl = FDNADistCommandline(FPHYLIP_BIN("fdnadist"), \
        sequence=seqboot_out, outfile=dnadist_out, method="f")
    stdout, stderr = dnadist_cl()
    logging.debug\
        ("Calculating distance for bootstrapped alignments: %s" % dnadist_cl)

    neighbor_out = phy_file.rsplit(".",1)[0] + ".njtree"
    e = phy_file.rsplit(".",1)[0] + ".fneighbor"
    neighbor_cl = FNeighborCommandline(FPHYLIP_BIN("fneighbor"), \
        datafile=dnadist_out, outfile=e, outtreefile=neighbor_out)
    stdout, stderr = neighbor_cl()
    logging.debug("Building Neighbor Joining tree: %s" % neighbor_cl)

    consense_out = phy_file.rsplit(".",1)[0] + ".consensustree.nodesupport"
    e = phy_file.rsplit(".",1)[0] + ".fconsense"
    consense_cl = FConsenseCommandline(FPHYLIP_BIN("fconsense"), \
        intreefile=neighbor_out, outfile=e, outtreefile=consense_out)
    stdout, stderr = consense_cl()
    logging.debug("Building consensus tree: %s" % consense_cl)

    # distance without bootstrapping
    dnadist_out0 = phy_file.rsplit(".",1)[0] + ".fdnadist0"
    dnadist_cl0 = FDNADistCommandline(FPHYLIP_BIN("fdnadist"), \
        sequence=phy_file, outfile=dnadist_out0, method="f")
    stdout, stderr = dnadist_cl0()
    logging.debug\
        ("Calculating distance for original alignment: %s" % dnadist_cl0)

    # infer branch length on consensus tree
    consensustree1 = phy_file.rsplit(".",1)[0] + ".consensustree.branchlength"
    run_ffitch(distfile=dnadist_out0, outtreefile=consensustree1, \
            intreefile=consense_out)

    # write final tree
    ct_s = Tree(consense_out)

    if outgroup:
        t1 = consensustree1 + ".rooted"
        t2 = smart_reroot(consensustree1, outgroup, t1)
        if t2 == t1:
            outfile = outfile.replace(".unrooted", "")
        ct_b = Tree(t2)
    else:
        ct_b = Tree(consensustree1)

    nodesupport = {}
    for node in ct_s.traverse("postorder"):
        node_children = tuple(sorted([f.name for f in node]))
        if len(node_children) > 1:
            nodesupport[node_children] = node.dist/100.

    for k,v in nodesupport.items():
        ct_b.get_common_ancestor(*k).support = v
    print(ct_b)
    ct_b.write(format=0, outfile=outfile)

    try:
        s = op.getsize(outfile)
    except OSError:
        s = 0
    if s:
        logging.debug("NJ tree printed to %s" % outfile)
        return outfile, phy_file
    else:
        logging.debug("Something was wrong. NJ tree was not built.")
        return None
def build_nj_phylip ( alignment , outfile , outgroup , work_dir = "." ) : phy_file = op . join ( work_dir , "work" , "aln.phy" ) try : AlignIO . write ( alignment , file ( phy_file , "w" ) , "phylip" ) except ValueError : print ( "Repeated seq name, possibly due to truncation. NJ tree not built." , file = sys . stderr ) return None seqboot_out = phy_file . rsplit ( "." , 1 ) [ 0 ] + ".fseqboot" seqboot_cl = FSeqBootCommandline ( FPHYLIP_BIN ( "fseqboot" ) , sequence = phy_file , outfile = seqboot_out , seqtype = "d" , reps = 100 , seed = 12345 ) stdout , stderr = seqboot_cl ( ) logging . debug ( "Resampling alignment: %s" % seqboot_cl ) dnadist_out = phy_file . rsplit ( "." , 1 ) [ 0 ] + ".fdnadist" dnadist_cl = FDNADistCommandline ( FPHYLIP_BIN ( "fdnadist" ) , sequence = seqboot_out , outfile = dnadist_out , method = "f" ) stdout , stderr = dnadist_cl ( ) logging . debug ( "Calculating distance for bootstrapped alignments: %s" % dnadist_cl ) neighbor_out = phy_file . rsplit ( "." , 1 ) [ 0 ] + ".njtree" e = phy_file . rsplit ( "." , 1 ) [ 0 ] + ".fneighbor" neighbor_cl = FNeighborCommandline ( FPHYLIP_BIN ( "fneighbor" ) , datafile = dnadist_out , outfile = e , outtreefile = neighbor_out ) stdout , stderr = neighbor_cl ( ) logging . debug ( "Building Neighbor Joining tree: %s" % neighbor_cl ) consense_out = phy_file . rsplit ( "." , 1 ) [ 0 ] + ".consensustree.nodesupport" e = phy_file . rsplit ( "." , 1 ) [ 0 ] + ".fconsense" consense_cl = FConsenseCommandline ( FPHYLIP_BIN ( "fconsense" ) , intreefile = neighbor_out , outfile = e , outtreefile = consense_out ) stdout , stderr = consense_cl ( ) logging . debug ( "Building consensus tree: %s" % consense_cl ) dnadist_out0 = phy_file . rsplit ( "." , 1 ) [ 0 ] + ".fdnadist0" dnadist_cl0 = FDNADistCommandline ( FPHYLIP_BIN ( "fdnadist" ) , sequence = phy_file , outfile = dnadist_out0 , method = "f" ) stdout , stderr = dnadist_cl0 ( ) logging . debug ( "Calculating distance for original alignment: %s" % dnadist_cl0 ) consensustree1 = phy_file . rsplit ( "." , 1 ) [ 0 ] + ".consensustree.branchlength" run_ffitch ( distfile = dnadist_out0 , outtreefile = consensustree1 , intreefile = consense_out ) ct_s = Tree ( consense_out ) if outgroup : t1 = consensustree1 + ".rooted" t2 = smart_reroot ( consensustree1 , outgroup , t1 ) if t2 == t1 : outfile = outfile . replace ( ".unrooted" , "" ) ct_b = Tree ( t2 ) else : ct_b = Tree ( consensustree1 ) nodesupport = { } for node in ct_s . traverse ( "postorder" ) : node_children = tuple ( sorted ( [ f . name for f in node ] ) ) if len ( node_children ) > 1 : nodesupport [ node_children ] = node . dist / 100. for k , v in nodesupport . items ( ) : ct_b . get_common_ancestor ( * k ) . support = v print ( ct_b ) ct_b . write ( format = 0 , outfile = outfile ) try : s = op . getsize ( outfile ) except OSError : s = 0 if s : logging . debug ( "NJ tree printed to %s" % outfile ) return outfile , phy_file else : logging . debug ( "Something was wrong. NJ tree was not built." ) return None
build neighbor joining tree of DNA seqs with PHYLIP in EMBOSS

    PHYLIP manual
    http://evolution.genetics.washington.edu/phylip/doc/