def calcfst(pops, posdict, gtdict): """ """ fst_obs = [] ix = 0 popiix = [] pw = len(pops) fstarray = np.zeros([len(posdict.keys()), (pw*(pw-1))/2]) # Observed FST for p in pops: popiix.append(range(ix, ix + p)) ix += p for r in gtdict.keys(): fst_obs = [] for i, pix in enumerate(popiix): for j, jix in enumerate(popiix): if i > j: popX = gtdict[r][pix] popY = gtdict[r][jix] sdfst = simData() geno_fst = np.vstack([popX, popY]) gtpop_fst = [''.join(str(n) for n in y) for y in geno_fst] sdfst.assign_sep(posdict[r], gtpop_fst) size = [popX.shape[0], popY.shape[0]] f1 = fst(sdfst, size) fst_obs.append(f1.slatkin()) fstarray[int(r), :] = fst_obs return(fstarray)
def testShared(self): x = [(0.1, "0011"), (0.2, "1100"), (0.3, "0100"), (0.4, "1101"), (0.5, "0101")] d = simData() d.assign(x) f = fst(d, [2, 2]) self.assertEqual(f.shared(0, 1), [0.5])
def permtest(gtdict, posdict, pops, n_perm, fstarray): """ """ nhap = sum(pops) fst_t = [] r = random.choice(gtdict.keys()) # FST random permutations for p in range(n_perm): popX = gtdict[r][np.random.randint(0, nhap, pops[0])] popY = gtdict[r][np.random.randint(0, nhap, pops[0])] sdfst = simData() geno_fst = np.vstack([popX, popY]) gtpop_fst = [''.join(str(n) for n in y) for y in geno_fst] sdfst.assign_sep(posdict[r], gtpop_fst) size = [popX.shape[0], popY.shape[0]] f1 = fst(sdfst, size) # fst_t.append(f1.slatkin()) fst_t.append(f1.hsm()) # fst_t.appen(f1.hbk()) # mark significant FST fst_tnp = np.array(fst_t) Fstdist = [len(np.where(f > fst_tnp)[0]) for f in fstarray] return ([1 - (f / float(n_perm)) for f in Fstdist])
def testShared(self): x = [(0.1,b"0011"),(0.2,b"1100"), (0.3,b"0100"),(0.4,b"1101"), (0.5,b"0101")] d = simData() d.assign(x) f = fst(d,[2,2]) self.assertEqual(f.shared(0,1),[0.5])
def testExceptionPriv(self): with self.assertRaises(RuntimeError): x = [(0.1, "0011"), (0.2, "1100"), (0.3, "0100"), (0.4, "1101"), (0.5, "0101")] d = simData() d.assign(x) f = fst(d, [2, 2]) #2 is out of range. sh = f.priv(2, 1)
def testFixed(self): x = [(0.1, "0011"), (0.2, "1100"), (0.3, "0100"), (0.4, "1101"), (0.5, "0101")] d = simData() d.assign(x) f = fst(d, [2, 2]) p = f.fixed(0, 1) expected = [0.1, 0.2] self.assertEqual(p, expected)
def testPriv(self): x = [(0.1, "0011"), (0.2, "1100"), (0.3, "0100"), (0.4, "1101"), (0.5, "0101")] d = simData() d.assign(x) f = fst(d, [2, 2]) p = f.priv(0, 1) expected = {0: [0.3], 1: [0.4]} self.assertEqual(p, expected)
def testException1(self): with self.assertRaises(RuntimeError): x = [(0.1, "0011"), (0.2, "1100"), (0.3, "0100"), (0.4, "1101"), (0.5, "0101")] d = simData() d.assign(x) ##the second argument's sum is > total sample size ##libsequence will throw a SeqException here, ##which gets tranlated to a RuntimeError f = fst(d, [2, 3])
def testExceptionPriv(self): with self.assertRaises(RuntimeError): x = [(0.1,b"0011"),(0.2,b"1100"), (0.3,b"0100"),(0.4,b"1101"), (0.5,b"0101")] d = simData() d.assign(x) f = fst(d,[2,2]) #2 is out of range. sh = f.priv(2,1)
def testFixed(self): x = [(0.1,b"0011"),(0.2,b"1100"), (0.3,b"0100"),(0.4,b"1101"), (0.5,b"0101")] d = simData() d.assign(x) f = fst(d,[2,2]) p = f.fixed(0,1) expected = [0.1,0.2] self.assertEqual(p,expected)
def testPriv(self): x = [(0.1,b"0011"),(0.2,b"1100"), (0.3,b"0100"),(0.4,b"1101"), (0.5,b"0101")] d = simData() d.assign(x) f = fst(d,[2,2]) p = f.priv(0,1) expected = {0:[0.3],1:[0.4]} self.assertEqual(p,expected)
def testException1(self): with self.assertRaises(RuntimeError): x = [(0.1,b"0011"),(0.2,b"1100"), (0.3,b"0100"),(0.4,b"1101"), (0.5,b"0101")] d = simData() d.assign(x) ##the second argument's sum is > total sample size ##libsequence will throw a SeqException here, ##which gets tranlated to a RuntimeError f = fst(d,[2,3])
def calcfst(pops, posdict, gtdict, L, snp=True, hud=True): """ """ ix = 0 pw = len(pops) fstarray = np.zeros([len(posdict.keys()), int((pw * (pw - 1)) / 2)]) # Observed FST pix = 0 popdict = {} for p in pops: popdict[pix] = list(range(ix, ix + p)) ix += p pix += 1 for r in gtdict.keys(): fst_obs = [] if snp: pos_snp = [(np.random.choice(posdict[r]))] gt_snp = np.where(posdict[r] == pos_snp)[0] if len(gt_snp) > 1: gt_snp = gt_snp[0] else: pos_snp = list(posdict[r]) for x, y in combinations(popdict.keys(), 2): popX = gtdict[r][popdict[x]] popY = gtdict[r][popdict[y]] sdfst = simData() geno = np.vstack([popX, popY]) geno_fst = geno[:, gt_snp] gtpop = [''.join(str(n) for n in y) for y in geno_fst] gtpop_fst = [i.encode() for i in gtpop] sdfst.assign_sep(pos_snp, gtpop_fst) size = [popX.shape[0], popY.shape[0]] f1 = fst(sdfst, size) if hud: fst_obs.append(f1.hsm()) else: fst_obs.append(f1.slatkin()) # fst_obs.append(f1.hbk()) # # pi for z in popdict.keys(): sdpop = simData() popZ = gtdict[r][popdict[z]] geno = popZ[:, gt_snp] gtpop = [''.join(str(n) for n in y) for y in geno] gtpop_pi = [i.encode() for i in gtpop] sdpop.assign_sep(pos_snp, gtpop_pi) pspopr = polySIM(sdpop) pi = pspopr.thetapi() print("pop {} pi:{}".format(z, pi / L)) fstarray[int(r), :] = fst_obs return (fstarray)