diff COBRAxy/flux_simulation.py @ 147:3fca9b568faf draft

Uploaded
author bimib
date Wed, 06 Nov 2024 13:57:24 +0000
parents accda943dfb9
children ebd2065dbdc2
line wrap: on
line diff
--- a/COBRAxy/flux_simulation.py	Wed Nov 06 10:12:52 2024 +0000
+++ b/COBRAxy/flux_simulation.py	Wed Nov 06 13:57:24 2024 +0000
@@ -11,7 +11,7 @@
 import sys
 
 ################################# process args ###############################
-def process_args(args :List[str]) -> argparse.Namespace:
+def process_args(args :List[str] = None) -> argparse.Namespace:
     """
     Processes command-line arguments.
 
@@ -88,7 +88,13 @@
                         required = False,
                         help = 'output type analysis')
     
-    ARGS = parser.parse_args()
+    parser.add_argument(
+        '-idop', '--output_path', 
+        type = str,
+        default='result',
+        help = 'output path for maps')
+    
+    ARGS = parser.parse_args(args)
     return ARGS
 
 ########################### warning ###########################################
@@ -109,7 +115,7 @@
 
 def write_to_file(dataset: pd.DataFrame, name: str, keep_index:bool=False)->None:
     dataset.index.name = 'Reactions'
-    dataset.to_csv(ARGS.output_folder + name + ".csv", sep = '\t', index = keep_index)
+    dataset.to_csv(ARGS.output_path + name + ".csv", sep = '\t', index = keep_index)
 
 ############################ dataset input ####################################
 def read_dataset(data :str, name :str) -> pd.DataFrame:
@@ -156,17 +162,17 @@
     for i in range(0, n_batches):
         optgp = OptGPSampler(model, thinning, seed)
         samples = optgp.sample(n_samples)
-        samples.to_csv(ARGS.output_folder +  model_name + '_'+ str(i)+'_OPTGP.csv', index=False)
+        samples.to_csv(ARGS.output_path +  model_name + '_'+ str(i)+'_OPTGP.csv', index=False)
         seed+=1
     samplesTotal = pd.DataFrame()
     for i in range(0, n_batches):
-        samples_batch = pd.read_csv(ARGS.output_folder  +  model_name + '_'+ str(i)+'_OPTGP.csv')
+        samples_batch = pd.read_csv(ARGS.output_path  +  model_name + '_'+ str(i)+'_OPTGP.csv')
         samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True)
 
     write_to_file(samplesTotal.T, model_name, True)
 
     for i in range(0, n_batches):
-        os.remove(ARGS.output_folder +   model_name + '_'+ str(i)+'_OPTGP.csv')
+        os.remove(ARGS.output_path +   model_name + '_'+ str(i)+'_OPTGP.csv')
     pass
 
 
@@ -199,18 +205,18 @@
             ARGS.out_log)
             CBS_backend.randomObjectiveFunctionSampling_cobrapy(model, n_samples, df_coefficients.iloc[:,i*n_samples:(i+1)*n_samples], 
                                                     samples)
-        utils.logWarning(ARGS.output_folder +  model_name + '_'+ str(i)+'_CBS.csv', ARGS.out_log)
-        samples.to_csv(ARGS.output_folder +  model_name + '_'+ str(i)+'_CBS.csv', index=False)
+        utils.logWarning(ARGS.output_path +  model_name + '_'+ str(i)+'_CBS.csv', ARGS.out_log)
+        samples.to_csv(ARGS.output_path +  model_name + '_'+ str(i)+'_CBS.csv', index=False)
 
     samplesTotal = pd.DataFrame()
     for i in range(0, n_batches):
-        samples_batch = pd.read_csv(ARGS.output_folder  +  model_name + '_'+ str(i)+'_CBS.csv')
+        samples_batch = pd.read_csv(ARGS.output_path  +  model_name + '_'+ str(i)+'_CBS.csv')
         samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True)
 
     write_to_file(samplesTotal.T, model_name, True)
 
     for i in range(0, n_batches):
-        os.remove(ARGS.output_folder +   model_name + '_'+ str(i)+'_CBS.csv')
+        os.remove(ARGS.output_path +   model_name + '_'+ str(i)+'_CBS.csv')
     pass
 
 
@@ -244,7 +250,7 @@
     df_mean, df_median, df_quantiles = fluxes_statistics(name, ARGS.output_types)
 
     if("fluxes" not in ARGS.output_types):
-        os.remove(ARGS.output_folder  +  name + '.csv')
+        os.remove(ARGS.output_path  +  name + '.csv')
 
     returnList = []
     returnList.append(df_mean)
@@ -278,7 +284,7 @@
     df_median= pd.DataFrame()
     df_quantiles= pd.DataFrame()
 
-    df_samples = pd.read_csv(ARGS.output_folder  +  model_name + '.csv', sep = '\t', index_col = 0).T
+    df_samples = pd.read_csv(ARGS.output_path  +  model_name + '.csv', sep = '\t', index_col = 0).T
     df_samples = df_samples.round(8)
 
     for output_type in output_types:
@@ -363,7 +369,7 @@
     return df_pFBA, df_FVA, df_sensitivity
 
 ############################# main ###########################################
-def main() -> None:
+def main(args :List[str] = None) -> None:
     """
     Initializes everything and sets the program in motion based on the fronted input arguments.
 
@@ -376,10 +382,8 @@
     num_processors = cpu_count()
 
     global ARGS
-    ARGS = process_args(sys.argv)
-
-    ARGS.output_folder = 'flux_simulation/'
-    
+    ARGS = process_args(args)
+  
     
     model_type :utils.Model = ARGS.model_selector
     if model_type is utils.Model.Custom: