| 
93
 | 
     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 import utils.rule_parsing  as rulesUtils
 | 
| 
147
 | 
     9 from typing import Optional, Tuple, Union, List, Dict
 | 
| 
93
 | 
    10 import utils.reaction_parsing as reactionUtils
 | 
| 
 | 
    11 
 | 
| 
 | 
    12 ARGS : argparse.Namespace
 | 
| 
343
 | 
    13 def process_args(args: List[str] = None) -> argparse.Namespace:
 | 
| 
 | 
    14     """
 | 
| 
 | 
    15     Parse command-line arguments for CustomDataGenerator.
 | 
| 
93
 | 
    16     """
 | 
| 
343
 | 
    17 
 | 
| 
 | 
    18     parser = argparse.ArgumentParser(
 | 
| 
 | 
    19         usage="%(prog)s [options]",
 | 
| 
 | 
    20         description="Generate custom data from a given model"
 | 
| 
 | 
    21     )
 | 
| 
93
 | 
    22 
 | 
| 
343
 | 
    23     parser.add_argument("--out_log", type=str, required=True,
 | 
| 
 | 
    24                         help="Output log file")
 | 
| 
93
 | 
    25 
 | 
| 
343
 | 
    26     parser.add_argument("--model", type=str,
 | 
| 
 | 
    27                         help="Built-in model identifier (e.g., ENGRO2, Recon, HMRcore)")
 | 
| 
 | 
    28     parser.add_argument("--input", type=str,
 | 
| 
 | 
    29                         help="Custom model file (JSON or XML)")
 | 
| 
 | 
    30     parser.add_argument("--name", type=str, required=True,
 | 
| 
 | 
    31                         help="Model name (default or custom)")
 | 
| 
93
 | 
    32     
 | 
| 
343
 | 
    33     parser.add_argument("--medium_selector", type=str, required=True,
 | 
| 
 | 
    34                         help="Medium selection option (default/custom)")
 | 
| 
 | 
    35     parser.add_argument("--medium", type=str,
 | 
| 
 | 
    36                         help="Custom medium file if medium_selector=Custom")
 | 
| 
 | 
    37     
 | 
| 
 | 
    38     parser.add_argument("--output_format", type=str, choices=["tabular", "xlsx"], required=True,
 | 
| 
 | 
    39                         help="Output format: CSV (tabular) or Excel (xlsx)")
 | 
| 
 | 
    40     
 | 
| 
365
 | 
    41     parser.add_argument("--out_tabular", type=str,
 | 
| 
363
 | 
    42                         help="Output file for the merged dataset (CSV or XLSX)")
 | 
| 
365
 | 
    43     
 | 
| 
 | 
    44     parser.add_argument("--out_xlsx", type=str,
 | 
| 
 | 
    45                         help="Output file for the merged dataset (CSV or XLSX)")
 | 
| 
343
 | 
    46     
 | 
| 
353
 | 
    47     parser.add_argument("--tool_dir", type=str, default=os.path.dirname(__file__),
 | 
| 
363
 | 
    48                         help="Tool directory (passed from Galaxy as $__tool_directory__)")
 | 
| 
353
 | 
    49 
 | 
| 
93
 | 
    50 
 | 
| 
343
 | 
    51     return parser.parse_args(args)
 | 
| 
93
 | 
    52 
 | 
| 
 | 
    53 ################################- INPUT DATA LOADING -################################
 | 
| 
 | 
    54 def load_custom_model(file_path :utils.FilePath, ext :Optional[utils.FileFormat] = None) -> cobra.Model:
 | 
| 
 | 
    55     """
 | 
| 
 | 
    56     Loads a custom model from a file, either in JSON or XML format.
 | 
| 
 | 
    57 
 | 
| 
 | 
    58     Args:
 | 
| 
 | 
    59         file_path : The path to the file containing the custom model.
 | 
| 
 | 
    60         ext : explicit file extension. Necessary for standard use in galaxy because of its weird behaviour.
 | 
| 
 | 
    61 
 | 
| 
 | 
    62     Raises:
 | 
| 
 | 
    63         DataErr : if the file is in an invalid format or cannot be opened for whatever reason.    
 | 
| 
 | 
    64     
 | 
| 
 | 
    65     Returns:
 | 
| 
 | 
    66         cobra.Model : the model, if successfully opened.
 | 
| 
 | 
    67     """
 | 
| 
 | 
    68     ext = ext if ext else file_path.ext
 | 
| 
 | 
    69     try:
 | 
| 
 | 
    70         if ext is utils.FileFormat.XML:
 | 
| 
 | 
    71             return cobra.io.read_sbml_model(file_path.show())
 | 
| 
 | 
    72         
 | 
