-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_xgb_svm_gbc_sgd.py
221 lines (168 loc) · 7.1 KB
/
test_xgb_svm_gbc_sgd.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
209
210
211
212
213
214
215
216
217
218
219
220
import pandas as pd
import numpy as np
from math import exp
import seaborn as sns
import glob
import os
#import pybedtools
#import pyBigWig
#import pysam
pd.set_option('display.max_colwidth', -1)
import matplotlib.pyplot as plt
import xgboost as xgb
from XGB_TFBSContext import *
shape_path = "/home/ckibet/lustre/Dream_challenge/DNAShape"
human_genome = "/home/ckibet/lustre/Dream_challenge/annotations"
chipseq_path = "/home/ckibet/lustre/XGB-TFBSContext/Data/Downloaded"
dn_hg_dict, kmer_name = get_kmer_dict_rev("%s/Data/dn_hg_max_normalized.txt" % BASE_DIR, "test")
hg_dn_dict, kmer_name = get_kmer_dict_rev("%s/Data/hg_dn_backround_noise_minmax.txt" % BASE_DIR, "test")
def get_feature_df(tf, pos):
"""
Given a TF and the position of the peak file of interest
Creat a DataFrame with all the coordinates
This is the main Feature Vector
"""
peak_files = get_peak_files(tf)
combined_bed, trim_to = get_combined_bed(peak_files[pos])
E_score_dict, kmer_name = get_contigmers_dict(get_contigmers(tf)[0],"test")
## Calculate all the necessary features
#E_score_combined = get_kmer_score(combined_bed, sum_kmer_score, E_score_dict)
feature_frame = pd.DataFrame()
feature_frame["sum_kmer_score"] = get_kmer_score(combined_bed, sum_kmer_score, E_score_dict)
feature_frame ["max_kmer_score"] = get_kmer_score(combined_bed, max_score_kmer, E_score_dict)
test_score = get_kmer_score(combined_bed, max_score_kmer_pos, E_score_dict)
double_deal = test_score.apply(pd.Series)
feature_frame ["max_kmer_score_pos"] = double_deal[0]
hits_df = get_hits_df(double_deal, combined_bed)
feature_frame["dnase"] = apply_get_max_dnase(hits_df)
feature_frame["phatsCons"] = apply_get_phatscon(hits_df)
feature_frame["phyloP100way"] = apply_get_phatscon(hits_df, "phyloP100way")
feature_frame["dn_hg_score"] = get_kmer_score(combined_bed, max_score_kmer, dn_hg_dict)
feature_frame["hg_dn_score"] = get_kmer_score(combined_bed, max_score_kmer, hg_dn_dict)
# feature_frame["pwm_score"] = get_kmer_score(combined_bed, energyscore, get_motif_details(tf))
feature_frame.reset_index(drop=True, inplace=True)
pos_tss = get_distance_to_tss(hits_df.head(trim_to))
neg_tss = get_distance_to_tss(hits_df.tail(trim_to))
pos_neg_tss = pos_tss.append(neg_tss)
pos_neg_tss.reset_index(drop=True, inplace=True)
feature_frame["tss_dist"] = pos_neg_tss
for shape in "ProT MGW HelT Roll".split():
#feature_frame["%s_shape" % shape] = apply_get_shape(hits_df, shape)
feature_fr = apply_get_full_shape(hits_df).apply(pd.Series)
feature_fr.columns = get_shape_names(shape)
feature_frame = feature_frame.T.append(feature_fr.T).T
return feature_frame, trim_to
def pop_this(feat):
try:
all_feats.pop(all_feats.index(feat))
except ValueError:
try:
for i in range(8):
all_feats.pop(all_feats.index(feat+"_%i" % i))
except ValueError:
pass
from sklearn import svm, grid_search
from sklearn.linear_model import SGDClassifier
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
#For partitioning the data
from sklearn.cross_validation import train_test_split
from sklearn.cross_validation import cross_val_score, KFold
#Libsvm format data loading
from sklearn.datasets import load_svmlight_file
#Accuracy metrics
from sklearn.metrics import accuracy_score, classification_report, auc
# Creating an learning pipeline
from sklearn.pipeline import Pipeline
from sklearn import feature_selection
from sklearn.externals import joblib
from xgboost import XGBClassifier
import xgboost as xgb
from sklearn.preprocessing import StandardScaler, MinMaxScaler
def train_sgd(feature_frame, feature_frame_p, y_train, y_test):
scaler = MinMaxScaler()
#Scale the train data
scaler.fit(feature_frame)
X_train = scaler.transform(feature_frame)
#Scale the test data as well
scaler.fit(feature_frame_p)
X_test = scaler.transform(feature_frame_p)
clf = SGDClassifier()
clf.fit(X_train, y_train)
pred_sgd = clf.predict(X_test)
return roc_auc_score(y_test, pred_sgd)
def train_svm(feature_frame, feature_frame_p, y_train, y_test):
scaler = MinMaxScaler()
#Scale the train data
scaler.fit(feature_frame)
X_train = scaler.transform(feature_frame)
#Scale the test data as well
scaler.fit(feature_frame_p)
X_test = scaler.transform(feature_frame_p)
clf = svm.SVC()
clf.fit(X_train, y_train)
pred_svm = clf.predict(X_test)
return roc_auc_score(y_test, pred_svm)
def train_xgb(feature_frame,feature_frame_p,y_train, y_test):
xgdmat = xgb.DMatrix(feature_frame, y_train)
our_params = {'eta': 0.3, 'seed':0, 'subsample': 1, 'colsample_bytree': 1,
'objective': 'binary:logistic', 'max_depth':6, 'min_child_weight':1}
my_model = xgb.train(our_params,xgdmat)
testdmat = xgb.DMatrix(feature_frame_p, y_test)
y_pred = my_model.predict(testdmat)
return roc_auc_score(y_test, y_pred)
def train_gradient(feature_frame,feature_frame_p,y_train, y_test):
clf = GradientBoostingClassifier()
clf.fit(feature_frame, y_train)
pred_sgd = clf.predict(feature_frame_p)
return roc_auc_score(y_test, pred_sgd)
feat_list = ['max_kmer_score','dnase','sum_kmer_score',"phatsCons",
'Roll', 'ProT', 'MGW', 'HelT',
'max_kmer_score_pos','dn_hg_score',
'hg_dn_score',"tss_dist", "phyloP100way"]
## test for feature importance by leav one out elimination
tf_in_pbm_chip = ['Ap2',
'Arid3a',
'Egr1',
'Elk1',
'Elk4',
'Ets1',
'Gabp',
'Gata3',
'Gr',
'Hnf4a',
'Irf3',
'Jund',
'Mafk',
'Max',
'Pou2f2',
'Rxra',
'Sp1',
'Srf',
'Tbp',
'Tcf7l2']
with open("%s/Results/test.txt" % BASE_DIR, "w") as tf_scores:
tf_scores.write("Tf_name\t")
for j in "sgd, svms, xgboost, gradient".split():
tf_scores.write("%s\t" % j)
for tf in tf_in_pbm_chip:
tf_scores.write("\n%s\t" % tf)
#tf_feats.write("\n%s\t" % tf)
print tf
pybedtools.cleanup()
feature_frame, trim_to = get_feature_df(tf, 0)
feature_frame_p,trim_to_p = get_feature_df(tf, -1)
y_train = np.concatenate((np.ones(trim_to), np.zeros(trim_to)), axis=0)
y_test = np.concatenate((np.ones(trim_to_p), np.zeros(trim_to_p)), axis=0)
#all_feats = list(feature_frame.columns)
#All
# my_model = train_xgboost(feature_frame[all_feats], y_train, tf)
# testdmat = xgb.DMatrix(feature_frame_p[all_feats], y_test)
# y_pred = my_model.predict(testdmat)
feature_frame = feature_frame.fillna(0)
feature_frame_p = feature_frame_p.fillna(0)
sgd = train_sgd(feature_frame, feature_frame_p, y_train, y_test)
svms = train_svm(feature_frame, feature_frame_p, y_train, y_test)
xgboost = train_xgb(feature_frame, feature_frame_p, y_train, y_test)
gradient = train_gradient(feature_frame, feature_frame_p, y_train, y_test)
for mod in [sgd, svms, xgboost, gradient]:
tf_scores.write("%.4f\t" % mod)