Exemple #1
0
    def __init__(self,
                 data,
                 name="test",
                 workdir="analysis-mb",
                 clock=False,
                 **kwargs):

        # path attributes
        self._kwargs = {}
        self._kwargs.update(kwargs)

        # check workdir
        if workdir:
            workdir = os.path.abspath(os.path.expanduser(workdir))
        else:
            workdir = os.path.abspath(os.path.curdir)
        if not os.path.exists(workdir):
            os.makedirs(workdir)

        # entered args
        self.name = name
        self.workdir = workdir
        self.data = os.path.abspath(os.path.expanduser(data))
        self.nexus = os.path.join(self.workdir, self.name + ".nex")
        self.binary = ""
        self._get_binary(self._kwargs.get("binary"))

        self.params = Params()
        defaults = {
            "clockratepr": "lognorm(-7,0.6)",
            "clockvarpr": "tk02",
            "tk02varpr": "exp(1.0)",
            "brlenspr": "clock:birthdeath",
            "samplestrat": "diversity",
            "sampleprob": "0.1",
            "speciationpr": "exp(10)",
            "extinctionpr": "beta(2, 200)",
            "treeagepr": "offsetexp(1, 5)",
            "ngen": 100000,
            "nruns": "2",
            "nchains": 4,
            "samplefreq": 1000,
        }
        for i, j in defaults.items():
            setattr(self.params, i, j)

        # set params
        for key in self._kwargs:
            setattr(self.params, key, self._kwargs[key])

        # attributes
        self.rasync = None
        self.stdout = None
        self.stderr = None

        # results files
        self.trees = Params()
        self.trees.constre = os.path.join(self.workdir,
                                          "{}.con.tre".format(self.name))
        self.trees.info = os.path.join(self.workdir,
                                       "{}.lstat".format(self.name))

        # write a mrbayes block to a copy of the NEXUS file.
        cwargs = self.params.__dict__.copy()
        cwargs["nexus"] = self.data
        cwargs["outname"] = self.nexus
        if clock:
            self._nexstring = NEX_TEMPLATE_2.format(**cwargs)
        else:
            self._nexstring = NEX_TEMPLATE_1.format(**cwargs)
        with open(self.nexus, 'w') as out:
            out.write(self._nexstring)
Exemple #2
0
    def __init__(self,
                 data,
                 name="test",
                 workdir="analysis-mb",
                 clock=False,
                 **kwargs):

        # path attributes
        self._kwargs = {}
        self._kwargs.update(kwargs)

        # check workdir
        if workdir:
            workdir = os.path.abspath(os.path.expanduser(workdir))
        else:
            workdir = os.path.abspath(os.path.curdir)
        if not os.path.exists(workdir):
            os.makedirs(workdir)

        # entered args
        self.clock = clock
        self.name = name
        self.workdir = workdir
        self.data = os.path.abspath(os.path.expanduser(data))
        self.nexus = os.path.join(self.workdir, self.name + ".nex")
        self.binary = ""
        self._get_binary(self._kwargs.get("binary"))

        self.params = Params()
        defaults = {
            "clockratepr": "lognorm(-7,0.6)",
            "clockvarpr": "tk02",
            "tk02varpr": "exp(1.0)",
            "brlenspr": "clock:birthdeath",
            "samplestrat": "diversity",
            "sampleprob": "0.1",
            "speciationpr": "exp(10)",
            "extinctionpr": "beta(2, 200)",
            "treeagepr": "offsetexp(1, 5)",
            "ngen": 100000,
            "nruns": "1",
            "nchains": 4,
            "samplefreq": 1000,
        }
        for i, j in defaults.items():
            setattr(self.params, i, j)

        # set params
        for key in self._kwargs:
            setattr(self.params, key, self._kwargs[key])

        # attributes
        self.rasync = None
        self.stdout = None
        self.stderr = None

        # results files
        self.trees = Params()
        runs = ("" if int(self.params.nruns) < 2 else "run1.")
        self.trees.constre = os.path.join(self.workdir,
                                          "{}.nex.con.tre".format(self.name))
        self.trees.posttre = os.path.join(self.workdir,
                                          "{}.nex.{}t".format(self.name, runs))
        self.trees.pstat = os.path.join(self.workdir,
                                        "{}.nex.pstat".format(self.name))
        self._write_nexus_file()

        # check attribute for existing results at this name.
        if self.result_files:
            print(
                "Existing results loaded for run [{}], see .trees attribute.".
                format(len(self.result_files), self.name))
