From 971d29e82391cc269405f59b5aad5f31061b633d Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Thu, 22 Jun 2023 16:31:18 +0300 Subject: Update calls to `database_connection` function Pass in the database URI at call time. --- wqflask/base/data_set/__init__.py | 4 +-- wqflask/base/data_set/dataset.py | 11 +++---- wqflask/base/data_set/datasetgroup.py | 5 ++-- wqflask/base/data_set/genotypedataset.py | 3 +- wqflask/base/data_set/mrnaassaydataset.py | 7 +++-- wqflask/base/data_set/phenotypedataset.py | 5 ++-- wqflask/base/data_set/probably_unused.py | 3 +- wqflask/base/data_set/utils.py | 6 ++-- wqflask/base/trait.py | 4 +-- wqflask/db/webqtlDatabaseFunction.py | 6 ++-- wqflask/maintenance/gen_select_dataset.py | 21 ++----------- .../maintenance/generate_probesetfreeze_file.py | 3 +- wqflask/maintenance/quantile_normalize.py | 18 ++--------- wqflask/maintenance/set_resource_defaults.py | 19 ++---------- wqflask/utility/authentication_tools.py | 4 +-- wqflask/utility/helper_functions.py | 3 +- wqflask/wqflask/api/correlation.py | 5 ++-- wqflask/wqflask/api/router.py | 32 ++++++++++---------- .../wqflask/correlation/correlation_functions.py | 3 +- wqflask/wqflask/db_info.py | 3 +- wqflask/wqflask/do_search.py | 6 ++-- wqflask/wqflask/docs.py | 5 ++-- .../wqflask/external_tools/send_to_geneweaver.py | 3 +- .../wqflask/external_tools/send_to_webgestalt.py | 3 +- wqflask/wqflask/heatmap/heatmap.py | 3 +- wqflask/wqflask/interval_analyst/GeneUtil.py | 4 +-- .../marker_regression/display_mapping_results.py | 7 +++-- wqflask/wqflask/marker_regression/gemma_mapping.py | 3 +- wqflask/wqflask/marker_regression/rqtl_mapping.py | 4 +-- wqflask/wqflask/marker_regression/run_mapping.py | 4 +-- wqflask/wqflask/metadata_edits.py | 35 +++++++++++++--------- wqflask/wqflask/partial_correlations_views.py | 4 +-- wqflask/wqflask/search_results.py | 4 +-- wqflask/wqflask/show_trait/SampleList.py | 5 ++-- wqflask/wqflask/show_trait/show_trait.py | 8 ++--- wqflask/wqflask/snp_browser/snp_browser.py | 12 ++++---- wqflask/wqflask/update_search_results.py | 5 ++-- wqflask/wqflask/views.py | 11 +++---- 38 files changed, 138 insertions(+), 153 deletions(-) (limited to 'wqflask') diff --git a/wqflask/base/data_set/__init__.py b/wqflask/base/data_set/__init__.py index e49c6a93..69eaab53 100644 --- a/wqflask/base/data_set/__init__.py +++ b/wqflask/base/data_set/__init__.py @@ -10,7 +10,7 @@ from redis import Redis # local imports from .dataset import DataSet from base import webqtlConfig -from utility.tools import USE_REDIS +from utility.tools import get_setting, USE_REDIS from .datasettype import DatasetType from .tempdataset import TempDataSet from .datasetgroup import DatasetGroup @@ -54,7 +54,7 @@ def create_dataset(dataset_name, dataset_type=None, def datasets(group_name, this_group=None, redis_conn=Redis()): key = "group_dataset_menu:v2:" + group_name dataset_menu = [] - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute(''' (SELECT '#PublishFreeze',PublishFreeze.FullName,PublishFreeze.Name FROM PublishFreeze,InbredSet diff --git a/wqflask/base/data_set/dataset.py b/wqflask/base/data_set/dataset.py index feab2b23..435d74a9 100644 --- a/wqflask/base/data_set/dataset.py +++ b/wqflask/base/data_set/dataset.py @@ -7,6 +7,7 @@ from redis import Redis from base import species from utility import chunks +from utility.tools import get_setting from gn3.monads import MonadicDict, query_sql from pymonad.maybe import Maybe, Nothing from .datasetgroup import DatasetGroup @@ -64,7 +65,7 @@ class DataSet: """Get the accession_id of this dataset depending on the dataset type.""" __query = "" - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: if self.type == "Publish": __query = ( "SELECT InfoFiles.GN_AccesionId AS accession_id FROM " @@ -115,7 +116,7 @@ class DataSet: all is passed. """ - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: try: if self.type == "ProbeSet": cursor.execute( @@ -148,7 +149,7 @@ class DataSet: results = {} traits_name_dict = () - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT ProbeSetXRef.DataId,ProbeSet.Name " "FROM ProbeSet, ProbeSetXRef, ProbeSetFreeze " @@ -179,7 +180,7 @@ class DataSet: if self.group.parlist != None and self.group.f1list != None: if (self.group.parlist + self.group.f1list) in self.samplelist: self.samplelist += self.group.parlist + self.group.f1list - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT Strain.Name, Strain.Id FROM " "Strain, Species WHERE Strain.Name IN " @@ -219,7 +220,7 @@ class DataSet: if (self.group.parlist + self.group.f1list) in self.samplelist: self.samplelist += self.group.parlist + self.group.f1list - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT Strain.Name, Strain.Id FROM Strain, Species " f"WHERE Strain.Name IN {create_in_clause(self.samplelist)} " diff --git a/wqflask/base/data_set/datasetgroup.py b/wqflask/base/data_set/datasetgroup.py index 72577f38..95dc976f 100644 --- a/wqflask/base/data_set/datasetgroup.py +++ b/wqflask/base/data_set/datasetgroup.py @@ -15,6 +15,7 @@ from utility.tools import ( locate, USE_REDIS, flat_files, + get_setting, flat_file_exists, locate_ignore_error) @@ -29,7 +30,7 @@ class DatasetGroup: def __init__(self, dataset, name=None): """This sets self.group and self.group_id""" - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: if not name: cursor.execute(dataset.query_for_group, (dataset.name,)) @@ -64,7 +65,7 @@ class DatasetGroup: def get_mapping_methods(self): mapping_id = () - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT MappingMethodId FROM " "InbredSet WHERE Name= %s", diff --git a/wqflask/base/data_set/genotypedataset.py b/wqflask/base/data_set/genotypedataset.py index 2381a42a..b903cd72 100644 --- a/wqflask/base/data_set/genotypedataset.py +++ b/wqflask/base/data_set/genotypedataset.py @@ -2,6 +2,7 @@ from .dataset import DataSet from utility import webqtlUtil +from utility.tools import get_setting from db import webqtlDatabaseFunction from .utils import geno_mrna_confidentiality from wqflask.database import database_connection @@ -46,7 +47,7 @@ GenoFreeze.Name = %s""" def retrieve_sample_data(self, trait): results = [] - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT Strain.Name, GenoData.value, " "GenoSE.error, 'N/A', Strain.Name2 " diff --git a/wqflask/base/data_set/mrnaassaydataset.py b/wqflask/base/data_set/mrnaassaydataset.py index b76c5a74..4eb998b5 100644 --- a/wqflask/base/data_set/mrnaassaydataset.py +++ b/wqflask/base/data_set/mrnaassaydataset.py @@ -6,6 +6,7 @@ import codecs from .dataset import DataSet from .utils import geno_mrna_confidentiality from wqflask.database import database_connection +from utility.tools import get_setting class MrnaAssayDataSet(DataSet): ''' @@ -73,7 +74,7 @@ ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND ProbeSetFreeze.Name = %s""" # Note: setting trait_list to [] is probably not a great idea. if not trait_list: trait_list = [] - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: for this_trait in trait_list: if not this_trait.haveinfo: @@ -144,7 +145,7 @@ ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND ProbeSetFreeze.Name = %s""" return trait_list def retrieve_sample_data(self, trait): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT Strain.Name, ProbeSetData.value, " "ProbeSetSE.error, NStrain.count, " @@ -168,7 +169,7 @@ ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND ProbeSetFreeze.Name = %s""" return cursor.fetchall() def retrieve_genes(self, column_name): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( f"SELECT ProbeSet.Name, ProbeSet.{column_name} " "FROM ProbeSet,ProbeSetXRef WHERE " diff --git a/wqflask/base/data_set/phenotypedataset.py b/wqflask/base/data_set/phenotypedataset.py index bdf28a7a..5a39418a 100644 --- a/wqflask/base/data_set/phenotypedataset.py +++ b/wqflask/base/data_set/phenotypedataset.py @@ -2,6 +2,7 @@ from .dataset import DataSet from base import webqtlConfig +from utility.tools import get_setting from wqflask.database import database_connection class PhenotypeDataSet(DataSet): @@ -96,7 +97,7 @@ SELECT InbredSet.Name, InbredSet.Id, InbredSet.GeneticType, InbredSet.InbredSetC this_trait.LRS_location_repr = "N/A" if this_trait.lrs: - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT Geno.Chr, Geno.Mb FROM " "Geno, Species WHERE " @@ -113,7 +114,7 @@ SELECT InbredSet.Name, InbredSet.Id, InbredSet.GeneticType, InbredSet.InbredSetC LRS_Chr, float(LRS_Mb)) def retrieve_sample_data(self, trait): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT Strain.Name, PublishData.value, " "PublishSE.error, NStrain.count, " diff --git a/wqflask/base/data_set/probably_unused.py b/wqflask/base/data_set/probably_unused.py index 4e54bcff..abd3ad07 100644 --- a/wqflask/base/data_set/probably_unused.py +++ b/wqflask/base/data_set/probably_unused.py @@ -3,6 +3,7 @@ import pickle as pickle from wqflask.database import database_connection +from utility.tools import get_setting def create_datasets_list(): if USE_REDIS: @@ -19,7 +20,7 @@ def create_datasets_list(): 'Geno': 'GenoFreeze'} for dataset_type in type_dict: - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute("SELECT Name FROM %s", (type_dict[dataset_type],)) results = cursor.fetchall(query) diff --git a/wqflask/base/data_set/utils.py b/wqflask/base/data_set/utils.py index 703fee04..d18180c4 100644 --- a/wqflask/base/data_set/utils.py +++ b/wqflask/base/data_set/utils.py @@ -7,12 +7,12 @@ import hashlib from typing import List -from utility.tools import SQL_URI +from utility.tools import get_setting, SQL_URI from base.webqtlConfig import TMPDIR from wqflask.database import parse_db_url, database_connection def geno_mrna_confidentiality(ob): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT confidentiality, " f"AuthorisedUsers FROM {ob.type}Freeze WHERE Name = %s", @@ -26,7 +26,7 @@ def query_table_timestamp(dataset_type: str): """function to query the update timestamp of a given dataset_type""" # computation data and actions - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: fetch_db_name = parse_db_url(SQL_URI) cursor.execute( "SELECT UPDATE_TIME FROM " diff --git a/wqflask/base/trait.py b/wqflask/base/trait.py index 37085448..103ff0c0 100644 --- a/wqflask/base/trait.py +++ b/wqflask/base/trait.py @@ -7,7 +7,7 @@ from base import webqtlConfig from base.webqtlCaseData import webqtlCaseData from base.data_set import create_dataset from utility.authentication_tools import check_resource_availability -from utility.tools import GN2_BASE_URL +from utility.tools import get_setting, GN2_BASE_URL from utility.redis_tools import get_redis_conn, get_resource_id from flask import g, request, url_for @@ -394,7 +394,7 @@ def retrieve_trait_info(trait, dataset, get_qtl_info=False): if not dataset: raise ValueError("Dataset doesn't exist") - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: trait_info = () if dataset.type == 'Publish': cursor.execute( diff --git a/wqflask/db/webqtlDatabaseFunction.py b/wqflask/db/webqtlDatabaseFunction.py index 878792b4..b09cf377 100644 --- a/wqflask/db/webqtlDatabaseFunction.py +++ b/wqflask/db/webqtlDatabaseFunction.py @@ -22,12 +22,14 @@ from wqflask.database import database_connection +from utility.tools import get_setting + def retrieve_species(group): """Get the species of a group (e.g. returns string "mouse" on "BXD" """ - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT Species.Name FROM Species, InbredSet WHERE InbredSet.Name = %s AND InbredSet.SpeciesId = Species.Id", (group,)) @@ -37,7 +39,7 @@ def retrieve_species(group): def retrieve_species_id(group): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute("SELECT SpeciesId FROM InbredSet WHERE Name = %s", (group,)) return cursor.fetchone()[0] diff --git a/wqflask/maintenance/gen_select_dataset.py b/wqflask/maintenance/gen_select_dataset.py index 9f4b670d..01b2fc15 100644 --- a/wqflask/maintenance/gen_select_dataset.py +++ b/wqflask/maintenance/gen_select_dataset.py @@ -37,7 +37,7 @@ sys.path.insert(0, './') # NEW: import app to avoid a circular dependency on utility.tools from wqflask import app -from utility.tools import locate, locate_ignore_error, TEMPDIR, SQL_URI +from utility.tools import get_setting import simplejson as json import urllib.parse @@ -48,21 +48,6 @@ from pprint import pformat as pf from wqflask.database import database_connection -def parse_db_uri(): - """Converts a database URI to the db name, host name, user name, and password""" - - parsed_uri = urllib.parse.urlparse(SQL_URI) - - db_conn_info = dict( - db=parsed_uri.path[1:], - host=parsed_uri.hostname, - user=parsed_uri.username, - passwd=parsed_uri.password) - - print(db_conn_info) - return db_conn_info - - def get_species(cursor): """Build species list""" #cursor.execute("select Name, MenuName from Species where Species.Name != 'macaque monkey' order by OrderId") @@ -268,8 +253,6 @@ def build_datasets(species, group, type_name): def main(cursor): """Generates and outputs (as json file) the data for the main dropdown menus on the home page""" - parse_db_uri() - species = get_species(cursor) groups = get_groups(cursor, species) types = get_types(groups) @@ -308,6 +291,6 @@ def _test_it(): if __name__ == '__main__': - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: with conn.cursor() as cursor: main(cursor) diff --git a/wqflask/maintenance/generate_probesetfreeze_file.py b/wqflask/maintenance/generate_probesetfreeze_file.py index f43f952b..2f917c71 100644 --- a/wqflask/maintenance/generate_probesetfreeze_file.py +++ b/wqflask/maintenance/generate_probesetfreeze_file.py @@ -12,6 +12,7 @@ from base import webqtlConfig from pprint import pformat as pf +from utility.tools import get_setting from wqflask.database import database_connection @@ -107,7 +108,7 @@ def main(): "(Oct08)_RankInv_Beta.txt") dataset_name = "Eye_AXBXA_1008_RankInv" - with database_connection as conn: + with database_connection(get_setting("SQL_URI")) as conn: with conn.cursor() as cursor: strains = get_strains(cursor) print("Getting probset_vals") diff --git a/wqflask/maintenance/quantile_normalize.py b/wqflask/maintenance/quantile_normalize.py index 90ec72de..36049a82 100644 --- a/wqflask/maintenance/quantile_normalize.py +++ b/wqflask/maintenance/quantile_normalize.py @@ -9,21 +9,7 @@ from flask import Flask, g, request from wqflask import app from wqflask.database import database_connection - - -def parse_db_uri(): - """Converts a database URI to the db name, host name, user name, and password""" - - parsed_uri = urllib.parse.urlparse(SQL_URI) - - db_conn_info = dict( - db=parsed_uri.path[1:], - host=parsed_uri.hostname, - user=parsed_uri.username, - passwd=parsed_uri.password) - - print(db_conn_info) - return db_conn_info +from utility.tools import get_setting def create_dataframe(input_file): @@ -99,7 +85,7 @@ def set_data(cursor, dataset_name): if __name__ == '__main__': - with database_connection as conn: + with database_connection(get_setting("SQL_URI")) as conn: with conn.cursor() as cursor: success, _ = bulk(es, set_data(cursor, sys.argv[1])) diff --git a/wqflask/maintenance/set_resource_defaults.py b/wqflask/maintenance/set_resource_defaults.py index 0d9372ff..cebe33c0 100644 --- a/wqflask/maintenance/set_resource_defaults.py +++ b/wqflask/maintenance/set_resource_defaults.py @@ -26,7 +26,7 @@ sys.path.insert(0, './') from wqflask import app from utility import hmac -from utility.tools import SQL_URI +from utility.tools import get_setting from utility.redis_tools import get_redis_conn, get_user_id, add_resource, get_resources, get_resource_info Redis = get_redis_conn() @@ -35,21 +35,6 @@ import urllib.parse from wqflask.database import database_connection -def parse_db_uri(): - """Converts a database URI to the db name, host name, user name, and password""" - - parsed_uri = urllib.parse.urlparse(SQL_URI) - - db_conn_info = dict( - db=parsed_uri.path[1:], - host=parsed_uri.hostname, - user=parsed_uri.username, - passwd=parsed_uri.password) - - print(db_conn_info) - return db_conn_info - - def insert_probeset_resources(cursor, default_owner_id): current_resources = Redis.hgetall("resources") cursor.execute(""" SELECT @@ -163,6 +148,6 @@ def main(cursor): if __name__ == '__main__': - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: with conn.cursor() as cursor: main(cursor) diff --git a/wqflask/utility/authentication_tools.py b/wqflask/utility/authentication_tools.py index 7d80b3fb..5d00d600 100644 --- a/wqflask/utility/authentication_tools.py +++ b/wqflask/utility/authentication_tools.py @@ -9,7 +9,7 @@ from utility.redis_tools import (get_redis_conn, get_resource_info, get_resource_id, add_resource) -from utility.tools import GN_PROXY_URL +from utility.tools import get_setting, GN_PROXY_URL Redis = get_redis_conn() @@ -82,7 +82,7 @@ def add_new_resource(dataset, trait_id=None): def get_group_code(dataset): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT InbredSetCode FROM InbredSet WHERE Name=%s", (dataset.group.name,) diff --git a/wqflask/utility/helper_functions.py b/wqflask/utility/helper_functions.py index 0c0794dc..8b20bd74 100644 --- a/wqflask/utility/helper_functions.py +++ b/wqflask/utility/helper_functions.py @@ -3,6 +3,7 @@ from base.trait import create_trait from base.species import TheSpecies from utility import hmac +from utility.tools import get_setting from wqflask.database import database_connection @@ -51,7 +52,7 @@ def get_species_groups(): """Group each species into a group""" _menu = {} species, group_name = None, None - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT s.MenuName, i.InbredSetName FROM InbredSet i " "INNER JOIN Species s ON s.SpeciesId = i.SpeciesId " diff --git a/wqflask/wqflask/api/correlation.py b/wqflask/wqflask/api/correlation.py index 9fafc881..f3a06e19 100644 --- a/wqflask/wqflask/api/correlation.py +++ b/wqflask/wqflask/api/correlation.py @@ -4,6 +4,7 @@ import scipy from base import data_set from base.trait import create_trait, retrieve_sample_data from utility import corr_result_helpers +from utility.tools import get_setting from wqflask.correlation import correlation_functions from wqflask.database import database_connection @@ -127,7 +128,7 @@ def do_literature_correlation_for_all_traits(this_trait, target_dataset, trait_g if mouse_gene_id and str(mouse_gene_id).find(";") == -1: result = "" - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: with conn.cursor() as cursor: cursor.execute( ("SELECT value FROM LCorrRamin3 " @@ -198,7 +199,7 @@ def convert_to_mouse_gene_id(species=None, gene_id=None): return None mouse_gene_id = None - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: with conn.cursor() as cursor: if species == 'mouse': mouse_gene_id = gene_id diff --git a/wqflask/wqflask/api/router.py b/wqflask/wqflask/api/router.py index d7eeac34..0218f207 100644 --- a/wqflask/wqflask/api/router.py +++ b/wqflask/wqflask/api/router.py @@ -21,7 +21,7 @@ from wqflask import app from wqflask.api import correlation, mapping, gen_menu -from utility.tools import flat_files +from utility.tools import flat_files, get_setting from wqflask.database import database_connection @@ -37,7 +37,7 @@ def hello_world(): @app.route("/api/v_{}/species".format(version)) def get_species_list(): species_list = [] - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT SpeciesId, Name, FullName, TaxonomyId FROM Species" ) @@ -55,7 +55,7 @@ def get_species_list(): @app.route("/api/v_{}/species/".format(version)) @app.route("/api/v_{}/species/.".format(version)) def get_species_info(species_name, file_format="json"): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT SpeciesId, Name, FullName, TaxonomyId " "FROM Species WHERE (Name=%s OR FullName=%s " @@ -75,7 +75,7 @@ def get_species_info(species_name, file_format="json"): @app.route("/api/v_{}/groups/".format(version)) def get_groups_list(species_name=None): _groups = () - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: if species_name: cursor.execute( "SELECT InbredSet.InbredSetId, " @@ -123,7 +123,7 @@ def get_groups_list(species_name=None): @app.route("/api/v_{}/group//.".format(version)) def get_group_info(group_name, species_name=None, file_format="json"): group = () - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: if species_name: cursor.execute( "SELECT InbredSet.InbredSetId, InbredSet.SpeciesId, " @@ -176,7 +176,7 @@ def get_group_info(group_name, species_name=None, file_format="json"): @app.route("/api/v_{}/datasets//".format(version)) def get_datasets_for_group(group_name, species_name=None): _datasets = () - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: if species_name: cursor.execute( "SELECT ProbeSetFreeze.Id, ProbeSetFreeze.ProbeFreezeId, " @@ -270,7 +270,7 @@ def get_dataset_info(dataset_name, group_name=None, file_format="json"): (ProbeSetFreeze.Name = "{0}" OR ProbeSetFreeze.Name2 = "{0}" OR ProbeSetFreeze.FullName = "{0}" OR ProbeSetFreeze.ShortName = "{0}") """.format(dataset_name) - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute(f"{probeset_query}{where_statement}") if dataset := cursor.fetchone(): @@ -447,7 +447,7 @@ def fetch_traits(dataset_name, file_format="json"): if 'limit_to' in request.args: limit_number = request.args['limit_to'] query += "LIMIT " + str(limit_number) - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: if file_format == "json": filename = dataset_name + "_traits.json" cursor.execute(query.format(dataset_id)) @@ -570,7 +570,7 @@ def all_sample_data(dataset_name, file_format="csv"): header_list.append("id") header_list += sample_list results_list.append(header_list) - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: for i, trait_id in enumerate(trait_ids): line_list = [] line_list.append(str(trait_names[i])) @@ -604,7 +604,7 @@ def all_sample_data(dataset_name, file_format="csv"): @app.route("/api/v_{}/sample_data//".format(version)) @app.route("/api/v_{}/sample_data//.".format(version)) def trait_sample_data(dataset_name, trait_name, file_format="json"): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT Strain.Name, Strain.Name2, " "ProbeSetData.value, ProbeSetData.Id, " @@ -693,7 +693,7 @@ def trait_sample_data(dataset_name, trait_name, file_format="json"): @app.route("/api/v_{}/trait_info//".format(version)) @app.route("/api/v_{}/trait_info//.".format(version)) def get_trait_info(dataset_name, trait_name, file_format="json"): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT ProbeSet.Id, ProbeSet.Name, ProbeSet.Symbol, " "ProbeSet.description, ProbeSet.Chr, ProbeSet.Mb, " @@ -892,7 +892,7 @@ def get_genotypes(group_name, file_format="csv", dataset_name=None): @app.route("/api/v_{}/gen_dropdown".format(version), methods=("GET",)) def gen_dropdown_menu(): - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: results = gen_menu.gen_dropdown_json(conn) if len(results) > 0: @@ -920,7 +920,7 @@ def get_dataset_trait_ids(dataset_name, start_vars): limit_string = "LIMIT " + str(start_vars['limit_to']) else: limit_string = "" - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: if "Geno" in dataset_name: data_type = "Geno" # ZS: Need to pass back the dataset type cursor.execute( @@ -981,7 +981,7 @@ def get_dataset_trait_ids(dataset_name, start_vars): def get_samplelist(dataset_name): group_id = get_group_id_from_dataset(dataset_name) - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT Strain.Name FROM Strain, StrainXRef " "WHERE StrainXRef.StrainId = Strain.Id AND " @@ -994,7 +994,7 @@ def get_samplelist(dataset_name): def get_group_id_from_dataset(dataset_name): result = () - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: if "Publish" in dataset_name: cursor.execute( "SELECT InbredSet.Id FROM " @@ -1027,7 +1027,7 @@ def get_group_id_from_dataset(dataset_name): def get_group_id(group_name): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT InbredSet.Id FROM InbredSet " "WHERE InbredSet.Name = %s", diff --git a/wqflask/wqflask/correlation/correlation_functions.py b/wqflask/wqflask/correlation/correlation_functions.py index 5c01b0ac..75399256 100644 --- a/wqflask/wqflask/correlation/correlation_functions.py +++ b/wqflask/wqflask/correlation/correlation_functions.py @@ -26,6 +26,7 @@ from base.mrna_assay_tissue_data import MrnaAssayTissueData from gn3.computations.correlations import compute_corr_coeff_p_value from wqflask.database import database_connection +from utility.tools import get_setting ##################################################################################### # Input: primaryValue(list): one list of expression values of one probeSet, @@ -60,7 +61,7 @@ def cal_zero_order_corr_for_tiss(primary_values, target_values, method="pearson" def get_trait_symbol_and_tissue_values(symbol_list=None): - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: tissue_data = MrnaAssayTissueData(gene_symbols=symbol_list, conn=conn) if len(tissue_data.gene_symbols) > 0: results = tissue_data.get_symbol_values_pairs() diff --git a/wqflask/wqflask/db_info.py b/wqflask/wqflask/db_info.py index 3dff422f..7eae86e7 100644 --- a/wqflask/wqflask/db_info.py +++ b/wqflask/wqflask/db_info.py @@ -5,6 +5,7 @@ import re from MySQLdb.cursors import DictCursor from wqflask.database import database_connection +from utility.tools import get_setting class InfoPage: @@ -74,7 +75,7 @@ class InfoPage: raise ValueError('No correct parameter found') results = {} - with database_connection() as conn, conn.cursor(DictCursor) as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor(DictCursor) as cursor: if self.gn_accession_id: cursor.execute(f"{query_base}GN_AccesionId = %s", (self.gn_accession_id,)) diff --git a/wqflask/wqflask/do_search.py b/wqflask/wqflask/do_search.py index fbeb7a49..9bc92247 100644 --- a/wqflask/wqflask/do_search.py +++ b/wqflask/wqflask/do_search.py @@ -11,7 +11,7 @@ from pprint import pformat as pf import sys from db import webqtlDatabaseFunction -from utility.tools import GN2_BASE_URL +from utility.tools import get_setting, GN2_BASE_URL class DoSearch: @@ -37,7 +37,7 @@ class DoSearch: def execute(self, query): """Executes query and returns results""" query = self.normalize_spaces(query) - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute(query) return cursor.fetchall() @@ -950,7 +950,7 @@ if __name__ == "__main__": from wqflask.database import database_connection - with database_connection() as db_conn: + with database_connection(get_setting("SQL_URI")) as db_conn: with db_conn.cursor() as cursor: dataset_name = "HC_M2_0606_P" dataset = create_dataset(db_conn, dataset_name) diff --git a/wqflask/wqflask/docs.py b/wqflask/wqflask/docs.py index c0a07ae2..5d20ce4d 100644 --- a/wqflask/wqflask/docs.py +++ b/wqflask/wqflask/docs.py @@ -2,12 +2,13 @@ import codecs from flask import g from wqflask.database import database_connection +from utility.tools import get_setting class Docs: def __init__(self, entry, start_vars={}): results = None - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute("SELECT Docs.title, CAST(Docs.content AS BINARY) " "FROM Docs WHERE Docs.entry LIKE %s", (str(entry),)) result = cursor.fetchone() @@ -33,7 +34,7 @@ def update_text(start_vars): '"', '\\"').replace("'", "\\'") try: if g.user_session.record.get('user_email_address') in ["zachary.a.sloan@gmail.com", "labwilliams@gmail.com"]: - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: # Disable updates fully - all docs should be in markdown - please move them there, just like the Environments doc cursor.execute("UPDATEX Docs SET content=%s WHERE entry=%s", (content, start_vars.get("entry_type"),)) diff --git a/wqflask/wqflask/external_tools/send_to_geneweaver.py b/wqflask/wqflask/external_tools/send_to_geneweaver.py index e1178720..94407ae8 100644 --- a/wqflask/wqflask/external_tools/send_to_geneweaver.py +++ b/wqflask/wqflask/external_tools/send_to_geneweaver.py @@ -19,6 +19,7 @@ # This module is used by GeneNetwork project (www.genenetwork.org) from wqflask.database import database_connection from utility import helper_functions +from utility.tools import get_setting class SendToGeneWeaver: @@ -62,7 +63,7 @@ def get_trait_name_list(trait_list): def test_chip(trait_list): final_chip_name = "" - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: for trait_db in trait_list: dataset = trait_db[1] cursor.execute( diff --git a/wqflask/wqflask/external_tools/send_to_webgestalt.py b/wqflask/wqflask/external_tools/send_to_webgestalt.py index bc48e6d0..3e0ad2d9 100644 --- a/wqflask/wqflask/external_tools/send_to_webgestalt.py +++ b/wqflask/wqflask/external_tools/send_to_webgestalt.py @@ -22,6 +22,7 @@ from wqflask.database import database_connection from base.trait import retrieve_trait_info from utility import helper_functions +from utility.tools import get_setting class SendToWebGestalt: @@ -68,7 +69,7 @@ class SendToWebGestalt: def test_chip(trait_list): final_chip_name = "" - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: for trait_db in trait_list: dataset = trait_db[1] cursor.execute( diff --git a/wqflask/wqflask/heatmap/heatmap.py b/wqflask/wqflask/heatmap/heatmap.py index 8ef85d3c..43ea4a4d 100644 --- a/wqflask/wqflask/heatmap/heatmap.py +++ b/wqflask/wqflask/heatmap/heatmap.py @@ -10,6 +10,7 @@ from redis import Redis from flask import Flask, g from wqflask.database import database_connection +from utility.tools import get_setting Redis = Redis() @@ -33,7 +34,7 @@ class Heatmap: chrnames = [] self.species = species.TheSpecies(dataset=self.trait_list[0][1]) - with database_connection() as conn, conn.cursor() as db_cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as db_cursor: for this_chr in self.species.chromosomes.chromosomes(db_cursor): chrnames.append([self.species.chromosomes.chromosomes(db_cursor)[this_chr].name, self.species.chromosomes.chromosomes(db_cursor)[this_chr].mb_length]) diff --git a/wqflask/wqflask/interval_analyst/GeneUtil.py b/wqflask/wqflask/interval_analyst/GeneUtil.py index 5e3e9c9d..72bd8bae 100644 --- a/wqflask/wqflask/interval_analyst/GeneUtil.py +++ b/wqflask/wqflask/interval_analyst/GeneUtil.py @@ -2,7 +2,7 @@ import string from wqflask.database import database_connection -from utility.tools import flat_files +from utility.tools import flat_files, get_setting def load_homology(chr_name, start_mb, end_mb, source_file): homology_list = [] @@ -52,7 +52,7 @@ def loadGenes(chrName, diffCol, startMb, endMb, species='mouse'): # List All Species in the Gene Table speciesDict = {} results = [] - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute("SELECT Species.Name, GeneList081722.SpeciesId " "FROM Species, GeneList081722 WHERE " "GeneList081722.SpeciesId = Species.Id " diff --git a/wqflask/wqflask/marker_regression/display_mapping_results.py b/wqflask/wqflask/marker_regression/display_mapping_results.py index bf89b0db..e371878b 100644 --- a/wqflask/wqflask/marker_regression/display_mapping_results.py +++ b/wqflask/wqflask/marker_regression/display_mapping_results.py @@ -40,6 +40,7 @@ from base import webqtlConfig from base.GeneralObject import GeneralObject from utility import webqtlUtil from utility import Plot +from utility.tools import get_setting from wqflask.interval_analyst import GeneUtil from base.webqtlConfig import GENERATED_IMAGE_DIR from utility.pillow_utils import draw_rotated_text, draw_open_polygon @@ -464,7 +465,7 @@ class DisplayMappingResults: elif self.dataset.group.species == "rat" and indChr.name == "21": self.ChrList.append(("X", i)) self.ChrList.append((indChr.name, i)) - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute("SELECT Length FROM Chr_Length, InbredSet " "WHERE Chr_Length.SpeciesId = InbredSet.SpeciesId " "AND InbredSet.Name = %s AND Chr_Length.Name IN " @@ -515,7 +516,7 @@ class DisplayMappingResults: self.diffCol = [] for i, strain in enumerate(self.diffCol): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute("SELECT Id FROM Strain WHERE Symbol = %s", (strain,)) if result := cursor.fetchone(): @@ -1033,7 +1034,7 @@ class DisplayMappingResults: SNPCounts = [] while startMb < endMb: - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: # snp count cursor.execute("SELECT COUNT(*) FROM BXDSnpPosition " "WHERE Chr = %s AND Mb >= %s AND Mb < %s AND " diff --git a/wqflask/wqflask/marker_regression/gemma_mapping.py b/wqflask/wqflask/marker_regression/gemma_mapping.py index 4420796c..5d424c97 100644 --- a/wqflask/wqflask/marker_regression/gemma_mapping.py +++ b/wqflask/wqflask/marker_regression/gemma_mapping.py @@ -12,6 +12,7 @@ from utility.tools import flat_files, assert_file from utility.tools import GEMMA_WRAPPER_COMMAND from utility.tools import TEMPDIR from utility.tools import WEBSERVER_MODE +from utility.tools import get_setting from wqflask.database import database_connection from gn3.computations.gemma import generate_hash_of_string @@ -51,7 +52,7 @@ def run_gemma(this_trait, this_dataset, samples, vals, covariates, use_loco, this_chromosomes_name = [] - with database_connection() as conn, conn.cursor() as db_cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as db_cursor: for this_chr in this_dataset.species.chromosomes.chromosomes(db_cursor): this_chromosomes_name.append(this_dataset.species.chromosomes.chromosomes(db_cursor)[this_chr].name) diff --git a/wqflask/wqflask/marker_regression/rqtl_mapping.py b/wqflask/wqflask/marker_regression/rqtl_mapping.py index 9a42bc35..f6ef2e9a 100644 --- a/wqflask/wqflask/marker_regression/rqtl_mapping.py +++ b/wqflask/wqflask/marker_regression/rqtl_mapping.py @@ -14,7 +14,7 @@ import numpy as np from base.webqtlConfig import TMPDIR from base.trait import create_trait from utility.redis_tools import get_redis_conn -from utility.tools import locate, GN3_LOCAL_URL +from utility.tools import locate, get_setting, GN3_LOCAL_URL from wqflask.database import database_connection @@ -78,7 +78,7 @@ def write_covarstruct_file(cofactors: str) -> str: and the second column indicates whether they're numerical or categorical """ trait_datatype_json = None - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute("SELECT value FROM TraitMetadata WHERE type='trait_data_type'") trait_datatype_json = json.loads(cursor.fetchone()[0]) diff --git a/wqflask/wqflask/marker_regression/run_mapping.py b/wqflask/wqflask/marker_regression/run_mapping.py index 952d9749..98a97d8d 100644 --- a/wqflask/wqflask/marker_regression/run_mapping.py +++ b/wqflask/wqflask/marker_regression/run_mapping.py @@ -35,7 +35,7 @@ from wqflask.database import database_connection from wqflask.marker_regression import gemma_mapping, rqtl_mapping, qtlreaper_mapping, plink_mapping from wqflask.show_trait.SampleList import SampleList -from utility.tools import locate, locate_ignore_error, GEMMA_COMMAND, PLINK_COMMAND, TEMPDIR +from utility.tools import locate, get_setting, locate_ignore_error, GEMMA_COMMAND, PLINK_COMMAND, TEMPDIR from utility.external import shell from base.webqtlConfig import TMPDIR, GENERATED_TEXT_DIR @@ -669,7 +669,7 @@ def geno_db_exists(this_dataset): def get_chr_lengths(mapping_scale, mapping_method, dataset, qtl_results): chr_lengths = [] if mapping_scale == "physic": - with database_connection() as conn, conn.cursor() as db_cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as db_cursor: for i, the_chr in enumerate(dataset.species.chromosomes.chromosomes(db_cursor)): this_chr = { "chr": dataset.species.chromosomes.chromosomes(db_cursor)[the_chr].name, diff --git a/wqflask/wqflask/metadata_edits.py b/wqflask/wqflask/metadata_edits.py index 06eca376..ca7e8ae7 100644 --- a/wqflask/wqflask/metadata_edits.py +++ b/wqflask/wqflask/metadata_edits.py @@ -130,7 +130,8 @@ def edit_probeset(conn, name): @required_access( ("group:resource:view-resource", "group:resource:edit-resource")) def display_phenotype_metadata(dataset_id: str, name: str): - with database_connection() as conn: + from utility.tools import get_setting + with database_connection(get_setting("SQL_URI")) as conn: _d = edit_phenotype(conn=conn, name=name, dataset_id=dataset_id) group_name = retrieve_group_name(dataset_id, conn) @@ -155,7 +156,8 @@ def display_phenotype_metadata(dataset_id: str, name: str): @required_access( ("group:resource:view-resource", "group:resource:edit-resource")) def display_probeset_metadata(name: str): - with database_connection() as conn: + from utility.tools import get_setting + with database_connection(get_setting("SQL_URI")) as conn: _d = edit_probeset(conn=conn, name=name) return render_template( "edit_probeset.html", @@ -171,6 +173,7 @@ def display_probeset_metadata(name: str): @required_access( ("group:resource:view-resource", "group:resource:edit-resource")) def update_phenotype(dataset_id: str, name: str): + from utility.tools import get_setting data_ = request.form.to_dict() TMPDIR = current_app.config.get("TMPDIR") author = ( @@ -195,7 +198,7 @@ def update_phenotype(dataset_id: str, name: str): f"{author}.{request.args.get('resource-id')}." f"{current_time}" ) diff_data = {} - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: group_name = retrieve_group_name(dataset_id, conn) sample_list = retrieve_sample_list(group_name) headers = ["Strain Name", "Value", "SE", "Count"] @@ -289,7 +292,7 @@ View the diffs here", "success") "authorized_users": data_.get("authorized-users"), } updated_phenotypes = "" - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: updated_phenotypes = update( conn, "Phenotype", @@ -323,7 +326,7 @@ View the diffs here", "success") "year": data_.get("year"), } updated_publications = "" - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: existing_publication = fetchone( conn=conn, @@ -372,7 +375,7 @@ View the diffs here", "success") ), } ) - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: insert( conn, table="metadata_audit", @@ -395,7 +398,8 @@ View the diffs here", "success") ("group:resource:view-resource", "group:resource:edit-resource"), dataset_key="dataset_id", trait_key="name") def update_probeset(name: str): - with database_connection() as conn: + from utility.tools import get_setting + with database_connection(get_setting("SQL_URI")) as conn: data_ = request.form.to_dict() probeset_ = { "id_": data_.get("id"), @@ -486,7 +490,8 @@ def update_probeset(name: str): @metadata_edit.route("//traits//csv") @login_required def get_sample_data_as_csv(dataset_id: str, phenotype_id: int): - with database_connection() as conn: + from utility.tools import get_setting + with database_connection(get_setting("SQL_URI")) as conn: return Response( get_trait_csv_sample_data( conn=conn, @@ -554,8 +559,9 @@ def show_diff(name): @metadata_edit.route("//traits//history") @metadata_edit.route("/probeset/") def show_history(dataset_id: str = "", name: str = ""): + from utility.tools import get_setting diff_data_ = None - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: json_data = None if dataset_id: # This is a published phenotype json_data = fetchall( @@ -630,13 +636,14 @@ def reject_data(resource_id: str, file_name: str): @edit_admins_access_required @login_required def approve_data(resource_id: str, file_name: str): + from utility.tools import get_setting sample_data = {file_name: str} TMPDIR = current_app.config.get("TMPDIR") with open( os.path.join(f"{TMPDIR}/sample-data/diffs", file_name), "r" ) as myfile: sample_data = json.load(myfile) - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: for modification in ( modifications := [d for d in sample_data.get("Modifications")] ): @@ -653,7 +660,7 @@ def approve_data(resource_id: str, file_name: str): ) n_deletions = 0 - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: for data in [d for d in sample_data.get("Deletions")]: __deletions = delete_sample_data( conn=conn, @@ -671,7 +678,7 @@ def approve_data(resource_id: str, file_name: str): sample_data.get("Deletions").remove(data) n_insertions = 0 - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: for data in [d for d in sample_data.get("Additions")]: if insert_sample_data( conn=conn, @@ -690,7 +697,7 @@ def approve_data(resource_id: str, file_name: str): sample_data.get("Deletions"), ] ): - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: insert( conn, table="metadata_audit", @@ -750,4 +757,4 @@ def create_delta_csv(base_csv, form_data, sample_list): else: delta_csv_lines.append(line) - return "\n".join(delta_csv_lines) \ No newline at end of file + return "\n".join(delta_csv_lines) diff --git a/wqflask/wqflask/partial_correlations_views.py b/wqflask/wqflask/partial_correlations_views.py index a11d902c..c680d913 100644 --- a/wqflask/wqflask/partial_correlations_views.py +++ b/wqflask/wqflask/partial_correlations_views.py @@ -14,7 +14,7 @@ from flask import ( render_template) from wqflask import app -from utility.tools import GN_SERVER_URL +from utility.tools import get_setting, GN_SERVER_URL from wqflask.database import database_connection from gn3.db.partial_correlations import traits_info @@ -309,7 +309,7 @@ def partial_correlations(): for error in args["errors"]: flash(error, "alert-danger") - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: target_dbs = target_databases(conn, traits, threshold=0) return render_template( "partial_correlations/pcorrs_select_operations.html", diff --git a/wqflask/wqflask/search_results.py b/wqflask/wqflask/search_results.py index 6222dd88..f661aba7 100644 --- a/wqflask/wqflask/search_results.py +++ b/wqflask/wqflask/search_results.py @@ -17,7 +17,7 @@ from wqflask.database import database_connection from utility import hmac from utility.authentication_tools import check_resource_availability -from utility.tools import GN2_BASE_URL +from utility.tools import get_setting, GN2_BASE_URL from utility.type_checking import is_str @@ -381,7 +381,7 @@ def trait_info_str(trait, dataset_type): def get_GO_symbols(a_search): gene_list = None - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute("SELECT genes FROM GORef WHERE goterm=%s", (f"{a_search['key']}:{a_search['search_term'][0]}",)) gene_list = cursor.fetchone()[0].strip().split() diff --git a/wqflask/wqflask/show_trait/SampleList.py b/wqflask/wqflask/show_trait/SampleList.py index 44b89671..0f99243e 100644 --- a/wqflask/wqflask/show_trait/SampleList.py +++ b/wqflask/wqflask/show_trait/SampleList.py @@ -7,6 +7,7 @@ from pprint import pformat as pf from utility import Plot from utility import Bunch +from utility.tools import get_setting class SampleList: def __init__(self, @@ -122,7 +123,7 @@ class SampleList: """Finds which extra attributes apply to this dataset""" # Get attribute names and distinct values for each attribute - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT DISTINCT CaseAttribute.Id, " "CaseAttribute.Name, CaseAttribute.Description, " @@ -161,7 +162,7 @@ class SampleList: def get_extra_attribute_values(self): if self.attributes: - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT Strain.Name AS SampleName, " "CaseAttributeId AS Id, " diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index ae5e1ebb..0fbd1a32 100644 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -18,7 +18,7 @@ from wqflask.show_trait.SampleList import SampleList from base.trait import create_trait from base import data_set from utility import helper_functions -from utility.tools import locate_ignore_error +from utility.tools import get_setting, locate_ignore_error from utility.tools import GN_PROXY_URL from utility.redis_tools import get_redis_conn, get_resource_id @@ -392,7 +392,7 @@ class ShowTrait: if self.dataset.group.species == "mouse": self.aba_link = webqtlConfig.ABA_URL % self.this_trait.symbol results = () - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT chromosome, txStart, txEnd FROM " "GeneList WHERE geneSymbol = %s", @@ -419,7 +419,7 @@ class ShowTrait: "rattus-norvegicus", self.this_trait.symbol) results = () - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT kgID, chromosome, txStart, txEnd " "FROM GeneList_rn33 WHERE geneSymbol = %s", @@ -621,7 +621,7 @@ def get_nearest_marker(this_trait, this_db): this_mb = this_trait.locus_mb # One option is to take flanking markers, another is to take the # two (or one) closest - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute( "SELECT Geno.Name FROM Geno, GenoXRef, " "GenoFreeze WHERE Geno.Chr = %s AND " diff --git a/wqflask/wqflask/snp_browser/snp_browser.py b/wqflask/wqflask/snp_browser/snp_browser.py index 208f1206..59a4dccf 100644 --- a/wqflask/wqflask/snp_browser/snp_browser.py +++ b/wqflask/wqflask/snp_browser/snp_browser.py @@ -6,6 +6,8 @@ from base import webqtlConfig from wqflask.database import database_connection +from utility.tools import get_setting + class SnpBrowser: @@ -152,7 +154,7 @@ class SnpBrowser: self.snp_list = None __query = "" __vars = None - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: if self.gene_name != "": if self.species_id != 0: __query = ("SELECT geneSymbol, chromosome, txStart, " @@ -665,7 +667,7 @@ def get_browser_sample_lists(species_id=1): strain_lists = {} mouse_strain_list = [] rat_strain_list = [] - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: with conn.cursor() as cursor: cursor.execute("SHOW COLUMNS FROM SnpPattern") _mouse_snp_pattern = cursor.fetchall() @@ -885,7 +887,7 @@ def get_gene_id(species_id, gene_name): query = ("SELECT geneId FROM GeneList WHERE " "SpeciesId = %s AND geneSymbol = %s") - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: with conn.cursor() as cursor: cursor.execute(query, (species_id, gene_name)) if (result := cursor.fetchone()): @@ -900,7 +902,7 @@ def get_gene_id_name_dict(species_id, gene_name_list): query = ("SELECT geneId, geneSymbol FROM " "GeneList WHERE SpeciesId = %s AND " f"geneSymbol in ({', '.join(['%s'] * len(gene_name_list))})") - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: with conn.cursor() as cursor: cursor.execute(query, (species_id, *gene_name_list)) results = cursor.fetchall() @@ -911,7 +913,7 @@ def get_gene_id_name_dict(species_id, gene_name_list): def check_if_in_gene(species_id, chr_, mb): - with database_connection() as conn: + with database_connection(get_setting("SQL_URI")) as conn: with conn.cursor() as cursor: if species_id != 0: # ZS: Check if this is necessary cursor.execute( diff --git a/wqflask/wqflask/update_search_results.py b/wqflask/wqflask/update_search_results.py index dcccbf3b..8789c903 100644 --- a/wqflask/wqflask/update_search_results.py +++ b/wqflask/wqflask/update_search_results.py @@ -4,6 +4,7 @@ from base.data_set import create_dataset from base.trait import GeneralTrait from db import webqtlDatabaseFunction from wqflask.database import database_connection +from utility.tools import get_setting class GSearch: @@ -14,7 +15,7 @@ class GSearch: #self.row_range = kw['row_range'] if self.type == "gene": results = None - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: cursor.execute(""" SELECT Species.`Name` AS species_name, InbredSet.`Name` AS inbredset_name, Tissue.`Name` AS tissue_name, ProbeSetFreeze.Name AS probesetfreeze_name, @@ -42,7 +43,7 @@ probesetfreeze_name, probeset_name LIMIT 6000""", self.trait_list.append(this_trait) elif self.type == "phenotype": - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: results = None cursor.execute(""" SELECT Species.`Name`, InbredSet.`Name`, PublishFreeze.`Name`, PublishXRef.`Id`, diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py index e7a32d34..ca2c34cd 100644 --- a/wqflask/wqflask/views.py +++ b/wqflask/wqflask/views.py @@ -82,6 +82,7 @@ from wqflask.oauth2.client import no_token_get from wqflask.oauth2.request_utils import process_error from utility import temp_data +from utility.tools import get_setting from utility.tools import TEMPDIR from utility.tools import USE_REDIS from utility.tools import REDIS_URL @@ -481,7 +482,7 @@ def export_perm_data(): @app.route("/show_temp_trait", methods=('POST',)) def show_temp_trait_page(): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: user_id = ((g.user_session.record.get(b"user_id") or b"").decode("utf-8") or g.user_session.record.get("user_id") or "") template_vars = show_trait.ShowTrait(cursor, @@ -499,7 +500,7 @@ def show_trait_page(): assert len(privileges_data) == 1 privileges_data = privileges_data[0] trait_privileges = tuple(item for item in privileges_data["privileges"]) - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: user_id = ((g.user_session.record.get(b"user_id") or b"").decode("utf-8") or g.user_session.record.get("user_id") or "") @@ -537,7 +538,7 @@ def heatmap_page(): temp_uuid = uuid.uuid4() traits = [trait.strip() for trait in start_vars['trait_list'].split(',')] - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: if traits[0] != "": version = "v5" key = "heatmap:{}:".format( @@ -973,7 +974,7 @@ def corr_scatter_plot_page(): @app.route("/snp_browser", methods=('GET',)) def snp_browser_page(): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: template_vars = snp_browser.SnpBrowser(cursor, request.args) return render_template("snp_browser.html", **template_vars.__dict__) @@ -987,7 +988,7 @@ def db_info_page(): @app.route("/snp_browser_table", methods=('GET',)) def snp_browser_table(): - with database_connection() as conn, conn.cursor() as cursor: + with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor: snp_table_data = snp_browser.SnpBrowser(cursor, request.args) current_page = server_side.ServerSideTable( snp_table_data.rows_count, -- cgit v1.2.3