Пример #1
0
def write_dlcoal_recon2(filename, coal_tree, extra,
                       exts={"coal_tree": ".coal.tree",
                             "coal_recon": ".coal.recon",
                             "locus_tree": ".locus.tree",
                             "locus_recon": ".locus.recon",
                             "locus_events": ".locus.events",
                             "daughters": ".daughters"
                             },
                       filenames={}):
    """Writes a reconciled gene tree to files"""

    # coal
    coal_tree.write(filenames.get("coal_tree", filename + exts["coal_tree"]),
                    rootData=True)
    phylo.write_recon(
        filenames.get("coal_recon", filename + exts["coal_recon"]),
        extra["coal_recon"])

    # locus
    extra["locus_tree"].write(
        filenames.get("locus_tree", filename + exts["locus_tree"]),
        rootData=True)
    phylo.write_recon(
        filenames.get("locus_recon", filename + exts["locus_recon"]),
        extra["locus_recon"])
    phylo.write_events(
        filenames.get("locus_events", filename + exts["locus_events"]),
        extra["locus_events"])

    util.write_list(
        filenames.get("daughters", filename + exts["daughters"]),
        [x.name for x in extra["daughters"]])
Пример #2
0
def write_dlcoal_recon(filename, coal_tree, extra,
                       exts={"coal_tree": ".coal.tree",
                             "coal_recon": ".coal.recon",
                             "locus_tree": ".locus.tree",
                             "locus_recon": ".locus.recon",
                             "daughters": ".daughters"
                             },
                       filenames={}):
    """Writes a reconciled gene tree to files"""

    # coal
    coal_tree.write(filenames.get("coal_tree", filename + exts["coal_tree"]),
                    rootData=True)
    phylo.write_recon_events(
        filenames.get("coal_recon", filename + exts["coal_recon"]),
        extra["coal_recon"], noevent="none")

    # locus
    extra["locus_tree"].write(
        filenames.get("locus_tree", filename + exts["locus_tree"]),
        rootData=True)
    phylo.write_recon_events(
        filenames.get("locus_recon", filename + exts["locus_recon"]),
        extra["locus_recon"], extra["locus_events"])

    util.write_list(
        filenames.get("daughters", filename + exts["daughters"]),
        [x.name for x in extra["daughters"]])
Пример #3
0
    def write(self, filename, coal_tree,
              exts={"coal_tree": ".coal.tree",
                    "coal_recon": ".coal.recon",
                    "locus_tree": ".locus.tree",
                    "locus_recon": ".locus.recon",
                    "daughters": ".daughters"
                    },
              filenames={}):
        """Writes a reconciled gene tree to files"""

        assert coal_tree and self.coal_recon and \
               self.locus_tree and self.locus_recon and self.locus_events and \
               (self.daughters is not None)

        # coal
        coal_tree.write(
            filenames.get("coal_tree", filename + exts["coal_tree"]),
            rootData=True)
        phylo.write_recon_events(
            filenames.get("coal_recon", filename + exts["coal_recon"]),
            self.coal_recon, noevent="none")

        # locus
        self.locus_tree.write(
            filenames.get("locus_tree", filename + exts["locus_tree"]),
            rootData=True)
        phylo.write_recon_events(
            filenames.get("locus_recon", filename + exts["locus_recon"]),
            self.locus_recon, self.locus_events)

        util.write_list(
            filenames.get("daughters", filename + exts["daughters"]),
            [x.name for x in self.daughters])
