Skip to content

helpers

A list of functions with a clear purpose that does not belong specifically to any of the existing units.

Classes:

Functions:

ArgparseChecker

Methods:

  • str2bool

    ArgumentParser tool to figure out the bool value

  • logging_lvl

    ArgumentParser tool to figure out the bool value

str2bool staticmethod

str2bool(v)

ArgumentParser tool to figure out the bool value

Source code in ties/helpers.py
116
117
118
119
120
121
122
123
124
125
126
@staticmethod
def str2bool(v):
    "ArgumentParser tool to figure out the bool value"
    if isinstance(v, bool):
        return v
    if v.lower() in ('yes', 'true', 't', 'y', '1'):
        return True
    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
        return False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')

logging_lvl staticmethod

logging_lvl(v)

ArgumentParser tool to figure out the bool value

Source code in ties/helpers.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
@staticmethod
def logging_lvl(v):
    "ArgumentParser tool to figure out the bool value"
    logging_levels = {
        'NOTSET': logging.NOTSET,
          'DEBUG': logging.DEBUG,
          'INFO': logging.INFO,
          'WARNING': logging.WARNING,
          'ERROR': logging.ERROR,
          'CRITICAL': logging.CRITICAL,
          # extras
           "ALL": logging.INFO,
           "FALSE": logging.ERROR
                      }

    if isinstance(v, bool) and v is True:
        return logging.WARNING
    elif isinstance(v, bool) and v is False:
        # effectively we disable logging until an error happens
        return logging.ERROR
    elif v.upper() in logging_levels:
        return logging_levels[v.upper()]
    else:
        raise argparse.ArgumentTypeError('Meaningful logging level expected.')

get_new_atom_names

get_new_atom_names(atoms, name_counter=None)

todo - add unit tests

@parameter/returns name_counter: a dictionary with atom as the key such as 'N', 'C', etc, the counter keeps track of the last used counter for each name. Empty means that the counting will start from 1. input atoms: mdanalysis atoms

Source code in ties/helpers.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def get_new_atom_names(atoms, name_counter=None):
    """
    todo - add unit tests

    @parameter/returns name_counter: a dictionary with atom as the key such as 'N', 'C', etc,
    the counter keeps track of the last used counter for each name.
    Empty means that the counting will start from 1.
    input atoms: mdanalysis atoms
    """
    if name_counter is None:
        name_counter = {}

    # {new_uniqe_name: old_atom_name}
    reverse_renaming_map = {}

    for atom in atoms:
        # count the letters before any digit
        letter_count = 0
        for letter in atom.name:
            if not letter.isalpha():
                break

            letter_count += 1

        # use max 3 letters from the atom name
        letter_count = min(letter_count, 3)

        letters = atom.name[:letter_count]

        # how many atoms do we have with these letters? ie C1, C2, C3 -> 3
        last_used_counter = name_counter.get(letters, 0) + 1

        # rename
        new_name = letters + str(last_used_counter)

        # if the name is longer than 4 character,
        # shorten the number of letters
        if len(new_name) > 4:
            # the name is too long, use only the first character
            new_name = letters[:4-len(str(last_used_counter))] + str(last_used_counter)

            # we assume that there is fewer than 1000 atoms with that name
            assert len(str(last_used_counter)) < 1000

        reverse_renaming_map[new_name] = atom.name

        atom.name = new_name

        # update the counter
        name_counter[letters] = last_used_counter

    return name_counter, reverse_renaming_map

get_atom_names_counter

get_atom_names_counter(atoms)

name_counter: a dictionary with atom as the key such as 'N', 'C', etc, the counter keeps track of the last used counter for each name. Ie if there are C1, C2, C3, this will return {'C':3} as the last counter.

Source code in ties/helpers.py
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def get_atom_names_counter(atoms):
    """
    name_counter: a dictionary with atom as the key such as 'N', 'C', etc,
    the counter keeps track of the last used counter for each name.
    Ie if there are C1, C2, C3, this will return {'C':3} as the last counter.
    """
    name_counter = {}

    for atom in atoms:
        # get the first letters that is not a character
        afterLetters = [i for i, l in enumerate(atom.name) if l.isalpha()][-1] + 1

        atom_name = atom.name[:afterLetters]
        atom_number = int(atom.name[afterLetters:])

        # we are starting the counter from 0 as we always add 1 later on
        last_used_counter = name_counter.get(atom_name, 0)

        # update the counter
        name_counter[atom_name] = max(last_used_counter + 1, atom_number)

    return name_counter

parse_frcmod_sections

parse_frcmod_sections(filename)

Copied from the previous TIES. It's simpler and this approach must be fine then.

Source code in ties/helpers.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def parse_frcmod_sections(filename):
    """
    Copied from the previous TIES. It's simpler and this approach must be fine then.
    """
    frcmod_info = {}
    section = 'REMARK'

    with open(filename) as F:
        for line in F:
            start_line = line[0:9].strip()

            if start_line in ['MASS', 'BOND', 'IMPROPER',
                              'NONBON', 'ANGLE', 'DIHE']:
                section = start_line
                frcmod_info[section] = []
            elif line.strip() and section != 'REMARK':
                frcmod_info[section].append(line)

    return frcmod_info