Skip to content

Commit

Permalink
Merge pull request #82 from Clinical-Genomics-Lund/sample-page-pagein…
Browse files Browse the repository at this point in the history
…ation

Sample page pageination
  • Loading branch information
Markus Johansson authored Jun 30, 2021
2 parents 95c0ab0 + 4bcb1d9 commit 23fe91c
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ About changelog [here](https://keepachangelog.com/en/1.0.0/)
- Added load sample command for loading samples into gens database
- Added index command for creating new indexes
- Added view sample command for displaying sample information
- Added pagination samples table
### Changed
- Changed development status from Alpha to Stable
- Samples are loaded based on the paths given when uploading the sample
Expand Down
60 changes: 60 additions & 0 deletions assets/css/home.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@import "defaults", "navbar";

$table-border-color: #ddd;
$selected-color: adjust-color($menubar-bg-color, $lightness: +30%);


.align-right { text-align: right; }
Expand All @@ -11,6 +12,10 @@ $table-border-color: #ddd;
justify-content: center;
align-items: center;

.display-info {
margin-bottom: 4px;
}

table {
border-collapse: collapse;
tr {
Expand All @@ -34,7 +39,62 @@ $table-border-color: #ddd;
// other styling
}
}
}

.pagination {
float: right;
margin-top: 10px;
padding-bottom: 50px;


ul {
margin: 0;
padding: 0;
list-style: none;
}

li:hover { background-color: $table-border-color;}

li {
float: left;
border-style: solid solid solid hidden;
border-width: 1px;
border-color: grey;
}

li:first-child {
border-style: solid;
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
}

li:last-child {
border-style: solid solid solid hidden;
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
}

a {
display: block;
padding: 8px;
text-decoration: none;
color: $default-font-color;
}
}

.disabled {
pointer-events: none;
a {
color: dimgray;
}
}

.selected {
background-color: $selected-color;

a {
color: $menubar-font-color;
}
}

// icons
Expand Down
14 changes: 9 additions & 5 deletions gens/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
import connexion
from flask import abort, current_app, jsonify, request

from gens.db import (ANNOTATIONS_COLLECTION, TRANSCRIPTS_COLLECTION,
VariantCategory, query_records_in_region, query_sample,
query_variants)
from gens.db import (
ANNOTATIONS_COLLECTION,
TRANSCRIPTS_COLLECTION,
VariantCategory,
query_records_in_region,
query_sample,
query_variants,
)
from gens.exceptions import RegionParserException
from gens.graph import (REQUEST, get_cov, overview_chrom_dimensions,
parse_region_str)
from gens.graph import REQUEST, get_cov, overview_chrom_dimensions, parse_region_str

from .constants import CHROMOSOMES, GENOME_BUILDS
from .io import get_tabix_files
Expand Down
29 changes: 27 additions & 2 deletions gens/blueprints/home/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
<h1>Samples</h1>
<p>There are {{ samples | length }} samles loaded into Gens. Click on a <em>sample_id</em> to open it</p>
<div>
{{ samples_table(samples) }}
{{ samples_table(samples, pagination) }}
</div>
</div>
{% endblock content %}

{% macro samples_table(samples_obj)%}
{% macro samples_table(samples_obj, pagination)%}
{% set per_page = samples_obj | length %}
<p class="display-info">Displaying <em>{{ pagination["from"] }}</em> to <em>{{ pagination["to"] }}</em> of <em>{{ per_page * pagination.last_page }}</em> samples</p>
<table>
<thead>
<tr>
Expand Down Expand Up @@ -64,4 +66,27 @@ <h1>Samples</h1>
{% endfor %}
</tbody>
</table>
{{ pagination_bar(pagination) }}
{% endmacro %}

{% macro pagination_bar(pagination)%}
<div class="pagination">
<ul>
{% set start = 1 if pagination["current_page"] == 1 else pagination["current_page"] - 1 %}
<!-- next -->
<li class="page-item {{ 'disabled' if pagination["current_page"] == 1 else '' }}">
<a href="{{url_for('home.home', page=pagination["current_page"] - 1) }}">Previous</a>
</li>
<!-- create numbers -->
{% for num in range(start, start + 3) %}
<li class="page-item {{ 'disabled' if pagination["last_page"] < num else '' }} {{ 'selected' if pagination["current_page"] == num else '' }}">
<a href="{{url_for('home.home', page=num) }}">{{ num }}</a>
</li>
{% endfor %}
<!-- previous -->
<li class="page-item {{ 'disabled' if pagination["current_page"] == pagination["last_page"] else '' }}">
<a href="{{url_for('home.home', page=pagination["current_page"] + 1) }}">Next</a>
</li>
</ul>
</div>
{% endmacro %}
28 changes: 23 additions & 5 deletions gens/blueprints/home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import os
from itertools import groupby

