Skip to content

SlurmControl

find_list_indices

find_list_indices(reference_cmds, query_ids)

Search for ID's present in supplied list in a reference list and return the indices of the reference list where they are found

Source code in symdesign/tools/SlurmControl.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def find_list_indices(reference_cmds, query_ids):
    """Search for ID's present in supplied list in a reference list and return the indices of the reference list where
    they are found"""
    # full_lines_set = set(full_lines)
    # cmd_lines_set = set(cmd_lines)
    query_ids_sort = sorted(query_ids)
    # cmd_lines_sort = sorted(cmd_lines)

    idxs = []
    for i, cmd_id in enumerate(reference_cmds):  # _sort
        for id in query_ids_sort:
            if id in cmd_id:
                idxs.append(i)
                break
    idxs_sorted = sorted(idxs)
    logger.info(','.join(str(i + 1) for i in idxs_sorted))

    return idxs_sorted

filter_by_indices

filter_by_indices(index_array, _iterable, zero=True)

Return the indices from an iterable that match a specified input index array

Source code in symdesign/tools/SlurmControl.py
34
35
36
37
def filter_by_indices(index_array, _iterable, zero=True):
    """Return the indices from an iterable that match a specified input index array"""
    offset = 0 if zero else 1
    return [_iterable[idx - offset] for idx in index_array]
link_pair(pair: tuple[str, str], force: bool = False) -> None

Combine docking files of one docking combination with another

Parameters:

  • pair (tuple[str, str]) –

    source file, destination file (link)

  • force (bool, default: False ) –

    Whether to remove links before creation

Source code in symdesign/tools/SlurmControl.py
86
87
88
89
90
91
92
93
94
95
def link_pair(pair: tuple[str, str], force: bool = False) -> None:
    """Combine docking files of one docking combination with another

    Args:
        pair: source file, destination file (link)
        force: Whether to remove links before creation
    """
    if force:
        os.remove(pair[1])
    os.symlink(*pair)  # , target_is_directory=True)

investigate_job_array_failure

investigate_job_array_failure(job_id, output_dir=os.path.join(os.getcwd(), 'output'))

Returns an array for each of the errors encountered. All=True returns the set

Source code in symdesign/tools/SlurmControl.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def investigate_job_array_failure(job_id, output_dir=os.path.join(os.getcwd(), 'output')):
    """Returns an array for each of the errors encountered. All=True returns the set"""
    job_output_files = glob(os.path.join(output_dir, '*%s*' % job_id))
    if not job_output_files:
        raise RuntimeError('Found no files with %s glob. Did you provide the correct arguments? See --help'
                           % os.path.join(output_dir, '*%s*' % job_id))
    potential_errors = [job_file if os.path.getsize(job_file) > 0 else None for job_file in job_output_files]
    logger.info('Found array ids from job %s with SBATCH output:\n\t%s'
                % (job_id, ','.join(str(i) for i, error in enumerate(potential_errors, 1) if error)))
    parsed_errors = list(map(classify_slurm_error_type, potential_errors))
    job_file_array_id_d = \
        {job_file: int(os.path.splitext(job_file.split('_')[-1])[0]) for job_file in job_output_files}
    # generate a dictionary of the job_file to array_id
    # for job in job_output_files:
    #     array_id = os.path.splitext(job.split('_')[-1])[0]
    #     job_file_array_id_d[array] = job
    memory_array = \
        sorted(job_file_array_id_d[job_output_files[i]] for i, error in enumerate(parsed_errors) if error == 'memory')
    failure_array = \
        sorted(job_file_array_id_d[job_output_files[i]] for i, error in enumerate(parsed_errors) if error == 'failure')
    other_array = \
        sorted(job_file_array_id_d[job_output_files[i]] for i, error in enumerate(parsed_errors) if error == 'other')

    return memory_array, failure_array, other_array

change_script_array

change_script_array(script_file, array)

Take a script file and replace the array line with a new array

Source code in symdesign/tools/SlurmControl.py
136
137
138
139
140
141
142
143
144
145
146
147
148
def change_script_array(script_file, array):
    """Take a script file and replace the array line with a new array"""
    with open(script_file, 'r') as f:
        lines = f.readlines()
        for i, line in enumerate(lines):
            if '#SBATCH -a' in line or '#SBATCH --array' in line:
                lines[i] = '#SBATCH --array=%s' % ','.join(str(a) for a in array)

    new_script = '%s_%s' % (os.path.splitext(script_file)[0], 're-do_SLURM_failures.sh')
    with open(new_script, 'w') as f:
        f.write('\n'.join(line for line in map(str.strip, lines)))

    return new_script