-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathvcelldb_service.py
More file actions
386 lines (318 loc) · 13.7 KB
/
vcelldb_service.py
File metadata and controls
386 lines (318 loc) · 13.7 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from app.core.logger import get_logger
import httpx
import asyncio
import re
from app.schemas.vcelldb_schema import BiomodelRequestParams, SimulationRequestParams
from urllib.parse import urlencode, quote
from langfuse import observe
from typing import List
VCELL_API_BASE_URL = "https://vcell.cam.uchc.edu/api/v0"
logger = get_logger("vcelldb_service")
def sanitize_vcml_content(vcml_content: str) -> str:
"""
Sanitizes VCML content by removing only ImageData tags and their content.
Args:
vcml_content (str): Raw VCML content as string.
Returns:
str: Sanitized VCML content with ImageData tags removed.
"""
# Remove only ImageData tags and their content using regex
# This pattern matches <ImageData ...> ... </ImageData> including nested content
# The pattern handles multiline content and preserves the rest of the XML structure
sanitized_content = re.sub(
r"<ImageData[^>]*>.*?</ImageData>",
"",
vcml_content,
flags=re.DOTALL | re.MULTILINE,
)
# Clean up any extra whitespace that might be left after removing ImageData
sanitized_content = re.sub(r"\n\s*\n", "\n", sanitized_content)
logger.info("VCML content sanitized: ImageData tags removed")
return sanitized_content
async def check_vcell_connectivity() -> bool:
"""
Check if the VCell API is reachable by attempting to resolve the hostname.
Returns:
bool: True if the API is reachable, False otherwise.
"""
try:
import socket
hostname = "vcell.cam.uchc.edu"
logger.info(f"Checking connectivity to {hostname}")
# Try to resolve the hostname
ip_address = socket.gethostbyname(hostname)
logger.info(f"Successfully resolved {hostname} to {ip_address}")
return True
except socket.gaierror as e:
logger.error(f"DNS resolution failed for {hostname}: {e}")
return False
except Exception as e:
logger.error(f"Unexpected error checking connectivity: {e}")
return False
@observe(name="FETCH_BIOMODELS")
async def fetch_biomodels(params: BiomodelRequestParams) -> dict:
"""
Fetch a list of biomodels from the VCell API based on filtering and sorting parameters.
Args:
params (BiomodelRequestParams): Request parameters for filtering biomodels.
Returns:
dict: A dictionary containing a list of biomodels with metadata.
"""
# Transform None to "" (optional, only if needed for empty fields)
params_dict = {k: (v if v is not None else "") for k, v in params.dict().items()}
logger.info(f"Fetching biomodels with parameters: {params_dict}")
# Construct the query string using urlencoded parameters (params_dict)
query_string = urlencode(params_dict)
# Construct the full URL
url = f"{VCELL_API_BASE_URL}/biomodel?{query_string}"
# Log the URL being queried
logger.info(f"Querying URL: {url}")
# Perform the API request
async with httpx.AsyncClient() as client:
response = await client.get(url)
response.raise_for_status()
raw_data = response.json()
# Extract biomodels list (assuming API returns a list directly)
biomodels = raw_data if isinstance(raw_data, list) else raw_data.get("data", [])
# Build response with metadata
return {
"search_params": params_dict,
"models_count": len(biomodels),
"unique_model_keys (bmkey)": [
model.get("bmKey") for model in biomodels if model.get("bmKey")
],
"data": biomodels,
}
@observe(name="FETCH_SIMULATION_DETAILS")
async def fetch_simulation_details(params: SimulationRequestParams) -> dict:
"""
Fetch detailed information about a specific simulation for a given biomodel.
Args:
params (SimulationRequestParams): Contains both biomodel ID and simulation ID.
Returns:
Simulation: A Simulation object containing simulation details.
"""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{VCELL_API_BASE_URL}/biomodel/{params.bmId}/simulation/{params.simId}"
)
response.raise_for_status()
return response.json()
@observe(name="GET_VCML_FILE")
async def get_vcml_file(
biomodel_id: str, truncate: bool = False, max_retries: int = 3
) -> str:
"""
Fetches the VCML file content for a given biomodel with retry logic.
Args:
biomodel_id (str): ID of the biomodel.
truncate (bool): Whether to truncate the VCML file.
max_retries (int): Maximum number of retry attempts.
Returns:
str: VCML content of the biomodel.
"""
logger.info(f"Fetching VCML file for biomodel: {biomodel_id}")
# Check connectivity first
if not await check_vcell_connectivity():
logger.error(
"VCell API is not reachable. Please check your network connection and DNS settings."
)
raise Exception(
"VCell API is not reachable. Please check your network connection and DNS settings."
)
for attempt in range(max_retries + 1):
try:
url = f"{VCELL_API_BASE_URL}/biomodel/{biomodel_id}/biomodel.vcml"
logger.info(
f"Requesting URL: {url} (attempt {attempt + 1}/{max_retries + 1})"
)
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.get(url)
logger.info(f"Response status: {response.status_code}")
logger.info(f"Response headers: {dict(response.headers)}")
response.raise_for_status()
if truncate:
return sanitize_vcml_content(response.text[:500])
else:
return sanitize_vcml_content(response.text)
except httpx.HTTPStatusError as e:
logger.error(
f"HTTP error fetching VCML file for biomodel {biomodel_id}: {e.response.status_code} - {e.response.text}"
)
if attempt == max_retries:
raise e
logger.warning(f"Retrying in {2 ** attempt} seconds...")
await asyncio.sleep(2**attempt)
except httpx.RequestError as e:
logger.error(
f"Request error fetching VCML file for biomodel {biomodel_id}: {str(e)}"
)
if attempt == max_retries:
raise e
logger.warning(f"Retrying in {2 ** attempt} seconds...")
await asyncio.sleep(2**attempt)
except Exception as e:
logger.error(
f"Unexpected error fetching VCML file for biomodel {biomodel_id}: {str(e)}"
)
if attempt == max_retries:
raise e
logger.warning(f"Retrying in {2 ** attempt} seconds...")
await asyncio.sleep(2**attempt)
# This should never be reached, but just in case
raise Exception(
f"Failed to fetch VCML file for biomodel {biomodel_id} after {max_retries + 1} attempts"
)
@observe(name="GET_SBML_FILE")
async def get_sbml_file(biomodel_id: str) -> str:
"""
Fetches the SBML file content for a given biomodel.
Args:
biomodel_id (str): ID of the biomodel.
Returns:
str: SBML content of the biomodel.
"""
try:
url = f"{VCELL_API_BASE_URL}/biomodel/{biomodel_id}/biomodel.sbml"
logger.info(f"Requesting SBML file URL: {url}")
async with httpx.AsyncClient(timeout=180.0) as client:
response = await client.get(url)
response.raise_for_status()
return response.text
except httpx.HTTPStatusError as e:
logger.error(
f"HTTP error fetching SBML file for biomodel {biomodel_id}: {e.response.status_code} - {e.response.text}"
)
raise e
except httpx.RequestError as e:
logger.error(
f"Request error fetching SBML file for biomodel {biomodel_id}: {str(e)}"
)
raise e
except Exception as e:
logger.error(
f"Unexpected error fetching SBML file for biomodel {biomodel_id}: {str(e)}"
)
raise e
@observe(name="GET_DIAGRAM_URL")
async def get_diagram_url(biomodel_id: str) -> str:
"""
Gets diagram image URL for a given biomodel.
Args:
biomodel_id (str): ID of the biomodel.
Returns:
str: URL pointing to the biomodel's diagram image.
"""
return f"{VCELL_API_BASE_URL}/biomodel/{biomodel_id}/diagram"
@observe(name="GET_DIAGRAM_IMAGE")
async def get_diagram_image(biomodel_id: str) -> bytes:
"""
Fetches the diagram image for a given biomodel from the VCell API and returns the image bytes.
Args:
biomodel_id (str): ID of the biomodel.
Returns:
bytes: The image content (PNG) of the biomodel diagram.
"""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{VCELL_API_BASE_URL}/biomodel/{biomodel_id}/diagram"
)
response.raise_for_status()
return response.content
@observe(name="FETCH_BIOMODEL_APPLICATIONS_FILES")
async def fetch_biomodel_applications_files(biomodel_id: str) -> dict:
"""
Fetch applications data along with SBML and BNGL file URLs for a given biomodel.
Args:
biomodel_id (str): ID of the biomodel.
Returns:
dict: A dictionary containing applications data and file URLs for each application.
"""
params = BiomodelRequestParams(bmId=biomodel_id)
logger.info(f"Fetching biomodel applications files for biomodel: {biomodel_id}")
biomodel_data = await fetch_biomodels(params)
logger.info(f"Biomodel data: {biomodel_data}")
applications = []
if biomodel_data.get("data") and len(biomodel_data["data"]) > 0:
biomodel = biomodel_data["data"][0]
applications = biomodel.get("applications", [])
logger.info(f"Applications: {applications}")
# Generate file URLs for each application
applications_with_files = []
for app in applications:
app_name = app.get("name", "")
# URL encode the app name for use in query parameters
encoded_app_name = quote(app_name)
# Create file URLs
bngl_url = f"{VCELL_API_BASE_URL}/biomodel/{biomodel_id}/biomodel.bngl?appname={encoded_app_name}"
sbml_url = f"{VCELL_API_BASE_URL}/biomodel/{biomodel_id}/biomodel.sbml?appname={encoded_app_name}"
# Add file URLs to the application data
app_with_files = {**app, "bngl_url": bngl_url, "sbml_url": sbml_url}
logger.info(f"Application with files: {app_with_files}")
applications_with_files.append(app_with_files)
logger.info(f"Applications with files: {applications_with_files}")
return {
"biomodel_id": biomodel_id,
"applications": applications_with_files,
"total_applications": len(applications_with_files),
}
@observe(name="FETCH_PUBLICATIONS")
async def fetch_publications() -> List[dict]:
"""
Fetch a list of publications from the VCell API.
Returns:
List[dict]: A list of publication dictionaries with sanitized data.
"""
url = f"{VCELL_API_BASE_URL}/publication?submitLow=&submitHigh=&startRow=1&maxRows=1000&hasData=all&waiting=on&queued=on&dispatched=on&running=on"
logger.info(f"Fetching publications from URL: {url}")
try:
async with httpx.AsyncClient() as client:
response = await client.get(url)
response.raise_for_status()
publications = response.json()
# Ensure we return a list of dictionaries
if isinstance(publications, list):
# Sanitize publications by removing unwanted fields
sanitized_publications = []
for pub in publications:
if isinstance(pub, dict):
# Create a copy and remove unwanted fields
sanitized_pub = pub.copy()
sanitized_pub.pop("wittid", None)
sanitized_pub.pop("date", None)
sanitized_pub.pop("url", None)
sanitized_pub.pop("pubKey", None)
sanitized_pub.pop("endnoteid", None)
# Clean up author arrays - remove empty strings and combine
authors = pub.get("authors", [])
if authors:
# Remove empty strings and separators, combine into single string
clean_authors = [
a.strip()
for a in authors
if a.strip() and a.strip() not in ["&", ","]
]
sanitized_pub["authors"] = ", ".join(clean_authors)
sanitized_publications.append(sanitized_pub)
else:
# If not a dict, keep as is but log warning
logger.warning(f"Publication is not a dictionary: {type(pub)}")
sanitized_publications.append(pub)
logger.info(
f"Successfully fetched and sanitized {len(sanitized_publications)} publications"
)
return sanitized_publications
else:
logger.warning(f"Unexpected response format: {type(publications)}")
return []
except httpx.HTTPStatusError as e:
logger.error(
f"HTTP error fetching publications: {e.response.status_code} - {e.response.text}"
)
raise e
except httpx.RequestError as e:
logger.error(f"Request error fetching publications: {str(e)}")
raise e
except Exception as e:
logger.error(f"Unexpected error fetching publications: {str(e)}")
raise e