Skip to content

Commit

Permalink
Show number of documents and date of last update on genes page (#3323)
Browse files Browse the repository at this point in the history
* Genes last updated

* Date of last genes collection update on genes page

* Add also nr of docs

* Fix typo

* Codefactor and linting fixes

* Show ngenes by build

* Update scout/server/blueprints/genes/templates/genes/genes.html

Co-authored-by: Daniel Nilsson <[email protected]>

Co-authored-by: Daniel Nilsson <[email protected]>
  • Loading branch information
northwestwitch and dnil authored May 6, 2022
1 parent b0ef15f commit 06f10bc
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ About changelog [here](https://keepachangelog.com/en/1.0.0/)
- Script to convert old OMIM case format (list of integers) to new format (list of dictionaries)
- Additional check for user logged in status before serving alignment files
- Download .cgh files from cancer samples table on cancer case page
- Number of documents and date of last update on genes page
### Changed
- Verify user before redirecting to IGV alignments and sashimi plots
- Build case IGV tracks starting from case and variant objects instead of passing all params in a form
Expand Down
28 changes: 27 additions & 1 deletion scout/server/blueprints/genes/controllers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
# -*- coding: utf-8 -*-
from pprint import pprint as pp
from flask import flash, redirect, url_for

from scout.server.links import add_gene_links, add_tx_links, omim
from scout.server.utils import document_generated


def genes(store, query):
"""Creates content for the genes template
Args:
store (scout.adapter.MongoAdapter)
query (string)
Returns:
data (dict)
"""
hgnc_id = None
if "|" in query:
try:
hgnc_id = int(query.split(" | ", 1)[0])
except ValueError:
flash("Provided gene info could not be parsed!", "warning")
if hgnc_id:
return redirect(url_for(".gene", hgnc_id=hgnc_id))

nr_genes_37 = store.nr_genes(build="37")
nr_genes_38 = store.nr_genes(build="38")
genes_subset = list(store.all_genes(add_transcripts=False, limit=20))
last_updated = document_generated(genes_subset[0]["_id"] if genes_subset else None)
return dict(genes=genes_subset, last_updated=last_updated, nr_genes=(nr_genes_37, nr_genes_38))


def gene(store, hgnc_id):
Expand Down
4 changes: 3 additions & 1 deletion scout/server/blueprints/genes/templates/genes/genes.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

<div class="container-float">
<div class="card panel-default w-100 mt-3 justify-content-center">
<div class="panel-heading">Genes</div>
<div class="panel-heading">Genes
<span style="float:right;"> {{nr_genes[0]}} GRCh37 genes | {{nr_genes[1]}} GRCh38 genes | Last updated {{last_updated.strftime('%d-%m-%Y %H:%M:%S')}}</span>
</div>
<table class="table table-bordered">
<thead>
<th>Gene symbol</th>
Expand Down
12 changes: 2 additions & 10 deletions scout/server/blueprints/genes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,8 @@
def genes():
"""Render seach box for genes."""
query = request.args.get("query", "")
hgnc_id = None
if "|" in query:
try:
hgnc_id = int(query.split(" | ", 1)[0])
except ValueError:
flash("Provided gene info could not be parsed!", "warning")
if hgnc_id:
return redirect(url_for(".gene", hgnc_id=hgnc_id))
gene_q = store.all_genes(limit=20)
return dict(genes=gene_q)
data = controllers.genes(store, query)
return data


@genes_bp.route("/genes/<int:hgnc_id>")
Expand Down
14 changes: 14 additions & 0 deletions scout/server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
LOG = logging.getLogger(__name__)


def document_generated(document_id):
"""Returns the generation time of a certain MongodDB document
Args:
document_id (bson.objectid.ObjectId)
Returns:
document_id.generation_time (datetime.datetime) or None if document_id is not of type ObjectId
"""
if isinstance(document_id, ObjectId):
return document_id.generation_time
LOG.error(f"Could not retrieve generation date for Object {document_id}")


def html_to_pdf_file(
html_string, orientation, dpi=96, margins=["1.5cm", "1cm", "1cm", "1cm"], zoom=1
):
Expand Down
21 changes: 21 additions & 0 deletions tests/server/test_server_utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
"""Tests for server utils"""
import tempfile
from datetime import datetime
from io import BytesIO

import pytest
from bson.objectid import ObjectId
from flask import url_for

from scout.server.links import get_variant_links
from scout.server.utils import (
append_safe,
case_has_alignments,
case_has_mt_alignments,
document_generated,
find_index,
html_to_pdf_file,
variant_case,
)


def test_objectid_generated_valid_objid():
"""Test the function that returns the timestamp of a certain Mongo ObjectId for a document"""
# GIVEN a database document ObjectId
objid = ObjectId("6270e450615e1675f40b5ce4")

# THEN document_generated should return a timestamp
assert isinstance(document_generated(objid), datetime)


def test_objectid_generated_none():
"""Test the function that returns the timestamp of a MondoDB ObjectId when the ObjectId has wrong type"""

# GIVEN an id of different type than ObjectId
objid = None
# THEN document_generated should return None
assert document_generated(objid) is None


def test_case_has_alignments(case_obj):
"""Test function that adds info on availability of autosomal alignment files for a case"""

Expand Down

0 comments on commit 06f10bc

Please sign in to comment.