GWAS power的计算

import math
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from scipy.special import chdtri
from collections import defaultdict
%matplotlib inline
  1. 对于GWAS中power值(statistical power)的计算,用自己的话来说,Power值就是我们在Pvalue<α时(α:通常设为5e-8)显著水平下,原假设(H0)为假,接受备择假设(H1)的概率。
def pchisq(q,df,ncp=0):
    """
    Calculates the cumulative of the chi-square distribution
    """
    from scipy.stats import chi2,ncx2
    if ncp==0:
        result=chi2.cdf(x=q,df=df,loc=0,scale=1)
    else:
        result=ncx2.cdf(x=q,df=df,nc=ncp,loc=0,scale=1)
    return result

def qchisq(p,df,ncp=0):
    """
    Calculates the quantile function of the chi-square distribution
    """
    from scipy.stats import chi2,ncx2
    if ncp==0:
        result=chi2.ppf(q=p,df=df,loc=0,scale=1)
    else:
        result=ncx2.ppf(q=p,df=df,nc=ncp,loc=0,scale=1)
    return result
def get_power(MAF, beta_alt, count, pvalue=5e-8):
#     MAF = 0.5
#     beta_alt = 0.2
#     count = 2000
    sigma = math.sqrt(1 - 2*MAF*(1-MAF)*beta_alt**2) # error sd after SNP effect is accounted for (see next part for explanation)
    ses = sigma/math.sqrt(count*2*MAF*(1-MAF)) 
    # q_thresh = scipy.stats.chi2.ppf(q= 5e-8, df = 1)
    q_thresh = qchisq(1-pvalue, 1)
    pwr = 1- pchisq(q_thresh, 1, (beta_alt/ses)**2) 
    return pwr
(1)给定效应值(Effect size,beta)为 1.2,计算在不同 MAF 和样本量下它所对应的 Power,并画出图。这里的 MAF 分为五个档位,突变频率从小到大分别是:0.01, 0.05, 0.1,0.15 和 0.2。把结果画在同一个图中,X 轴为样本量,Y 轴为 Power,画图时每一个 MAF 对应一条线。alpha 的阈值都定为 5e-8
# 计算在指定sample数据下,每个beta值对应的power值,并用字典保存
beta_value = np.arange(0, 1.2, 0.01)
count = 2000
pwr_dict=defaultdict(dict)
for maf in [0.01, 0.05, 0.1, 0.15 ,0.2]:
    pwr_dict[maf]=defaultdict(list)
    for beta in beta_value:
        pwr_dict[maf][beta]=get_power(maf, beta, count)
# 字典用pandas转为dataframe,再画图
df = pd.DataFrame(pwr_dict)
df.plot()
plt.title("Sample = 2000",fontsize=15)
plt.xlabel("Beta",fontsize=13)
plt.ylabel("Statistical Power",fontsize=13)
plt.legend(title = 'MAF')

GWAS power的计算_第1张图片

(2)给定样本量(sample size)为 2000,计算在不同 MAF 和效应值(Effect size, beta)下它所对应的 Power,并画出图。这里的 MAF 分为五个档位,突变频率从小到大分别是:0.01, 0.05, 0.1,0.15 和 0.2。把结果画在同一个图中,X 轴为效应值,Y 轴为 Power,画图时每一个 MAF 对应一条线。alpha 的阈值都定为 5e-8。
# 计算在指定sample数据下,每个beta值对应的power值,并用字典保存
beta_value = 0.2
count = np.arange(1, 4000, 10)
pwr_dict=defaultdict(dict)
for maf in [0.01, 0.05, 0.1, 0.15 ,0.2]:
    pwr_dict[maf]=defaultdict(list)
    for cnt in count:
        pwr_dict[maf][cnt]=get_power(maf, beta_value, cnt)
# 字典用pandas转为dataframe,再画图
df = pd.DataFrame(pwr_dict)
df.plot()
plt.title("Beta = 0.2",fontsize=15)
plt.xlabel("Sample Count",fontsize=13)
plt.ylabel("Statistical Power",fontsize=13)
plt.legend(title = 'MAF')

GWAS power的计算_第2张图片


你可能感兴趣的:(生信,python)