451
|
1 import os
|
|
2 import csv
|
|
3 import cobra
|
|
4 import pickle
|
|
5 import argparse
|
|
6 import pandas as pd
|
|
7 import utils.general_utils as utils
|
|
8 from typing import Optional, Tuple, Union, List, Dict
|
|
9 import logging
|
|
10 import utils.rule_parsing as rulesUtils
|
|
11 import utils.reaction_parsing as reactionUtils
|
|
12 import utils.model_utils as modelUtils
|
|
13
|
|
14 ARGS : argparse.Namespace
|
|
15 def process_args(args: List[str] = None) -> argparse.Namespace:
|
453
|
16 parser = argparse.ArgumentParser(
|
|
17 usage="%(prog)s [options]",
|
|
18 description="Convert a tabular/CSV file to a COBRA model"
|
|
19 )
|
451
|
20
|
|
21
|
|
22 parser.add_argument("--out_log", type=str, required=True,
|
453
|
23 help="Output log file")
|
|
24
|
451
|
25
|
453
|
26 parser.add_argument("--input", type=str, required=True,
|
|
27 help="Input tabular file (CSV/TSV)")
|
|
28
|
|
29
|
451
|
30 parser.add_argument("--format", type=str, required=True, choices=["sbml", "json", "mat", "yaml"],
|
453
|
31 help="Model format (SBML, JSON, MATLAB, YAML)")
|
|
32
|
451
|
33
|
453
|
34 parser.add_argument("--output", type=str, required=True,
|
|
35 help="Output model file path")
|
|
36
|
|
37
|
451
|
38 parser.add_argument("--tool_dir", type=str, default=os.path.dirname(__file__),
|
453
|
39 help="Tool directory (passed from Galaxy as $__tool_directory__)")
|
451
|
40
|
|
41
|
|
42 return parser.parse_args(args)
|
|
43
|
453
|
44
|
451
|
45 ###############################- ENTRY POINT -################################
|
453
|
46
|
|
47 def main(args: List[str] = None) -> None:
|
451
|
48 global ARGS
|
|
49 ARGS = process_args(args)
|
|
50
|
453
|
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')
|
|
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)
|
|
59
|
|
60 try:
|
|
61 # Basic sanity checks
|
|
62 if not os.path.exists(ARGS.input):
|
|
63 logging.error('Input file not found: %s', ARGS.input)
|
|
64
|
|
65 out_dir = os.path.dirname(os.path.abspath(ARGS.output))
|
455
|
66
|
453
|
67 if out_dir and not os.path.isdir(out_dir):
|
|
68 try:
|
|
69 os.makedirs(out_dir, exist_ok=True)
|
|
70 logging.info('Created missing output directory: %s', out_dir)
|
|
71 except Exception as e:
|
|
72 logging.exception('Cannot create output directory: %s', out_dir)
|
|
73
|
|
74 model = modelUtils.build_cobra_model_from_csv(ARGS.input)
|
|
75
|
|
76 # Save model in requested format
|
|
77 if ARGS.format == "sbml":
|
|
78 cobra.io.write_sbml_model(model, ARGS.output)
|
|
79 elif ARGS.format == "json":
|
|
80 cobra.io.save_json_model(model, ARGS.output)
|
|
81 elif ARGS.format == "mat":
|
|
82 cobra.io.save_matlab_model(model, ARGS.output)
|
|
83 elif ARGS.format == "yaml":
|
|
84 cobra.io.save_yaml_model(model, ARGS.output)
|
|
85 else:
|
|
86 logging.error('Unknown format requested: %s', ARGS.format)
|
454
|
87 print(f"ERROR: Unknown format: {ARGS.format}")
|
|
88
|
453
|
89
|
|
90 logging.info('Model successfully written to %s (format=%s)', ARGS.output, ARGS.format)
|
|
91
|
|
92 except Exception:
|
|
93 # Log full traceback to the out_log so Galaxy users/admins can see what happened
|
|
94 logging.exception('Unhandled exception in fromCSVtoCOBRA')
|
451
|
95
|
|
96
|
|
97 if __name__ == '__main__':
|
453
|
98 main()
|