Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gene search form to sample page. #45

Merged
merged 1 commit into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions ExonCov/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,26 @@ def validate(self):
self.gene_id = gene.id

return valid_form


class SampleGeneForm(FlaskForm):
"""Sample Form to query a specific gene."""
gene = StringField('Gene', validators=[validators.InputRequired()])
gene_id = ''

def validate(self):
"""Extra validation to parse panel / gene selection"""
self.gene_id = ''
valid_form = True

if not FlaskForm.validate(self):
valid_form = False

if self.gene.data:
gene, error = get_gene(self.gene.data)
if error:
self.gene.errors.append(error)
else:
self.gene_id = gene.id

return valid_form
4 changes: 4 additions & 0 deletions ExonCov/static/css/exoncov.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ body {
float: left;
text-align: left;
}

.help-block-inline {
display: inline;
}
2 changes: 1 addition & 1 deletion ExonCov/templates/macros/forms.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
{{ field.label(class_='control-label') }}
{{ field(class_='form-control', size=40, **kwargs) }}
{% for error in field.errors %}
<span class="help-block">{{error}}</span>
<span class="help-block help-block-inline">{{error}}</span>
{% endfor %}
</div>
{% else %}
Expand Down
15 changes: 14 additions & 1 deletion ExonCov/templates/sample.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% from "macros/tables.html" import render_panel_measurement_td %}
{% from "macros/forms.html" import render_sample_datatable_form %}
{% from "macros/forms.html" import render_sample_datatable_form, render_inline_field %}
{% extends 'base.html' %}

{% block header %}{{ sample.name }}{% endblock %}
Expand All @@ -14,6 +14,19 @@
</dl>
</div>

<div class="page-header"><h1>Search gene</h1></div>
<div class="well">
<form method="POST" action="{{ url_for('sample', id=sample.id) }}" class="form-inline">
{{ form.csrf_token }}
{{ render_inline_field(form.gene) }}
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<button type="submit" class="btn btn-primary">Search</button>
</div>
</div>
</form>
</div>

{% if sample.custom_panels %}
<div class="page-header"><h1>Custom panels</h1></div>
<table class="table table-bordered table-hover table-condensed">
Expand Down
34 changes: 29 additions & 5 deletions ExonCov/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
from .forms import (
MeasurementTypeForm, CustomPanelForm, CustomPanelNewForm, CustomPanelValidateForm, SampleForm,
CreatePanelForm, PanelNewVersionForm, PanelEditForm, PanelVersionEditForm, SampleSetPanelGeneForm
CreatePanelForm, PanelNewVersionForm, PanelEditForm, PanelVersionEditForm, SampleSetPanelGeneForm, SampleGeneForm
)
from .utils import weighted_average

Expand Down Expand Up @@ -54,18 +54,42 @@ def samples():
return render_template('samples.html', form=sample_form, samples=samples)


@app.route('/sample/<int:id>')
@app.route('/sample/<int:id>', methods=['GET', 'POST'])
@login_required
def sample(id):
"""Sample page."""
sample = Sample.query.options(joinedload('sequencing_runs')).options(joinedload('project')).options(joinedload('custom_panels')).get_or_404(id)

# Setup gene form and validate
gene_form = SampleGeneForm()

if gene_form.validate_on_submit():
if gene_form.gene_id:
return redirect(url_for('sample_gene', sample_id=id, gene_id=gene_form.gene_id))

# Query Sample and panels
sample = (
Sample.query
.options(joinedload('sequencing_runs'))
.options(joinedload('project'))
.options(joinedload('custom_panels'))
.get_or_404(id)
)
query = (
db.session.query(PanelVersion, TranscriptMeasurement)
.filter_by(active=True)
.filter_by(validated=True)
.join(Transcript, PanelVersion.transcripts)
.join(TranscriptMeasurement)
.filter_by(sample_id=sample.id)
.order_by(PanelVersion.panel_name)
.all()
)
measurement_types = {
'measurement_mean_coverage': 'Mean coverage',
'measurement_percentage10': '>10',
'measurement_percentage15': '>15',
'measurement_percentage30': '>30'
}
query = db.session.query(PanelVersion, TranscriptMeasurement).filter_by(active=True).filter_by(validated=True).join(Transcript, PanelVersion.transcripts).join(TranscriptMeasurement).filter_by(sample_id=sample.id).order_by(PanelVersion.panel_name).all()
panels = {}

for panel, transcript_measurement in query:
Expand All @@ -84,7 +108,7 @@ def sample(id):
[panels[panel.id]['len'], transcript_measurement.len]
)
panels[panel.id]['len'] += transcript_measurement.len
return render_template('sample.html', sample=sample, panels=panels, measurement_types=measurement_types)
return render_template('sample.html', sample=sample, panels=panels, measurement_types=measurement_types, form=gene_form)
FiniDG marked this conversation as resolved.
Show resolved Hide resolved


@app.route('/sample/<int:id>/inactive_panels')
Expand Down