| 
 | 
    73         if ext is utils.FileFormat.JSON:
 | 
| 
 | 
    74             return cobra.io.load_json_model(file_path.show())
 | 
| 
 | 
    75 
 | 
| 
 | 
    76     except Exception as e: raise utils.DataErr(file_path, e.__str__())
 | 
| 
 | 
    77     raise utils.DataErr(file_path,
 | 
| 
 | 
    78         f"Formato \"{file_path.ext}\" non riconosciuto, sono supportati solo file JSON e XML")
 | 
| 
 | 
    79 
 | 
| 
 | 
    80 ################################- DATA GENERATION -################################
 | 
| 
 | 
    81 ReactionId = str
 | 
| 
 | 
    82 def generate_rules(model: cobra.Model, *, asParsed = True) -> Union[Dict[ReactionId, rulesUtils.OpList], Dict[ReactionId, str]]:
 | 
| 
 | 
    83     """
 | 
| 
 | 
    84     Generates a dictionary mapping reaction ids to rules from the model.
 | 
| 
 | 
    85 
 | 
| 
 | 
    86     Args:
 | 
| 
 | 
    87         model : the model to derive data from.
 | 
| 
 | 
    88         asParsed : if True parses the rules to an optimized runtime format, otherwise leaves them as strings.
 | 
| 
 | 
    89 
 | 
| 
 | 
    90     Returns:
 | 
| 
 | 
    91         Dict[ReactionId, rulesUtils.OpList] : the generated dictionary of parsed rules.
 | 
| 
 | 
    92         Dict[ReactionId, str] : the generated dictionary of raw rules.
 | 
| 
 | 
    93     """
 | 
| 
 | 
    94     # Is the below approach convoluted? yes
 | 
| 
 | 
    95     # Ok but is it inefficient? probably
 | 
| 
 | 
    96     # Ok but at least I don't have to repeat the check at every rule (I'm clinically insane)
 | 
| 
 | 
    97     _ruleGetter   =  lambda reaction : reaction.gene_reaction_rule
 | 
| 
 | 
    98     ruleExtractor = (lambda reaction :
 | 
| 
 | 
    99         rulesUtils.parseRuleToNestedList(_ruleGetter(reaction))) if asParsed else _ruleGetter
 | 
| 
 | 
   100 
 | 
| 
 | 
   101     return {
 | 
| 
 | 
   102         reaction.id : ruleExtractor(reaction)
 | 
| 
 | 
   103         for reaction in model.reactions
 | 
| 
 | 
   104         if reaction.gene_reaction_rule }
 | 
| 
 | 
   105 
 | 
| 
 | 
   106 def generate_reactions(model :cobra.Model, *, asParsed = True) -> Dict[ReactionId, str]:
 | 
| 
 | 
   107     """
 | 
| 
 | 
   108     Generates a dictionary mapping reaction ids to reaction formulas from the model.
 | 
| 
 | 
   109 
 | 
| 
 | 
   110     Args:
 | 
| 
 | 
   111         model : the model to derive data from.
 | 
| 
 | 
   112         asParsed : if True parses the reactions to an optimized runtime format, otherwise leaves them as they are.
 | 
| 
 | 
   113 
 | 
| 
 | 
   114     Returns:
 | 
| 
 | 
   115         Dict[ReactionId, str] : the generated dictionary.
 | 
| 
 | 
   116     """
 | 
| 
 | 
   117 
 | 
| 
 | 
   118     unparsedReactions = {
 | 
| 
 | 
   119         reaction.id : reaction.reaction
 | 
| 
 | 
   120         for reaction in model.reactions
 | 
| 
 | 
   121         if reaction.reaction 
 | 
| 
 | 
   122     }
 | 
| 
 | 
   123 
 | 
| 
 | 
   124     if not asParsed: return unparsedReactions
 | 
| 
 | 
   125     
 | 
| 
 | 
   126     return reactionUtils.create_reaction_dict(unparsedReactions)
 | 
| 
 | 
   127 
 | 
| 
 | 
   128 def get_medium(model:cobra.Model) -> pd.DataFrame:
 | 
| 
 | 
   129     trueMedium=[]
 | 
| 
 | 
   130     for r in model.reactions:
 | 
| 
 | 
   131         positiveCoeff=0
 | 
| 
 | 
   132         for m in r.metabolites:
 | 
| 
 | 
   133             if r.get_coefficient(m.id)>0:
 | 
| 
 | 
   134                 positiveCoeff=1;
 | 
| 
 | 
   135         if (positiveCoeff==0 and r.lower_bound<0):
 | 
| 
 | 
   136             trueMedium.append(r.id)
 | 
| 
 | 
   137 
 | 
| 
 | 
   138     df_medium = pd.DataFrame()
 | 
