-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_pheno.py
43 lines (30 loc) · 1.71 KB
/
generate_pheno.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import argparse
import json
import os
import pandas as pd
import constants
if __name__=="__main__":
parser = argparse.ArgumentParser(description='args')
parser.add_argument('-pop', '--pop_path', dest='pop_path', help='', default=os.path.join(constants.DATASETS_PATH, "ukbb","pop.panel"))
parser.add_argument('-pheno', '--pheno_path', dest='pheno_path', help="", default=os.path.join(constants.DATASETS_PATH, "ukbb","pheno"))
parser.add_argument('-f', '--filters', dest='filters', default='{"pop": ["AFR"]}', help='') # UR,BEB,PJL,MSL,ASW,ACB,CEU
parser.add_argument('-s', '--suffix', dest='suffix', default="AFR", help='') # UR,BEB,PJL,MSL,ASW,ACB,CEU
parser.add_argument('-t', '--target', dest='target', default="ukbb", help='') # UR,BEB,PJL,MSL,ASW,ACB,CEU
args = parser.parse_args()
pop_path=args.pop_path
pheno_path = args.pheno_path
suffix = args.suffix
filters=json.loads(args.filters)
target = args.target
df_pop=pd.read_csv(pop_path, sep='\t', index_col=0)
df_pheno=pd.read_csv(pheno_path, sep='\t', index_col=0)
df_agg=pd.concat([df_pop, df_pheno], axis=1)
auto_suffix=""
for field, value_set in filters.items():
df_agg=df_agg[df_agg.loc[:,field].isin(value_set)]
auto_suffix+=f"{field}={value_set}&"
df_agg=df_agg.dropna()
suffix=(auto_suffix[:-1] if suffix is None else suffix)
df_agg=df_agg.loc[:,~df_agg.columns.duplicated()]
df_agg.loc[:,["IID", "label"]].to_csv(os.path.join(constants.DATASETS_PATH, target, f"pheno_{suffix}"), sep='\t', index_label="FID")
df_agg.loc[:, ["IID", "super_pop", "pop"]].to_csv(os.path.join(constants.DATASETS_PATH, target, f"pop_{suffix}.panel"), sep='\t', index_label="FID")