Mercurial > repos > tduigou > seq_to_db
comparison save_to_db.py @ 2:dad2c4c3450a draft default tip
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
| author | tduigou |
|---|---|
| date | Wed, 23 Jul 2025 09:44:33 +0000 |
| parents | 3daf04425ea1 |
| children |
comparison
equal
deleted
inserted
replaced
| 1:3daf04425ea1 | 2:dad2c4c3450a |
|---|---|
| 7 import json | 7 import json |
| 8 from sqlalchemy import create_engine, inspect | 8 from sqlalchemy import create_engine, inspect |
| 9 from sqlalchemy.engine.url import make_url | 9 from sqlalchemy.engine.url import make_url |
| 10 from sqlalchemy.sql import text | 10 from sqlalchemy.sql import text |
| 11 from sqlalchemy.exc import OperationalError | 11 from sqlalchemy.exc import OperationalError |
| 12 | |
| 13 | |
| 14 def resolve_parameters(user_params: dict, json_params: dict, keys: list): | |
| 15 resolved = {} | |
| 16 for key in keys: | |
| 17 # Prefer user parameter if it's provided (not None or empty string) | |
| 18 if key in user_params and user_params[key]: | |
| 19 resolved[key] = user_params[key] | |
| 20 else: | |
| 21 resolved[key] = json_params.get(f"JSON_{key}") | |
| 22 return resolved | |
| 23 | 12 |
| 24 | 13 |
| 25 def fix_db_uri(uri): | 14 def fix_db_uri(uri): |
| 26 """Replace __at__ with @ in the URI if needed.""" | 15 """Replace __at__ with @ in the URI if needed.""" |
| 27 return uri.replace("__at__", "@") | 16 return uri.replace("__at__", "@") |
| 191 | 180 |
| 192 | 181 |
| 193 def main(): | 182 def main(): |
| 194 parser = argparse.ArgumentParser(description="Fetch annotations from PostgreSQL database and save as JSON.") | 183 parser = argparse.ArgumentParser(description="Fetch annotations from PostgreSQL database and save as JSON.") |
| 195 parser.add_argument("--input", required=True, help="Input gb files") | 184 parser.add_argument("--input", required=True, help="Input gb files") |
| 196 parser.add_argument("--sequence_column", required=True, help="DB column contains sequence for ganbank file") | 185 parser.add_argument("--use_json_paramers", required=False, help="Use parameters from JSON: true/false") |
| 197 parser.add_argument("--annotation_column", required=True, help="DB column contains head for ganbank file") | 186 parser.add_argument("--sequence_column", required=False, help="DB column contains sequence for GenBank file") |
| 198 parser.add_argument("--db_uri", required=True, help="Database URI connection string") | 187 parser.add_argument("--annotation_column", required=False, help="DB column contains head for GenBank file") |
| 199 parser.add_argument("--table", required=True, help="Table name in the database") | 188 parser.add_argument("--db_uri", required=False, help="Database URI connection string") |
| 200 parser.add_argument("--fragment_column", required=True, help="Fragment column name in the database") | 189 parser.add_argument("--table", required=False, help="Table name in the database") |
| 201 parser.add_argument("--output", required=True, help="Text report") | 190 parser.add_argument("--fragment_column", required=False, help="Fragment column name in the database") |
| 202 parser.add_argument("--file_name_mapping", required=True, help="real fragments names") | 191 parser.add_argument("--output", required=False, help="Text report") |
| 192 parser.add_argument("--file_name_mapping", required=False, help="Real fragment names") | |
| 203 parser.add_argument("--json_conf", required=False, help="JSON config file with DB parameters") | 193 parser.add_argument("--json_conf", required=False, help="JSON config file with DB parameters") |
| 204 parser.add_argument("--execution_enable", required=True, help="enabbe or desable execution directly from the tool option") | 194 parser.add_argument("--execution_enable", required=False, help="Enable or disable execution") |
| 195 parser.add_argument("--json_generating", required=False, help="Generate JSON: true/false") | |
| 196 parser.add_argument("--json_output", required=False, help="Output path for generated JSON") | |
| 197 | |
| 205 args = parser.parse_args() | 198 args = parser.parse_args() |
| 206 | 199 |
| 207 # enabbe or desable execution based on galaxy param (execution_enable) | |
| 208 | |
| 209 if args.execution_enable == 'false': | 200 if args.execution_enable == 'false': |
| 210 print("Execution disabled. 'Send Requenst to DB' is set to 'false'") | 201 print("Execution disabled. 'Send Request to DB' is set to 'false'") |
| 211 return | 202 return |
| 212 | 203 |
| 213 # Load JSON config if provided | 204 config_params = {} |
| 214 json_config = {} | 205 use_json = args.use_json_paramers == 'true' |
| 215 if args.json_conf != 'None' or '': | 206 generate_json = args.json_generating == 'true' |
| 207 | |
| 208 if use_json: | |
| 209 if not args.json_conf: | |
| 210 raise ValueError("You must provide --json_conf when --use_json_paramers is 'true'") | |
| 216 with open(args.json_conf, "r") as f: | 211 with open(args.json_conf, "r") as f: |
| 217 json_config = json.load(f) | 212 config_params = json.load(f) |
| 218 if "execution" in json_config and json_config["execution"] == "false": | 213 if config_params.get("execution", "") == "false": |
| 219 print("Execution was blocked by config (execution = false)") | 214 print("Execution blocked by config (execution = false)") |
| 220 return | 215 return |
| 221 | 216 else: |
| 222 # Prefer user input; fallback to JSON_ values if not provided | 217 config_params = { |
| 223 user_params = { | 218 "table": args.table, |
| 224 "table": args.table, | 219 "sequence_column": args.sequence_column, |
| 225 "sequence_column": args.sequence_column, | 220 "annotation_column": args.annotation_column, |
| 226 "annotation_column": args.annotation_column, | 221 "fragment_column": args.fragment_column, |
| 227 "fragment_column": args.fragment_column, | 222 "db_uri": args.db_uri, |
| 228 "db_uri": args.db_uri | 223 "execution": args.execution_enable |
| 229 } | 224 } |
| 230 | 225 |
| 231 keys = ["table", "sequence_column", "annotation_column", "fragment_column", "db_uri"] | 226 if generate_json: |
| 232 resolved = resolve_parameters(user_params, json_config, keys) | 227 if not args.json_output: |
| 233 | 228 raise ValueError("You must provide --json_output when --json_generating is 'true'") |
| 234 # Unpack resolved parameters | 229 with open(args.json_output, "w") as f: |
| 235 table = resolved["table"] | 230 json.dump(config_params, f, indent=2) |
| 236 sequence_column = resolved["sequence_column"] | 231 print(f"JSON configuration written to: {args.json_output}") |
| 237 annotation_column = resolved["annotation_column"] | 232 |
| 238 fragment_column = resolved["fragment_column"] | 233 # Extract final resolved parameters |
| 239 db_uri = fix_db_uri(resolved["db_uri"]) | 234 table = config_params["table"] |
| 240 | 235 sequence_column = config_params["sequence_column"] |
| 241 # Prepare gb files | 236 annotation_column = config_params["annotation_column"] |
| 237 fragment_column = config_params["fragment_column"] | |
| 238 db_uri = fix_db_uri(config_params["db_uri"]) | |
| 239 | |
| 242 gb_file_list = [f.strip() for f in args.input.split(",") if f.strip()] | 240 gb_file_list = [f.strip() for f in args.input.split(",") if f.strip()] |
| 243 | 241 |
| 244 # Start and wait for DB | 242 # Connect to DB |
| 245 # db_name = extract_db_name(db_uri) | |
| 246 # start_postgres_container(db_name) | |
| 247 MAX_RETRIES = 3 | 243 MAX_RETRIES = 3 |
| 248 for attempt in range(1, MAX_RETRIES + 1): | 244 for attempt in range(1, MAX_RETRIES + 1): |
| 249 try: | 245 try: |
| 250 wait_for_db(db_uri) | 246 wait_for_db(db_uri) |
| 251 break # Success | 247 break |
| 252 except Exception as e: | 248 except Exception as e: |
| 253 if attempt == MAX_RETRIES: | 249 if attempt == MAX_RETRIES: |
| 254 print(f"Attempt {attempt} failed: Could not connect to database at {db_uri}.") | 250 print(f"Attempt {attempt} failed: Could not connect to database at {db_uri}.") |
| 255 raise e | 251 raise e |
| 256 else: | 252 time.sleep(2) |
| 257 time.sleep(2) | 253 |
| 258 | 254 # Push annotations |
| 259 # Push annotations | |
| 260 push_gb_annotations( | 255 push_gb_annotations( |
| 261 gb_file_list, | 256 gb_file_list, |
| 262 sequence_column, | 257 sequence_column, |
| 263 annotation_column, | 258 annotation_column, |
| 264 db_uri, | 259 db_uri, |
