Ticket #2926: remove_atoms_add_coords.py

File remove_atoms_add_coords.py, 2.7 KB (added by tony.schaefer@…, 6 years ago)

Added by email2trac

Line 
1# Test whether add_coordset() assigns the correct atom coordinates.
2
3import numpy as np
4
5from chimerax.atomic import AtomicStructure
6
7elements = ["O", "C", "H", "H"]
8atom_names = ["O1", "C1", "H1", "H2"]
9
10coords_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
15trans_coords = coords_array + 1
16
17coordsets_array = np.array([coords_array, trans_coords])
18
19mdl = AtomicStructure(session, name="test")
20res = mdl.new_residue("res", "a", 1)
21
22for 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
28mdl.add_coordsets(coordsets_array, replace=True)
29for 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
38mdl.active_coordset_id = 1
39new_atom = mdl.new_atom(atom_names[-1], elements[-1])
40res.add_atom(atom)
41new_atom.coord = coords_array[3]
42
43mdl.atoms[3].delete()
44
45print("replaced last atom")
46mdl.add_coordsets(coordsets_array, replace=True)
47for 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
56print("coordinates of last atom from atom attribute:", mdl.atoms[-1].coord)
57print("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
60coordset_indices = mdl.atoms.coord_indices
61
62new_coordsets_array = np.zeros((2, 5, 3))
63for old_coordset, new_coordset in zip(coordsets_array, new_coordsets_array):
64 new_coordset[coordset_indices] = old_coordset
65
66print("trying to add these coordsets with an index for the deleted atom:")
67for 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
74mdl.remove_coordsets()
75for i, coordset in enumerate(new_coordsets_array):
76 mdl.add_coordset(i+1, coordset)
77
78print("coordsets after coordset change:")
79for 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)