diff options
Diffstat (limited to 'wqflask/utility')
-rw-r--r-- | wqflask/utility/chunks.py | 96 | ||||
-rw-r--r-- | wqflask/utility/tools.py | 84 | ||||
-rwxr-xr-x | wqflask/utility/webqtlUtil.py | 17 |
3 files changed, 181 insertions, 16 deletions
diff --git a/wqflask/utility/chunks.py b/wqflask/utility/chunks.py new file mode 100644 index 00000000..9565fb96 --- /dev/null +++ b/wqflask/utility/chunks.py @@ -0,0 +1,96 @@ +from __future__ import absolute_import, print_function, division + +import math +import time + + +def divide_into_chunks(the_list, number_chunks): + """Divides a list into approximately number_chunks smaller lists + + >>> divide_into_chunks([1, 2, 7, 3, 22, 8, 5, 22, 333], 3) + [[1, 2, 7], [3, 22, 8], [5, 22, 333]] + >>> divide_into_chunks([1, 2, 7, 3, 22, 8, 5, 22, 333], 4) + [[1, 2, 7], [3, 22, 8], [5, 22, 333]] + >>> divide_into_chunks([1, 2, 7, 3, 22, 8, 5, 22, 333], 5) + [[1, 2], [7, 3], [22, 8], [5, 22], [333]] + >>> + + """ + length = len(the_list) + + if length == 0: + return [[]] + + if length <= number_chunks: + number_chunks = length + + chunksize = int(math.ceil(length / number_chunks)) + + chunks = [] + for counter in range(0, length, chunksize): + chunks.append(the_list[counter:counter+chunksize]) + + return chunks + +def _confirm_chunk(original, result): + all_chunked = [] + for chunk in result: + all_chunked.extend(chunk) + print("length of all chunked:", len(all_chunked)) + assert original == all_chunked, "You didn't chunk right" + + +def _chunk_test(divide_func): + import random + random.seed(7) + + number_exact = 0 + total_amount_off = 0 + + for test in range(1, 1001): + print("\n\ntest:", test) + number_chunks = random.randint(1, 20) + number_elements = random.randint(0, 100) + the_list = list(range(1, number_elements)) + result = divide_func(the_list, number_chunks) + + print("Dividing list of length {} into approximately {} chunks - got {} chunks".format( + len(the_list), number_chunks, len(result))) + print("result:", result) + + _confirm_chunk(the_list, result) + + amount_off = abs(number_chunks - len(result)) + if amount_off == 0: + number_exact += 1 + else: + total_amount_off += amount_off + + + print("\n{} exact out of {} [Total amount off: {}]".format(number_exact, + test, + total_amount_off)) + assert number_exact == 558 + assert total_amount_off == 1580 + return number_exact, total_amount_off + + +def _main(): + info = dict() + #funcs = (("sam", sam_divide_into_chunks), ("zach", zach_divide_into_chunks)) + funcs = (("only one", divide_into_chunks),) + for name, func in funcs: + start = time.time() + number_exact, total_amount_off = _chunk_test(func) + took = time.time() - start + info[name] = dict(number_exact=number_exact, + total_amount_off=total_amount_off, + took=took) + + print("info is:", info) + +if __name__ == '__main__': + _main() + print("\nConfirming doctests...") + import doctest + doctest.testmod() diff --git a/wqflask/utility/tools.py b/wqflask/utility/tools.py new file mode 100644 index 00000000..760ded7c --- /dev/null +++ b/wqflask/utility/tools.py @@ -0,0 +1,84 @@ +# Tools/paths finder resolves external paths from settings and/or environment +# variables +# +# Currently supported: +# +# PYLMM_PATH finds the root of the git repository of the pylmm_gn2 tool + +import os +import sys +from wqflask import app + +def get_setting(id,default,guess,get_valid_path): + """ + Resolve a setting from the environment or the global settings in app.config + """ + # ---- Check whether environment exists + path = get_valid_path(os.environ.get(id)) + # ---- Check whether setting exists + setting = app.config.get(id) + if not path: + path = get_valid_path(setting) + # ---- Check whether default exists + if not path: + path = get_valid_path(default) + # ---- Guess directory + if not path: + if not setting: + setting = guess + path = get_valid_path(guess) + if not path: + raise Exception(id+' '+setting+' path unknown or faulty (update settings.py?). '+id+' should point to the root of the git repository') + + return path + +def pylmm_command(default=None): + """ + Return the path to the repository and the python command to call + """ + def get_valid_path(path): + """Test for a valid repository""" + if path: + sys.stderr.write("Trying PYLMM_PATH in "+path+"\n") + if path and os.path.isfile(path+'/pylmm_gn2/lmm.py'): + return path + else: + None + + guess = os.environ.get('HOME')+'/pylmm_gn2' + path = get_setting('PYLMM_PATH',default,guess,get_valid_path) + pylmm_command = 'python '+path+'/pylmm_gn2/lmm.py' + return path,pylmm_command + +def plink_command(default=None): + """ + Return the path to the repository and the python command to call + """ + def get_valid_path(path): + """Test for a valid repository""" + if path: + sys.stderr.write("Trying PLINK_PATH in "+path+"\n") + if path and os.path.isfile(path+'/plink'): + return path + else: + None + + guess = os.environ.get('HOME')+'/plink' + path = get_setting('PLINK_PATH',default,guess,get_valid_path) + plink_command = path+'/plink' + return path,plink_command + +def gemma_command(default=None): + def get_valid_path(path): + """Test for a valid repository""" + if path: + sys.stderr.write("Trying PLINK_PATH in "+path+"\n") + if path and os.path.isfile(path+'/plink'): + return path + else: + None + + guess = os.environ.get('HOME')+'/plink' + path = get_setting('PLINK_PATH',default,guess,get_valid_path) + gemma_command = path+'/gemma' + return path, gemma_command
\ No newline at end of file diff --git a/wqflask/utility/webqtlUtil.py b/wqflask/utility/webqtlUtil.py index 4d7981d9..f842dde0 100755 --- a/wqflask/utility/webqtlUtil.py +++ b/wqflask/utility/webqtlUtil.py @@ -43,6 +43,7 @@ ParInfo ={ 'BXH':['BHF1', 'HBF1', 'C57BL/6J', 'C3H/HeJ'], 'AKXD':['AKF1', 'KAF1', 'AKR/J', 'DBA/2J'], 'BXD':['B6D2F1', 'D2B6F1', 'C57BL/6J', 'DBA/2J'], +'C57BL-6JxC57BL-6NJF2':['', '', 'C57BL/6J', 'C57BL/6NJ'], 'BXD300':['B6D2F1', 'D2B6F1', 'C57BL/6J', 'DBA/2J'], 'B6BTBRF2':['B6BTBRF1', 'BTBRB6F1', 'C57BL/6J', 'BTBRT<+>tf/J'], 'BHHBF2':['B6HF2','HB6F2','C57BL/6J','C3H/HeJ'], @@ -880,22 +881,6 @@ def cmpGenoPos(A,B): except: return 0 -#XZhou: Must use "BINARY" to enable case sensitive comparison. -def authUser(name,password,db, encrypt=None): - try: - if encrypt: - query = 'SELECT privilege, id,name,password, grpName FROM User WHERE name= BINARY \'%s\' and password= BINARY \'%s\'' % (name,password) - else: - query = 'SELECT privilege, id,name,password, grpName FROM User WHERE name= BINARY \'%s\' and password= BINARY SHA(\'%s\')' % (name,password) - db.execute(query) - records = db.fetchone() - if not records: - raise ValueError - return records#(privilege,id,name,password,grpName) - except: - return (None, None, None, None, None) - - def hasAccessToConfidentialPhenotypeTrait(privilege, userName, authorized_users): access_to_confidential_phenotype_trait = 0 if webqtlConfig.USERDICT[privilege] > webqtlConfig.USERDICT['user']: |