• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CCPBioSim / CodeEntropy / 13726590978

07 Mar 2025 06:07PM UTC coverage: 1.726%. First build
13726590978

push

github

web-flow
Merge pull request #45 from CCPBioSim/39-implement-ci-pipeline-pre-commit-hooks

Implement CI pipeline pre-commit hooks

27 of 1135 new or added lines in 23 files covered. (2.38%)

53 of 3071 relevant lines covered (1.73%)

0.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/CodeEntropy/poseidon/extractData/nearestNonlike2.py
1
#!/usr/bin/env python
2

3
import logging
×
4

5
# import math
NEW
6
import sys
×
NEW
7
from collections import defaultdict  # Counter,
×
8
from datetime import datetime
×
9

10
# import MDAnalysis
11
# import numpy as np
12
# from MDAnalysis import *
13

14
# from MDAnalysis.analysis import distances
15

16
# from CodeEntropy.poseidon.extractData.generalFunctions import (
17
#     calcWaterSoluteAngle,
18
#     distance,
19
# )
20

21

NEW
22
def nested_dict():
×
NEW
23
    return defaultdict(nested_dict)
×
24

25

NEW
26
def getShellAssignment(all_data, excludedResnames, dimensions, startTime, verbosePrint):
×
27
    """
28
    Assign solvent to solvation shells
29
    """
30

NEW
31
    nearestNonlike_dict = {}  # need a dictionary of all UAs that are
×
32
    # assigned as nearest non-likes with values as a list of UAs
33

NEW
34
    if excludedResnames is None:
×
35
        excludedResnames = []
×
36

37
    for x in range(0, len(all_data)):
×
38
        atom = all_data[x]
×
NEW
39
        if atom.mass > 1.1 and atom.nearest_sorted_array is not None:
×
40
            # and len(atom.RAD) != 0:
41
            nearestNonlike = None
×
42
            nearestNonlikeDist = None
×
43
            for atomDist in atom.nearest_sorted_array:
×
NEW
44
                if nearestNonlikeDist is None:
×
45
                    nonlike = all_data[atomDist[0]]
×
NEW
46
                    if (
×
47
                        nonlike.resid != atom.resid
48
                        and nonlike.atom_num not in atom.bonded_to_atom_num
49
                        and nonlike.atom_num != atom.atom_num
50
                        and nonlike.resname != atom.resname
51
                        and nonlike.resname not in excludedResnames
52
                        and len(nonlike.RAD) != 0
53
                    ):
54
                        nearestNonlike = nonlike
×
55
                        nearestNonlikeDist = atomDist[1]
×
56
                else:
57
                    break
×
58

59
            # When nearest nonlike solute is found
NEW
60
            if nearestNonlikeDist is not None:
×
NEW
61
                atom.nearestAnyNonlike = (nearestNonlike, nearestNonlikeDist)
×
62

63
                if nearestNonlike.resid not in nearestNonlike_dict:
×
64
                    nearestNonlike_dict[nearestNonlike.resid] = [[], []]
×
NEW
65
                if nearestNonlike not in nearestNonlike_dict[nearestNonlike.resid][0]:
×
NEW
66
                    nearestNonlike_dict[nearestNonlike.resid][0].append(nearestNonlike)
×
67
                if atom not in nearestNonlike_dict[nearestNonlike.resid][1]:
×
68
                    nearestNonlike_dict[nearestNonlike.resid][1].append(atom)
×
69
            else:
70
                continue
×
71
        else:
72
            continue
×
73

NEW
74
    verbosePrint("NEAREST NON-LIKE ASSIGNMENT")
×
75
    verbosePrint(datetime.now() - startTime)
×
76
    sys.stdout.flush()
×
77

78
    # Part 2: For each nonlike UA with assigned molecules, get prox shell
79
    # This is the slow part of the code - prox shell assignment
80

81
    # Find closest nonlike for each molecule, needed below
82
    # so only one atom per molecule is used in prox assignment, rest
83
    # of atoms in molecules are given the same prox as nearest atom
