Skip to content

Commit 6afde5a

Browse files
committed
fixes
1 parent 0197965 commit 6afde5a

File tree

5 files changed

+105
-24
lines changed

5 files changed

+105
-24
lines changed

CGRdb/CLI/main_create.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright 2017-2019 Ramil Nugmanov <[email protected]>
3+
# Copyright 2017-2020 Ramil Nugmanov <[email protected]>
44
# Copyright 2019 Adelia Fatykhova <[email protected]>
55
# This file is part of CGRdb.
66
#
@@ -20,7 +20,7 @@
2020
from importlib import import_module
2121
from json import load
2222
from LazyPony import LazyEntityMeta
23-
from pkg_resources import get_distribution
23+
from pkg_resources import get_distribution, DistributionNotFound, VersionConflict
2424
from pony.orm import db_session, Database
2525
from ..sql import *
2626

@@ -31,9 +31,12 @@ def create_core(args):
3131
config = args.config and load(args.config) or {}
3232
if 'packages' not in config:
3333
config['packages'] = []
34-
for p in config['packages']: # check availability of extra packages
35-
p = get_distribution(p)
36-
import_module(p.project_name)
34+
for p in config['packages']:
35+
try:
36+
p = get_distribution(p)
37+
import_module(p.project_name)
38+
except (DistributionNotFound, VersionConflict):
39+
raise ImportError(f'packages not installed or has invalid versions: {p}')
3740

3841
db_config = Database()
3942
LazyEntityMeta.attach(db_config, database='CGRdb_config')
@@ -56,15 +59,6 @@ def create_core(args):
5659
db.execute(f'CREATE VIEW "{schema}"."Reaction" AS SELECT id, NULL::bytea as structure '
5760
f'FROM "{schema}"."ReactionRecord"')
5861

59-
db.execute(f'CREATE INDEX idx_moleculestructure__smlar ON "{schema}"."MoleculeStructure" USING '
60-
'GIST (fingerprint _int4_sml_ops)')
61-
db.execute(f'CREATE INDEX idx_moleculestructure__subst ON "{schema}"."MoleculeStructure" USING '
62-
'GIN (fingerprint gin__int_ops)')
63-
db.execute(f'CREATE INDEX idx_reactionindex__smlar ON "{schema}"."ReactionIndex" USING '
64-
'GIST (fingerprint _int4_sml_ops)')
65-
db.execute(f'CREATE INDEX idx_reactionindex__subst ON "{schema}"."ReactionIndex" USING '
66-
'GIN (fingerprint gin__int_ops)')
67-
6862
db.execute(init_session.replace('{schema}', schema))
6963
db.execute(merge_molecules.replace('{schema}', schema))
7064

@@ -87,5 +81,16 @@ def create_core(args):
8781
db.execute(search_reactions_by_molecule.replace('{schema}', schema))
8882
db.execute(search_mappingless_reaction.replace('{schema}', schema))
8983

84+
if args.indexed:
85+
with db_session:
86+
db.execute(f'CREATE INDEX idx_moleculestructure__smlar ON "{schema}"."MoleculeStructure" USING '
87+
'GIST (fingerprint _int4_sml_ops)')
88+
db.execute(f'CREATE INDEX idx_moleculestructure__subst ON "{schema}"."MoleculeStructure" USING '
89+
'GIN (fingerprint gin__int_ops)')
90+
db.execute(f'CREATE INDEX idx_reactionindex__smlar ON "{schema}"."ReactionIndex" USING '
91+
'GIST (fingerprint _int4_sml_ops)')
92+
db.execute(f'CREATE INDEX idx_reactionindex__subst ON "{schema}"."ReactionIndex" USING '
93+
'GIN (fingerprint gin__int_ops)')
94+
9095
with db_session:
9196
db_config.Config(name=schema, config=config, version=major_version)

CGRdb/CLI/main_index.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2020 Ramil Nugmanov <[email protected]>
4+
# This file is part of CGRdb.
5+
#
6+
# CGRdb is free software; you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published by
8+
# the Free Software Foundation; either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with this program; if not, see <https://www.gnu.org/licenses/>.
18+
#
19+
from importlib import import_module
20+
from LazyPony import LazyEntityMeta
21+
from pkg_resources import get_distribution, DistributionNotFound, VersionConflict
22+
from pony.orm import db_session, Database
23+
24+
25+
def index_core(args):
26+
major_version = '.'.join(get_distribution('CGRdb').version.split('.')[:-1])
27+
schema = args.name
28+
29+
db_config = Database()
30+
LazyEntityMeta.attach(db_config, database='CGRdb_config')
31+
db_config.bind('postgres', user=args.user, password=args.password, host=args.host, database=args.base,
32+
port=args.port)
33+
db_config.generate_mapping()
34+
35+
with db_session:
36+
config = db_config.Config.get(name=schema, version=major_version)
37+
if not config:
38+
raise KeyError('schema not exists')
39+
config = config.config
40+
41+
for p in config['packages']:
42+
try:
43+
p = get_distribution(p)
44+
import_module(p.project_name)
45+
except (DistributionNotFound, VersionConflict):
46+
raise ImportError(f'packages not installed or has invalid versions: {p}')
47+
48+
db = Database()
49+
LazyEntityMeta.attach(db, schema, 'CGRdb')
50+
db.bind('postgres', user=args.user, password=args.password, host=args.host, database=args.base, port=args.port)
51+
db.generate_mapping()
52+
53+
with db_session:
54+
db.execute(f'CREATE INDEX idx_moleculestructure__smlar ON "{schema}"."MoleculeStructure" USING '
55+
'GIST (fingerprint _int4_sml_ops)')
56+
db.execute(f'CREATE INDEX idx_moleculestructure__subst ON "{schema}"."MoleculeStructure" USING '
57+
'GIN (fingerprint gin__int_ops)')
58+
db.execute(f'CREATE INDEX idx_reactionindex__smlar ON "{schema}"."ReactionIndex" USING '
59+
'GIST (fingerprint _int4_sml_ops)')
60+
db.execute(f'CREATE INDEX idx_reactionindex__subst ON "{schema}"."ReactionIndex" USING '
61+
'GIN (fingerprint gin__int_ops)')

