def _getMrBayesSampleTree(self, sampNum): savedDoFastNextTok = var.nexus_doFastNextTok var.nexus_doFastNextTok = False tLine = self.tLines[sampNum] if self.verbose >= 3: print tLine f = cStringIO.StringIO(tLine) t = Tree() t.parseNexus(f, translationHash=self.translationHash, doModelComments=self.tree.model.nParts) # doModelComments is nParts var.nexus_doFastNextTok = savedDoFastNextTok t.taxNames = self.tree.taxNames for n in t.iterLeavesNoRoot(): n.seqNum = t.taxNames.index(n.name) t.model = copy.deepcopy(self.model) pLine = self.pLines[sampNum] if self.verbose >= 3: print pLine splitPLine = pLine.split() pGenNum = int(splitPLine[0]) splitTName = t.name.split('.') tGenNum = int(splitTName[1]) if tGenNum != pGenNum: raise P4Error( "something wrong. tGenNum=%i, pGenNum=%i" % (tGenNum, pGenNum)) if self.verbose >= 2: print "(zero-based) sample %i is gen %i" % (sampNum, tGenNum) # t.model.dump() splIndx = 3 # but could be Gen LnL LnPr TL ... while splIndx < self.nPrams: pNum = 0 # print "splIndx = %i, pramsHeader = %s" % (splIndx, # self.pramsHeader[splIndx]) if self.pramsHeader[splIndx].startswith('r(A<->C)'): if self.tree.model.nParts > 1: try: splitPramHeader = self.pramsHeader[ splIndx].split('{')[1][:-1] pNum = int(splitPramHeader) pNum -= 1 except: raise P4Error("could not get the part number") thisSum = 0.0 for i in range(6): theFloat = float(splitPLine[splIndx]) t.model.parts[pNum].rMatrices[0].val[i] = theFloat thisSum += theFloat splIndx += 1 factor = 1.0 / thisSum # must sum to one for i in range(6): t.model.parts[pNum].rMatrices[0].val[i] *= factor elif self.pramsHeader[splIndx].startswith('pi(A)'): if self.tree.model.nParts > 1: try: splitPramHeader = self.pramsHeader[ splIndx].split('{')[1][:-1] pNum = int(splitPramHeader) pNum -= 1 except: raise P4Error("could not get the part number") thisSum = 0.0 for i in range(4): theFloat = float(splitPLine[splIndx]) t.model.parts[pNum].comps[0].val[i] = theFloat thisSum += theFloat splIndx += 1 factor = 1.0 / thisSum # must sum to one for i in range(4): t.model.parts[pNum].comps[0].val[i] *= factor elif self.pramsHeader[splIndx].startswith('alpha'): if self.tree.model.nParts > 1: try: splitPramHeader = self.pramsHeader[ splIndx].split('{')[1][:-1] pNum = int(splitPramHeader) pNum -= 1 except: raise P4Error("could not get the part number") # print "got pNum = %i" % pNum # print "got splitPLine[%i] = %s" % (splIndx, # splitPLine[splIndx]) t.model.parts[pNum].gdasrvs[0].val[ 0] = float(splitPLine[splIndx]) splIndx += 1 elif self.pramsHeader[splIndx].startswith('pinvar'): if self.tree.model.nParts > 1: try: splitPramHeader = self.pramsHeader[ splIndx].split('{')[1][:-1] pNum = int(splitPramHeader) pNum -= 1 except: raise P4Error("could not get the part number") t.model.parts[pNum].pInvar.val = float(splitPLine[splIndx]) splIndx += 1 elif self.pramsHeader[splIndx].startswith('m'): if self.tree.model.nParts > 1: try: splitPramHeader = self.pramsHeader[ splIndx].split('{')[1][:-1] pNum = int(splitPramHeader) pNum -= 1 except: raise P4Error("could not get the part number") t.model.parts[pNum].relRate = float(splitPLine[splIndx]) splIndx += 1 else: print "splIndx=%i. Got unknown pram %s. Fix me!" % (splIndx, self.pramsHeader[splIndx]) splIndx += 1 if splIndx != len(splitPLine): raise P4Error("Something is wrong. After reading, splIndx=%i, but len split pram line=%i" % ( splIndx, len(splitPLine))) return t
def _getP4SampleTree(self, sampNum): savedDoFastNextTok = var.nexus_doFastNextTok var.nexus_doFastNextTok = False tLine = self.tLines[sampNum] if self.verbose >= 3: print tLine f = cStringIO.StringIO(tLine) t = Tree() t.parseNexus(f, translationHash=self.translationHash, doModelComments=self.tree.model.nParts) var.nexus_doFastNextTok = savedDoFastNextTok t.taxNames = self.tree.taxNames for n in t.iterLeavesNoRoot(): n.seqNum = t.taxNames.index(n.name) t.model = copy.deepcopy(self.model) if self.tree.model.nFreePrams: pLine = self.pLines[sampNum] if self.verbose >= 3: print pLine splitPLine = pLine.split() pGenNum = int(splitPLine[0]) splitTName = t.name.split('_') tGenNum = int(splitTName[1]) if tGenNum != pGenNum: raise P4Error( "something wrong. tGenNum=%i, pGenNum=%i" % (tGenNum, pGenNum)) if self.verbose >= 2: print "(zero-based) sample %i is gen %i" % (sampNum, tGenNum) # t.model.dump() splIndx = 1 for pNum in range(len(self.pramsProfile)): compNum = 0 rMatrixNum = 0 gdasrvNum = 0 for desc in self.pramsProfile[pNum]: if desc[0] == 'relRate': t.model.parts[pNum].relRate = float( splitPLine[splIndx]) splIndx += 1 elif desc[0] == 'comp': vv = [] for i in range(desc[1]): vv.append(float(splitPLine[splIndx])) splIndx += 1 for i in range(desc[1]): if vv[i] < var.PIVEC_MIN: vv[i] = var.PIVEC_MIN * 1.1 thisSum = sum(vv) factor = 1.0 / thisSum # must sum to one for i in range(desc[1]): t.model.parts[pNum].comps[ compNum].val[i] = vv[i] * factor compNum += 1 elif desc[0] == 'rMatrix': vv = [] for i in range(desc[1]): vv.append(float(splitPLine[splIndx])) splIndx += 1 if len(vv) == 1: # its a '2p' model, with a kappa t.model.parts[pNum].rMatrices[ rMatrixNum].val[0] = vv[0] else: # gtr for i in range(desc[1]): if vv[i] < var.RATE_MIN: vv[i] = var.RATE_MIN * 1.1 thisSum = sum(vv) factor = 1.0 / thisSum # must sum to one for i in range(desc[1]): t.model.parts[pNum].rMatrices[ rMatrixNum].val[i] = vv[i] * factor rMatrixNum += 1 elif desc[0] == 'gdasrv': t.model.parts[pNum].gdasrvs[gdasrvNum].val[ 0] = float(splitPLine[splIndx]) splIndx += 1 gdasrvNum += 1 elif desc[0] == 'pInvar': t.model.parts[pNum].pInvar.val = float( splitPLine[splIndx]) splIndx += 1 if splIndx != len(splitPLine): raise P4Error("Something is wrong. After reading, splIndx=%i, but len split pram line=%i" % ( splIndx, len(splitPLine))) return t