Пример #4
0
    def write(
            self,
            filename,
            coal_tree,
            exts={
                "coal_tree": ".coal.tree",
                "coal_recon": ".coal.recon",
                "locus_tree": ".locus.tree",
                "locus_recon": ".locus.recon",
                "daughters": ".daughters"
            },
            filenames={},
            filestreams={}):
        """Writes a reconciled gene tree to files"""

        assert coal_tree and self.coal_recon and \
               self.locus_tree and self.locus_recon and self.locus_events and \
               (self.daughters is not None)

        # coal tree and recon
        coal_tree.write(filestreams.get(
            "coal_tree",
            filenames.get("coal_tree", filename + exts["coal_tree"])),
                        rootData=True)
        phylo.write_recon_events(filestreams.get(
            "coal_recon",
            filenames.get("coal_recon", filename + exts["coal_recon"])),
                                 self.coal_recon,
                                 noevent="none")

        # locus tree and recon
        self.locus_tree.write(filestreams.get(
            "locus_tree",
            filenames.get("locus_tree", filename + exts["locus_tree"])),
                              rootData=True)
        phylo.write_recon_events(
            filestreams.get(
                "locus_recon",
                filenames.get("locus_recon", filename + exts["locus_recon"])),
            self.locus_recon, self.locus_events)

        # daughters
        util.write_list(
            filestreams.get(
                "daughters",
                filenames.get("daughters", filename + exts["daughters"])),
            [x.name for x in self.daughters])
def dndsMatrix(seqs, saveOutput="", verbose=False, safe=True):
    
    if safe:
        seqs = alignlib.mapalign(seqs, valfunc=removeStopCodons)
    
    phylip.validate_seqs(seqs)
    cwd = phylip.create_temp_dir()

    util.tic("yn00 on %d of length %d" % (len(seqs), len(seqs.values()[0])))

    # create input
    labels = phylip.write_phylip_align(file("seqfile.phylip", "w"), seqs)
    util.write_list(file("labels", "w"), labels)    
    
    # create control file
    out = file("yn00.ctl", "w")
    print >>out, "seqfile = seqfile.phylip"
    print >>out, "outfile = outfile"
    out.close()
    
    # run yn00
    if verbose:
        os.system("yn00 yn00.ctl")
    else:
        os.system("yn00 yn00.ctl > /dev/null")
    
    try:
        dnmat = phylip.read_dist_matrix("2YN.dN")
        dsmat = phylip.read_dist_matrix("2YN.dS")
    except:
        # could not make distance matrix
        if safe:
            # make dummy matrix
            dnmat = labels, [[0] * len(labels)] * len(labels)
            dsmat = labels, [[0] * len(labels)] * len(labels)
        else:
            raise Exception("could not read dn or ds matrices")
    
    if saveOutput != "":
        phylip.save_temp_dir(cwd, saveOutput)
    else:
        phylip.cleanup_temp_dir(cwd)
    
    util.toc()
    
    return dnmat, dsmat
Пример #6
0
def dndsMatrix(seqs, saveOutput="", verbose=False, safe=True):

    if safe:
        seqs = alignlib.mapalign(seqs, valfunc=removeStopCodons)

    phylip.validate_seqs(seqs)
    cwd = phylip.create_temp_dir()

    util.tic("yn00 on %d of length %d" % (len(seqs), len(seqs.values()[0])))

    # create input
    labels = phylip.write_phylip_align(file("seqfile.phylip", "w"), seqs)
    util.write_list(file("labels", "w"), labels)

    # create control file
    out = file("yn00.ctl", "w")
    print >> out, "seqfile = seqfile.phylip"
    print >> out, "outfile = outfile"
    out.close()

    # run yn00
    if verbose:
        os.system("yn00 yn00.ctl")
    else:
        os.system("yn00 yn00.ctl > /dev/null")

    try:
        dnmat = phylip.read_dist_matrix("2YN.dN")
        dsmat = phylip.read_dist_matrix("2YN.dS")
    except:
        # could not make distance matrix
        if safe:
            # make dummy matrix
            dnmat = labels, [[0] * len(labels)] * len(labels)
            dsmat = labels, [[0] * len(labels)] * len(labels)
        else:
            raise Exception("could not read dn or ds matrices")

    if saveOutput != "":
        phylip.save_temp_dir(cwd, saveOutput)
    else:
        phylip.cleanup_temp_dir(cwd)

    util.toc()

    return dnmat, dsmat
