Beispiel #1
0
def calc_h2(pheno, prev, eigen, keepArr, covar, numRemovePCs, lowtail):

    pheno = leapUtils._fixup_pheno(pheno)

    #Extract phenotype
    if isinstance(pheno, dict): phe = pheno['vals']
    else: phe = pheno
    if (len(phe.shape) == 2):
        if (phe.shape[1] == 1): phe = phe[:, 0]
        else: raise Exception('More than one phenotype found')
    if (keepArr is None): keepArr = np.ones(phe.shape[0], dtype=np.bool)

    #Compute kinship matrix
    XXT = eigen['XXT']

    #Remove top PCs from kinship matrix
    if (numRemovePCs > 0):
        if (eigen is None): S, U = leapUtils.eigenDecompose(XXT)
        else: S, U = eigen['arr_1'], eigen['arr_0']
        print 'Removing the top', numRemovePCs, 'PCs from the kinship matrix'
        XXT -= (U[:, -numRemovePCs:] * S[-numRemovePCs:]).dot(
            U[:, -numRemovePCs:].T)
    else:
        U, S = None, None

    #Determine if this is a case-control study
    pheUnique = np.unique(phe)
    if (pheUnique.shape[0] < 2):
        raise Exception('Less than two different phenotypes observed')
    isCaseControl = (pheUnique.shape[0] == 2)

    if isCaseControl:
        print 'Computing h2 for a binary phenotype'
        pheMean = phe.mean()
        phe[phe <= pheMean] = 0
        phe[phe > pheMean] = 1
        if (numRemovePCs > 0 or covar is not None):
            probs, thresholds = calcLiabThreholds(U, S, keepArr, phe,
                                                  numRemovePCs, prev, covar)
            h2 = calcH2Binary(XXT, phe, probs, thresholds, keepArr, prev)
        else:
            h2 = calcH2Binary(XXT, phe, None, None, keepArr, prev)
    else:
        if (covar is not None):
            raise Exception(
                'Covariates with a continuous phenotype are currently not supported'
            )
        print 'Computing h2 for a continuous phenotype'
        if (not lowtail): h2 = calcH2Continuous(XXT, phe, keepArr, prev)
        else: h2 = calcH2Continuous_twotails(XXT, phe, keepArr, prev)

    if (h2 <= 0): raise Exception("Negative heritability found. Exitting...")
    if (np.isnan(h2)):
        raise Exception(
            "Invalid heritability estimate. Please double-check your input for any errors."
        )

    print 'h2: %0.6f' % h2
    return h2
Beispiel #2
0
def calc_h2(pheno, prev, eigen, keepArr, covar, numRemovePCs, h2coeff, lowtail):

	pheno = leapUtils._fixup_pheno(pheno)

	#Extract phenotype
	if isinstance(pheno, dict):	phe = pheno['vals']	
	else: phe = pheno		
	if (len(phe.shape)==2):
		if (phe.shape[1]==1): phe=phe[:,0]
		else: raise Exception('More than one phenotype found')		
	if (keepArr is None): keepArr = np.ones(phe.shape[0], dtype=np.bool)
	
	#Compute kinship matrix	
	XXT = eigen['XXT']
	
	#Remove top PCs from kinship matrix
	if (numRemovePCs > 0):
		if (eigen is None): S,U = leapUtils.eigenDecompose(XXT)
		else: S, U = eigen['arr_1'], eigen['arr_0']		
		print 'Removing the top', numRemovePCs, 'PCs from the kinship matrix'
		XXT -= (U[:, -numRemovePCs:]*S[-numRemovePCs:]).dot(U[:, -numRemovePCs:].T)		
	else:
		U, S = None, None
		
	#Determine if this is a case-control study
	pheUnique = np.unique(phe)
	if (pheUnique.shape[0] < 2): raise Exception('Less than two different phenotypes observed')
	isCaseControl = (pheUnique.shape[0] == 2)
	
	if isCaseControl:
		print 'Computing h2 for a binary phenotype'
		pheMean = phe.mean()	
		phe[phe <= pheMean] = 0
		phe[phe > pheMean] = 1
		if (numRemovePCs > 0 or covar is not None):
			probs, thresholds = calcLiabThreholds(U, S, keepArr, phe, numRemovePCs, prev, covar)
			h2 = calcH2Binary(XXT, phe, probs, thresholds, keepArr, prev, h2coeff)
		else: h2 = calcH2Binary(XXT, phe, None, None, keepArr, prev, h2coeff)
	else:
		if (covar is not None): raise Exception('Covariates with a continuous phenotype are currently not supported')
		print 'Computing h2 for a continuous phenotype'
		if (not lowtail): h2 = calcH2Continuous(XXT, phe, keepArr, prev, h2coeff)
		else: h2 = calcH2Continuous_twotails(XXT, phe, keepArr, prev, h2coeff)
		
	if (h2 <= 0): raise Exception("Negative heritability found. Exitting...")	
	if (np.isnan(h2)): raise Exception("Invalid heritability estimate. Please double-check your input for any errors.")	
		
	print 'h2: %0.6f'%h2
	return h2
def eigenDecompose(bed, outFile=None):

	bed = leapUtils._fixupBed(bed)

	#Compute kinship matrix
	t0 = time.time()
	print 'Computing kinship matrix...'	
	XXT = leapUtils.symmetrize(blas.dsyrk(1.0, bed.val, lower=1)) / bed.val.shape[1]
	print 'Done in %0.2f'%(time.time()-t0), 'seconds'

	#Compute eigendecomposition
	S,U = leapUtils.eigenDecompose(XXT)
	if (outFile is not None): np.savez_compressed(outFile, arr_0=U, arr_1=S, XXT=XXT)	
	eigen = dict([])
	eigen['XXT'] = XXT
	eigen['arr_0'] = U
	eigen['arr_1'] = S
	return eigen
Beispiel #4
0
def eigenDecompose(bed, outFile):

    bed = leapUtils._fixupBed(bed)

    # Compute kinship matrix
    t0 = time.time()
    print "Computing kinship matrix..."
    XXT = leapUtils.symmetrize(blas.dsyrk(1.0, bed.val, lower=1)) / bed.val.shape[1]
    print "Done in %0.2f" % (time.time() - t0), "seconds"

    # Compute eigendecomposition
    S, U = leapUtils.eigenDecompose(XXT)
    if outFile is not None:
        np.savez_compressed(outFile, arr_0=U, arr_1=S, XXT=XXT)
    eigen = dict([])
    eigen["XXT"] = XXT
    eigen["arr_0"] = U
    eigen["arr_1"] = S
    return eigen
Beispiel #5
0
def eigenDecompose(bed, outFile=None):

    bed = leapUtils._fixupBed(bed)

    #Compute kinship matrix
    t0 = time.time()
    print 'Computing kinship matrix...'
    XXT = leapUtils.symmetrize(blas.dsyrk(1.0, bed.val,
                                          lower=1)) / bed.val.shape[1]
    print 'Done in %0.2f' % (time.time() - t0), 'seconds'

    #Compute eigendecomposition
    S, U = leapUtils.eigenDecompose(XXT)
    if (outFile is not None):
        np.savez_compressed(outFile, arr_0=U, arr_1=S, XXT=XXT)
    eigen = dict([])
    eigen['XXT'] = XXT
    eigen['arr_0'] = U
    eigen['arr_1'] = S
    return eigen