Skip to content

Commit

Permalink
Added export function for profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
proost committed Sep 19, 2017
1 parent f9d82ee commit 98f2fc9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
9 changes: 7 additions & 2 deletions planet/controllers/profile_comparison.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import json
import base64

from flask import Blueprint, request, render_template,flash
from sqlalchemy.orm import noload

from planet import cache
from planet.forms.profile_comparison import ProfileComparisonForm
from planet.helpers.chartjs import prepare_profiles
from planet.helpers.chartjs import prepare_profiles, prepare_profiles_download
from planet.models.expression.coexpression_clusters import CoexpressionCluster
from planet.models.expression.profiles import ExpressionProfile
from planet.models.relationships.sequence_cluster import SequenceCoexpressionClusterAssociation
Expand Down Expand Up @@ -84,10 +85,14 @@ def profile_comparison_main():
if len(profiles) > 50:
flash("To many profiles in this cluster only showing the first 50", 'warning')

# Get json object for chart
profile_chart = prepare_profiles(profiles[:50], normalize)

# Get table in base64 format for download
data = base64.encodebytes(prepare_profiles_download(profiles[:50], normalize).encode('utf-8'))

return render_template("expression_profile_comparison.html",
profiles=json.dumps(profile_chart), form=form)
profiles=json.dumps(profile_chart), form=form, data=data.decode('utf-8'))
else:
profiles = ExpressionProfile.query.filter(ExpressionProfile.sequence_id is not None).order_by(ExpressionProfile.species_id).limit(5).all()

Expand Down
32 changes: 32 additions & 0 deletions planet/helpers/chartjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,38 @@
from utils.color import __COLORS_RGBA as COLORS


def prepare_profiles_download(profiles, normalize=False):
"""
Function to convert a list of NetworkProfiles to a dict compatible with chart.js
:param profiles: list of profiles to include in the plot
:param normalize: normalize the profiles (the max value of each profile is scaled to 1)
:return dict with plot compatible with Chart.js
"""
labels = []

if len(profiles) > 0:
data = json.loads(profiles[0].profile)
labels = data['order']

# initiate output array with header
output = ['genes\t' + '\t'.join(labels)]

for count, p in enumerate(profiles):
data = json.loads(p.profile)
expression_values = [mean(data['data'][label]) for label in labels]
label = p.probe if p.sequence_id is None else p.sequence.name

if normalize:
max_expression = max(expression_values)
expression_values = [value/max_expression for value in expression_values]

output.append(label + '\t' + '\t'.join(str(e) for e in expression_values))

return '\n'.join(output)


def prepare_profiles(profiles, normalize=False):
"""
Function to convert a list of NetworkProfiles to a dict compatible with chart.js
Expand Down
6 changes: 6 additions & 0 deletions planet/templates/expression_profile_comparison.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ <h1>Profile comparison</h1>
<div id="canvas-holder" class="center-block">
<canvas id="chart-line" class="center-block" width="800" height="600"/>
</div>
{% if data %}
<div>
<a class="btn btn-primary pull-right" href="data:text/plain;base64,{{data}}" download="profile_data.txt">Download</a>
<br />
</div>
{% endif %}
{% else %}
<ol class="breadcrumb">
<li><a href="{{ url_for('main.screen') }}">Home</a></li>
Expand Down

0 comments on commit 98f2fc9

Please sign in to comment.