| 
 | 
   139     df_medium["reaction"] = trueMedium
 | 
| 
 | 
   140     return df_medium
 | 
| 
 | 
   141 
 | 
| 
 | 
   142 def generate_bounds(model:cobra.Model) -> pd.DataFrame:
 | 
| 
 | 
   143 
 | 
| 
 | 
   144     rxns = []
 | 
| 
 | 
   145     for reaction in model.reactions:
 | 
| 
 | 
   146         rxns.append(reaction.id)
 | 
| 
 | 
   147 
 | 
| 
 | 
   148     bounds = pd.DataFrame(columns = ["lower_bound", "upper_bound"], index=rxns)
 | 
| 
 | 
   149 
 | 
| 
 | 
   150     for reaction in model.reactions:
 | 
| 
 | 
   151         bounds.loc[reaction.id] = [reaction.lower_bound, reaction.upper_bound]
 | 
| 
 | 
   152     return bounds
 | 
| 
 | 
   153 
 | 
| 
 | 
   154 
 | 
| 
 | 
   155 ###############################- FILE SAVING -################################
 | 
| 
 | 
   156 def save_as_csv_filePath(data :dict, file_path :utils.FilePath, fieldNames :Tuple[str, str]) -> None:
 | 
| 
 | 
   157     """
 | 
| 
 | 
   158     Saves any dictionary-shaped data in a .csv file created at the given file_path as FilePath.
 | 
| 
 | 
   159 
 | 
| 
 | 
   160     Args:
 | 
| 
 | 
   161         data : the data to be written to the file.
 | 
| 
 | 
   162         file_path : the path to the .csv file.
 | 
| 
 | 
   163         fieldNames : the names of the fields (columns) in the .csv file.
 | 
| 
 | 
   164     
 | 
| 
 | 
   165     Returns:
 | 
| 
 | 
   166         None
 | 
| 
 | 
   167     """
 | 
| 
 | 
   168     with open(file_path.show(), 'w', newline='') as csvfile:
 | 
| 
 | 
   169         writer = csv.DictWriter(csvfile, fieldnames = fieldNames, dialect="excel-tab")
 | 
| 
 | 
   170         writer.writeheader()
 | 
| 
 | 
   171 
 | 
| 
 | 
   172         for key, value in data.items():
 | 
| 
 | 
   173             writer.writerow({ fieldNames[0] : key, fieldNames[1] : value })
 | 
| 
 | 
   174 
 | 
| 
 | 
   175 def save_as_csv(data :dict, file_path :str, fieldNames :Tuple[str, str]) -> None:
 | 
| 
 | 
   176     """
 | 
| 
 | 
   177     Saves any dictionary-shaped data in a .csv file created at the given file_path as string.
 | 
| 
 | 
   178 
 | 
| 
 | 
   179     Args:
 | 
| 
 | 
   180         data : the data to be written to the file.
 | 
| 
 | 
   181         file_path : the path to the .csv file.
 | 
| 
 | 
   182         fieldNames : the names of the fields (columns) in the .csv file.
 | 
| 
 | 
   183     
 | 
| 
 | 
   184     Returns:
 | 
| 
 | 
   185         None
 | 
| 
 | 
   186     """
 | 
| 
 | 
   187     with open(file_path, 'w', newline='') as csvfile:
 | 
| 
 | 
   188         writer = csv.DictWriter(csvfile, fieldnames = fieldNames, dialect="excel-tab")
 | 
| 
 | 
   189         writer.writeheader()
 | 
| 
 | 
   190 
 | 
| 
 | 
   191         for key, value in data.items():
 | 
| 
 | 
   192             writer.writerow({ fieldNames[0] : key, fieldNames[1] : value })
 | 
| 
 | 
   193 
 | 
| 
 | 
   194 ###############################- ENTRY POINT -################################
 | 
| 
147
 | 
   195 def main(args:List[str] = None) -> None:
 | 
| 
93
 | 
   196     """
 | 
| 
 | 
   197     Initializes everything and sets the program in motion based on the fronted input arguments.
 | 
| 
 | 
   198     
 | 
| 
 | 
   199     Returns:
 | 
| 
 | 
   200         None
 | 
| 
 | 
   201     """
 | 
| 
 | 
   202     # get args from frontend (related xml)
 | 
| 
 | 
   203     global ARGS
 | 
| 
147
 | 
   204     ARGS = process_args(args)
 | 
| 
93
 | 
   205 
 | 
| 
 | 
   206     # this is the worst thing I've seen so far, congrats to the former MaREA devs for suggesting this!
 | 
| 
361
 | 
   207     #if os.path.isdir(ARGS.output_path) == False: 
 | 
