def process_custom_mcs(custom_mcs, savestate=None, verbosity=0):
    """ Parses user supplied custom MCS data

    :param [str, dict] custom_mcs: mcs data to be parsed 
    :param savestate_util.SavableState savestate: saved state data 
    :param int  verbosity: controls verbosity level 
    :rtype: dict
    """

    custom_mcs_result = {}
    if custom_mcs:
        custom_mcs = os_util.detect_type(custom_mcs, test_for_dict=True)
        if isinstance(custom_mcs, str):
            if rdkit.Chem.MolFromSmarts(custom_mcs) is not None:
                os_util.local_print(
                    'Using user-supplied MCS {} for all molecules.'.format(
                        custom_mcs),
                    msg_verbosity=os_util.verbosity_level.info,
                    current_verbosity=verbosity)
                custom_mcs_result = {'*': custom_mcs}
            else:
                os_util.local_print(
                    'Could not parse you custom MCS "{}".'.format(custom_mcs),
                    msg_verbosity=os_util.verbosity_level.error,
                    current_verbosity=verbosity)
                raise SystemExit(1)
        elif isinstance(custom_mcs, dict):
            if all([(isinstance(key, frozenset) and len(key) == 2)
                    for key in custom_mcs]):
                custom_mcs_result = custom_mcs
            elif all([(isinstance(key, str) and key.count('-') == 1)
                      for key in custom_mcs]):
                custom_mcs_result = {
                    frozenset(key.split('-')): value
                    for key, value in custom_mcs.items()
                }
            else:
                os_util.local_print(
                    'Could not parse you custom MCS "{}". If providing a dict, make sure to follow '
                    'the required format (see documentation).'.format(
                        custom_mcs),
                    msg_verbosity=os_util.verbosity_level.error,
                    current_verbosity=verbosity)
                raise SystemExit(1)
        else:
            os_util.local_print(
                'Could not parse you custom MCS. A string or dict is required, but your data "{}" '
                'was parsed as a {} (see documentation for formatting options).'
                ''.format(custom_mcs, type(custom_mcs)),
                msg_verbosity=os_util.verbosity_level.error,
                current_verbosity=verbosity)
            raise SystemExit(1)

        if savestate is not None:
            savestate['custom_mcs'] = custom_mcs_result
            savestate.setdefault('mcs_dict', {}).update(custom_mcs_result)
            savestate.save_data()

    return custom_mcs_result
def test_center_molecule(map_bias, all_molecules, verbosity=0):
    """ Test center molecule to prepare star or wheel maps

    :param [list, str] map_bias: test this bias string or list
    :param list all_molecules: all molecules read com input
    :param int verbosity: sets the verbosity level
    :rtype: str
    """

    map_bias = os_util.detect_type(map_bias, test_for_list=True)
    if not map_bias:
        os_util.local_print(
            'A star map requires one, and only one, center molecule. You supplied none.',
            msg_verbosity=os_util.verbosity_level.error,
            current_verbosity=verbosity)
        raise SystemExit(1)
    if isinstance(map_bias, list) and len(map_bias) > 1:
        os_util.local_print(
            'A star map requires one, and only one, center molecule. You supplied {} ({})'
            ''.format(len(map_bias), map_bias),
            msg_verbosity=os_util.verbosity_level.error,
            current_verbosity=verbosity)
        raise SystemExit(1)

    if isinstance(map_bias, list):
        map_bias = map_bias[0]

    if map_bias not in all_molecules:
        os_util.local_print(
            'The center molecule you supplied ({}) not found in {}.'
            ''.format(map_bias, ', '.join(all_molecules)),
            msg_verbosity=os_util.verbosity_level.error,
            current_verbosity=verbosity)
        raise ValueError('Molecule not found')

    return map_bias
Example #3
0
def test_detect_type():
    assert os_util.detect_type('-1') == -1
    assert os_util.detect_type('2.5') == 2.5
    assert os_util.detect_type('float("inf")') == 'float("inf")'
    assert os_util.detect_type('[1,2,3]') == [1, 2, 3]
    assert os_util.detect_type('{1,2,3}') == {1, 2, 3}
    assert os_util.detect_type('{"a": 1, "b": 2, "c": 3}') == {
        "a": 1,
        "b": 2,
        "c": 3
    }
    assert os_util.detect_type('off') is False
    assert os_util.detect_type('oN') is True
    assert os_util.detect_type('off', test_for_boolean=False) == 'off'
    assert os_util.detect_type(' a   = 1, b : 2    ;c=3',
                               test_for_dict=True) == 'a   = 1, b : 2    ;c=3'
    assert os_util.detect_type(' a   = 1; b : 2    ;c=3',
                               test_for_dict=True) == {
                                   "a": 1,
                                   "b": 2,
                                   "c": 3
                               }
    assert os_util.detect_type(' a   = 1  2   3; b : 2    ;c=3',
                               test_for_dict=True) == {
                                   "a": [1, 2, 3],
                                   "b": 2,
                                   "c": 3
                               }
    assert os_util.detect_type('1 ; 2,3', test_for_list=True) == [1, 2, 3]
    assert os_util.detect_type('1 2 3', test_for_list=True) == [1, 2, 3]
    assert os_util.detect_type('1 2a 3', test_for_list=True) == '1 2a 3'
    assert os_util.detect_type('1 ; "2",3', test_for_list=True) == [1, "2", 3]
    assert os_util.detect_type(
        '/home/user\n//\\//123',
        test_for_list=True) == ['/home/user', '//\\//123']
    assert os_util.detect_type('abc: 123\ndef:123\n   \n\n  das:abc   com  # comm', test_for_dict=True) == \
           {'abc': 123, 'def': 123, 'das': 'abc   com'}