def test_permutation(self): #test permutation function for dn in self.datasets: D = data.load(os.path.join(self.dir_name, dn)) perm = SP.random.permutation(D['X'].shape[0]) #1. set permuattion lmm = limix.CLMM() lmm.setK(D['K']) lmm.setSNPs(D['X']) lmm.setCovs(D['Cov']) lmm.setPheno(D['Y']) if 1: #pdb.set_trace() perm = SP.array( perm, dtype='int32' ) #Windows needs int32 as long -> fix interface to accept int64 types lmm.setPermutation(perm) lmm.process() pv_perm1 = lmm.getPv().ravel() #2. do by hand lmm = limix.CLMM() lmm.setK(D['K']) lmm.setSNPs(D['X'][perm]) lmm.setCovs(D['Cov']) lmm.setPheno(D['Y']) lmm.process() pv_perm2 = lmm.getPv().ravel() D2 = (SP.log10(pv_perm1) - SP.log10(pv_perm2))**2 RV = SP.sqrt(D2.mean()) self.assertTrue(RV < 1E-6)
def estimateHeritabilities(self, K, verbose=False): """ estimate variance components and fixed effects from a single trait model having only two terms """ # Fit single trait model varg = SP.zeros(self.P) varn = SP.zeros(self.P) fixed = SP.zeros((1, self.P)) for p in range(self.P): y = self.Y[:, p:p + 1] lmm = limix.CLMM() lmm.setK(K) lmm.setSNPs(SP.ones((K.shape[0], 1))) lmm.setPheno(y) lmm.setCovs(SP.zeros((K.shape[0], 1))) lmm.setVarcompApprox0(-20, 20, 1000) lmm.process() delta = SP.exp(lmm.getLdelta0()[0, 0]) Vtot = SP.exp(lmm.getLSigma()[0, 0]) varg[p] = Vtot varn[p] = delta * Vtot fixed[:, p] = lmm.getBetaSNP() if verbose: print p sth = {} sth['varg'] = varg sth['varn'] = varn sth['fixed'] = fixed return sth
def simple_lmm(snps, pheno, K=None, covs=None, test='lrt', NumIntervalsDelta0=100, NumIntervalsDeltaAlt=0, searchDelta=False): """ Univariate fixed effects linear mixed model test for all SNPs Args: snps: [N x S] SP.array of S SNPs for N individuals pheno: [N x 1] SP.array of 1 phenotype for N individuals K: [N x N] SP.array of LMM-covariance/kinship koefficients (optional) If not provided, then linear regression analysis is performed covs: [N x D] SP.array of D covariates for N individuals test: 'lrt' for likelihood ratio test (default) or 'f' for F-test NumIntervalsDelta0: number of steps for delta optimization on the null model (100) NumIntervalsDeltaAlt:number of steps for delta optimization on the alt. model (0 - no optimization) searchDelta: Carry out delta optimization on the alternative model? if yes We use NumIntervalsDeltaAlt steps Returns: limix LMM object """ t0 = time.time() if K is None: K = SP.eye(snps.shape[0]) lm = limix.CLMM() lm.setK(K) lm.setSNPs(snps) lm.setPheno(pheno) if covs is None: covs = SP.ones((snps.shape[0], 1)) lm.setCovs(covs) if test == 'lrt': lm.setTestStatistics(0) elif test == 'f': lm.setTestStatistics(1) else: print test raise NotImplementedError("only f or lrt are implemented") #set number of delta grid optimizations? lm.setNumIntervals0(NumIntervalsDelta0) if searchDelta: lm.setNumIntervalsAlt(NumIntervalsDeltaAlt) else: lm.setNumIntervalsAlt(0) lm.process() t1 = time.time() print("finished GWAS testing in %.2f seconds" % (t1 - t0)) return lm
def test_exceptions(self): D = data.load(os.path.join(self.dir_name, self.datasets[0])) lmm = limix.CLMM() N = 100 K = SP.eye(N) X = SP.randn(N, 100) Y = SP.randn(N + 1, 1) Cov = SP.randn(N, 1) lmm.setK(K) lmm.setSNPs(X) lmm.setCovs(Cov) lmm.setPheno(Y) try: lmm.process() except Exception, e: self.assertTrue(1 == 1) pass
def _getH2singleTrait(self, K, verbose=None): """ Internal function for parameter initialization estimate variance components and fixed effect using a linear mixed model with an intercept and 2 random effects (one is noise) Args: K: covariance matrix of the non-noise random effect term """ verbose = limix.getVerbose(verbose) # Fit single trait model varg = sp.zeros(self.P) varn = sp.zeros(self.P) fixed = sp.zeros((1, self.P)) for p in range(self.P): y = self.Y[:, p:p + 1] # check if some sull value I = sp.isnan(y[:, 0]) if I.sum() > 0: y = y[~I, :] _K = K[~I, :][:, ~I] else: _K = copy.copy(K) lmm = limix.CLMM() lmm.setK(_K) lmm.setSNPs(sp.ones((y.shape[0], 1))) lmm.setPheno(y) lmm.setCovs(sp.zeros((y.shape[0], 1))) lmm.setVarcompApprox0(-20, 20, 1000) lmm.process() delta = sp.exp(lmm.getLdelta0()[0, 0]) Vtot = sp.exp(lmm.getLSigma()[0, 0]) varg[p] = Vtot varn[p] = delta * Vtot fixed[:, p] = lmm.getBetaSNP() if verbose: print p sth = {} sth['varg'] = varg sth['varn'] = varn sth['fixed'] = fixed return sth
def test_lmm1(self): """basic test, comapring pv""" for dn in self.datasets: D = data.load(os.path.join(self.dir_name, dn)) lmm = limix.CLMM() lmm.setK(D['K']) lmm.setSNPs(D['X']) lmm.setCovs(D['Cov']) lmm.setPheno(D['Y']) lmm.process() pv = lmm.getPv().ravel() BetaSte = lmm.getBetaSNPste().ravel() Beta = lmm.getBetaSNP() D2pv = ((SP.log10(pv) - SP.log10(D['pv']))**2) D2Beta = (Beta - D['Beta'])**2 D2BetaSte = (BetaSte - D['BetaSte'])**2 RV = SP.sqrt(D2pv.mean()) < 1E-6 RV = RV & (D2Beta.mean() < 1E-6) RV = RV & (D2BetaSte.mean() < 1E-6) self.assertTrue(RV)
def test_lmm1(self): """basic test, comapring pv""" for dn in self.datasets: D = data.load(os.path.join(self.dir_name, dn)) #make vllarg X. This needs to be changed later NL = 1000 self.NL = NL X = SP.tile(D['X'], (1, self.NL)) lmm = limix.CLMM() lmm.setK(D['K']) lmm.setSNPs(X) lmm.setCovs(D['Cov']) lmm.setPheno(D['Y']) lmm.process() pv = lmm.getPv().ravel() BetaSte = lmm.getBetaSNPste().ravel() Beta = lmm.getBetaSNP() D2pv = (SP.log10(pv) - SP.log10(SP.tile(D['pv'], self.NL))**2) D2Beta = (Beta - SP.tile(D['Beta'], self.NL))**2 D2BetaSte = (BetaSte - SP.tile(D['BetaSte'], self.NL))**2 RV = SP.sqrt(D2pv.mean()) < 1E-6 RV = RV & (D2Beta.mean() < 1E-6) RV = RV & (D2BetaSte.mean() < 1E-6) self.assertTrue(RV)
print "needed %.2f seconds for variance component learning" % (t1 - t0) #get optmized variance component Ke = GP['covar_E'].K() K = GP['covar'].K() #variance components are in here: params_E = GP['covar_E'].getParams() params_G = GP['covar_G'].getParams() K0 = SP.eye(K.shape[0]) #1. Main effect #covariates C = SP.ones([2 * N, 1]) lmm = limix.CLMM() lmm.setK(scale_k(K)) lmm.setSNPs(Xf) lmm.setPheno(Y) #covariates: column of ones lmm.setCovs(C) #EmmaX mode with useful default settings lmm.setEMMAX() t0 = time.time() lmm.process() t1 = time.time() pv = lmm.getPv().flatten() print "neede %.2f seconds for GWAS scan" % (t1 - t0) #comparison with standard Kpop lmm.setK(Kpopf)