-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI_Final.py
More file actions
419 lines (341 loc) · 18.9 KB
/
GUI_Final.py
File metadata and controls
419 lines (341 loc) · 18.9 KB
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
import shutil
import tkinter as tk
from tkinter import ttk, filedialog
import os
import pandas as pd
import subprocess
class Application(tk.Tk):
def __init__(self):
super().__init__()
self.title("Genomics Pipeline")
self.geometry("800x600")
self.default_populations = {
"AFR": "Africa",
"AMR": "America",
"CAU": "Caucasus",
"CAS": "Central Asia",
"EUR": "Europe",
"SAH": "Sahul",
"SIB": "Siberia",
"SAS": "South Asia",
"SEAM": "Southeast Asia Mainland",
"SEAI": "Southeast Asia Island",
"WAS": "West Asia"
}
self.custom_populations = {}
self.rois = []
self.create_widgets()
def create_widgets(self):
# Tabs
self.tab_control = ttk.Notebook(self)
self.input_tab = ttk.Frame(self.tab_control)
self.progress_tab = ttk.Frame(self.tab_control)
self.tab_control.add(self.input_tab, text='Input')
self.tab_control.add(self.progress_tab, text='Progress')
self.tab_control.pack(expand=1, fill='both')
# Input tab
self.input_frame = ttk.LabelFrame(self.input_tab, text="Genomics Pipeline Input")
self.input_frame.pack(padx=10, pady=10, fill="both", expand=True)
# Directory
ttk.Label(self.input_frame, text="Directory:").grid(column=0, row=0, padx=10, pady=10)
self.directory_entry = ttk.Entry(self.input_frame, width=50)
self.directory_entry.grid(column=1, row=0, padx=10, pady=10)
self.browse_button = ttk.Button(self.input_frame, text="Browse", command=self.browse_directory)
self.browse_button.grid(column=2, row=0, padx=10, pady=10)
# Population selection
ttk.Label(self.input_frame, text="Population:").grid(column=0, row=1, padx=10, pady=10)
self.population_var = tk.StringVar(value="Default Population")
self.default_radio = ttk.Radiobutton(self.input_frame, text="Default Population", variable=self.population_var, value="Default Population", command=self.update_population_table)
self.custom_radio = ttk.Radiobutton(self.input_frame, text="Custom Population", variable=self.population_var, value="Custom Population", command=self.update_population_table)
self.default_radio.grid(column=1, row=1, padx=10, pady=10, sticky=tk.W)
self.custom_radio.grid(column=2, row=1, padx=10, pady=10, sticky=tk.W)
# Population table
self.population_table = ttk.Treeview(self.input_frame, columns=("Short ID", "Full Name"), show='headings')
self.population_table.heading("Short ID", text="Short ID")
self.population_table.heading("Full Name", text="Full Name")
self.population_table.grid(column=0, row=2, columnspan=5, padx=10, pady=10)
# Add custom population entry
self.add_custom_population_button = ttk.Button(self.input_frame, text="Add Custom Population", command=self.add_custom_population_entry)
self.add_custom_population_button.grid(column=0, row=3, columnspan=5, padx=10, pady=10)
self.add_custom_population_button.state(["disabled"])
self.update_population_table()
# Chromosome number
ttk.Label(self.input_frame, text="Chromosome:").grid(column=0, row=4, padx=10, pady=10)
self.chromosome_entry = ttk.Entry(self.input_frame)
self.chromosome_entry.grid(column=1, row=4, padx=10, pady=10)
# ROI start
ttk.Label(self.input_frame, text="ROI Start:").grid(column=0, row=5, padx=10, pady=10)
self.roi_start_entry = ttk.Entry(self.input_frame)
self.roi_start_entry.grid(column=1, row=5, padx=10, pady=10)
# ROI end
ttk.Label(self.input_frame, text="ROI End:").grid(column=2, row=5, padx=10, pady=10)
self.roi_end_entry = ttk.Entry(self.input_frame)
self.roi_end_entry.grid(column=3, row=5, padx=10, pady=10)
# Add ROI button
self.add_roi_button = ttk.Button(self.input_frame, text="Add ROI", command=self.add_roi)
self.add_roi_button.grid(column=4, row=5, padx=10, pady=10)
# ROI list
self.roi_listbox = tk.Listbox(self.input_frame, height=5, width=50)
self.roi_listbox.grid(column=0, row=6, columnspan=5, padx=10, pady=10, sticky="we")
# Remove ROI button
self.remove_roi_button = ttk.Button(self.input_frame, text="Remove Selected ROI", command=self.remove_selected_roi)
self.remove_roi_button.grid(column=4, row=7, padx=10, pady=10)
# Run button
self.run_button = ttk.Button(self.input_frame, text="Run", command=self.run_pipeline)
self.run_button.grid(column=1, row=8, columnspan=3, padx=10, pady=10)
# Progress tab
self.progress_frame = ttk.LabelFrame(self.progress_tab, text="Progress")
self.progress_frame.pack(padx=10, pady=10, fill="both", expand=True)
self.progress_text = tk.Text(self.progress_frame, height=20, wrap='word')
self.progress_text.pack(padx=10, pady=10, fill="both", expand=True)
def browse_directory(self):
directory = filedialog.askdirectory()
if directory:
self.directory_entry.delete(0, tk.END)
self.directory_entry.insert(0, directory)
def update_population_table(self):
for row in self.population_table.get_children():
self.population_table.delete(row)
if self.population_var.get() == "Default Population":
for short_id, full_name in self.default_populations.items():
self.population_table.insert("", "end", values=(short_id, full_name))
self.add_custom_population_button.state(["disabled"])
else:
for short_id, full_name in self.custom_populations.items():
self.population_table.insert("", "end", values=(short_id, full_name))
self.add_custom_population_button.state(["!disabled"])
def add_custom_population_entry(self):
add_window = tk.Toplevel(self)
add_window.title("Add Custom Population")
ttk.Label(add_window, text="Short ID:").grid(column=0, row=0, padx=10, pady=10)
short_id_entry = ttk.Entry(add_window)
short_id_entry.grid(column=1, row=0, padx=10, pady=10)
ttk.Label(add_window, text="Full Name:").grid(column=0, row=1, padx=10, pady=10)
full_name_entry = ttk.Entry(add_window)
full_name_entry.grid(column=1, row=1, padx=10, pady=10)
ttk.Button(add_window, text="Add", command=lambda: self.save_custom_population(add_window, short_id_entry.get(), full_name_entry.get())).grid(column=0, row=2, columnspan=2, padx=10, pady=10)
def save_custom_population(self, window, short_id, full_name):
if short_id and full_name:
self.custom_populations[short_id] = full_name
self.update_population_table()
window.destroy()
def add_roi(self):
start = self.roi_start_entry.get()
end = self.roi_end_entry.get()
if start and end:
roi = f"{start}-{end}"
self.rois.append(roi)
self.roi_listbox.insert(tk.END, roi)
self.roi_start_entry.delete(0, tk.END)
self.roi_end_entry.delete(0, tk.END)
def remove_selected_roi(self):
selected_indices = self.roi_listbox.curselection()
for index in selected_indices[::-1]:
self.roi_listbox.delete(index)
del self.rois[index]
def run_pipeline(self):
directory = self.directory_entry.get()
chromosome = self.chromosome_entry.get()
population = self.population_var.get()
rois = ", ".join(self.rois)
self.progress_text.insert(tk.END, f"Running pipeline for {population}...\n")
self.progress_text.insert(tk.END, f"Directory: {directory}\n")
self.progress_text.insert(tk.END, f"Chromosome: {chromosome}\n")
# self.progress_text.insert(tk.END, f"ROIs: {rois}\n")
# self.progress_text.insert(tk.END, f"self.rois: {self.rois}\n")
# Run the first step of the pipeline
self.run_first_step(directory, chromosome, population)
# Extract the single element from the list
roi_str = self.rois[0]
# Split the string at the '-' to get the two numbers
start_roi, end_roi = roi_str.split('-')
plink_path = "D:/Projects/Summer 2024/plink.exe" # Specify the full path to PLINK
self.make_ped_map_info(directory, chromosome, start_roi, end_roi, plink_path)
def run_first_step(self, directory, chromosome, population):
plink_path = "D:/Projects/Summer 2024/plink.exe" # Specify the full path to PLINK
fam_files = [f for f in os.listdir(directory) if f.endswith(".fam")]
for fam_file in fam_files:
fam_file_path = os.path.join(directory, fam_file)
self.progress_text.insert(tk.END, f"Updating IDs in {fam_file_path}...\n")
self.update_fam_ids(fam_file_path)
# Run PLINK command to create BFILES with updated FAM file
bfile_prefix = fam_file.replace(".fam", "")
updated_fam_file_path = fam_file_path.replace(".fam", "_updated.fam")
command = [
plink_path,
"--bfile", os.path.join(directory, bfile_prefix),
"--fam", updated_fam_file_path,
"--make-bed",
"--out", os.path.join(directory, bfile_prefix + "_updated")
]
# Print the command for debugging purposes
self.progress_text.insert(tk.END, f"Executing command: {' '.join(command)}\n")
result = subprocess.run(command, capture_output=True, text=True)
self.progress_text.insert(tk.END, result.stdout)
if result.returncode != 0:
self.progress_text.insert(tk.END, f"Error: {result.stderr}\n")
else:
self.progress_text.insert(tk.END, f"Successfully created BFILES for {fam_file}\n")
self.run_population_files(directory, fam_files, plink_path)
def update_fam_ids(self, fam_file):
populations = {
"Africa": "AFR",
"Americas": "AMR",
"Caucasus": "CAU",
"Central_Asia": "CAS",
"Europe": "EUR",
"Sahul": "SAH",
"Siberia": "SIB",
"South_Asia": "SAS",
"Southeast_Asia_Mainland": "SEAM",
"Southeast_Asia_Island": "SEAI",
"West_Asia": "WAS"
}
# Check if the file is not empty
if os.path.getsize(fam_file) == 0:
self.progress_text.insert(tk.END, f"File {fam_file} is empty.\n")
return
# Read the fam file into a DataFrame
try:
df = pd.read_csv(fam_file, sep='\s+', header=None)
except pd.errors.EmptyDataError:
self.progress_text.insert(tk.END, f"File {fam_file} has no columns to parse.\n")
return
# Create a new column for the population short tags
df['pop_short_tag'] = df[0].map(populations)
# Filter out rows with NaN values in 'pop_short_tag'
df = df[df['pop_short_tag'].notna()]
# Initialize ID counters for individual IDs
id_counters = {key: 1 for key in populations.values()}
# Iterate through the DataFrame and update the family and individual IDs
for index, row in df.iterrows():
short_tag = row['pop_short_tag']
new_fam_id = short_tag
new_ind_id = f"{short_tag}{id_counters[short_tag]}"
df.at[index, 0] = new_fam_id
df.at[index, 1] = new_ind_id
id_counters[short_tag] += 1
# Drop the temporary column used for sorting
df.drop(columns=['pop_short_tag'], inplace=True)
# Save the updated DataFrame to the fam file
df.to_csv(fam_file.replace(".fam", "_updated.fam"), sep=' ', index=False, header=False)
def run_population_files(self, directory, fam_files, plink_path):
for fam_file in fam_files:
# Construct the full path to the folder and the fam file
folder_path = os.path.dirname(os.path.join(directory, fam_file))
fam_file_path = os.path.join(folder_path, fam_file)
# Extract chromosome number from folder name
chr_number = ''.join([i for i in os.path.basename(folder_path) if i.isdigit()])
# Read the updated .fam file into a DataFrame
updated_fam_file_path = fam_file_path.replace(".fam", "_updated.fam")
try:
df = pd.read_csv(updated_fam_file_path, sep='\s+', header=None)
self.progress_text.insert(tk.END, f"Read updated .fam file '{updated_fam_file_path}' successfully with shape {df.shape}\n")
except Exception as e:
self.progress_text.insert(tk.END, f"Error reading updated .fam file '{updated_fam_file_path}': {e}\n")
continue # Skip to the next file
# Create a new directory for the individual population files
population_folder_path = os.path.join(folder_path, "population_bfiles")
os.makedirs(population_folder_path, exist_ok=True)
for short_tag, full_name in self.default_populations.items():
self.progress_text.insert(tk.END, f"Processing population: {short_tag}\n") # Debug statement
pop_df = df[df[0].astype(str) == short_tag]
self.progress_text.insert(tk.END, f"Population {short_tag} dataframe shape: {pop_df.shape}\n")
if not pop_df.empty:
pop_fam_file = os.path.join(population_folder_path, f"Chr{chr_number}_{short_tag}.fam")
pop_df.to_csv(pop_fam_file, sep=' ', index=False, header=False)
# Debug: Print population fam file creation
self.progress_text.insert(tk.END, f"Created FAM file for population {short_tag}: {pop_fam_file}\n")
# Run PLINK command to update the files
bfile_prefix = fam_file.replace(".fam", "_updated")
plink_command = [
plink_path,
"--bfile", os.path.join(folder_path, bfile_prefix),
"--keep-fam", pop_fam_file,
"--make-bed",
"--out", os.path.join(population_folder_path, f"Chr{chr_number}_{short_tag}")
]
self.progress_text.insert(tk.END, f"Running PLINK command: {' '.join(plink_command)}\n")
try:
result = subprocess.run(plink_command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
self.progress_text.insert(tk.END, f"PLINK output for population {short_tag}:\n{result.stdout}\n")
except subprocess.CalledProcessError as e:
self.progress_text.insert(tk.END, f"Error executing PLINK command for population {short_tag}: {e}\n")
self.progress_text.insert(tk.END, f"Standard Output:\n{e.stdout}\n")
self.progress_text.insert(tk.END, f"Standard Error:\n{e.stderr}\n")
else:
self.progress_text.insert(tk.END, f"No matching entries for population: {short_tag}\n") # Debug statement
def make_ped_map_info(self, base_dir, chromosome, start_loc, end_loc, plink_path):
populations = {
"AFR": "Africa",
"AMR": "America",
"CAU": "Caucasus",
"CAS": "Central Asia",
"EUR": "Europe",
"SAH": "Sahul",
"SIB": "Siberia",
"SAS": "South Asia",
"SEAM": "Southeast Asia Mainland",
"SEAI": "Southeast Asia Island",
"WAS": "West Asia"
}
# Construct the full path to the folder
folder_path = os.path.join(base_dir, f"population_bfiles")
# Create a new folder to store the output files
output_folder = os.path.join(folder_path, f"{start_loc}_to_{end_loc}")
os.makedirs(output_folder, exist_ok=True)
# List all individual population files in the folder
for short_tag, pop in populations.items():
bfile_name = f"Chr{chromosome}_{short_tag}"
bfile_path = os.path.join(folder_path, bfile_name)
if os.path.exists(bfile_path + '.bed'): # Check if the .bed file exists
self.progress_text.insert(tk.END, f"Processing {bfile_name}...")
# Generate PLINK commands
recode_command = [
plink_path,
"--bfile", bfile_path,
"--chr", chromosome,
"--from-bp", start_loc,
"--to-bp", end_loc,
"--recode", "hv",
"--out", os.path.join(output_folder, f"{short_tag}_ROI")
]
recode_command_no_hv = [
plink_path,
"--bfile", bfile_path,
"--chr", chromosome,
"--from-bp", start_loc,
"--to-bp", end_loc,
"--recode",
"--out", os.path.join(output_folder, f"{short_tag}_ROI")
]
# Run PLINK commands
try:
self.progress_text.insert(tk.END, f"Running PLINK command: {recode_command}")
subprocess.run(recode_command, check=True)
self.progress_text.insert(tk.END, f"Running PLINK command: {' '.join(recode_command_no_hv)}")
subprocess.run(recode_command_no_hv, check=True)
self.progress_text.insert(tk.END, f"Files for {bfile_name} successfully created in {output_folder}")
# Define the source file path
source_file = r"D:\Projects\Summer 2024\myfile.pl"
shutil.copy(source_file, output_folder)
# Convert .ped to FASTA
ped_file = f"{short_tag}_ROI.ped"
fasta_file = f"{short_tag}_FASTA.fas"
# Change to the directory containing the .ped file
os.chdir(output_folder)
# Run the Perl script
perl_command = f"perl myfile.pl {ped_file} > {fasta_file}"
self.progress_text.insert(tk.END, f"Running Perl command: {perl_command}")
result = subprocess.run(perl_command, shell=True, capture_output=True, text=True)
if result.returncode == 0:
self.progress_text.insert(tk.END, f"FASTA file for {bfile_name} successfully created: {fasta_file}")
else:
self.progress_text.insert(tk.END, f"Error in Perl command: {result.stderr}")
except subprocess.CalledProcessError as e:
self.progress_text.insert(tk.END, f"Error processing {bfile_name}: {e}")
else:
self.progress_text.insert(tk.END, f"File {bfile_name} does not exist.")
if __name__ == "__main__":
app = Application()
app.mainloop()