-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
336 lines (250 loc) · 13.7 KB
/
Main.py
File metadata and controls
336 lines (250 loc) · 13.7 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
# %%
# Imports
import numpy as np
import pandas as pd
import csv
import sys
import time
import os
import shutil
import PySimpleGUI as sg
import string
# %%
# Timestamp
timestr = time.strftime("%Y%m%d-%H%M%S")
# %%
# Inputs
input_file_folder = "/Users/keltoumboukra/Desktop/Coding projects - Git/SamplePoolingForNGS/Files/ClariostarOutputs/Archive/"
input_file_name = "qubit_data_Marko_22112022.CSV"
# final desired vol (nL)
final_pool_vol = 10000
# well position of pool in output plate
pool_well_in_output_plate = "B1"
# minimum volume to be pipetted (nL)
min_pipetting_capacity = float(25) # for Echo
# minimum volume that can be pipetted
vol_available_in_well = 10000 # for e.g, if there is 20 uL in the well, 5000 nL is what can be available as 15 uL is the minimum working range in the PP-0200 Echo plate)
# Beckman or Echo
BeckmanOrEcho = "Echo"
# %%
sg.theme('LightBrown9')
layout = [
[sg.Text('Choose automated platform to use:\n\t- Echo: non genomic DNA\n\t- Beckman: genomic DNA\n\t- Beckman PacBio: Pooling for PacBio on Beckman')],
[sg.Text('Desired platform name:')],
[sg.DropDown(["Echo", "Beckman", "Beckman PacBio"], default_value="Echo")],
[sg.Submit(), sg.Cancel()]
]
window = sg.Window('NGS Pooling', layout)
event, values = window.read()
window.maximize()
window.close()
BeckmanOrEcho = values[0]
# %%
# Create GUI
sg.theme('LightBrown9')
if BeckmanOrEcho == "Echo":
layout = [
[sg.Text('Please enter the desired parameters')],
[sg.Text('Input file folder (ending with /)', size =(25, 1)), sg.InputText("/Users/keltoumboukra/Desktop/Coding projects - Git/SamplePoolingForNGS/Files/ClariostarOutputs/")],
[sg.Text('Input file name', size =(25, 1)), sg.InputText("qubit_data_Marko_22112022.CSV")],
[sg.Text('Output file folder (ending with /)', size =(25, 1)), sg.InputText("/Users/keltoumboukra/Desktop/Coding projects - Git/SamplePoolingForNGS/Files/")],
[sg.Text('Final pool volume (nL)', size =(25, 1)), sg.InputText("1000")],
[sg.Text('Pooling well position', size =(25, 1)), sg.InputText("B1")],
[sg.Text('Minimum pipetting capacity (nL)', size =(25, 1)), sg.InputText("25")],
[sg.Text('Sample volume available (nL)', size =(25, 1)), sg.InputText("5000")],
[sg.Text('Exclude samples with concentration lower than (ng/uL)', size =(25, 1)), sg.InputText("0.35")],
[sg.Submit(), sg.Cancel()]
]
elif BeckmanOrEcho == "Beckman" or "Beckman PacBio":
layout = [
[sg.Text('Please enter the desired parameters')],
[sg.Text('Input file folder (ending with /)', size =(25, 1)), sg.InputText("/Users/keltoumboukra/Desktop/Coding projects - Git/SamplePoolingForNGS/Files/ClariostarOutputs/")],
[sg.Text('Input file name', size =(25, 1)), sg.InputText("qubit_data_Marko_22112022.CSV")],
[sg.Text('Output file folder (ending with /)', size =(25, 1)), sg.InputText("/Users/keltoumboukra/Desktop/Coding projects - Git/SamplePoolingForNGS/Files/")],
[sg.Text('Final pool volume (uL)', size =(25, 1)), sg.InputText("10")],
[sg.Text('Pooling well position', size =(25, 1)), sg.InputText("B1")],
[sg.Text('Minimum pipetting capacity (uL)', size =(25, 1)), sg.InputText("0.5")],
[sg.Text('Sample volume available (uL)', size =(25, 1)), sg.InputText("25")],
[sg.Text('Exclude conc (ng/uL) lower than', size =(25, 1)), sg.InputText("0.35")],
[sg.Submit(), sg.Cancel()]
]
window = sg.Window('Robot File Generator For NGS Pooling', layout)
event, values = window.read()
window.maximize()
window.close()
# Assigning values
input_file_folder = values[0]
input_file_name = values[1]
output_files_folder = values[2]
final_pool_vol = float(values[3])
pool_well_in_output_plate = values[4]
min_pipetting_capacity = float(values[5])
vol_available_in_well = float(values[6])
min_threshold_values = float(values[7])
# Dataframes
input_file_df = pd.read_csv(input_file_folder + input_file_name)
input_file_df.rename(columns={input_file_df.columns[2]: "Concentration"},inplace=True)
# %%
# Outputs
output_files_folder = output_files_folder + "NGSpooling_" + timestr
os.mkdir(output_files_folder)
output_files_folder = output_files_folder + "/"
output_file_name = "ProcessingDetails.csv"
output_file_df = pd.DataFrame(columns=['Sample Well ID','Sample Concentration','Sample Calculated Weight', 'Sample Calculated Weight Normalised', 'Sample Calculated Volume In Pool'])
if BeckmanOrEcho == "Echo":
output_echo_file_name = "EchoFile.csv"
output_echo_file_df = pd.DataFrame(columns=['Source Well','Destination Well','Transfer Volume'])
elif BeckmanOrEcho == "Beckman":
output_beckman_file_name = "BeckmanFile.csv"
output_beckman_file_df = pd.DataFrame(columns=['Source Well','Destination Well','Transfer Volume'])
elif BeckmanOrEcho == "Beckman PacBio":
output_beckmanPacBio_file_name = "BeckmanPacBioFile.csv"
output_beckmanPacBio_file_df = pd.DataFrame(columns=['Source Well 384','Source Well 96','Source Plate 96','Destination Well','Transfer Volume'])
output_report_name = "ExceptionsReport.csv"
output_report_df = pd.DataFrame(columns=['Sample Well ID','Sample Concentration','Comment'])
# Copy input file into the output folder
os.mkdir(output_files_folder + "InputFiles/")
shutil.copyfile(input_file_folder+input_file_name, output_files_folder + "InputFiles/" + "InputFile.csv")
# %%
# Trim input data frame
# Remove standards
input_file_df = input_file_df[input_file_df["Content"].str.contains("Standard") == False]
input_file_df = input_file_df.reset_index(drop=True)
# Remove samples with negative concentrations and add to output report
for row in input_file_df.itertuples():
Index = row [0]
Well = row[1]
Concentration = row[3]
if Concentration <= min_threshold_values:
output_report_df = output_report_df.append({'Sample Well ID': Well, 'Sample Concentration': Concentration, 'Comment': "Sample has negative value for concentration"}, ignore_index=True)
input_file_df = input_file_df.drop(Index)
input_file_df = input_file_df.reset_index(drop=True)
output_report_df = output_report_df.reset_index(drop=True)
# Copy trimmed input file into the output folder
input_file_df.to_csv(output_files_folder + "InputFiles/" + "InputTrimmedFile.csv", index=False)
# %%
# Calculate Weights
max_concentration = input_file_df['Concentration'].max()
sum_weights = float()
for row in input_file_df.itertuples():
Index = row [0]
Well = row[1]
Concentration = row[3]
sample_weight = max_concentration/Concentration
sum_weights += sample_weight
output_file_df = output_file_df.append({'Sample Well ID': Well, 'Sample Concentration': Concentration, 'Sample Calculated Weight': sample_weight}, ignore_index=True)
# %%
# Calculate Normalised Weights
sum_normalised_weights = float(0)
for row in input_file_df.itertuples():
Index = row [0]
Well = row[1]
Concentration = row[3]
sample_normalised_weight = output_file_df["Sample Calculated Weight"].iloc[Index]/sum_weights
sum_normalised_weights += sample_normalised_weight # for QC, should be =1
output_file_df.at[Index,'Sample Calculated Weight Normalised']=sample_normalised_weight
# %%
# Translate 384 coordinates to 96 coordinates for Beckman PacBio
def _translate(id):
id = id -1
if (id // 24) % 2 ==0:
if id % 2==0:
destinationrow = 1 + id % 24 //2
destinationCol = chr(ord('@')+1+id // 48)
return { "plate": 1, "well": destinationCol + str(destinationrow)}
else:
destinationrow = 1 + id % 24 //2
destinationCol = chr(ord('@')+1+id // 48)
return { "plate": 2, "well": destinationCol + str(destinationrow)}
else:
if id % 2==0:
destinationrow = 1 +id % 24 //2
destinationCol = chr(ord('@')+1+id // 48)
return { "plate": 3, "well": destinationCol + str(destinationrow)}
else:
destinationrow = 1 +id % 24 //2
destinationCol = chr(ord('@')+1+id // 48)
return { "plate": 4, "well": destinationCol + str(destinationrow)}
# Generate dictionary to to convert 384 well number to 96-well plate number and well coordinate
x = list(map(_translate, [i+1 for i in range(0,384)]))
rows = [i for i in string.ascii_uppercase[:16]]
columns = [i for i in range(1, 24 + 1)]
wells = [letter + str(number) if number >= 10 else letter + "0" + str(number) for letter in rows for number in columns]
d1=dict(zip(wells,x))
d1
#print(d1["G10"])
#print(d1["G10"]["plate"])
#print(d1["G10"]["well"])
# %%
# Calculate Volume to pipette for each sample in the final pool
vol_in_pool = float()
sum_vol_in_pool = float(0)
fail_bool = bool(0)
for row in input_file_df.itertuples():
Index = row [0]
Well = row[1]
Concentration = row[3]
vol_in_pool = output_file_df["Sample Calculated Weight Normalised"].iloc[Index]*final_pool_vol
sum_vol_in_pool += vol_in_pool # for QC, should be = final_pool_vol
if vol_in_pool < min_pipetting_capacity:
output_report_df = output_report_df.append({'Sample Well ID': Well, 'Sample Concentration': Concentration, 'Comment': "Volume to be pipetted for this well ({} nL) is smaller than pipetting capacity ({} nL). You MUST reprocess the file".format(vol_in_pool,min_pipetting_capacity)}, ignore_index=True)
fail_bool = bool(1)
elif vol_in_pool > vol_available_in_well:
output_report_df = output_report_df.append({'Sample Well ID': Well, 'Sample Concentration': Concentration, 'Comment': "Volume to be pipetted for this well ({} nL) is larger than the volume available ({} nL). You MUST reprocess the file".format(vol_in_pool,vol_available_in_well)}, ignore_index=True)
fail_bool = bool(1)
else:
output_file_df.at[Index,'Sample Calculated Volume In Pool']=vol_in_pool
if BeckmanOrEcho == "Echo":
output_echo_file_df = output_echo_file_df.append({'Source Well': Well, 'Destination Well': pool_well_in_output_plate, 'Transfer Volume': vol_in_pool}, ignore_index=True)
elif BeckmanOrEcho == "Beckman":
output_beckman_file_df = output_beckman_file_df.append({'Source Well': Well, 'Destination Well': pool_well_in_output_plate, 'Transfer Volume': vol_in_pool}, ignore_index=True)
elif BeckmanOrEcho == "Beckman PacBio":
output_beckmanPacBio_file_df = output_beckmanPacBio_file_df.append({'Source Well 384': Well,'Source Well 96': d1[Well]["well"],'Source Plate 96' : d1[Well]["plate"],'Destination Well' : pool_well_in_output_plate,'Transfer Volume': vol_in_pool}, ignore_index=True)
# %%
# Calculate final pool concentration for QC
sum_concentrations = float(0)
for row in output_file_df.itertuples():
Concentration = row[2]
VolInPool = row[5]
sum_concentrations += (Concentration*VolInPool)
final_pool_concentration = "%.3f"%(sum_concentrations / final_pool_vol)
# %%
# Export files and communicate outcome to user in GUI
os.mkdir(output_files_folder + "OutputFiles/")
if BeckmanOrEcho == "Echo":
if fail_bool == bool(1):
layout = [[sg.Text("Processing failed. The concentration of 1 sample or more is out of range and doesn't allow pooling with the settings entered. Check report file for details.")], [sg.Button("OK")]]
window = sg.Window("Echo File Generator For NGS Pooling", layout)
else:
output_file_df.to_csv(output_files_folder + "OutputFiles/" + output_file_name, index=False)
output_echo_file_df.to_csv(output_files_folder + "OutputFiles/" + output_echo_file_name, index=False)
layout = [[sg.Text("Processing successful! Final pool concentration: {} ng/uL.".format(final_pool_concentration))], [sg.Button("OK")]]
window = sg.Window("Echo File Generator For NGS Pooling", layout)
elif BeckmanOrEcho == "Beckman":
if fail_bool == bool(1):
layout = [[sg.Text("Processing failed. The concentration of 1 sample or more is out of range and doesn't allow pooling with the settings entered. Check report file for details.")], [sg.Button("OK")]]
window = sg.Window("Beckman File Generator For NGS Pooling", layout)
else:
output_file_df.to_csv(output_files_folder + "OutputFiles/" + output_file_name, index=False)
output_beckman_file_df = output_beckman_file_df.sort_values(by='Transfer Volume', ascending=False)
output_beckman_file_df.to_csv(output_files_folder + "OutputFiles/" + output_beckman_file_name, index=False)
layout = [[sg.Text("Processing successful! Final pool concentration: {} ng/uL.".format(final_pool_concentration))], [sg.Button("OK")]]
window = sg.Window("Beckman File Generator For NGS Pooling", layout)
elif BeckmanOrEcho == "Beckman PacBio":
if fail_bool == bool(1):
layout = [[sg.Text("Processing failed. The concentration of 1 sample or more is out of range and doesn't allow pooling with the settings entered. Check report file for details.")], [sg.Button("OK")]]
window = sg.Window("Beckman File Generator For NGS Pooling", layout)
else:
output_file_df.to_csv(output_files_folder + "OutputFiles/" + output_file_name, index=False)
output_beckmanPacBio_file_df = output_beckmanPacBio_file_df.sort_values(by='Transfer Volume', ascending=False)
output_beckmanPacBio_file_df.to_csv(output_files_folder + "OutputFiles/" + output_beckmanPacBio_file_name, index=False)
layout = [[sg.Text("Processing successful! Final pool concentration: {} ng/uL.".format(final_pool_concentration))], [sg.Button("OK")]]
window = sg.Window("Beckman PacBio File Generator For NGS Pooling", layout)
output_report_df.to_csv(output_files_folder + "OutputFiles/" + output_report_name, index=False)
while True:
event, values = window.read()
if event == "OK" or event == sg.WIN_CLOSED:
break
window.close()
# %%
# %%