From 05f8cf8e500a79eecb51db07e6dc5dd96131925e Mon Sep 17 00:00:00 2001 From: zsloan Date: Thu, 10 Dec 2020 15:22:29 -0600 Subject: Added an export for the Correlation Matrix --- .../wqflask/correlation_matrix/show_corr_matrix.py | 43 +++++++++++++++++++--- wqflask/wqflask/templates/correlation_matrix.html | 17 ++++++++- wqflask/wqflask/views.py | 11 ++++++ 3 files changed, 64 insertions(+), 7 deletions(-) (limited to 'wqflask') diff --git a/wqflask/wqflask/correlation_matrix/show_corr_matrix.py b/wqflask/wqflask/correlation_matrix/show_corr_matrix.py index 0269ce68..a77877d2 100644 --- a/wqflask/wqflask/correlation_matrix/show_corr_matrix.py +++ b/wqflask/wqflask/correlation_matrix/show_corr_matrix.py @@ -20,24 +20,23 @@ import datetime import math +import random +import string + import numpy as np import scipy import rpy2.robjects as robjects -import utility.webqtlUtil # this is for parallel computing only. -import utility.logger +from rpy2.robjects.packages import importr from base import data_set +from base.webqtlConfig import GENERATED_TEXT_DIR from functools import reduce from functools import cmp_to_key -from rpy2.robjects.packages import importr - from utility import webqtlUtil from utility import helper_functions from utility import corr_result_helpers from utility.redis_tools import get_redis_conn -logger = utility.logger.getLogger(__name__) - Redis = get_redis_conn() THIRTY_DAYS = 60 * 60 * 24 * 30 @@ -135,6 +134,8 @@ class CorrelationMatrix(object): self.corr_results.append(corr_result_row) self.pca_corr_results.append(pca_corr_result_row) + self.export_filename, self.export_filepath = export_corr_matrix(self.corr_results) + self.trait_data_array = [] for trait_db in self.trait_list: this_trait = trait_db[0] @@ -232,6 +233,36 @@ class CorrelationMatrix(object): loadings_array.append(loadings_row) return loadings_array +def export_corr_matrix(corr_results): + corr_matrix_filename = "corr_matrix_" + ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(6)) + matrix_export_path = "{}{}.csv".format(GENERATED_TEXT_DIR, corr_matrix_filename) + with open(matrix_export_path, "w+") as output_file: + output_file.write("Time/Date: " + datetime.datetime.now().strftime("%x / %X") + "\n") + output_file.write("\n") + output_file.write("Correlation ") + for i, item in enumerate(corr_results[0]): + output_file.write("Trait" + str(i + 1) + ": " + str(item[0].dataset.name) + "::" + str(item[0].name) + "\t") + output_file.write("\n") + for i, row in enumerate(corr_results): + output_file.write("Trait" + str(i + 1) + ": " + str(row[0][0].dataset.name) + "::" + str(row[0][0].name) + "\t") + for item in row: + output_file.write(str(item[1]) + "\t") + output_file.write("\n") + + output_file.write("\n") + output_file.write("\n") + output_file.write("N ") + for i, item in enumerate(corr_results[0]): + output_file.write("Trait" + str(i) + ": " + str(item[0].dataset.name) + "::" + str(item[0].name) + "\t") + output_file.write("\n") + for i, row in enumerate(corr_results): + output_file.write("Trait" + str(i) + ": " + str(row[0][0].dataset.name) + "::" + str(row[0][0].name) + "\t") + for item in row: + output_file.write(str(item[2]) + "\t") + output_file.write("\n") + + return corr_matrix_filename, matrix_export_path + def zScore(trait_data_array): NN = len(trait_data_array[0]) if NN < 10: diff --git a/wqflask/wqflask/templates/correlation_matrix.html b/wqflask/wqflask/templates/correlation_matrix.html index 96ad9c35..78d7e9aa 100644 --- a/wqflask/wqflask/templates/correlation_matrix.html +++ b/wqflask/wqflask/templates/correlation_matrix.html @@ -1,10 +1,11 @@ {% extends "base.html" %} +{% block title %}Correlation Matrix{% endblock %} {% block css %} - + {% endblock %} {% block content %} @@ -63,6 +64,12 @@
+
+ + + +
+
{% if pca_works == "True" %}

PCA Traits

@@ -172,6 +179,14 @@ "paging": false, "orderClasses": true } ); + + export_corr_matrix = function() { + $('#matrix_export_form').attr('action', '/export_corr_matrix'); + return $('#matrix_export_form').submit(); + } + + $('#export').click(export_corr_matrix); + {% endblock %} diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py index 3557a62a..25563e86 100644 --- a/wqflask/wqflask/views.py +++ b/wqflask/wqflask/views.py @@ -799,6 +799,17 @@ def export_mapping_results(): return response +@app.route("/export_corr_matrix", methods = ('POST',)) +def export_corr_matrix(): + file_path = request.form.get("export_filepath") + file_name = request.form.get("export_filename") + results_csv = open(file_path, "r").read() + response = Response(results_csv, + mimetype='text/csv', + headers={"Content-Disposition":"attachment;filename=" + file_name + ".csv"}) + + return response + @app.route("/export", methods = ('POST',)) def export(): logger.info("request.form:", request.form) -- cgit v1.2.3