Mercurial > repos > tduigou > seq_from_db
annotate get_db_info.py @ 2:11a3752feb0a draft default tip
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
| author | tduigou |
|---|---|
| date | Wed, 15 Oct 2025 12:33:41 +0000 |
| parents | 7680420caf9f |
| children |
| rev | line source |
|---|---|
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
1 import subprocess |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
2 import argparse |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
3 import time |
|
1
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
4 import json |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
5 import os |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
6 import socket |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
7 import re |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
8 from Bio.Seq import Seq |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
9 import pandas as pd |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
10 from Bio.SeqRecord import SeqRecord |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
11 from sqlalchemy import create_engine, inspect |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
12 from sqlalchemy.engine.url import make_url |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
13 from sqlalchemy.sql import text |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
14 from sqlalchemy.exc import OperationalError |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
15 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
16 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
17 def fix_db_uri(uri): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
18 """Replace __at__ with @ in the URI if needed.""" |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
19 return uri.replace("__at__", "@") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
20 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
21 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
22 def is_port_in_use(uri): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
23 """Check if a TCP port is already in use on host.""" |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
24 url = make_url(uri) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
25 host = url.host |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
26 port = url.port |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
27 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
28 s.settimeout(2) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
29 return s.connect_ex((host, port)) == 0 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
30 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
31 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
32 def extract_db_name(uri): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
33 """Extract the database name from the SQLAlchemy URI.""" |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
34 url = make_url(uri) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
35 return url.database |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
36 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
37 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
38 # this fuction is to activate the Docker id the DB is in container. BUT IT IS NOT USED IN MAIN() |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
39 def start_postgres_container(db_name): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
40 """Start a PostgreSQL container with the given database name as the container name.""" |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
41 container_name = db_name |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
42 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
43 # Check if container is already running |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
44 container_running = subprocess.run( |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
45 f"docker ps -q -f name={container_name}", shell=True, capture_output=True, text=True |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
46 ) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
47 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
48 if container_running.stdout.strip(): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
49 print(f"Container '{container_name}' is already running.") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
50 return |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
51 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
52 # Check if container exists (stopped) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
53 container_exists = subprocess.run( |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
54 f"docker ps -a -q -f name={container_name}", shell=True, capture_output=True, text=True |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
55 ) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
56 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
57 if container_exists.stdout.strip(): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
58 print(f"Starting existing container '{container_name}'...") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
59 subprocess.run(f"docker start {container_name}", shell=True) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
60 print(f"PostgreSQL Docker container '{container_name}' activated.") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
61 return |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
62 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
63 # If container does not exist, create and start a new one |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
64 port = 5432 if not is_port_in_use(5432) else 5433 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
65 postgres_password = os.getenv("POSTGRES_PASSWORD", "RK17") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
66 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
67 start_command = [ |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
68 "docker", "run", "--name", container_name, |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
69 "-e", f"POSTGRES_PASSWORD={postgres_password}", |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
70 "-p", f"{port}:5432", |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
71 "-d", "postgres" |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
72 ] |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
73 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
74 try: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
75 subprocess.run(start_command, check=True) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
76 print(f"PostgreSQL Docker container '{container_name}' started on port {port}.") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
77 except subprocess.CalledProcessError as e: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
78 print(f"Failed to start Docker container: {e}") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
79 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
80 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
81 def wait_for_db(uri, timeout=60): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
82 """Try connecting to the DB until it works or timeout.""" |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
83 engine = create_engine(uri) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
84 start_time = time.time() |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
85 while time.time() - start_time < timeout: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
86 try: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
87 with engine.connect(): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
88 print("Connected to database.") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
89 return |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
90 except OperationalError: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
91 print("Database not ready, retrying...") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
92 time.sleep(2) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
93 raise Exception("Database connection failed after timeout.") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
94 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
95 |
|
2
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
96 def fetch_annotations(csv_file, sequence_column, annotation_columns, db_uri, table_name, fragment_column_name, output, output_report): |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
97 """Fetch annotations from the database and save the result as GenBank files.""" |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
98 db_uri = fix_db_uri(db_uri) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
99 df = pd.read_csv(csv_file, sep=',', header=None) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
100 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
101 engine = create_engine(db_uri) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
102 connection = engine.connect() |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
103 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
104 annotated_data = [] |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
105 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
106 try: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
107 with connection: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
108 inspector = inspect(engine) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
109 columns = [column['name'] for column in inspector.get_columns(table_name)] |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
110 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
111 # Fetch all fragments from the table once |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
112 if fragment_column_name not in columns: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
113 raise ValueError(f"Fragment column '{fragment_column_name}' not found in table '{table_name}'.") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
114 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
115 fragment_column_index = columns.index(fragment_column_name) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
116 all_rows = connection.execute(text(f"SELECT * FROM {table_name}")).fetchall() |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
117 fragment_map = {row[fragment_column_index]: row for row in all_rows} |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
118 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
119 # Compare fragments between CSV and DB |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
120 csv_fragments = set() |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
121 all_ids = set(df[0].dropna().astype(str)) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
122 for _, row in df.iterrows(): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
123 for col in df.columns: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
124 if col != 0: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
125 fragment = row[col] |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
126 if pd.notna(fragment): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
127 fragment_str = str(fragment) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
128 if fragment_str not in all_ids: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
129 csv_fragments.add(fragment_str) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
130 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
131 db_fragments = set(fragment_map.keys()) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
132 missing_fragments = sorted(list(csv_fragments - db_fragments)) |
|
2
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
133 |
|
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
134 # Write report file |
|
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
135 with open(output_report, "w") as report_file: |
|
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
136 if missing_fragments: |
|
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
137 for frag in missing_fragments: |
|
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
138 report_file.write(f"{frag}\n") |
|
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
139 else: |
|
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
140 report_file.write("") |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
141 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
142 # === CONTINUE WITH GB FILE CREATION === |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
143 for _, row in df.iterrows(): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
144 annotated_row = {"Backbone": row[0], "Fragments": []} |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
145 for col in df.columns: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
146 if col != 0: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
147 fragment = row[col] |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
148 if fragment not in csv_fragments: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
149 continue |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
150 db_row = fragment_map.get(fragment) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
151 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
152 if db_row: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
153 fragment_data = {"id": fragment} |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
154 for i, column_name in enumerate(columns[1:]): # skip ID column |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
155 fragment_data[column_name] = db_row[i + 1] |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
156 else: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
157 fragment_data = {"id": fragment, "metadata": "No data found"} |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
158 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
159 annotated_row["Fragments"].append(fragment_data) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
160 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
161 annotated_data.append(annotated_row) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
162 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
163 except Exception as e: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
164 print(f"Error occurred during annotation: {e}") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
165 raise # Ensures the error exits the script |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
166 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
167 # GenBank file generation per fragment |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
168 try: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
169 for annotated_row in annotated_data: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
170 backbone_id = annotated_row["Backbone"] |
|
2
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
171 |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
172 for fragment in annotated_row["Fragments"]: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
173 fragment_id = fragment["id"] |
|
2
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
174 |
|
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
175 # Skip generation for missing fragments |
|
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
176 if fragment_id in missing_fragments: |
|
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
177 continue |
|
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
178 |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
179 sequence = fragment.get(sequence_column, "") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
180 annotation = fragment.get(annotation_columns, "") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
181 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
182 # Create the SeqRecord |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
183 record = SeqRecord( |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
184 Seq(sequence), |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
185 id=fragment_id, |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
186 name=fragment_id, |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
187 description=f"Fragment {fragment_id} from Backbone {backbone_id}" |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
188 ) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
189 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
190 # Add annotations to GenBank header |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
191 record.annotations = { |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
192 k: str(fragment[k]) for k in annotation_columns if k in fragment |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
193 } |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
194 |
|
2
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
195 # LOCUS line extraction from annotation |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
196 locus_line_match = re.search(r"LOCUS\s+.+", annotation) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
197 if locus_line_match: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
198 locus_line = locus_line_match.group() |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
199 else: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
200 print(f"LOCUS info missing for fragment {fragment_id}") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
201 locus_line = f"LOCUS {fragment_id: <20} {len(sequence)} bp DNA linear UNK 01-JAN-2025" |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
202 |
|
2
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
203 # Format sequence |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
204 if "ORIGIN" in sequence: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
205 origin_block = sequence.strip() |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
206 else: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
207 formatted_sequence = "ORIGIN\n" |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
208 seq_str = str(record.seq) |
|
2
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
209 for i in range(0, len(seq_str), 60): |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
210 line_seq = seq_str[i:i + 60] |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
211 formatted_sequence += f"{str(i + 1).rjust(9)} { ' '.join([line_seq[j:j+10] for j in range(0, len(line_seq), 10)]) }\n" |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
212 origin_block = formatted_sequence.strip() |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
213 |
|
2
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
214 # Extract FEATURES section |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
215 features_section = "" |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
216 features_start = annotation.find("FEATURES") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
217 if features_start != -1: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
218 features_section = annotation[features_start:] |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
219 |
|
2
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
220 # Write GenBank file |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
221 if not os.path.exists(output): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
222 os.makedirs(output) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
223 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
224 gb_filename = os.path.join(output, f"{fragment_id}.gb") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
225 with open(gb_filename, "w") as f: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
226 f.write(locus_line + "\n") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
227 f.write(f"DEFINITION {record.description}\n") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
228 f.write(f"ACCESSION {record.id}\n") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
229 f.write(f"VERSION DB\n") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
230 f.write(f"KEYWORDS .\n") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
231 f.write(f"SOURCE .\n") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
232 f.write(features_section) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
233 f.write(origin_block + "\n") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
234 f.write("//\n") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
235 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
236 except Exception as e: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
237 print(f"Error saving GenBank files: {e}") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
238 return |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
239 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
240 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
241 def main(): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
242 parser = argparse.ArgumentParser(description="Fetch annotations from PostgreSQL database and save as JSON.") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
243 parser.add_argument("--input", required=True, help="Input CSV file") |
|
1
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
244 parser.add_argument("--use_json_paramers", required=True, help="Use parameters from JSON: true/false") |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
245 parser.add_argument("--sequence_column", required=False, help="DB column contains sequence for ganbank file") |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
246 parser.add_argument("--annotation_columns", required=False, help="DB column contains head for ganbank file") |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
247 parser.add_argument("--db_uri", required=False, help="Database URI connection string") |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
248 parser.add_argument("--table", required=False, help="Table name in the database") |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
249 parser.add_argument("--fragment_column", required=False, help="Fragment column name in the database") |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
250 parser.add_argument("--output", required=True, help="Output dir for gb files") |
|
1
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
251 parser.add_argument("--json_conf", required=False, help="JSON config file with DB parameters") |
|
2
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
252 parser.add_argument("--report", required=True, help="Output report for fragments checking in DB") |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
253 args = parser.parse_args() |
|
1
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
254 |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
255 # get param and chek for json |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
256 config_params = {} |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
257 use_json = args.use_json_paramers == 'true' |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
258 if use_json: |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
259 if not args.json_conf: |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
260 raise ValueError("You must provide --json_conf when --use_json_paramers is 'true'") |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
261 with open(args.json_conf, "r") as f: |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
262 config_params = json.load(f) |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
263 else: |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
264 config_params = { |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
265 "table": args.table, |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
266 "sequence_column": args.sequence_column, |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
267 "annotation_column": args.annotation_columns, |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
268 "fragment_column": args.fragment_column, |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
269 "db_uri": args.db_uri, |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
270 } |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
271 |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
272 # Extract final resolved parameters |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
273 table = config_params["table"] |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
274 sequence_column = config_params["sequence_column"] |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
275 annotation_column = config_params["annotation_column"] |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
276 fragment_column = config_params["fragment_column"] |
|
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
277 db_uri = fix_db_uri(config_params["db_uri"]) |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
278 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
279 # Wait until the database is ready |
|
1
7680420caf9f
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit db4ac861e1d03fcdfe94321d858839124e493930-dirty
tduigou
parents:
0
diff
changeset
|
280 db_uri = fix_db_uri(db_uri) |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
281 # db_name = extract_db_name(db_uri) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
282 # start_postgres_container(db_name) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
283 MAX_RETRIES = 3 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
284 for attempt in range(1, MAX_RETRIES + 1): |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
285 try: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
286 wait_for_db(db_uri) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
287 break # Success |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
288 except Exception as e: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
289 if attempt == MAX_RETRIES: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
290 print(f"Attempt {attempt} failed: Could not connect to database at {db_uri}.") |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
291 raise e |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
292 else: |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
293 time.sleep(2) |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
294 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
295 # Fetch annotations from the database and save as gb |
|
2
11a3752feb0a
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 7f5d8b62d749a0c41110cd9c04e0254e4fd44893-dirty
tduigou
parents:
1
diff
changeset
|
296 fetch_annotations(args.input, sequence_column, annotation_column, db_uri, table, fragment_column, args.output, args.report) |
|
0
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
297 |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
298 if __name__ == "__main__": |
|
3dfba58f44f1
planemo upload for repository https://github.com/brsynth/galaxytools/tree/main/tools commit 3401816c949b538bd9c67e61cbe92badff6a4007-dirty
tduigou
parents:
diff
changeset
|
299 main() |