Пример #7
0
def align2tree(prog,
               seqs,
               verbose=True,
               force=False,
               args=None,
               usertree=None,
               saveOutput="",
               bootiter=1,
               seed=1,
               jumble=1):
    validate_seqs(seqs)
    cwd = create_temp_dir()

    util.tic("%s on %d of length %d" %
             (prog, len(seqs), len(seqs.values()[0])))

    # create input
    labels = write_phylip_align(file("infile", "w"), seqs)
    util.write_list(file("labels", "w"), labels)

    # initialize default arguments
    if args == None:
        args = "y"

    # create user tree if given
    if usertree != None:
        write_in_tree("intree", usertree, labels)
        args = "u\n" + args  # add user tree option

    # bootstrap alignment if needed
    if bootiter > 1:
        exec_phylip("seqboot", "r\n%d\ny\n%d" % (bootiter, seed), verbose)
        os.rename("outfile", "infile")

        # add bootstrap arguments
        args = "m\nD\n%d\n%d\n%d\n%s" % (bootiter, seed, jumble, args)

    # run phylip
    exec_phylip(prog, args, verbose)

    # check for PHYLIP GIVE UP
    if is_phylip_give_up("outfile"):
        tree = treelib.Tree()
        tree.make_root()

        # make star tree
        for key in seqs:
            tree.add_child(tree.root, treelib.TreeNode(key))

    else:
        # parse tree
        if bootiter == 1:
            tree = read_out_tree("outtree", labels, bootiter)

            # parse likelihood
            if prog in ["dnaml", "proml"]:
                tree.data["logl"] = read_logl("outfile")

        else:
            trees = read_out_tree("outtree", labels, bootiter)

    if saveOutput != "":
        save_temp_dir(cwd, saveOutput)
    else:
        cleanup_temp_dir(cwd)

    util.toc()

    if bootiter == 1:
        return tree
    else:
        return trees
def phyml(seqs, verbose=True, args=None, 
          usertree=None, seqtype="pep", saveOutput="", bootiter=0,
          opttree=True, optbranches=True, nrates=4):
    
    phylip.validate_seqs(seqs)
    cwd = phylip.create_temp_dir()
    
    util.tic("phyml on %d of length %d" % (len(seqs), len(seqs.values()[0])))

    # create input
    labels = phylip.write_phylip_align(file("infile", "w"), seqs)
    util.write_list(file("labels", "w"), labels)
    
    options = "y"
    
    # only bootstrap when iterations are above 1
    if bootiter == 1:
        bootiter = 0
    
    if usertree != None:
        usertree = treelib.unroot(usertree)
        phylip.write_in_tree("intree", usertree, labels)
        treefile = "intree"
    else:
        treefile = "BIONJ"
    
    
    optimize = ""
    if opttree:
        optimize += "y "
    else:
        optimize += "n "
    
    if optbranches:
        optimize += "y "
    else:
        optimize += "n "
    
    
    if args == None:
        if seqtype == "dna":
            args = "infile 0 s 1 %d HKY e e %d e %s %s" % \
                (bootiter, nrates, treefile, optimize)
        elif seqtype == "pep":
            args = "infile 1 s 1 %d JTT e %d e %s %s" % \
                (bootiter, nrates, treefile, optimize)
        else:
            assert False, "unknown sequence type '%s'" % seqtype
    
    
    phylip.exec_phylip("phyml %s" % args, options, verbose)
    
    # parse tree
    tree = phylip.read_out_tree("infile_phyml_tree.txt", labels)
    
    # parse likelihood
    tree.data["logl"] = float(file("infile_phyml_lk.txt").read())
    
    if saveOutput != "":
        phylip.save_temp_dir(cwd, saveOutput)
    else:
        phylip.cleanup_temp_dir(cwd)
    util.toc()
    
    return tree