Exemple #3
0
    def __init__(self,
                 data=None,
                 tests=None,
                 newick=None,
                 nboots=1000,
                 mincov=1):
        """ 
        ipyrad.analysis Baba Class object.

        Parameters
        ----------
        data : string or ndarray
            A string path to a .loci file produced by ipyrad. Alternatively, 
            data can be entered as a Numpy array of float allele frequencies 
            with dimension (nloci, 4 or 5, maxlen). See simulation example 
            in the docs.
            
        tests : dict or list of dicts
            A dictionary mapping Sample names to test taxon names, e.g., 
            test = {'p1': ['a', 'b'], 'p2': ['c'], 'p3': ['e'], 'p4': ['f']}.
            Four taxon tests should have p1-p4 whereas five taxon tests will 
            used if dict keys are p1-p5. Other key names will raise an error. 
            The highest value name (e.g., p5) is the outgroup. 
        
        newick: str
            ...
    
        Functions
        ---------
        run()
            ...
        generate_tests_from_tree()
            ...
        plot()
            ...

        """
        # check external imports
        if not sys.modules.get("toytree"):
            raise ImportError(_TOYTREE_IMPORT)
        if not sys.modules.get("toyplot"):
            raise ImportError(_TOYPLOT_IMPORT)

        ## parse data as (1) path to data file, or (2) ndarray
        if isinstance(data, str):
            self.data = os.path.realpath(data)
        else:
            self.data = data

        if isinstance(newick, toytree.tree):
            self.newick = newick.tree.write()
        else:
            self.newick = newick

        ## store tests, check for errors
        self.tests = tests

        ## parameters
        self.params = Params()
        self.params.mincov = mincov
        self.params.nboots = nboots
        self.params.quiet = False
        self.params.database = None

        ## results storage
        self.results_table = None
        self.results_boots = None
Exemple #4
0
    def __init__(
        self,
        data,
        name="test",
        workdir="analysis-mb", 
        clock_model=False,
        constraints=None,
        **kwargs):

        # path attributes
        self._kwargs = {}            
        self._kwargs.update(kwargs)

        # check workdir
        if workdir:
            workdir = os.path.abspath(os.path.expanduser(workdir))
        else:
            workdir = os.path.abspath(os.path.curdir)
        if not os.path.exists(workdir):
            os.makedirs(workdir)

        # entered args
        self.clock_model = clock_model
        self.constraints = constraints
        self.name = name
        self.workdir = workdir
        self.data = os.path.abspath(os.path.expanduser(data))
        self.nexus = os.path.join(self.workdir, self.name + ".nex")
        self.binary = ""
        self._get_binary(self._kwargs.get("binary"))

        self.params = Params()
        defaults = {
            "ngen": 100000,
            "nruns": "1",
            "nchains": 4,
            "samplefreq": 1000,
        }
        if self.clock_model == 1:
            defaults.update(TEMPLATE_2_DICT)
        elif self.clock_model == 2:
            defaults.update(TEMPLATE_3_DICT)

        for i, j in defaults.items():
            setattr(self.params, i, j)

        # set params (overrides defaults)
        for key in self._kwargs:
            setattr(self.params, key, self._kwargs[key])

        # attributes
        self.rasync = None
        self.stdout = None
        self.stderr = None

        # results files        
        self.trees = Params()
        runs = ("" if int(self.params.nruns) < 2 else "run1.")
        self.trees.constre = os.path.join(
            self.workdir, "{}.nex.con.tre".format(self.name))
        self.trees.posttre = os.path.join(
            self.workdir, "{}.nex.{}t".format(self.name, runs))
        self.trees.pstat = os.path.join(
            self.workdir, "{}.nex.pstat".format(self.name))
        self._write_nexus_file()

        # check attribute for existing results at this name.
        if self.result_files:
            print(
                "Existing results loaded for run [{}], see .trees attribute."
                .format(len(self.result_files), self.name)
            )