from flask import Blueprint, current_app, render_template
from flask import Blueprint, current_app, render_template, request

from gens import version
from gens.db import get_samples, get_timestamps

LOG = logging.getLogger(__name__)

SAMPLES_PER_PAGE = 20

IN_CONFIG = (
"ENV",
"DEFAULT_ANNOTATION_TRACK",
Expand All @@ -30,11 +32,26 @@


# define views
@home_bp.route("/", methods=["GET"])
@home_bp.route("/home", methods=["GET"])
@home_bp.route("/", methods=["GET", "POST"])
@home_bp.route("/home", methods=["GET", "POST"])
def home():
db = current_app.config["GENS_DB"]

# set pagination
page = request.args.get("page", 1, type=int)
start = (page - 1) * SAMPLES_PER_PAGE
samples, tot_samples = get_samples(db, start=start, n_samples=SAMPLES_PER_PAGE)
# calculate pagination
pagination_info = {
"from": start + 1,
"to": start + SAMPLES_PER_PAGE,
"current_page": page,
"last_page": (
tot_samples // SAMPLES_PER_PAGE
if tot_samples % SAMPLES_PER_PAGE == 0
else (tot_samples // SAMPLES_PER_PAGE) + 1
),
}
# parse samples
samples = [
{
"sample_id": smp.sample_id,
Expand All @@ -44,11 +61,12 @@ def home():
and os.path.isfile(smp.coverage_file),
"created_at": smp.created_at.strftime("%Y-%m-%d"),
}
for smp in get_samples(db)
for smp in samples
]
return render_template(
"home.html",
samples=samples,
pagination=pagination_info,
version=version,
)

Expand Down
24 changes: 18 additions & 6 deletions gens/commands/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@
from pymongo import ASCENDING

from gens.constants import GENOME_BUILDS
from gens.db import (ANNOTATIONS_COLLECTION, CHROMSIZES_COLLECTION,
SAMPLES_COLLECTION, TRANSCRIPTS_COLLECTION, create_index,
get_indexes, register_data_update, store_sample)
from gens.load import (ParserError, build_transcripts, parse_annotation_entry,
parse_annotation_file, parse_chrom_sizes,
update_height_order)
from gens.db import (
ANNOTATIONS_COLLECTION,
CHROMSIZES_COLLECTION,
SAMPLES_COLLECTION,
TRANSCRIPTS_COLLECTION,
create_index,
get_indexes,
register_data_update,
store_sample,
)
from gens.load import (
ParserError,
build_transcripts,
parse_annotation_entry,
parse_annotation_file,
parse_chrom_sizes,
update_height_order,
)

LOG = logging.getLogger(__name__)
valid_genome_builds = [str(gb) for gb in GENOME_BUILDS]
Expand Down
2 changes: 1 addition & 1 deletion gens/commands/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def samples(summary):
"""View samples stored in the database"""
db = app.config["GENS_DB"]
# print samples to terminal
samples = get_samples(db)
samples, _ = get_samples(db)
if summary: # count number of samples per genome build
columns = ("Genome build", "N samples")
sample_tbl = (
Expand Down
10 changes: 7 additions & 3 deletions gens/db/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from .annotation import ANNOTATIONS as ANNOTATIONS_COLLECTION
from .annotation import TRANSCRIPTS as TRANSCRIPTS_COLLECTION
from .annotation import (VariantCategory, get_timestamps,
query_records_in_region, query_variants,
register_data_update)
from .annotation import (
VariantCategory,
get_timestamps,
query_records_in_region,
query_variants,
register_data_update,
)
from .chrom_sizes import CHROMSIZES as CHROMSIZES_COLLECTION
from .chrom_sizes import get_chromosome_size
from .db import init_database_connection as init_database
Expand Down
6 changes: 3 additions & 3 deletions gens/db/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def get_samples(db, start=0, n_samples=None):
for r in db[COLLECTION].find().sort("created_at", ASCENDING)
)
# limit results to n results
if n_samples and 0 < n_samples:
results = itertools.islice(results, start=start, stop=start + n_samples)
return results
if isinstance(n_samples, (int)) and 0 < n_samples:
results = itertools.islice(results, start, start + n_samples)
return results, db[COLLECTION].count_documents({})


def query_sample(db, sample_id, genome_build):
Expand Down
8 changes: 6 additions & 2 deletions gens/load/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from .annotations import (ParserError, parse_annotation_entry,
parse_annotation_file, update_height_order)
from .annotations import (
ParserError,
parse_annotation_entry,
parse_annotation_file,
update_height_order,
)
from .chrom_sizes import parse_chrom_sizes
from .transcripts import build_transcripts

0 comments on commit 23fe91c

Please sign in to comment.