chromaquant 0.3.1__py3-none-any.whl → 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,425 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ COPYRIGHT STATEMENT:
5
+
6
+ ChromaQuant – A quantification software for complex gas chromatographic data
7
+
8
+ Copyright (c) 2024, by Julia Hancock
9
+ Affiliation: Dr. Julie Elaine Rorrer
10
+ URL: https://www.rorrerlab.com/
11
+
12
+ License: BSD 3-Clause License
13
+
14
+ ---
15
+
16
+ SUBPACKAGE FOR PERFORMING GAS TCD QUANTIFICATION STEPS
17
+
18
+ Julia Hancock
19
+ Started 12-29-2024
20
+
21
+ """
22
+ """ PACKAGES """
23
+ from chemformula import ChemFormula
24
+
25
+ #Function for quantifying gas TCD data w/ volume estimation method, no pressure adjustment
26
+ def gasTCD_VE(BreakdownDF,DBRF,gasBag_cond,peak_error):
27
+
28
+ #Unpack gas bag conditions
29
+ temp = gasBag_cond[0] #temperature of gas bag, C
30
+ pressure = gasBag_cond[1] #sample pressure in gas bag, psi
31
+ co2 = gasBag_cond[2] #CO2 volume, mL
32
+
33
+ #Initialize compound name column in BreakdownDF
34
+ BreakdownDF['Compound Name'] = 'None'
35
+
36
+ #Function to find if CO2 peak exists
37
+ def getCO2(BreakdownDF,DBRF,TCD_cond,peak_error):
38
+
39
+ #Unpack TCD conditions
40
+ co2 = TCD_cond[0]
41
+ pressure = TCD_cond[1]
42
+ temp = TCD_cond[2]
43
+ R = TCD_cond[3]
44
+
45
+ #Find the CO2 peak row in DBRF
46
+ CO2_row = DBRF.loc[DBRF['Compound Name'] == "Carbon Dioxide"].iloc[0]
47
+
48
+ #Get the retention time
49
+ CO2_RT = CO2_row['RT (min)']
50
+
51
+ #Get the minimum and maximum of the RT range using the peak error
52
+ CO2_RTmin = CO2_RT - peak_error
53
+ CO2_RTmax = CO2_RT + peak_error
54
+
55
+ #Define boolean describing whether or not CO2 match has been found
56
+ CO2_bool = False
57
+ #Define volume estimate
58
+ volume = 0
59
+
60
+ #Iterate through every row in BreakdownDF
61
+ for i, row in BreakdownDF.iterrows():
62
+
63
+ #If the TCD retention time is within range of the CO2 entry...
64
+ if CO2_RTmin <= row['RT'] <= CO2_RTmax:
65
+
66
+ #Add the compound name to the breakdown dataframe
67
+ BreakdownDF.at[i,'Compound Name'] = 'Carbon Dioxide'
68
+
69
+ #Add the other relevant information to the breakdown dataframe
70
+ BreakdownDF.at[i,'Formula'] = 'CO2'
71
+ BreakdownDF.at[i,'RF (Area/vol.%)'] = CO2_row['RF']
72
+ BreakdownDF.at[i,'MW (g/mol)'] = ChemFormula('CO2').formula_weight
73
+
74
+ #Get volume percent using response factor
75
+ volpercent = row['Area']/CO2_row['RF']
76
+ BreakdownDF.at[i,'Vol.%'] = volpercent
77
+
78
+ #Calculate total volume using volume percent
79
+ volume = co2 * 100 / volpercent #total volume, m^3
80
+
81
+ #Assign CO2 volume
82
+ BreakdownDF.at[i,'Volume (m^3)'] = co2
83
+
84
+ #Get moles using ideal gas law (PV=nRT)
85
+ BreakdownDF.at[i,'Moles (mol)'] = co2*pressure/(temp*R)
86
+
87
+ #Get mass (mg) using moles and molar mass
88
+ BreakdownDF.at[i,'Mass (mg)'] = BreakdownDF.at[i,'Moles (mol)'] * BreakdownDF.at[i,'MW (g/mol)'] * 1000
89
+
90
+ #Set CO2_bool to True
91
+ CO2_bool = True
92
+
93
+ break
94
+
95
+ #Otherwise, pass
96
+ else:
97
+ pass
98
+
99
+ return CO2_bool, volume, BreakdownDF
100
+
101
+ #Add min and max peak assignment values to DBRF
102
+ for i, row in DBRF.iterrows():
103
+ DBRF.at[i,'RT Max'] = DBRF.at[i,'RT (min)'] + peak_error
104
+ DBRF.at[i,'RT Min'] = DBRF.at[i,'RT (min)'] - peak_error
105
+
106
+ #Convert sinfo variables to new units
107
+ co2 = co2 / 10**6 #volume injected CO2, m^3
108
+ temp = temp + 273.15 #reactor temperature, K
109
+ pressure = pressure / 14.504*100000 #reactor pressure, Pa
110
+
111
+ #Define ideal gas constant, m^3*Pa/K*mol
112
+ R = 8.314
113
+
114
+ #Define variable to total volume (m^3)
115
+ V_TC = 0
116
+
117
+ #Define list of conditions
118
+ TCD_cond = [co2,pressure,temp,R]
119
+
120
+ #Check if there is a peak in the BreakdownDF that can be assigned to CO2
121
+ CO2_bool, V_TC, BreakdownDF = getCO2(BreakdownDF,DBRF,TCD_cond,peak_error)
122
+
123
+ if CO2_bool:
124
+ #Iterate through every row in BreakdownDF
125
+ for i, row in BreakdownDF.iterrows():
126
+
127
+ #Iterate through every row in DBRF
128
+ for i2, row2 in DBRF.iterrows():
129
+
130
+ #If the TCD retention time is within the range for a given DBRF entry...
131
+ if row2['RT Min'] <= row['RT'] <= row2['RT Max']:
132
+
133
+ #Add the compound name to the breakdown dataframe
134
+ BreakdownDF.at[i,'Compound Name'] = row2['Compound Name']
135
+
136
+ #Add the other relevant information to the breakdown dataframe
137
+ BreakdownDF.at[i,'Formula'] = row2['Formula']
138
+ BreakdownDF.at[i,'RF (Area/vol.%)'] = row2['RF']
139
+ BreakdownDF.at[i,'MW (g/mol)'] = ChemFormula(row2['Formula']).formula_weight
140
+
141
+ #Get volume percent using response factor
142
+ volpercent = row['Area']/row2['RF']
143
+ BreakdownDF.at[i,'Vol.%'] = volpercent
144
+
145
+ #Get volume using volume percent
146
+ vol = V_TC*volpercent/100
147
+ BreakdownDF.at[i,'Volume (m^3)'] = vol
148
+
149
+ #Get moles using ideal gas law (PV=nRT)
150
+ BreakdownDF.at[i,'Moles (mol)'] = vol*pressure/(temp*R)
151
+
152
+ #Get mass (mg) using moles and molar mass
153
+ BreakdownDF.at[i,'Mass (mg)'] = BreakdownDF.at[i,'Moles (mol)'] * BreakdownDF.at[i,'MW (g/mol)'] * 1000
154
+
155
+ #Otherwise, pass
156
+ else:
157
+ pass
158
+ #Otherwise, pass
159
+ else:
160
+ pass
161
+
162
+ #Convert total volume to mL
163
+ V_TC *= 10**6
164
+
165
+ return BreakdownDF, V_TC
166
+
167
+ #Function for quantifying gas TCD data w/ scale factor method
168
+ def gasTCD_SF(BreakdownDF,DBRF,gasBag_cond,reactor_cond,peak_error):
169
+
170
+ #Initialize compound name column in BreakdownDF
171
+ BreakdownDF['Compound Name'] = 'None'
172
+
173
+ #Function to determine total volume using ideal gas law
174
+ def volumeIGL(V_C,reactor_cond):
175
+
176
+ #Unpack reactor conditions
177
+ P_f = reactor_cond[0] #reactor quench pressure, psig
178
+ V_R = reactor_cond[1] #reactor internal volume, mL
179
+ P_0 = reactor_cond[2] #atmospheric pressure, psi
180
+
181
+ #Estimate total volume of gas bag, mL
182
+ V_T = V_R * (P_f + P_0) / P_0
183
+
184
+ #Estimate total volume of gas bag plus volume CO2, mL
185
+ V_TC = V_T + V_C
186
+
187
+ return V_T, V_TC
188
+
189
+ # Function to estimate scale factor from CO2
190
+ def getScaleFactor(BreakdownDF,DBRF,V_C,V_TC):
191
+
192
+ #Find the CO2 peak row in DBRF
193
+ CO2_row = DBRF.loc[DBRF['Compound Name'] == "Carbon Dioxide"].iloc[0]
194
+
195
+ #Get the retention time
196
+ CO2_RT = CO2_row['RT (min)']
197
+
198
+ #Get the minimum and maximum of the RT range using the peak error
199
+ CO2_RTmin = CO2_RT - peak_error
200
+ CO2_RTmax = CO2_RT + peak_error
201
+
202
+ #Define boolean describing whether or not CO2 match has been found
203
+ CO2_bool = False
204
+
205
+ #Iterate through every row in BreakdownDF
206
+ for i, row in BreakdownDF.iterrows():
207
+
208
+ #If the TCD retention time is within range of the CO2 entry...
209
+ if CO2_RTmin <= row['RT'] <= CO2_RTmax:
210
+
211
+ #Get estimated volume fraction of CO2
212
+ psiCO2_e = row['Area'] / (100 * CO2_row['RF'])
213
+
214
+ #Get actual volume fraction of CO2
215
+ psiCO2_a = V_C / V_TC
216
+
217
+ #Define scale factor as ratio of actual volume fraction to estimated volume fraction
218
+ SF = psiCO2_a / psiCO2_e
219
+
220
+ #Set CO2_bool to True
221
+ CO2_bool = True
222
+
223
+ break
224
+
225
+ #Otherwise, pass
226
+ else:
227
+ pass
228
+
229
+ #Raise error if no CO2 peak found
230
+ if CO2_bool == False:
231
+ raise Exception("[gasTCD][ERROR] No CO2 peak found in TCD")
232
+
233
+ else:
234
+ pass
235
+
236
+ return SF
237
+
238
+ #Function to calculate amounts of each species using the scale factor
239
+ def quantTCD(BreakdownDF,DBRF,gasBag_cond,reactor_cond):
240
+
241
+ # Unpack gas bag conditions
242
+ GB_temp = gasBag_cond[0] # Temperature of gas bag, C
243
+ GB_pressure = gasBag_cond[1] # Sample pressure in gas bag, psig
244
+ V_C = gasBag_cond[2] # CO2 volume, mL
245
+
246
+ # Get total and total+CO2 gas bag volumes
247
+ V_T, V_TC = volumeIGL(V_C,reactor_cond)
248
+
249
+ # Define ideal gas constant, m^3*Pa/K*mol
250
+ R = 8.314
251
+
252
+ #Define conversion factor, Pa * m^3 * psi^-1 * mL^-1
253
+ C = 0.00689476
254
+
255
+ #Get scale factor
256
+ SF = getScaleFactor(BreakdownDF,DBRF,V_C,V_TC)
257
+
258
+ #Add min and max peak assignment values to DBRF
259
+ for i, row in DBRF.iterrows():
260
+ DBRF.at[i,'RT Max'] = DBRF.at[i,'RT (min)'] + peak_error
261
+ DBRF.at[i,'RT Min'] = DBRF.at[i,'RT (min)'] - peak_error
262
+
263
+ #Iterate through every row in BreakdownDF
264
+ for i, row in BreakdownDF.iterrows():
265
+
266
+ #Iterate through every row in DBRF
267
+ for i2, row2 in DBRF.iterrows():
268
+
269
+ #If the TCD retention time is within the range for a given DBRF entry...
270
+ if row2['RT Min'] <= row['RT'] <= row2['RT Max']:
271
+
272
+ #Add the compound name to the breakdown dataframe
273
+ BreakdownDF.at[i,'Compound Name'] = row2['Compound Name']
274
+
275
+ #Add the other relevant information to the breakdown dataframe
276
+ BreakdownDF.at[i,'Formula'] = row2['Formula']
277
+ BreakdownDF.at[i,'RF (Area/vol.%)'] = row2['RF']
278
+ BreakdownDF.at[i,'MW (g/mol)'] = ChemFormula(row2['Formula']).formula_weight
279
+
280
+ #Get estimated volume percent using response factor
281
+ volpercent_e = row['Area']/row2['RF']
282
+ BreakdownDF.at[i,'Est. Vol.%'] = volpercent_e
283
+
284
+ #Get adjusted volume percent using scale factor
285
+ volpercent_a = volpercent_e * SF
286
+ BreakdownDF.at[i,'Adj. Vol.%'] = volpercent_a
287
+
288
+ #Get moles using ideal gas law (PV=nRT)
289
+ BreakdownDF.at[i,'Moles (mol)'] = C * (GB_pressure * volpercent_a / 100 * V_TC)/(R * (GB_temp + 273.15))
290
+
291
+ #Get mass (mg) using moles and molar mass
292
+ BreakdownDF.at[i,'Mass (mg)'] = BreakdownDF.at[i,'Moles (mol)'] * BreakdownDF.at[i,'MW (g/mol)'] * 1000
293
+
294
+ #Otherwise, pass
295
+ else:
296
+ pass
297
+
298
+ return BreakdownDF, V_TC, SF
299
+
300
+ BreakdownDF, V_TC, SF = quantTCD(BreakdownDF,DBRF,gasBag_cond,reactor_cond)
301
+
302
+ return BreakdownDF, V_TC, SF
303
+
304
+ #Function for quantifying gas TCD data w/ internal standard method
305
+ def gasTCD_IS(BreakdownDF,DBRF,gasBag_cond,reactor_cond,peak_error):
306
+
307
+ #Initialize compound name column in BreakdownDF
308
+ BreakdownDF['Compound Name'] = 'None'
309
+
310
+ #Define molar mass of CO2, g/mol
311
+ Mc = 44.009
312
+
313
+ # Function to estimate mass of CO2 from provided conditions and get area of CO2
314
+ def massCO2(BreakdownDF,DBRF,R,C,V_C,P_0,GB_temp):
315
+
316
+ #Find the CO2 peak row in DBRF
317
+ CO2_row = DBRF.loc[DBRF['Compound Name'] == "Carbon Dioxide"].iloc[0]
318
+
319
+ #Get the retention time
320
+ CO2_RT = CO2_row['RT (min)']
321
+
322
+ #Get the minimum and maximum of the RT range using the peak error
323
+ CO2_RTmin = CO2_RT - peak_error
324
+ CO2_RTmax = CO2_RT + peak_error
325
+
326
+ #Define boolean describing whether or not CO2 match has been found
327
+ CO2_bool = False
328
+
329
+ #Iterate through every row in BreakdownDF
330
+ for i, row in BreakdownDF.iterrows():
331
+
332
+ #If the TCD retention time is within range of the CO2 entry...
333
+ if CO2_RTmin <= row['RT'] <= CO2_RTmax:
334
+
335
+ #Get mass of CO2, mg
336
+ mc = C * Mc * (P_0 * V_C) / (R * (GB_temp + 273.15)) * 1000
337
+
338
+ #Get area of CO2
339
+ Ac = row['Area']
340
+
341
+ #Set CO2_bool to True
342
+ CO2_bool = True
343
+
344
+ break
345
+
346
+ #Otherwise, pass
347
+ else:
348
+ pass
349
+
350
+ #Raise error if no CO2 peak found
351
+ if CO2_bool == False:
352
+ raise Exception("[gasTCD][ERROR] No CO2 peak found in TCD")
353
+
354
+ else:
355
+ pass
356
+
357
+ return mc, Ac
358
+
359
+ #Function to calculate amounts of each species using the scale factor
360
+ def quantTCD(BreakdownDF,DBRF,gasBag_cond,reactor_cond):
361
+
362
+ # Unpack gas bag conditions
363
+ GB_temp = gasBag_cond[0] # Temperature of gas bag, C
364
+ GB_pressure = gasBag_cond[1] # Sample pressure in gas bag, psig
365
+ V_C = gasBag_cond[2] # CO2 volume, mL
366
+
367
+ #Unpack reactor conditions
368
+ P_f = reactor_cond[0] #reactor quench pressure, psig
369
+ V_R = reactor_cond[1] #reactor internal volume, mL
370
+ P_0 = reactor_cond[2] #atmospheric pressure, psi
371
+
372
+ # Define ideal gas constant, m^3*Pa/K*mol
373
+ R = 8.314
374
+
375
+ #Define conversion factor, Pa * m^3 * psi^-1 * mL^-1
376
+ C = 0.00689476
377
+
378
+ #Get mass of CO2
379
+ mc, Ac = massCO2(BreakdownDF,DBRF,R,C,V_C,P_0,GB_temp)
380
+
381
+ #Get total volume plus CO2
382
+ V_T = V_R * (P_f + P_0) / P_0
383
+ V_TC = V_T + V_C
384
+ print(V_TC)
385
+ #Add min and max peak assignment values to DBRF
386
+ for i, row in DBRF.iterrows():
387
+ DBRF.at[i,'RT Max'] = DBRF.at[i,'RT (min)'] + peak_error
388
+ DBRF.at[i,'RT Min'] = DBRF.at[i,'RT (min)'] - peak_error
389
+
390
+ #Iterate through every row in BreakdownDF
391
+ for i, row in BreakdownDF.iterrows():
392
+
393
+ #Iterate through every row in DBRF
394
+ for i2, row2 in DBRF.iterrows():
395
+
396
+ #If the TCD retention time is within the range for a given DBRF entry...
397
+ if row2['RT Min'] <= row['RT'] <= row2['RT Max']:
398
+
399
+ #Add the compound name to the breakdown dataframe
400
+ BreakdownDF.at[i,'Compound Name'] = row2['Compound Name']
401
+
402
+ #Add the other relevant information to the breakdown dataframe
403
+ BreakdownDF.at[i,'Formula'] = row2['Formula']
404
+ BreakdownDF.at[i,'RF'] = row2['RF']
405
+ BreakdownDF.at[i,'MW (g/mol)'] = ChemFormula(row2['Formula']).formula_weight
406
+
407
+ #If the peak is CO2...
408
+ if row['Compound Name'] == "Carbon Dioxide":
409
+ #Set mass to mc
410
+ BreakdownDF.at[i,'Mass(mg)'] = mc
411
+
412
+ #Otherwise...
413
+ else:
414
+ #Get mass (mg) using mass of carbon dioxide and response factor
415
+ BreakdownDF.at[i,'Mass (mg)'] = (mc * (BreakdownDF.at[i,'Area'] / Ac)) / BreakdownDF.at[i,'RF']
416
+
417
+ #Otherwise, pass
418
+ else:
419
+ pass
420
+
421
+ return BreakdownDF, V_TC
422
+
423
+ BreakdownDF, V_TC = quantTCD(BreakdownDF,DBRF,gasBag_cond,reactor_cond)
424
+
425
+ return BreakdownDF, V_TC