-
Notifications
You must be signed in to change notification settings - Fork 22
/
Topology.jl
298 lines (265 loc) · 8.72 KB
/
Topology.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# Chemfiles.jl, a modern library for chemistry file reading and writing
# Copyright (C) Guillaume Fraux and contributors -- BSD license
export bonds_count, angles_count, dihedrals_count, impropers_count
export bonds, angles, dihedrals, impropers
export add_bond!, remove_bond!, clear_bonds!, bond_order, bond_orders
export add_residue!, residues, are_linked, count_residues
__ptr(topology::Topology) = __ptr(topology.__handle)
__const_ptr(topology::Topology) = __const_ptr(topology.__handle)
"""
Possible bond orders in Chemfiles:
- `Chemfiles.UnknownBond`: when the bond order is not specified
- `Chemfiles.SingleBond`: for single bonds
- `Chemfiles.DoubleBond`: for double bonds
- `Chemfiles.TripleBond`: for triple bonds
- `Chemfiles.QuadrupleBond`: for quadruple bonds (present in some metals)
- `Chemfiles.QuintupletBond`: for qintuplet bonds (present in some metals)
- `Chemfiles.AmideBond`: for amide bonds
- `Chemfiles.AromaticBond`: for aromatic bonds
"""
@enum BondOrder begin
UnknownBond = lib.CHFL_BOND_UNKNOWN
SingleBond = lib.CHFL_BOND_SINGLE
DoubleBond = lib.CHFL_BOND_DOUBLE
TripleBond = lib.CHFL_BOND_TRIPLE
QuadrupleBond = lib.CHFL_BOND_QUADRUPLE
QuintupletBond = lib.CHFL_BOND_QUINTUPLET
AmideBond = lib.CHFL_BOND_AMIDE
AromaticBond = lib.CHFL_BOND_AROMATIC
end
"""
Create an empty `Topology`.
"""
function Topology()
ptr = @__check_ptr(lib.chfl_topology())
return Topology(CxxPointer(ptr, is_const=false))
end
"""
Get a copy of the `Topology` of the given `frame`.
"""
function Topology(frame::Frame)
ptr = @__check_ptr(lib.chfl_topology_from_frame(__const_ptr(frame)))
topology = Topology(CxxPointer(ptr, is_const=true))
copy = deepcopy(topology)
finalize(topology)
return copy
end
"""
Get the `Topology` size, i.e. the current number of atoms.
"""
function Base.size(topology::Topology)
count = Ref{UInt64}(0)
__check(lib.chfl_topology_atoms_count(
__const_ptr(topology), count
))
return Int(count[])
end
"""
Add an `atom` at the end of a `topology`.
"""
function add_atom!(topology::Topology, atom::Atom)
__check(lib.chfl_topology_add_atom(__ptr(topology), __const_ptr(atom)))
return nothing
end
"""
Remove the atom at the given `index` from a `topology`.
"""
function remove_atom!(topology::Topology, index::Integer)
__check(lib.chfl_topology_remove(__ptr(topology), UInt64(index)))
return nothing
end
"""
Get the number of bonds in the `topology`.
"""
function bonds_count(topology::Topology)
count = Ref{UInt64}(0)
__check(lib.chfl_topology_bonds_count(__const_ptr(topology), count))
return Int(count[])
end
"""
Get the number of angles in the `topology`.
"""
function angles_count(topology::Topology)
count = Ref{UInt64}(0)
__check(lib.chfl_topology_angles_count(__const_ptr(topology), count))
return Int(count[])
end
"""
Get the number of dihedral angles in the `topology`.
"""
function dihedrals_count(topology::Topology)
count = Ref{UInt64}(0)
__check(lib.chfl_topology_dihedrals_count(__const_ptr(topology), count))
return Int(count[])
end
"""
Get the number of improper angles in the `topology`.
"""
function impropers_count(topology::Topology)
count = Ref{UInt64}(0)
__check(lib.chfl_topology_impropers_count(__const_ptr(topology), count))
return Int(count[])
end
"""
Get the bonds in the `topology`, in a `2 x bonds_count(topology)` array.
"""
function bonds(topology::Topology)
count = UInt64(bonds_count(topology))
result = Array{UInt64}(undef, 2, count)
__check(lib.chfl_topology_bonds(__const_ptr(topology), pointer(result), count))
return result
end
"""
Get the angles in the `topology`, in a `3 x angles_count(topology)` array.
"""
function angles(topology::Topology)
count = UInt64(angles_count(topology))
result = Array{UInt64}(undef, 3, count)
__check(lib.chfl_topology_angles(__const_ptr(topology), pointer(result), count))
return result
end
"""
Get the dihedral angles in the `topology`, in a `4 x dihedrals_count(topology)`
array.
"""
function dihedrals(topology::Topology)
count = UInt64(dihedrals_count(topology))
result = Array{UInt64}(undef, 4, count)
__check(lib.chfl_topology_dihedrals(__const_ptr(topology), pointer(result), count))
return result
end
"""
Get the improper angles in the `topology`, in a `4 x impropers_count(topology)`
array.
"""
function impropers(topology::Topology)
count = UInt64(impropers_count(topology))
result = Array{UInt64}(undef, 4, count)
__check(lib.chfl_topology_impropers(__const_ptr(topology), pointer(result), count))
return result
end
"""
Add a bond between the atoms `i` and `j` in the `topology`, optionaly
setting the bond `order`.
"""
function add_bond!(topology::Topology, i::Integer, j::Integer, order=nothing)
if order === nothing
__check(lib.chfl_topology_add_bond(__ptr(topology), UInt64(i), UInt64(j)))
else
# Check that the order is a valid BondOrder
order = BondOrder(Integer(order))
__check(lib.chfl_topology_bond_with_order(
__ptr(topology), UInt64(i), UInt64(j), lib.chfl_bond_order(order)
))
end
return nothing
end
"""
Remove any existing bond between the atoms `i` and `j` in the `topology`.
"""
function remove_bond!(topology::Topology, i::Integer, j::Integer)
__check(lib.chfl_topology_remove_bond(
__ptr(topology), UInt64(i), UInt64(j)
))
return nothing
end
"""
Remove all bonds, angles and dihedral angles from this `Topology`.
"""
function clear_bonds!(topology::Topology)
__check(lib.chfl_topology_clear_bonds(__ptr(topology)))
return nothing
end
"""
Get the `BondOrder` for the bond between atoms `i` and `j` in the
`topology`.
"""
function bond_order(topology::Topology, i::Integer, j::Integer)
order = Ref{lib.chfl_bond_order}(lib.CHFL_BOND_UNKNOWN)
__check(lib.chfl_topology_bond_order(
__const_ptr(topology), UInt64(i), UInt64(j), order
))
return BondOrder(order[])
end
"""
Get the `BondOrder` for all the bonds in the `topology`.
"""
function bond_orders(topology::Topology)
count = UInt64(bonds_count(topology))
result = Array{lib.chfl_bond_order}(undef, count)
__check(lib.chfl_topology_bond_orders(
__const_ptr(topology), pointer(result), count
))
return map(BondOrder, result)
end
"""
Add a copy of `residue` to this `topology`.
The residue id must not already be in the topology, and the residue must
contain only atoms that are not already in another residue.
"""
function add_residue!(topology::Topology, residue::Residue)
__check(lib.chfl_topology_add_residue(
__ptr(topology), __const_ptr(residue)
))
return nothing
end
"""
Get the number of residues in the `topology`.
"""
function count_residues(topology::Topology)
count = Ref{UInt64}(0)
__check(lib.chfl_topology_residues_count(
__const_ptr(topology), count
))
return Int(count[])
end
"""
Check if the two residues `first` and `second` from the `topology` are
linked together, *i.e.* if there is a bond between one atom in the first
residue and one atom in the second one.
"""
function are_linked(topology::Topology, first::Residue, second::Residue)
result = Ref{UInt8}(0)
__check(lib.chfl_topology_residues_linked(
__const_ptr(topology), __const_ptr(first), __const_ptr(second), result
))
return convert(Bool, result[])
end
"""
Resize the `topology` to hold `natoms` atoms. If the new number of atoms is
bigger than the current number, new atoms will be created with an empty name
and type.
If it is lower than the current number of atoms, the last atoms will be removed,
together with the associated bonds, angles and dihedrals.
"""
function Base.resize!(topology::Topology, size::Integer)
__check(lib.chfl_topology_resize(
__ptr(topology), UInt64(size)
))
end
"""
Make a deep copy of a `topology`.
"""
function Base.deepcopy(topology::Topology)
ptr = lib.chfl_topology_copy(__const_ptr(topology))
return Topology(CxxPointer(ptr, is_const=false))
end
# Indexing support
"""
Get the `Atom` at the given `index` of the `topology`. By default this creates
a copy so as to be safe. To not create a copy, use `@view topology[index]`.
See also [`Base.view(topology::Topology, index::Integer)`](@ref)
"""
Base.getindex(topology::Topology, index::Integer) = Atom(topology, index)
"""
Get the `Atom` at the given `index` of the `topology` without creating a copy.
!!! warning
This function can lead to unefined behavior when keeping the returned `Atom`
around. See [`Base.view(frame::Frame, index::Integer)`](@ref) for more
information on this issue.
"""
function Base.view(topology::Topology, index::Integer)
ptr = @__check_ptr(lib.chfl_atom_from_topology(__ptr(topology), UInt64(index)))
atom = Atom(CxxPointer(ptr, is_const=false))
return atom
end