代码拉取完成,页面将自动刷新
import numpy as np, os, pandas as pd, sys, warnings
from functools import lru_cache
from rpy2.robjects import r, pandas2ri
pandas2ri.activate()
sys.path.append('.')
warnings.filterwarnings('error', category=RuntimeWarning)
@lru_cache(maxsize=None)
def get_gene_model(gene, ref_panel, weights_dir='fusion_twas/WEIGHTS'):
weight_file = f'{weights_dir}/{ref_panel}/{gene}_500kb.wgt.RDat'
assert os.path.exists(os.path.dirname(weight_file))
if not os.path.exists(weight_file):
print(f'WARNING: weight file {weight_file} missing for gene {gene}! '
f'Removing from the analysis.')
return None
r.load(weight_file)
performance = r['cv.performance'][0]
sorted_order = np.argsort(performance)
best_model_index = sorted_order[-1] # model with highest performance
if best_model_index == 2:
best_model_index = sorted_order[-2] # if top1 is best, take 2nd-best
model_weights = r['wgt.matrix'][:, best_model_index]
if np.isnan(model_weights).all():
print(f'WARNING: Best model for gene {gene} has all-nan weights! '
f'Removing from the analysis.')
return None
if (model_weights == 0).all():
print(f'WARNING: Best model for gene {gene} has all-0 weights! '
f'Removing from the analysis.')
return None
rs_numbers = r.snps['V2'].values
assert len(model_weights) == len(rs_numbers)
model_weights = pd.Series(data=model_weights, index=rs_numbers)
# Remove SNPs with 0 weight (or nan weight)
model_weights = model_weights[model_weights != 0]
return model_weights
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。