<commit_before>import logging
import re

from flask import Blueprint, current_app, flash, jsonify, redirect, request, url_for
from flask_login import current_user
from markupsafe import Markup

from scout.constants import ACMG_CRITERIA, ACMG_MAP
from scout.server.blueprints.variant.controllers import build_clinvar_submission, clinvar_export
from scout.server.blueprints.variant.controllers import evaluation as evaluation_controller
from scout.server.blueprints.variant.controllers import observations, str_variant_reviewer
from scout.server.blueprints.variant.controllers import variant as variant_controller
from scout.server.blueprints.variant.controllers import variant_acmg as acmg_controller
from scout.server.blueprints.variant.controllers import variant_acmg_post
from scout.server.blueprints.variant.verification_controllers import (
    MissingVerificationRecipientError,
    variant_verification,
)
from scout.server.extensions import loqusdb, store
from scout.server.utils import institute_and_case, public_endpoint, templated
from scout.utils.acmg import get_acmg
from scout.utils.ensembl_rest_clients import EnsemblRestApiClient

LOG = logging.getLogger(__name__)

variant_bp = Blueprint(
    "variant",
    __name__,
    template_folder="templates",
    static_folder="static",
)


@variant_bp.route("/update_tracks", methods=["POST"])
def update_tracks_settings():
    """Update custom track settings for a user according to form choices"""
    user_obj = store.user(email=current_user.email)
    selected_tracks = request.form.getlist("user_tracks") or []
    # update user in database with custom tracks info
    user_obj["igv_tracks"] = selected_tracks
    store.update_user(user_obj)
    return redirect(request.referrer)


@variant_bp.route("/<institute_id>/<case_name>/<variant_id>")
@templated("variant/variant.html")
def variant(institute_id, case_name, variant_id):
    """Display a specific SNV variant."""
    LOG.debug("Variants view requesting data for variant %s", variant_id)

    data = variant_controller(store, institute_id, case_name, variant_id=variant_id)
    if data is None:
        flash("An error occurred while retrieving variant object", "danger")
        return redirect(
            url_for("variants.variants", institute_id=institute_id, case_name=case_name)
        )

    if current_app.config.get("LOQUSDB_SETTINGS"):
        LOG.debug("Fetching loqusdb information for %s", variant_id)
        data["observations"] = observations(store, loqusdb, data["case"], data["variant"])

    return data


@variant_bp.route("/<institute_id>/<case_name>/cancer/<variant_id>")
@templated("variant/cancer-variant.html")
def cancer_variant(institute_id, case_name, variant_id):
    """Display a specific SNV variant."""
    LOG.debug("Variants view requesting data for variant %s", variant_id)

    data = variant_controller(store, institute_id, case_name, variant_id=variant_id)
    if data is None:
        flash("An error occurred while retrieving variant object", "danger")
        return redirect(
            url_for(
                "variants.cancer_variants",
                institute_id=institute_id,
                case_name=case_name,
            )
        )

    if current_app.config.get("LOQUSDB_SETTINGS"):
        LOG.debug("Fetching loqusdb information for %s", variant_id)
        data["observations"] = observations(store, loqusdb, data["case"], data["variant"])

    return data


@variant_bp.route("/<institute_id>/<case_name>/sv/variants/<variant_id>")
@templated("variant/sv-variant.html")
def sv_variant(institute_id, case_name, variant_id):
    """Display a specific structural variant."""
    data = variant_controller(store, institute_id, case_name, variant_id, add_other=False)

    if data is None:
        flash("An error occurred while retrieving variant object", "danger")
        return redirect(
            url_for("variants.sv_variants", institute_id=institute_id, case_name=case_name)
        )

    if current_app.config.get("LOQUSDB_SETTINGS"):
        LOG.debug("Fetching loqusdb information for %s", variant_id)
        data["observations"] = observations(store, loqusdb, data["case"], data["variant"])

    return data


@variant_bp.route("/<institute_id>/<case_name>/str/variant/<variant_id>", methods=["GET"])
@templated("variant/str-variant-reviewer.html")
def reviewer_aln(institute_id, case_name, variant_id):
    """Display STR variant alignment using the REViewer service."""
    variant_type = request.args.get("variant_type", "clinical")

    institute_obj, case_obj = institute_and_case(store, institute_id, case_name)
    data = str_variant_reviewer(store, case_obj, variant_id)

    return dict(
        institute=institute_obj,
        case=case_obj,
        variant_type=variant_type,
        format="html",
        **data,
    )


@variant_bp.route("/<institute_id>/<case_name>/<variant_id>/acmg", methods=["GET", "POST"])
@templated("variant/acmg.html")
def variant_acmg(institute_id, case_name, variant_id):
    """ACMG classification form."""
    if request.method == "GET":
        data = acmg_controller(store, institute_id, case_name, variant_id)
        return data

    criteria = []
    criteria_terms = request.form.getlist("criteria")
    for term in criteria_terms:<commit_msg>reverting...<commit_after>import logging
import re

from flask import Blueprint, current_app, flash, jsonify, redirect, request, url_for
from flask_login import current_user
from markupsafe import Markup

