@@ -30,6 +30,8 @@ def __init__(self, type_of_file):
30
30
'isotopologue_proportions' ,
31
31
'isotopologues' ,
32
32
'abundances' ]
33
+ self .metabolites2isotopologues_df = None
34
+ self .user_given_names = dict ()
33
35
34
36
def load_metadata (self , metadata_path ):
35
37
self .metadata = ut .open_metadata (metadata_path )
@@ -123,48 +125,79 @@ def transpose_frames(self):
123
125
def load_metabolite_to_isotopologue_df (self , confdict ):
124
126
"""df of correspondences between isotopologues and metabolites
125
127
proper to the given data"""
126
- try :
127
- isotopologues_full = list (self .frames_dict [confdict [
128
- "isotopologue_proportions" ]].index )
129
- # ok apply same proposed solution whether Key or Type error:
130
- except TypeError :
131
- isotopologues_full = list (self .frames_dict [confdict [
132
- "isotopologues" ]].index )
133
- except KeyError :
134
- isotopologues_full = list (self .frames_dict [confdict [
135
- "isotopologues" ]].index )
136
-
137
- self .metabolites2isotopologues_df = ut .isotopologues_meaning_df (
138
- isotopologues_full )
128
+ if (confdict ['isotopologue_proportions' ] is not None ) or (
129
+ confdict ['isotopologues' ] is not None ):
130
+ try :
131
+ isotopologues_full = list (self .frames_dict [confdict [
132
+ "isotopologue_proportions" ]].index )
133
+ # ok apply same proposed solution whether Key or Type error:
134
+ except TypeError :
135
+ isotopologues_full = list (self .frames_dict [confdict [
136
+ "isotopologues" ]].index )
137
+ except KeyError :
138
+ isotopologues_full = list (self .frames_dict [confdict [
139
+ "isotopologues" ]].index )
140
+
141
+ self .metabolites2isotopologues_df = ut .isotopologues_meaning_df (
142
+ isotopologues_full )
143
+
144
+ def set_user_given_names (self , confdict ):
145
+ """
146
+ Set user given names of quantifications only.
147
+ This will be useful for final_files_names property"""
148
+ user_given_names = dict ()
149
+ for keyname_quantif in self .expected_keys_confdict :
150
+ if confdict [keyname_quantif ] is not None :
151
+ user_given_names [keyname_quantif ] = confdict [
152
+ keyname_quantif ]
153
+ self .user_given_names = user_given_names
139
154
140
155
def fill_missing_data (self , confdict ) -> Dict [str , str ]:
156
+ """
157
+ Computes the quantification that the user set None in confdict.
158
+ This is done using the absolute isotopic values, and if not provided,
159
+ will compute mean enrichment from isotopic proportions.
160
+ """
141
161
tmp , confdict_new = ut .complete_missing_frames (
142
162
confdict , self .frames_dict , self .metabolites2isotopologues_df )
143
163
self .frames_dict = tmp
144
164
145
165
return confdict_new
146
166
147
- def true_key_value_available_frames (self , confdict ):
167
+ def update_truly_available_frames (self , confdict ):
168
+ """
169
+ Sets the properties .available_frames and .reverse_available_frames,
170
+ which are dictionaries of quantifications names that truly exist
171
+ in the object (in the frames_dict)
172
+ """
148
173
reverse_dict = dict ()
149
174
avail_dict = dict ()
150
175
true_reverse_dict = dict ()
151
176
for m in self .expected_keys_confdict :
152
- reverse_dict [confdict [m ]] = m
177
+ try :
178
+ reverse_dict [confdict [m ]] = m
179
+ except KeyError :
180
+ continue
153
181
for h in self .frames_dict .keys ():
182
+ # if the quantification content and key exists
154
183
if (self .frames_dict [h ] is not None ) and (h is not None ):
155
- avail_dict [reverse_dict [h ]] = h
156
- true_reverse_dict [h ] = reverse_dict [h ]
184
+ try :
185
+ avail_dict [reverse_dict [h ]] = h
186
+ true_reverse_dict [h ] = reverse_dict [h ]
187
+ except Exception as e :
188
+ print (e )
189
+ continue
157
190
158
191
self .available_frames = avail_dict
159
192
self .reverse_available_frames = true_reverse_dict
160
193
161
194
def save_isotopologues_preview (self , args , confdict , groom_out_path ):
162
- compartmentalized_dict = ut .df_to__dic_bycomp (
163
- self .frames_dict [confdict ['isotopologue_proportions' ]],
164
- self .metadata )
165
- output_plots_dir = os .path .join (groom_out_path , "preview_plots" )
166
195
if args .isotopologues_preview :
167
- logger .info (f"prepare isotopologue proportions overview figures" )
196
+ compartmentalized_dict = ut .df_to__dic_bycomp (
197
+ self .frames_dict [confdict ['isotopologue_proportions' ]],
198
+ self .metadata )
199
+ output_plots_dir = os .path .join (groom_out_path , "preview_plots" )
200
+ logger .info ("prepare isotopologue proportions overview figures" )
168
201
if not os .path .exists (output_plots_dir ):
169
202
os .makedirs (output_plots_dir )
170
203
ut .save_isos_preview (
@@ -178,8 +211,8 @@ def pull_internal_standard(self, confdict, args):
178
211
):
179
212
try :
180
213
x = self .frames_dict [confdict ['abundances' ]].columns .tolist ()
181
- y = self .frames_dict [confdict ['abundances' ]
182
- ]. loc [ args .use_internal_standard , :].tolist ()
214
+ y = self .frames_dict [confdict ['abundances' ]]. loc [
215
+ args .use_internal_standard , :].tolist ()
183
216
instandard_abun_df = pd .DataFrame (
184
217
{"sample" : x ,
185
218
args .use_internal_standard : y
@@ -228,6 +261,41 @@ def normalize_by_internal_standard(self, args, confdict):
228
261
args .use_internal_standard )
229
262
self .frames_dict = frames_dict
230
263
264
+ def set_final_files_names (self ):
265
+ """
266
+ Set final names of output files:
267
+ 1. set final names dictionary and add as attribute, and
268
+ 2. assign the values of 1. to the keys of the object.frames_dict
269
+ """
270
+ not_user_defined_dict = ut .retrieve_dict_not_user_defined ()
271
+ final_files_names_d = dict () # set final names dictionary
272
+ for valuename in self .reverse_available_frames .keys ():
273
+ keyname = self .reverse_available_frames [valuename ]
274
+ if keyname in list (self .user_given_names .keys ()):
275
+ final_files_names_d [keyname ] = self .user_given_names [keyname ]
276
+ else :
277
+ if self .available_frames [keyname ] is not None :
278
+ final_files_names_d [keyname ] = not_user_defined_dict [
279
+ keyname ]
280
+ # end for
281
+ frames_names_list = list (self .frames_dict .keys ())
282
+ for frame_name in frames_names_list : # assign final names to frames
283
+ if frame_name == "abundances_computed" :
284
+ self .frames_dict [final_files_names_d [
285
+ "abundances" ]] = self .frames_dict [frame_name ]
286
+ del self .frames_dict [frame_name ]
287
+ if frame_name == "mean_enrichment_computed" :
288
+ self .frames_dict [final_files_names_d [
289
+ "mean_enrichment" ]] = self .frames_dict [frame_name ]
290
+ del self .frames_dict [frame_name ]
291
+ if frame_name == "isotopologue_props_computed" :
292
+ self .frames_dict [final_files_names_d [
293
+ "isotopologue_proportions" ]] = self .frames_dict [
294
+ frame_name ]
295
+ del self .frames_dict [frame_name ]
296
+ # end for
297
+ self .final_files_names = final_files_names_d
298
+
231
299
def compartmentalize_frames_dict (self ):
232
300
for k in self .frames_dict .keys ():
233
301
tmp = ut .df_to__dic_bycomp (
@@ -257,7 +325,8 @@ def drop_metabolites(self):
257
325
def frames_filterby_min_admited_isotopol_proportions (
258
326
self , confdict , isosprop_min_admitted : float
259
327
):
260
- isos_propor_dic = self .frames_dict [confdict ['isotopologue_proportions' ]]
328
+ isos_propor_dic = self .frames_dict [
329
+ confdict ['isotopologue_proportions' ]]
261
330
bad_mets = dict ()
262
331
for co in isos_propor_dic .keys ():
263
332
tmp = isos_propor_dic [co ]
@@ -270,21 +339,27 @@ def frames_filterby_min_admited_isotopol_proportions(
270
339
self .frames_dict , self .reverse_available_frames , bad_mets )
271
340
self .frames_dict = tmp
272
341
273
- def stomp_fraction_values (self , args , confdict ):
342
+ def stomp_fraction_values (self , args , final_files_names : dict ):
274
343
if args .fractions_stomp_values :
275
344
for frac_type in ["mean_enrichment" , "isotopologue_proportions" ]:
276
- curr_dict = self .frames_dict [confdict [frac_type ]]
277
- for co in curr_dict .keys ():
278
- df = curr_dict [co ]
279
- df [df < 0 ] = 0
280
- df [df > 1 ] = 1
281
- curr_dict [co ] = df
282
- self .frames_dict [confdict [frac_type ]] = curr_dict
283
-
284
- def transfer__abund_nan__to_all_tables (self , confdict ):
285
- tmp = ut .transfer__abund_nan__to_all_tables (
286
- confdict , self .frames_dict , self .metadata )
287
- self .frames_dict = tmp
345
+ try :
346
+ curr_dict = self .frames_dict [final_files_names [frac_type ]]
347
+ for co in curr_dict .keys ():
348
+ df = curr_dict [co ]
349
+ df [df < 0 ] = 0
350
+ df [df > 1 ] = 1
351
+ curr_dict [co ] = df
352
+ self .frames_dict [final_files_names [frac_type ]] = curr_dict
353
+ except KeyError as e :
354
+ logger .info (f"{ e } : unavailable (stomp fractions)" )
355
+
356
+ def transfer__abund_nan__to_all_tables (self , final_files_names : dict ):
357
+ try :
358
+ tmp = ut .transfer__abund_nan__to_all_tables (
359
+ final_files_names , self .frames_dict , self .metadata )
360
+ self .frames_dict = tmp
361
+ except KeyError as e :
362
+ logger .info (f"{ e } : unavailable (propagate NaN values)" )
288
363
289
364
# end class
290
365
@@ -330,12 +405,11 @@ def save_tables(frames_dict, groom_out_path, output_extension) -> None:
330
405
def wrapper_common_steps (combo_data : CompositeData ,
331
406
args , confdict , groom_out_path : str ) -> None :
332
407
combo_data .load_metabolite_to_isotopologue_df (confdict )
333
- confdict = combo_data .fill_missing_data (confdict )
334
- combo_data .true_key_value_available_frames (confdict )
335
-
408
+ combo_data .set_user_given_names (confdict ) # only user def names
409
+ confdict = combo_data .fill_missing_data (confdict ) # critical completion
410
+ combo_data . update_truly_available_frames ( confdict ) # update 1
336
411
combo_data .save_isotopologues_preview (args , confdict , groom_out_path )
337
-
338
- combo_data .pull_internal_standard (confdict , args )
412
+ combo_data .pull_internal_standard (confdict , args ) # before normalisation
339
413
340
414
if combo_data .material_df is not None :
341
415
logger .info ("computing normalization by amount of material" )
@@ -345,12 +419,16 @@ def wrapper_common_steps(combo_data: CompositeData,
345
419
args , confdict )
346
420
else :
347
421
combo_data .normalize_total_abundance_by_material (args , confdict )
422
+
348
423
combo_data .normalize_by_internal_standard (args , confdict )
424
+ combo_data .set_final_files_names ()
425
+ combo_data .update_truly_available_frames (combo_data .final_files_names ) # update 2
349
426
combo_data .compartmentalize_frames_dict ()
350
427
# last steps use compartmentalized frames
351
428
combo_data .drop_metabolites ()
352
- combo_data .stomp_fraction_values (args , confdict )
353
- combo_data .transfer__abund_nan__to_all_tables (confdict )
429
+ combo_data .stomp_fraction_values (args , combo_data .final_files_names )
430
+ combo_data .transfer__abund_nan__to_all_tables (
431
+ combo_data .final_files_names )
354
432
save_tables (combo_data .frames_dict , groom_out_path ,
355
433
args .output_files_extension )
356
434
0 commit comments