44import matplotlib .pyplot as plt
55import matplotlib .patches as patches
66sns .set_context ('talk' )
7- import os
7+ import os
88import numpy as np
9- from tqdm import tqdm
9+ from tqdm import tqdm
1010from dataclasses import dataclass
1111import toml
1212
@@ -26,10 +26,11 @@ class LC_FTICR_WorkflowParameters:
2626 end_time : int # minutes
2727 time_block : int #seconds
2828 # removed original values (check if I should do that)
29- # Reference mass list:
29+ # Reference mass list:
3030 refmasslist_neg : str = "data/referencec/Hawkes_neg.ref"
3131 # input output paths
32- input_file_path : str = "data/raw_data/..."
32+ full_input_file_path : str = "data/raw_data/..."
33+ input_file_directory : str = "data/raw_data/..."
3334 input_file_name : str = "..."
3435 output_directory : str = "data/..."
3536 output_file_name : str = "..."
@@ -50,19 +51,20 @@ def to_toml(self):
5051
5152
5253 ### function that init parser and get data
53- def init_parser_extract_data (self ) -> pd .DataFrame :
54+ def init_parser_extract_data (self ) -> pd .DataFrame :
5455 # Define datafile location
55- file_in = self .input_file_path + self . input_file_name
56+ file_in = self .full_input_file_path
5657
5758 # Start with all scans
58- LCMSParameters .lc_ms .scans = (- 1 , - 1 ) # this needs to be read in
59+ LCMSParameters .lc_ms .scans = (- 1 , - 1 ) # this needs to be read in
5960 # the (-1, -1) tells software to choose all scans that exist
6061
6162 # Init parser object
63+ print (file_in )
6264 parser = ImportMassSpectraThermoMSFileReader (file_in )
63-
65+
6466 # Get the TIC data, scan ids, etc
65- # TIC = Total Ion Chromatogram
67+ # TIC = Total Ion Chromatogram
6668 tic_data = parser .get_tic (ms_type = None , peak_detection = False , smooth = False , plot = False , trace_type = 'TIC' )[0 ]
6769 tic_df = pd .DataFrame (index = tic_data .scans , columns = ['scans' , 'tic' , 'time' ])
6870 tic_df ['scans' ] = tic_data .scans
@@ -76,23 +78,23 @@ def init_parser_extract_data(self) -> pd.DataFrame:
7678 # Process the time block mass spectrum
7779 def proc_time_block_inner (self , msreader , datafile , block ):
7880 # scans = list(subset_df['scan'])
79-
81+
8082 # load_and_set_toml_parameters_ms(MSParameters, self.corems_toml_path)
8183 with open (self .ms_toml_path , "r" ) as infile :
8284 MSParameters .mass_spectrum = MassSpectrumSetting (** toml .load (infile ))
8385 with open (self .mspeak_toml_path , "r" ) as infile :
8486 MSParameters .ms_peak = MassSpecPeakSetting (** toml .load (infile ))
8587 msobj = msreader .get_average_mass_spectrum (spectrum_mode = 'profile' ,auto_process = True )
86-
88+
8789 #msobj.clear_molecular_formulas()
8890 MzDomainCalibration (msobj , self .refmasslist_neg ).run ()
8991
9092 #set_mf_settings(msobj)
9193 load_and_set_toml_parameters_class ("MolecularFormulaSearch" , msobj .molecular_search_settings , parameters_path = self .mfsearch_toml_path )
9294
9395 SearchMolecularFormulas (msobj ).run_worker_mass_spectrum ()
94-
95-
96+
97+
9698 msdf = msobj .to_dataframe ()
9799 msdf ['datafile' ] = datafile
98100 msdf ['block' ] = block
@@ -116,7 +118,7 @@ def proc_time_block_inner(self, msreader, datafile, block):
116118
117119 def process_with_time_block (self , tic_df ):
118120 # Strip out the time where there's no useful data
119- file_in = self .input_file_path + self . input_file_name
121+ file_in = self .full_input_file_path
120122 tic_df = tic_df [(tic_df ['time' ] > self .start_time ) & (tic_df ['time' ] < self .end_time )]
121123
122124 # Block the scans into 30-second blocks, for signal averaging.
@@ -138,7 +140,7 @@ def process_with_time_block(self, tic_df):
138140 LCMSParameters .lc_ms .scans = (scan_tuple )
139141 # Init parser object
140142 parser = ImportMassSpectraThermoMSFileReader (file_in )
141-
143+
142144 msdf , statdict = self .proc_time_block_inner (parser , file_in , block ) ### i'm not sure about this self.function()
143145 all_msdfs_in_file .append (msdf )
144146 all_statdics .append (statdict )
@@ -155,7 +157,7 @@ def create_summary(self, all_statdics):
155157 # Create a DataFrame
156158 summary_df = pd .DataFrame (flat_list )
157159 summary_df .to_csv (self .output_directory + self .output_file_name + "-statdicts.csv" )
158- return (summary_df )
160+ return (summary_df )
159161
160162
161163################################### LC-FTICR PLOTS ###################################
@@ -164,22 +166,22 @@ def create_summary(self, all_statdics):
164166## for creating plots
165167def filter_out_common_background (df ):
166168 formula_block_counts = df .pivot_table (index = 'Molecular Formula' , columns = 'block' , aggfunc = 'size' , fill_value = 0 )
167-
169+
168170 # Filter to get 'Molecular Formula' entries that appear in all blocks
169171 common_formulas = formula_block_counts [formula_block_counts .gt (0 ).sum (axis = 1 ) == len (df ['block' ].unique ())].index
170-
172+
171173 # Step 2: Further filter based on similar 'Peak Height' values (using a threshold)
172174 # Create a function to check if 'Peak Height' values are within a tolerance
173175 def peak_height_similar (df , tolerance = 0.99 ): # 10% tolerance
174176 peak_heights = df ['Peak Height' ]
175177 return peak_heights .max () - peak_heights .min () <= tolerance * peak_heights .mean ()
176-
178+
177179 # Group the dataframe by 'Molecular Formula' and apply the peak height similarity check
178180 similar_peak_height_formulas = df .groupby ('Molecular Formula' ).filter (lambda x : peak_height_similar (x ))['Molecular Formula' ].unique ()
179-
181+
180182 # Combine both conditions
181183 formulas_to_remove = set (common_formulas ).intersection (similar_peak_height_formulas )
182-
184+
183185 # Step 3: Remove these entries from the dataframe
184186 filtered_df = df [~ df ['Molecular Formula' ].isin (formulas_to_remove )]
185187 return filtered_df
@@ -271,7 +273,7 @@ def plot_properties(summary_df_path,output_dir):
271273 axes [i , 0 ].set_title (f'Trend of { prop } by Block' )
272274 axes [i , 0 ].set_xlabel ('Block' )
273275 axes [i , 0 ].set_ylabel (prop )
274-
276+
275277 # Distribution plot
276278 sns .histplot (summary_df [prop ], kde = True , ax = axes [i , 1 ], bins = 10 , color = 'skyblue' )
277279 axes [i , 1 ].set_title (f'Distribution of { prop } ' )
@@ -301,9 +303,43 @@ def run_LC_FTICR_workflow(lc_fticr_workflow_paramaters_toml_file):
301303 plot_properties (summary_df , lc_object .output_directory )
302304 return ()
303305
304- def run_LC_FTICR_workflow_wdl (* args , ** kwargs ):
305- # read in LC_WorkflowParameters from toml file
306- lc_object = LC_FTICR_WorkflowParameters (** kwargs )
306+ def run_LC_FTICR_workflow_wdl (
307+ start_time ,
308+ end_time ,
309+ time_block ,
310+ refmasslist_neg ,
311+ full_input_file_path ,
312+ input_file_directory ,
313+ input_file_name ,
314+ output_directory ,
315+ output_file_name ,
316+ output_file_type ,
317+ lc_fticr_toml_path ,
318+ ms_toml_path ,
319+ mspeak_toml_path ,
320+ mfsearch_toml_path ,
321+ plot_van_krevelen_all_ids ,
322+ plot_van_krevelen_individual ,
323+ plot_properties ,
324+ ):
325+ # read in LC_WorkflowParameters from wdl inputs
326+ lc_object = LC_FTICR_WorkflowParameters (start_time = start_time ,
327+ end_time = end_time ,
328+ time_block = time_block ,
329+ refmasslist_neg = refmasslist_neg ,
330+ full_input_file_path = full_input_file_path ,
331+ input_file_directory = input_file_directory ,
332+ input_file_name = input_file_name ,
333+ output_directory = output_directory ,
334+ output_file_name = output_file_name ,
335+ output_file_type = output_file_type ,
336+ lc_fticr_toml_path = lc_fticr_toml_path ,
337+ ms_toml_path = ms_toml_path ,
338+ mspeak_toml_path = mspeak_toml_path ,
339+ mfsearch_toml_path = mfsearch_toml_path ,
340+ plot_van_krevelen_all_ids = plot_van_krevelen_all_ids ,
341+ plot_van_krevelen_individual = plot_van_krevelen_individual ,
342+ plot_properties = plot_properties )
307343 # call functions
308344 tic_df = lc_object .init_parser_extract_data ()
309345 all_msdfs_df , all_statdics_df = lc_object .process_with_time_block (tic_df )
0 commit comments