changeset 115:5971a04cc59a draft

Uploaded
author luca_milaz
date Sun, 21 Jul 2024 19:18:46 +0000
parents 0ece61204983
children 902921535134
files marea_2/ras_to_bounds.py
diffstat 1 files changed, 26 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/marea_2/ras_to_bounds.py	Sun Jul 21 19:02:05 2024 +0000
+++ b/marea_2/ras_to_bounds.py	Sun Jul 21 19:18:46 2024 +0000
@@ -74,6 +74,30 @@
         log.write(s + "\n\n")
     print(s)
 
+############################ dataset input ####################################
+def read_dataset(data :str, name :str) -> pd.DataFrame:
+    """
+    Read a dataset from a CSV file and return it as a pandas DataFrame.
+
+    Args:
+        data (str): Path to the CSV file containing the dataset.
+        name (str): Name of the dataset, used in error messages.
+
+    Returns:
+        pandas.DataFrame: DataFrame containing the dataset.
+
+    Raises:
+        pd.errors.EmptyDataError: If the CSV file is empty.
+        sys.exit: If the CSV file has the wrong format, the execution is aborted.
+    """
+    try:
+        dataset = pd.read_csv(data, sep = '\t', header = 0, engine='python')
+    except pd.errors.EmptyDataError:
+        sys.exit('Execution aborted: wrong format of ' + name + '\n')
+    if len(dataset.columns) < 2:
+        sys.exit('Execution aborted: wrong format of ' + name + '\n')
+    return dataset
+
 def generate_bounds(model:cobra.Model, medium:dict, ras=None) -> pd.DataFrame:
     model_new = model.copy()
     rxns_ids = []
@@ -132,7 +156,7 @@
     mediumPath = utils.FilePath("medium", ".csv", prefix = ARGS.output_folder)
 
     if(ARGS.ras_selector == True):
-        ras = pd.read_table(ARGS.input_ras, header=0, sep=r'\s+', index_col = 0).T
+        ras = read_dataset(ARGS.input_ras, "ras dataset")
         ras.replace("None", None, inplace=True)
         ras = ras.astype(float)
     
@@ -143,7 +167,7 @@
         model = model_type.getCOBRAmodel(toolDir=ARGS.tool_dir)
 
     if(ARGS.medium_selector == "Custom"):
-        medium = pd.read_csv(ARGS.input_medium, index_col = 0)
+        medium = read_dataset(ARGS.input_medium, "medium dataset")
         medium = medium.astype(float)
         medium = medium['medium'].to_dict()
     else: