def clean_core(args): major_version = '.'.join(get_distribution('CGRdb').version.split('.')[:-1]) schema = args.name db_config = Database() LazyEntityMeta.attach(db_config, database='CGRdb_config') db_config.bind('postgres', **args.connection) db_config.generate_mapping() with db_session: config = db_config.Config.get(name=schema, version=major_version) if not config: raise KeyError('schema not exists or version incompatible') config = config.config for p in config['packages']: try: p = get_distribution(p) import_module(p.project_name) except (DistributionNotFound, VersionConflict): raise ImportError( f'packages not installed or has invalid versions: {p}') db = Database() LazyEntityMeta.attach(db, schema, 'CGRdb') db.bind('postgres', **args.connection) db.generate_mapping() with db_session: db.execute(f'TRUNCATE TABLE "{schema}"."MoleculeSearchCache", ' f'"{schema}"."ReactionSearchCache" RESTART IDENTITY')
def update_core(args): major_version = '.'.join(get_distribution('CGRdb').version.split('.')[:-1]) schema = args.name db_config = Database() LazyEntityMeta.attach(db_config, database='CGRdb_config') db_config.bind('postgres', user=args.user, password=args.password, host=args.host, database=args.base, port=args.port) db_config.generate_mapping() with db_session: config = db_config.Config.get(name=schema, version=major_version) if not config: raise KeyError('schema not exists or version incompatible') config = config.config for p in config['packages']: try: p = get_distribution(p) import_module(p.project_name) except (DistributionNotFound, VersionConflict): raise ImportError( f'packages not installed or has invalid versions: {p}') db = Database() LazyEntityMeta.attach(db, schema, 'CGRdb') db.bind('postgres', user=args.user, password=args.password, host=args.host, database=args.base, port=args.port) db.generate_mapping() with db_session: db.execute(init_session.replace('{schema}', schema)) db.execute(merge_molecules.replace('{schema}', schema)) db.execute(insert_molecule.replace('{schema}', schema)) db.execute(after_insert_molecule.replace('{schema}', schema)) db.execute(delete_molecule.replace('{schema}', schema)) db.execute(insert_reaction.replace('{schema}', schema)) db.execute(search_similar_molecules.replace('{schema}', schema)) db.execute(search_substructure_molecule.replace('{schema}', schema)) db.execute(search_similar_reactions.replace('{schema}', schema)) db.execute(search_substructure_reaction.replace('{schema}', schema)) db.execute( search_substructure_fingerprint_molecule.replace( '{schema}', schema)) db.execute( search_similar_fingerprint_molecule.replace('{schema}', schema)) db.execute(search_reactions_by_molecule.replace('{schema}', schema)) db.execute(search_mappingless_reaction.replace('{schema}', schema))
def init_core(args): db = Database() LazyEntityMeta.attach(db, database='CGRdb_config') db.bind('postgres', **args.connection) db.generate_mapping(create_tables=True) with db_session: db.execute('CREATE EXTENSION IF NOT EXISTS intarray') db.execute('CREATE EXTENSION IF NOT EXISTS plpython3u')
def init_core(args): db = Database() LazyEntityMeta.attach(db, database='CGRdb_config') db.bind('postgres', user=args.user, password=args.password, host=args.host, database=args.base, port=args.port) db.generate_mapping(create_tables=True)
def index_core(args): major_version = '.'.join(get_distribution('CGRdb').version.split('.')[:-1]) schema = args.name db_config = Database() LazyEntityMeta.attach(db_config, database='CGRdb_config') db_config.bind('postgres', user=args.user, password=args.password, host=args.host, database=args.base, port=args.port) db_config.generate_mapping() with db_session: config = db_config.Config.get(name=schema, version=major_version) if not config: raise KeyError('schema not exists or version incompatible') config = config.config for p in config['packages']: try: p = get_distribution(p) import_module(p.project_name) except (DistributionNotFound, VersionConflict): raise ImportError( f'packages not installed or has invalid versions: {p}') db = Database() LazyEntityMeta.attach(db, schema, 'CGRdb') db.bind('postgres', user=args.user, password=args.password, host=args.host, database=args.base, port=args.port) db.generate_mapping() with db_session: db.execute( f'CREATE INDEX idx_moleculestructure__smlar ON "{schema}"."MoleculeStructure" USING ' 'GIST (fingerprint _int4_sml_ops)') db.execute( f'CREATE INDEX idx_moleculestructure__subst ON "{schema}"."MoleculeStructure" USING ' 'GIN (fingerprint gin__int_ops)') db.execute( f'CREATE INDEX idx_reactionindex__smlar ON "{schema}"."ReactionIndex" USING ' 'GIST (fingerprint _int4_sml_ops)') db.execute( f'CREATE INDEX idx_reactionindex__subst ON "{schema}"."ReactionIndex" USING ' 'GIN (fingerprint gin__int_ops)')
def create_core(args): schema = args.name config = args.config and load(args.config) or {} db_config = Database() LazyEntityMeta.attach(db_config, database='CGRdb_config') db_config.bind('postgres', user=args.user, password=args.password, host=args.host, database=args.base, port=args.port) db_config.generate_mapping() db = Database() LazyEntityMeta.attach(db, schema, 'CGRdb') db.bind('postgres', user=args.user, password=args.password, host=args.host, database=args.base, port=args.port) db.generate_mapping(create_tables=True) with db_session: db.execute('CREATE EXTENSION IF NOT EXISTS smlar') db.execute('CREATE EXTENSION IF NOT EXISTS intarray') db.execute('CREATE EXTENSION IF NOT EXISTS pg_cron') with db_session: db.execute(f'CREATE INDEX idx_smlar_molecule_structure ON "{schema}"."MoleculeStructure" USING ' 'GIST (bit_array _int4_sml_ops)') db.execute(f'CREATE INDEX idx_smlar_reaction_index ON "{schema}"."ReactionIndex" USING ' 'GIST (bit_array _int4_sml_ops)') db.execute(f'CREATE INDEX idx_subst_molecule_structure ON "{schema}"."MoleculeStructure" USING ' 'GIN (bit_array gin__int_ops)') db.execute(f'CREATE INDEX idx_subst_reaction_index ON "{schema}"."ReactionIndex" USING ' 'GIN (bit_array gin__int_ops)') db.execute(f"SELECT cron.schedule('0 3 * * *', $$$$\n" f'DELETE FROM "{schema}"."MoleculeSearchCache"' " WHERE date < CURRENT_TIMESTAMP - INTERVAL '1 day' $$$$)") db.execute(f"SELECT cron.schedule('0 3 * * *', $$$$\n" f'DELETE FROM "{schema}"."ReactionSearchCache"' " WHERE date < CURRENT_TIMESTAMP - INTERVAL '1 day' $$$$)") with db_session: db_config.Config(name=schema, config=config, version=major_version)
def load_schema(schema, *args, **kwargs): """ Load schema from db with compatible version :param schema: schema name for loading """ major_version = '.'.join(get_distribution('CGRdb').version.split('.')[:-1]) db_config = Database() LazyEntityMeta.attach(db_config, database='CGRdb_config') db_config.bind('postgres', *args, **kwargs) db_config.generate_mapping() with db_session: config = db_config.Config.get(name=schema, version=major_version) if not config: raise KeyError('schema not exists') config = config.config for p in config['packages']: try: p = get_distribution(p) import_module(p.project_name) except (DistributionNotFound, VersionConflict): raise ImportError( f'packages not installed or has invalid versions: {p}') db = Database() LazyEntityMeta.attach(db, schema, 'CGRdb') db.bind('postgres', *args, **kwargs) db.generate_mapping() init = f'SELECT "{schema}".cgrdb_init_session(\'{dumps(config)}\')' db.cgrdb_init_session = db_session()( lambda: db.execute(init) and True or False) db.cgrdb_init_session() return db
def create_core(args): major_version = '.'.join(get_distribution('CGRdb').version.split('.')[:-1]) schema = args.name config = args.config and load(args.config) or {} if 'packages' not in config: config['packages'] = [] for p in config['packages']: try: p = get_distribution(p) import_module(p.project_name) except (DistributionNotFound, VersionConflict): raise ImportError( f'packages not installed or has invalid versions: {p}') db_config = Database() LazyEntityMeta.attach(db_config, database='CGRdb_config') db_config.bind('postgres', user=args.user, password=args.password, host=args.host, database=args.base, port=args.port) db_config.generate_mapping() with db_session: if db_config.Config.exists(name=schema): raise KeyError('schema already exists') db = Database() LazyEntityMeta.attach(db, schema, 'CGRdb') db.bind('postgres', user=args.user, password=args.password, host=args.host, database=args.base, port=args.port) db.generate_mapping(create_tables=True) with db_session: db.execute(f'ALTER TABLE "{schema}"."Reaction" DROP COLUMN structure') db.execute( f'ALTER TABLE "{schema}"."Reaction" RENAME TO "ReactionRecord"') db.execute( f'CREATE VIEW "{schema}"."Reaction" AS SELECT id, NULL::bytea as structure ' f'FROM "{schema}"."ReactionRecord"') db.execute(init_session.replace('{schema}', schema)) db.execute(merge_molecules.replace('{schema}', schema)) db.execute(insert_molecule.replace('{schema}', schema)) db.execute(insert_molecule_trigger.replace('{schema}', schema)) db.execute(after_insert_molecule.replace('{schema}', schema)) db.execute(after_insert_molecule_trigger.replace('{schema}', schema)) db.execute(delete_molecule.replace('{schema}', schema)) db.execute(delete_molecule_trigger.replace('{schema}', schema)) db.execute(insert_reaction.replace('{schema}', schema)) db.execute(insert_reaction_trigger.replace('{schema}', schema)) db.execute(search_similar_molecules.replace('{schema}', schema)) db.execute(search_substructure_molecule.replace('{schema}', schema)) db.execute(search_similar_reactions.replace('{schema}', schema)) db.execute(search_substructure_reaction.replace('{schema}', schema)) db.execute( search_substructure_fingerprint_molecule.replace( '{schema}', schema)) db.execute( search_similar_fingerprint_molecule.replace('{schema}', schema)) db.execute(search_reactions_by_molecule.replace('{schema}', schema)) db.execute(search_mappingless_reaction.replace('{schema}', schema)) if args.indexed: with db_session: db.execute( f'CREATE INDEX idx_moleculestructure__smlar ON "{schema}"."MoleculeStructure" USING ' 'GIST (fingerprint _int4_sml_ops)') db.execute( f'CREATE INDEX idx_moleculestructure__subst ON "{schema}"."MoleculeStructure" USING ' 'GIN (fingerprint gin__int_ops)') db.execute( f'CREATE INDEX idx_reactionindex__smlar ON "{schema}"."ReactionIndex" USING ' 'GIST (fingerprint _int4_sml_ops)') db.execute( f'CREATE INDEX idx_reactionindex__subst ON "{schema}"."ReactionIndex" USING ' 'GIN (fingerprint gin__int_ops)') with db_session: db_config.Config(name=schema, config=config, version=major_version)
def index_core(args): from ..index import SimilarityIndex, SubstructureIndex major_version = '.'.join(get_distribution('CGRdb').version.split('.')[:-1]) schema = args.name db_config = Database() LazyEntityMeta.attach(db_config, database='CGRdb_config') db_config.bind('postgres', **args.connection) db_config.generate_mapping() with db_session: config = db_config.Config.get(name=schema, version=major_version) if not config: raise KeyError('schema not exists or version incompatible') config = config.config for p in config['packages']: try: p = get_distribution(p) import_module(p.project_name) except (DistributionNotFound, VersionConflict): raise ImportError( f'packages not installed or has invalid versions: {p}') db = Database() LazyEntityMeta.attach(db, schema, 'CGRdb') db.bind('postgres', **args.connection) db.generate_mapping() if 'check_threshold' in args.params: sort_by_tanimoto = args.params['check_threshold'] is not None else: sort_by_tanimoto = True with db_session: substructure_molecule = SubstructureIndex( db.execute( f'SELECT id, fingerprint FROM "{schema}"."MoleculeStructure"'), False) with db_session: similarity_molecule = SimilarityIndex( db.execute( f'SELECT id, fingerprint FROM "{schema}"."MoleculeStructure"'), **args.params) if sort_by_tanimoto: # pairing fingerprints for memory saving substructure_molecule._fingerprints = similarity_molecule._fingerprints with db_session: substructure_reaction = SubstructureIndex( db.execute( f'SELECT id, fingerprint FROM "{schema}"."ReactionIndex"'), False) with db_session: similarity_reaction = SimilarityIndex( db.execute( f'SELECT id, fingerprint FROM "{schema}"."ReactionIndex"'), **args.params) if sort_by_tanimoto: # pairing fingerprints for memory saving substructure_reaction._fingerprints = similarity_reaction._fingerprints dump((substructure_molecule, substructure_reaction, similarity_molecule, similarity_reaction), args.data)
def create_core(args): major_version = '.'.join(get_distribution('CGRdb').version.split('.')[:-1]) schema = args.name config = args.config and load(args.config) or {} if 'packages' not in config: config['packages'] = [] for p in config['packages']: try: p = get_distribution(p) import_module(p.project_name) except (DistributionNotFound, VersionConflict): raise ImportError( f'packages not installed or has invalid versions: {p}') db_config = Database() LazyEntityMeta.attach(db_config, database='CGRdb_config') db_config.bind('postgres', **args.connection) db_config.generate_mapping() with db_session: if db_config.Config.exists(name=schema): raise KeyError('schema already exists') with db_session: db_config.execute(f'DROP SCHEMA IF EXISTS {schema} CASCADE') db_config.execute(f'CREATE SCHEMA {schema}') db = Database() LazyEntityMeta.attach(db, schema, 'CGRdb') db.bind('postgres', **args.connection) db.generate_mapping(create_tables=True) with db_session: db.execute(f'ALTER TABLE "{schema}"."Reaction" DROP COLUMN structure') db.execute( f'ALTER TABLE "{schema}"."Reaction" RENAME TO "ReactionRecord"') db.execute( f'CREATE VIEW "{schema}"."Reaction" AS SELECT id, NULL::bytea as structure ' f'FROM "{schema}"."ReactionRecord"') with db_session: db.execute(init_session.replace('{schema}', schema)) db.execute(insert_molecule.replace('{schema}', schema)) db.execute(after_insert_molecule.replace('{schema}', schema)) db.execute(delete_molecule.replace('{schema}', schema)) db.execute(insert_reaction.replace('{schema}', schema)) db.execute(merge_molecules.replace('{schema}', schema)) db.execute(insert_molecule_trigger.replace('{schema}', schema)) db.execute(after_insert_molecule_trigger.replace('{schema}', schema)) db.execute(delete_molecule_trigger.replace('{schema}', schema)) db.execute(insert_reaction_trigger.replace('{schema}', schema)) db.execute(search_structure_molecule.replace('{schema}', schema)) db.execute(search_structure_reaction.replace('{schema}', schema)) db.execute(search_similar_molecules.replace('{schema}', schema)) db.execute(search_substructure_molecule.replace('{schema}', schema)) db.execute(search_similar_reactions.replace('{schema}', schema)) db.execute(search_substructure_reaction.replace('{schema}', schema)) db.execute(search_reactions_by_molecule.replace('{schema}', schema)) db.execute(search_mappingless_reaction.replace('{schema}', schema)) with db_session: db_config.Config(name=schema, config=config, version=major_version)
# -*- coding: utf-8 -*- import CGRdb from LazyPony import LazyEntityMeta from os.path import abspath from pony.orm import Database from sys import path parent = abspath('..') if parent not in path: path.insert(0, parent) LazyEntityMeta.attach(Database(), database='CGRdb') author = 'Dr. Ramil Nugmanov' copyright = '2017-2020, Dr. Ramil Nugmanov <*****@*****.**>' version = '4.0' project = 'CGRdb' needs_sphinx = '1.8' extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.autosummary', 'm2r', 'nbsphinx' ] nbsphinx_kernel_name = 'python3' exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', '**.ipynb_checkpoints'] templates_path = ['_templates'] source_suffix = '.rst' master_doc = 'index' language = None pygments_style = 'flasky'
def __init__(self, user=None, password=None, host=None, database=None, port=5432, workpath='/tmp'): """ load all schemas from db with compatible version :param user: if None then used from config :param password: if None then used from config :param host: if None then used from config :param database: if None then used from config :param port: if None then used from config """ if user is None or password is None or host is None or database is None or port is None or workpath is None: try: from config import DB_PASS, DB_HOST, DB_USER, DB_NAME, DB_PORT, WORKPATH except ImportError: raise ImportError('install config.py correctly') if user is None: user = DB_USER if password is None: password = DB_PASS if host is None: host = DB_HOST if database is None: database = DB_NAME if port is None: port = DB_PORT if workpath is None: workpath = WORKPATH db_config = Database() LazyEntityMeta.attach(db_config, database='CGRdb_config') db_config.bind('postgres', user=user, password=password, host=host, database=database, port=port) db_config.generate_mapping() self.__schemas = {} with db_session: config = db_config.Config.select( lambda x: x.version == major_version)[:] for c in config: db = Database() LazyEntityMeta.attach(db, c.name, 'CGRdb') db.Molecule._fragmentor_workpath = db.Reaction._fragmentor_workpath = workpath for k, v in c.config.get('molecule', {}).items(): setattr(db.Molecule, f'_{k}', v) for k, v in c.config.get('reaction', {}).items(): setattr(db.Reaction, f'_{k}', v) db.bind('postgres', user=user, password=password, host=host, database=database, port=port) db.generate_mapping() self.__schemas[c.name] = db