-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
executable file
·482 lines (445 loc) · 18.1 KB
/
predict.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/env python3
"""
Using trained model and input FASTA file, classify each sequence as a true or false TF
binding site.
"""
__author__ = "Akshay Paropkari"
__version__ = "0.3.5"
import argparse
from itertools import product
from os.path import isfile
from sys import exit
from time import strftime
from joblib import load
from utils import build_feature_table, parse_fasta
err = []
try:
import numpy as np
from numpy.linalg import norm
except ImportError:
err.append("numpy")
try:
import pandas as pd
except ImportError:
err.append("pandas")
try:
import matplotlib as mpl
from matplotlib import pyplot as plt
plt.switch_backend("agg")
except ImportError:
err.append("matplotlib")
try:
assert len(err) == 0
except AssertionError:
for error in err:
print("Please install {0}".format(error))
exit()
def handle_program_options():
parser = argparse.ArgumentParser(
description="Using trained model and an input FASTA file of potential binding "
"sites, predict True vs. False binding sequence from input FASTA file.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"prediction_FASTA",
type=str,
metavar="path/to/sequences_to_predict.fasta",
help="Specify location and name of FASTA file whose sequences "
"are to be classified by the model [REQUIRED]",
)
parser.add_argument(
"prediction_BED",
type=str,
metavar="path/to/entries_to_predict.bed",
help="Specify location and name of BED6 file whose entries "
"are to be classified by the model. This file contains same "
"information as the file for `read_prediction_FASTA` option "
"[REQUIRED]",
)
parser.add_argument(
"fg_fasta_file",
type=str,
metavar="/path/to/true_binding_seqeuences.fasta",
help="Specify location and name of foreground/true positive "
"sequence FASTA file [REQUIRED]",
)
parser.add_argument(
"model_file",
type=str,
metavar="/path/to/model_file.pkl.z",
help="Specify location and name of model pickle file associated "
"with 'protein_name'. This file can(should) be generated using "
"cross_validate.py script [REQUIRED]",
)
parser.add_argument(
"read_training_data",
type=str,
metavar="/path/to/tf_training_dataset.feather",
help="Specify location and name of training data used to build "
"`model_file` feather format file. [REQUIRED]",
)
parser.add_argument(
"protein_name",
type=str,
choices=["bcr1", "brg1", "efg1", "ndt80", "rob1", "tec1"],
help="Specify the name of transcription factor. Please see the "
"list of valid choices for this parameter [REQUIRED]",
)
parser.add_argument(
"genome_wide_shape_fasta_file",
nargs="+",
metavar="/path/to/organism_genome_shape.fasta.*",
help="Path to Candida albicans genome-wide 3D DNA shape "
"(DNAShapeR output files) data single-line FASTA format files "
"[REQUIRED]",
)
parser.add_argument(
"save_feature_table",
type=str,
metavar="/path/to/tf_feature_table.feather",
help="Specify location and name of the file to save the feature table "
"to be used as input for classification. The results will be saved in feather "
"format [REQUIRED]",
)
parser.add_argument(
"prediction_results",
type=str,
metavar="/path/to/true_binding_site_predictions.bed",
help="Specify location and name of the file to save the results "
"of classification prediction. The results will be saved in BED6 "
"format [REQUIRED]",
)
parser.add_argument(
"model_stats",
type=str,
metavar="/path/to/tf_model_stats/folder",
help="Specify location and name of folder to save SVM model statistics "
"like distance of hyperplane and log probability of classification [REQUIRED]",
)
parser.add_argument(
"roc_like_curve",
type=str,
metavar="/path/to/roc_like_curve.pdf",
help="Specify location and name of the file to save the ROC-like curve "
"of positive classification. X-axis is positive TFBS predictions and Y-axis "
"denotes true TFBS [REQUIRED]",
)
return parser.parse_args()
def map_headers_to_values(fasta_header, values) -> dict:
"""
Given equal length lists of FASTA header lines and calculated numerical values,
return a dict mapping using zip()
:type fasta_header: array-like, list or numpy array
:param fasta_header: List or numpy array of FASTA header lines
:type values: array-like, list or numpy array
:param values: List or numpy array of numerical values equal to length of
fasta_header
"""
try:
assert len(fasta_header) == len(values)
except AssertionError as e:
exit(
"Could not create a mapping between FASTA headers and input numerical array."
"\n{0}".format(e)
)
else:
return dict(zip(fasta_header, values))
def all_possible_seq_pairs(list1, fg_seqs):
"""
Get all possible pairs of foreground and background sequences for calculating metrics
and return all possible background-foreground sequence pairs
:type list1: array-like
:param list1: Array of background sequences
:type fg_seqs: array-like
:param fg_seqs: Array of foreground sequences
"""
return (list(product(fg_seqs, [seq])) for seq in list1)
def main():
print("#" * 90, "\n\n", strftime("%x %X | START CLASSIFICATION\n"))
args = handle_program_options()
# Check input validity
try:
assert isfile(args.prediction_FASTA)
assert isfile(args.prediction_BED)
assert isfile(args.fg_fasta_file)
assert isfile(args.model_file)
assert isfile(args.read_training_data)
assert len(set(args.genome_wide_shape_fasta_file)) == 5
except Exception as e:
print("Error with input file(s). Please review the error\n{0}".format(e))
exit()
else:
protein_name = args.protein_name.capitalize()
try:
output_format = args.roc_like_curve.split("/")[-1].split(".")[-1]
assert output_format in ["pdf", "svg", "png", "jpg", "tiff", "eps", "ps"]
except AssertionError:
print(
f"Error: Please check the output file format provided. '{output_format}' format is not supported in {args.roc_like_curve}."
)
######################################
# Read in training data feather file #
######################################
try:
training_data = (
pd.read_feather(args.read_training_data)
.drop(columns=["index"])
.set_index("location", verify_integrity=True)
)
except Exception:
print("Error: Please check input file {0}".format(args.read_training_data))
exit()
else:
# feather file reading successful, collect feature set
# starting at 2, since location is now row index
training_features = list(training_data.columns[2:])
X_train = training_data.iloc[:, 2:].to_numpy()
######################
# Read in shape data #
######################
shape_data = {
file.split(".")[-1]: {
header: seq.strip().split(",") for header, seq in parse_fasta(file)
}
for file in args.genome_wide_shape_fasta_file
}
##########################################################
# Calculating GC content and sequence similarity metrics #
##########################################################
prediction_data_df = build_feature_table(
args.prediction_FASTA, args.fg_fasta_file, shape_data, minhash=True
).set_index("location", verify_integrity=True)
try:
prediction_data_df = prediction_data_df.loc[:, training_features]
except KeyError as ke:
# misalignment of feature space
prediction_data_features = list(prediction_data_df.columns)
print(
strftime(
f"%x %X | Some feature labels were not found in prediction dataset\n{ke}"
)
)
if len(prediction_data_features) > len(training_features):
additional_features = set(prediction_data_features).difference(
training_features
)
print(f"Features model is unaware of -\n{additional_features}")
exit()
else:
missing_features = set(training_features).difference(
prediction_data_features
)
print(f"Missing features -\n{missing_features}")
exit()
else:
print(
strftime(
f"%x %X | Saving feature table (input for model) in {args.save_feature_table}"
)
)
save_predict_data = prediction_data_df.reset_index()
save_predict_data.to_feather(args.save_feature_table)
#################################
# Interpolate / drop NaN values #
#################################
dropped_entries = list(
prediction_data_df[prediction_data_df.isnull().any(axis=1)].index
)
original_row_count = prediction_data_df.shape[0]
prediction_data_df = prediction_data_df.dropna()
lost_entries_pct = 100 * (len(dropped_entries) / original_row_count)
print(
strftime(
"%x %X | Losing {0} ({1:0.2f}%) entries due to prescence of NaNs".format(
len(dropped_entries), lost_entries_pct
)
)
)
prediction_data_features = prediction_data_df.to_numpy()
model = load(args.model_file)
prediction_data_features = (
model["scaler"].fit(X_train).transform(prediction_data_features)
)
X_train_transformed = model["scaler"].fit_transform(X_train)
###################################################
# Classify sequences as either True or False TFBS #
###################################################
print(strftime("%x %X | Classifying sequences"))
clf = model["search"].best_estimator_
try:
pred_results = clf.predict(prediction_data_features)
except Exception as e:
exit("\nError in classification\n{0}".format(e))
else:
positive_pred_orfs = prediction_data_df.index.to_numpy()[
np.where(pred_results)
]
print(
strftime(
f"%x %X | Model predicted {len(positive_pred_orfs):,}{len(positive_pred_orfs) / len(prediction_data_df.index): .2%} positive TFBS hits"
)
)
######################################
# Calculate distance from hyperplane #
######################################
try:
prediction_decision = np.asarray(
clf.decision_function(prediction_data_features) / norm(clf.coef_)
)
training_decision = np.asarray(
clf.decision_function(X_train_transformed) / norm(clf.coef_)
)
except Exception:
print(
f"{'': >20}Error while calculating exact distance from hyperplane. "
"Reverting to using relative distance from hyperplane."
)
prediction_decision = np.asarray(
clf.decision_function(prediction_data_features)
)
training_decision = np.asarray(
clf.decision_function(X_train_transformed)
)
print(
strftime(f"%x %X | Saving SVM model statistics to {args.model_stats}")
)
training_model_stats = pd.DataFrame.from_records(
np.column_stack(
[
training_data.index,
clf.predict(X_train_transformed),
training_decision,
clf.predict_log_proba(X_train_transformed),
]
),
columns=[
"location",
"prediction",
"distance_from_hyperplane",
"log_probability_of_True_TFBS",
"log_probability_of_False_TFBS",
],
).set_index("location", verify_integrity=True)
fnh = f"{args.model_stats}{args.protein_name}_training_model_statistics.txt"
training_model_stats.to_csv(f"{fnh}", sep="\t", na_rep="NA")
prediction_model_stats = pd.DataFrame.from_records(
np.column_stack(
[
prediction_data_df.index,
pred_results,
prediction_decision,
clf.predict_log_proba(prediction_data_features),
]
),
columns=[
"location",
"prediction",
"distance_from_hyperplane",
"log_probability_of_True_TFBS",
"log_probability_of_False_TFBS",
],
).set_index("location", verify_integrity=True)
fnh = (
f"{args.model_stats}{args.protein_name}_prediction_model_statistics.txt"
)
prediction_model_stats.to_csv(f"{fnh}", sep="\t", na_rep="NA")
# Collect positive training and prediction data
positive_pred_data = prediction_decision[
np.where(prediction_decision > 0.0)
]
positive_train_data = training_decision[np.where(training_decision > 0.0)]
# Collect high-confidence positive TFBS prediction
min_dist_true_TFBS_from_hyperplane = min(
training_model_stats["distance_from_hyperplane"][
training_model_stats["prediction"] == 1
].tolist()
)
positive_high_confidence_pred_data = prediction_decision[
np.where(prediction_decision > min_dist_true_TFBS_from_hyperplane)
]
########################################################################
# Plot ROC-like curves for true positive and positively predicted data #
########################################################################
max_dist = max(
np.r_[positive_high_confidence_pred_data, positive_train_data]
)
new_predictions = np.fromiter(
[
np.count_nonzero(positive_high_confidence_pred_data <= dist)
for dist in np.linspace(
min_dist_true_TFBS_from_hyperplane, max_dist, 100
)
],
"int32",
)
# transform new_predictions for cleaner axes
new_predictions = np.sqrt(new_predictions)
new_predictions = np.true_divide(new_predictions, new_predictions.max())
true_predictions = np.fromiter(
[
np.count_nonzero(positive_train_data <= dist)
for dist in np.linspace(
min_dist_true_TFBS_from_hyperplane, max_dist, 100
)
],
"int32",
)
# transform true_predictions for cleaner axes
true_predictions = np.sqrt(true_predictions)
true_predictions = np.true_divide(true_predictions, true_predictions.max())
print(strftime(f"%x %X | Saving ROC-like plot to {args.roc_like_curve}"))
with mpl.style.context("fast"):
plt.figure(figsize=(7, 7), edgecolor="k", tight_layout=True)
plt.step(
new_predictions, true_predictions, lw=2, alpha=1, where="post",
)
plt.fill_between(
new_predictions, true_predictions, alpha=0.5, step="post"
)
plt.xlabel("Positive prediction rate", color="k", size=20)
plt.ylabel("True positives rate", color="k", size=20)
plt.figtext(
0.2,
0.935,
f"{protein_name}",
c="w",
backgroundcolor="k",
size=20,
weight="bold",
ha="center",
va="center",
)
plt.tight_layout()
plt.savefig(
args.roc_like_curve,
dpi=300.0,
format=output_format,
edgecolor="k",
bbox_inches="tight",
pad_inches=0.1,
)
###########################################################
# Save positive predictions in BED format #
# 1.chrom 2.chromStart 3.chromEnd 4.name 5.score 6.strand #
###########################################################
print(
strftime(
f"%x %X | Writing positive prediction results to {args.prediction_results}"
)
)
with open(args.prediction_results, "w") as pred_out:
for genome_loc in positive_pred_orfs:
chrom = genome_loc.strip().split(":")[0]
chromStart = genome_loc.strip().split(":")[1].split("-")[0]
chromEnd = genome_loc.strip().split(":")[1].split("-")[1].split("(")[0]
pred_name = "{0}_TFBS".format(protein_name)
score = "."
strand = genome_loc.strip().split("(")[1][0]
pred_out.write(
"{0}\t{1}\t{2}\t{3}\t{4}\t{5}\n".format(
chrom, chromStart, chromEnd, pred_name, score, strand
)
)
print(strftime("\n%x %X | END CLASSIFICATION\n"), sep="\n")
if __name__ == "__main__":
exit(main())