84
    molecule_resid_nearest_dict = {}
×
85
    for x in range(0, len(all_data)):
×
86
        atom = all_data[x]
×
NEW
87
        if atom.nearestAnyNonlike is not None:
×
88
            nearestNonlike = atom.nearestAnyNonlike[0]
×
89
            dist = atom.nearestAnyNonlike[1]
×
90
            if atom.resid not in molecule_resid_nearest_dict:
×
NEW
91
                molecule_resid_nearest_dict[atom.resid] = [nearestNonlike.resid, dist]
×
92
            elif atom.resid in molecule_resid_nearest_dict:
×
93
                if dist < molecule_resid_nearest_dict[atom.resid][1]:
×
NEW
94
                    molecule_resid_nearest_dict[atom.resid] = [
×
95
                        nearestNonlike.resid,
96
                        dist,
97
                    ]
98
                else:
99
                    continue
×
100
            else:
101
                continue
×
102

103
    multiple_shellList = []
×
104
    multiple_atomNumList = []
×
105
    localMols = 0
×
106

107
    for resid, nonlike_atom_lists in nearestNonlike_dict.items():
×
108
        nonlike_list = nonlike_atom_lists[0]
×
109
        atom_list = nonlike_atom_lists[1]
×
110

111
        for nonlike in nonlike_list:
×
112

113
            if len(nonlike.RAD) != 0:
×
114
                for neighbour in nonlike.RAD:
×
115
                    X = neighbour
×
NEW
116
                    if (
×
117
                        X in atom_list
118
                        and X.atom_num not in multiple_atomNumList
119
                        and molecule_resid_nearest_dict[X.resid][0] == resid
120
                    ):
121
                        for molecule_atom in X.molecule_atomNums:
×
NEW
122
                            ma = all_data[molecule_atom]
×
123
                            multiple_shellList.append([ma, 1])
×
124
                            multiple_atomNumList.append(ma.atom_num)
×
125
                            ma.hydrationShellRAD_solute = 1
×
126
                            localMols += 1
×
127
                    else:
128
                        continue
×
129

130
            if len(nonlike.RAD) == 0:
×
NEW
131
                logging.warning("no RAD nonlike:", nonlike)
×
132

133
    # Anything beyond the 1st shell is assigned to 2nd
134
    for x in range(0, len(all_data)):
×
135
        atom = all_data[x]
×
NEW
136
        if atom.hydrationShellRAD_solute is None and atom.mass > 1.1:
×
137
            atom.hydrationShellRAD_solute = 2
×
138
        else:
139
            continue
×
140

141

142
def moleculePositionRankingRAD(all_data, waterTuple, dimensions):
×
143
    """
144
    For each UA, rank its RAD shell by proximity to that
145
    references UAs nearest Non-like molecule UA.
146
    num = RAD shell from same molecule type, when nearest nonlike
147
        resid is the same as the reference.
148
    'X' = when same molecule type has different nearest nonlike resid.
149
    'RESNAME' = when molecule of different type is in RAD shell.
150
    '0' = closest different type molecule in RAD shell.
151
            (the one its assigned to, its nearest non-like!)
152

153

154
    Extra:
155
    for each 'X' found in a RAD shell, use this information
156
    to find neighbouring zones. Think voronoi but with zones.
157
    For each molecule that defines a zone, find it's neighbouring zones
158
    from X's in shell type.
159

160

161
    The following should not occur with new HB defn (HB in RAD shell only):
162
    'AX' = acceptors not in RAD shell but same molecule type as ref.
163
    'AS' = acceptors not in RAD shell and diff mol. type to ref.
164
    'DX' = donor equivalent to 'AX'
165
    'DS' = donor equivalent to 'AS'
166
    """
167

168
    for x in range(0, len(all_data)):
×
169
        atom = all_data[x]
×
170
        if atom.mass > 1.1:
×
171

172
            # part 1: get ranking of RAD Nc according to extended shells.
173
            shell_list_ranked = []
×
174
            shell_list_atom_nums = []
