waterEntropy 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
waterEntropy/.coverage ADDED
Binary file
@@ -0,0 +1,6 @@
1
+ """calculate water entropy near heterogeneous interfaces"""
2
+
3
+ # Add imports here
4
+ # from waterEntropy.neighbours.force_torque import *
5
+
6
+ __version__ = "1.0.0"
@@ -0,0 +1,169 @@
1
+ """
2
+ These functions calculate hydrogen bonding within a shell.
3
+ """
4
+
5
+ import numpy as np
6
+
7
+ import waterEntropy.analysis.RAD as RADShell
8
+ import waterEntropy.maths.trig as Trig
9
+ from waterEntropy.utils.helpers import nested_dict
10
+
11
+
12
+ class HBCollection:
13
+ """
14
+ Class for hydrogen bond donors and their acceptors
15
+ """
16
+
17
+ def __init__(self):
18
+ self.donating_to = (
19
+ nested_dict()
20
+ ) # structure: UA_idx[donating_idx] = accepting_idx
21
+ self.accepting_from = (
22
+ nested_dict()
23
+ ) # structure: UA_idx[accepting_idx] = [donating_idx1,]
24
+
25
+ def add_data(self, UA_idx: int, donator_idx: int, acceptor_idx: int):
26
+ """
27
+ For a given donator index, save its acceptor index in the donating_to
28
+ dictionary. And for that acceptor, save the donator index as a list in
29
+ the accepting from dictionary.
30
+
31
+ :param self: class instance
32
+ :param UA_idx: atom index of the UA of the bonded donating hydrogen
33
+ :param donator_idx: atom index of the donator bonded to the UA
34
+ :param acceptor_idx: atom index of the UA accepting the
35
+ hydrogen bond in the coordination shell
36
+ """
37
+ if donator_idx not in self.donating_to[UA_idx]:
38
+ self.donating_to[UA_idx][donator_idx] = acceptor_idx
39
+ if acceptor_idx not in self.accepting_from:
40
+ self.accepting_from[acceptor_idx] = []
41
+ if donator_idx not in self.accepting_from[acceptor_idx]:
42
+ self.accepting_from[acceptor_idx].append(donator_idx)
43
+
44
+ def find_donators(self, UA_idx: int):
45
+ """
46
+ Find the donators for a given residue, this returns a dict where the
47
+ key is the accepting atom index and the value is a list of the atom
48
+ indices that donate to it.
49
+
50
+ :param cls: class instance
51
+ :param UA_idx: atom index of UA being donated to
52
+ """
53
+ return self.accepting_from.get(UA_idx, None)
54
+
55
+ def find_acceptor(self, UA_idx: int):
56
+ """
57
+ Find the acceptors for a given resid, this returns a dictionary where
58
+ the key is the donator index and the value is the acceptor index
59
+
60
+ :param cls: class instance
61
+ :param UA_idx: atom index of UA accepting HBs
62
+ """
63
+ return self.donating_to.get(UA_idx, None)
64
+
65
+
66
+ def get_HB_terms(heavy_atom, donator, acceptor, DA_distance, dimensions):
67
+ """
68
+ Get two terms for calculating the hydrogen bond between a donator and
69
+ potential acceptor in a coordination shell.
70
+ For a hydrogen bond to form, the following criteria need to be met:
71
+
72
+ 1. Angle between heavy atom bonded to donator (X), the donator (D) and
73
+ the acceptor (A) is over 90 degrees
74
+
75
+ 2. The relative charge is most negative over all other neighbours in a
76
+ coordination shell
77
+
78
+ :param heavy_atom: the mdanalysis instance for the heavy atom bonded to
79
+ the donator
80
+ :param donator: the mdanalysis instance for the donator
81
+ :param acceptor: the mdanalysis instance for the possible acceptor
82
+ :param DA_distance: the distance between donor and acceptor
83
+ :param dimensions: the dimensions of the simulation box
84
+ """
85
+ relative_charge = (float(donator.charge) * float(acceptor.charge)) / float(
86
+ DA_distance**2
87
+ )
88
+ cosine_angle = Trig.get_angle(
89
+ heavy_atom.position, donator.position, acceptor.position, dimensions
90
+ )
91
+ XDA_angle = np.degrees(np.arccos(cosine_angle))
92
+
93
+ return relative_charge, XDA_angle
94
+
95
+
96
+ def get_shell_HB_acceptors(shell, system, HBs: HBCollection):
97
+ # pylint: disable=too-many-locals
98
+ """
99
+ Find the hydrogen bond acceptors for the central UA hydrogens that are
100
+ electropositive. The assumption is made that hydrogen bonding can only
101
+ occur inside the coordination shell, this is needed for shell neighbour
102
+ labelling used in orientational entropy calculations
103
+
104
+ :param shell: the instance for containing coordination shell neighbours
105
+ :param system: mdanalysis instance of all atoms in current frame
106
+ :param HBs: instance of HBCollection class
107
+ """
108
+ X_idx = shell.atom_idx # shell central heavy atom
109
+ heavy_atom = system.atoms[X_idx]
110
+ # 1. iterate over atoms bonded to atom X
111
+ for i in heavy_atom.bonds:
112
+ D_idx = i.indices[1]
113
+ bonded = system.atoms[D_idx]
114
+ # 2. find hydrogen atoms bonded to heavy atom with positive charge
115
+ if bonded.mass < 1.1 and bonded.mass > 1.0 and bonded.charge > 0:
116
+ donator = system.atoms[D_idx]
117
+ # 3. set starting hydrogen bond term and possible acceptor to a large number
118
+ # the acceptor that gives the lowest relative charge is the acceptor
119
+ current_relative_charge = 99
120
+ current_acceptor = False
121
+ # 4. get shell neighbours ordered by ascending distance
122
+ sorted_indices, sorted_distances = Trig.get_shell_neighbour_selection(
123
+ shell, donator, system
124
+ )
125
+ for A_idx, DA_distance in zip(sorted_indices, sorted_distances):
126
+ # 5. check if neighbouring atom in shell is an acceptor,
127
+ # if so override current possible acceptor
128
+ acceptor = system.atoms[A_idx]
129
+ if not current_acceptor:
130
+ current_acceptor = acceptor
131
+ relative_charge, XDA_angle = get_HB_terms(
132
+ heavy_atom, donator, acceptor, DA_distance, system.dimensions[:3]
133
+ )
134
+ # 6. Check if an atom is a possible hydrogen bond acceptor
135
+ if relative_charge < current_relative_charge and float(XDA_angle) > 90:
136
+ current_relative_charge = relative_charge
137
+ current_acceptor = acceptor
138
+ # 7. create a new object for hydrogen bonding in a RAD shell
139
+ HBs.add_data(X_idx, D_idx, current_acceptor.index)
140
+
141
+
142
+ def get_shell_HBs(shell, system, HBs: HBCollection, shells: RADShell.ShellCollection):
143
+ """
144
+ For a given UA and its coordination shell neighbours, find what the central
145
+ UA donates to and accepts from in its shell.
146
+
147
+ :param shell: the instance for class waterEntropy.neighbours.RAD.RAD
148
+ containing coordination shell neighbours
149
+ :param system: mdanalysis instance of all atoms in current frame
150
+ :param HBs: HBCollection instance
151
+ :param shells: ShellCollection instance
152
+ """
153
+ # 1. first check that HB donations haven't already been found
154
+ donates_to = HBs.find_acceptor(shell.atom_idx)
155
+ if not donates_to:
156
+ get_shell_HB_acceptors(shell, system, HBs)
157
+ donates_to = HBs.find_acceptor(shell.atom_idx)
158
+ # 2. now iterate through shell and find shells of shell neighbours
159
+ for n_idx in shell.UA_shell:
160
+ neighbour_shell = shells.find_shell(n_idx)
161
+ if not neighbour_shell:
162
+ neighbour_shell = RADShell.get_RAD_shell(
163
+ system.atoms[n_idx], system, shells
164
+ )
165
+ # 3. find what each shell neighbour donates to in their shell
166
+ neighbour_donates_to = HBs.find_acceptor(n_idx)
167
+ if not neighbour_donates_to:
168
+ get_shell_HB_acceptors(neighbour_shell, system, HBs)
169
+ neighbour_donates_to = HBs.find_acceptor(n_idx)
@@ -0,0 +1,199 @@
1
+ """
2
+ Store the labelled neighbours that are donated to and accepted from the central
3
+ atom in a shell. Shells with the same neighbours are grouped together and
4
+ along with the counts for donating to and accepting from each neighbour type.
5
+ These labelled HB neighbours are used to calculate orientational
6
+ entropy of water molecules.
7
+ """
8
+
9
+ from waterEntropy.analysis.HB import HBCollection
10
+ from waterEntropy.analysis.shells import ShellCollection
11
+ from waterEntropy.utils.helpers import nested_dict
12
+ import waterEntropy.utils.selections as Selections
13
+
14
+
15
+ class HBLabelCollection:
16
+ """
17
+ Labelled shell counts used for Sorient.
18
+ The counts are placed into two dictionaries used for statistics later,
19
+ the structure of these two dictionaries are as follows:
20
+
21
+ dict1 = {"resname": {("labelled_shell"): {"shell_count": 0,
22
+ "donates_to": {"labelled_donators": 0,},
23
+ "accepts_from": {"labelled_acceptors": 0,}
24
+ }}}
25
+ dict2 = {"nearest_resid": {"resname":
26
+ {("labelled_shell"): {"shell_count": 0,
27
+ "donates_to": {"labelled_donators": 0,},
28
+ "accepts_from": {"labelled_acceptors": 0,}
29
+ }}}
30
+ """
31
+
32
+ def __init__(self):
33
+ self.labelled_shell_counts = nested_dict() # save shell instances in here
34
+ self.resid_labelled_shell_counts = nested_dict() # save shell instances in here
35
+
36
+ def add_data(self, resid, resname, labelled_shell, donates_to, accepts_from):
37
+ # pylint: disable=too-many-arguments
38
+ """Add data to class dictionaries"""
39
+ self.add_shell_counts(resid, resname, labelled_shell)
40
+ self.add_donates_to(resid, resname, labelled_shell, donates_to)
41
+ self.add_accepts_from(resid, resname, labelled_shell, accepts_from)
42
+
43
+ def add_shell_counts(self, resid, resname, labelled_shell):
44
+ """
45
+ Add a labelled shell to a dictionary that keeps track of the counts
46
+ for each labelled shell type with constituents alpha-numerically
47
+ ordered
48
+
49
+ :param self: class instance
50
+ :param resid: residue id of nearest nonlike atom in the labelled shell
51
+ :param resname: residue name of nearest nonlike atom in the labelled shell
52
+ :param labelled_shell: coordination shell with labelled neighbours
53
+ """
54
+ labelled_shell = tuple(sorted(labelled_shell))
55
+ if "shell_count" not in self.labelled_shell_counts[resname][labelled_shell]:
56
+ self.labelled_shell_counts[resname][labelled_shell]["shell_count"] = 1
57
+ else:
58
+ self.labelled_shell_counts[resname][labelled_shell]["shell_count"] += 1
59
+
60
+ if (
61
+ "shell_count"
62
+ not in self.resid_labelled_shell_counts[resid][resname][labelled_shell]
63
+ ):
64
+ self.resid_labelled_shell_counts[resid][resname][labelled_shell][
65
+ "shell_count"
66
+ ] = 1
67
+ else:
68
+ self.resid_labelled_shell_counts[resid][resname][labelled_shell][
69
+ "shell_count"
70
+ ] += 1
71
+
72
+ def add_donates_to(self, resid, resname, labelled_shell, donates_to):
73
+ """
74
+ Add a labelled neighbours donated to in a dictionary that keeps
75
+ track of the counts for each labelled shell type with
76
+ constituents alpha-numerically ordered
77
+
78
+ :param self: class instance
79
+ :param resid: residue id of nearest nonlike atom in the labelled shell
80
+ :param resname: residue name of nearest nonlike atom in the labelled shell
81
+ :param labelled_shell: coordination shell with labelled neighbours
82
+ :param donates_to: list of labelled neighbours that are donated to
83
+ """
84
+ labelled_shell = tuple(sorted(labelled_shell))
85
+ # donates_to = tuple(sorted(donates_to))
86
+ for a in donates_to:
87
+ if (
88
+ a
89
+ not in self.labelled_shell_counts[resname][labelled_shell]["donates_to"]
90
+ ):
91
+ self.labelled_shell_counts[resname][labelled_shell]["donates_to"][a] = 1
92
+ else:
93
+ self.labelled_shell_counts[resname][labelled_shell]["donates_to"][
94
+ a
95
+ ] += 1
96
+
97
+ if (
98
+ a
99
+ not in self.resid_labelled_shell_counts[resid][resname][labelled_shell][
100
+ "donates_to"
101
+ ]
102
+ ):
103
+ self.resid_labelled_shell_counts[resid][resname][labelled_shell][
104
+ "donates_to"
105
+ ][a] = 1
106
+ else:
107
+ self.resid_labelled_shell_counts[resid][resname][labelled_shell][
108
+ "donates_to"
109
+ ][a] += 1
110
+
111
+ def add_accepts_from(self, resid, resname, labelled_shell, accepts_from):
112
+ """
113
+ Add a labelled neighbours accepted from to in a dictionary that keeps
114
+ track of the counts for each labelled shell type with
115
+ constituents alpha-numerically ordered
116
+
117
+ :param self: class instance
118
+ :param resid: residue id of nearest nonlike atom in the labelled shell
119
+ :param resname: residue name of nearest nonlike atom in the labelled shell
120
+ :param labelled_shell: coordination shell with labelled neighbours
121
+ :param accepts_from: list of labelled neighbours that are accepted_from
122
+ """
123
+ labelled_shell = tuple(sorted(labelled_shell))
124
+ for d in accepts_from:
125
+ if (
126
+ d
127
+ not in self.labelled_shell_counts[resname][labelled_shell][
128
+ "accepts_from"
129
+ ]
130
+ ):
131
+ self.labelled_shell_counts[resname][labelled_shell]["accepts_from"][
132
+ d
133
+ ] = 1
134
+ else:
135
+ self.labelled_shell_counts[resname][labelled_shell]["accepts_from"][
136
+ d
137
+ ] += 1
138
+
139
+ if (
140
+ d
141
+ not in self.resid_labelled_shell_counts[resid][resname][labelled_shell][
142
+ "accepts_from"
143
+ ]
144
+ ):
145
+ self.resid_labelled_shell_counts[resid][resname][labelled_shell][
146
+ "accepts_from"
147
+ ][d] = 1
148
+ else:
149
+ self.resid_labelled_shell_counts[resid][resname][labelled_shell][
150
+ "accepts_from"
151
+ ][d] += 1
152
+
153
+
154
+ def get_HB_labels(atom_idx: int, system, HBs: HBCollection, shells: ShellCollection):
155
+ """
156
+ For a given central atom, get what UAs it donates and accepts from, then
157
+ find what shell labels these correspond to. Update the shells class
158
+ instance with this information
159
+
160
+ :param atom_idx: atom index to find HB labels for
161
+ :param system: mdanalysis instance of all atoms in current frame
162
+ :param HBs: HBCollection class instance
163
+ :param shells: ShellCollection class instance
164
+ """
165
+ shell = shells.find_shell(atom_idx)
166
+ accepts_from_labels = []
167
+ donates_to_labels = []
168
+ if shell:
169
+ # 1.check if HB donating to and accepting from have previously
170
+ # been found
171
+ if shell.labels:
172
+ # 2. Find what UAs the central atom donates to and accepts from
173
+ # with the HBs class instance
174
+ donates_to = HBs.find_acceptor(atom_idx)
175
+ accepts_from = HBs.find_donators(atom_idx)
176
+ if accepts_from:
177
+ for d_idx in accepts_from:
178
+ # 3. check if UA being accepted from is in shell of
179
+ # central UA and add label to list
180
+ bonded_UA = Selections.find_bonded_heavy_atom(d_idx, system)
181
+ if bonded_UA.index in shell.UA_shell:
182
+ shell_idx = shell.UA_shell.index(bonded_UA.index)
183
+ accepts_from_labels.append(shell.labels[shell_idx])
184
+ else:
185
+ # don't add donating neighbour if not in central UA
186
+ # shell
187
+ continue
188
+
189
+ if donates_to:
190
+ # 4. iterate through acceptors and add labels
191
+ for d_idx, a_idx in donates_to.items():
192
+ acceptor = system.atoms[a_idx]
193
+ if acceptor.mass < 1.1:
194
+ acceptor = Selections.find_bonded_heavy_atom(a_idx, system)
195
+ shell_idx = shell.UA_shell.index(acceptor.index)
196
+ donates_to_labels.append(shell.labels[shell_idx])
197
+ # 5. Add labelled neighbours to shells instance
198
+ shell.donates_to_labels = donates_to_labels
199
+ shell.accepts_from_labels = accepts_from_labels
@@ -0,0 +1,96 @@
1
+ """
2
+ These functions calculate coordination shells using RAD the relative
3
+ angular distance.
4
+ """
5
+
6
+ import numpy as np
7
+
8
+ from waterEntropy.analysis.shells import ShellCollection
9
+ import waterEntropy.maths.trig as Trig
10
+
11
+
12
+ def get_RAD_neighbours(i_coords, sorted_indices, sorted_distances, system):
13
+ # pylint: disable=too-many-locals
14
+ r"""
15
+ For a given set of atom coordinates, find its RAD shell from the distance
16
+ sorted atom list, truncated to the closests 30 atoms.
17
+
18
+ This function calculates coordination shells using RAD the relative
19
+ angular distance, as defined first in DOI:10.1063/1.4961439
20
+ where united atoms (heavy atom + bonded Hydrogens) are defined as neighbours if
21
+ they fulfil the following condition:
22
+
23
+ .. math::
24
+ \Bigg(\frac{1}{r_{ij}}\Bigg)^2>\Bigg(\frac{1}{r_{ik}}\Bigg)^2 \cos \theta_{jik}
25
+
26
+ For a given particle :math:`i`, neighbour :math:`j` is in its coordination
27
+ shell if :math:`k` is not blocking particle :math:`j`. In this implementation
28
+ of RAD, we enforce symmetry, whereby neighbouring particles must be in each
29
+ others coordination shells.
30
+
31
+ :param i_coords: xyz coordinates of atom :math:`i`
32
+ :param sorted_indices: list of atom indices sorted from closest to
33
+ furthest from atom :math:`i`
34
+ :param sorted_distances: list of atom distances sorted from closest to
35
+ furthest from atom :math:`i`
36
+ :param system: mdanalysis instance of atoms in a frame
37
+ """
38
+ # 1. truncate neighbour list to closest 30 united atoms
39
+ range_limit = min(len(sorted_distances), 30)
40
+ shell = []
41
+ count = -1
42
+ # 2. iterate through neighbours from closest to furthest
43
+ for y in sorted_indices[:range_limit]:
44
+ count += 1
45
+ y_idx = np.where(sorted_indices == y)[0][0]
46
+ j = system.atoms.indices[y]
47
+ j_coords = system.atoms.positions[y]
48
+ rij = sorted_distances[y_idx]
49
+ blocked = False
50
+ # 3. iterate through neighbours than atom j and check if they block
51
+ # it from atom i
52
+ for z in sorted_indices[:count]: # only closer UAs can block
53
+ z_idx = np.where(sorted_indices == z)[0][0]
54
+ k_coords = system.atoms.positions[z]
55
+ rik = sorted_distances[z_idx]
56
+ # 4. find the angle jik
57
+ costheta_jik = Trig.get_angle(
58
+ j_coords, i_coords, k_coords, system.dimensions[:3]
59
+ )
60
+ if np.isnan(costheta_jik):
61
+ break
62
+ # 5. check if k blocks j from i
63
+ LHS = (1 / rij) ** 2
64
+ RHS = ((1 / rik) ** 2) * costheta_jik
65
+ if LHS < RHS:
66
+ blocked = True
67
+ break
68
+ # 6. if j is not blocked from i by k, then its in i's shell
69
+ if blocked is False:
70
+ shell.append(j)
71
+ return shell
72
+
73
+
74
+ def get_RAD_shell(UA, system, shells: ShellCollection):
75
+ """
76
+ For a given united atom, find its RAD shell, returning the atom indices
77
+ for the heavy atoms that are in its shell.
78
+
79
+ :param UA: mdanalysis instance of a united atom in a frame
80
+ :param system: mdanalysis instance of atoms in a frame
81
+ :param shells: ShellCollection instance
82
+ """
83
+ # 1. first check if a shell has already been found for this UA
84
+ shell = shells.find_shell(UA.index)
85
+ if not shell:
86
+ # 2. get the nearest neighbours for the UA, sorted from closest to
87
+ # furthest
88
+ sorted_indices, sorted_distances = Trig.get_sorted_neighbours(UA.index, system)
89
+ # 3. now find the RAD shell of the UA
90
+ shell_indices = get_RAD_neighbours(
91
+ UA.position, sorted_indices, sorted_distances, system
92
+ )
93
+ # 4. populate the class instance for RAD shells
94
+ shells.add_data(UA.index, shell_indices)
95
+ shell = shells.find_shell(UA.index)
96
+ return shell
@@ -0,0 +1 @@
1
+ """waterEntropy: calculate water entropy near heterogeneous interfaces"""
@@ -0,0 +1,102 @@
1
+ """
2
+ Label neighbours in a coordination shell based on what they are and what their
3
+ neighbours are.
4
+ """
5
+
6
+ import waterEntropy.analysis.RAD as RADShell
7
+ from waterEntropy.analysis.shells import ShellCollection
8
+
9
+
10
+ def get_shell_labels(atom_idx: int, system, shell, shells: ShellCollection):
11
+ """
12
+ Get the shell labels of an atoms shell based on the following:
13
+ For a central UA, rank its coordination shell by proximity to that
14
+ central UA's nearest non-like molecule UA.
15
+
16
+ * '#_RESNAME' = RAD shell from same molecule type, when nearest nonlike resid is the same as the reference.
17
+
18
+ * 'X_RESNAME' = when same molecule type has different nearest nonlike resid.
19
+
20
+ * 'RESNAME' = when molecule of different type is in RAD shell.
21
+
22
+ * '0_RESNAME' = closest different type molecule in RAD shell. (the one its assigned to, its nearest non-like!)
23
+
24
+ :param atom_idx: atom index of central atom in coordination shell
25
+ :param system: mdanalysis instance of atoms in a frame
26
+ :param shell: shell instance of atom_idx
27
+ :param shells: ShellCollection instance
28
+ """
29
+ center = system.atoms[atom_idx]
30
+ # 1. find the closest different UA in a shell
31
+ # different = not the same resname
32
+ nearest_nonlike_idx = get_nearest_nonlike(shell, system)
33
+ # 2. only find labels if a solute is in the shell
34
+ if nearest_nonlike_idx is not None:
35
+ nearest_nonlike = system.atoms[nearest_nonlike_idx]
36
+ shell_labels = []
37
+ for n in shell.UA_shell:
38
+ neighbour = system.atoms[n]
39
+ # 3a. label nearest nonlike atom as "0_RESNAME"
40
+ if neighbour.index == nearest_nonlike.index:
41
+ shell_labels.append(f"0_{neighbour.resname}")
42
+ # 3b. label other nonlike atoms as "RESNAME"
43
+ if (
44
+ neighbour.index != nearest_nonlike.index
45
+ and neighbour.resname != center.resname
46
+ ):
47
+ shell_labels.append(neighbour.resname)
48
+ # 3c. find RAD shells for shell constituents with same resname
49
+ # as central atom
50
+ if (
51
+ neighbour.index != nearest_nonlike.index
52
+ and neighbour.resname == center.resname
53
+ ):
54
+ neighbour_shell = shells.find_shell(neighbour.index)
55
+ if not neighbour_shell:
56
+ neighbour_shell = RADShell.get_RAD_shell(neighbour, system, shells)
57
+ # 3d. find nearest nonlike of neighbours with same resname
58
+ # as central atom
59
+ neighbour_nearest_nonlike_idx = get_nearest_nonlike(
60
+ neighbour_shell, system
61
+ )
62
+ # 3e. if neighbour has a pure shell, then it is in the second
63
+ # shell of the nearest nonlike
64
+ if neighbour_nearest_nonlike_idx is None:
65
+ shell_labels.append(f"2_{neighbour.resname}")
66
+ else:
67
+ # 3f. if neighbours nearest nonlike is the same atom as
68
+ # central atom, assume it is in the first shell
69
+ # if neighbour_nearest_nonlike_idx == nearest_nonlike_idx:
70
+ neighbour_nearest_nonlike = system.atoms[
71
+ neighbour_nearest_nonlike_idx
72
+ ]
73
+ # 3g. if neighbours nearest nonlike is in the same resid as
74
+ # central atom, assume it is in the first shell
75
+ if neighbour_nearest_nonlike.resid == nearest_nonlike.resid:
76
+ shell_labels.append(f"1_{neighbour.resname}")
77
+ else:
78
+ # 3h. if neighbours nearest nonlike is not the same resid
79
+ # as central nearest resid, it is in the first shell
80
+ # of a different resid and labelled as "X_RESNAME"
81
+ shell_labels.append(f"X_{neighbour.resname}")
82
+ shell.labels = shell_labels # sorted(shell_labels) #don't sort yet
83
+ shell.nearest_nonlike_idx = nearest_nonlike.index
84
+ return shell
85
+
86
+
87
+ def get_nearest_nonlike(shell, system):
88
+ """
89
+ For a given shell, find the closest neighbour that is not the same
90
+ atom/molecule type as the central united atom.
91
+
92
+ :param shell: shell instance of an atom
93
+ :param system: mdanalysis instance of atoms in a frame
94
+ """
95
+ nearest_nonlike_idx = None
96
+ center = system.atoms[shell.atom_idx]
97
+ for n in shell.UA_shell:
98
+ neighbour = system.atoms[n]
99
+ if neighbour.resname != center.resname and neighbour.type != center.type:
100
+ nearest_nonlike_idx = n
101
+ break
102
+ return nearest_nonlike_idx
@@ -0,0 +1,80 @@
1
+ """Store shell information here"""
2
+
3
+
4
+ class Shell:
5
+ """Represent a single atom shell with dynamic properties, which are set in
6
+ the ShellCollection class."""
7
+
8
+ def __init__(self, atom_idx):
9
+ object.__setattr__(self, "atom_idx", atom_idx) # Store atom index directly
10
+ object.__setattr__(self, "properties", {}) # Internal dictionary
11
+
12
+ def __getattr__(self, key: str):
13
+ """Allow dot notation access to stored properties.
14
+
15
+ :param key: name of the property in the properties dictionary
16
+ """
17
+ if key in self.properties:
18
+ return self.properties[key]
19
+ raise AttributeError(f"Property '{key}' not found for atom {self.atom_idx}")
20
+
21
+ def __setattr__(self, key: str, value):
22
+ """Allow adding properties dynamically, except for 'atom_idx'.
23
+
24
+ :param key: name of the property being added to the properties dictionary
25
+ :param value: value for the key being added to the properties dictionary
26
+ """
27
+ if key == "atom_idx": # Ensure atom index remains immutable
28
+ object.__setattr__(self, key, value)
29
+ else:
30
+ self.properties[key] = value # Store additional properties
31
+
32
+ def __repr__(self):
33
+ """Print the atom and its properties."""
34
+ return f"Shell(atom_idx={self.atom_idx}, properties={self.properties})"
35
+
36
+
37
+ class ShellCollection:
38
+ """Manage multiple atom shells and allow adding/updating properties."""
39
+
40
+ def __init__(self):
41
+ self.shells = {} # Dictionary storing Shell objects indexed by atom index
42
+
43
+ def add_data(self, atom_idx: int, UA_shell: list[int]):
44
+ """Add a new atom shell to the collection if it doesn't exist and set
45
+ various properties used to describe the class.
46
+
47
+ :param atom_idx: the heavy atom index of the central atom in a shell
48
+ :param UA_shell: the list of heavy atom indices in the shell of atom_idx
49
+ """
50
+ if atom_idx not in self.shells:
51
+ self.shells[atom_idx] = Shell(atom_idx)
52
+ self.set_property(atom_idx, "atom_idx", atom_idx)
53
+ self.set_property(atom_idx, "UA_shell", UA_shell)
54
+ self.set_property(atom_idx, "nearest_nonlike_idx", None)
55
+ self.set_property(atom_idx, "labels", None)
56
+ self.set_property(atom_idx, "donates_to_labels", None)
57
+ self.set_property(atom_idx, "accepts_from_labels", None)
58
+
59
+ def set_property(self, atom_idx, key: str, value):
60
+ """Set a property for a specific atom shell, ensuring it exists first.
61
+
62
+ :param atom_idx: the heavy atom index of the central atom in a shell
63
+ :param key: the name of the key being added to the shells dictionary
64
+ :param value: the value of the key being added to the shells dictionary
65
+ """
66
+ if atom_idx not in self.shells:
67
+ self.shells[atom_idx] = Shell(atom_idx) # Auto-create atom if missing
68
+ setattr(self.shells[atom_idx], key, value)
69
+
70
+ def find_shell(self, atom_idx: int):
71
+ """
72
+ Get the shell instance if atom_idx is a key in shells dictionary
73
+
74
+ :param atom_idx: index of central atom in shell
75
+ """
76
+ return self.shells.get(atom_idx, None)
77
+
78
+ def __repr__(self):
79
+ """Return a dictionary-like representation of stored atoms."""
80
+ return repr(self.shells)