Esempio n. 1
0
def simulate(self,
             calculatePatterns=True,
             resetSequences=True,
             resetNexusSetsConstantMask=True):
    """Simulate into the attached data.

    The tree self needs to have a data and model attached.

    This week, generation of random numbers uses the C language random
    function, which is in stdlib on Linux.  It will use the same
    series of random numbers over and over, unless you tell it
    otherwise.  That means that (unless you tell it otherwise) it will
    generate the same simulated data if you run it twice.  To reset
    the randomizer, you can use func.reseedCRandomizer(), eg

    func.reseedCRandomizer(os.getpid())

    
    """

    self._commonCStuff()

    # If there is a NexusSets object attached to any of the alignments
    # in the Data, the constant sites mask at least will become out of sync, but we can't just
    # delete the whole nexusSets object, as they define what the parts are.
    #for a in self.data.alignments:
    #
    #    if a.nexusSets:
    #        a.nexusSets = None

    # Probably better to do something like this
    # a.nexusSets.constant.mask = self.constantMask()
    # at the end.

    #print "About to pf.p4_simulate(self.cTree)"
    pf.p4_simulate(self.cTree)
    if calculatePatterns:
        for p in self.data.parts:
            pf.makePatterns(p.cPart)
            pf.setGlobalInvarSitesVec(p.cPart)
    if resetSequences:
        self.data.resetSequencesFromParts()
        if resetNexusSetsConstantMask:
            for a in self.data.alignments:
                if a.nexusSets:
                    a.nexusSets.constant.mask = a.constantMask()
    else:
        if resetNexusSetsConstantMask:
            gm = ['Tree.simulate().']
            gm.append(
                "resetSequences is not set, but resetNexusSetsConstantMask is set,"
            )
            gm.append("which is probably not going to work as you want.")
            raise Glitch, gm
Esempio n. 2
0
def simulate(self, calculatePatterns=True, resetSequences=True, resetNexusSetsConstantMask=True):
    """Simulate into the attached data.

    The tree self needs to have a data and model attached.

    This week, generation of random numbers uses the C language random
    function, which is in stdlib on Linux.  It will use the same
    series of random numbers over and over, unless you tell it
    otherwise.  That means that (unless you tell it otherwise) it will
    generate the same simulated data if you run it twice.  To reset
    the randomizer, you can use func.reseedCRandomizer(), eg

    func.reseedCRandomizer(os.getpid())

    
    """
    
    self._commonCStuff()

    # If there is a NexusSets object attached to any of the alignments
    # in the Data, the constant sites mask at least will become out of sync, but we can't just
    # delete the whole nexusSets object, as they define what the parts are.
    #for a in self.data.alignments:
    # 
    #    if a.nexusSets:
    #        a.nexusSets = None

    # Probably better to do something like this
    # a.nexusSets.constant.mask = self.constantMask()
    # at the end.
    
    #print "About to pf.p4_simulate(self.cTree)"
    pf.p4_simulate(self.cTree)
    if calculatePatterns:
        for p in self.data.parts:
            pf.makePatterns(p.cPart)
            pf.setGlobalInvarSitesVec(p.cPart)
    if resetSequences:
        self.data.resetSequencesFromParts()
        if resetNexusSetsConstantMask:
            for a in self.data.alignments:
                if a.nexusSets:
                    a.nexusSets.constant.mask = a.constantMask()
    else:
        if resetNexusSetsConstantMask:
            gm = ['Tree.simulate().']
            gm.append("resetSequences is not set, but resetNexusSetsConstantMask is set,")
            gm.append("which is probably not going to work as you want.")
            raise Glitch(gm)
Esempio n. 3
0
    def simulate(self, calculatePatterns=True, resetSequences=True, resetNexusSetsConstantMask=True, refTree=None):
        """Simulate into the attached data.

        The tree self needs to have a data and model attached.

        This week, generation of random numbers uses the C language random
        function, which is in stdlib on Linux.  It will use the same
        series of random numbers over and over, unless you tell it
        otherwise.  That means that (unless you tell it otherwise) it will
        generate the same simulated data if you run it twice.  To reset
        the randomizer, you can use func.reseedCRandomizer(), eg

        func.reseedCRandomizer(os.getpid())

        The usual way to simulate does not use reference data.  An unsual way to
        simulate comes from (inspired by?) PhyloBayes, where the simulation is
        conditional on the original data.  It uses conditional likelihoods of
        that reference data at the root.  To turn that on, set refTree to the
        tree+model+data that you would like to use.  Calculate a likelihood with
        that refTree before using it, so that conditional likelihoods are set.
        The tree and model for refTree should be identical to the tree and model
        for self.

        Args: 

            calculatePatterns (bool): True by default. Whether to "compress" the
                newly simulated data to facilitate a faster likelihood
                calculation.

            resetSequences (bool): True by default. whether to bring the
                simulated sequences in C back into Python

            resetNexusSetsConstantMask (bool): True by default.  When
                simulations are made, the constant mask in any associated nexus
                sets will get out of sync.  Setting this to True makes a new
                mask and sets it.

            refTree (Tree): None by default.  If supplied, a tree+model+data
                which has had its likelihood calculated, where the tree+model is
                identical to self.

        """

        if refTree:
            from tree import Tree
            assert isinstance(refTree, Tree)
            assert refTree.model
            assert refTree.data
            if not refTree.cTree:
                refTree.calcLogLike(verbose=False)
            assert refTree.model.cModel
            assert refTree.data.cData
            
            

        self._commonCStuff()
        if refTree:
            assert refTree.data.cData != self.data.cData
            assert refTree.data.nParts == self.data.nParts
            assert refTree.data.nTax == self.data.nTax
            for i in range(self.data.nTax):
                assert refTree.data.taxNames[i] == self.data.taxNames[i]
            assert len(refTree.data.alignments) == len(self.data.alignments)
            assert refTree.logLike, "Do a likelihood calculation with the refTree before using it here."
            # could have some more checks ...
            

        # If there is a NexusSets object attached to any of the alignments
        # in the Data, the constant sites mask at least will become out of sync, but we can't just
        # delete the whole nexusSets object, as they define what the parts are.
        # for a in self.data.alignments:
        #
        #    if a.nexusSets:
        #        a.nexusSets = None

        # Probably better to do something like this
        # a.nexusSets.constant.mask = self.constantMask()
        # at the end.

        # print "About to pf.p4_simulate(self.cTree)"
        if refTree:
            pf.p4_simulate(self.cTree, refTree.cTree)
        else:
            pf.p4_simulate(self.cTree, 0)
        if calculatePatterns:
            for p in self.data.parts:
                pf.makePatterns(p.cPart)
                pf.setGlobalInvarSitesVec(p.cPart)
        if resetSequences:
            self.data.resetSequencesFromParts()
            if resetNexusSetsConstantMask:
                for a in self.data.alignments:
                    if a.nexusSets:
                        a.nexusSets.constant.mask = a.constantMask()
        else:
            if resetNexusSetsConstantMask:
                gm = ['Tree.simulate().']
                gm.append(
                    "resetSequences is not set, but resetNexusSetsConstantMask is set,")
                gm.append("which is probably not going to work as you want.")
                raise P4Error(gm)