| 
 | 
   208     #    os.makedirs(ARGS.output_path)
 | 
| 
343
 | 
   209 
 | 
| 
350
 | 
   210     if ARGS.input:
 | 
| 
343
 | 
   211         # load custom model
 | 
| 
 | 
   212         model = load_custom_model(
 | 
| 
 | 
   213             utils.FilePath.fromStrPath(ARGS.input), utils.FilePath.fromStrPath(ARGS.name).ext)
 | 
| 
 | 
   214     else:
 | 
| 
 | 
   215         # load built-in model
 | 
| 
93
 | 
   216 
 | 
| 
343
 | 
   217         try:
 | 
| 
 | 
   218             model_enum = utils.Model[ARGS.model]  # e.g., Model['ENGRO2']
 | 
| 
 | 
   219         except KeyError:
 | 
| 
 | 
   220             raise utils.ArgsErr("model", "one of Recon/ENGRO2/HMRcore/Custom_model", ARGS.model)
 | 
| 
 | 
   221 
 | 
| 
 | 
   222         # Load built-in model (Model.getCOBRAmodel uses tool_dir to locate local models)
 | 
| 
 | 
   223         try:
 | 
| 
353
 | 
   224             model = model_enum.getCOBRAmodel(toolDir=ARGS.tool_dir)
 | 
| 
343
 | 
   225         except Exception as e:
 | 
| 
 | 
   226             # Wrap/normalize load errors as DataErr for consistency
 | 
| 
 | 
   227             raise utils.DataErr(ARGS.model, f"failed loading built-in model: {e}")
 | 
| 
 | 
   228 
 | 
| 
 | 
   229     # Determine final model name: explicit --name overrides, otherwise use the model id
 | 
| 
 | 
   230     model_name = ARGS.name if ARGS.name else ARGS.model
 | 
| 
93
 | 
   231 
 | 
| 
 | 
   232     # generate data
 | 
| 
 | 
   233     rules = generate_rules(model, asParsed = False)
 | 
| 
 | 
   234     reactions = generate_reactions(model, asParsed = False)
 | 
| 
 | 
   235     bounds = generate_bounds(model)
 | 
| 
 | 
   236     medium = get_medium(model)
 | 
| 
 | 
   237 
 | 
| 
343
 | 
   238     df_rules = pd.DataFrame(list(rules.items()), columns = ["ReactionID", "Rule"])
 | 
| 
 | 
   239     df_reactions = pd.DataFrame(list(reactions.items()), columns = ["ReactionID", "Reaction"])
 | 
| 
 | 
   240 
 | 
| 
 | 
   241     df_bounds = bounds.reset_index().rename(columns = {"index": "ReactionID"})
 | 
| 
 | 
   242     df_medium = medium.rename(columns = {"reaction": "ReactionID"})
 | 
| 
 | 
   243     df_medium["InMedium"] = True # flag per indicare la presenza nel medium
 | 
| 
 | 
   244 
 | 
| 
 | 
   245     merged = df_reactions.merge(df_rules, on = "ReactionID", how = "outer")
 | 
| 
 | 
   246     merged = merged.merge(df_bounds, on = "ReactionID", how = "outer")
 | 
| 
 | 
   247 
 | 
| 
 | 
   248     merged = merged.merge(df_medium, on = "ReactionID", how = "left")
 | 
| 
 | 
   249 
 | 
| 
 | 
   250     merged["InMedium"] = merged["InMedium"].fillna(False)
 | 
| 
 | 
   251 
 | 
| 
 | 
   252     merged = merged.sort_values(by = "InMedium", ascending = False)
 | 
| 
 | 
   253 
 | 
| 
359
 | 
   254     #out_file = os.path.join(ARGS.output_path, f"{os.path.basename(ARGS.name).split('.')[0]}_custom_data")
 | 
| 
343
 | 
   255 
 | 
| 
 | 
   256     #merged.to_csv(out_file, sep = '\t', index = False)
 | 
| 
 | 
   257 
 | 
| 
 | 
   258 
 | 
| 
 | 
   259     ####
 | 
| 
 | 
   260 
 | 
| 
 | 
   261     if ARGS.output_format == "xlsx":
 | 
| 
365
 | 
   262         merged.to_excel(ARGS.out_xlsx, index=False)
 | 
| 
343
 | 
   263     else:
 | 
| 
365
 | 
   264         merged.to_csv(ARGS.out_tabular, sep="\t", index=False)
 | 
| 
343
 | 
   265 
 | 
| 
367
 | 
   266 print("CustomDataGenerator: completed successfully")
 | 
| 
93
 | 
   267 
 | 
| 
 | 
   268 if __name__ == '__main__':
 | 
| 
 | 
   269     main() |