From eb84f7c0e384e08b810e052fd3935f6d977b7ea2 Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Thu, 16 Jun 2016 17:08:50 +0000 Subject: Introduce behaviour parameters to facilitate development --- wqflask/utility/tools.py | 29 +++++++++++++--------- wqflask/wqflask/search_results.py | 2 +- wqflask/wqflask/views.py | 52 ++++++++++++++++++++++----------------- 3 files changed, 48 insertions(+), 35 deletions(-) (limited to 'wqflask') diff --git a/wqflask/utility/tools.py b/wqflask/utility/tools.py index dd8c4a1e..955f3bdd 100644 --- a/wqflask/utility/tools.py +++ b/wqflask/utility/tools.py @@ -7,12 +7,12 @@ from wqflask import app def get_setting(command_id,guess=None): """Resolve a setting from the environment or the global settings in - app.config, with get_valid_path is a function checking whether the + app.config, with valid_path is a function checking whether the path points to an expected directory and returns the full path to the binary command guess = os.environ.get('HOME')+'/pylmm' - get_setting('PYLMM_PATH',guess) + valid_path(get_setting('PYLMM_PATH',guess)) first tries the environment variable in +id+, next gets the Flask app setting for the same +id+ and finally does an educated @@ -31,13 +31,13 @@ def get_setting(command_id,guess=None): """ def value(command): if command: - sys.stderr.write("Found path "+command+"\n") + # sys.stderr.write("Found "+command+"\n") return command else: return None # ---- Check whether environment exists - sys.stderr.write("Looking for "+command_id+"\n") + # sys.stderr.write("Looking for "+command_id+"\n") command = value(os.environ.get(command_id)) if not command: # ---- Check whether setting exists in app @@ -45,7 +45,8 @@ def get_setting(command_id,guess=None): if not command: command = value(guess) if not command: - raise Exception(command_id+' path unknown or faulty (update settings.py?). '+command_id+' should point to the path') + raise Exception(command_id+' setting unknown or faulty (update settings.py?).') + sys.stderr.write("Set "+command_id+"="+str(command)+"\n") return command def valid_bin(bin): @@ -127,11 +128,17 @@ def locate_ignore_error(name, subdir=None): def tempdir(): return valid_path(get_setting("TEMPDIR","/tmp")) - # Cached values -PYLMM_COMMAND = pylmm_command() -GEMMA_COMMAND = gemma_command() -PLINK_COMMAND = plink_command() -FLAT_FILES = flat_files() -TEMPDIR = tempdir() +WEBSERVER_MODE = get_setting('WEBSERVER_MODE') +LOGGING = get_setting('LOGGING') +DEBUG_LOG_LEVEL = get_setting('DEBUG_LOG_LEVEL') +LOG_SQL = get_setting('LOG_SQL') in [True,'TRUE','True','true'] +USE_REDIS = get_setting('USE_REDIS') in [True,'TRUE','True','true'] + +PYLMM_COMMAND = pylmm_command() +GEMMA_COMMAND = gemma_command() +PLINK_COMMAND = plink_command() +FLAT_FILES = flat_files() +TEMPDIR = tempdir() + diff --git a/wqflask/wqflask/search_results.py b/wqflask/wqflask/search_results.py index 8a0485b3..39f6d62c 100644 --- a/wqflask/wqflask/search_results.py +++ b/wqflask/wqflask/search_results.py @@ -31,7 +31,7 @@ from base.data_set import create_dataset from base.trait import GeneralTrait from wqflask import parser from wqflask import do_search -from utility import webqtlUtil +from utility import webqtlUtil,tools from dbFunction import webqtlDatabaseFunction from utility import formatting diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py index df1f77bc..ba250d4f 100644 --- a/wqflask/wqflask/views.py +++ b/wqflask/wqflask/views.py @@ -52,7 +52,7 @@ from wqflask.wgcna import wgcna_analysis from wqflask.ctl import ctl_analysis from utility import temp_data -from utility.tools import TEMPDIR +from utility.tools import TEMPDIR,USE_REDIS from base import webqtlFormData from base.webqtlConfig import GENERATED_IMAGE_DIR @@ -138,11 +138,16 @@ def search_page(): else: key = "search_results:v1:" + json.dumps(request.args, sort_keys=True) print("key is:", pf(key)) - with Bench("Loading cache"): - result = Redis.get(key) + if USE_REDIS: + with Bench("Trying Redis cache"): + result = Redis.get(key) + else: + print("Skipping Redis cache (USE_REDIS=False)") + result = None if result: - print("Cache hit!!!") + print("Cache hit on search results!!!") + print("USE_REDIS=",USE_REDIS) with Bench("Loading results"): result = pickle.loads(result) else: @@ -152,8 +157,9 @@ def search_page(): result = the_search.__dict__ print("result: ", pf(result)) - Redis.set(key, pickle.dumps(result, pickle.HIGHEST_PROTOCOL)) - Redis.expire(key, 60*60) + if USE_REDIS: + Redis.set(key, pickle.dumps(result, pickle.HIGHEST_PROTOCOL)) + Redis.expire(key, 60*60) if result['search_term_exists']: return render_template("search_result_page.html", **result) @@ -168,7 +174,7 @@ def gsearchact(): return render_template("gsearch_gene.html", **result) elif type == "phenotype": return render_template("gsearch_pheno.html", **result) - + @app.route("/gsearch_updating", methods=('POST',)) def gsearch_updating(): print("REQUEST ARGS:", request.values) @@ -179,7 +185,7 @@ def gsearch_updating(): # return render_template("gsearch_gene_updating.html", **result) # elif type == "phenotype": # return render_template("gsearch_pheno.html", **result) - + @app.route("/docedit") def docedit(): doc = docs.Docs(request.args['entry']) @@ -294,13 +300,13 @@ def export_trait_csv(): return Response(csv_data, mimetype='text/csv', headers={"Content-Disposition":"attachment;filename=sample_data.csv"}) - + @app.route('/export_perm_data', methods=('POST',)) def export_perm_data(): """CSV file consisting of the permutation data for the mapping results""" num_perm = float(request.form['num_perm']) perm_data = json.loads(request.form['perm_results']) - + buff = StringIO.StringIO() writer = csv.writer(buff) writer.writerow(["Suggestive LRS (p=0.63) = " + str(perm_data[int(num_perm*0.37-1)])]) @@ -338,10 +344,10 @@ def show_trait_page(): @app.route("/heatmap", methods=('POST',)) def heatmap_page(): print("In heatmap, request.form is:", pf(request.form)) - + start_vars = request.form temp_uuid = uuid.uuid4() - + traits = [trait.strip() for trait in start_vars['trait_list'].split(',')] if traits[0] != "": version = "v5" @@ -349,33 +355,33 @@ def heatmap_page(): print("key is:", pf(key)) with Bench("Loading cache"): result = Redis.get(key) - + if result: print("Cache hit!!!") with Bench("Loading results"): result = pickle.loads(result) - + else: print("Cache miss!!!") - + template_vars = heatmap.Heatmap(request.form, temp_uuid) template_vars.js_data = json.dumps(template_vars.js_data, default=json_default_handler, indent=" ") - + result = template_vars.__dict__ for item in template_vars.__dict__.keys(): print(" ---**--- {}: {}".format(type(template_vars.__dict__[item]), item)) - + pickled_result = pickle.dumps(result, pickle.HIGHEST_PROTOCOL) print("pickled result length:", len(pickled_result)) Redis.set(key, pickled_result) Redis.expire(key, 60*60) - + with Bench("Rendering template"): rendered_template = render_template("heatmap.html", **result) - + else: rendered_template = render_template("empty_collection.html", **{'tool':'Heatmap'}) @@ -468,18 +474,18 @@ def marker_regression_page(): imgB64 = imgdata.encode("base64") bytesarray = array.array('B', imgB64) result['pair_scan_array'] = bytesarray - rendered_template = render_template("pair_scan_results.html", **result) + rendered_template = render_template("pair_scan_results.html", **result) else: #for item in template_vars.__dict__.keys(): # print(" ---**--- {}: {}".format(type(template_vars.__dict__[item]), item)) - + gn1_template_vars = marker_regression_gn1.MarkerRegression(result).__dict__ pickled_result = pickle.dumps(result, pickle.HIGHEST_PROTOCOL) print("pickled result length:", len(pickled_result)) Redis.set(key, pickled_result) Redis.expire(key, 1*60) - + with Bench("Rendering template"): rendered_template = render_template("marker_regression_gn1.html", **gn1_template_vars) @@ -542,7 +548,7 @@ def corr_matrix_page(): template_vars.js_data = json.dumps(template_vars.js_data, default=json_default_handler, indent=" ") - + return render_template("correlation_matrix.html", **template_vars.__dict__) else: return render_template("empty_collection.html", **{'tool':'Correlation Matrix'}) -- cgit v1.2.3