Skip to content

Commit

Permalink
Add all project files including workflows and scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
brigit-parrish committed Dec 18, 2023
1 parent bec3135 commit e92c64b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
Empty file.
Empty file added src/BioModelsCache/__init__.py
Empty file.
73 changes: 73 additions & 0 deletions src/BioModelsCache/biomodels_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import json
from biomodels_restful_api_client import services as bmservices
import re

def remove_html_tags(text):
"""
Removes html tags from a string
Parameters:
1. text: A string of text with HTML tags that must be removed.
Returns:
str: The input string with all HTML tags removed.
"""
clean = re.compile('<.*?>')
return re.sub(clean, '', text)

def update_cache(model, modelResults):
"""
Update the cache with the model data if it's a curated BioModel and not already present.
Parameters:
1. model: A dictionary representing the model data to be cached.
2. modelResults : A dictionary representing the cached models.
Returns:
bool: Returns True if the cache was updated with the model, False if it is not a BioModel or if the Biomodel
is already in the cache.
"""
modelNumber = model['publicationId']
if "BIOMD" not in modelNumber:
return False

if modelNumber in modelResults and modelResults[modelNumber] == model:
return False

if "description" in model:
model["description"] = remove_html_tags(model["description"])

modelResults[modelNumber] = model
return True

def cache_biomodels():
"""
Fetch and cache information for a set number of BioModels.
"""
totalModels = 2000
i = 0

modelIdentifiers = bmservices.get_model_identifiers()
models = modelIdentifiers["models"]
modelResults = {}

for nModel in models:
if i < totalModels:
result = bmservices.get_model_info(nModel)
if 'publicationId' in result:
updated_cache = update_cache(result, modelResults)
if updated_cache:
i += 1
save_to_json(modelResults)


def save_to_json(modelInfo):
"""
Saves the cached biomodel to the Json file.
Parameters:
1. modelInfo: A dictionary containing the model data to be saved to the file.
"""
with open('cached_biomodels.json', 'w') as json_file:
json.dump(modelInfo, json_file)

Empty file.

0 comments on commit e92c64b

Please sign in to comment.