forked from CG-it/CG-it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgit_map.tcl
358 lines (289 loc) · 10.5 KB
/
cgit_map.tcl
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/tclsh
# +-------+
# | CG-it |
# +-------+
# CG-it, a VMD package to simplify creating coarse grained SDK
# topologies.
# Copyright (c) 2013 by Chris MacDermaid <chris.macdermaid@gmail.com>
# and Giacomo Fiorin <giacomo.fiorin@gmail.com>
#(Shinoda) Shinoda, DeVane, Klein, Mol Sim, 33, 27 (2007).
#(DeVane) Shinoda, DeVane, Klein, Soft Matter, 4, 2453-2462 (2008).
proc CGit::map {args} {
set sel [lindex $args 0]
set molid [$sel molid]
set seltext [$sel text]
set seltext "(same residue as ($seltext))"
set frame [molinfo $molid get frame]
set args [lrange $args 1 end]
set nargs [llength $args]
set newargs ""
## Default Arguments
set first $frame; set last $frame; set stride 1;
## Make a local selection that contains entire residues,
## we can only CG entire residues for now.
set localsel [atomselect $molid $seltext]
## Check for flags and remove them from arguments
for {set i 0} {$i < $nargs} {incr i} {
set arg [lindex $args $i]
if {[string match -?* $arg]} {
set val [lindex $args [expr $i+1]]
switch -- $arg {
-first {set first $val}
-last {set last $val}
-stride {set stride $val}
-all {set first 0; set last\
[molinfo $molid get numframes]}
-now {set first $frame; set last $frame}
default {cgCon -info "Map: Unknown option $arg"; return}
}
} else {
lappend newargs $arg
}
}
## Change to the first frame
molinfo $molid set frame $first
## Change the occupancy to a unique beadID
## This beadID is used when mapping to distinguish
## separate beads.
setBeadID $localsel
## Get the order inwhich we should traverse VMD's
## coordinate array such that atoms belonging to the
## same bead are grouped together
setOrder $localsel
## We coarsegrain one third of the water in the system.
## Identify those molecules using a unique identifier.
## This unfortunately destroys any dynamical information
## in the system w.r.t. water.
$sel set user4 1; # > 0 == include atom in mapping
set sel2 [atomselect $molid "$seltext and water and name \"O.*\""]
set nwater [$sel2 num]
if {$nwater > 0} {
set sel3 [atomselect $molid "water"]; $sel3 set user4 -1; $sel3 delete;
set user4 {}
set residues [$sel2 get residue]
foreach {r1 r2 r3} $residues {
lappend user4 1 -1 -1
}
$sel2 set user4 [lrange $user4 0 $nwater-1]
}
$sel2 delete
set props {}
set keys {residue resid resname chain segname occupancy beta}
## Get unique residues defined in the selection, currently only entire
## residues can be coarse-grained
set sel2 [atomselect $molid "$seltext and user4 > 0"]
set residues [lsort -unique -increasing -integer -index 0\
[$sel2 get $keys]]
## Create the beads for each residue
foreach r $residues {
lassign $r {*}$keys
foreach bead [getMap $resname -keys {type name charge mass} -transpose] {
lappend props [list {*}$bead $chain $resid $resname\
$segname $occupancy $beta]
}
}
set keys {type name charge mass chain\
resid resname segname occupancy beta}
## Make a new "empty" mol
set molname CGmol-$molid
set newmol -1
set ntotal [llength $props]
if {[catch {mol new atoms $ntotal} newmol]} {
cgCon -error "map: could not create new molecule: $molid"
cgCon -error $newmol
return -1
}
mol rename $newmol $molname
## Set the properties
set sel3 [atomselect $newmol "all"]
$sel3 set $keys $props
$sel3 delete
## Update PBC
molinfo $newmol set {a b c} [molinfo $molid get {a b c}]
## Update internal VMD structures
mol reanalyze $newmol
## Map (Will automatically use the c-routine if the library is loaded)
cgmap -sel $sel2 -append $newmol -first $first -last $last -stride $stride
## Cleanup
$sel2 delete
$localsel delete
## Return the created MOLID
return $newmol
}
## Assigns a unique BeadID to atoms
## using the user field.
proc CGit::setBeadID {sel} {
set molid [$sel molid]
set seltext [$sel text]
## Get the residues specified in the selection
set resnames [lsort -unique -ascii [$sel get resname]]
## Clear the user field
$sel set user -1.0
foreach r $resnames {
set sel2 [atomselect $molid "($seltext) and resname $r"]
## Get a list of beads for this residue
set beads [getMap $r -keys {map}]
set nbeads($r) [llength $beads]
## Create lookup hash
set id 0
foreach b $beads {
foreach a $b {
set lookup($r,$a) $id
}
incr id
}
$sel2 delete
}
## Multiply the unique residue id by
## the lookup index
set ids {}
foreach name [$sel get name]\
resid [$sel get residue]\
resn [$sel get resname] {
if {[catch {set idx $lookup($resn,$name)}]} {
cgCon -err "Unknown atom $name in residue type $resn"
return -code error
}
lappend ids [expr {int($resid * $nbeads($resn) + $idx)}]
}
$sel set user $ids
}
proc CGit::setOrder {sel} {
set molid [$sel molid]
set seltext [$sel text]
set seltext "(($seltext) and not water)"
## Get the residues specified in the selection
set resnames [lsort -unique -ascii [$sel get resname]]
## Clear the user fields
$sel set user2 [$sel get index]
$sel set user3 [$sel get mass]
## This is not the most efficient or elegant, but it's
## fast enough and it gets the job done.
## Sort by bead order supplied in the map, then pass that
## to the mapper in the "user" fields.
foreach r $resnames {
set sel2 [atomselect $molid "($seltext) and resname $r"]
set segid [$sel2 get segid]
set beads [getMap $r -keys {map}]
$sel2 set {segid user4 user user2 user3}\
[lsort -command [list aacompare [join $beads]]\
[$sel2 get {name residue user index mass}]]
$sel2 set segid $segid
$sel2 delete
}
}
## Compares residue first, then
## by particular atom order specified
## as a list in "l"
proc CGit::aacompare {l a b} {
lassign $a name_a residue_a
lassign $b name_b residue_b
if {$residue_a < $residue_b} {
return -1
} elseif {$residue_a > $residue_b} {
return 1
} else {
set i [lsearch -ascii -exact $l $name_a]
set j [lsearch -ascii -exact $l $name_b]
return [expr {$i < $j ? -1 : $i > $j ? 1 : 0}]
}
}
## Map the coordinates
## The best spead you can get with this routine
## is about 20 fps.
proc CGit::cgmap {args} {
set nargs [llength $args]
set newargs ""
## Default Arguments
set first 0; set last 0; set stride 1;
set molid [molinfo top]; set appendmol -1; set sel ""
## Check for flags and remove them from arguments
for {set i 0} {$i < $nargs} {incr i} {
set arg [lindex $args $i]
if {[string match -?* $arg]} {
set val [lindex $args [expr $i+1]]
switch -- $arg {
-molid {set molid $val}
-append {set appendmol $val}
-first {set first $val}
-last {set last $val}
-stride {set stride $val}
-sel {set sel $val; \
set molid [$sel molid]}
-all {set first 0; set last\
[molinfo $molid get numframes]}
-now {set first $frame; set last $frame}
default {cgCon -info "Map: Unknown option $arg"; return}
}
} else {
lappend newargs $arg
}
}
if {$molid < 0 || $appendmol < 0} {
cgCon -err "Must specify molids to work with"
return -code error
}
set localsel 0
if {$sel == ""} {
set sel [atomselect $molid "all"]
set localsel 1
}
set nframes [molinfo $molid get numframes]
set natoms [$sel num]
set sel_all [atomselect $molid "all"]
set sel2 [atomselect $appendmol "all"]
## Output to screen
set print [expr {($last - $first) / 10}]
if {$print < 10} {set print 10}
if {$print > 10} {set print 100}
## BeadIDs and array access order
set bid [$sel get user]; set order [$sel get user2]
lappend bid -1
## Mass is invariant over frames
set m [$sel_all get mass];
for {set frame $first}\
{$frame <= $last && $frame < $nframes} {incr frame $stride} {
if {[expr {$frame % $print}] == 0} {
cgCon -info "Mapping frame $frame"
}
## Update the frame/selections
molinfo $molid set frame $frame
$sel_all update; $sel update
set x [$sel_all get x]; set y [$sel_all get y]; set z [$sel_all get z]
set com {};
set w 0; set cx 0; set cy 0; set cz 0;
for {set i 0} {$i < $natoms} {incr i} {
set idx [expr int([lindex $order $i])]
set b1 [lindex $bid $i]
set b2 [lindex $bid $i+1]
set xx [lindex $x $idx]
set yy [lindex $y $idx]
set zz [lindex $z $idx]
set mm [lindex $m $idx]
if {$b1 != $b2} {
set w [expr {$w + $mm}]
set cx [expr {$cx + ($mm * $xx)}]
set cy [expr {$cy + ($mm * $yy)}]
set cz [expr {$cz + ($mm * $zz)}]
lappend com [vecscale\
[list $cx $cy $cz]\
[expr {1.0/$w}]]
set w 0; set cx 0; set cy 0; set cz 0;
} else {
set w [expr {$w + $mm}]
set cx [expr {$cx + ($mm * $xx)}]
set cy [expr {$cy + ($mm * $yy)}]
set cz [expr {$cz + ($mm * $zz)}]
}
}
## Make a new frame and set coordinates
animate dup $appendmol; $sel2 update
$sel2 set {x y z} $com
## Update the PBCs
molinfo $appendmol set {a b c}\
[molinfo $molid get {a b c}]
}
if {$localsel} {$sel delete}
$sel2 delete
$sel_all delete
}