-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestingPyCharm_NCBI_BLAST_filtering.py
307 lines (208 loc) · 10.8 KB
/
TestingPyCharm_NCBI_BLAST_filtering.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
#READ ME FILE
#REQUIREMENTS
#INSTALL BLAST+ "https://ftp.ncbi.nlm.nih.gov/blast/executables/blast+/LATEST/"
# if you have windows select "ncbi-blast-2.10.0+-x64-win64.tar.gz"
#To run this script you need to provide the following handles in the command line:
#1. CALL THE PROGRAM
#2. SELECT THE PATH TO YOUR DATABASE -a ~PATH and input BLAST
#4. SELECT THE OUTPUT PATH OR THE LOCATION YOU WANT YOUR BLAST -b ~PATH\output_Location\
#5. CHOOSE A NAME FOR YOUR BLAST OUTPUT FILE -c Name of your output file. e.g. BLAST_OUTPUT.txt
#10. SELECT YOUR PERCENTAGE OVERLAP. DEFAULT IS 80%.IF YOU ARE UNSURE DON'T USE THIS PARAMETRE -d 0.8
#11. SELECT YOUR BITSCORE VALUE, DEFAULT IS 50. IF YOU ARE UNSURE DON'T USE THIS PARAMETRE -e 50
#THE COMMAND LINE USED FOR BLAST MUST HAVE THE FOLLOWING PARAMETRES FOR THIS SCRIPT TO WORK!
#blastn
#-task dc-megablast
#-db
#-query
#-out
# -word_size
#-perc_identity
#-num_threads
# -outfmt ' 6 qseqid sacc stitle qseq sseq nident mismatch pident length evalue bitscore qstart qend sstart send gapopen gaps qlen slen"')
import sys, getopt
import os
def main(argv):
a_BLAST_input_path_and_file_ = ""
b_Output_Path_= ""
c_Output_file_name = "_BLAST"
#FIlTERING PARAMETRES
d_Percentage_overlap = 0.8
e_bitscore = 50
try:
opts, args = getopt.getopt(argv,"a:b:c:d:e:",["a_BLAST_input_path_and_file_=",
"b_Output_Path_=",
"c_Output_file_name=",
"d_Percentage_overlap=",
"e_bitscore=",
])
except getopt.GetoptError:
print ("ERROR:")
print ('CHECK YOUR INPUT HANDLES')
print("NCBI_BLAST_filtering.py -a <a_BLAST_input_path_and_file_> -b <b_Output_Path_> -c <c_Output_file_name> -d <d_Percentage_overlap> -e <e_bitscore> ")
sys.exit(2)
for opt, arg in opts:
if opt in ( '-h'):
print ("HELP")
print ('NCBI_BLAST_filtering.py -a <a_BLAST_input_path_and_file_> -b <b_Output_Path_> -c <c_Output_file_name> -d <d_Percentage_overlap> -e <e_bitscore>')
sys.exit()
elif opt in ("-a", "--a_BLAST_input_path_and_file_"):
a_BLAST_input_path_and_file_ = arg
elif opt in ("-b", "--b_Output_Path_"):
b_Output_Path_ = arg
elif opt in ("-c", "--c_Output_file_name"):
c_Output_file_name = arg
elif opt in ("-d", "--d_Percentage_overlap"):
d_Percentage_overlap = float(arg)
elif opt in ("-e", "--e_bitscore"):
e_bitscore = int(arg)
print('\n')
print("Percentage Overlap")
print(d_Percentage_overlap)
print('\n')
print("Bitscore")
print(e_bitscore)
print('\n')
###################################################################################################################################
#FILTERING BLAST OUTPUTFILE
#BLAST FILTERING PARAMETRES
qseqid = 0
sacc = 1
stitle = 2
qseq = 3
sseq = 4
nident = 5
mismatch = 6
pident = 7
length = 8
evalue = 9
bitscore = 10
qstart = 11
qend = 12
sstart = 13
send = 14
gapopen = 15
gaps = 16
qlen = 17
slen = 18
PercentageOverlapINT = 19
BLAST_OUTPUT_FILE = a_BLAST_input_path_and_file_
file = open(BLAST_OUTPUT_FILE, 'r')
filtered_file = b_Output_Path_ + c_Output_file_name + "_filtered.txt"
filtered_files = open (filtered_file, "w+")
#headers
AllHeadersFromFilteredFile = ""
AllHeadersFromFilteredFile = ( "qseqid" + '\t' + "sacc" + '\t' + "stitle" + '\t' +
"qseq" + '\t' + "sseq" + '\t' + "nident" + '\t' + "mismatch"+ '\t' +
"pident" + '\t' + "length" + '\t' + "evalue"+ '\t' +
"bitscore" + '\t' + "qstart" + '\t' + "qend" + '\t' + "sstart" + '\t' +
"send" + '\t' + "gapopen" + '\t' + "gaps" + '\t' +
"qlen"+ '\t' + "slen" + '\t' + "PercentageOverlap" + '\n')
filtered_files.write(AllHeadersFromFilteredFile)
#Reading files
tempstring = "temp"
while tempstring:
tempstring = file.readline()
if tempstring == "":
break
templine = tempstring.splitlines()
x = templine[0]
rowlist= x.split('\t')
columns = (rowlist[qseqid] + '\t' + rowlist[sacc] + '\t' + rowlist[stitle]+ '\t' +
rowlist[qseq]+ '\t' + rowlist[sseq]+ '\t' + rowlist[nident]+ '\t' + rowlist[mismatch]+ '\t' +
rowlist[pident] + '\t' + rowlist[length] + '\t' + rowlist[evalue]+ '\t' +
rowlist[bitscore]+ '\t' + rowlist[qstart]+ '\t' + rowlist[qend]+ '\t' + rowlist[sstart]+ '\t' +
rowlist[send] + '\t' + rowlist[gapopen] + '\t' + rowlist[gaps]+ '\t' +
rowlist[qlen] + '\t' + rowlist[slen] + '\t')
Querylength = int(rowlist[qlen])
Length = int(rowlist[length])
SubjectLength = int(rowlist[slen])
min_length = min(Querylength, SubjectLength)
PercentageOverlap = (Length / min_length)
rowlist.append(str(PercentageOverlap))
columns = (rowlist[qseqid] + '\t' + rowlist[sacc] + '\t' + rowlist[stitle]+ '\t' +
rowlist[qseq]+ '\t' + rowlist[sseq]+ '\t' + rowlist[nident]+ '\t' + rowlist[mismatch]+ '\t' +
rowlist[pident] + '\t' + rowlist[length] + '\t' + rowlist[evalue]+ '\t' +
rowlist[bitscore]+ '\t' + rowlist[qstart]+ '\t' + rowlist[qend]+ '\t' + rowlist[sstart]+ '\t' +
rowlist[send] + '\t' + rowlist[gapopen] + '\t' + rowlist[gaps]+ '\t' +
rowlist[qlen] + '\t' + rowlist[slen] + '\t' + rowlist[PercentageOverlapINT] + '\n' )
#FILTERING STEP 1 <<<<< DEFAULT "Percentage overlap >80% or 0.8" >>>>> AND <<<<< DEFAULT "BitScore >50" >>>>>
#HANDLES
if float(rowlist[PercentageOverlapINT]) >= d_Percentage_overlap:
if float(rowlist[bitscore]) >= e_bitscore:
filtered_files.write(columns)
file.close()
filtered_files.close()
#TO BE CHECKED
filtered_files_2 = open(filtered_file,'r')
###################################################################################################################################
#####################################################PART 3
#FILTERING STEP 2
filter_part2_path_and_name = b_Output_Path_ + c_Output_file_name + "_sorted.txt"
filtered_files_part2= open (filter_part2_path_and_name, "w")
#headers
AllHeadersFromFilteredFile = ""
AllHeadersFromFilteredFile = ( "qseqid" + '\t' + "sacc" + '\t' + "stitle" + '\t' +
"qseq" + '\t' + "sseq" + '\t' + "nident" + '\t' + "mismatch"+ '\t' +
"pident" + '\t' + "length" + '\t' + "evalue"+ '\t' +
"bitscore" + '\t' + "qstart" + '\t' + "qend" + '\t' + "sstart" + '\t' +
"send" + '\t' + "gapopen" + '\t' + "gaps" + '\t' +
"qlen"+ '\t' + "slen" + '\t' + "PercentageOverlap" + '\n')
filtered_files_part2.write(AllHeadersFromFilteredFile)
#Reading files
lst_lst = []
counter = 0
tempstring = "temp"
while tempstring:
tempstring = filtered_files_2.readline()
if tempstring == "":
break
if counter != 0:
templine = tempstring.splitlines()
x = templine[0]
rowlist_2= x.split('\t')
lst_lst.append(rowlist_2)
columns = (rowlist_2[qseqid] + '\t' + rowlist_2[sacc] + '\t' + rowlist_2[stitle]+ '\t' +
rowlist_2[qseq]+ '\t' + rowlist_2[sseq]+ '\t' + rowlist_2[nident]+ '\t' + rowlist_2[mismatch]+ '\t' +
rowlist_2[pident] + '\t' + rowlist_2[length] + '\t' + rowlist_2[evalue]+ '\t' +
rowlist_2[bitscore]+ '\t' + rowlist_2[qstart]+ '\t' + rowlist_2[qend]+ '\t' + rowlist_2[sstart]+ '\t' +
rowlist_2[send] + '\t' + rowlist_2[gapopen] + '\t' + rowlist_2[gaps]+ '\t' +
rowlist_2[qlen] + '\t' + rowlist_2[slen] + '\t' + rowlist_2[PercentageOverlapINT] + '\n' )
counter += 1
#READ THE NEW FILE AND ENTER THE LOOP
#SORTING
list.sort(lst_lst, key = lambda DataRow_0: float(DataRow_0[pident]), reverse=True)
list.sort(lst_lst, key = lambda DataRow_2: float(DataRow_2[PercentageOverlapINT]), reverse=True)
list.sort(lst_lst, key = lambda DataRow_1: float(DataRow_1[bitscore]), reverse=True)
list.sort(lst_lst, key = lambda DataRow_3: DataRow_3[qseqid])
Dictionary_lst_lst = {}
#Reading list_list
length = len(lst_lst)
for i in range(length):
temp_rowlist = lst_lst[i]
temp_rowlist_length = len(temp_rowlist)
if temp_rowlist_length != 20:
print("length of tem_row_list_is:")
print(temp_rowlist_length)
continue
row_string_for_output = ""
Variable_QSeqID = temp_rowlist[qseqid]
try:
for j in range(temp_rowlist_length - 1):
temp_string = temp_rowlist[j]
row_string_for_output += (temp_string + "\t")
row_string_for_output += temp_rowlist[temp_rowlist_length - 1]
row_string_for_output += "\n"
except IndexError:
print("Exception thrown")
print(row_string_for_output)
#Tuple
TheTuple_rowlist = (Variable_QSeqID, row_string_for_output)
if Variable_QSeqID in Dictionary_lst_lst:
print("key already in dictionary")
else:
Dictionary_lst_lst [Variable_QSeqID] = row_string_for_output
filtered_files_part2.write(row_string_for_output)
filtered_files_part2.close()
filtered_files_2.close()
if __name__ == "__main__":
main(sys.argv[1:])