Skip to content

Commit

Permalink
To close #169 by adding endpoint 'my_hpos' (#379)
Browse files Browse the repository at this point in the history
* More tests for variants

* Add test for enabling a non-existing user

* Add my_hpos endpoint #169
  • Loading branch information
alanwilter authored Nov 11, 2021
1 parent 3262561 commit 8848eff
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
12 changes: 12 additions & 0 deletions tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,21 @@ def test_my_variants(_demo_client):
resp = _demo_client.get("/my_variants?limit=10000")
assert len(resp.json) == 4099
assert "'variant_id': [{'display': '14-95236097-C-A'" in str(resp.json)
resp = _demo_client.get("/my_variants?limit=100001")
assert resp.status_code == 400
assert resp.json == {"message": "The maximum page size for variants is 100000"}


def test_my_genes(_demo_client):
resp = _demo_client.get("/my_genes")
assert len(resp.json) == 3
assert "'percentage_gene_gc_content': 49.73" in str(resp.json)


def test_my_hpos(_demo_client):
resp = _demo_client.get("/my_hpos")
assert len(resp.json) == 7
assert "'Abnormal retinal morphology'" in str(resp.json)
resp = _demo_client.get("/my_hpos?limit=100001")
assert resp.status_code == 400
assert resp.json == {"message": "The maximum page size for variants is 100000"}
3 changes: 3 additions & 0 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def test_enable_user(_admin):
assert status == 400
assert not response.json.get("success")
assert response.json.get("message") == "Cannot change the status of Admin user!"
response, status = enable_user("abcdefxyz", "True")
assert status == 404
assert response.json.get("message") == "User not found"


def test_bad_attempt_to_disable_user(_admin):
Expand Down
7 changes: 6 additions & 1 deletion tests/test_variants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from io import StringIO
from views.variant import _get_genotypes # noqa: F401
from views.variant import variant, variant_preview
from views.variant import variant, variant_preview, _get_variants


def test_get_genotypes_exception():
Expand Down Expand Up @@ -67,3 +67,8 @@ def test_variant_preview(_demo):
def test_wrong_variant_preview(_demo):
response = variant_preview("something-else")
assert response.status_code == 400


def test_get_variants(_demo):
response = _get_variants("wrong")
assert not response
36 changes: 34 additions & 2 deletions views/hpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"""
from flask import session, jsonify
from psycopg2 import sql
from views import application
from views import MAX_PAGE_SIZE, application
from views.auth import requires_auth, USER
from views.exceptions import PhenopolisException
from views.individual import _get_authorized_individuals
from views.postgres import get_db, session_scope
from views.general import process_for_display, cache_on_browser
from views.general import _get_pagination_parameters, process_for_display, cache_on_browser
from db.helpers import cursor2dict, query_user_config


Expand Down Expand Up @@ -174,3 +176,33 @@ def _preview(cur, user, hpo_id):
q = q1 + q2
cur.execute(q, [user])
return cur.rowcount


@application.route("/my_hpos")
@requires_auth
def get_all_hpos():
with session_scope() as db_session:
individuals = _get_authorized_individuals(db_session)
sqlq_all_hpos = sql.SQL(
"""
select distinct t.hpo_id, t."name" from phenopolis.individual_feature ife
join hpo.term t on t.id = ife.feature_id
where ife.individual_id = any(%s) and ife.type in ('observed')
"""
)
try:
limit, offset = _get_pagination_parameters()
if limit > MAX_PAGE_SIZE:
return (
jsonify(message="The maximum page size for variants is {}".format(MAX_PAGE_SIZE)),
400,
)
sqlq = sqlq_all_hpos + sql.SQL("limit {} offset {}".format(limit, offset))
with get_db() as conn:
with conn.cursor() as cur:
cur.execute(sqlq, [[x.id for x in individuals]])
hpos = cursor2dict(cur)
process_for_display(db_session, hpos)
except PhenopolisException as e:
return jsonify(success=False, message=str(e)), e.http_status
return jsonify(hpos), 200
2 changes: 1 addition & 1 deletion views/variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _get_variants(target: str):
* variant_id (e.g '14-76156575-A-G') - and it will return the variant(s) dict for it
'12-7241974-C-T', e.g., returns 2 dicts because of 'phenopolis.individual_variant'
* gene_id (e.g. 'ENSG00000144285') - and it will return all variants linked to that gene
* phenopolis_id (e.g. 'PH00008256') - aand it will return all variants linked to that patient
* phenopolis_id (e.g. 'PH00008256') - and it will return all variants linked to that patient
input 'target' must obey its respective string format.
Returns:
List[dict variant]: empty ([]), one or more variants depending on input target
Expand Down

0 comments on commit 8848eff

Please sign in to comment.