Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Neuron search and switch to scicrunch _dev #19

Open
wants to merge 64 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
72f5ecc
Merge remote-tracking branch 'nih/master'.
agarny Nov 11, 2020
6cf3c3c
Merge remote-tracking branch 'upstream/master'.
agarny Nov 11, 2020
60a9276
Merge remote-tracking branch 'upstream/master'.
agarny Feb 1, 2021
df7d8ef
Merge branch 'main' of https://github.com/agarny/sparc-api into main
agarny Mar 1, 2021
34eb00d
Merge remote-tracking branch 'upstream/main'.
agarny May 3, 2021
0ab30f9
Merge remote-tracking branch 'nih-sparc/master'.
agarny May 6, 2021
9597f07
Merge remote-tracking branch 'upstream/main'.
agarny May 6, 2021
bd472bf
Prevent the app from crashing if SPARC_PORTAL_AWS_KEY and/or SPARC_PO…
agarny May 6, 2021
1727449
Require the `osparc` Python package.
agarny May 6, 2021
471a5e1
Merge branch 'main' into simulation.
agarny May 6, 2021
5760f3c
Retrieve oSPARC-related configuration.
agarny May 6, 2021
84e380e
Updated the required version of various Python packages.
agarny May 6, 2021
42b693d
Retrieve oSPARC-related configuration.
agarny May 6, 2021
1ac51c5
Added an endpoint to run a simulation on oSPARC.
agarny May 7, 2021
ebe54e3
Pass a model URL to run_simulation().
agarny May 7, 2021
9c8326e
Some minor cleaning up.
agarny May 7, 2021
79c97d9
Add support for arguments to the run_simulation endpoint.
agarny May 7, 2021
1bc81cb
Can now pip install the version we need of the oSPARC API.
agarny May 10, 2021
ebbeb82
Some minor cleaning up.
agarny May 11, 2021
0591f99
Use the POST method for our simulation endpoint.
agarny May 11, 2021
8c149f8
Slight improvements to README.md.
agarny May 11, 2021
943461b
oSPARC: check whether the exception has at least one argument.
agarny May 12, 2021
fa5fbca
Some tests for the `/simulation` endpoint.
agarny May 12, 2021
74badcf
Working implementation of scaffold search
Tehsurfer May 20, 2021
ddd66b4
Merge branch 'master' of https://github.com/nih-sparc/sparc-api into …
alan-wu May 30, 2021
5e35096
Add endpoint to retrieve doi from scicrunch
Tehsurfer Jun 1, 2021
9b37fc6
switch to more up to date index
Tehsurfer Jun 1, 2021
3d666ab
Merge remote-tracking branch 'upstream/main'.
agarny Jun 1, 2021
6f96294
Merge branch 'main' into simulation.
agarny Jun 1, 2021
1e39cff
Use the latest SciCrunch endpoint we were given.
agarny Jun 2, 2021
dc2ab72
process_kb_results: prevent some warnings.
agarny Jun 2, 2021
c206f58
Revert "process_kb_results: prevent some warnings."
agarny Jun 2, 2021
a15095a
process_kb_results: prevent some warnings.
agarny Jun 2, 2021
6469759
Use the latest development version of the SciCrunch endpoint.
agarny Jun 2, 2021
20de86e
process_kb_results: retrieve additional links.
agarny Jun 2, 2021
35df8f3
Use SciCrunch rather than Scicrunch in error messages.
agarny Jun 2, 2021
8037229
Can now filter results for scaffold and/or simulation datasets.
agarny Jun 3, 2021
2a14415
Some minor cleaning up.
agarny Jun 3, 2021
070169c
Filtering: can now filter for "All xxx".
agarny Jun 3, 2021
56d4d14
Simulation filtering: look for either "CellML" or "SED-ML".
agarny Jun 3, 2021
60c7ce5
Simulation filtering: look for either "CellML" or "SED-ML".
agarny Jun 3, 2021
3ddc1c6
Added a couple of tests for the scaffolds and simulations filtering.
agarny Jun 3, 2021
0c6df77
Added a couple of tests for the scaffolds and simulations filtering.
agarny Jun 3, 2021
7698bc7
Revert the default value of SCI_CRUNCH_HOST.
agarny Jun 3, 2021
ec7d7f0
Filtering: use the scaffold MIME type rather an item's name.
agarny Jun 4, 2021
e89602b
Add doi endpoint
Tehsurfer Jun 15, 2021
6efca8e
Switch back to stable endpoint
Tehsurfer Jun 15, 2021
d8547f7
Added "pmr_latest_exposure" to retrieve the latest exposure, if any, …
agarny Jun 16, 2021
0a58725
Add endpoint for neuron queries
Tehsurfer Jun 18, 2021
61c8771
Switch to dev endpoint
Tehsurfer Jun 22, 2021
17836d4
Adjust query endpoint to be more flexible
Tehsurfer Jun 22, 2021
10ce60f
Bug fix and query change
Tehsurfer Jun 22, 2021
300a85a
Fix mistake in order of data processing
Tehsurfer Jun 22, 2021
2bc2d6a
Switch to using additional mimetypes as intented
Tehsurfer Jun 23, 2021
f93de5b
Merge branch 'mimetypes-used' of https://github.com/Tehsurfer/sparc-a…
alan-wu Jun 23, 2021
7f4865e
Rename genotype to organ.
alan-wu Jun 24, 2021
e1074b7
Merge branch 'simulation' of https://github.com/agarny/sparc-api into…
alan-wu Jun 24, 2021
6fab9e0
Fix to hit processing
Tehsurfer Jun 24, 2021
033b9dc
Merge branch 'mimetypes-used' of https://github.com/Tehsurfer/sparc-a…
alan-wu Jun 24, 2021
c72739e
Improve filters for dataset with scaffold.
alan-wu Jun 24, 2021
39f43f2
Add test for available scaffolds.
alan-wu Aug 2, 2021
2f59cf3
Change to use the production endpoint.
alan-wu Aug 3, 2021
fe65ca7
Add pubmed proxy
Tehsurfer Aug 10, 2021
e9e4970
Merge branch 'main' into scicrunch-doi
Tehsurfer Aug 11, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Make sure you have python 3 installed `python3 --version`
python3 -m venv ./venv
. ./venv/bin/activate
pip install -r requirements.txt
pip install -r requirements-dev.txt
gunicorn main:app
```

Expand All @@ -24,6 +23,7 @@ If you do not have the NIH SPARC portal user environment variables setup already
After the previous steps or if you already have those environment variables setup, run:

```
export PYTHONPATH=`pwd`
pip install -r requirements-dev.txt
pytest
```
5 changes: 4 additions & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Config(object):
KNOWLEDGEBASE_KEY = os.environ.get("KNOWLEDGEBASE_KEY", "secret-key")
DEPLOY_ENV = os.environ.get("DEPLOY_ENV", "development")
SPARC_APP_HOST = os.environ.get("SPARC_APP_HOST", "https://sparc-app.herokuapp.com")
SCI_CRUNCH_HOST = os.environ.get("SCICRUNCH_HOST", "https://scicrunch.org/api/1/elastic/SPARC_Datasets_pr")
SCI_CRUNCH_HOST = os.environ.get("SCICRUNCH_HOST", "https://scicrunch.org/api/1/elastic/SPARC_PortalDatasets_pr")
MAPSTATE_TABLENAME = os.environ.get("MAPSTATE_TABLENAME", "mapstates")
SCAFFOLDSTATE_TABLENAME = os.environ.get("SCAFFOLDSTATE_TABLENAME", "scaffoldstates")
WRIKE_TOKEN = os.environ.get("WRIKE_TOKEN")
Expand All @@ -42,4 +42,7 @@ class Config(object):
CCB_HEAD_WRIKE_ID = os.environ.get("CCB_HEAD_WRIKE_ID")
MODERATOR_WRIKE_ID = os.environ.get("MODERATOR_WRIKE_ID")
MAILCHIMP_API_KEY = os.environ.get("MAILCHIMP_API_KEY")
OSPARC_API_URL=os.environ.get("OSPARC_API_URL", "https://api.osparc.io")
OSPARC_API_KEY=os.environ.get("OSPARC_API_KEY")
OSPARC_API_SECRET=os.environ.get("OSPARC_API_SECRET")

98 changes: 95 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
from requests.auth import HTTPBasicAuth
import os

import app.osparc as osparc
import requests

# from pymongo import MongoClient

app = Flask(__name__)
Expand Down Expand Up @@ -210,6 +213,59 @@ def direct_download_url(path):
resource = response["Body"].read()
return resource

# /scicrunch/: Returns scicrunch results for a given <search> query
@app.route("/scicrunch-dataset/<doi1>/<doi2>")
def sci_doi(doi1,doi2):
doi = doi1 + '/' + doi2
print(doi)
data = create_doi_request(doi)
try:
response = requests.post(
f'{Config.SCI_CRUNCH_HOST}/_search?api_key={Config.KNOWLEDGEBASE_KEY}',
json=data)
return response.json()
except requests.exceptions.HTTPError as err:
logging.error(err)
return json.dumps({'error': err})

# /pubmed/<id> Used as a proxy for making requests to pubmed
@app.route("/pubmed/<id>")
@app.route("/pubmed/<id>/")
def pubmed(id):
try:
response = requests.get(f'https://pubmed.ncbi.nlm.nih.gov/{id}/')
return response.text
except requests.exceptions.HTTPError as err:
logging.error(err)
return json.dumps({'error': err})

# /scicrunch-query-string/: Returns results for given organ curie. These can be processed by the sidebar
@app.route("/scicrunch-query-string/")
def sci_organ():
fields = request.args.getlist('field')
curie = request.args.get('curie')
# field example: "*organ.curie"
data = {
"size": 20,
"from": 0,
"query": {
"query_string": {
"fields": fields,
"query": curie
}
}
}

try:
response = requests.post(
f'{Config.SCI_CRUNCH_HOST}/_search?api_key={Config.KNOWLEDGEBASE_KEY}',
json=data)
return process_kb_results(response.json())
except requests.exceptions.HTTPError as err:
logging.error(err)
return json.dumps({'error': err})



# /search/: Returns scicrunch results for a given <search> query
@app.route("/search/", defaults={'query': ''})
Expand Down Expand Up @@ -243,9 +299,9 @@ def filter_search(query):
results = process_kb_results(response.json())
except requests.exceptions.HTTPError as err:
logging.error(err)
return jsonify({'error': str(err), 'message': 'Scicrunch is not currently reachable, please try again later'}), 502
return jsonify({'error': str(err), 'message': 'SciCrunch is not currently reachable, please try again later'}), 502
except json.JSONDecodeError as e:
return jsonify({'message': 'Could not parse Scicrunch output, please try again later',
return jsonify({'message': 'Could not parse SciCrunch output, please try again later',
'error': 'JSONDecodeError'}), 502
return results

Expand All @@ -268,7 +324,7 @@ def get_facets(type):
json_result = response.json()
results.append(json_result)
except BaseException as e:
return jsonify({'message': 'Could not parse Scicrunch output, please try again later',
return jsonify({'message': 'Could not parse SciCrunch output, please try again later',
'error': 'JSONDecodeError'}), 502

# Select terms from the results
Expand Down Expand Up @@ -565,3 +621,39 @@ def subscribe_to_mailchimp():
return resp.json()
else:
abort(400, description="Missing email_address, first_name or last_name")


@app.route("/simulation", methods=["POST"])
def simulation():
data = request.get_json()

if data and "model_url" in data and "json_config" in data:
return json.dumps(osparc.run_simulation(data["model_url"], data["json_config"]))
else:
abort(400, description="Missing model URL and/or JSON configuration")


@app.route("/pmr_latest_exposure", methods=["POST"])
def pmr_latest_exposure():
data = request.get_json()

if data and "workspace_url" in data:
try:
resp = requests.get(data["workspace_url"],
headers={"Accept": "application/vnd.physiome.pmr2.json.1"})
if resp.status_code == 200:
try:
# Return the latest exposure for the given workspace.
url = resp.json()["collection"]["items"][0]["links"][0]["href"]
except:
# There is no latest exposure for the given workspace.
url = ""
return jsonify(
url=url
)
else:
return resp.json()
except:
abort(400, description="Invalid workspace URL")
else:
abort(400, description="Missing workspace URL")
103 changes: 103 additions & 0 deletions app/osparc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from app.config import Config
import json
import osparc
import tempfile
from time import sleep


def run_simulation(model_url, json_config):
temp_config_file = tempfile.NamedTemporaryFile(mode="w+")

json.dump(json_config, temp_config_file)

temp_config_file.seek(0)

try:
api_client = osparc.ApiClient(osparc.Configuration(
host=Config.OSPARC_API_URL,
username=Config.OSPARC_API_KEY,
password=Config.OSPARC_API_SECRET
))

# Upload the configuration file.

files_api = osparc.FilesApi(api_client)

try:
config_file = files_api.upload_file(temp_config_file.name)
except:
raise Exception(
"the simulation configuration file could not be uploaded")

# Create the simulation.

solvers_api = osparc.SolversApi(api_client)

solver = solvers_api.get_solver_release(
"simcore/services/comp/opencor", "1.0.3")

job = solvers_api.create_job(
solver.id,
solver.version,
osparc.JobInputs({
"model_url": model_url,
"config_file": config_file
})
)

# Start the simulation job.

status = solvers_api.start_job(solver.id, solver.version, job.id)

if status.state != "PUBLISHED":
raise Exception("the simulation job could not be submitted")

# Wait for the simulation job to be complete (or to fail).

while True:
status = solvers_api.inspect_job(solver.id, solver.version, job.id)

if status.progress == 100:
break

sleep(1)

status = solvers_api.inspect_job(solver.id, solver.version, job.id)

if status.state != "SUCCESS":
raise Exception("the simulation failed")

# Retrieve the simulation job outputs.

try:
outputs = solvers_api.get_job_outputs(
solver.id, solver.version, job.id)
except:
raise Exception(
"the simulation job outputs could not be retrieved")

# Download the simulation results.

try:
results_filename = files_api.download_file(
outputs.results["output_1"].id)
except:
raise Exception("the simulation results could not be retrieved")

results_file = open(results_filename, "r")

res = {
"status": "ok",
"results": json.load(results_file)
}

results_file.close()
except Exception as e:
res = {
"status": "nok",
"description": e.args[0] if len(e.args) > 0 else "unknown"
}

temp_config_file.close()

return res
Loading