1 | # Test whether add_coordset() assigns the correct atom coordinates.
|
---|
2 |
|
---|
3 | import numpy as np
|
---|
4 |
|
---|
5 | from chimerax.atomic import AtomicStructure
|
---|
6 |
|
---|
7 | elements = ["O", "C", "H", "H"]
|
---|
8 | atom_names = ["O1", "C1", "H1", "H2"]
|
---|
9 |
|
---|
10 | coords_array = np.array([[0.00000, 0.00000, 0.68769], \
|
---|
11 | [0.00000, 0.00000, -0.53966], \
|
---|
12 | [0.00000, 0.93947, -1.13180], \
|
---|
13 | [0.00000, -0.93947, -1.13180]])
|
---|
14 |
|
---|
15 | trans_coords = coords_array + 1
|
---|
16 |
|
---|
17 | coordsets_array = np.array([coords_array, trans_coords])
|
---|
18 |
|
---|
19 | mdl = AtomicStructure(session, name="test")
|
---|
20 | res = mdl.new_residue("res", "a", 1)
|
---|
21 |
|
---|
22 | for i, (ele, name) in enumerate(zip(elements, atom_names)):
|
---|
23 | atom = mdl.new_atom(name, ele)
|
---|
24 | atom.coord = coords_array[i]
|
---|
25 | res.add_atom(atom)
|
---|
26 |
|
---|
27 | #this works
|
---|
28 | mdl.add_coordsets(coordsets_array, replace=True)
|
---|
29 | for cid in mdl.coordset_ids:
|
---|
30 | print("coordset", cid)
|
---|
31 | print(mdl.coordset(cid).xyzs)
|
---|
32 | print("atom coords:")
|
---|
33 | mdl.active_coordset_id = cid
|
---|
34 | for atom in mdl.atoms:
|
---|
35 | print(atom.coord)
|
---|
36 |
|
---|
37 | #the issue happens when removing and adding atoms
|
---|
38 | mdl.active_coordset_id = 1
|
---|
39 | new_atom = mdl.new_atom(atom_names[-1], elements[-1])
|
---|
40 | res.add_atom(atom)
|
---|
41 | new_atom.coord = coords_array[3]
|
---|
42 |
|
---|
43 | mdl.atoms[3].delete()
|
---|
44 |
|
---|
45 | print("replaced last atom")
|
---|
46 | mdl.add_coordsets(coordsets_array, replace=True)
|
---|
47 | for cid in mdl.coordset_ids:
|
---|
48 | print("coordset", cid)
|
---|
49 | print(mdl.coordset(cid).xyzs)
|
---|
50 | print("atom coords:")
|
---|
51 | mdl.active_coordset_id = cid
|
---|
52 | for atom in mdl.atoms:
|
---|
53 | print(atom.coord)
|
---|
54 |
|
---|
55 | #XXX these values differ
|
---|
56 | print("coordinates of last atom from atom attribute:", mdl.atoms[-1].coord)
|
---|
57 | print("coordinates of last atom from coordset:", mdl.coordset(mdl.active_coordset_id).xyzs[-1])
|
---|
58 |
|
---|
59 | #make a new coordset array with a position for the deleted atom
|
---|
60 | coordset_indices = mdl.atoms.coord_indices
|
---|
61 |
|
---|
62 | new_coordsets_array = np.zeros((2, 5, 3))
|
---|
63 | for old_coordset, new_coordset in zip(coordsets_array, new_coordsets_array):
|
---|
64 | new_coordset[coordset_indices] = old_coordset
|
---|
65 |
|
---|
66 | print("trying to add these coordsets with an index for the deleted atom:")
|
---|
67 | for i, coordset in enumerate(new_coordsets_array):
|
---|
68 | print(i+1)
|
---|
69 | print(coordset)
|
---|
70 |
|
---|
71 | #XXX mdl.add_coordsets is not allowed because my coordsets should be 5 long and mdl.num_atoms == 4
|
---|
72 | #mdl.add_coordsets(new_coordsets_array, replace=True)
|
---|
73 |
|
---|
74 | mdl.remove_coordsets()
|
---|
75 | for i, coordset in enumerate(new_coordsets_array):
|
---|
76 | mdl.add_coordset(i+1, coordset)
|
---|
77 |
|
---|
78 | print("coordsets after coordset change:")
|
---|
79 | for cid in mdl.coordset_ids:
|
---|
80 | print("coordset", cid)
|
---|
81 | print(mdl.coordset(cid).xyzs)
|
---|
82 | print("atom coords:")
|
---|
83 | mdl.active_coordset_id = cid
|
---|
84 | for atom in mdl.atoms:
|
---|
85 | print(atom.coord)
|
---|