from scout.constants import ACMG_CRITERIA, ACMG_MAP
from scout.server.blueprints.variant.controllers import build_clinvar_submission, clinvar_export
from scout.server.blueprints.variant.controllers import evaluation as evaluation_controller
from scout.server.blueprints.variant.controllers import observations, str_variant_reviewer
from scout.server.blueprints.variant.controllers import variant as variant_controller
from scout.server.blueprints.variant.controllers import variant_acmg as acmg_controller
from scout.server.blueprints.variant.controllers import variant_acmg_post
from scout.server.blueprints.variant.verification_controllers import (
    MissingVerificationRecipientError,
    variant_verification,
)
from scout.server.extensions import loqusdb, store
from scout.server.utils import institute_and_case, public_endpoint, templated
from scout.utils.acmg import get_acmg
from scout.utils.ensembl_rest_clients import EnsemblRestApiClient

LOG = logging.getLogger(__name__)

variant_bp = Blueprint(
    "variant",
    __name__,
    template_folder="templates",
    static_folder="static",
)


@variant_bp.route("/update_tracks", methods=["POST"])
def update_tracks_settings():
    """Update custom track settings for a user according to form choices"""
    user_obj = store.user(email=current_user.email)
    selected_tracks = request.form.getlist("user_tracks") or []
    # update user in database with custom tracks info
    user_obj["igv_tracks"] = selected_tracks
    store.update_user(user_obj)
    return redirect(request.referrer)


@variant_bp.route("/<institute_id>/<case_name>/<variant_id>")
@templated("variant/variant.html")
def variant(institute_id, case_name, variant_id):
    """Display a specific SNV variant."""
    LOG.debug("Variants view requesting data for variant %s", variant_id)

    data = variant_controller(store, institute_id, case_name, variant_id=variant_id)
    if data is None:
        flash("An error occurred while retrieving variant object", "danger")
        return redirect(
            url_for("variants.variants", institute_id=institute_id, case_name=case_name)
        )

    if current_app.config.get("LOQUSDB_SETTINGS"):
        LOG.debug("Fetching loqusdb information for %s", variant_id)
        data["observations"] = observations(store, loqusdb, data["case"], data["variant"])

    return data


@variant_bp.route("/<institute_id>/<case_name>/cancer/<variant_id>")
@templated("variant/cancer-variant.html")
def cancer_variant(institute_id, case_name, variant_id):
    """Display a specific SNV variant."""
    LOG.debug("Variants view requesting data for variant %s", variant_id)

    data = variant_controller(store, institute_id, case_name, variant_id=variant_id)
    if data is None:
        flash("An error occurred while retrieving variant object", "danger")
        return redirect(
            url_for(
                "variants.cancer_variants",
                institute_id=institute_id,
                case_name=case_name,
            )
        )

    if current_app.config.get("LOQUSDB_SETTINGS"):
        LOG.debug("Fetching loqusdb information for %s", variant_id)
        data["observations"] = observations(store, loqusdb, data["case"], data["variant"])

    return data


@variant_bp.route("/<institute_id>/<case_name>/sv/variants/<variant_id>")
@templated("variant/sv-variant.html")
def sv_variant(institute_id, case_name, variant_id):
    """Display a specific structural variant."""
    data = variant_controller(store, institute_id, case_name, variant_id, add_other=False)

    if data is None:
        flash("An error occurred while retrieving variant object", "danger")
        return redirect(
            url_for("variants.sv_variants", institute_id=institute_id, case_name=case_name)
        )

    if current_app.config.get("LOQUSDB_SETTINGS"):
        LOG.debug("Fetching loqusdb information for %s", variant_id)
        data["observations"] = observations(store, loqusdb, data["case"], data["variant"])

    return data


@variant_bp.route("/<institute_id>/<case_name>/str/variant/<variant_id>", methods=["GET"])
@templated("variant/str-variant-reviewer.html")
def reviewer_aln(institute_id, case_name, variant_id):
    """Display STR variant alignment using the REViewer service."""
    if request.args.get("variant_type", "clinical") == "research":
        variant_type = "research"
    else:
        variant_type = "clinical"

    institute_obj, case_obj = institute_and_case(store, institute_id, case_name)
    data = str_variant_reviewer(store, case_obj, variant_id)

    return dict(
        institute=institute_obj,
        case=case_obj,
        variant_type=variant_type,
        format="html",
        **data,
    )


@variant_bp.route("/<institute_id>/<case_name>/<variant_id>/acmg", methods=["GET", "POST"])
@templated("variant/acmg.html")
def variant_acmg(institute_id, case_name, variant_id):
    """ACMG classification form."""
    if request.method == "GET":
        data = acmg_controller(store, institute_id, case_name, variant_id)
        return data

    criteria = []
    criteria_terms = request.form.getlist("criteria")
    for term in criteria_terms:
{'fraction_non_alphanumeric': 0.1079609463011641, 'fraction_numerical': 0.0, 'mean_word_length': 4.513975155279503, 'pattern_counts': {'":': 3, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2221159', 'n_tokens_mistral': 3261, 'n_tokens_neox': 3153, 'n_words': 701}