Mercurial > repos > bimib > cobraxy
comparison COBRAxy/fromCSVtoCOBRA_beta.py @ 453:51f794fff930 draft
Uploaded
| author | francesco_lapi |
|---|---|
| date | Thu, 11 Sep 2025 20:58:37 +0000 |
| parents | a4e8f3188813 |
| children | 3654c08668f1 |
comparison
equal
deleted
inserted
replaced
| 452:40a8eeb5f614 | 453:51f794fff930 |
|---|---|
| 11 import utils.reaction_parsing as reactionUtils | 11 import utils.reaction_parsing as reactionUtils |
| 12 import utils.model_utils as modelUtils | 12 import utils.model_utils as modelUtils |
| 13 | 13 |
| 14 ARGS : argparse.Namespace | 14 ARGS : argparse.Namespace |
| 15 def process_args(args: List[str] = None) -> argparse.Namespace: | 15 def process_args(args: List[str] = None) -> argparse.Namespace: |
| 16 """ | |
| 17 Parse command-line arguments for CustomDataGenerator. | |
| 18 """ | |
| 19 | |
| 20 parser = argparse.ArgumentParser( | 16 parser = argparse.ArgumentParser( |
| 21 usage="%(prog)s [options]", | 17 usage="%(prog)s [options]", |
| 22 description="Generate custom data from a given model" | 18 description="Convert a tabular/CSV file to a COBRA model" |
| 23 ) | 19 ) |
| 24 | 20 |
| 21 | |
| 25 parser.add_argument("--out_log", type=str, required=True, | 22 parser.add_argument("--out_log", type=str, required=True, |
| 26 help="Output log file") | 23 help="Output log file") |
| 27 | 24 |
| 28 parser.add_argument("--input", type=str, | 25 |
| 29 help="Input tabular file") | 26 parser.add_argument("--input", type=str, required=True, |
| 30 | 27 help="Input tabular file (CSV/TSV)") |
| 28 | |
| 29 | |
| 31 parser.add_argument("--format", type=str, required=True, choices=["sbml", "json", "mat", "yaml"], | 30 parser.add_argument("--format", type=str, required=True, choices=["sbml", "json", "mat", "yaml"], |
| 32 help="Model format (SBML, JSON, MATLAB, YAML)") | 31 help="Model format (SBML, JSON, MATLAB, YAML)") |
| 33 | 32 |
| 34 parser.add_argument("--output", type=str, | 33 |
| 35 help="Output model file") | 34 parser.add_argument("--output", type=str, required=True, |
| 36 | 35 help="Output model file path") |
| 36 | |
| 37 | |
| 37 parser.add_argument("--tool_dir", type=str, default=os.path.dirname(__file__), | 38 parser.add_argument("--tool_dir", type=str, default=os.path.dirname(__file__), |
| 38 help="Tool directory (passed from Galaxy as $__tool_directory__)") | 39 help="Tool directory (passed from Galaxy as $__tool_directory__)") |
| 39 | |
| 40 | 40 |
| 41 | 41 |
| 42 return parser.parse_args(args) | 42 return parser.parse_args(args) |
| 43 | 43 |
| 44 | |
| 44 ###############################- ENTRY POINT -################################ | 45 ###############################- ENTRY POINT -################################ |
| 45 def main(args:List[str] = None) -> None: | 46 |
| 46 """ | 47 def main(args: List[str] = None) -> None: |
| 47 Initializes everything and sets the program in motion based on the fronted input arguments. | |
| 48 | |
| 49 Returns: | |
| 50 None | |
| 51 """ | |
| 52 # get args from frontend (related xml) | |
| 53 global ARGS | 48 global ARGS |
| 54 ARGS = process_args(args) | 49 ARGS = process_args(args) |
| 55 | 50 |
| 56 model = modelUtils.build_cobra_model_from_csv(ARGS.model_upload) | 51 # configure logging to the requested log file (overwrite each run) |
| 52 logging.basicConfig(filename=ARGS.out_log, | |
| 53 level=logging.DEBUG, | |
| 54 format='%(asctime)s %(levelname)s: %(message)s', | |
| 55 filemode='w') | |
| 57 | 56 |
| 57 logging.info('Starting fromCSVtoCOBRA tool') | |
| 58 logging.debug('Args: input=%s format=%s output=%s tool_dir=%s', ARGS.input, ARGS.format, ARGS.output, ARGS.tool_dir) | |
| 58 | 59 |
| 59 if ARGS.format == "sbml": | 60 try: |
| 60 cobra.io.write_sbml_model(model, ARGS.output) | 61 # Basic sanity checks |
| 61 elif ARGS.format == "json": | 62 if not os.path.exists(ARGS.input): |
| 62 cobra.io.save_json_model(model, ARGS.output) | 63 logging.error('Input file not found: %s', ARGS.input) |
| 63 elif ARGS.format == "mat": | 64 print(f"ERROR: Input file not found: {ARGS.input}", file=sys.stderr) |
| 64 cobra.io.save_matlab_model(model, ARGS.output) | 65 sys.exit(2) |
| 65 elif ARGS.format == "yaml": | 66 |
| 66 cobra.io.save_yaml_model(model, ARGS.output) | 67 out_dir = os.path.dirname(os.path.abspath(ARGS.output)) |
| 68 if out_dir and not os.path.isdir(out_dir): | |
| 69 try: | |
| 70 os.makedirs(out_dir, exist_ok=True) | |
| 71 logging.info('Created missing output directory: %s', out_dir) | |
| 72 except Exception as e: | |
| 73 logging.exception('Cannot create output directory: %s', out_dir) | |
| 74 print(f"ERROR: Cannot create output directory: {out_dir}", file=sys.stderr) | |
| 75 sys.exit(3) | |
| 76 | |
| 77 # Build the model from the CSV (NOTE: use ARGS.input here) | |
| 78 model = modelUtils.build_cobra_model_from_csv(ARGS.input) | |
| 79 | |
| 80 # Save model in requested format | |
| 81 if ARGS.format == "sbml": | |
| 82 cobra.io.write_sbml_model(model, ARGS.output) | |
| 83 elif ARGS.format == "json": | |
| 84 cobra.io.save_json_model(model, ARGS.output) | |
| 85 elif ARGS.format == "mat": | |
| 86 cobra.io.save_matlab_model(model, ARGS.output) | |
| 87 elif ARGS.format == "yaml": | |
| 88 cobra.io.save_yaml_model(model, ARGS.output) | |
| 89 else: | |
| 90 logging.error('Unknown format requested: %s', ARGS.format) | |
| 91 print(f"ERROR: Unknown format: {ARGS.format}", file=sys.stderr) | |
| 92 sys.exit(4) | |
| 93 | |
| 94 logging.info('Model successfully written to %s (format=%s)', ARGS.output, ARGS.format) | |
| 95 | |
| 96 except Exception: | |
| 97 # Log full traceback to the out_log so Galaxy users/admins can see what happened | |
| 98 logging.exception('Unhandled exception in fromCSVtoCOBRA') | |
| 99 traceback.print_exc(file=sys.stderr) | |
| 100 sys.exit(1) | |
| 101 | |
| 67 | 102 |
| 68 if __name__ == '__main__': | 103 if __name__ == '__main__': |
| 69 main() | 104 main() |