×
175

176
            for neighbour in atom.RAD:
×
177
                shell_list_atom_nums.append(neighbour.atom_num)
×
178
                # include Hs in shell_list_atom_nums
179
                for bonded in neighbour.bonded_to_atom_num:
×
180
                    b = all_data[bonded]
×
181
                    if b.mass < 1.1:
×
182
                        shell_list_atom_nums.append(b.atom_num)
×
183
                    else:
184
                        continue
×
185

NEW
186
            if len(atom.RAD) != 0 and atom.nearestAnyNonlike is not None:
×
187

188
                for neighbour in atom.RAD:
×
NEW
189
                    if neighbour.nearestAnyNonlike is not None:
×
NEW
190
                        if (
×
191
                            neighbour.nearestAnyNonlike[0].resid
192
                            == atom.nearestAnyNonlike[0].resid
193
                            and neighbour.resname == atom.resname
194
                        ):
195
                            shell_list_ranked.append(
×
196
                                (
197
                                    str(neighbour.hydrationShellRAD_solute),
198
                                    neighbour.atom_num,
199
                                )
200
                            )
201

NEW
202
                        if (
×
203
                            neighbour.nearestAnyNonlike[0].resid
204
                            != atom.nearestAnyNonlike[0].resid
205
                            and neighbour.resname == atom.resname
206
                        ):
NEW
207
                            shell_list_ranked.append(("X", neighbour.atom_num))
×
208

NEW
209
                        if (
×
210
                            neighbour.resname != atom.resname
211
                            and neighbour.resid == atom.nearestAnyNonlike[0].resid
212
                        ):
NEW
213
                            if neighbour.atom_num == atom.nearestAnyNonlike[0].atom_num:
×
NEW
214
                                shell_list_ranked.append(("0", neighbour.atom_num))
×
215
                            else:
NEW
216
                                shell_list_ranked.append(
×
217
                                    (
218
                                        "%s_%s"
219
                                        % (neighbour.resname, neighbour.atom_name),
220
                                        neighbour.atom_num,
221
                                    )
222
                                )
223

NEW
224
                        if (
×
225
                            neighbour.resid != atom.nearestAnyNonlike[0].resid
226
                            and neighbour.resname != atom.resname
227
                        ):
NEW
228
                            shell_list_ranked.append(
×
229
                                (
230
                                    "%s_%s" % (neighbour.resname, neighbour.atom_name),
231
                                    neighbour.atom_num,
232
                                )
233
                            )
234

235
                    else:
236
                        continue
×
237

238
            # part 2: get HBing info wrt RAD Nc ranking, only for water
239

240
            acceptor_list = []
×
241
            donor_list = []
×
242

NEW
243
            if atom.hydrationShellRAD_solute == 1 and atom.resname in waterTuple:
×
244

245
                # what Hs is atom accepting from:
246
                if len(atom.nearest_Hs) != 0:
×
247
                    for H in atom.nearest_Hs:
×
248
                        for bonded_atom_num in H.bonded_to_atom_num:
×
249
                            bonded = all_data[bonded_atom_num]
×
250
                            if bonded.mass > 1.1:
×
251
                                X = bonded
×
252
                                if X.atom_num in shell_list_atom_nums:
×
253
                                    for rank_num in shell_list_ranked:
×
254
                                        rank = rank_num[0]
×
255
                                        num = rank_num[1]
×
256
                                        if num == X.atom_num:
×
257
                                            acceptor_list.append((rank, num))
×
258
                                        else:
259
                                            continue
×
NEW
260
                                elif (
×
261
                                    X.atom_num not in shell_list_atom_nums
262
                                    and X.resname == atom.resname
263
                                ):
NEW
264
                                    acceptor_list.append(("AX", X.atom_num))
×
NEW
265
                                    logging.warning("AX")
×
266

NEW
267
                                elif (
×
268
                                    X.atom_num not in shell_list_atom_nums
269
                                    and X.resname != atom.resname
270
                                ):
