From 2df34255de526e1e016100a8772d2c8e10eb970f Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Fri, 24 Jan 2014 20:52:46 +0000 Subject: Added natural sort to sort the index column for the sample data in the trait page Hid the Quick Search on the index page until it is working well. Started writing a file to run the pyLMM code in the background to avoid memory issues/timing out --- misc/notes.txt | 4 ++ wqflask/wqflask/my_pylmm/run_pylmm.py | 77 ++++++++++++++++++++++ .../DataTables/js/dataTables.naturalSort.js | 56 ++++++++++++++++ wqflask/wqflask/templates/index_page.html | 6 +- 4 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 wqflask/wqflask/my_pylmm/run_pylmm.py create mode 100644 wqflask/wqflask/static/new/packages/DataTables/js/dataTables.naturalSort.js diff --git a/misc/notes.txt b/misc/notes.txt index 5a43ab8a..fa2152d9 100644 --- a/misc/notes.txt +++ b/misc/notes.txt @@ -273,6 +273,10 @@ grep -ir (search string) (directory) =========================================== +Use argparse to deal with command line arguments (instead of argv) + +=========================================== + Change owner/group: chown zas1024 somefile (change owner of somefile to zas1024) diff --git a/wqflask/wqflask/my_pylmm/run_pylmm.py b/wqflask/wqflask/my_pylmm/run_pylmm.py new file mode 100644 index 00000000..9ac03ad2 --- /dev/null +++ b/wqflask/wqflask/my_pylmm/run_pylmm.py @@ -0,0 +1,77 @@ +from __future__ import absolute_import, print_function, division + +from base import data_set +from base.species import TheSpecies + + def run(dataset_name, vals, temp_uuid): + """Generates p-values for each marker""" + + tempdata = temp_data.TempData(temp_uuid) + + dataset = data_set.create_dataset(dataset_name) + species = TheSpecies(dataset=dataset) + + samples = [] # Want only ones with values + vals = vals + + for sample in dataset.group.samplelist: + samples.append(str(sample)) + + gen_data(dataset, vals, tempdata) + + + def gen_data(dataset, vals) + dataset.group.get_markers() + + pheno_vector = np.array([val == "x" and np.nan or float(val) for val in vals]) + + if dataset.group.species == "human": + p_values, t_stats = gen_human_results(pheno_vector, tempdata) + else: + genotype_data = [marker['genotypes'] for marker in self.dataset.group.markers.markers] + + no_val_samples = self.identify_empty_samples() + trimmed_genotype_data = self.trim_genotypes(genotype_data, no_val_samples) + + genotype_matrix = np.array(trimmed_genotype_data).T + + #print("pheno_vector: ", pf(pheno_vector)) + #print("genotype_matrix: ", pf(genotype_matrix)) + #print("genotype_matrix.shape: ", pf(genotype_matrix.shape)) + + t_stats, p_values = lmm.run( + pheno_vector, + genotype_matrix, + restricted_max_likelihood=True, + refit=False, + temp_data=tempdata + ) + #print("p_values:", p_values) + + self.dataset.group.markers.add_pvalues(p_values) + return self.dataset.group.markers.markers + + + def gen_human_results(self, pheno_vector, tempdata): + file_base = os.path.join(webqtlConfig.PYLMM_PATH, self.dataset.group.name) + + plink_input = input.plink(file_base, type='b') + input_file_name = os.path.join(webqtlConfig.SNP_PATH, self.dataset.group.name + ".snps.gz") + + pheno_vector = pheno_vector.reshape((len(pheno_vector), 1)) + covariate_matrix = np.ones((pheno_vector.shape[0],1)) + kinship_matrix = np.fromfile(open(file_base + '.kin','r'),sep=" ") + kinship_matrix.resize((len(plink_input.indivs),len(plink_input.indivs))) + + p_values, t_stats = lmm.run_human( + pheno_vector, + covariate_matrix, + input_file_name, + kinship_matrix, + loading_progress=tempdata + ) + + return p_values, t_stats + +if __name__ == '__main__': + run(dataset_name, vals, temp_uuid) \ No newline at end of file diff --git a/wqflask/wqflask/static/new/packages/DataTables/js/dataTables.naturalSort.js b/wqflask/wqflask/static/new/packages/DataTables/js/dataTables.naturalSort.js new file mode 100644 index 00000000..c9e26682 --- /dev/null +++ b/wqflask/wqflask/static/new/packages/DataTables/js/dataTables.naturalSort.js @@ -0,0 +1,56 @@ +(function() { + +/* + * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license + * Author: Jim Palmer (based on chunking idea from Dave Koelle) + * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo + * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js + */ +function naturalSort (a, b) { + var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi, + sre = /(^[ ]*|[ ]*$)/g, + dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, + hre = /^0x[0-9a-f]+$/i, + ore = /^0/, + // convert all to strings and trim() + x = a.toString().replace(sre, '') || '', + y = b.toString().replace(sre, '') || '', + // chunk/tokenize + xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), + yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), + // numeric, hex or date detection + xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)), + yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null; + // first try and sort Hex codes or Dates + if (yD) + if ( xD < yD ) return -1; + else if ( xD > yD ) return 1; + // natural sorting through split numeric strings and default strings + for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) { + // find floats not starting with '0', string or 0 if not defined (Clint Priest) + var oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0; + var oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0; + // handle numeric vs string comparison - number < string - (Kyle Adams) + if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? 1 : -1; + // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2' + else if (typeof oFxNcL !== typeof oFyNcL) { + oFxNcL += ''; + oFyNcL += ''; + } + if (oFxNcL < oFyNcL) return -1; + if (oFxNcL > oFyNcL) return 1; + } + return 0; +} + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "natural-asc": function ( a, b ) { + return naturalSort(a,b); + }, + + "natural-desc": function ( a, b ) { + return naturalSort(a,b) * -1; + } +} ); + +}()); \ No newline at end of file diff --git a/wqflask/wqflask/templates/index_page.html b/wqflask/wqflask/templates/index_page.html index d177a7bd..a7d7b513 100644 --- a/wqflask/wqflask/templates/index_page.html +++ b/wqflask/wqflask/templates/index_page.html @@ -17,7 +17,7 @@