forked from HaploKit/DiseaseCapsule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier_PRS.py
209 lines (163 loc) · 6.39 KB
/
classifier_PRS.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
import pickle
import json
import csv
import numpy as np
import os
import sys
import pandas as pd
import random
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import argparse
from sklearn import metrics
from sklearn.linear_model import LogisticRegression
from sklearn import svm
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn import svm
top_k='_p0.05'
vcf='/export/scratch3/vincent/project/als/GWAS2019_NL_QC/qc/rm_batch_snp/vcf/vcf_p0.05/all_chrs.vcf'
#top_k='_p5e-8'
#vcf='/export/scratch3/vincent/project/als/GWAS2019_NL_QC/qc/rm_batch_snp/vcf/vcf_p5e-8/all_chrs.vcf'
method=sys.argv[1]
vcf_df=pd.read_csv(vcf,sep='\t',index_col=2)
labels_file='/export/scratch3/vincent/project/als/analysis/qc/rmbatch/labels.csv'
labels_df = pd.read_csv(labels_file, index_col=0)
sample_ids = labels_df.FID.tolist()
sample_ids[:4]
# the output is a vector for probability in DL
lab_num = {1: [1, 0], # negative, max_idx=0
2: [0, 1]} # positive, max_idx=1
pheno_new = []
for i in labels_df.Pheno.tolist():
pheno_new.append(lab_num[i])
vcf_df = vcf_df.iloc[:,8:]
vcf_df.columns = [a.split('_')[0] for a in vcf_df.columns]
vcf_df=vcf_df[sample_ids]
# mapping dictionary
genotype2num = {"0/0":'0',
"0/1":'1',
"1/0":'1',
"1/1":'2',
"./.":'-1'}
vcf_df.replace(genotype2num,inplace=True)
vcf_df.shape
vcf_df[:3]
dataset_X=np.array(vcf_df).astype('float32').T
# N = dataset_X.shape[1]
dataset_Y = np.array(pheno_new)
dataset_Y = dataset_Y.reshape((len(dataset_Y), dataset_Y.shape[1]))
dataset_X.shape
dataset_Y.shape
dataset_X[:3]
dataset_Y[:3]
for _ in range(9):
for ratio in [0.1,0.3,0.5,0.7,0.9,1.0]:
# ratio=float(sys.argv[2]) #for downsampling train sampels
prefix='lr_none_'+str(ratio) #PRS based
# train dataset
train_idx = [int(line.strip()) for line in open("./train_val.balanced.idx", 'r')]
# train_idx = [int(line.strip()) for line in open("../train_val.unique.idx", 'r')]
# test dataset
te_idx = [int(line.strip()) for line in open("./test.idx", 'r')]
#subsampling
random.seed(123)
random.shuffle(train_idx)
random.shuffle(te_idx)
train_idx = random.sample(train_idx,int(len(train_idx)*ratio))
random.seed(123)
random.shuffle(train_idx)
x_train=[]
y_train=[]
x_test=[]
y_test=[]
x_train=dataset_X[train_idx]
x_test=dataset_X[te_idx]
y_train=dataset_Y[train_idx]
y_test =dataset_Y[te_idx]
x_train.shape
x_test.shape
y_train.shape
y_test.shape
y_train = np.argmax(y_train, axis=1)
y_test_num = np.argmax(y_test, axis=1)
if method=='lr':
#### LR
print("\nrunning LR...\n")
logisticRegr = LogisticRegression(random_state=1991,solver='saga')
logisticRegr.fit(x_train,y_train)
y_pred = logisticRegr.predict(x_test)
y_test_num = np.argmax(y_test, axis=1)
tn, fp, fn, tp = confusion_matrix(y_test_num, y_pred).ravel()
acc = round((tp + tn) * 1. / (tp + fp + tn + fn),3)
ps = round(tp*1./(tp+fp),3)
rc = round(tp*1./(tp+fn),3)
f1=round(2*(ps*rc)/(ps+rc),3)
print('Accuracy:',(tp+tn)*1./(tp+tn+fp+fn))
print("Pression: ", ps)
print("Recall:", rc)
print("F1: ",2*(ps*rc)/(ps+rc))
print("TP={}, TN={}, FP={}, FN={}".format(tp,tn,fp,fn))
with open(prefix+'.out.csv','a') as fw:
fw.write(','.join([prefix]+list(map(str,[ps,rc,f1,acc])))+'\n')
elif method =='rf':
###### predict based top promoters selected by randomforest
print("\nrunning RandomForest...\n")
# parameters are from the previous paper
#clf=RandomForestClassifier(random_state=1991)
clf=RandomForestClassifier(n_estimators=100,max_depth=5,random_state=1991)
# Train the model using the training sets
clf.fit(x_train, y_train)
# Make predictions using the testing set
y_pred = clf.predict(x_test)
y_test_num = np.argmax(y_test, axis=1)
tn, fp, fn, tp = confusion_matrix(y_test_num, y_pred).ravel()
acc = round((tp + tn) * 1. / (tp + fp + tn + fn),3)
ps = round(tp*1./(tp+fp),3)
rc = round(tp*1./(tp+fn),3)
f1=round(2*(ps*rc)/(ps+rc),3)
print('Accuracy:',(tp+tn)*1./(tp+tn+fp+fn))
print("Pression: ", ps)
print("Recall:", rc)
print("F1: ",2*(ps*rc)/(ps+rc))
with open('rf.all_chr.top'+str(top_k)+'.out.csv','w') as fw:
fw.write(','.join(['all_chrs']+list(map(str,[ps,rc,f1,acc])))+'\n')
elif method=='svm':
######## SVM
print("\nrunning SVM...\n")
lsvm = svm.SVC(random_state=1991)
lsvm.fit(x_train,y_train)
y_pred = lsvm.predict(x_test)
y_test_num = np.argmax(y_test, axis=1)
tn, fp, fn, tp = confusion_matrix(y_test_num, y_pred).ravel()
acc = round((tp + tn) * 1. / (tp + fp + tn + fn),3)
ps = round(tp*1./(tp+fp),3)
rc = round(tp*1./(tp+fn),3)
f1=round(2*(ps*rc)/(ps+rc),3)
print('Accuracy:',(tp+tn)*1./(tp+tn+fp+fn))
print("Pression: ", ps)
print("Recall:", rc)
print("F1: ",2*(ps*rc)/(ps+rc))
print("TP={}, TN={}, FP={}, FN={}".format(tp,tn,fp,fn))
with open('svm.all_chr.top'+str(top_k)+'.out.csv','w') as fw:
fw.write(','.join(['all_chrs']+list(map(str,[ps,rc,f1,acc])))+'\n')
elif method=='adab':
######## adab
print("\nrunning AdaBoostClassifier...\n")
clf = AdaBoostClassifier(random_state=1991)
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
y_test_num = np.argmax(y_test, axis=1)
tn, fp, fn, tp = confusion_matrix(y_test_num, y_pred).ravel()
acc = round((tp + tn) * 1. / (tp + fp + tn + fn),3)
ps = round(tp*1./(tp+fp),3)
rc = round(tp*1./(tp+fn),3)
f1=round(2*(ps*rc)/(ps+rc),3)
print('Accuracy:',(tp+tn)*1./(tp+tn+fp+fn))
print("Pression: ", ps)
print("Recall:", rc)
print("F1: ",2*(ps*rc)/(ps+rc))
print("TP={}, TN={}, FP={}, FN={}".format(tp,tn,fp,fn))
with open('adab.all_chr.top'+str(top_k)+'.out.csv','w') as fw:
fw.write(','.join(['all_chrs']+list(map(str,[ps,rc,f1,acc])))+'\n')