diff options
-rw-r--r-- | wqflask/wqflask/show_trait/show_trait.py | 56 | ||||
-rw-r--r-- | wqflask/wqflask/static/new/javascript/initialize_show_trait_tables.js | 8 |
2 files changed, 38 insertions, 26 deletions
diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index b6fcbcb8..424b8ee5 100644 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -156,8 +156,14 @@ class ShowTrait: self.make_sample_lists() - self.qnorm_vals = quantile_normalize_vals(self.sample_groups) - self.z_scores = get_z_scores(self.sample_groups) + trait_vals_by_group = [] + for sample_type in self.sample_groups: + trait_vals_by_group.append(get_trait_vals(sample_type.sample_list)) + + self.max_digits_by_group = get_max_digits(trait_vals_by_group) + + self.qnorm_vals = quantile_normalize_vals(self.sample_groups, trait_vals_by_group) + self.z_scores = get_z_scores(self.sample_groups, trait_vals_by_group) self.temp_uuid = uuid.uuid4() @@ -294,6 +300,7 @@ class ShowTrait: js_data = dict(trait_id=self.trait_id, trait_symbol=trait_symbol, + max_digits = self.max_digits_by_group, short_description=short_description, unit_type=trait_units, dataset_type=self.dataset.type, @@ -513,7 +520,26 @@ class ShowTrait: self.dataset.group.allsamples = all_samples_ordered -def quantile_normalize_vals(sample_groups): +def get_trait_vals(sample_list): + trait_vals = [] + for sample in sample_list: + try: + trait_vals.append(float(sample.value)) + except: + continue + + return trait_vals + +def get_max_digits(trait_vals): + max_digits = [] + for these_vals in trait_vals: + max_val = max(these_vals) + digits = len(str(max_val)) + max_digits.append(digits - 1) + + return max_digits + +def quantile_normalize_vals(sample_groups, trait_vals): def normf(trait_vals): ranked_vals = ss.rankdata(trait_vals) p_list = [] @@ -529,15 +555,8 @@ def quantile_normalize_vals(sample_groups): return normed_vals qnorm_by_group = [] - for sample_type in sample_groups: - trait_vals = [] - for sample in sample_type.sample_list: - try: - trait_vals.append(float(sample.value)) - except: - continue - - qnorm_vals = normf(trait_vals) + for i, sample_type in enumerate(sample_groups): + qnorm_vals = normf(trait_vals[i]) qnorm_vals_with_x = [] counter = 0 for sample in sample_type.sample_list: @@ -552,17 +571,10 @@ def quantile_normalize_vals(sample_groups): return qnorm_by_group -def get_z_scores(sample_groups): +def get_z_scores(sample_groups, trait_vals): zscore_by_group = [] - for sample_type in sample_groups: - trait_vals = [] - for sample in sample_type.sample_list: - try: - trait_vals.append(float(sample.value)) - except: - continue - - zscores = ss.mstats.zscore(np.array(trait_vals)).tolist() + for i, sample_type in enumerate(sample_groups): + zscores = ss.mstats.zscore(np.array(trait_vals[i])).tolist() zscores_with_x = [] counter = 0 for sample in sample_type.sample_list: diff --git a/wqflask/wqflask/static/new/javascript/initialize_show_trait_tables.js b/wqflask/wqflask/static/new/javascript/initialize_show_trait_tables.js index 05e4d547..f9369fb9 100644 --- a/wqflask/wqflask/static/new/javascript/initialize_show_trait_tables.js +++ b/wqflask/wqflask/static/new/javascript/initialize_show_trait_tables.js @@ -40,9 +40,9 @@ build_columns = function() { 'data': null, 'render': function(data, type, row, meta) { if (data.value == null) { - return '<input type="text" data-value="x" data-qnorm="x" data-zscore="x" name="value:' + data.name + '" style="text-align: right;" class="trait_value_input edit_sample_value" value="x" size=6 maxlength=6>' + return '<input type="text" data-value="x" data-qnorm="x" data-zscore="x" name="value:' + data.name + '" style="text-align: right;" class="trait_value_input edit_sample_value" value="x" size=' + js_data.max_digits[0] + '>' } else { - return '<input type="text" data-value="' + data.value.toFixed(3) + '" data-qnorm="' + js_data['qnorm_values'][0][parseInt(data.this_id) - 1] + '" data-zscore="' + js_data['zscore_values'][0][parseInt(data.this_id) - 1] + '" name="value:' + data.name + '" class="trait_value_input edit_sample_value" value="' + data.value.toFixed(3) + '" size=6 maxlength=6>' + return '<input type="text" data-value="' + data.value.toFixed(3) + '" data-qnorm="' + js_data['qnorm_values'][0][parseInt(data.this_id) - 1] + '" data-zscore="' + js_data['zscore_values'][0][parseInt(data.this_id) - 1] + '" name="value:' + data.name + '" class="trait_value_input edit_sample_value" value="' + data.value.toFixed(3) + '" size=' + js_data.max_digits[0] + '>' } } } @@ -66,9 +66,9 @@ build_columns = function() { 'data': null, 'render': function(data, type, row, meta) { if (data.variance == null) { - return '<input type="text" data-value="x" data-qnorm="x" data-zscore="x" name="value:' + data.name + '" class="trait_value_input edit_sample_se" value="x" size=6 maxlength=6>' + return '<input type="text" data-value="x" data-qnorm="x" data-zscore="x" name="value:' + data.name + '" class="trait_value_input edit_sample_se" value="x" size=6>' } else { - return '<input type="text" data-value="' + data.variance.toFixed(3) + '" data-qnorm="x" data-zscore="x" name="value:' + data.name + '" class="trait_value_input edit_sample_se" value="' + data.variance.toFixed(3) + '" size=6 maxlength=6>' + return '<input type="text" data-value="' + data.variance.toFixed(3) + '" data-qnorm="x" data-zscore="x" name="value:' + data.name + '" class="trait_value_input edit_sample_se" value="' + data.variance.toFixed(3) + '" size=6>' } } } |