From fe94046b9fbd5b5d1f85920fc4263eb5c4aa1328 Mon Sep 17 00:00:00 2001 From: zsloan Date: Mon, 4 Mar 2019 10:37:46 -0600 Subject: - Added z-score transformation - Added text indicating which transformation has been applied --- wqflask/wqflask/show_trait/show_trait.py | 25 ++++++++++++-------- .../wqflask/static/new/javascript/show_trait.js | 27 ++++++++++++++++++++++ wqflask/wqflask/templates/show_trait.html | 12 +++++----- .../wqflask/templates/show_trait_edit_data.html | 2 +- .../templates/show_trait_transform_and_filter.html | 1 + 5 files changed, 51 insertions(+), 16 deletions(-) diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index 4e4c7705..932260de 100644 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -12,6 +12,7 @@ from collections import OrderedDict import redis Redis = redis.StrictRedis() +import numpy as np import scipy.stats as ss from flask import Flask, g @@ -138,6 +139,7 @@ class ShowTrait(object): self.make_sample_lists() self.qnorm_vals = quantile_normalize_vals(self.sample_groups) + self.z_scores = get_z_scores(self.sample_groups) # Todo: Add back in the ones we actually need from below, as we discover we need them hddn = OrderedDict() @@ -226,6 +228,7 @@ class ShowTrait(object): attribute_names = self.sample_groups[0].attributes, num_values = self.num_values, qnorm_values = self.qnorm_vals, + zscore_values = self.z_scores, sample_column_width = sample_column_width, temp_uuid = self.temp_uuid) self.js_data = js_data @@ -277,11 +280,15 @@ class ShowTrait(object): if self.dataset.group.species == "human": primary_sample_names += other_sample_names + if other_sample_names: + primary_header = "%s Only" % (self.dataset.group.name) + else: + primary_header = "Samples" primary_samples = SampleList(dataset = self.dataset, sample_names=primary_sample_names, this_trait=self.this_trait, sample_group_type='primary', - header="%s Only" % (self.dataset.group.name)) + header=primary_header) if other_sample_names and self.dataset.group.species != "human" and self.dataset.group.name != "CFW": parent_f1_samples = None @@ -292,13 +299,11 @@ class ShowTrait(object): if parent_f1_samples: other_sample_names = parent_f1_samples + other_sample_names - logger.debug("other_sample_names:", other_sample_names) - other_samples = SampleList(dataset=self.dataset, sample_names=other_sample_names, this_trait=self.this_trait, sample_group_type='other', - header="Non-%s" % (self.dataset.group.name)) + header="Other") self.sample_groups = (primary_samples, other_samples) else: @@ -360,17 +365,19 @@ def get_z_scores(sample_groups): except: continue - qnorm_vals = normf(trait_vals) - qnorm_vals_with_x = [] + zscores = ss.mstats.zscore(np.array(trait_vals)).tolist() + zscores_with_x = [] counter = 0 for sample in sample_type.sample_list: if sample.display_value == "x": - qnorm_vals_with_x.append("x") + zscores_with_x.append("x") else: - qnorm_vals_with_x.append(qnorm_vals[counter]) + zscores_with_x.append("%0.3f" % zscores[counter]) counter += 1 - qnorm_by_group.append(qnorm_vals_with_x) + zscore_by_group.append(zscores_with_x) + + return zscore_by_group def get_nearest_marker(this_trait, this_db): this_chr = this_trait.locus_chr diff --git a/wqflask/wqflask/static/new/javascript/show_trait.js b/wqflask/wqflask/static/new/javascript/show_trait.js index e985078b..9bbcd560 100644 --- a/wqflask/wqflask/static/new/javascript/show_trait.js +++ b/wqflask/wqflask/static/new/javascript/show_trait.js @@ -557,6 +557,7 @@ block_outliers = function() { $('#block_outliers').click(block_outliers); reset_samples_table = function() { $('input[name="transform"]').val(""); + $('span[name="transform_text"]').text("") return $('.trait_value_input').each((function(_this) { return function(_index, element) { $(element).val($(element).data('value')); @@ -623,26 +624,52 @@ qnorm_data = function() { })(this)); }; +zscore_data = function() { + return $('.edit_sample_value').each((function(_this) { + return function(_index, element) { + current_value = parseFloat($(element).data("value")) + 1; + if(isNaN(current_value)) { + return current_value + } else { + $(element).val($(element).data("zscore")); + return $(element).data("zscore"); + } + }; + })(this)); +}; + normalize_data = function() { if ($('#norm_method option:selected').val() == 'log2'){ if ($('input[name="transform"]').val() != "log2") { log_normalize_data() $('input[name="transform"]').val("log2") + $('span[name="transform_text"]').text(" - log2 transformed") } } else if ($('#norm_method option:selected').val() == 'sqrt'){ if ($('input[name="transform"]').val() != "sqrt") { sqrt_normalize_data() $('input[name="transform"]').val("sqrt") + $('span[name="transform_text"]').text(" - Square Root transformed") } } else if ($('#norm_method option:selected').val() == 'invert'){ invert_data() + $('input[name="transform"]').val("inverted") + $('span[name="transform_text"]').text(" - Inverted") } else if ($('#norm_method option:selected').val() == 'qnorm'){ if ($('input[name="transform"]').val() != "qnorm") { qnorm_data() $('input[name="transform"]').val("qnorm") + $('span[name="transform_text"]').text(" - Quantile Normalized") + } + } + else if ($('#norm_method option:selected').val() == 'zscore'){ + if ($('input[name="transform"]').val() != "zscore") { + zscore_data() + $('input[name="transform"]').val("zscore") + $('span[name="transform_text"]').text(" - Z-Scores") } } } diff --git a/wqflask/wqflask/templates/show_trait.html b/wqflask/wqflask/templates/show_trait.html index 3bd57f89..e4feb817 100644 --- a/wqflask/wqflask/templates/show_trait.html +++ b/wqflask/wqflask/templates/show_trait.html @@ -294,9 +294,9 @@ 'data': null, 'render': function(data, type, row, meta) { if (data.value == null) { - return '' + return '' } else { - return '' + return '' } } }{% if sample_groups[0].se_exists() %}, @@ -315,9 +315,9 @@ 'data': null, 'render': function(data, type, row, meta) { if (data.variance == null) { - return '' + return '' } else { - return '' + return '' } } }{% endif %}{% if has_num_cases %}, @@ -328,9 +328,9 @@ 'data': null, 'render': function(data, type, row, meta) { if (data.num_cases == null) { - return '' + return '' } else { - return '' + return '' } } }{% endif %}{% if sample_groups[0].attributes|length > 0 %}{% for attribute in sample_groups[0].attributes|sort() %}, diff --git a/wqflask/wqflask/templates/show_trait_edit_data.html b/wqflask/wqflask/templates/show_trait_edit_data.html index a5e42fdb..667d557c 100644 --- a/wqflask/wqflask/templates/show_trait_edit_data.html +++ b/wqflask/wqflask/templates/show_trait_edit_data.html @@ -13,7 +13,7 @@ {% for sample_type in sample_groups %} {% set outer_loop = loop %}