# HG changeset patch # User luca_milaz # Date 1721589526 0 # Node ID 5971a04cc59afe48c408528e2e6dd48b32ca212c # Parent 0ece6120498313561f727552aa10b0ea0d101b78 Uploaded diff -r 0ece61204983 -r 5971a04cc59a marea_2/ras_to_bounds.py --- 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: