Example #1
0
def test_phenomodel_POST_remove_subpanel_checkbox(app, user_obj, institute_obj):
    """Test removing a single checkbox from a phenotype model subpanel"""

    # GIVEN an institute with a phenotype model
    store.create_phenomodel(institute_obj["internal_id"], "Test model", "Model description")
    model_obj = store.phenomodel_collection.find_one()
    # containing a subpanel with a checkbox
    TEST_SUBPANEL["checkboxes"] = {"HP:000001": {"name": "HP:000001"}}
    model_obj["subpanels"] = {"subpanel_x": TEST_SUBPANEL}
    store.update_phenomodel(model_obj["_id"], model_obj)

    # GIVEN an initialized app
    # GIVEN a valid user and institute
    with app.test_client() as client:
        resp = client.get(url_for("auto_login"))

        # WHEN the user removes the checkbox using the endpoint, POST request
        form_data = dict(checkgroup_remove="#".join(["HP:000001", "subpanel_x"]))
        resp = client.post(
            url_for(
                "overview.checkbox_edit",
                institute_id=institute_obj["internal_id"],
                model_id=model_obj["_id"],
            ),
            data=form_data,
        )
    # THEN the checkbox should be removed from the subpanel
    updated_model = store.phenomodel_collection.find_one()
    assert updated_model["subpanels"]["subpanel_x"]["checkboxes"] == {}
Example #2
0
def update_phenomodel(model_id, user_form):
    """Update a phenotype model according to the user form

    Args:
        model_id(ObjectId): document ID of the model to be updated
        user_form(request.form): a POST request form object
    """
    model_dict = store.phenomodel(model_id)
    update_model = False
    if model_dict is None:
        return
    if user_form.get(
            "update_model"):  # update either model name of description
        update_model = True
        model_dict["name"] = user_form.get("model_name")
        model_dict["description"] = user_form.get("model_desc")
    if user_form.get(
            "subpanel_delete"):  # Remove a phenotype submodel from phenomodel
        subpanels = model_dict["subpanels"]
        # remove panel from subpanels dictionary
        subpanels.pop(user_form.get("subpanel_delete"), None)
        model_dict["subpanels"] = subpanels
        update_model = True
    if user_form.get("add_subpanel"):  # Add a new phenotype submodel
        update_model = _add_subpanel(model_id, model_dict, user_form)
    if user_form.get("model_save"
                     ):  # Save model according user preferences in the preview
        update_model = phenomodel_checkgroups_filter(model_dict, user_form)

    if update_model:
        store.update_phenomodel(model_id=model_id, model_obj=model_dict)
Example #3
0
def test_phenomodel_unlock(app, user_obj, institute_obj, mocker,
                           mock_redirect):
    """Test the endpoint to unlock a phenomodel and make it editable only by all users"""

    mocker.patch("scout.server.blueprints.institutes.views.redirect",
                 return_value=mock_redirect)

    # GIVEN an institute with phenotype model
    store.create_phenomodel(institute_obj["internal_id"], "Test model",
                            "Model description")
    model = store.phenomodel_collection.find_one()

    # GIVEN an initialized app
    with app.test_client() as client:
        # GIVEN that the user could be logged in
        client.get(url_for("auto_login"))
        # Given that the phenomodel is locked and current user is admin
        model["admins"] = [current_user.email]
        store.update_phenomodel(model["_id"], model)
        locked_model = store.phenomodel_collection.find_one()
        assert locked_model["admins"] == [current_user.email]

        # When the test_phenomodel_lock endpoint is used to unlock the model
        form_data = dict(model_id=model["_id"], )
        resp = client.post(
            url_for("overview.lock_phenomodel"),
            data=form_data,
        )
        # Then the page should redirect
        assert resp.status_code == 302
        # And the model will have no admins
        unlocked_model = store.phenomodel_collection.find_one()
        assert unlocked_model["admins"] == []
Example #4
0
def test_phenomodel_POST_add_hpo_checkbox_to_subpanel(app, user_obj,
                                                      institute_obj,
                                                      hpo_checkboxes):
    """Test adding an HPO checkbox with its children to a subpanel of a phenotype model via POST request"""

    # GIVEN an institute with a phenotype model
    store.create_phenomodel(institute_obj["internal_id"], "Test model",
                            "Model description")
    model_obj = store.phenomodel_collection.find_one()
    # containing a subpanel
    model_obj["subpanels"] = {"subpanel_x": TEST_SUBPANEL}
    store.update_phenomodel(model_obj["_id"], model_obj)

    # GIVEN a database with the required HPO terms (one parent term and one child term)
    store.hpo_term_collection.insert_many(hpo_checkboxes)

    # GIVEN an initialized app
    # GIVEN a valid user and institute
    with app.test_client() as client:
        resp = client.get(url_for("auto_login"))

        # WHEN the user creates an HPO checkbox using the endpoint
        form_data = dict(
            hpo_subpanel_id="subpanel_x",
            hpoHasTitle="on",
            hpoTermTitle="Title for term",
            hpo_term=" | ".join(
                [hpo_checkboxes[0]["_id"], hpo_checkboxes[0]["description"]]),
            hpo_custom_name="Alternative HPO name",
            add_hpo="",
            includeChildren="on",
        )
        resp = client.post(
            url_for(
                "overview.checkbox_edit",
                institute_id=institute_obj["internal_id"],
                model_id=model_obj["_id"],
            ),
            data=form_data,
        )
        # THEN the term should have been added to the subpanel checkboxes
        updated_model = store.phenomodel_collection.find_one()
        checkbox = updated_model["subpanels"]["subpanel_x"]["checkboxes"][
            "HP:0025190"]
        assert checkbox["name"] == "HP:0025190"
        assert checkbox["checkbox_type"] == "hpo"
        assert checkbox[
            "description"] == "Bilateral tonic-clonic seizure with generalized onset"
        assert checkbox["term_title"] == form_data["hpoTermTitle"]
        assert checkbox["custom_name"] == form_data["hpo_custom_name"]
        # Additionally, the HPO term checkbox should contain a nested HPO term:
        nested_hpo_term = {
            "name": hpo_checkboxes[1]["_id"],
            "description": hpo_checkboxes[1]["description"],
        }
        assert checkbox["children"] == [nested_hpo_term]
Example #5
0
def test_phenomodel_POST_add_omim_checkbox_to_subpanel(app, user_obj,
                                                       institute_obj,
                                                       omim_checkbox):
    """Test adding an OMIM checkbox to a subpanel of a phenotype model via POST request"""

    # GIVEN an institute with a phenotype model
    store.create_phenomodel(institute_obj["internal_id"], "Test model",
                            "Model description")
    model_obj = store.phenomodel_collection.find_one()
    # containing a subpanel
    model_obj["subpanels"] = {"subpanel_x": TEST_SUBPANEL}
    store.update_phenomodel(model_obj["_id"], model_obj)
    model_obj = store.phenomodel_collection.find_one()
    assert model_obj["subpanels"]["subpanel_x"]

    # GIVEN that database contains the HPO term to add to the subopanel
    store.disease_term_collection.insert_one(omim_checkbox)

    # GIVEN an initialized app
    # GIVEN a valid user and institute
    with app.test_client() as client:
        resp = client.get(url_for("auto_login"))

        # WHEN the user creates an OMIM checkbox using the endpoint
        form_data = dict(
            omim_subpanel_id="subpanel_x",
            omimHasTitle="on",
            omimTermTitle="Title for term",
            omim_term=" | ".join(
                [omim_checkbox["_id"], omim_checkbox["description"]]),
            omim_custom_name="Alternative OMIM name",
            add_omim="",
        )
        resp = client.post(
            url_for(
                "overview.checkbox_edit",
                institute_id=institute_obj["internal_id"],
                model_id=model_obj["_id"],
            ),
            data=form_data,
        )
        # THEN the term should have been added to the subpanel checkboxe
        updated_model = store.phenomodel_collection.find_one()
        checkbox = updated_model["subpanels"]["subpanel_x"]["checkboxes"][
            "OMIM:121210"]
        assert checkbox["name"] == "OMIM:121210"
        assert checkbox["checkbox_type"] == "omim"
        assert checkbox["description"] == "Febrile seizures familial 1"
        assert checkbox["term_title"] == form_data["omimTermTitle"]
        assert checkbox["custom_name"] == form_data["omim_custom_name"]
Example #6
0
def lock_phenomodel():
    """Lock or unlock a specific phenomodel for editing"""
    form = request.form
    model_id = form.get("model_id")
    phenomodel_obj = store.phenomodel(model_id)
    if phenomodel_obj is None:
        return redirect(request.referrer)

    phenomodel_obj["admins"] = []
    if (
            "lock" in form
    ):  # lock phenomodel for all users except current user and specified collaborators
        phenomodel_obj["admins"] = [current_user.email
                                    ] + form.getlist("user_admins")

    # update phenomodels admins:
    store.update_phenomodel(model_id, phenomodel_obj)
    return redirect(request.referrer)
Example #7
0
def edit_subpanel_checkbox(model_id, user_form):
    """Update checkboxes from one or more panels according to the user form
    Args:
        model_id(ObjectId): document ID of the model to be updated
        user_form(request.form): a POST request form object
    """
    model_dict = store.phenomodel(model_id)
    update_model = False
    if model_dict is None:
        return
    if "add_hpo" in user_form:  # add an HPO checkbox to subpanel
        update_model = _subpanel_hpo_checkgroup_add(model_dict, user_form)
    if "add_omim" in user_form:  # add an OMIM checkbox to subpanel
        update_model = _subpanel_omim_checkbox_add(model_dict, user_form)
    if user_form.get("checkgroup_remove"
                     ):  # remove a checkbox of any type from subpanel
        update_model = _subpanel_checkgroup_remove_one(model_dict, user_form)

    if update_model:
        store.update_phenomodel(model_id=model_id, model_obj=model_dict)