diff options
author | zsloan | 2021-06-16 21:57:03 +0000 |
---|---|---|
committer | zsloan | 2021-06-16 21:57:03 +0000 |
commit | 6e1b4d1c53b96ed0bb6335deebd888c24b28d366 (patch) | |
tree | 75849eb2927530fd627a088b763468f659950081 | |
parent | f2e035bb4ff5a1dd5b465ae694105b1a7de956c8 (diff) | |
download | genenetwork2-6e1b4d1c53b96ed0bb6335deebd888c24b28d366.tar.gz |
Rewrote show_corr_results.py to remove all code calculating correlations (that was moved to correlation_gn3_api.py, which will probably be renamed at some point) and only include the code generating the table JSON and some template variables
-rw-r--r-- | wqflask/wqflask/correlation/show_corr_results.py | 588 |
1 files changed, 139 insertions, 449 deletions
diff --git a/wqflask/wqflask/correlation/show_corr_results.py b/wqflask/wqflask/correlation/show_corr_results.py index 2f3df67a..6c6d8f4e 100644 --- a/wqflask/wqflask/correlation/show_corr_results.py +++ b/wqflask/wqflask/correlation/show_corr_results.py @@ -18,474 +18,145 @@ # # This module is used by GeneNetwork project (www.genenetwork.org) -import collections import json -import scipy -import numpy -# import rpy2.robjects as ro # R Objects -import utility.logger -import utility.webqtlUtil -from base.trait import create_trait +from base.trait import create_trait, jsonable +from base.data_set import create_dataset -from base import data_set -from utility import helper_functions -from utility import corr_result_helpers from utility import hmac -from wqflask.correlation import correlation_functions -from utility.benchmark import Bench - -from utility.type_checking import is_str -from utility.type_checking import get_float -from utility.type_checking import get_int -from utility.type_checking import get_string -from utility.db_tools import escape - -from flask import g - -logger = utility.logger.getLogger(__name__) - -METHOD_LIT = "3" -METHOD_TISSUE_PEARSON = "4" -METHOD_TISSUE_RANK = "5" - -TISSUE_METHODS = [METHOD_TISSUE_PEARSON, METHOD_TISSUE_RANK] - -TISSUE_MOUSE_DB = 1 - - -class CorrelationResults: - def __init__(self, start_vars): - # get trait list from db (database name) - # calculate correlation with Base vector and targets - - # Check parameters - assert('corr_type' in start_vars) - assert(is_str(start_vars['corr_type'])) - assert('dataset' in start_vars) - # assert('group' in start_vars) permitted to be empty? - assert('corr_sample_method' in start_vars) - assert('corr_samples_group' in start_vars) - assert('corr_dataset' in start_vars) - assert('corr_return_results' in start_vars) - if 'loc_chr' in start_vars: - assert('min_loc_mb' in start_vars) - assert('max_loc_mb' in start_vars) - - with Bench("Doing correlations"): - if start_vars['dataset'] == "Temp": - self.dataset = data_set.create_dataset( - dataset_name="Temp", dataset_type="Temp", group_name=start_vars['group']) - self.trait_id = start_vars['trait_id'] - self.this_trait = create_trait(dataset=self.dataset, - name=self.trait_id, - cellid=None) - else: - helper_functions.get_species_dataset_trait(self, start_vars) - - corr_samples_group = start_vars['corr_samples_group'] - - self.sample_data = {} - self.corr_type = start_vars['corr_type'] - self.corr_method = start_vars['corr_sample_method'] - self.min_expr = get_float(start_vars, 'min_expr') - self.p_range_lower = get_float(start_vars, 'p_range_lower', -1.0) - self.p_range_upper = get_float(start_vars, 'p_range_upper', 1.0) - - - if ('loc_chr' in start_vars - and 'min_loc_mb' in start_vars - and 'max_loc_mb' in start_vars): - - self.location_type = get_string(start_vars, 'location_type') - self.location_chr = get_string(start_vars, 'loc_chr') - self.min_location_mb = get_int(start_vars, 'min_loc_mb') - self.max_location_mb = get_int(start_vars, 'max_loc_mb') - else: - self.location_type = self.location_chr = self.min_location_mb = self.max_location_mb = None - - self.get_formatted_corr_type() - self.return_number = int(start_vars['corr_return_results']) - - # The two if statements below append samples to the sample list based upon whether the user - # rselected Primary Samples Only, Other Samples Only, or All Samples - - primary_samples = self.dataset.group.samplelist - if self.dataset.group.parlist != None: - primary_samples += self.dataset.group.parlist - if self.dataset.group.f1list != None: - primary_samples += self.dataset.group.f1list - - # If either BXD/whatever Only or All Samples, append all of that group's samplelist - if corr_samples_group != 'samples_other': - self.process_samples(start_vars, primary_samples) - - # If either Non-BXD/whatever or All Samples, get all samples from this_trait.data and - # exclude the primary samples (because they would have been added in the previous - # if statement if the user selected All Samples) - if corr_samples_group != 'samples_primary': - if corr_samples_group == 'samples_other': - primary_samples = [x for x in primary_samples if x not in ( - self.dataset.group.parlist + self.dataset.group.f1list)] - self.process_samples(start_vars, list( - self.this_trait.data.keys()), primary_samples) - - self.target_dataset = data_set.create_dataset( - start_vars['corr_dataset']) - self.target_dataset.get_trait_data(list(self.sample_data.keys())) - - self.header_fields = get_header_fields( - self.target_dataset.type, self.corr_method) - - if self.target_dataset.type == "ProbeSet": - self.filter_cols = [7, 6] - elif self.target_dataset.type == "Publish": - self.filter_cols = [6, 0] - else: - self.filter_cols = [4, 0] - - self.correlation_results = [] +def set_template_vars(start_vars, correlation_data): + corr_type = start_vars['corr_type'] + corr_method = start_vars['corr_sample_method'] - self.correlation_data = {} + this_dataset_ob = create_dataset(dataset_name=start_vars['dataset']) + this_trait = create_trait(dataset=this_dataset_ob, + name=start_vars['trait_id']) - if self.corr_type == "tissue": - self.trait_symbol_dict = self.dataset.retrieve_genes("Symbol") + correlation_data['this_trait'] = jsonable(this_trait, this_dataset_ob) + correlation_data['this_dataset'] = this_dataset_ob.as_dict() - tissue_corr_data = self.do_tissue_correlation_for_all_traits() - if tissue_corr_data != None: - for trait in list(tissue_corr_data.keys())[:self.return_number]: - self.get_sample_r_and_p_values( - trait, self.target_dataset.trait_data[trait]) - else: - for trait, values in list(self.target_dataset.trait_data.items()): - self.get_sample_r_and_p_values(trait, values) - - elif self.corr_type == "lit": - self.trait_geneid_dict = self.dataset.retrieve_genes("GeneId") - lit_corr_data = self.do_lit_correlation_for_all_traits() - - for trait in list(lit_corr_data.keys())[:self.return_number]: - self.get_sample_r_and_p_values( - trait, self.target_dataset.trait_data[trait]) - - elif self.corr_type == "sample": - for trait, values in list(self.target_dataset.trait_data.items()): - self.get_sample_r_and_p_values(trait, values) - - self.correlation_data = collections.OrderedDict(sorted(list(self.correlation_data.items()), - key=lambda t: -abs(t[1][0]))) - - # ZS: Convert min/max chromosome to an int for the location range option - range_chr_as_int = None - for order_id, chr_info in list(self.dataset.species.chromosomes.chromosomes.items()): - if 'loc_chr' in start_vars: - if chr_info.name == self.location_chr: - range_chr_as_int = order_id - - for _trait_counter, trait in enumerate(list(self.correlation_data.keys())[:self.return_number]): - trait_object = create_trait( - dataset=self.target_dataset, name=trait, get_qtl_info=True, get_sample_info=False) - if not trait_object: - continue - - chr_as_int = 0 - for order_id, chr_info in list(self.dataset.species.chromosomes.chromosomes.items()): - if self.location_type == "highest_lod": - if chr_info.name == trait_object.locus_chr: - chr_as_int = order_id - else: - if chr_info.name == trait_object.chr: - chr_as_int = order_id - - - if (float(self.correlation_data[trait][0]) >= self.p_range_lower - and float(self.correlation_data[trait][0]) <= self.p_range_upper): - - if (self.target_dataset.type == "ProbeSet" or self.target_dataset.type == "Publish") and bool(trait_object.mean): - if (self.min_expr != None) and (float(trait_object.mean) < self.min_expr): - continue - - if range_chr_as_int != None and (chr_as_int != range_chr_as_int): - continue - if self.location_type == "highest_lod": - if (self.min_location_mb != None) and (float(trait_object.locus_mb) < float(self.min_location_mb)): - continue - if (self.max_location_mb != None) and (float(trait_object.locus_mb) > float(self.max_location_mb)): - continue - else: - if (self.min_location_mb != None) and (float(trait_object.mb) < float(self.min_location_mb)): - continue - if (self.max_location_mb != None) and (float(trait_object.mb) > float(self.max_location_mb)): - continue - - (trait_object.sample_r, - trait_object.sample_p, - trait_object.num_overlap) = self.correlation_data[trait] - - # Set some sane defaults - trait_object.tissue_corr = 0 - trait_object.tissue_pvalue = 0 - trait_object.lit_corr = 0 - if self.corr_type == "tissue" and tissue_corr_data != None: - trait_object.tissue_corr = tissue_corr_data[trait][1] - trait_object.tissue_pvalue = tissue_corr_data[trait][2] - elif self.corr_type == "lit": - trait_object.lit_corr = lit_corr_data[trait][1] - - self.correlation_results.append(trait_object) - - if self.corr_type != "lit" and self.dataset.type == "ProbeSet" and self.target_dataset.type == "ProbeSet": - self.do_lit_correlation_for_trait_list() - - if self.corr_type != "tissue" and self.dataset.type == "ProbeSet" and self.target_dataset.type == "ProbeSet": - self.do_tissue_correlation_for_trait_list() - - self.json_results = generate_corr_json( - self.correlation_results, self.this_trait, self.dataset, self.target_dataset) - -############################################################################################################################################ - - def get_formatted_corr_type(self): - self.formatted_corr_type = "" - if self.corr_type == "lit": - self.formatted_corr_type += "Literature Correlation " - elif self.corr_type == "tissue": - self.formatted_corr_type += "Tissue Correlation " - elif self.corr_type == "sample": - self.formatted_corr_type += "Genetic Correlation " - - if self.corr_method == "pearson": - self.formatted_corr_type += "(Pearson's r)" - elif self.corr_method == "spearman": - self.formatted_corr_type += "(Spearman's rho)" - elif self.corr_method == "bicor": - self.formatted_corr_type += "(Biweight r)" - - def do_tissue_correlation_for_trait_list(self, tissue_dataset_id=1): - """Given a list of correlation results (self.correlation_results), gets the tissue correlation value for each""" - - # Gets tissue expression values for the primary trait - primary_trait_tissue_vals_dict = correlation_functions.get_trait_symbol_and_tissue_values( - symbol_list=[self.this_trait.symbol]) - - if self.this_trait.symbol.lower() in primary_trait_tissue_vals_dict: - primary_trait_tissue_values = primary_trait_tissue_vals_dict[self.this_trait.symbol.lower( - )] - gene_symbol_list = [ - trait.symbol for trait in self.correlation_results if trait.symbol] - - corr_result_tissue_vals_dict = correlation_functions.get_trait_symbol_and_tissue_values( - symbol_list=gene_symbol_list) - - for trait in self.correlation_results: - if trait.symbol and trait.symbol.lower() in corr_result_tissue_vals_dict: - this_trait_tissue_values = corr_result_tissue_vals_dict[trait.symbol.lower( - )] - - result = correlation_functions.cal_zero_order_corr_for_tiss(primary_trait_tissue_values, - this_trait_tissue_values, - self.corr_method) - - trait.tissue_corr = result[0] - trait.tissue_pvalue = result[2] - - def do_tissue_correlation_for_all_traits(self, tissue_dataset_id=1): - # Gets tissue expression values for the primary trait - primary_trait_tissue_vals_dict = correlation_functions.get_trait_symbol_and_tissue_values( - symbol_list=[self.this_trait.symbol]) - - if self.this_trait.symbol.lower() in primary_trait_tissue_vals_dict: - primary_trait_tissue_values = primary_trait_tissue_vals_dict[self.this_trait.symbol.lower( - )] - - #print("trait_gene_symbols: ", pf(trait_gene_symbols.values())) - corr_result_tissue_vals_dict = correlation_functions.get_trait_symbol_and_tissue_values( - symbol_list=list(self.trait_symbol_dict.values())) - - #print("corr_result_tissue_vals: ", pf(corr_result_tissue_vals_dict)) - - #print("trait_gene_symbols: ", pf(trait_gene_symbols)) - - tissue_corr_data = {} - for trait, symbol in list(self.trait_symbol_dict.items()): - if symbol and symbol.lower() in corr_result_tissue_vals_dict: - this_trait_tissue_values = corr_result_tissue_vals_dict[symbol.lower( - )] + target_dataset_ob = create_dataset(correlation_data['target_dataset']) + correlation_data['target_dataset'] = target_dataset_ob.as_dict() - result = correlation_functions.cal_zero_order_corr_for_tiss(primary_trait_tissue_values, - this_trait_tissue_values, - self.corr_method) + table_json = correlation_json_for_table(correlation_data, + correlation_data['this_trait'], + correlation_data['this_dataset'], + target_dataset_ob) - tissue_corr_data[trait] = [symbol, result[0], result[2]] + correlation_data['table_json'] = table_json - tissue_corr_data = collections.OrderedDict(sorted(list(tissue_corr_data.items()), - key=lambda t: -abs(t[1][1]))) + if target_dataset_ob.type == "ProbeSet": + filter_cols = [7, 6] + elif target_dataset_ob.type == "Publish": + filter_cols = [6, 0] + else: + filter_cols = [4, 0] - return tissue_corr_data + correlation_data['corr_method'] = corr_method + correlation_data['filter_cols'] = filter_cols + correlation_data['header_fields'] = get_header_fields(target_dataset_ob.type, correlation_data['corr_method']) + correlation_data['formatted_corr_type'] = get_formatted_corr_type(corr_type, corr_method) - def do_lit_correlation_for_trait_list(self): + return correlation_data - input_trait_mouse_gene_id = self.convert_to_mouse_gene_id( - self.dataset.group.species.lower(), self.this_trait.geneid) +def correlation_json_for_table(correlation_data, this_trait, this_dataset, target_dataset_ob): + """Return JSON data for use with the DataTable in the correlation result page - for trait in self.correlation_results: + Keyword arguments: + correlation_data -- Correlation results + this_trait -- Trait being correlated against a dataset, as a dict + this_dataset -- Dataset of this_trait, as a dict + target_dataset_ob - Target dataset, as a Dataset ob + """ + this_trait = correlation_data['this_trait'] + this_dataset = correlation_data['this_dataset'] + target_dataset = target_dataset_ob.as_dict() - if trait.geneid: - trait.mouse_gene_id = self.convert_to_mouse_gene_id( - self.dataset.group.species.lower(), trait.geneid) - else: - trait.mouse_gene_id = None - - if trait.mouse_gene_id and str(trait.mouse_gene_id).find(";") == -1: - result = g.db.execute( - """SELECT value - FROM LCorrRamin3 - WHERE GeneId1='%s' and - GeneId2='%s' - """ % (escape(str(trait.mouse_gene_id)), escape(str(input_trait_mouse_gene_id))) - ).fetchone() - if not result: - result = g.db.execute("""SELECT value - FROM LCorrRamin3 - WHERE GeneId2='%s' and - GeneId1='%s' - """ % (escape(str(trait.mouse_gene_id)), escape(str(input_trait_mouse_gene_id))) - ).fetchone() - - if result: - lit_corr = result.value - trait.lit_corr = lit_corr - else: - trait.lit_corr = 0 - else: - trait.lit_corr = 0 - - def do_lit_correlation_for_all_traits(self): - input_trait_mouse_gene_id = self.convert_to_mouse_gene_id( - self.dataset.group.species.lower(), self.this_trait.geneid) - - lit_corr_data = {} - for trait, gene_id in list(self.trait_geneid_dict.items()): - mouse_gene_id = self.convert_to_mouse_gene_id( - self.dataset.group.species.lower(), gene_id) - - if mouse_gene_id and str(mouse_gene_id).find(";") == -1: - #print("gene_symbols:", input_trait_mouse_gene_id + " / " + mouse_gene_id) - result = g.db.execute( - """SELECT value - FROM LCorrRamin3 - WHERE GeneId1='%s' and - GeneId2='%s' - """ % (escape(mouse_gene_id), escape(input_trait_mouse_gene_id)) - ).fetchone() - if not result: - result = g.db.execute("""SELECT value - FROM LCorrRamin3 - WHERE GeneId2='%s' and - GeneId1='%s' - """ % (escape(mouse_gene_id), escape(input_trait_mouse_gene_id)) - ).fetchone() - if result: - #print("result:", result) - lit_corr = result.value - lit_corr_data[trait] = [gene_id, lit_corr] + corr_results = correlation_data['correlation_results'] + results_list = [] + for i, trait_dict in enumerate(corr_results): + trait_name = list(trait_dict.keys())[0] + trait = trait_dict[trait_name] + target_trait_ob = create_trait(dataset=target_dataset_ob, + name=trait_name, + get_qtl_info=True) + target_trait = jsonable(target_trait_ob, target_dataset_ob) + if target_trait['view'] == False: + continue + results_dict = {} + results_dict['index'] = i + 1 + results_dict['trait_id'] = target_trait['name'] + results_dict['dataset'] = target_dataset['name'] + results_dict['hmac'] = hmac.data_hmac( + '{}:{}'.format(target_trait['name'], target_dataset['name'])) + results_dict['sample_r'] = f"{float(trait['corr_coeffient']):.3f}" + results_dict['num_overlap'] = trait['num_overlap'] + results_dict['sample_p'] = f"{float(trait['p_value']):.3e}" + if target_dataset['type'] == "ProbeSet": + results_dict['symbol'] = target_trait['symbol'] + results_dict['description'] = "N/A" + results_dict['location'] = target_trait['location'] + results_dict['mean'] = "N/A" + results_dict['additive'] = "N/A" + if bool(target_trait['description']): + results_dict['description'] = target_trait['description'] + if bool(target_trait['mean']): + results_dict['mean'] = f"{float(target_trait['mean']):.3f}" + try: + results_dict['lod_score'] = f"{float(target_trait['lrs_score']) / 4.61:.1f}" + except: + results_dict['lod_score'] = "N/A" + results_dict['lrs_location'] = target_trait['lrs_location'] + if bool(target_trait['additive']): + results_dict['additive'] = f"{float(target_trait['additive']):.3f}" + results_dict['lit_corr'] = "--" + results_dict['tissue_corr'] = "--" + results_dict['tissue_pvalue'] = "--" + if this_dataset['type'] == "ProbeSet": + if 'lit_corr' in trait: + results_dict['lit_corr'] = f"{float(trait['lit_corr']):.3f}" + if 'tissue_corr' in trait: + results_dict['tissue_corr'] = f"{float(trait['tissue_corr']):.3f}" + results_dict['tissue_pvalue'] = f"{float(trait['tissue_p_val']):.3e}" + elif target_dataset['type'] == "Publish": + results_dict['abbreviation_display'] = "N/A" + results_dict['description'] = "N/A" + results_dict['mean'] = "N/A" + results_dict['authors_display'] = "N/A" + results_dict['additive'] = "N/A" + results_dict['pubmed_link'] = "N/A" + results_dict['pubmed_text'] = "N/A" + + if bool(target_trait['abbreviation']): + results_dict['abbreviation_display'] = target_trait['abbreviation'] + if bool(target_trait['description']): + results_dict['description'] = target_trait['description'] + if bool(target_trait['mean']): + results_dict['mean'] = f"{float(target_trait['mean']):.3f}" + if bool(target_trait['authors']): + authors_list = target_trait['authors'].split(',') + if len(authors_list) > 6: + results_dict['authors_display'] = ", ".join( + authors_list[:6]) + ", et al." else: - lit_corr_data[trait] = [gene_id, 0] - else: - lit_corr_data[trait] = [gene_id, 0] - - lit_corr_data = collections.OrderedDict(sorted(list(lit_corr_data.items()), - key=lambda t: -abs(t[1][1]))) - - return lit_corr_data - - def convert_to_mouse_gene_id(self, species=None, gene_id=None): - """If the species is rat or human, translate the gene_id to the mouse geneid - - If there is no input gene_id or there's no corresponding mouse gene_id, return None - - """ - if not gene_id: - return None - - mouse_gene_id = None - - if species == 'mouse': - mouse_gene_id = gene_id - - elif species == 'rat': - - query = """SELECT mouse - FROM GeneIDXRef - WHERE rat='%s'""" % escape(gene_id) - - result = g.db.execute(query).fetchone() - if result != None: - mouse_gene_id = result.mouse - - elif species == 'human': - - query = """SELECT mouse - FROM GeneIDXRef - WHERE human='%s'""" % escape(gene_id) - - result = g.db.execute(query).fetchone() - if result != None: - mouse_gene_id = result.mouse - - return mouse_gene_id - - def get_sample_r_and_p_values(self, trait, target_samples): - """Calculates the sample r (or rho) and p-value - - Given a primary trait and a target trait's sample values, - calculates either the pearson r or spearman rho and the p-value - using the corresponding scipy functions. - - """ - - self.this_trait_vals = [] - target_vals = [] - for index, sample in enumerate(self.target_dataset.samplelist): - if sample in self.sample_data: - sample_value = self.sample_data[sample] - target_sample_value = target_samples[index] - self.this_trait_vals.append(sample_value) - target_vals.append(target_sample_value) - - self.this_trait_vals, target_vals, num_overlap = corr_result_helpers.normalize_values( - self.this_trait_vals, target_vals) - - if num_overlap > 5: - # ZS: 2015 could add biweight correlation, see http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3465711/ - # if self.corr_method == 'bicor': - # sample_r, sample_p = do_bicor( - # self.this_trait_vals, target_vals) - if self.corr_method == 'pearson': - sample_r, sample_p = scipy.stats.pearsonr( - self.this_trait_vals, target_vals) - else: - sample_r, sample_p = scipy.stats.spearmanr( - self.this_trait_vals, target_vals) - - if numpy.isnan(sample_r): - pass - else: - self.correlation_data[trait] = [ - sample_r, sample_p, num_overlap] - - def process_samples(self, start_vars, sample_names, excluded_samples=None): - if not excluded_samples: - excluded_samples = () + results_dict['authors_display'] = target_trait['authors'] + if 'pubmed_id' in target_trait: + results_dict['pubmed_link'] = target_trait['pubmed_link'] + results_dict['pubmed_text'] = target_trait['pubmed_text'] + try: + results_dict['lod_score'] = f"{float(target_trait['lrs_score']) / 4.61:.1f}" + except: + results_dict['lod_score'] = "N/A" + results_dict['lrs_location'] = target_trait['lrs_location'] + if bool(target_trait['additive']): + results_dict['additive'] = f"{float(target_trait['additive']):.3f}" + else: + results_dict['location'] = target_trait['lrs_location'] - sample_val_dict = json.loads(start_vars['sample_vals']) - for sample in sample_names: - if sample not in excluded_samples: - value = sample_val_dict[sample] - if not value.strip().lower() == 'x': - self.sample_data[str(sample)] = float(value) + results_list.append(results_dict) + return json.dumps(results_list) # def do_bicor(this_trait_vals, target_trait_vals): # r_library = ro.r["library"] # Map the library function @@ -598,6 +269,25 @@ def generate_corr_json(corr_results, this_trait, dataset, target_dataset, for_ap return json.dumps(results_list) +def get_formatted_corr_type(corr_type, corr_method): + formatted_corr_type = "" + if corr_type == "lit": + formatted_corr_type += "Literature Correlation " + elif corr_type == "tissue": + formatted_corr_type += "Tissue Correlation " + elif corr_type == "sample": + formatted_corr_type += "Genetic Correlation " + + if corr_method == "pearson": + formatted_corr_type += "(Pearson's r)" + elif corr_method == "spearman": + formatted_corr_type += "(Spearman's rho)" + elif corr_method == "bicor": + formatted_corr_type += "(Biweight r)" + + return formatted_corr_type + + def get_header_fields(data_type, corr_method): if data_type == "ProbeSet": if corr_method == "spearman": |