CGRdb/CLI/parser.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
#
33
# Copyright 2017 Boris Sattarov <[email protected]>
4-
# Copyright 2017, 2018 Ramil Nugmanov <[email protected]>
4+
# Copyright 2017-2020 Ramil Nugmanov <[email protected]>
55
# This file is part of CGRdb.
66
#
77
# CGRdb is free software; you can redistribute it and/or modify
@@ -20,6 +20,7 @@
2020
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter, FileType
2121
from importlib.util import find_spec
2222
from .main_create import create_core
23+
from .main_index import index_core
2324
from .main_init import init_core
2425

2526

@@ -43,16 +44,30 @@ def create_db(subparsers):
4344
parser.add_argument('--port', '-P', default=5432, help='database port')
4445
parser.add_argument('--base', '-b', default='postgres', help='database name')
4546
parser.add_argument('--name', '-n', help='schema name', required=True)
47+
parser.add_argument('--no-index', help='schema name', dest='indexed', action='store_false')
4648
parser.add_argument('--config', '-c', default=None, type=FileType(), help='database config in JSON format')
4749
parser.set_defaults(func=create_core)
4850

4951

52+
def create_index(subparsers):
53+
parser = subparsers.add_parser('index', help='create index in reactions db',
54+
formatter_class=ArgumentDefaultsHelpFormatter)
55+
parser.add_argument('--user', '-u', default='postgres', help='admin login')
56+
parser.add_argument('--password', '-p', required=True, help='admin pass')
57+
parser.add_argument('--host', '-H', default='localhost', help='host name')
58+
parser.add_argument('--port', '-P', default=5432, help='database port')
59+
parser.add_argument('--base', '-b', default='postgres', help='database name')
60+
parser.add_argument('--name', '-n', help='schema name', required=True)
61+
parser.set_defaults(func=index_core)
62+
63+
5064
def argparser():
5165
parser = ArgumentParser(description="CGRdb", epilog="(c) Dr. Ramil Nugmanov", prog='cgrdb')
5266
subparsers = parser.add_subparsers(title='subcommands', description='available utilities')
5367

5468
create_db(subparsers)
5569
init_db(subparsers)
70+
create_index(subparsers)
5671

5772
if find_spec('argcomplete'):
5873
from argcomplete import autocomplete

CGRdb/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,20 @@ def load_schema(schema, *args, **kwargs):
3030
3131
:param schema: schema name for loading
3232
"""
33+
major_version = '.'.join(get_distribution('CGRdb').version.split('.')[:-1])
34+
3335
db_config = Database()
3436
LazyEntityMeta.attach(db_config, database='CGRdb_config')
3537
db_config.bind('postgres', *args, **kwargs)
3638
db_config.generate_mapping()
3739

3840
with db_session:
39-
major_version = '.'.join(get_distribution('CGRdb').version.split('.')[:-1])
4041
config = db_config.Config.get(name=schema, version=major_version)
4142
if not config:
4243
raise KeyError('schema not exists')
4344
config = config.config
4445

45-
for p in config.get('packages', ()):
46+
for p in config['packages']:
4647
try:
4748
p = get_distribution(p)
4849
import_module(p.project_name)
@@ -51,7 +52,6 @@ def load_schema(schema, *args, **kwargs):
5152

5253
db = Database()
5354
LazyEntityMeta.attach(db, schema, 'CGRdb')
54-
5555
db.bind('postgres', *args, **kwargs)
5656
db.generate_mapping()
5757

CGRdb/sql/reaction_by_molecule.sql

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ from CGRtools.containers import MoleculeContainer, QueryContainer
2323
from compress_pickle import loads
2424

2525
if search == 1: # search: 1 - substructure, 2 - similar
26-
search_type = 'substructure'
26+
search_function = 'substructure'
2727
elif search == 2:
28-
search_type = 'similar'
28+
search_function = 'similar'
2929
else:
3030
raise plpy.spiexceptions.DataException('search type invalid')
3131

3232
if role == 0: # role: 0 - any, 1 - reactant, 2 - product
3333
role_filter = ''
34-
search_type += '_any'
34+
search_type = search_function + '_any'
3535
elif role == 1:
3636
role_filter = 'WHERE r.is_product = False'
37-
search_type += '_reactant'
37+
search_type = search_function + '_reactant'
3838
elif role == 2:
3939
role_filter = 'WHERE r.is_product = True'
40-
search_type += '_product'
40+
search_type = search_function + '_product'
4141
else:
4242
raise plpy.spiexceptions.DataException('role invalid')
4343

@@ -57,7 +57,7 @@ if found:
5757
return found[0]
5858

5959
# search molecules
60-
found = plpy.execute(f'''SELECT * FROM "{schema}".cgrdb_search_{search_type}_molecules('\\x{data.hex()}'::bytea)''')[0]
60+
found = plpy.execute(f'''SELECT * FROM "{schema}".cgrdb_search_{search_function}_molecules('\\x{data.hex()}'::bytea)''')[0]
6161
# check for empty results
6262
if not found['count']:
6363
# store empty cache

0 commit comments

Comments
 (0)