From 8c06a9534d3340ff0287b5c70f63b2000b6eb612 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Mon, 29 Aug 2022 12:11:41 +0300 Subject: Remove usage of "logger" and un-necessary comments wrt the same Logging is used to introspect variables or notify the commencement of a given operation. Logging should only be used to log errors. Also, most of the logging is either "logger.debug" or "logger.info"; and this won't show up in production/testing since we need a logging level above "WARNING" for them to show up. * wqflask/base/data_set.py (create_datasets_list): Remove logger. (Markers.add_pvalues): Ditto. (DataSet.retrieve_other_names): Ditto. * wqflask/base/mrna_assay_tissue_data.py: Ditto. * wqflask/base/webqtlCaseData.py: Ditto. * wqflask/db/call.py (fetch1): Ditto. (gn_server): Ditto. * wqflask/db/gn_server.py: Ditto. * wqflask/maintenance/set_resource_defaults.py: Ditto. * wqflask/utility/Plot.py (find_outliers): Ditto. * wqflask/utility/gen_geno_ob.py: Ditto. * wqflask/utility/helper_functions.py: Ditto. * wqflask/utility/pillow_utils.py: Ditto. * wqflask/utility/redis_tools.py: Ditto. * wqflask/wqflask/api/gen_menu.py (get_groups): Ditto. * wqflask/wqflask/api/mapping.py: Ditto. * wqflask/wqflask/api/router.py (get_dataset_info): Ditto. * wqflask/wqflask/collect.py (report_change): Ditto. * wqflask/wqflask/correlation/corr_scatter_plot.py: Ditto. * wqflask/wqflask/ctl/ctl_analysis.py (CTL): Ditto. (CTL.__init__): Ditto. (CTL.run_analysis): Ditto. (CTL.process_results): Ditto. * wqflask/wqflask/db_info.py: Ditto. * wqflask/wqflask/do_search.py (DoSearch.execute): Ditto. (DoSearch.mescape): Ditto. (DoSearch.get_search): Ditto. (MrnaAssaySearch.run_combined): Ditto. (MrnaAssaySearch.run): Ditto. (PhenotypeSearch.run_combined): Ditto. (GenotypeSearch.get_where_clause): Ditto. (LrsSearch.get_where_clause): Ditto. (MeanSearch.run): Ditto. (RangeSearch.get_where_clause): Ditto. (PvalueSearch.run): Ditto. * wqflask/wqflask/docs.py: Ditto. * wqflask/wqflask/export_traits.py: Ditto. * wqflask/wqflask/external_tools/send_to_bnw.py: Ditto. * wqflask/wqflask/external_tools/send_to_geneweaver.py: Ditto. * wqflask/wqflask/external_tools/send_to_webgestalt.py: Ditto. * wqflask/wqflask/gsearch.py (GSearch.__init__): Ditto. * wqflask/wqflask/heatmap/heatmap.py: Ditto. * wqflask/wqflask/marker_regression/display_mapping_results.py (DisplayMappingResults): Ditto. * wqflask/wqflask/marker_regression/gemma_mapping.py: Ditto. * wqflask/wqflask/marker_regression/plink_mapping.py (run_plink): Ditto. * wqflask/wqflask/marker_regression/qtlreaper_mapping.py (run_reaper): Ditto. * wqflask/wqflask/marker_regression/rqtl_mapping.py: Ditto. * wqflask/wqflask/marker_regression/run_mapping.py (RunMapping.__init__): Ditto. * wqflask/wqflask/parser.py (parse): Ditto. * wqflask/wqflask/search_results.py (SearchResultPage.__init__): Ditto. * wqflask/wqflask/update_search_results.py (GSearch.__init__): Ditto. * wqflask/wqflask/user_login.py (send_email): Ditto. (logout): Ditto. (forgot_password_submit): Ditto. (password_reset): Ditto. (password_reset_step2): Ditto. (register): Ditto. * wqflask/wqflask/user_session.py (create_signed_cookie): Ditto. --- wqflask/db/call.py | 4 ---- wqflask/db/gn_server.py | 3 --- 2 files changed, 7 deletions(-) (limited to 'wqflask/db') diff --git a/wqflask/db/call.py b/wqflask/db/call.py index 1fe0772b..a6bbda54 100644 --- a/wqflask/db/call.py +++ b/wqflask/db/call.py @@ -31,9 +31,6 @@ GN_SERVER result when set (which should return a Tuple) res2 = func(result) else: res2 = result, - if LOG_SQL: - logger.debug("Replaced SQL call", query) - logger.debug(path, res2) return res2 else: return fetchone(query) @@ -75,5 +72,4 @@ def gn_server(path): res = urllib2.urlopen(GN_SERVER_URL + path) rest = res.read() res2 = json.loads(rest) - logger.debug(res2) return res2 diff --git a/wqflask/db/gn_server.py b/wqflask/db/gn_server.py index f9b01658..af6682ba 100644 --- a/wqflask/db/gn_server.py +++ b/wqflask/db/gn_server.py @@ -2,9 +2,6 @@ from db.call import gn_server -from utility.logger import getLogger -logger = getLogger(__name__) - def menu_main(): return gn_server("/int/menu/main.json") -- cgit v1.2.3 From 9d0f492d8a999c1a3eb6525b0ae72bbbcaad4279 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Tue, 30 Aug 2022 11:36:58 +0300 Subject: Remove "with Bench ..." calls "with Bench" instruments how long a function takes and generates time reports on as INFO logs. This should be done on a developer server. Should the log level be low enough, this bench marks will generate a lot of noise. Instrumentation should be done during development. * wqflask/base/data_set.py (create_datasets_list): Remove "with Bench...". * wqflask/db/call.py (fetchone): Ditto. (fetchall): Ditto. (gn_server): Ditto. * wqflask/wqflask/gsearch.py (GSearch.__init__): Ditto. * wqflask/wqflask/marker_regression/display_mapping_results.py (DisplayMappingResults.__init__): Ditto. * wqflask/wqflask/marker_regression/run_mapping.py (RunMapping.__init__): Ditto. * wqflask/wqflask/update_search_results.py (GSearch.__init__): Ditto. * wqflask/wqflask/views.py (search_page): Ditto. (heatmap_page): Ditto. (mapping_results_page): Ditto. --- wqflask/base/data_set.py | 20 +- wqflask/db/call.py | 38 ++-- wqflask/wqflask/gsearch.py | 240 ++++++++++----------- .../marker_regression/display_mapping_results.py | 6 +- wqflask/wqflask/marker_regression/run_mapping.py | 62 +++--- wqflask/wqflask/update_search_results.py | 34 ++- wqflask/wqflask/views.py | 68 +++--- 7 files changed, 216 insertions(+), 252 deletions(-) (limited to 'wqflask/db') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 8206b67c..33e6f353 100644 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -32,7 +32,6 @@ from maintenance import get_group_samplelists from utility.tools import locate, locate_ignore_error, flat_files from utility import gen_geno_ob from utility import chunks -from utility.benchmark import Bench from utility import webqtlUtil from db import webqtlDatabaseFunction from base import species @@ -201,16 +200,15 @@ def create_datasets_list(): if result is None: datasets = list() - with Bench("Creating DataSets object"): - type_dict = {'Publish': 'PublishFreeze', - 'ProbeSet': 'ProbeSetFreeze', - 'Geno': 'GenoFreeze'} - - for dataset_type in type_dict: - query = "SELECT Name FROM {}".format(type_dict[dataset_type]) - for result in fetchall(query): - dataset = create_dataset(result.Name, dataset_type) - datasets.append(dataset) + type_dict = {'Publish': 'PublishFreeze', + 'ProbeSet': 'ProbeSetFreeze', + 'Geno': 'GenoFreeze'} + + for dataset_type in type_dict: + query = "SELECT Name FROM {}".format(type_dict[dataset_type]) + for result in fetchall(query): + dataset = create_dataset(result.Name, dataset_type) + datasets.append(dataset) if USE_REDIS: r.set(key, pickle.dumps(datasets, pickle.HIGHEST_PROTOCOL)) diff --git a/wqflask/db/call.py b/wqflask/db/call.py index a6bbda54..c27e0326 100644 --- a/wqflask/db/call.py +++ b/wqflask/db/call.py @@ -11,13 +11,10 @@ except: import urllib2 import json from utility.tools import USE_GN_SERVER, LOG_SQL, GN_SERVER_URL -from utility.benchmark import Bench from utility.logger import getLogger logger = getLogger(__name__) -# from inspect import stack - def fetch1(query, path=None, func=None): """Fetch one result as a Tuple using either a SQL query or the URI @@ -41,11 +38,10 @@ def fetchone(query): original fetchone, but with logging) """ - with Bench("SQL", LOG_SQL): - def helper(query): - res = g.db.execute(query) - return res.fetchone() - return logger.sql(query, helper) + def helper(query): + res = g.db.execute(query) + return res.fetchone() + return logger.sql(query, helper) def fetchall(query): @@ -53,23 +49,21 @@ def fetchall(query): original fetchall, but with logging) """ - with Bench("SQL", LOG_SQL): - def helper(query): - res = g.db.execute(query) - return res.fetchall() - return logger.sql(query, helper) + def helper(query): + res = g.db.execute(query) + return res.fetchall() + return logger.sql(query, helper) def gn_server(path): """Return JSON record by calling GN_SERVER """ - with Bench("GN_SERVER", LOG_SQL): - res = () - try: - res = urllib.request.urlopen(GN_SERVER_URL + path) - except: - res = urllib2.urlopen(GN_SERVER_URL + path) - rest = res.read() - res2 = json.loads(rest) - return res2 + res = () + try: + res = urllib.request.urlopen(GN_SERVER_URL + path) + except: + res = urllib2.urlopen(GN_SERVER_URL + path) + rest = res.read() + res2 = json.loads(rest) + return res2 diff --git a/wqflask/wqflask/gsearch.py b/wqflask/wqflask/gsearch.py index 74964297..6d49bc79 100644 --- a/wqflask/wqflask/gsearch.py +++ b/wqflask/wqflask/gsearch.py @@ -11,7 +11,6 @@ from base import webqtlConfig from utility import hmac -from utility.benchmark import Bench from utility.authentication_tools import check_resource_availability from utility.type_checking import is_float, is_int, is_str, get_float, get_int, get_string @@ -61,74 +60,72 @@ class GSearch: ORDER BY species_name, inbredset_name, tissue_name, probesetfreeze_name, probeset_name LIMIT 6000 """ % (self.terms) - with Bench("Running query"): - re = g.db.execute(sql).fetchall() + re = g.db.execute(sql).fetchall() trait_list = [] dataset_to_permissions = {} - with Bench("Creating trait objects"): - for i, line in enumerate(re): - this_trait = {} - this_trait['index'] = i + 1 - this_trait['name'] = line[5] - this_trait['dataset'] = line[3] - this_trait['dataset_fullname'] = line[4] - this_trait['hmac'] = hmac.data_hmac( - '{}:{}'.format(line[5], line[3])) - this_trait['species'] = line[0] - this_trait['group'] = line[1] - this_trait['tissue'] = line[2] - this_trait['symbol'] = "N/A" - if line[6]: - this_trait['symbol'] = line[6] - this_trait['description'] = "N/A" - if line[7]: - this_trait['description'] = line[7].decode( - 'utf-8', 'replace') - this_trait['location_repr'] = "N/A" - if (line[8] != "NULL" and line[8] != "") and (line[9] != 0): - this_trait['location_repr'] = 'Chr%s: %.6f' % ( - line[8], float(line[9])) + for i, line in enumerate(re): + this_trait = {} + this_trait['index'] = i + 1 + this_trait['name'] = line[5] + this_trait['dataset'] = line[3] + this_trait['dataset_fullname'] = line[4] + this_trait['hmac'] = hmac.data_hmac( + '{}:{}'.format(line[5], line[3])) + this_trait['species'] = line[0] + this_trait['group'] = line[1] + this_trait['tissue'] = line[2] + this_trait['symbol'] = "N/A" + if line[6]: + this_trait['symbol'] = line[6] + this_trait['description'] = "N/A" + if line[7]: + this_trait['description'] = line[7].decode( + 'utf-8', 'replace') + this_trait['location_repr'] = "N/A" + if (line[8] != "NULL" and line[8] != "") and (line[9] != 0): + this_trait['location_repr'] = 'Chr%s: %.6f' % ( + line[8], float(line[9])) - this_trait['LRS_score_repr'] = "N/A" - this_trait['additive'] = "N/A" - this_trait['mean'] = "N/A" + this_trait['LRS_score_repr'] = "N/A" + this_trait['additive'] = "N/A" + this_trait['mean'] = "N/A" - if line[11] != "" and line[11] != None: - this_trait['LRS_score_repr'] = f"{line[11]:.3f}" - if line[14] != "" and line[14] != None: - this_trait['additive'] = f"{line[14]:.3f}" - if line[10] != "" and line[10] != None: - this_trait['mean'] = f"{line[10]:.3f}" + if line[11] != "" and line[11] != None: + this_trait['LRS_score_repr'] = f"{line[11]:.3f}" + if line[14] != "" and line[14] != None: + this_trait['additive'] = f"{line[14]:.3f}" + if line[10] != "" and line[10] != None: + this_trait['mean'] = f"{line[10]:.3f}" - locus_chr = line[16] - locus_mb = line[17] + locus_chr = line[16] + locus_mb = line[17] - max_lrs_text = "N/A" - if locus_chr and locus_mb: - max_lrs_text = f"Chr{locus_chr}: {locus_mb}" - this_trait['max_lrs_text'] = max_lrs_text + max_lrs_text = "N/A" + if locus_chr and locus_mb: + max_lrs_text = f"Chr{locus_chr}: {locus_mb}" + this_trait['max_lrs_text'] = max_lrs_text - this_trait['additive'] = "N/A" - if line[14] != "" and line[14] != None: - this_trait['additive'] = '%.3f' % line[14] - this_trait['dataset_id'] = line[15] + this_trait['additive'] = "N/A" + if line[14] != "" and line[14] != None: + this_trait['additive'] = '%.3f' % line[14] + this_trait['dataset_id'] = line[15] - dataset_ob = SimpleNamespace( - id=this_trait["dataset_id"], type="ProbeSet", name=this_trait["dataset"], species=this_trait["species"]) - if dataset_ob.id not in dataset_to_permissions: - permissions = check_resource_availability(dataset_ob) - dataset_to_permissions[dataset_ob.id] = permissions - else: - pemissions = dataset_to_permissions[dataset_ob.id] - if type(permissions['data']) is list: - if "view" not in permissions['data']: - continue - else: - if permissions['data'] == 'no-access': - continue + dataset_ob = SimpleNamespace( + id=this_trait["dataset_id"], type="ProbeSet", name=this_trait["dataset"], species=this_trait["species"]) + if dataset_ob.id not in dataset_to_permissions: + permissions = check_resource_availability(dataset_ob) + dataset_to_permissions[dataset_ob.id] = permissions + else: + pemissions = dataset_to_permissions[dataset_ob.id] + if type(permissions['data']) is list: + if "view" not in permissions['data']: + continue + else: + if permissions['data'] == 'no-access': + continue - trait_list.append(this_trait) + trait_list.append(this_trait) self.trait_count = len(trait_list) self.trait_list = trait_list @@ -208,76 +205,75 @@ class GSearch: """.format(group_clause, search_term) re = g.db.execute(sql).fetchall() trait_list = [] - with Bench("Creating trait objects"): - for i, line in enumerate(re): - this_trait = {} - this_trait['index'] = i + 1 - this_trait['name'] = str(line[4]) - if len(str(line[12])) == 3: - this_trait['display_name'] = str( - line[12]) + "_" + this_trait['name'] - else: - this_trait['display_name'] = this_trait['name'] - this_trait['dataset'] = line[2] - this_trait['dataset_fullname'] = line[3] - this_trait['hmac'] = hmac.data_hmac( - '{}:{}'.format(line[4], line[2])) - this_trait['species'] = line[0] - this_trait['group'] = line[1] - if line[9] != None and line[6] != None: - this_trait['description'] = line[6].decode( - 'utf-8', 'replace') - elif line[5] != None: - this_trait['description'] = line[5].decode( - 'utf-8', 'replace') - else: - this_trait['description'] = "N/A" - this_trait['dataset_id'] = line[14] + for i, line in enumerate(re): + this_trait = {} + this_trait['index'] = i + 1 + this_trait['name'] = str(line[4]) + if len(str(line[12])) == 3: + this_trait['display_name'] = str( + line[12]) + "_" + this_trait['name'] + else: + this_trait['display_name'] = this_trait['name'] + this_trait['dataset'] = line[2] + this_trait['dataset_fullname'] = line[3] + this_trait['hmac'] = hmac.data_hmac( + '{}:{}'.format(line[4], line[2])) + this_trait['species'] = line[0] + this_trait['group'] = line[1] + if line[9] != None and line[6] != None: + this_trait['description'] = line[6].decode( + 'utf-8', 'replace') + elif line[5] != None: + this_trait['description'] = line[5].decode( + 'utf-8', 'replace') + else: + this_trait['description'] = "N/A" + this_trait['dataset_id'] = line[14] - this_trait['LRS_score_repr'] = "N/A" - this_trait['additive'] = "N/A" - this_trait['mean'] = "N/A" + this_trait['LRS_score_repr'] = "N/A" + this_trait['additive'] = "N/A" + this_trait['mean'] = "N/A" - if line[10] != "" and line[10] != None: - this_trait['LRS_score_repr'] = f"{line[10]:.3f}" - # Some Max LRS values in the DB are wrongly listed as 0.000, but shouldn't be displayed - if this_trait['LRS_score_repr'] == "0.000": - this_trait['LRS_score_repr'] = "N/A" - if line[11] != "" and line[11] != None: - this_trait['additive'] = f"{line[11]:.3f}" - if line[13] != "" and line[13] != None: - this_trait['mean'] = f"{line[13]:.3f}" + if line[10] != "" and line[10] != None: + this_trait['LRS_score_repr'] = f"{line[10]:.3f}" + # Some Max LRS values in the DB are wrongly listed as 0.000, but shouldn't be displayed + if this_trait['LRS_score_repr'] == "0.000": + this_trait['LRS_score_repr'] = "N/A" + if line[11] != "" and line[11] != None: + this_trait['additive'] = f"{line[11]:.3f}" + if line[13] != "" and line[13] != None: + this_trait['mean'] = f"{line[13]:.3f}" - locus_chr = line[15] - locus_mb = line[16] + locus_chr = line[15] + locus_mb = line[16] - max_lrs_text = "N/A" - if locus_chr and locus_mb: - max_lrs_text = f"Chr{locus_chr}: {locus_mb}" - this_trait['max_lrs_text'] = max_lrs_text + max_lrs_text = "N/A" + if locus_chr and locus_mb: + max_lrs_text = f"Chr{locus_chr}: {locus_mb}" + this_trait['max_lrs_text'] = max_lrs_text - this_trait['authors'] = line[7] - this_trait['year'] = line[8] - this_trait['pubmed_text'] = "N/A" - this_trait['pubmed_link'] = "N/A" - if this_trait['year'].isdigit(): - this_trait['pubmed_text'] = this_trait['year'] - if line[9] != "" and line[9] != None: - this_trait['pubmed_link'] = webqtlConfig.PUBMEDLINK_URL % line[8] - if line[12]: - this_trait['display_name'] = line[12] + \ - "_" + str(this_trait['name']) + this_trait['authors'] = line[7] + this_trait['year'] = line[8] + this_trait['pubmed_text'] = "N/A" + this_trait['pubmed_link'] = "N/A" + if this_trait['year'].isdigit(): + this_trait['pubmed_text'] = this_trait['year'] + if line[9] != "" and line[9] != None: + this_trait['pubmed_link'] = webqtlConfig.PUBMEDLINK_URL % line[8] + if line[12]: + this_trait['display_name'] = line[12] + \ + "_" + str(this_trait['name']) - dataset_ob = SimpleNamespace(id=this_trait["dataset_id"], type="Publish", species=this_trait["species"]) - permissions = check_resource_availability(dataset_ob, this_trait['name']) - if type(permissions['data']) is list: - if "view" not in permissions['data']: - continue - else: - if permissions['data'] == 'no-access': - continue + dataset_ob = SimpleNamespace(id=this_trait["dataset_id"], type="Publish", species=this_trait["species"]) + permissions = check_resource_availability(dataset_ob, this_trait['name']) + if type(permissions['data']) is list: + if "view" not in permissions['data']: + continue + else: + if permissions['data'] == 'no-access': + continue - trait_list.append(this_trait) + trait_list.append(this_trait) self.trait_count = len(trait_list) self.trait_list = trait_list diff --git a/wqflask/wqflask/marker_regression/display_mapping_results.py b/wqflask/wqflask/marker_regression/display_mapping_results.py index 278d8a6b..ef0c357e 100644 --- a/wqflask/wqflask/marker_regression/display_mapping_results.py +++ b/wqflask/wqflask/marker_regression/display_mapping_results.py @@ -42,7 +42,6 @@ from base import webqtlConfig from base.GeneralObject import GeneralObject from utility import webqtlUtil from utility import Plot -from utility.benchmark import Bench from wqflask.interval_analyst import GeneUtil from base.webqtlConfig import GENERATED_IMAGE_DIR from utility.pillow_utils import draw_rotated_text, draw_open_polygon @@ -587,9 +586,8 @@ class DisplayMappingResults: ################################################################ showLocusForm = "" intCanvas = Image.new("RGBA", size=(self.graphWidth, self.graphHeight)) - with Bench("Drawing Plot"): - gifmap = self.plotIntMapping( - intCanvas, startMb=self.startMb, endMb=self.endMb, showLocusForm=showLocusForm) + gifmap = self.plotIntMapping( + intCanvas, startMb=self.startMb, endMb=self.endMb, showLocusForm=showLocusForm) self.gifmap = gifmap.__str__() diff --git a/wqflask/wqflask/marker_regression/run_mapping.py b/wqflask/wqflask/marker_regression/run_mapping.py index 7a424b17..c57596e9 100644 --- a/wqflask/wqflask/marker_regression/run_mapping.py +++ b/wqflask/wqflask/marker_regression/run_mapping.py @@ -33,7 +33,6 @@ from utility import webqtlUtil from utility import helper_functions from utility import Plot, Bunch from utility import temp_data -from utility.benchmark import Bench from wqflask.marker_regression import gemma_mapping, rqtl_mapping, qtlreaper_mapping, plink_mapping from wqflask.show_trait.SampleList import SampleList @@ -204,13 +203,12 @@ class RunMapping: self.first_run = False self.score_type = "-logP" self.manhattan_plot = True - with Bench("Running GEMMA"): - if self.use_loco == "True": - marker_obs, self.output_files = gemma_mapping.run_gemma( - self.this_trait, self.dataset, self.samples, self.vals, self.covariates, self.use_loco, self.maf, self.first_run, self.output_files) - else: - marker_obs, self.output_files = gemma_mapping.run_gemma( - self.this_trait, self.dataset, self.samples, self.vals, self.covariates, self.use_loco, self.maf, self.first_run, self.output_files) + if self.use_loco == "True": + marker_obs, self.output_files = gemma_mapping.run_gemma( + self.this_trait, self.dataset, self.samples, self.vals, self.covariates, self.use_loco, self.maf, self.first_run, self.output_files) + else: + marker_obs, self.output_files = gemma_mapping.run_gemma( + self.this_trait, self.dataset, self.samples, self.vals, self.covariates, self.use_loco, self.maf, self.first_run, self.output_files) results = marker_obs elif self.mapping_method == "rqtl_plink": results = self.run_rqtl_plink() @@ -372,33 +370,29 @@ class RunMapping: self.qtl_results.append(marker) total_markers = len(self.qtl_results) + export_mapping_results(self.dataset, self.this_trait, self.qtl_results, self.mapping_results_path, + self.mapping_method, self.mapping_scale, self.score_type, + self.transform, self.covariates, self.n_samples, self.vals_hash) + + if len(self.qtl_results) > 30000: + self.qtl_results = trim_markers_for_figure( + self.qtl_results) + self.results_for_browser = trim_markers_for_figure( + self.results_for_browser) + filtered_annotations = [] + for marker in self.results_for_browser: + for annot_marker in self.annotations_for_browser: + if annot_marker['rs'] == marker['rs']: + filtered_annotations.append(annot_marker) + break + self.annotations_for_browser = filtered_annotations + browser_files = write_input_for_browser( + self.dataset, self.results_for_browser, self.annotations_for_browser) + else: + browser_files = write_input_for_browser( + self.dataset, self.results_for_browser, self.annotations_for_browser) - with Bench("Exporting Results"): - export_mapping_results(self.dataset, self.this_trait, self.qtl_results, self.mapping_results_path, - self.mapping_method, self.mapping_scale, self.score_type, - self.transform, self.covariates, self.n_samples, self.vals_hash) - - with Bench("Trimming Markers for Figure"): - if len(self.qtl_results) > 30000: - self.qtl_results = trim_markers_for_figure( - self.qtl_results) - self.results_for_browser = trim_markers_for_figure( - self.results_for_browser) - filtered_annotations = [] - for marker in self.results_for_browser: - for annot_marker in self.annotations_for_browser: - if annot_marker['rs'] == marker['rs']: - filtered_annotations.append(annot_marker) - break - self.annotations_for_browser = filtered_annotations - browser_files = write_input_for_browser( - self.dataset, self.results_for_browser, self.annotations_for_browser) - else: - browser_files = write_input_for_browser( - self.dataset, self.results_for_browser, self.annotations_for_browser) - - with Bench("Trimming Markers for Table"): - self.trimmed_markers = trim_markers_for_table(results) + self.trimmed_markers = trim_markers_for_table(results) chr_lengths = get_chr_lengths( self.mapping_scale, self.mapping_method, self.dataset, self.qtl_results) diff --git a/wqflask/wqflask/update_search_results.py b/wqflask/wqflask/update_search_results.py index 6b1b4e97..f132e2c6 100644 --- a/wqflask/wqflask/update_search_results.py +++ b/wqflask/wqflask/update_search_results.py @@ -5,8 +5,6 @@ from base.data_set import create_dataset from base.trait import GeneralTrait from db import webqtlDatabaseFunction -from utility.benchmark import Bench - class GSearch: @@ -43,18 +41,15 @@ class GSearch: ORDER BY species_name, inbredset_name, tissue_name, probesetfreeze_name, probeset_name LIMIT 6000 """ % (self.terms) - with Bench("Running query"): - re = g.db.execute(sql).fetchall() + re = g.db.execute(sql).fetchall() self.trait_list = [] - with Bench("Creating trait objects"): - for line in re: - dataset = create_dataset( - line[3], "ProbeSet", get_samplelist=False) - trait_id = line[4] - # with Bench("Building trait object"): - this_trait = GeneralTrait( - dataset=dataset, name=trait_id, get_qtl_info=True, get_sample_info=False) - self.trait_list.append(this_trait) + for line in re: + dataset = create_dataset( + line[3], "ProbeSet", get_samplelist=False) + trait_id = line[4] + this_trait = GeneralTrait( + dataset=dataset, name=trait_id, get_qtl_info=True, get_sample_info=False) + self.trait_list.append(this_trait) elif self.type == "phenotype": sql = """ @@ -90,13 +85,12 @@ class GSearch: """ % (self.terms, self.terms, self.terms, self.terms, self.terms, self.terms, self.terms, self.terms, self.terms, self.terms) re = g.db.execute(sql).fetchall() self.trait_list = [] - with Bench("Creating trait objects"): - for line in re: - dataset = create_dataset(line[2], "Publish") - trait_id = line[3] - this_trait = GeneralTrait( - dataset=dataset, name=trait_id, get_qtl_info=True, get_sample_info=False) - self.trait_list.append(this_trait) + for line in re: + dataset = create_dataset(line[2], "Publish") + trait_id = line[3] + this_trait = GeneralTrait( + dataset=dataset, name=trait_id, get_qtl_info=True, get_sample_info=False) + self.trait_list.append(this_trait) self.results = self.convert_to_json() diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py index d80d557b..acd65587 100644 --- a/wqflask/wqflask/views.py +++ b/wqflask/wqflask/views.py @@ -95,7 +95,6 @@ from utility.redis_tools import get_redis_conn from base.webqtlConfig import GENERATED_IMAGE_DIR, DEFAULT_PRIVILEGES -from utility.benchmark import Bench from pprint import pformat as pf @@ -217,12 +216,11 @@ def twitter(filename): def search_page(): result = None if USE_REDIS: - with Bench("Trying Redis cache"): - key = "search_results:v1:" + \ - json.dumps(request.args, sort_keys=True) - result = Redis.get(key) - if result: - result = pickle.loads(result) + key = "search_results:v1:" + \ + json.dumps(request.args, sort_keys=True) + result = Redis.get(key) + if result: + result = pickle.loads(result) the_search = SearchResultPage(request.args) result = the_search.__dict__ valid_search = result['search_term_exists'] @@ -529,12 +527,10 @@ def heatmap_page(): version = "v5" key = "heatmap:{}:".format( version) + json.dumps(start_vars, sort_keys=True) - with Bench("Loading cache"): - result = Redis.get(key) + result = Redis.get(key) if result: - with Bench("Loading results"): - result = pickle.loads(result) + result = pickle.loads(result) else: template_vars = heatmap.Heatmap(request.form, temp_uuid) @@ -547,9 +543,7 @@ def heatmap_page(): pickled_result = pickle.dumps(result, pickle.HIGHEST_PROTOCOL) Redis.set(key, pickled_result) Redis.expire(key, 60 * 60) - - with Bench("Rendering template"): - rendered_template = render_template("heatmap.html", **result) + rendered_template = render_template("heatmap.html", **result) else: rendered_template = render_template( @@ -762,40 +756,36 @@ def mapping_results_page(): version = "v3" key = "mapping_results:{}:".format( version) + json.dumps(start_vars, sort_keys=True) - with Bench("Loading cache"): - result = None # Just for testing + result = None # Just for testing if result: - with Bench("Loading results"): - result = pickle.loads(result) + result = pickle.loads(result) else: - with Bench("Total time in RunMapping"): - try: - template_vars = run_mapping.RunMapping(start_vars, temp_uuid) - if template_vars.no_results: - rendered_template = render_template("mapping_error.html") - return rendered_template - except: + try: + template_vars = run_mapping.RunMapping(start_vars, temp_uuid) + if template_vars.no_results: rendered_template = render_template("mapping_error.html") return rendered_template + except: + rendered_template = render_template("mapping_error.html") + return rendered_template - if not template_vars.pair_scan: - template_vars.js_data = json.dumps(template_vars.js_data, - default=json_default_handler, - indent=" ") + if not template_vars.pair_scan: + template_vars.js_data = json.dumps(template_vars.js_data, + default=json_default_handler, + indent=" ") - result = template_vars.__dict__ + result = template_vars.__dict__ - if result['pair_scan']: - with Bench("Rendering template"): - rendered_template = render_template( - "pair_scan_results.html", **result) - else: - gn1_template_vars = display_mapping_results.DisplayMappingResults( - result).__dict__ + if result['pair_scan']: + rendered_template = render_template( + "pair_scan_results.html", **result) + else: + gn1_template_vars = display_mapping_results.DisplayMappingResults( + result).__dict__ - rendered_template = render_template( - "mapping_results.html", **gn1_template_vars) + rendered_template = render_template( + "mapping_results.html", **gn1_template_vars) return rendered_template -- cgit v1.2.3 From d9a6e9433e4fd5cc20ed45a53c34cab4357eac8e Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Tue, 30 Aug 2022 21:39:38 +0300 Subject: Replace fetchall, fetchone, fetch1 with database_connection * wqflask/base/data_set.py: Replace "db.call" import with "database_connection". (create_datasets_list): Use "database_connection" to fetch data. (DatasetGroup.__init__): Ditto. (DataSet.retrieve_other_names): Ditto. (PhenotypeDataSet.setup): Remove query escaping in string and format the string. (GenotypeDataSet.setup): Ditto. (MrnaAssayDataSet.setup): Ditto. * wqflask/db/webqtlDatabaseFunction.py: Remove db.call import. (retrieve_species): Use database_connection() to fetch data. (retrieve_species_id): Ditto. --- wqflask/base/data_set.py | 115 ++++++++++++++--------------------- wqflask/db/webqtlDatabaseFunction.py | 16 +++-- 2 files changed, 55 insertions(+), 76 deletions(-) (limited to 'wqflask/db') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 33e6f353..57bd6be3 100644 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -21,7 +21,6 @@ from dataclasses import dataclass from dataclasses import field from dataclasses import InitVar from typing import Optional, Dict, List -from db.call import fetchall, fetchone, fetch1 from utility.tools import USE_GN_SERVER, USE_REDIS, flat_files, flat_file_exists, GN2_BASE_URL from db.gn_server import menu_main from pprint import pformat as pf @@ -40,6 +39,7 @@ from flask import Flask, g from base.webqtlConfig import TMPDIR from urllib.parse import urlparse from utility.tools import SQL_URI +from wqflask.database import database_connection import os import math import string @@ -205,11 +205,14 @@ def create_datasets_list(): 'Geno': 'GenoFreeze'} for dataset_type in type_dict: - query = "SELECT Name FROM {}".format(type_dict[dataset_type]) - for result in fetchall(query): - dataset = create_dataset(result.Name, dataset_type) - datasets.append(dataset) - + with database_connection() as conn, conn.cursor() as cursor: + cursor.execute("SELECT Name FROM %s", + (type_dict[dataset_type],)) + results = cursor.fetchall(query) + if results: + for result in results: + datasets.append( + create_dataset(result.Name, dataset_type)) if USE_REDIS: r.set(key, pickle.dumps(datasets, pickle.HIGHEST_PROTOCOL)) r.expire(key, 60 * 60) @@ -319,12 +322,16 @@ class DatasetGroup: def __init__(self, dataset, name=None): """This sets self.group and self.group_id""" - if name == None: - self.name, self.id, self.genetic_type, self.code = fetchone( - dataset.query_for_group) - else: - self.name, self.id, self.genetic_type, self.code = fetchone( - "SELECT InbredSet.Name, InbredSet.Id, InbredSet.GeneticType, InbredSet.InbredSetCode FROM InbredSet where Name='%s'" % name) + query = """SELECT InbredSet.Name, InbredSet.Id, InbredSet.GeneticType, + InbredSet.InbredSetCode FROM InbredSet where Name=%s + """ + if not name: + query, name = dataset.query_for_group, self.name + with database_connection() as conn, conn.cursor() as cursor: + cursor.execute(query, (name,)) + results = cursor.fetchone() + if results: + self.name, self.id, self.genetic_type, self.code = results if self.name == 'BXD300': self.name = "BXD" @@ -620,36 +627,25 @@ class DataSet: """ try: + query = "" + _vars = None if self.type == "ProbeSet": - query_args = tuple(escape(x) for x in ( - self.name, - self.name, - self.name)) - - self.id, self.name, self.fullname, self.shortname, self.data_scale, self.tissue = fetch1(""" - SELECT ProbeSetFreeze.Id, ProbeSetFreeze.Name, ProbeSetFreeze.FullName, ProbeSetFreeze.ShortName, ProbeSetFreeze.DataScale, Tissue.Name - FROM ProbeSetFreeze, ProbeFreeze, Tissue - WHERE ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id - AND ProbeFreeze.TissueId = Tissue.Id - AND (ProbeSetFreeze.Name = '%s' OR ProbeSetFreeze.FullName = '%s' OR ProbeSetFreeze.ShortName = '%s') - """ % (query_args), "/dataset/" + self.name + ".json", - lambda r: (r["id"], r["name"], r["full_name"], - r["short_name"], r["data_scale"], r["tissue"]) - ) + query = """ +SELECT ProbeSetFreeze.Id, ProbeSetFreeze.Name, ProbeSetFreeze.FullName, ProbeSetFreeze.ShortName, ProbeSetFreeze.DataScale, Tissue.Name +FROM ProbeSetFreeze, ProbeFreeze, Tissue +WHERE ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id +AND ProbeFreeze.TissueId = Tissue.Id +AND (ProbeSetFreeze.Name = %s OR ProbeSetFreeze.FullName = %s OR ProbeSetFreeze.ShortName = %s""" + _vars = (self.id, self.name, self.fullname, self.shortname, self.data_scale, self.tissue,) else: - query_args = tuple(escape(x) for x in ( - (self.type + "Freeze"), - self.name, - self.name, - self.name)) - + query = """ +SELECT Id, Name, FullName, ShortName FROM %s +WHERE (Name = %s OR FullName = '%s' OR ShortName = %s)""" self.tissue = "N/A" - self.id, self.name, self.fullname, self.shortname = fetchone(""" - SELECT Id, Name, FullName, ShortName - FROM %s - WHERE (Name = '%s' OR FullName = '%s' OR ShortName = '%s') - """ % (query_args)) - + _vars = (self.id, self.name, self.fullname, self.shortname,) + with database_connection() as conn, conn.cursor() as cursor: + cursor.execute(query, (query_args,)) + _vars = cursor.fetchone() except TypeError: pass @@ -848,16 +844,8 @@ class PhenotypeDataSet(DataSet): 'Additive Effect'] self.type = 'Publish' - - self.query_for_group = ''' - SELECT - InbredSet.Name, InbredSet.Id, InbredSet.GeneticType, InbredSet.InbredSetCode - FROM - InbredSet, PublishFreeze - WHERE - PublishFreeze.InbredSetId = InbredSet.Id AND - PublishFreeze.Name = "%s" - ''' % escape(self.name) + self.query_for_group = """ +SELECT InbredSet.Name, InbredSet.Id, InbredSet.GeneticType, InbredSet.InbredSetCode FROM InbredSet, PublishFreeze WHERE PublishFreeze.InbredSetId = InbredSet.Id AND PublishFreeze.Name = %s""" def check_confidentiality(self): # (Urgently?) Need to write this @@ -964,16 +952,10 @@ class GenotypeDataSet(DataSet): # Todo: Obsolete or rename this field self.type = 'Geno' - - self.query_for_group = ''' - SELECT - InbredSet.Name, InbredSet.Id, InbredSet.GeneticType, InbredSet.InbredSetCode - FROM - InbredSet, GenoFreeze - WHERE - GenoFreeze.InbredSetId = InbredSet.Id AND - GenoFreeze.Name = "%s" - ''' % escape(self.name) + self.query_for_group = """ +SELECT InbredSet.Name, InbredSet.Id, InbredSet.GeneticType, InbredSet.InbredSetCode +FROM InbredSet, GenoFreeze WHERE GenoFreeze.InbredSetId = InbredSet.Id AND +GenoFreeze.Name = %s""" def check_confidentiality(self): return geno_mrna_confidentiality(self) @@ -1072,17 +1054,10 @@ class MrnaAssayDataSet(DataSet): # Todo: Obsolete or rename this field self.type = 'ProbeSet' - - self.query_for_group = ''' - SELECT - InbredSet.Name, InbredSet.Id, InbredSet.GeneticType, InbredSet.InbredSetCode - FROM - InbredSet, ProbeSetFreeze, ProbeFreeze - WHERE - ProbeFreeze.InbredSetId = InbredSet.Id AND - ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND - ProbeSetFreeze.Name = "%s" - ''' % escape(self.name) + self.query_for_group = """ +SELECT InbredSet.Name, InbredSet.Id, InbredSet.GeneticType, InbredSet.InbredSetCode +FROM InbredSet, ProbeSetFreeze, ProbeFreeze WHERE ProbeFreeze.InbredSetId = InbredSet.Id AND +ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND ProbeSetFreeze.Name = %s""" def check_confidentiality(self): return geno_mrna_confidentiality(self) diff --git a/wqflask/db/webqtlDatabaseFunction.py b/wqflask/db/webqtlDatabaseFunction.py index 9ec650a4..122c546f 100644 --- a/wqflask/db/webqtlDatabaseFunction.py +++ b/wqflask/db/webqtlDatabaseFunction.py @@ -20,19 +20,23 @@ # # This module is used by GeneNetwork project (www.genenetwork.org) -from db.call import fetch1 +from wqflask.database import database_connection def retrieve_species(group): """Get the species of a group (e.g. returns string "mouse" on "BXD" """ - result = fetch1("select Species.Name from Species, InbredSet where InbredSet.Name = '%s' and InbredSet.SpeciesId = Species.Id" % ( - group), "/cross/" + group + ".json", lambda r: (r["species"],))[0] + with database_connection() as conn, conn.cursor() as cursor: + cursor.execute( + "SELECT Species.Name FROM Species, InbredSet WHERE InbredSet.Name = %s AND InbredSet.SpeciesId = Species.Id", + (group,)) + return cursor.fetchone()[0] return result def retrieve_species_id(group): - result = fetch1("select SpeciesId from InbredSet where Name = '%s'" % ( - group), "/cross/" + group + ".json", lambda r: (r["species_id"],))[0] - return result + with database_connection() as conn, conn.cursor() as cursor: + cursor.execute("SELECT SpeciesId FROM InbredSet WHERE Name = %s", + (group,)) + return cursor.fetchone()[0] -- cgit v1.2.3 From 327506abbc9b4a368f5785b8f90086cc5e038eb8 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Tue, 30 Aug 2022 21:49:28 +0300 Subject: Delete unused modules * wqflask/base/data_set.py: Delete "menu_main" import. * wqflask/db/call.py: Delete it. * wqflask/db/gn_server.py: Ditto. * wqflask/wqflask/submit_bnw.py: Ditto. --- wqflask/base/data_set.py | 1 - wqflask/db/call.py | 69 ------------------------------------------- wqflask/db/gn_server.py | 7 ----- wqflask/wqflask/submit_bnw.py | 10 ------- 4 files changed, 87 deletions(-) delete mode 100644 wqflask/db/call.py delete mode 100644 wqflask/db/gn_server.py delete mode 100644 wqflask/wqflask/submit_bnw.py (limited to 'wqflask/db') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 57bd6be3..433365fc 100644 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -22,7 +22,6 @@ from dataclasses import field from dataclasses import InitVar from typing import Optional, Dict, List from utility.tools import USE_GN_SERVER, USE_REDIS, flat_files, flat_file_exists, GN2_BASE_URL -from db.gn_server import menu_main from pprint import pformat as pf from utility.db_tools import escape from utility.db_tools import mescape diff --git a/wqflask/db/call.py b/wqflask/db/call.py deleted file mode 100644 index c27e0326..00000000 --- a/wqflask/db/call.py +++ /dev/null @@ -1,69 +0,0 @@ -# Module for calling the backend - -from flask import g - -import string -try: # Python2 support - import urllib.request - import urllib.error - import urllib.parse -except: - import urllib2 -import json -from utility.tools import USE_GN_SERVER, LOG_SQL, GN_SERVER_URL - -from utility.logger import getLogger -logger = getLogger(__name__) - - -def fetch1(query, path=None, func=None): - """Fetch one result as a Tuple using either a SQL query or the URI -path to GN_SERVER (when USE_GN_SERVER is True). Apply func to -GN_SERVER result when set (which should return a Tuple) - - """ - if USE_GN_SERVER and path: - result = gn_server(path) - if func != None: - res2 = func(result) - else: - res2 = result, - return res2 - else: - return fetchone(query) - - -def fetchone(query): - """Return tuple containing one row by calling SQL directly (the -original fetchone, but with logging) - - """ - def helper(query): - res = g.db.execute(query) - return res.fetchone() - return logger.sql(query, helper) - - -def fetchall(query): - """Return row iterator by calling SQL directly (the -original fetchall, but with logging) - - """ - def helper(query): - res = g.db.execute(query) - return res.fetchall() - return logger.sql(query, helper) - - -def gn_server(path): - """Return JSON record by calling GN_SERVER - - """ - res = () - try: - res = urllib.request.urlopen(GN_SERVER_URL + path) - except: - res = urllib2.urlopen(GN_SERVER_URL + path) - rest = res.read() - res2 = json.loads(rest) - return res2 diff --git a/wqflask/db/gn_server.py b/wqflask/db/gn_server.py deleted file mode 100644 index af6682ba..00000000 --- a/wqflask/db/gn_server.py +++ /dev/null @@ -1,7 +0,0 @@ -# Backend query functions (logic) - -from db.call import gn_server - - -def menu_main(): - return gn_server("/int/menu/main.json") diff --git a/wqflask/wqflask/submit_bnw.py b/wqflask/wqflask/submit_bnw.py deleted file mode 100644 index b21a88cc..00000000 --- a/wqflask/wqflask/submit_bnw.py +++ /dev/null @@ -1,10 +0,0 @@ -from base.trait import GeneralTrait -from base import data_set -from utility import helper_functions - -import utility.logger -logger = utility.logger.getLogger(__name__) - - -def get_bnw_input(start_vars): - logger.debug("BNW VARS:", start_vars) -- cgit v1.2.3