Пример #9
0
def align2tree(prog, seqs, verbose=True, force = False, args=None, 
               usertree=None, saveOutput="", 
               bootiter=1,
               seed=1,
               jumble=1):
    validate_seqs(seqs)
    cwd = create_temp_dir()

    util.tic("%s on %d of length %d" % (prog, len(seqs), len(seqs.values()[0])))

    # create input
    labels = write_phylip_align(file("infile", "w"), seqs)
    util.write_list(file("labels", "w"), labels)

    # initialize default arguments
    if args == None:
        args = "y"
    
    # create user tree if given
    if usertree != None:
        write_in_tree("intree", usertree, labels)
        args = "u\n" + args # add user tree option
    
    
    # bootstrap alignment if needed
    if bootiter > 1:
    	exec_phylip("seqboot", "r\n%d\ny\n%d" % (bootiter, seed), verbose)
        os.rename("outfile", "infile")    
    	
    	# add bootstrap arguments
    	args = "m\nD\n%d\n%d\n%d\n%s" % (bootiter, seed, jumble, args)
        
    
    # run phylip
    exec_phylip(prog, args, verbose)
    
    # check for PHYLIP GIVE UP
    if is_phylip_give_up("outfile"):
        tree = treelib.Tree()
        tree.make_root()
        
        # make star tree
        for key in seqs:
            tree.add_child(tree.root, treelib.TreeNode(key))
        
    else:
        # parse tree
        if bootiter == 1:
            tree = read_out_tree("outtree", labels, bootiter)

            # parse likelihood
            if prog in ["dnaml", "proml"]:
                tree.data["logl"] = read_logl("outfile")

        else:
            trees = read_out_tree("outtree", labels, bootiter)
    
    
    if saveOutput != "":
        save_temp_dir(cwd, saveOutput)
    else:
        cleanup_temp_dir(cwd)
    
    util.toc()
    
    
    if bootiter == 1:
        return tree
    else:
        return trees
Пример #10
0
def phyml(seqs,
          verbose=True,
          args=None,
          usertree=None,
          seqtype="pep",
          saveOutput="",
          bootiter=0,
          opttree=True,
          optbranches=True,
          nrates=4):

    phylip.validate_seqs(seqs)
    cwd = phylip.create_temp_dir()

    util.tic("phyml on %d of length %d" % (len(seqs), len(seqs.values()[0])))

    # create input
    labels = phylip.write_phylip_align(file("infile", "w"), seqs)
    util.write_list(file("labels", "w"), labels)

    options = "y"

    # only bootstrap when iterations are above 1
    if bootiter == 1:
        bootiter = 0

    if usertree != None:
        usertree = treelib.unroot(usertree)
        phylip.write_in_tree("intree", usertree, labels)
        treefile = "intree"
    else:
        treefile = "BIONJ"

    optimize = ""
    if opttree:
        optimize += "y "
    else:
        optimize += "n "

    if optbranches:
        optimize += "y "
    else:
        optimize += "n "

    if args == None:
        if seqtype == "dna":
            args = "infile 0 s 1 %d HKY e e %d e %s %s" % \
                (bootiter, nrates, treefile, optimize)
        elif seqtype == "pep":
            args = "infile 1 s 1 %d JTT e %d e %s %s" % \
                (bootiter, nrates, treefile, optimize)
        else:
            assert False, "unknown sequence type '%s'" % seqtype

    phylip.exec_phylip("phyml %s" % args, options, verbose)

    # parse tree
    tree = phylip.read_out_tree("infile_phyml_tree.txt", labels)

    # parse likelihood
    tree.data["logl"] = float(file("infile_phyml_lk.txt").read())

    if saveOutput != "":
        phylip.save_temp_dir(cwd, saveOutput)
    else:
        phylip.cleanup_temp_dir(cwd)
    util.toc()

    return tree