Skip to content

Commit

Permalink
Merge pull request #249 from sennetconsortium/tjmadonna/add-vitessce-…
Browse files Browse the repository at this point in the history
…config

Tjmadonna/add vitessce config
  • Loading branch information
maxsibilla authored Jan 5, 2024
2 parents 5f9bdee + 680a408 commit 6821136
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 34 deletions.
2 changes: 2 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from routes.validation import validation_blueprint
from routes.file import file_blueprint
from routes.assayclassifier import assayclassifier_blueprint
from routes.vitessce import vitessce_blueprint

# Local Modules
from lib.file_upload_helper import UploadFileHelper
Expand All @@ -43,6 +44,7 @@
app.register_blueprint(validation_blueprint)
app.register_blueprint(file_blueprint)
app.register_blueprint(assayclassifier_blueprint)
app.register_blueprint(vitessce_blueprint)

# Suppress InsecureRequestWarning warning when requesting status on https with ssl cert verify disabled
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
Expand Down
31 changes: 31 additions & 0 deletions src/routes/assayclassifier/rule_chain.py → src/lib/rule_chain.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import json
import logging
from pathlib import Path
from sys import stdout
import urllib.request

from flask import current_app
from hubmap_commons.schema_tools import check_json_matches_schema
from rule_engine import Rule, EngineError, Context
import yaml
Expand All @@ -12,6 +15,34 @@
SCHEMA_BASE_URI = "http://schemata.hubmapconsortium.org/"


rule_chain = None


def initialize_rule_chain():
global rule_chain
rule_src_uri = current_app.config["RULE_CHAIN_URI"]
try:
json_rules = urllib.request.urlopen(rule_src_uri)
except json.decoder.JSONDecodeError as excp:
raise RuleSyntaxException(excp) from excp
rule_chain = RuleLoader(json_rules).load()
print("RULE CHAIN FOLLOWS")
rule_chain.dump(stdout)
print("RULE CHAIN ABOVE")


def calculate_assay_info(metadata: dict) -> dict:
if not rule_chain:
initialize_rule_chain()
for key, value in metadata.items():
if type(value) is str:
if value.isdigit():
metadata[key] = int(value)
rslt = rule_chain.apply(metadata)
# TODO: check that rslt has the expected parts
return rslt


class NoMatchException(Exception):
pass

Expand Down
File renamed without changes.
5 changes: 4 additions & 1 deletion src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ hubmap-sdk==1.0.4
Werkzeug==2.3.7

# The commons package requires requests>=2.22.0
requests==2.25.1
requests==2.27.1

# Use the published package from PyPI as default
# Use the branch name of commons from github for testing new changes made in commons from different branch
Expand All @@ -16,5 +16,8 @@ atlas-consortia-commons==1.0.5
# For assay type rules
rule_engine==4.1.0

# For vitessce config generation
https://github.com/hubmapconsortium/portal-visualization/archive/refs/tags/0.1.0.zip

# Testing
pytest==7.3.1
36 changes: 3 additions & 33 deletions src/routes/assayclassifier/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import json
import logging
from sys import stdout
import urllib.request

from flask import Blueprint, request, Response, current_app, jsonify
from hubmap_commons.exceptions import HTTPException
Expand All @@ -12,46 +9,19 @@

from lib.decorators import require_json
from lib.exceptions import ResponseException
from .rule_chain import (
from lib.rule_chain import (
NoMatchException,
RuleLoader,
RuleLogicException,
RuleSyntaxException,
initialize_rule_chain,
calculate_assay_info,
)

assayclassifier_blueprint = Blueprint("assayclassifier", __name__)

logger: logging.Logger = logging.getLogger(__name__)


rule_chain = None


def initialize_rule_chain():
global rule_chain
rule_src_uri = current_app.config["RULE_CHAIN_URI"]
try:
json_rules = urllib.request.urlopen(rule_src_uri)
except json.decoder.JSONDecodeError as excp:
raise RuleSyntaxException(excp) from excp
rule_chain = RuleLoader(json_rules).load()
print("RULE CHAIN FOLLOWS")
rule_chain.dump(stdout)
print("RULE CHAIN ABOVE")


def calculate_assay_info(metadata: dict) -> dict:
if not rule_chain:
initialize_rule_chain()
for key, value in metadata.items():
if type(value) is str:
if value.isdigit():
metadata[key] = int(value)
rslt = rule_chain.apply(metadata)
# TODO: check that rslt has the expected parts
return rslt


@assayclassifier_blueprint.route("/assaytype/<ds_uuid>", methods=["GET"])
def get_ds_assaytype(ds_uuid: str):
try:
Expand Down
44 changes: 44 additions & 0 deletions src/routes/vitessce/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import logging

from flask import Blueprint, Response, current_app, jsonify, request
from hubmap_commons.hm_auth import AuthHelper
from portal_visualization.builder_factory import get_view_config_builder

from lib.decorators import require_json
from lib.rule_chain import calculate_assay_info

vitessce_blueprint = Blueprint("vitessce", __name__)
logger = logging.getLogger(__name__)


@vitessce_blueprint.route("/vitessce/config", methods=["POST"])
@require_json(param="entity")
def vitessce_config(entity: dict):
def get_assaytype(entity):
if "metadata" in entity.get("metadata", {}):
metadata = entity["metadata"]["metadata"]
else:
if "data_types" in entity and entity.get("data_types"):
metadata = {
"entity_type": entity.get("entity_type"),
"data_types": entity.get("data_types"),
}
else:
metadata = {
"entity_type": entity.get("entity_type"),
"data_types": [entity.get("dataset_type")],
}
return calculate_assay_info(metadata)

try:
auth_helper_instance = AuthHelper.instance()
groups_token = auth_helper_instance.getAuthorizationTokens(request.headers)
BuilderCls = get_view_config_builder(entity, get_assaytype)
builder = BuilderCls(
entity, groups_token, current_app.config["ASSETS_WEBSERVICE_URL"]
)
vitessce_conf = builder.get_conf_cells(marker=None)
return jsonify(vitessce_conf[0] or {}), 200
except Exception as e:
logger.error(e)
return Response("Unexpected error while retrieving Vitessce config", 500)

0 comments on commit 6821136

Please sign in to comment.