NEW
271
                                    acceptor_list.append(("AS", X.atom_num))
×
NEW
272
                                    logging.warning("AS")
×
273
                                else:
274
                                    continue
×
275

276
                # what atoms are bonded Hs donating to:
277
                for bonded_atom_num in atom.bonded_to_atom_num:
×
278
                    H = all_data[bonded_atom_num]
×
NEW
279
                    if H.mass < 1.1 and H.nearest_atom is not None:
×
280
                        X = H.nearest_atom
×
281
                        if X.mass > 1.1:
×
282
                            if X.atom_num in shell_list_atom_nums:
×
283
                                for rank_num in shell_list_ranked:
×
284
                                    rank = rank_num[0]
×
285
                                    num = rank_num[1]
×
286
                                    if num == X.atom_num:
×
287
                                        # treat a broken HB as a SD for now
288
                                        if (rank, num) not in donor_list:
×
289
                                            donor_list.append((rank, num))
×
NEW
290
                                            break
×
291
                                    else:
292
                                        continue
×
293
                            # check donating inside RAD shell
NEW
294
                            elif (
×
295
                                X.atom_num not in shell_list_atom_nums
296
                                and X.resname == atom.resname
297
                            ):
NEW
298
                                donor_list.append(("DX", X.atom_num))
×
NEW
299
                                logging.warning("DX", atom.atom_name, X.atom_name)
×
NEW
300
                            elif (
×
301
                                X.atom_num not in shell_list_atom_nums
302
                                and X.resname != atom.resname
303
                            ):
NEW
304
                                donor_list.append(("DS", X.atom_num))
×
NEW
305
                                logging.warning("DS", atom.atom_name, X.atom_name)
×
306
                            else:
307
                                continue
×
308

309
                        # dealing with H-H interactions.
310
                        elif X.mass < 1.1:
×
311
                            UA_X = None
×
312
                            for bonded_atom_num2 in X.bonded_to_atom_num:
×
313
                                UA_X = all_data[bonded_atom_num2]
×
NEW
314
                                if (
×
315
                                    UA_X.mass > 1.1
316
                                    and UA_X.atom_num in shell_list_atom_nums
317
                                ):
318
                                    for rank_num in shell_list_ranked:
×
319
                                        rank = rank_num[0]
×
320
                                        num = rank_num[1]
×
321
                                        if num == UA_X.atom_num:
×
322
                                            # treat a H-H interaction as SD and
323
                                            # as unique to H-X interaction.
NEW
324
                                            if ("H", X.atom_num) not in donor_list:
×
NEW
325
                                                donor_list.append(("H", X.atom_num))
×
326
                                            else:
327
                                                continue
×
328
                                        else:
329
                                            continue
×
NEW
330
                                elif (
×
331
                                    UA_X.mass > 1.1
332
                                    and UA_X.atom_num not in shell_list_atom_nums
333
                                ):
NEW
334
                                    donor_list.append(("DH", X.atom_num))
×
NEW
335
                                    logging.warning(
×
336
                                        "DH", atom.atom_name, UA_X.atom_name
337
                                    )
338
                                else:
339
                                    continue
×
340

341
            shell_list_ranked.sort(key=lambda x: x[0])
×
342
            acceptor_list.sort(key=lambda x: x[0])
×
343
            donor_list.sort(key=lambda x: x[0])
×
344

345
            shell = [i[0] for i in shell_list_ranked]
×
346
            acceptors = [i[0] for i in acceptor_list]
×
347
            donors = [i[0] for i in donor_list]
×
348

349
            atom.RAD_shell_ranked = (shell, acceptors, donors)
×
350

351
            # create a list for RAD neighbours and relative
352
            # distances (QQ/r^2)
353

354
            RAD_nAtoms = []
×
355
            for neighbour in atom.RAD:
×
NEW
356
                RAD_nAtoms.append(
×
357
                    [neighbour.atom_name, neighbour.resid, neighbour.resname]
358
                )
359

360
            atom.RAD_nAtoms = RAD_nAtoms
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc