Mercurial > repos > bimib > cobraxy
comparison COBRAxy/custom_data_generator.py @ 384:8a1213d1393d draft
Uploaded
| author | francesco_lapi | 
|---|---|
| date | Fri, 05 Sep 2025 09:53:40 +0000 | 
| parents | 0a3ca20848f3 | 
| children | cccbf259459e | 
   comparison
  equal
  deleted
  inserted
  replaced
| 383:d2e36a8ab451 | 384:8a1213d1393d | 
|---|---|
| 34 parser.add_argument("--medium_selector", type=str, required=True, | 34 parser.add_argument("--medium_selector", type=str, required=True, | 
| 35 help="Medium selection option (default/custom)") | 35 help="Medium selection option (default/custom)") | 
| 36 parser.add_argument("--medium", type=str, | 36 parser.add_argument("--medium", type=str, | 
| 37 help="Custom medium file if medium_selector=Custom") | 37 help="Custom medium file if medium_selector=Custom") | 
| 38 | 38 | 
| 39 parser.add_argument("--output_format", type=str, choices=["tabular", "xlsx"], required=True, | |
| 40 help="Output format: CSV (tabular) or Excel (xlsx)") | |
| 41 | |
| 42 parser.add_argument("--out_tabular", type=str, | 39 parser.add_argument("--out_tabular", type=str, | 
| 43 help="Output file for the merged dataset (CSV or XLSX)") | |
| 44 | |
| 45 parser.add_argument("--out_xlsx", type=str, | |
| 46 help="Output file for the merged dataset (CSV or XLSX)") | 40 help="Output file for the merged dataset (CSV or XLSX)") | 
| 47 | 41 | 
| 48 parser.add_argument("--tool_dir", type=str, default=os.path.dirname(__file__), | 42 parser.add_argument("--tool_dir", type=str, default=os.path.dirname(__file__), | 
| 49 help="Tool directory (passed from Galaxy as $__tool_directory__)") | 43 help="Tool directory (passed from Galaxy as $__tool_directory__)") | 
| 50 | 44 | 
| 197 os.makedirs(os.path.dirname(path) or ".", exist_ok=True) | 191 os.makedirs(os.path.dirname(path) or ".", exist_ok=True) | 
| 198 df.to_csv(path, sep="\t", index=False) | 192 df.to_csv(path, sep="\t", index=False) | 
| 199 except Exception as e: | 193 except Exception as e: | 
| 200 raise utils.DataErr(path, f"failed writing tabular output: {e}") | 194 raise utils.DataErr(path, f"failed writing tabular output: {e}") | 
| 201 | 195 | 
| 202 def save_as_xlsx_df(df: pd.DataFrame, path: str) -> None: | |
| 203 try: | |
| 204 if not path.lower().endswith(".xlsx"): | |
| 205 path += ".xlsx" | |
| 206 os.makedirs(os.path.dirname(path) or ".", exist_ok=True) | |
| 207 df.to_excel(path, index=False) | |
| 208 except Exception as e: | |
| 209 raise utils.DataErr(path, f"failed writing xlsx output: {e}") | |
| 210 | 196 | 
| 211 ###############################- ENTRY POINT -################################ | 197 ###############################- ENTRY POINT -################################ | 
| 212 def main(args:List[str] = None) -> None: | 198 def main(args:List[str] = None) -> None: | 
| 213 """ | 199 """ | 
| 214 Initializes everything and sets the program in motion based on the fronted input arguments. | 200 Initializes everything and sets the program in motion based on the fronted input arguments. | 
| 270 #merged.to_csv(out_file, sep = '\t', index = False) | 256 #merged.to_csv(out_file, sep = '\t', index = False) | 
| 271 | 257 | 
| 272 | 258 | 
| 273 #### | 259 #### | 
| 274 | 260 | 
| 275 # write only the requested output | 261 | 
| 276 if ARGS.output_format == "xlsx": | 262 if not ARGS.out_tabular: | 
| 277 if not ARGS.out_xlsx: | 263 raise utils.ArgsErr("out_tabular", "output path (--out_tabular) is required when output_format == tabular", ARGS.out_tabular) | 
| 278 raise utils.ArgsErr("out_xlsx", "output path (--out_xlsx) is required when output_format == xlsx", ARGS.out_xlsx) | 264 save_as_tabular_df(merged, ARGS.out_tabular) | 
| 279 save_as_xlsx_df(merged, ARGS.out_xlsx) | 265 expected = ARGS.out_tabular | 
| 280 expected = ARGS.out_xlsx | |
| 281 else: | |
| 282 if not ARGS.out_tabular: | |
| 283 raise utils.ArgsErr("out_tabular", "output path (--out_tabular) is required when output_format == tabular", ARGS.out_tabular) | |
| 284 save_as_tabular_df(merged, ARGS.out_tabular) | |
| 285 expected = ARGS.out_tabular | |
| 286 | 266 | 
| 287 # verify output exists and non-empty | 267 # verify output exists and non-empty | 
| 288 if not expected or not os.path.exists(expected) or os.path.getsize(expected) == 0: | 268 if not expected or not os.path.exists(expected) or os.path.getsize(expected) == 0: | 
| 289 raise utils.DataErr(expected, "Output non creato o vuoto") | 269 raise utils.DataErr(expected, "Output non creato o vuoto") | 
| 290 | 270 | 
