Skip to content

Commit

Permalink
Adding initial dashboard code
Browse files Browse the repository at this point in the history
  • Loading branch information
edgargaticaCU committed Feb 29, 2024
1 parent 426a1a7 commit 03b65b7
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
91 changes: 91 additions & 0 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import services
import json
import jsonpickle
import logging

config = {
"DEBUG": True,
Expand Down Expand Up @@ -66,6 +67,25 @@ def assertion_lookup(aid):
return json.dumps(one_data)


@app.route('/dashboard', strict_slashes=False)
@cache.cached(timeout=30)
def dashboard_page():
logging.info('starting')
# pmc, pmid = get_documents_counts(1)
pmc = 1159142
pmid = 2174169
logging.info('got document counts')
# assoc = {'a': 1, 'b': 2}
# asser = {'x': 100, 'y': 150}
assoc = get_association_counts()
logging.info(f'got association counts {assoc}')
asser = get_assertion_counts()
logging.info(f'got assertion counts {asser}')
return render_template('dashboard.html',
pmc_count=pmc, pmid_count=pmid,
records_dict=assoc, assertions_dict=asser)


@app.route('/evidence/<evidence_id>', strict_slashes=False)
@cache.cached(timeout=30)
def evidence_page(evidence_id):
Expand Down Expand Up @@ -309,6 +329,8 @@ def get_edge_list(assertions, use_uniprot=False):
for ev in assertion.evidence_list:
if ev.get_top_predicate() == predicate:
edge_list.append({
"assertion_id": ev.assertion_id,
"evidence_id": ev.evidence_id,
"document_pmid": ev.document_id,
"document_zone": ev.document_zone,
"document_year": ev.document_year_published,
Expand Down Expand Up @@ -386,6 +408,73 @@ def get_translated_options() -> (list, list):
return subjects, objects


def get_documents_counts(version=1) -> tuple[int, int]:
pmc_query = text("SELECT SUM(IF(docid LIKE 'PMC%', 1, 0)) AS PMC, SUM(IF(docid LIKE 'PMID%', 1, 0)) AS PMID FROM "
"(SELECT DISTINCT(document_id) as docid "
"FROM evidence JOIN evidence_version ON "
"evidence.evidence_id = evidence_version.evidence_id "
"WHERE evidence_version.version = :v) x")
# pmid_query = text("SELECT COUNT(DISTINCT(document_id)) "
# "FROM evidence JOIN evidence_version ON "
# "evidence.evidence_id = evidence_version.evidence_id "
# "WHERE evidence_version.version = :v AND document_id LIKE 'PMID%'")
s = Session()
logging.info('starting query')
row, = s.execute(pmc_query, {'v': version}).all()
pmc_count = row['PMC']
pmid_count = row['PMID']

# logging.info('starting query 2')
# pmid_count = s.execute(pmid_query, {'v': version}).scalar()
# document_query = text("SELECT DISTINCT(e.document_id) "
# "FROM evidence e INNER JOIN evidence_version ev ON e.evidence_id = ev.evidence_id "
# "WHERE version = :v")
# document_list = [doc_id for doc_id, in s.execute(document_query, {'v': version})]
logging.info('data retrieved')
# pmc_count = 0
# pmid_count = 0
# for document in document_list:
# if document.startswith('PMC'):
# pmc_count += 1
# elif document.startswith('PMID'):
# pmid_count += 1
# logging.info('counting complete')
return pmc_count, pmid_count

def get_association_counts() -> dict[str, int]:
association_query = text("select assertion.association_curie, top_evidence_scores.predicate_curie, count(1) AS c "
"from evidence "
"join evidence_version on evidence.evidence_id = evidence_version.evidence_id "
"join top_evidence_scores on evidence.evidence_id = top_evidence_scores.evidence_id "
"join assertion on evidence.assertion_id = assertion.assertion_id "
"where evidence_version.version = 1 "
"group by assertion.association_curie, top_evidence_scores.predicate_curie")
# ass_query = text("SELECT association_curie, 'related_to' AS predicate_curie, 7 AS c FROM assertion LIMIT 1000")
s = Session()
association_count_dict = {}
for row in s.execute(association_query):
key = row['association_curie'] + '|' + row['predicate_curie']
value = row['c']
association_count_dict[key] = value
return association_count_dict


def get_assertion_counts() -> dict[str, int]:
assertion_count_query = text("select assertion.association_curie, count(1) AS c "
"from assertion "
"join evidence on evidence.assertion_id = assertion.assertion_id "
"join evidence_version on evidence.evidence_id = evidence_version.evidence_id "
"where evidence_version.version = 2 "
"group by assertion.association_curie")
s = Session()
assertion_count_dict = {}
for row in s.execute(assertion_count_query):
key = row['association_curie']
value = row['c']
assertion_count_dict[key] = value
return assertion_count_dict


@app.teardown_appcontext
def shutdown_session(response_or_exc):
Session.remove()
Expand All @@ -399,6 +488,8 @@ def shutdown_session(response_or_exc):
assert secret_password
models.init_db(username=username, password=secret_password)
Session = scoped_session(models.Session)
logging.basicConfig(format='%(asctime)s %(module)s:%(funcName)s:%(levelname)s: %(message)s', level=logging.INFO)
logging.info('Starting Main')

if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
Expand Down
20 changes: 20 additions & 0 deletions templates/dashboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>{{ title }}</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
</head>
<body>
<p>Unique PMC documents that support at least one assertion: {{ pmc_count }}</p>
<p>Unique PubMed documents that support at least one assertion: {{ pmid_count }}</p>
<p>Record counts by association type: {{ records_dict }}</p>
<p>Assertion count by type: {{ assertions_dict }}</p>
</body>
</html>
6 changes: 5 additions & 1 deletion templates/freetext_search.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ <h6 class="card-subtitle mb-2 text-muted" id="queryResultsCount">The number of r
let sentenceHTML = spliceStrings(item.sentence, spliceList);
let evidenceHTML = '<a target="_blank" href="https://pubmed.ncbi.nlm.nih.gov/' + item.document_pmid.split(':')[1] + '/">'
+ item.document_pmid + '<small><i class="bi bi-box-arrow-up-right"></i></small></a> ' + item.document_year + ' ' + item.document_zone
+ ' (Confidence - ' + item.confidence_score + ') [' + item.predicate_curie + '] : <br>';
+ ' (Confidence - ' + item.confidence_score + ') [' + item.predicate_curie + ']'
+ '<a target="_blank" href="https://tmui.text-mining-kp.org/assertion/' + item.assertion_id + '">'
+ 'Assertion Feedback<small><i class="bi bi-box-arrow-up-right"></i></small></a>'
+ '<a target="_blank" href="https://tmui.text-mining-kp.org/evidence/' + item.evidence_id + '">'
+ 'Evidence Feedback<small><i class="bi bi-box-arrow-up-right"></i></small></a>: <br>';
listElement = document.createElement('li');
listElement.className = 'list-group-item';
listElement.innerHTML = evidenceHTML + sentenceHTML;
Expand Down

0 comments on commit 03b65b7

Please sign in to comment.