Révision f3004731 modules/calculation.py

b/modules/calculation.py
1 1
import os
2 2
import logging
3 3

  
4
from rdkit.Chem import AllChem as Chem
5

  
6 4
logger = logging.getLogger('DockOnSurf')
7 5

  
8 6

  
9
def prep_iso(inp_file, mol: Chem.rdchem.Mol, exemplars):
10
    """Prepares the directories to run the calculations
11

  
12
    @param inp_file: Input file of the code to run the calculations with.
13
    @param mol: rdkit mol object of the relevant molecule.
14
    @param exemplars: list of conformer indices selected as representative.
15
    @return: None
7
def create_bak_calc_dir(run_type):
8
    """Checks if calculations directory already exists, backs it up if so and
9
    creates an empty one.
10
    @param run_type: Type of calculation. 'isolated', 'screening' or
11
    'refinement'
16 12
    """
17
    from shutil import copy
18

  
19
    # Checking if 'isolated' directory already exists and backing it up if so.
20
    dir_name = 'isolated'
13
    dir_name = run_type
21 14
    bak_num = 0
22 15
    while dir_name in os.listdir("."):
23 16
        bak_num += 1
24 17
        dir_name = dir_name.split(".")[0] + f".bak{bak_num}"
25 18
    if bak_num > 0:
26
        os.rename('isolated', dir_name)
27
        logger.warning("'isolated' directory already present. Moved former "
19
        os.rename(run_type, dir_name)
20
        logger.warning(f"'{run_type}' directory already present. Moved former "
28 21
                       f"directory to {dir_name}")
29
    os.mkdir('isolated')
22
    os.mkdir(run_type)
30 23

  
31
    # Creating and setting up directories for every conformer calculation.
32
    for i, conf in enumerate(exemplars):
33
        os.mkdir(f'isolated/conf_{i}')
34
        copy(inp_file, f'isolated/conf_{i}/')
35
        Chem.MolToXYZFile(mol, f'isolated/conf_{i}/coord.xyz', confId=int(conf))
36 24

  
25
def prep_cp2k(inp_file, run_type, atms_list):
26
    """Prepares the directories to run isolated calculation with CP2K.
37 27

  
38
def run_calc(run_type, inp_vars, **kwargs):
28
    @param inp_file: CP2K Input file to run the calculations with.
29
    @param run_type: Type of calculation. 'isolated', 'screening' or
30
        'refinement'
31
    @param atms_list: list of ase.Atoms objects to run the calculation of.
32
    @return: None
33
    """
34
    from shutil import copy
35
    import ase.io
36
    from pycp2k import CP2K
37
    cp2k = CP2K()
38
    cp2k.parse(inp_file)
39
    force_eval = cp2k.CP2K_INPUT.FORCE_EVAL_list[0]
40
    coord_file = force_eval.SUBSYS.TOPOLOGY.Coord_file_name
41

  
42
    # Creating and setting up directories for every atoms configuration.
43
    for i, conf in enumerate(atms_list):
44
        os.mkdir(f'{run_type}/conf_{i}')
45
        copy(inp_file, f'{run_type}/conf_{i}/')
46
        ase.io.write(f'{run_type}/conf_{i}/{coord_file}', conf)
47

  
48

  
49
def run_calc(run_type, inp_vars, atms_list):
39 50
    """Directs the calculation run according to the provided arguments.
40 51

  
41
    @param run_type: Type of calculation. Isolated, screening or refinement
52
    @param run_type: Type of calculation. 'isolated', 'screening' or
53
    'refinement'
42 54
    @param inp_vars: Calculation parameters from input file.
43
    @type kwargs: keyword arguments relevant to the specified run_type:
44
    for isolated:
45
        confs: rdkit mol object of the relevant molecule containing the
46
        conformers
47
        exemplars: list of conformer indices that are exemplars for every
48
        cluster.
49
    for screening:
55
    @param atms_list: List of ase.Atoms objects containing the sets of atoms
56
    aimed to run the calculations of.
50 57
    """
51 58
    run_types = ['isolated', 'screening', 'refinement']
52 59
    run_type_err = f"'run_type' should be one of the following: {run_types}"
......
56 63

  
57 64
    logger.info(f"Running {run_type} calculation with {inp_vars['code']} on "
58 65
                f"{inp_vars['batch_q_sys']}")
59

  
66
    create_bak_calc_dir(run_type)
60 67
    if run_type == 'isolated':
61
        key_err = "If 'run_type' is 'isolated', 'run_calc' needs the following"\
62
                  "arguments: 'inp_file', 'confs' and 'exemplars'."
63
        err = False
64
        try:
65
            confs = kwargs['confs']
66
            exemplars = kwargs['exemplars']
67
        except KeyError as e:
68
            logger.error(key_err)
69
            err = e
70
        else:
71
            err = False
72
        finally:
73
            if isinstance(err, BaseException):
74
                raise err
75

  
76
        prep_iso(inp_vars['isol_inp_file'], confs, exemplars)
68
        if inp_vars['code'] == 'cp2k':
69
            prep_cp2k(inp_vars['isol_inp_file'], run_type, atms_list)

Formats disponibles : Unified diff