annotate save_to_db.py @ 0:9f845ccb1a9f draft

planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
author tduigou
date Wed, 11 Jun 2025 13:36:44 +0000
parents
children 3daf04425ea1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
1 import subprocess
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
2 import argparse
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
3 import time
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
4 import os
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
5 import socket
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
6 import re
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
7 import json
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
8 from sqlalchemy import create_engine, inspect
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
9 from sqlalchemy.engine.url import make_url
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
10 from sqlalchemy.sql import text
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
11 from sqlalchemy.exc import OperationalError
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
12
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
13
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
14 def resolve_parameters(user_params: dict, json_params: dict, keys: list):
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
15 resolved = {}
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
16 for key in keys:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
17 # Prefer user parameter if it's provided (not None or empty string)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
18 if key in user_params and user_params[key]:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
19 resolved[key] = user_params[key]
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
20 else:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
21 resolved[key] = json_params.get(f"JSON_{key}")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
22 return resolved
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
23
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
24
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
25 def fix_db_uri(uri):
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
26 """Replace __at__ with @ in the URI if needed."""
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
27 return uri.replace("__at__", "@")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
28
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
29
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
30 def is_port_in_use(uri):
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
31 """Check if a TCP port is already in use on host."""
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
32 url = make_url(uri)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
33 host = url.host
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
34 port = url.port
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
35 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
36 s.settimeout(2)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
37 return s.connect_ex((host, port)) == 0
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
38
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
39 def extract_db_name(uri):
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
40 """Extract the database name from the SQLAlchemy URI."""
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
41 url = make_url(uri)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
42 return url.database
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
43
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
44
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
45 # this fuction is to activate the Docker id the DB is in container. BUT IT IS NOT USED IN MAIN()
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
46 def start_postgres_container(db_name):
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
47 """Start a PostgreSQL container with the given database name as the container name."""
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
48 container_name = db_name
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
49
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
50 # Check if container is already running
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
51 container_running = subprocess.run(
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
52 f"docker ps -q -f name={container_name}", shell=True, capture_output=True, text=True
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
53 )
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
54
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
55 if container_running.stdout.strip():
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
56 print(f"Container '{container_name}' is already running.")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
57 return
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
58
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
59 # Check if container exists (stopped)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
60 container_exists = subprocess.run(
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
61 f"docker ps -a -q -f name={container_name}", shell=True, capture_output=True, text=True
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
62 )
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
63
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
64 if container_exists.stdout.strip():
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
65 print(f"Starting existing container '{container_name}'...")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
66 subprocess.run(f"docker start {container_name}", shell=True)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
67 print(f"PostgreSQL Docker container '{container_name}' activated.")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
68 return
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
69
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
70 # If container does not exist, create and start a new one
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
71 port = 5432 if not is_port_in_use(5432) else 5433
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
72 postgres_password = os.getenv("POSTGRES_PASSWORD", "RK17")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
73
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
74 start_command = [
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
75 "docker", "run", "--name", container_name,
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
76 "-e", f"POSTGRES_PASSWORD={postgres_password}",
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
77 "-p", f"{port}:5432",
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
78 "-d", "postgres"
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
79 ]
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
80
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
81 try:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
82 subprocess.run(start_command, check=True)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
83 print(f"PostgreSQL Docker container '{container_name}' started on port {port}.")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
84 except subprocess.CalledProcessError as e:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
85 print(f"Failed to start Docker container: {e}")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
86
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
87
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
88 def wait_for_db(uri, timeout=60):
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
89 """Try connecting to the DB until it works or timeout."""
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
90 engine = create_engine(uri)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
91 start_time = time.time()
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
92 while time.time() - start_time < timeout:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
93 try:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
94 with engine.connect():
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
95 print("Connected to database.")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
96 return
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
97 except OperationalError:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
98 print("Database not ready, retrying...")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
99 time.sleep(2)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
100 raise Exception("Database connection failed after timeout.")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
101
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
102
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
103 def push_gb_annotations(gb_files, sequence_column, annotation_column, db_uri, table_name, fragment_column_name, output, file_name_mapping):
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
104 """Push GenBank file content into the database if the fragment is not already present."""
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
105 db_uri = fix_db_uri(db_uri)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
106 engine = create_engine(db_uri)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
107 inserted_fragments = []
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
108
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
109 try:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
110 # Parse the file_name_mapping string into a dictionary {base_file_name: fragment_name}
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
111 file_name_mapping_dict = {
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
112 os.path.basename(path): os.path.splitext(fragment_name)[0]
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
113 for mapping in file_name_mapping.split(",")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
114 for path, fragment_name in [mapping.split(":")]
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
115 }
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
116
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
117 #print("File name mapping dictionary:")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
118 #print(file_name_mapping_dict) # Debugging: Print the mapping dictionary
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
119
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
120 with engine.begin() as connection:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
121 inspector = inspect(engine)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
122 columns = [col['name'] for col in inspector.get_columns(table_name)]
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
123
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
124 if fragment_column_name not in columns:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
125 raise ValueError(f"Fragment column '{fragment_column_name}' not found in table '{table_name}'.")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
126
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
127 # Get existing fragments
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
128 all_rows = connection.execute(text(f"SELECT {fragment_column_name} FROM {table_name}")).fetchall()
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
129 existing_fragments = {row[0] for row in all_rows}
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
130
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
131 insert_rows = []
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
132
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
133 for gb_file in gb_files:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
134 # Extract base file name (just the file name, not the full path)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
135 real_file_name = os.path.basename(gb_file)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
136 fragment_name = file_name_mapping_dict.get(real_file_name)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
137
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
138 print(f"Processing file: {real_file_name}({fragment_name})") # Debugging: Log the current file
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
139
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
140 # Get the corresponding fragment name from the mapping
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
141 fragment_name = file_name_mapping_dict.get(real_file_name)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
142
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
143 if not fragment_name:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
144 raise ValueError(f"Fragment name not found for file '{real_file_name}' in file_name_mapping.")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
145
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
146 # If the fragment is already in the DB, raise an error and stop the process
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
147 if fragment_name in existing_fragments:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
148 raise RuntimeError(f"Fatal Error: Fragment '{fragment_name}' already exists in DB. Stopping the process.")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
149
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
150 with open(gb_file, "r") as f:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
151 content = f.read()
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
152
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
153 origin_match = re.search(r"^ORIGIN.*$", content, flags=re.MULTILINE)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
154 if not origin_match:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
155 raise ValueError(f"ORIGIN section not found in file: {gb_file}")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
156
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
157 origin_start = origin_match.start()
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
158 annotation_text = content[:origin_start].strip()
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
159 sequence_text = content[origin_start:].strip()
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
160
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
161 values = {}
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
162 values[fragment_column_name] = fragment_name
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
163 values[annotation_column] = annotation_text
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
164 values[sequence_column] = sequence_text
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
165
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
166 insert_rows.append(values)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
167 inserted_fragments.append(fragment_name)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
168
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
169 # Insert the rows into the database
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
170 for values in insert_rows:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
171 col_names = ", ".join(values.keys())
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
172 placeholders = ", ".join([f":{key}" for key in values.keys()])
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
173 insert_stmt = text(f"INSERT INTO {table_name} ({col_names}) VALUES ({placeholders})")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
174
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
175 # print(f"Inserting into DB: {values}") # Debugging print statement
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
176 connection.execute(insert_stmt, values)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
177
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
178 # print(f"Insert result: {result.rowcount if hasattr(result, 'rowcount') else 'N/A'}") # Debugging the row count
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
179
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
180 print(f"Inserted {len(insert_rows)} fragments.")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
181
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
182 # Write inserted fragment names to a text file
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
183 with open(output, "w") as log_file:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
184 for frag in inserted_fragments:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
185 log_file.write(f"{frag}\n")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
186 print(f"Fragment names written to '{output}'.")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
187
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
188 except Exception as e:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
189 print(f"Error during GB file insertion: {e}")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
190 raise
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
191
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
192
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
193 def main():
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
194 parser = argparse.ArgumentParser(description="Fetch annotations from PostgreSQL database and save as JSON.")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
195 parser.add_argument("--input", required=True, help="Input gb files")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
196 parser.add_argument("--sequence_column", required=True, help="DB column contains sequence for ganbank file")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
197 parser.add_argument("--annotation_column", required=True, help="DB column contains head for ganbank file")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
198 parser.add_argument("--db_uri", required=True, help="Database URI connection string")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
199 parser.add_argument("--table", required=True, help="Table name in the database")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
200 parser.add_argument("--fragment_column", required=True, help="Fragment column name in the database")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
201 parser.add_argument("--output", required=True, help="Text report")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
202 parser.add_argument("--file_name_mapping", required=True, help="real fragments names")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
203 parser.add_argument("--json_conf", required=False, help="JSON config file with DB parameters")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
204 args = parser.parse_args()
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
205
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
206 # Load JSON config if provided
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
207 json_config = {}
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
208 if args.json_conf != 'None' or '':
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
209 with open(args.json_conf, "r") as f:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
210 json_config = json.load(f)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
211 if "execution" in json_config and json_config["execution"] == "false":
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
212 print("Execution was blocked by config (execution = false)")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
213 return
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
214
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
215 # Prefer user input; fallback to JSON_ values if not provided
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
216 user_params = {
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
217 "table": args.table,
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
218 "sequence_column": args.sequence_column,
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
219 "annotation_column": args.annotation_column,
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
220 "fragment_column": args.fragment_column,
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
221 "db_uri": args.db_uri
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
222 }
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
223
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
224 keys = ["table", "sequence_column", "annotation_column", "fragment_column", "db_uri"]
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
225 resolved = resolve_parameters(user_params, json_config, keys)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
226
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
227 # Unpack resolved parameters
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
228 table = resolved["table"]
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
229 sequence_column = resolved["sequence_column"]
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
230 annotation_column = resolved["annotation_column"]
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
231 fragment_column = resolved["fragment_column"]
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
232 db_uri = fix_db_uri(resolved["db_uri"])
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
233
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
234 # Prepare gb files
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
235 gb_file_list = [f.strip() for f in args.input.split(",") if f.strip()]
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
236
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
237 # Start and wait for DB
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
238 # db_name = extract_db_name(db_uri)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
239 # start_postgres_container(db_name)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
240 MAX_RETRIES = 3
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
241 for attempt in range(1, MAX_RETRIES + 1):
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
242 try:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
243 wait_for_db(db_uri)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
244 break # Success
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
245 except Exception as e:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
246 if attempt == MAX_RETRIES:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
247 print(f"Attempt {attempt} failed: Could not connect to database at {db_uri}.")
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
248 raise e
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
249 else:
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
250 time.sleep(2)
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
251
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
252 # Push annotations
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
253 push_gb_annotations(
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
254 gb_file_list,
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
255 sequence_column,
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
256 annotation_column,
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
257 db_uri,
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
258 table,
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
259 fragment_column,
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
260 args.output,
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
261 args.file_name_mapping
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
262 )
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
263
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
264
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
265 if __name__ == "__main__":
9f845ccb1a9f planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff changeset
266 main()