def individuals(ctx, root): """ Show all individuals in the database. If no database was found run puzzle init first. """ if root is None: root = expanduser("~") if os.path.isfile(root): logger.error("'root' can't be a file") ctx.abort() logger.info("Root directory is: {}".format(root)) db_path = os.path.join(root, 'puzzle_db.sqlite3') logger.info("db path is: {}".format(db_path)) if not os.path.exists(db_path): logger.warn("database not initialized, run 'puzzle init'") ctx.abort() store = SqlStore(db_path) for ind in store.get_individuals(): print(ind)
def init(ctx, reset): """Initialize a database that store metadata Check if "root" dir exists, otherwise create the directory and build the database. If a database already exists, do nothing. The behaviour will be different with different plugins. A config file in YAML format will be created in puzzle/configs with information about the database. VCF: A sqlite database will be built in the home directory of the user GEMINI: A sqlite database will be built in the home directory of the user """ puzzle_dir = ctx.obj['root'] if os.path.exists(puzzle_dir): logger.debug("Found puzzle directory: {0}".format(puzzle_dir)) else: logger.info("Create directory: {0}".format(puzzle_dir)) os.makedirs(puzzle_dir) logger.debug('Directory created') logger.debug('Connect to database and create tables') store = SqlStore(ctx.obj['db_path']) store.set_up(reset=reset)
def init(ctx, reset, root, phenomizer): """Initialize a database that store metadata Check if "root" dir exists, otherwise create the directory and build the database. If a database already exists, do nothing. The behaviour will be different with different plugins. A config file in YAML format will be created in puzzle/configs with information about the database. VCF: A sqlite database will be built in the home directory of the user GEMINI: A sqlite database will be built in the home directory of the user """ configs = {} if root is None: root = ctx.obj.get('root') or os.path.expanduser("~/.puzzle") configs['root'] = root print(phenomizer) if os.path.isfile(root): logger.error("'root' can't be a file") ctx.abort() logger.info("Root directory is: {}".format(root)) db_path = os.path.join(root, 'puzzle_db.sqlite3') logger.info("db path is: {}".format(db_path)) resource_dir = os.path.join(root, 'resources') logger.info("resource dir is: {}".format(resource_dir)) if os.path.exists(resource_dir): logger.debug("Found puzzle directory: {0}".format(root)) if os.path.exists(resource_dir) and not reset: logger.warning("Puzzle db already in place") ctx.abort() else: logger.info("Create directory: {0}".format(resource_dir)) os.makedirs(resource_dir) logger.debug('Directory created') logger.debug('Connect to database and create tables') store = SqlStore(db_path) store.set_up(reset=reset) if phenomizer: phenomizer = [str(term) for term in phenomizer] configs['phenomizer_auth'] = phenomizer if not ctx.obj.get('config_path'): logger.info("Creating puzzle config file in {0}".format(PUZZLE_CONFIG_PATH)) with codecs.open(PUZZLE_CONFIG_PATH, 'w', encoding='utf-8') as f: f.write(yaml.dump(configs)) logger.debug("Config created")
def load(ctx, variant_source, family_file, family_type): """ Load a case into the database. This can be done with a config file or from command line. If no database was found run puzzle init first. """ db_path = ctx.obj['db_path'] if not os.path.exists(db_path): logger.warn("database not initialized, run 'puzzle init'") ctx.abort() logger.debug('Set puzzle backend to {0}'.format(ctx.obj['mode'])) mode = ctx.obj['mode'] logger.debug('Set puzzle mode to {0}'.format(ctx.obj['variant_type'])) variant_type = ctx.obj['variant_type'] if mode == 'vcf': logger.info("Initialzing VCF plugin") if not family_file: logger.error("Please provide a ped like file") ctx.abort() try: plugin = VcfPlugin( root_path=variant_source, case_lines=family_file, case_type=family_type, vtype=variant_type ) except SyntaxError as e: logger.error(e.message) ctx.abort() elif mode == 'gemini': logger.debug("Initialzing GEMINI plugin") try: plugin = GeminiPlugin(db=variant_source, vtype=variant_type) except NameError: logger.error("Need to have gemini installed to use gemini plugin") ctx.abort() except DatabaseError as e: logger.error("{0} is not a valid gemini db".format(variant_source)) logger.info("variant-source has to point to a gemini database") ctx.abort() logger.debug("Plugin setup was succesfull") # from gemini can create multiple cases store = SqlStore(db_path) for case_obj in plugin.cases(): # extract case information logger.debug("adding case: {}".format(case_obj['case_id'])) store.add_case(case_obj, vtype=variant_type, mode=mode)
def individuals(ctx): """ Show all individuals in the database. If no database was found run puzzle init first. """ db_path = ctx.obj['db_path'] if not os.path.exists(db_path): logger.warn("database not initialized, run 'puzzle init'") ctx.abort() store = SqlStore(db_path) for ind in store.individuals(): print(ind)
def init(ctx, reset, root, phenomizer): """Initialize a database that store metadata Check if "root" dir exists, otherwise create the directory and build the database. If a database already exists, do nothing. """ configs = {} if root is None: root = ctx.obj.get('root') or os.path.expanduser("~/.puzzle") configs['root'] = root if os.path.isfile(root): logger.error("'root' can't be a file") ctx.abort() logger.info("Root directory is: {}".format(root)) db_path = os.path.join(root, 'puzzle_db.sqlite3') logger.info("db path is: {}".format(db_path)) resource_dir = os.path.join(root, 'resources') logger.info("resource dir is: {}".format(resource_dir)) if os.path.exists(resource_dir): logger.debug("Found puzzle directory: {0}".format(root)) if os.path.exists(resource_dir) and not reset: logger.warning("Puzzle db already in place") ctx.abort() else: logger.info("Create directory: {0}".format(resource_dir)) os.makedirs(resource_dir) logger.debug('Directory created') logger.debug('Connect to database and create tables') store = SqlStore(db_path) store.set_up(reset=reset) if phenomizer: phenomizer = [str(term) for term in phenomizer] configs['phenomizer_auth'] = phenomizer if not ctx.obj.get('config_path'): logger.info("Creating puzzle config file in {0}".format(PUZZLE_CONFIG_PATH)) with codecs.open(PUZZLE_CONFIG_PATH, 'w', encoding='utf-8') as f: f.write(yaml.dump(configs)) logger.debug("Config created")
def init(ctx, reset, root): """Initialize a database that store metadata Check if "root" dir exists, otherwise create the directory and build the database. If a database already exists, do nothing. The behaviour will be different with different plugins. A config file in YAML format will be created in puzzle/configs with information about the database. VCF: A sqlite database will be built in the home directory of the user GEMINI: A sqlite database will be built in the home directory of the user """ if root is None: root = os.path.expanduser("~/.puzzle") if os.path.isfile(root): logger.error("'root' can't be a file") ctx.abort() logger.info("Root directory is: {}".format(root)) db_path = os.path.join(root, 'puzzle_db.sqlite3') logger.info("db path is: {}".format(db_path)) resource_dir = os.path.join(root, 'resources') logger.info("resource dir is: {}".format(resource_dir)) if os.path.exists(resource_dir): logger.debug("Found puzzle directory: {0}".format(root)) if os.path.exists(resource_dir) and not reset: logger.warning("Puzzle db already in place") ctx.abort() else: logger.info("Create directory: {0}".format(resource_dir)) os.makedirs(resource_dir) logger.debug('Directory created') logger.debug('Connect to database and create tables') store = SqlStore(db_path) store.set_up(reset=reset)
def puzzle_dir(request, dir_path): """Return a puzzle dir with a database initialized""" db_path = os.path.join(dir_path, 'puzzle_db.sqlite3') logger.debug("db path is: {}".format(db_path)) resource_dir = os.path.join(dir_path, 'resources') logger.debug("resource dir is: {}".format(resource_dir)) logger.debug("Create directory: {0}".format(resource_dir)) os.makedirs(resource_dir) logger.debug('Directory created') logger.debug('Connect to database and create tables') store = SqlStore(db_path) store.set_up(reset=False) #It's getting tear downed by dir_path()... return dir_path
def puzzle_dir(request, dir_path): """Return a puzzle dir with a database initialized""" db_path = os.path.join(dir_path, 'puzzle_db.sqlite3') logger.debug("db path is: {}".format(db_path)) resource_dir = os.path.join(dir_path, 'resources') logger.debug("resource dir is: {}".format(resource_dir)) logger.debug("Create directory: {0}".format(resource_dir)) os.makedirs(resource_dir) logger.debug('Directory created') logger.debug('Connect to database and create tables') store = SqlStore(db_path) store.set_up(reset=False) # It's getting tear downed by dir_path()... return dir_path
def populated_puzzle_db(request, dir_path, case_obj): """Return a puzzle dir with a populated database""" db_path = os.path.join(dir_path, 'puzzle_db.sqlite3') logger.debug("db path is: {}".format(db_path)) resource_dir = os.path.join(dir_path, 'resources') logger.debug("resource dir is: {}".format(resource_dir)) logger.debug("Create directory: {0}".format(resource_dir)) os.makedirs(resource_dir) logger.debug('Directory created') logger.debug('Connect to database and create tables') store = SqlStore(db_path) store.set_up(reset=False) store.add_case(case_obj, vtype='sv', mode='vcf') #It's getting tear downed by dir_path()... return dir_path
def individuals(ctx, root): """ Show all individuals in the database. If no database was found run puzzle init first. """ root = root or ctx.obj.get('root') or os.path.expanduser("~/.puzzle") if os.path.isfile(root): logger.error("'root' can't be a file") ctx.abort() logger.info("Root directory is: {}".format(root)) db_path = os.path.join(root, 'puzzle_db.sqlite3') logger.info("db path is: {}".format(db_path)) if not os.path.exists(db_path): logger.warn("database not initialized, run 'puzzle init'") ctx.abort() store = SqlStore(db_path) for ind in store.individuals(): click.echo(ind)
def cases(ctx, root): """ Show all cases in the database. If no database was found run puzzle init first. """ root = root or ctx.obj.get('root') or os.path.expanduser("~/.puzzle") if os.path.isfile(root): logger.error("'root' can't be a file") ctx.abort() logger.info("Root directory is: {}".format(root)) db_path = os.path.join(root, 'puzzle_db.sqlite3') logger.info("db path is: {}".format(db_path)) if not os.path.exists(db_path): logger.warn("database not initialized, run 'puzzle init'") ctx.abort() store = SqlStore(db_path) for case in store.cases(): click.echo(case)
def delete(ctx, family_id, individual_id, root): """ Delete a case or individual from the database. If no database was found run puzzle init first. """ root = root or ctx.obj.get('root') or os.path.expanduser("~/.puzzle") if os.path.isfile(root): logger.error("'root' can't be a file") ctx.abort() logger.info("Root directory is: {}".format(root)) db_path = os.path.join(root, 'puzzle_db.sqlite3') logger.info("db path is: {}".format(db_path)) if not os.path.exists(db_path): logger.warn("database not initialized, run 'puzzle init'") ctx.abort() store = SqlStore(db_path) if family_id: case_obj = store.case(case_id=family_id) if case_obj is None: logger.warning( "Family {0} does not exist in database".format(family_id)) ctx.abort() store.delete_case(case_obj) elif individual_id: ind_obj = store.individual(ind_id=individual_id) if ind_obj.ind_id != individual_id: logger.warning("Individual {0} does not exist in database".format( individual_id)) ctx.abort() store.delete_individual(ind_obj) else: logger.warning("Please provide a family or individual id") ctx.abort()
def delete(ctx, family_id, individual_id, root): """ Delete a case or individual from the database. If no database was found run puzzle init first. """ if root is None: root = os.path.expanduser("~/.puzzle") if os.path.isfile(root): logger.error("'root' can't be a file") ctx.abort() logger.info("Root directory is: {}".format(root)) db_path = os.path.join(root, 'puzzle_db.sqlite3') logger.info("db path is: {}".format(db_path)) if not os.path.exists(db_path): logger.warn("database not initialized, run 'puzzle init'") ctx.abort() store = SqlStore(db_path) if family_id: case_obj = store.case(case_id=family_id) if case_obj.case_id != family_id: logger.warning("Family {0} does not exist in database".format(family_id)) ctx.abort() store.delete_case(case_obj) elif individual_id: ind_obj = store.individual(ind_id=individual_id) if ind_obj.ind_id != individual_id: logger.warning("Individual {0} does not exist in database".format(individual_id)) ctx.abort() store.delete_individual(ind_obj) else: logger.warning("Please provide a family or individual id") ctx.abort()
def delete(ctx, family_id, individual_id): """ Delete a case or individual from the database. If no database was found run puzzle init first. """ db_path = ctx.obj['db_path'] if not os.path.exists(db_path): logger.warn("database not initialized, run 'puzzle init'") ctx.abort() store = SqlStore(db_path) if family_id: case_obj = store.case(case_id=family_id) if case_obj.case_id != family_id: logger.warning("Family {0} does not exist in database".format(family_id)) ctx.abort() store.delete_case(case_obj) elif individual_id: ind_obj = store.individual(ind_id=individual_id) if ind_obj.ind_id != individual_id: logger.warning("Individual {0} does not exist in database".format(individual_id)) ctx.abort() store.delete_individual(ind_obj)
def view(ctx, host, port, debug, pattern, family_file, family_type, variant_source, root, no_browser, phenomizer): """Visualize DNA variant resources. 1. Look for variant source(s) to visualize and inst. the right plugin """ main_loop = (not debug) or (os.environ.get('WERKZEUG_RUN_MAIN') == 'true') root = root or ctx.obj.get('root') or os.path.expanduser("~/.puzzle") phenomizer_auth = phenomizer or ctx.obj.get('phenomizer_auth') BaseConfig.PHENOMIZER_AUTH = True if ctx.obj.get( 'phenomizer_auth') else False BaseConfig.STORE_ENABLED = True if variant_source is None: logger.info("Root directory is: {}".format(root)) db_path = os.path.join(root, 'puzzle_db.sqlite3') logger.info("db path is: {}".format(db_path)) if not os.path.exists(db_path): logger.warn("database not initialized, run 'puzzle init'") ctx.abort() if os.path.isfile(root): logger.error("'root' can't be a file") ctx.abort() store = SqlStore(db_path, phenomizer_auth=phenomizer_auth) for case_obj in store.cases(): if case_obj.variant_mode == 'gemini': if not GEMINI: logger.error( "Need to have gemini instaled to view gemini database") ctx.abort() else: logger.info("Using in memory database") tmpdir = tempfile.mkdtemp() tmpdb = os.path.join(tmpdir, 'puzzle.sqlite3') logger.info("building database: {}".format(tmpdb)) store = SqlStore("sqlite:///{}".format(tmpdb), phenomizer_auth=phenomizer_auth) if main_loop: store.set_up() cases = [] if os.path.isfile(variant_source): file_type = get_file_type(variant_source) #Test if gemini is installed if file_type == 'unknown': logger.error("File has to be vcf or gemini db") ctx.abort() elif file_type == 'gemini': #Check if gemini is installed if not GEMINI: logger.error( "Need to have gemini installed to use gemini plugin" ) ctx.abort() variant_type = get_variant_type(variant_source) cases = get_cases(variant_source=variant_source, case_lines=family_file, case_type=family_type, variant_type=variant_type, variant_mode=file_type) else: for file in path(variant_source).walkfiles(pattern): file_type = get_file_type(file) if file_type != 'unknown': variant_type = get_variant_type(file) #Test if gemini is installed if file_type == 'gemini': if not GEMINI: logger.error( "Need to have gemini installed to use gemini plugin" ) ctx.abort() for case in get_cases(variant_source=file, case_type=family_type, variant_type=variant_type, variant_mode=file_type): cases.append(case) for case_obj in cases: if store.case(case_obj.case_id) is not None: logger.warn("{} already exists in the database".format( case_obj.case_id)) continue # extract case information logger.debug("adding case: {}".format(case_obj.case_id)) store.add_case(case_obj, vtype=case_obj.variant_type, mode=case_obj.variant_mode) logger.debug("Plugin setup was succesfull") BaseConfig.PUZZLE_BACKEND = store BaseConfig.UPLOAD_DIR = os.path.join(root, 'resources') app = create_app(config_obj=BaseConfig) if no_browser is False: webbrowser.open_new_tab("http://{}:{}".format(host, port)) app.run(host=host, port=port, debug=debug)
def load(ctx, variant_source, family_file, family_type, root, mode, variant_type): """ Load a variant source into the database. If no database was found run puzzle init first. 1. VCF: If a vcf file is used it can be loaded with a ped file 2. GEMINI: Ped information will be retreived from the gemini db """ if root is None: #This is the default puzzle folder root = os.path.expanduser("~/.puzzle") if os.path.isfile(root): logger.error("'root' can't be a file") ctx.abort() logger.info("Root directory is: {}".format(root)) db_path = os.path.join(root, 'puzzle_db.sqlite3') logger.info("db path is: {}".format(db_path)) if not os.path.exists(db_path): logger.warn("database not initialized, run 'puzzle init'") ctx.abort() logger.debug('Set puzzle backend to {0}'.format(mode)) logger.debug('Set variant type to {0}'.format(variant_type)) if mode == 'vcf': logger.info("Initialzing VCF plugin") try: plugin = VcfPlugin( root_path=variant_source, case_lines=family_file, case_type=family_type, vtype=variant_type ) except SyntaxError as e: logger.error(e.message) ctx.abort() elif mode == 'gemini': logger.debug("Initialzing GEMINI plugin") try: plugin = GeminiPlugin(db=variant_source, vtype=variant_type) except NameError: logger.error("Need to have gemini installed to use gemini plugin") ctx.abort() except DatabaseError as e: logger.error("{} is not a valid gemini db".format(variant_source)) logger.info("variant-source has to point to a gemini database") ctx.abort() logger.debug("Plugin setup was succesfull") # from gemini can create multiple cases store = SqlStore(db_path) for case_obj in plugin.cases(): if store.case(case_id=case_obj.case_id).case_id == case_obj.case_id: logger.warn("{} already exists in the database" .format(case_obj.case_id)) continue # extract case information logger.debug("adding case: {}".format(case_obj.case_id)) store.add_case(case_obj, vtype=variant_type, mode=mode)
def load(ctx, variant_source, family_file, family_type, root): """ Load a variant source into the database. If no database was found run puzzle init first. 1. VCF: If a vcf file is used it can be loaded with a ped file 2. GEMINI: Ped information will be retreived from the gemini db """ root = root or ctx.obj.get('root') or os.path.expanduser("~/.puzzle") if os.path.isfile(root): logger.error("'root' can't be a file") ctx.abort() logger.info("Root directory is: {}".format(root)) db_path = os.path.join(root, 'puzzle_db.sqlite3') logger.info("db path is: {}".format(db_path)) if not os.path.exists(db_path): logger.warn("database not initialized, run 'puzzle init'") ctx.abort() if not os.path.isfile(variant_source): logger.error("Variant source has to be a file") ctx.abort() mode = get_file_type(variant_source) if mode == 'unknown': logger.error("Unknown file type") ctx.abort() #Test if gemini is installed elif mode == 'gemini': logger.debug("Initialzing GEMINI plugin") if not GEMINI: logger.error("Need to have gemini installed to use gemini plugin") ctx.abort() logger.debug('Set puzzle backend to {0}'.format(mode)) variant_type = get_variant_type(variant_source) logger.debug('Set variant type to {0}'.format(variant_type)) cases = get_cases( variant_source=variant_source, case_lines=family_file, case_type=family_type, variant_type=variant_type, variant_mode=mode ) if len(cases) == 0: logger.warning("No cases found") ctx.abort() logger.info("Initializing sqlite plugin") store = SqlStore(db_path) for case_obj in cases: if store.case(case_obj.case_id) is not None: logger.warn("{} already exists in the database" .format(case_obj.case_id)) continue # extract case information logger.debug("adding case: {} to puzzle db".format(case_obj.case_id)) store.add_case(case_obj, vtype=variant_type, mode=mode)
def sql_store(): """Setup an in-memory database.""" _test_db = SqlStore('sqlite:///:memory:') _test_db.set_up() yield _test_db _test_db.tear_down()
def view(ctx, host, port, debug, pattern, family_file, family_type, variant_source, root, no_browser, phenomizer): """Visualize DNA variant resources. 1. Look for variant source(s) to visualize and inst. the right plugin """ root = root or ctx.obj.get('root') or os.path.expanduser("~/.puzzle") phenomizer_auth = phenomizer or ctx.obj.get('phenomizer_auth') BaseConfig.PHENOMIZER_AUTH = True if ctx.obj.get('phenomizer_auth') else False BaseConfig.STORE_ENABLED = True if variant_source is None: logger.info("Root directory is: {}".format(root)) db_path = os.path.join(root, 'puzzle_db.sqlite3') logger.info("db path is: {}".format(db_path)) if not os.path.exists(db_path): logger.warn("database not initialized, run 'puzzle init'") ctx.abort() if os.path.isfile(root): logger.error("'root' can't be a file") ctx.abort() store = SqlStore(db_path, phenomizer_auth=phenomizer_auth) for case_obj in store.cases(): if case_obj.variant_mode == 'gemini': if not GEMINI: logger.error("Need to have gemini instaled to view gemini database") ctx.abort() else: logger.info("Using in memory database") store = SqlStore('sqlite:///:memory:', phenomizer_auth=phenomizer_auth) store.set_up() cases = [] if os.path.isfile(variant_source): file_type = get_file_type(variant_source) #Test if gemini is installed if file_type == 'unknown': logger.error("File has to be vcf or gemini db") ctx.abort() elif file_type == 'gemini': #Check if gemini is installed if not GEMINI: logger.error("Need to have gemini installed to use gemini plugin") ctx.abort() variant_type = get_variant_type(variant_source) cases = get_cases( variant_source=variant_source, case_lines=family_file, case_type=family_type, variant_type=variant_type, variant_mode=file_type ) else: for file in path(variant_source).walkfiles(pattern): file_type = get_file_type(file) if file_type != 'unknown': variant_type = get_variant_type(file) #Test if gemini is installed if file_type == 'gemini': if not GEMINI: logger.error("Need to have gemini installed to use gemini plugin") ctx.abort() for case in get_cases( variant_source=file, case_type=family_type, variant_type=variant_type, variant_mode=file_type): cases.append(case) for case_obj in cases: if store.case(case_obj.case_id) is not None: logger.warn("{} already exists in the database" .format(case_obj.case_id)) continue # extract case information logger.debug("adding case: {}".format(case_obj.case_id)) store.add_case(case_obj, vtype=case_obj.variant_type, mode=case_obj.variant_mode) logger.debug("Plugin setup was succesfull") BaseConfig.PUZZLE_BACKEND = store BaseConfig.UPLOAD_DIR = os.path.join(root, 'resources') app = create_app(config_obj=BaseConfig) if not (no_browser or debug): webbrowser.open_new_tab("http://{}:{}".format(host, port)) app.run(host=host, port=port, debug=debug)