-
Notifications
You must be signed in to change notification settings - Fork 4
/
RFSB_TE_Classifier.py
266 lines (255 loc) · 13 KB
/
RFSB_TE_Classifier.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
#!/usr/bin/env python
############################################################################
##### Transposon Classifier RFSB - part of Transposon Ultimate #############
##### Kevin Riehl ([email protected], 2021) #########################
############################################################################
# @author Kevin Riehl 2020/21 <[email protected]>
# EXAMPLE 1: Predict using the model
# fastaFile = "Transposon_Sequences_PRJEB28388.fa"
# modelFile = "ergebnisTestModel.model"#"models/TE_Classifier_RFSB_models_All_Big.pickle"
# kmerConfigFile = "config/kmers.txt"
# dbFile = "config/RPSTBLASTN_LIB/db_large.pn"
# outputFile = "ergebnisTestPythonPipeline3.txt"
# tempFileA = "testA3.txt"#"testA.txt"
# tempFileB = "testB3.txt"#"testB.txt"
# tempFileC = "testC3.txt"#"testC.txt"
# createFeatures_kmer(fastaFile, kmerConfigFile, tempFileA)
# createFeatures_prot(fastaFile, dbFile, tempFileB, tempFileC)
# classifyTransposons(tempFileA, tempFileC, dbFile, modelFile, fastaFile, outputFile)
# EXAMPLE 2: Train a model
# fastaFile = "G:/CambridgeGenData/TE_DB/ALL_tiny.fasta"
## kmerConfigFile = "config/kmers.txt"
# taxonomyConfigFile = "config/taxonomy.txt"
# dbFile = "config/RPSTBLASTN_LIB/db_large.pn"
# outputFile = "ergebnisTestModel.model"
# eThreshold = 5.0
# labelFile = "G:/CambridgeGenData/TE_DB/ALL_tiny.label"
# tempFileA = "testA2.txt"
# tempFileB = "testB2.txt"
# tempFileC = "testC2.txt"
# createFeatures_kmer(fastaFile, kmerConfigFile, tempFileA)
# createFeatures_prot(fastaFile, dbFile, tempFileB, tempFileC)
# classes, names, classes_parent, classes_sort, levels = loadTaxonomy(labelFile, configFile="")
# trainClassificationModel(classes, names, classes_sort, classes_parent, levels, tempFileA, tempFileC, labelFile, dbFile, eThreshold, outputFile)
## Imports
from RFSB_FeatureGenerator import createFeatures_kmer, createFeatures_prot
from RFSB_ModelManager import classifyTransposons, trainClassificationModel
from RFSB_Evaluator import algorithmEvaluationSummary
from RFSB_TaxonomyLoader import loadTaxonomy
from RFSB_Help import helpExplanations
from datetime import datetime
import os.path
import sys
import os
## Methods
def getArgument(args, title):
for i in range(0, len(args)):
if(args[i].startswith("-"+title)):
if(i<len(args)-1):
return args[i+1]
else:
return ""
return ""
def checkEmptyFile(fastaFile):
f = open(fastaFile, "r")
content = f.readline()
f.close()
return content == ""
## MAIN CODE Examples
# Parameters
args = sys.argv[1:]
mode = getArgument(args, "mode")
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
if(mode=="trainModel"):
# get mandatory params
fastaFile = getArgument(args, "fastaFile")
labelFile = getArgument(args, "labelFile")
outputFile = getArgument(args, "outputModelFile")
if(not os.path.isfile(fastaFile)):
print("ERROR: Fasta File \"",fastaFile,"\" could not be found...EXIT")
if(not os.path.isfile(labelFile)):
print("ERROR: Label File \"",labelFile,"\" could not be found...EXIT")
if(outputFile==""):
print("ERROR: Please specify \"outputModelFile\"...EXIT")
# get optional params
taxonomyConfigFile = getArgument(args, "taxonomyConfigFile")
dbFile = getArgument(args, "proteinDBFile")
kmerConfigFile = getArgument(args, "kmerConfigFile")
eThreshold = getArgument(args, "eThreshold")
tempFolder = getArgument(args, "tempFolder")
delTemp = getArgument(args, "deleteTempFiles")
if(dbFile==""):
dbFile = os.path.join(script_dir,"config","RPSTBLASTN_LIB","db_large.pn")
# dbFile = "config/RPSTBLASTN_LIB/db_large.pn"
if(not os.path.isfile(dbFile)):
print("ERROR: ProteinDB \".pn\" File was not handed over. Standard file was used and could not be found. Please navigate into the folder of \"RFSB_TE_Classifier.py\" and run command again...EXIT")
elif(not os.path.isfile(dbFile)):
print("ERROR: ProteinDB \".pn\" File \"",dbFile,"\" could not be found...EXIT")
if(kmerConfigFile==""):
kmerConfigFile = os.path.join(script_dir,"config","kmers.txt")
# kmerConfigFile = "config/kmers.txt"
if(not os.path.isfile(kmerConfigFile)):
print("ERROR: K-mer configuration file was not handed over. Standard file was used and could not be found. Please navigate into the folder of \"RFSB_TE_Classifier.py\" and run command again...EXIT")
elif(not os.path.isfile(kmerConfigFile)):
print("ERROR: K-mer configuration file \"",dbFile,"\" could not be found...EXIT")
if(not eThreshold==""):
eThreshold = float(eThreshold)
else:
print("[INFO] Parameter \"eThreshold\" not set. Set to default value: '5.0'.")
eThreshold = 5.0
if(tempFolder==""):
if("/" in outputFile):
parts = outputFile.split("/")[:-1]
tempFolder = "/".join(parts)+"/"
else:
tempFolder = ""
if(delTemp==""):
print("[INFO] Parameter \"deleteTempFiles\" not set. Set to default value: 'True'.")
delTemp = True
elif(delTemp=="False" or delTemp=="FALSE"):
delTemp = False
else:
delTemp = True
# check if fasta file is empty
if(checkEmptyFile(fastaFile)):
print("ERORR: mode \"",mode,"\" FASTA File is emtpy...EXIT")
sys.exit(0)
# temporary files
tempFileA = tempFolder+outputFile.split("/")[-1]+".featuresA_"+str(datetime.now()).replace("-","").replace(":","").replace(" ","_").split(".")[0]+".temp"
tempFileB = tempFolder+outputFile.split("/")[-1]+".featuresB_"+str(datetime.now()).replace("-","").replace(":","").replace(" ","_").split(".")[0]+".temp"
tempFileC = tempFolder+outputFile.split("/")[-1]+".featuresC_"+str(datetime.now()).replace("-","").replace(":","").replace(" ","_").split(".")[0]+".temp"
# train model
print("[INFO] Generate kMer features...")
createFeatures_kmer(fastaFile, kmerConfigFile, tempFileA)
print("[INFO] Generate protein features using RPSTBLASTN...")
createFeatures_prot(fastaFile, dbFile, tempFileB, tempFileC)
print("[INFO] Load / Generate taxonomy...")
classes, names, classes_parent, classes_sort, levels = loadTaxonomy(labelFile, configFile=taxonomyConfigFile)
print("[INFO] Train model...")
trainClassificationModel(classes, names, classes_sort, classes_parent, levels, tempFileA, tempFileC, labelFile, dbFile, eThreshold, outputFile)
print("[INFO] Finished...")
# delete temporary files
if(delTemp):
os.remove(tempFileA)
os.remove(tempFileB)
os.remove(tempFileC)
elif(mode=="classify"):
# get mandatory params
fastaFile = getArgument(args, "fastaFile")
outputFile = getArgument(args, "outputPredictionFile")
if(not os.path.isfile(fastaFile)):
print("ERROR: Fasta File \"",fastaFile,"\" could not be found...EXIT")
if(outputFile==""):
print("ERROR: Please specify \"outputPredictionFile\"...EXIT")
# get optional params
modelFile = getArgument(args, "modelFile")
kmerConfigFile = getArgument(args, "kmerConfigFile")
dbFile = getArgument(args, "proteinDBFile")
tempFolder = getArgument(args, "tempFolder")
delTemp = getArgument(args, "deleteTempFiles")
if(not os.path.isfile(modelFile)):
modelFile = os.path.join(script_dir,"models","TE_Classifier_RFSB_models_All_Big.pickle")
# modelFile = "models/TE_Classifier_RFSB_models_All_Big.pickle"
if(not os.path.isfile(modelFile)):
print("ERROR: 'modelFile' was not handed over. Standard file was used and could not be found. Please navigate into the folder of \"RFSB_TE_Classifier.py\" and run command again...EXIT")
if(dbFile==""):
dbFile = os.path.join(script_dir,"config","RPSTBLASTN_LIB","db_large.pn")
# dbFile = "config/RPSTBLASTN_LIB/db_large.pn"
if(not os.path.isfile(dbFile)):
print("ERROR: ProteinDB \".pn\" File was not handed over. Standard file was used and could not be found. Please navigate into the folder of \"RFSB_TE_Classifier.py\" and run command again...EXIT")
elif(not os.path.isfile(dbFile)):
print("ERROR: ProteinDB \".pn\" File \"",dbFile,"\" could not be found...EXIT")
if(kmerConfigFile==""):
kmerConfigFile = os.path.join(script_dir,"config","kmers.txt")
# kmerConfigFile = "config/kmers.txt"
if(not os.path.isfile(kmerConfigFile)):
print("ERROR: K-mer configuration file was not handed over. Standard file was used and could not be found. Please navigate into the folder of \"RFSB_TE_Classifier.py\" and run command again...EXIT")
elif(not os.path.isfile(kmerConfigFile)):
print("ERROR: K-mer configuration file \"",kmerConfigFile,"\" could not be found...EXIT")
if(tempFolder==""):
if("/" in outputFile):
parts = outputFile.split("/")[:-1]
tempFolder = "/".join(parts)+"/"
else:
tempFolder = ""
if(delTemp==""):
print("[INFO] Parameter \"deleteTempFiles\" not set. Set to default value: 'True'.")
delTemp = True
elif(delTemp=="False" or delTemp=="FALSE"):
delTemp = False
else:
delTemp = True
# temporary files
tempFileA = tempFolder+outputFile.split("/")[-1]+".featuresA_"+str(datetime.now()).replace("-","").replace(":","").replace(" ","_").split(".")[0]+".temp"
tempFileB = tempFolder+outputFile.split("/")[-1]+".featuresB_"+str(datetime.now()).replace("-","").replace(":","").replace(" ","_").split(".")[0]+".temp"
tempFileC = tempFolder+outputFile.split("/")[-1]+".featuresC_"+str(datetime.now()).replace("-","").replace(":","").replace(" ","_").split(".")[0]+".temp"
# check if fasta file is empty
if(checkEmptyFile(fastaFile)):
print("ERORR: mode \"",mode,"\" FASTA File is emtpy...EXIT")
sys.exit(0)
# classify TEs
print("[INFO] Generate kMer features...")
createFeatures_kmer(fastaFile, kmerConfigFile, tempFileA)
print("[INFO] Generate Protein features using RPSTBLASTN...")
createFeatures_prot(fastaFile, dbFile, tempFileB, tempFileC)
print("[INFO] Load Model and classify...")
classifyTransposons(tempFileA, tempFileC, dbFile, modelFile, fastaFile, outputFile)
print("[INFO] Finished...")
# delete temporary files
if(delTemp):
os.remove(tempFileA)
os.remove(tempFileB)
os.remove(tempFileC)
elif mode=="evaluate":
# get mandatory params
predLabelFile = getArgument(args, "predLabelFile")
trueLabelFile = getArgument(args, "trueLabelFile")
if(not os.path.isfile(predLabelFile)):
print("ERROR: Prediction Label File \"",predLabelFile,"\" could not be found...EXIT")
if(not os.path.isfile(trueLabelFile)):
print("ERROR: True Label File \"",trueLabelFile,"\" could not be found...EXIT")
# get optional params
printResults = getArgument(args, "printResults")
saveCSV = getArgument(args, "saveResultsCSV")
savePickle = getArgument(args, "saveResultsPickle")
csvFile = getArgument(args, "outputCSVFile")
pickleFile = getArgument(args, "outputPickleFile")
generatePNG = getArgument(args, "generateDiagramPNG")
generateSVG = getArgument(args, "generateDiagramSVG")
diagTitle = getArgument(args, "diagramTitle")
svgFile = getArgument(args, "outputSVGFile")
pngFile = getArgument(args, "outputPNGFile")
if(printResults=="False" or printResults=="FALSE"):
printResults = False
else:
printResults = True
if(saveCSV=="False" or saveCSV=="FALSE"):
saveCSV = False
else:
saveCSV = True
if(savePickle=="True" or savePickle=="true"):
savePickle = True
else:
savePickle = False
if(pickleFile==""):
pickleFile = ".".join(predLabelFile.split(".")[:-1])+"_results.pickle"
if(csvFile==""):
csvFile = ".".join(predLabelFile.split(".")[:-1])+"_results.csv"
if(generatePNG=="" or generatePNG=="True" or generatePNG=="true"):
generatePNG = True
else:
generatePNG = False
if(generateSVG=="" or generateSVG=="True" or generateSVG=="true"):
generateSVG = True
else:
generateSVG = False
if(svgFile==""):
svgFile = ".".join(predLabelFile.split(".")[:-1])+"_diagram.svg"
if(pngFile==""):
pngFile = ".".join(predLabelFile.split(".")[:-1])+"_diagram.png"
# calculate performance
algorithmEvaluationSummary(predLabelFile, trueLabelFile, printResults, saveCSV, savePickle, generatePNG, generateSVG, diagTitle, csvFile, pickleFile, svgFile, pngFile)
elif mode=="help" or mode=="h" or "h" in args or "help" in args or "-h" in args or "-help" in args or "--h" in args or "--help" in args:
helpExplanations()
else:
print("ERORR: mode \"",mode,"\" unknown...EXIT")