Skip to content

VisualizeUtils

unpickle

unpickle(file_name: AnyStr)

Unpickle (deserialize) and return a python object located at filename

Source code in symdesign/visualization/VisualizeUtils.py
45
46
47
48
49
50
51
52
53
54
55
def unpickle(file_name: AnyStr):  # , protocol=pickle.HIGHEST_PROTOCOL):
    """Unpickle (deserialize) and return a python object located at filename"""
    if '.pkl' not in file_name and '.pickle' not in file_name:
        file_name = f'{file_name}.pkl'
    try:
        with open(file_name, 'rb') as serial_f:
            new_object = load(serial_f)
    except EOFError as ex:
        raise ValueError(f"The object serialized at location {file_name} couldn't be accessed. No data present!")

    return new_object

get_all_file_paths

get_all_file_paths(dir: str, suffix: str = '', extension: str = None, sort: bool = True) -> list[str]

Return all files in a directory with specified extensions and suffixes

Parameters:

  • dir (str) –

    The directory to retrieve filepaths from

  • suffix (str, default: '' ) –

    If a suffix should be appended to the files in the dir

  • extension (str, default: None ) –

    If a particular extension should be used to search for files

  • sort (bool, default: True ) –

    Whether to return the files in alphanumerically sorted order

Source code in symdesign/visualization/VisualizeUtils.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def get_all_file_paths(dir: str, suffix: str = '', extension: str = None, sort: bool = True) -> list[str]:
    """Return all files in a directory with specified extensions and suffixes

    Args:
        dir: The directory to retrieve filepaths from
        suffix: If a suffix should be appended to the files in the dir
        extension: If a particular extension should be used to search for files
        sort: Whether to return the files in alphanumerically sorted order
    """
    if not extension:
        extension = '.pdb'
    if sort:
        return [os.path.join(os.path.abspath(dir), file)
                for file in sorted(glob(os.path.join(os.path.abspath(dir), '*%s*%s' % (suffix, extension))))]
    else:
        return [os.path.join(os.path.abspath(dir), file)
                for file in glob(os.path.join(os.path.abspath(dir), '*%s*%s' % (suffix, extension)))]

save_group

save_group(group: str = 'all', one_file: bool = True, out_dir: str = os.getcwd()) -> None

Save all files inside a group to either one file or a list of files. Assumes the groups were generated using 'expand'

Parameters:

  • group (str, default: 'all' ) –

    name of the group to save

  • one_file (bool, default: True ) –

    Saves each protein in the group to one file, if False, then saves all members individually

  • out_dir (str, default: getcwd() ) –

    The directory location to save the files to

Returns:

  • None

    None

Source code in symdesign/visualization/VisualizeUtils.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def save_group(group: str = 'all', one_file: bool = True, out_dir: str = os.getcwd()) -> None:
    """Save all files inside a group to either one file or a list of files.
    Assumes the groups were generated using 'expand'

    Args:
        group: name of the group to save
        one_file: Saves each protein in the group to one file, if False, then saves all members individually
        out_dir: The directory location to save the files to

    Returns:
        None
    """
    if group == 'all':
        # remove appended symmetry mate number '_#' from the group instances and take the set of structures
        expanded_group = set('_'.join(name.split('_')[:-1]) for name in cmd.get_names('group_objects', enabled_only=1))
        groups = ['%s_expanded' % group for group in expanded_group]
    elif cmd.get_type(group) == 'object:group':
        groups = [group]
    else:
        print('Error: please provide a group name to save.')
        return None

    if one_file:
        for group in groups:
            cmd.save(os.path.join(out_dir, '%s.pdb' % group), group)
    else:
        for group in groups:
            stored.models = set()
            cmd.iterate(group, 'stored.models.add(model)')
            for model in stored.models:
                cmd.save(os.path.join(out_dir, '%s.pdb' % model), model)