-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBCleaner.py
340 lines (225 loc) · 9.62 KB
/
DBCleaner.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
######### Module metadata variables ########
__author__ = "David del Rio Aledo"
__credits__ = ["David del Rio", "Jose Manuel Rodriguez", "Inmaculada Jorge"]
__license__ = "Creative Commons Attribution-NonCommercial-NoDerivs 4.0 Unported License https://creativecommons.org/licenses/by-nc-nd/4.0/"
__version__ = "0.0.1"
__maintainer__ = "David del Rio"
__status__ = "Development"
###########################################
#########################
# Import global modules #
#########################
################# Tkinter libraries #################
from tkinter import *
from tkinter import ttk
######################################################
################# Others libraries #################
from Bio import SeqIO
import pandas as pd
import copy
import yaml
from yaml import Loader, Dumper
import argparse
from progress.bar import Bar, ChargingBar
######################################################
####################
# Global variables #
####################
sequences = []
descriptions=[]
ids=[]
fastas=[]
sequences_filtered=[]
sequences_exclude=[]
'''
list_entries=[]
list_labels_files=[]
list_labels_columns=[]
#### Table entries size ###
total_rows=3
total_columns=2
### Output information ###
output_iformation="Datos del procesado de la base de datos \n \n"
'''
####################
####################
###################
# Root activation #
###################
'''
root= Tk()
root.geometry("700x350")
'''
######################
# Output files names #
######################
#output_file="C:/Users/ddelrioa/Desktop/Database_PE1_tr_y_sw/prueba_2.fasta"
#output_file_100="C:/Users/ddelrioa/Desktop/Database_PE1_tr_y_sw/prueba_2_100.fasta"
#####################
# Filters functions #
#####################
def clean_fasta(df,sequences,ids,descriptions):
#For each file check if it has the text put in filters of params file.
#There are two types of filters: OR or AND, if you don't want someone put None in filters params.
#global output_iformation
index_df=-1
#with open(output_file,"w") as out_file:
for file in df[0]:
print(file)
count_total=0
count_filtred=0
index_df+=1
arguments_OR=df[1][index_df].split(sep=',')
arguments_AND=df[2][index_df].split(sep=',')
for fasta in SeqIO.parse(file,"fasta"):
name, sequence, description=fasta.id, str(fasta.seq), fasta.description
count_total+=1
filtered="No"
for flt in arguments_OR:
if str(flt) != None and str(flt) in description:
#out_file.write(">" + name + "\n" + sequence + "\n")
sequences.append(sequence)
ids.append(name)
descriptions.append(description)
fastas.append(fasta)
count_filtred+=1
filtered="Yes"
if arguments_AND !=None and all(x in description for x in arguments_AND)==True:
#out_file.write(">" + name + "\n" + sequence + "\n")
sequences.append(sequence)
ids.append(name)
descriptions.append(description)
fastas.append(fasta)
count_filtred+=1
filtered="Yes"
if filtered=="No":
sequences_exclude.append(name)
print("El archivo " +str(index_df+1)+ " tiene " + str(count_total) +" secuencias totales")
print("El archivo " +str(index_df+1)+ " tiene " + str(count_filtred) +" secuencias filtradas\n")
print("En total se han eliminado " + str(len(sequences_exclude)) +" secuencias\n\n")
'''
output_iformation=output_iformation+"El archivo " +str(index_df)+ " tiene " + str(count_total) +" secuencias totales\n"
output_iformation=output_iformation + "El archivo " +str(index_df)+ " tiene " + str(count_filtred) +" secuencias filtradas\n"
change_information(text=output_iformation)
output_iformation=output_iformation+ "En total se han eliminado " + str(len(sequences_exclude)) +" secuencias\n\n"
change_information(text=output_iformation)
'''
def filterer(fastas,descriptions,outfile):
#Check if there are equal sequences after applying clean_fasta function (fastas object is filled in that function)
#Those that are the same unite the accesions and attach all unique sequences to the file.
#global output_iformation
count_total=0
count_equal=0
fastas_comparation=copy.copy(fastas)
bar=ChargingBar('Comparando secuencias y eliminando repetidas', max=int(len(fastas)))
with open(outfile,"w") as out_file_100:
for fasta in fastas:
name, sequence, description=fasta.id, str(fasta.seq), fasta.description
fastas_comparation.pop(0)
count_total+=1
name_id=copy.copy(name)
equal=0
for i in range(len(fastas_comparation)):
if sequence == str(fastas_comparation[i].seq):
equal=1
sequences_filtered.append(sequence)
name_id=name_id+"__"+str(fastas_comparation[i].id)
count_equal+=1
#name_accession_1=re.search(r'[\|]([\w]+[\|])',name)
if equal==0:
out_file_100.write(">" + name + "\n" + sequence + "\n")
else:
out_file_100.write(">" + name_id + "\n" + sequence + "\n")
bar.next()
bar.finish()
print("De un total de " +str(count_total)+ " secuencias, son iguales " + str(count_equal)+"\n")
#output_iformation=output_iformation+"De un total de " +str(count_total)+ " secuencias, son iguales " + str(count_equal)+"\n"
#change_information(text=output_iformation)
#def change_information(text):
# otput_information_lbl.configure(text=text)
def params_reader(params):
#Read the params input file and transform to dataframe.
#Input params files, return dataframe and path of outfile
with open(params,'r') as params:
data=yaml.load(params,Loader=Loader)
frame_data=[]
index_frame_data=-1
for clave in data:
index_frame_data+=1
if clave!='outfile':
frame_data.append([])
frame_data[index_frame_data].append(data[clave][0]['file'])
frame_data[index_frame_data].append(','.join(data[clave][1]['filter_OR']))
frame_data[index_frame_data].append(','.join(data[clave][2]['filter_AND']))
else:
outfile_path=data[clave]
df=pd.DataFrame(frame_data)
print(df)
print('\n')
return df, outfile_path
#################
# Main function #
#################
def main(args):
#Then execute clean_fasta and filterer functions.
df, outfile_path=params_reader(params=args.paramsfile)
clean_fasta(df,sequences,ids,descriptions)
filterer(fastas,descriptions,outfile_path)
##########
# Parser #
##########
parser=argparse.ArgumentParser(prog='DBCleaner',
description='\n\n\nFilter fatas in each input file with the filters indicated searching the text in fasta description.\n'+
'Then compare all fasta filtreted to join the same sequences as one, the final accession is the union of those who are equal\n\n\n'
)
parser.add_argument('-p','--paramsfile',required=True, help='Input params file with all path to database files and filters')
args=parser.parse_args()
main(args)
'''
def main(list_entries):
#Read the text inserted in entries and create a dataframe.
#Then execute clean_fasta and filterer functions.
frame_data=[]
for i in range(total_rows):
frame_data.append([])
for j in range(total_columns):
frame_data[i].append(list_entries[i][j].get())
df=pd.DataFrame(frame_data)
print(df)
clean_fasta(df,sequences,ids,descriptions)
filterer(fastas,df,descriptions)
################
# GUI creation #
################
### Entries and labels ###
for i in range(total_rows):
File_lbl=Label(root,text="File " + str(i+1))
File_lbl.configure(bg="white", width=10, relief="groove", bd=2, fg="#000000")
File_lbl.grid(row=i+1, column=0)
list_labels_files.append(File_lbl)
for j in range(total_columns):
if j == 0:
column_lbl=Label(root,text="File path")
column_lbl.configure(bg="white", width=17, relief="groove", bd=2, fg="#000000")
column_lbl.grid(row=0, column=j+1)
list_labels_columns.append(column_lbl)
else:
column_lbl=Label(root,text="Filter " + str(j))
column_lbl.configure(bg="white", width=17, relief="groove", bd=2, fg="#000000")
column_lbl.grid(row=0, column=j+1)
list_labels_columns.append(column_lbl)
for i in range(total_rows):
list_entries.append([])
for j in range(total_columns):
entry=Entry(root, width=20, fg="black",bg="white")
entry.grid(row=i+1,column=j+1)
list_entries[i].append(entry)
otput_information_lbl=Label(root,text=output_iformation)
otput_information_lbl.configure(bg="white", width=50, relief="groove", bd=2, fg="#000000")
otput_information_lbl.place(x=500, y=10)
### Buttons ###
start_btn=Button(root, text="Start",command=lambda:[main(list_entries=list_entries)],
bg="grey",state="normal")
start_btn.place(x=400,y=30)
root.mainloop()
'''