From 51db16394ebe5936a2078293c676744b7ea74fc6 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Thu, 7 Mar 2013 23:42:58 +0000 Subject: Progress bar is now completely working Still need to figure out the problem that occurred with negative p-values after I refactored the LMM code --- wqflask/base/data_set.py | 1 + .../wqflask/marker_regression/marker_regression.py | 3 +- wqflask/wqflask/my_pylmm/pyLMM/lmm.py | 203 ++++++++++++++++----- .../new/javascript/show_trait_mapping_tools.coffee | 1 - .../new/javascript/show_trait_mapping_tools.js | 70 +------ .../wqflask/templates/show_trait_progress_bar.html | 2 +- 6 files changed, 159 insertions(+), 121 deletions(-) diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index c6d67e68..d474302c 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -75,6 +75,7 @@ class Markers(object): def add_pvalues(self, p_values): for marker, p_value in itertools.izip(self.markers, p_values): marker['p_value'] = p_value + print("p_value is:", marker['p_value']) marker['lod_score'] = -math.log10(marker['p_value']) #Using -log(p) for the LRS; need to ask Rob how he wants to get LRS from p-values marker['lrs_value'] = -math.log10(marker['p_value']) * 4.61 diff --git a/wqflask/wqflask/marker_regression/marker_regression.py b/wqflask/wqflask/marker_regression/marker_regression.py index 412d9e35..4ddc89c6 100755 --- a/wqflask/wqflask/marker_regression/marker_regression.py +++ b/wqflask/wqflask/marker_regression/marker_regression.py @@ -497,7 +497,8 @@ class MarkerRegression(object): genotype_matrix, kinship_matrix, REML=True, - refit=False) + refit=False, + temp_data=self.temp_data) Bench().report() diff --git a/wqflask/wqflask/my_pylmm/pyLMM/lmm.py b/wqflask/wqflask/my_pylmm/pyLMM/lmm.py index cc2e32a7..12f7c2ea 100644 --- a/wqflask/wqflask/my_pylmm/pyLMM/lmm.py +++ b/wqflask/wqflask/my_pylmm/pyLMM/lmm.py @@ -95,53 +95,53 @@ def calculate_kinship(genotype_matrix, temp_data): kinship_matrix = np.dot(genotype_matrix,genotype_matrix.T) * 1.0/float(m) return kinship_matrix -def GWAS(Y, X, K, Kva=[], Kve=[], X0=None, REML=True, refit=False): - """ - Performs a basic GWAS scan using the LMM. This function - uses the LMM module to assess association at each SNP and - does some simple cleanup, such as removing missing individuals - per SNP and re-computing the eigen-decomp - - Y - n x 1 phenotype vector - X - n x m SNP matrix - K - n x n kinship matrix - Kva,Kve = linalg.eigh(K) - or the eigen vectors and values for K - X0 - n x q covariate matrix - REML - use restricted maximum likelihood - refit - refit the variance component for each SNP - """ - n = X.shape[0] - m = X.shape[1] +def GWAS(Y, X, K, Kva=[], Kve=[], X0=None, REML=True, refit=False, temp_data=None): + """ + Performs a basic GWAS scan using the LMM. This function + uses the LMM module to assess association at each SNP and + does some simple cleanup, such as removing missing individuals + per SNP and re-computing the eigen-decomp + + Y - n x 1 phenotype vector + X - n x m SNP matrix + K - n x n kinship matrix + Kva,Kve = linalg.eigh(K) - or the eigen vectors and values for K + X0 - n x q covariate matrix + REML - use restricted maximum likelihood + refit - refit the variance component for each SNP + """ + n = X.shape[0] + m = X.shape[1] - if X0 == None: X0 = np.ones((n,1)) - - # Remove missing values in Y and adjust associated parameters - v = np.isnan(Y) - if v.sum(): - keep = True - v - Y = Y[keep] - X = X[keep,:] - X0 = X0[keep,:] - K = K[keep,:][:,keep] - Kva = [] - Kve = [] + if X0 == None: X0 = np.ones((n,1)) - L = LMM(Y,K,Kva,Kve,X0) - if not refit: L.fit() + # Remove missing values in Y and adjust associated parameters + v = np.isnan(Y) + if v.sum(): + keep = True - v + Y = Y[keep] + X = X[keep,:] + X0 = X0[keep,:] + K = K[keep,:][:,keep] + Kva = [] + Kve = [] - PS = [] - TS = [] + L = LMM(Y,K,Kva,Kve,X0) + if not refit: L.fit() - for i in range(m): - x = X[:,i].reshape((n,1)) - v = np.isnan(x).reshape((-1,)) - if v.sum(): + PS = [] + TS = [] + + for counter in range(m): + x = X[:,counter].reshape((n,1)) + v = np.isnan(x).reshape((-1,)) + if v.sum(): keep = True - v xs = x[keep,:] - if xs.var() == 0: - PS.append(np.nan) - TS.append(np.nan) - continue + if xs.var() == 0: + PS.append(np.nan) + TS.append(np.nan) + continue Ys = Y[keep] X0s = X0[keep,:] @@ -150,19 +150,124 @@ def GWAS(Y, X, K, Kva=[], Kve=[], X0=None, REML=True, refit=False): if refit: Ls.fit(X=xs) else: Ls.fit() ts,ps = Ls.association(xs,REML=REML) - else: - if x.var() == 0: - PS.append(np.nan) - TS.append(np.nan) - continue + else: + if x.var() == 0: + PS.append(np.nan) + TS.append(np.nan) + continue if refit: L.fit(X=x) ts,ps = L.association(x,REML=REML) - PS.append(ps) - TS.append(ts) + percent_complete = 45 + int(round((counter/m)*55)) + print("Percent complete: ", percent_complete) + temp_data.store("percent_complete", percent_complete) - return TS,PS + PS.append(ps) + TS.append(ts) + + return TS,PS + +#def GWAS(pheno_vector, +# genotype_matrix, +# kinship_matrix, +# kinship_eigenvals=None, +# kinship_eigenvectors=None, +# covariate_matrix=None, +# restricted_max_likelihood=True, +# refit=False, +# temp_data=None): +# """ +# Performs a basic GWAS scan using the LMM. This function +# uses the LMM module to assess association at each SNP and +# does some simple cleanup, such as removing missing individuals +# per SNP and re-computing the eigen-decomp +# +# Y - n x 1 phenotype vector +# X - n x m SNP matrix +# K - n x n kinship matrix +# Kva,Kve = linalg.eigh(K) - or the eigen vectors and values for K +# X0 - n x q covariate matrix +# REML - use restricted maximum likelihood +# refit - refit the variance component for each SNP +# +# """ +# +# assert temp_data, "You forgot to pass in temp_data" +# +# if kinship_eigenvals == None: +# kinship_eigenvals = [] +# if kinship_eigenvectors == None: +# kinship_eigenvectors = [] +# +# n = genotype_matrix.shape[0] +# m = genotype_matrix.shape[1] +# +# if covariate_matrix == None: +# covariate_matrix = np.ones((n,1)) +# +# # Remove missing values in Y and adjust associated parameters +# pheno_not_number = np.isnan(pheno_vector) +# if pheno_not_number.sum(): +# keep = True - pheno_not_number +# pheno_vector = pheno_vector[keep] +# genotype_matrix = genotype_matrix[keep,:] +# covariate_matrix = covariate_matrix[keep,:] +# kinship_matrix = kinship_matrix[keep,:][:,keep] +# kinship_eigenvals = [] +# kinship_eigenvectors = [] +# +# lmm_ob = LMM(pheno_vector, +# kinship_matrix, +# kinship_eigenvals, +# kinship_eigenvectors, +# covariate_matrix) +# if not refit: +# lmm_ob.fit() +# +# p_value_matrix = [] +# t_stats_matrix = [] +# +# for counter in range(m): +# #pheno_vector_2 = geno_vector[:, counter] +# #x = pheno_vector_2.reshape((n,1)) +# x = genotype_matrix[:,counter].reshape((n,1)) +# v = np.isnan(x).reshape((-1,)) +# if v.sum(): +# keep = True - v +# xs = x[keep,:] +# if xs.var() == 0: +# p_value_matrix.append(np.nan) +# t_stats_matrix.append(np.nan) +# continue +# +# pheno_vector_2 = pheno_vector[keep] +# covariate_matrix_2 = covariate_matrix[keep,:] +# kinship_matrix_2 = kinship_matrix[keep,:][:,keep] +# lmm_ob_2 = LMM(pheno_vector, kinship_matrix, covariate_matrix=covariate_matrix) +# if refit: +# lmm_ob_2.fit(X=xs) +# else: +# lmm_ob_2.fit() +# t_stats, p_values = lmm_ob_2.association(xs, REML=restricted_max_likelihood) +# else: +# if x.var() == 0: +# p_value_matrix.append(np.nan) +# t_stats_matrix.append(np.nan) +# continue +# +# if refit: +# lmm_ob.fit(X=x) +# t_stats,p_values = lmm_ob.association(x, REML=restricted_max_likelihood) +# +# p_value_matrix.append(p_values) +# t_stats_matrix.append(t_stats) +# +# percent_complete = 45 + int(round((counter/m)*55)) +# print("Percent complete: ", percent_complete) +# temp_data.store("percent_complete", percent_complete) +# +# return p_value_matrix, t_stats_matrix class LMM: diff --git a/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.coffee b/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.coffee index 35572f67..157f56a9 100644 --- a/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.coffee +++ b/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.coffee @@ -30,7 +30,6 @@ $ -> $("#marker_regression").click(() => $("#progress_bar_container").modal() - url = "/marker_regression" form_data = $('#trait_data_form').serialize() console.log("form_data is:", form_data) diff --git a/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.js b/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.js index 78459692..c8b0aa7b 100644 --- a/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.js +++ b/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.js @@ -1,74 +1,6 @@ // Generated by CoffeeScript 1.4.0 (function() { - $(function() { - var composite_mapping_fields, get_progress, submit_special, toggle_enable_disable, - _this = this; - submit_special = function() { - var url; - console.log("In submit_special"); - console.log("this is:", this); - console.log("$(this) is:", $(this)); - url = $(this).data("url"); - console.log("url is:", url); - $("#trait_data_form").attr("action", url); - return $("#trait_data_form").submit(); - }; - get_progress = function() { - var params, params_str, temp_uuid, url, - _this = this; - console.log("temp_uuid:", $("#temp_uuid").val()); - temp_uuid = $("#temp_uuid").val(); - params = { - key: temp_uuid - }; - params_str = $.param(params); - url = "/get_temp_data?" + params_str; - console.log("url:", url); - $.ajax({ - type: "GET", - url: url, - success: function(progress_data) { - console.log("in get_progress data:", progress_data); - console.log(progress_data['percent_complete'] + "%"); - return $('#marker_regression_progress').css("width", progress_data['percent_complete'] + "%"); - } - }); - return false; - }; - $("#marker_regression").click(function() { - var form_data, url; - $("#progress_bar_container").modal(); - url = "/marker_regression"; - form_data = $('#trait_data_form').serialize(); - console.log("form_data is:", form_data); - $.ajax({ - type: "POST", - url: url, - data: form_data, - success: function(data) { - clearInterval(_this.my_timer); - $('#progress_bar_container').modal('hide'); - return $("body").html(data); - } - }); - console.log("settingInterval"); - _this.my_timer = setInterval(get_progress, 1000); - return false; - }); - composite_mapping_fields = function() { - return $(".composite_fields").toggle(); - }; - $("#use_composite_choice").change(composite_mapping_fields); - toggle_enable_disable = function(elem) { - return $(elem).prop("disabled", !$(elem).prop("disabled")); - }; - $("#choose_closet_control").change(function() { - return toggle_enable_disable("#control_locus"); - }); - return $("#display_all_lrs").change(function() { - return toggle_enable_disable("#suggestive_lrs"); - }); - }); + }).call(this); diff --git a/wqflask/wqflask/templates/show_trait_progress_bar.html b/wqflask/wqflask/templates/show_trait_progress_bar.html index 2984cc02..eff5c391 100644 --- a/wqflask/wqflask/templates/show_trait_progress_bar.html +++ b/wqflask/wqflask/templates/show_trait_progress_bar.html @@ -3,7 +3,7 @@

Loading...