From 98f2fc92a15227373ab1f60e36f897f6a6c43822 Mon Sep 17 00:00:00 2001 From: sepro Date: Tue, 19 Sep 2017 16:46:59 +0200 Subject: [PATCH] Added export function for profiles --- planet/controllers/profile_comparison.py | 9 ++++-- planet/helpers/chartjs.py | 32 +++++++++++++++++++ .../expression_profile_comparison.html | 6 ++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/planet/controllers/profile_comparison.py b/planet/controllers/profile_comparison.py index d1a6160..34e8e49 100644 --- a/planet/controllers/profile_comparison.py +++ b/planet/controllers/profile_comparison.py @@ -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 @@ -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() diff --git a/planet/helpers/chartjs.py b/planet/helpers/chartjs.py index ed4cd1e..68c5ffb 100644 --- a/planet/helpers/chartjs.py +++ b/planet/helpers/chartjs.py @@ -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 diff --git a/planet/templates/expression_profile_comparison.html b/planet/templates/expression_profile_comparison.html index 936d5a3..21e5449 100644 --- a/planet/templates/expression_profile_comparison.html +++ b/planet/templates/expression_profile_comparison.html @@ -41,6 +41,12 @@

Profile comparison

+ {% if data %} +
+ Download +
+
+ {% endif %} {% else %}