-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
209 lines (89 loc) · 3.96 KB
/
Copy pathutils.py
File metadata and controls
209 lines (89 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# utils.py
import os
import logging
import subprocess
import requests
import time
from multiprocessing import BoundedSemaphore
global_request_semaphore = BoundedSemaphore(3) # Limit to 3 concurrent requests
def log_message(message, level="INFO"):
levels = {
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR
}
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logging.log(levels.get(level, logging.INFO), message)
def create_directory(path):
try:
os.makedirs(path, exist_ok=True)
return path
except Exception as e:
log_message(f"Error creating directory {path}: {e}", level="ERROR")
return None
def fetch_numeric_id(accession, email="your_email@example.com", tool="FetchOmics",
api_key=None, max_retries=10, delay=5):
"""
If api_key is not None, it will be added to the E-utilities request.
"""
params = {
"db": "gds",
"term": f"{accession}[Accession]",
"retmode": "json",
"email": email,
"tool": tool
}
# Include API key if provided
if api_key:
params["api_key"] = api_key
for attempt in range(1, max_retries + 1):
try:
with global_request_semaphore: # Limit concurrent requests
response = requests.get("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi", params=params)
if response.status_code == 429:
retry_after = int(response.headers.get("Retry-After", delay))
log_message(f"Rate limit exceeded (Attempt {attempt}). Retrying in {retry_after} seconds...")
time.sleep(retry_after)
continue
response.raise_for_status()
data = response.json()
id_list = data.get("esearchresult", {}).get("idlist", [])
if id_list:
log_message(f"Accession: {accession}, First GDS ID: {id_list[0]}")
time.sleep(0.34) # Ensure at most 3 requests per second
return id_list[0]
else:
log_message(f"Accession: {accession}, No GDS ID found.", level="WARNING")
return None
except requests.exceptions.RequestException as e:
log_message(f"Attempt {attempt}: Error fetching numeric ID for {accession}: {e}", level="WARNING")
if attempt < max_retries:
backoff_time = delay * (2 ** (attempt - 1))
log_message(f"Retrying in {backoff_time} seconds...")
time.sleep(backoff_time)
else:
log_message(f"Max retries reached for {accession}.", level="ERROR")
return None
def fetch_runinfo(gds_id, output_dir, max_retries=10, delay=5):
os.makedirs(output_dir, exist_ok=True)
output_file = os.path.join(output_dir, f"{gds_id}_SRA_RunInfo.csv")
command = (
f"elink -db gds -id {gds_id} -target sra | "
f"efetch -format runinfo > {output_file}"
)
for attempt in range(1, max_retries + 1):
try:
with global_request_semaphore:
subprocess.run(command, shell=True, check=True, text=True)
log_message(f"RunInfo for GDS ID {gds_id} saved to {output_file}")
time.sleep(0.34) # Ensure at most 3 requests per second
return output_file
except subprocess.CalledProcessError as e:
log_message(f"Attempt {attempt}: Error fetching RunInfo for GDS ID {gds_id}: {e}", level="WARNING")
if attempt < max_retries:
backoff_time = delay * (2 ** (attempt - 1))
log_message(f"Retrying in {backoff_time} seconds...")
time.sleep(backoff_time)
else:
log_message(f"Max retries reached for GDS ID {gds_id}.", level="ERROR")
return None