-
Notifications
You must be signed in to change notification settings - Fork 9
/
PorcupinePlot.tcl
397 lines (346 loc) · 10 KB
/
PorcupinePlot.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#Porcupine plot by Robert Schulz
#Copyright 2009
#Description
# VMD script to calculate the distances between atoms of molecules/frames
# and to draw them as arrows as well as to write the value to the user attribute
#Input data:
# 2 molecules/frames or a trajectory loaded into VMD
# 1: source porcupineplot.tcl
# 2: execute the individual procedures with the input selection(s)
# 2b: OR "ask" PorcupineHelp
package provide porcupineplot 1.0
namespace eval ::porcupineplot:: {
variable vecField
#global variables required for trajectory evaluation
set distMax 0.0
set distMin 100000.0
#list of colors corresponding to the BGR color scale
set minColor 32
set maxColor 1056
set NrColors [expr $maxColor- $minColor]
set colorScheme BGR
namespace ensemble create
namespace export Diff Traj Remove Help
}
#procedure to draw an arrow from $start to $end
proc vmd_draw_arrow {mol start end {scale 1.0} {res 10} {radius 0.2}} {
return [graphics $mol cone $start $end radius [expr $radius * 1.7] resolution $res ]
}
proc ::porcupineplot::SetColorScale {} {
variable minColor
variable maxColor
variable NrColors
variable colorScheme
color scale method $colorScheme
color scale min 0
color scale max 1
color scale midpoint 0.5
set minColor [expr [colorinfo num]]
set maxColor [expr [colorinfo max] - 1]
set NrColors [expr $maxColor- $minColor- 1]
}
#procedure to draw the corresponding ColorScaleBar
proc ::porcupineplot::ColorScaleBar {distRange} {
variable distMin
variable distMax
variable NrColors
if {$distRange > 0.0} {
puts "drawing color scale bar"
ColorScaleBar::color_scale_bar 0.8 0.05 0 1 $distMin $distMax 4
} else {
::ColorScaleBar::delete_color_scale_bar
}
}
#procedure to draw arrows for each residue between two states
proc ::porcupineplot::DrawColoredArrow {mol listVecs distRange arrowRes} {
variable distMin
variable distMax
variable minColor
variable NrColors
variable colorScheme
variable vecField
Remove
puts "drawing arrows for distances between $distMin and $distMax (colors from scheme $colorScheme)"
SetColorScale
set midColor [expr $NrColors* 0.5]
foreach vec $listVecs {
set dist [veclength [vecsub [lindex $vec 0] [lindex $vec 1]]]
if {($dist >= $distMin && $dist <= $distMax)} {
#set arrow colors dependent on length
if {$distRange > 0.0} {
set distcolor [expr int(double($NrColors) * double($dist - $distMin) / double($distRange))]
if {$distcolor < 0} {
set distcolor 0
} elseif {$distcolor >= $NrColors} {
set distcolor [expr $NrColors - 1]
}
draw color [expr $minColor + $distcolor]
} else {
draw color [expr $minColor + $midColor]
}
lappend vecField [vmd_draw_arrow $mol [lindex $vec 0] [lindex $vec 1] 1.0 $arrowRes 0.2]
}
}
}
#procedure to calculate and draw distances between residues in two different conformations
proc ::porcupineplot::Diff {selFirst selSecond args} {
variable distMax
variable distMin
variable NrColors
set argc [llength $args]
set MinSet -1
set MaxSet -1
set arrowRes 20
set trajMode 0
set resOffset 0
set resSel "residue"
set listVecs {}
set distMax 0.0
set distMin 100000.0
#read arguments
if {$argc > 0} {
for {set i 0} {$i < $argc} {incr i} {
set arg [lindex $args $i]
#offset for resid between the selections
if { [string match "-resoffset" $arg] } {
incr i
set resOffset [lindex $args $i]
#selection of a residue
} elseif { [string match "-ressel" $arg] } {
incr i
set resSel [lindex $args $i]
#distance minimum
} elseif { [string match "-min" $arg] } {
incr i
set MinSet [expr double([lindex $args $i])]
#distance maximum
} elseif { [string match "-max" $arg] } {
incr i
set MaxSet [expr double([lindex $args $i])]
#resolution of arrows
} elseif { [string match "-arrowres" $arg] } {
incr i
set arrowRes [expr int([lindex $args $i])]
#switch for trajectory mode
} elseif { [string match "-traj" $arg] } {
set trajMode 1
}
}
}
#break if input arguments are missing
if { $selFirst == "" || $selSecond == ""} {
puts "first 2 are mandatory arguments:"
puts "1st atom selection as starting point in space"
puts "2nd atom selection as final point in space"
puts "Optional arguments:"
puts "-min: Range min"
puts "-max: Range max"
puts "-arrowres: arrow resolution"
puts "-ressel: residue selection (resid/residue)"
return
}
#read selection properties
set molFirst [$selFirst molid]
set molSecond [$selSecond molid]
mol top $molFirst
set frameFirst [$selFirst frame]
set frameSecond [$selSecond frame]
set textFirst [$selFirst text]
set textSecond [$selSecond text]
#read residue list
set listRes [lsort -u -integer [$selFirst get $resSel]]
if {[$selFirst num] == 0} {
puts "Wrong selection 1: no atoms found"
return
}
if {[$selSecond num] == 0} {
puts "Wrong selection 2: no atoms found"
return
}
if {$trajMode == 0} {
puts "running through [llength $listRes] residues"
}
foreach res $listRes {
#selections per residue
set selResFirst [atomselect $molFirst "$resSel $res and $textFirst" frame $frameFirst]
set selResSecond [atomselect $molSecond "$resSel [expr $res+ $resOffset] and $textSecond" frame $frameSecond]
#write current positions to list
if {[$selResFirst num] > 0 && [$selResSecond num] > 0} {
foreach coordFirst [$selResFirst get {x y z}] coordSecond [$selResSecond get {x y z}] {
lappend listVecs [list $coordFirst $coordSecond]
set dist [veclength [vecsub $coordFirst $coordSecond]]
$selResFirst set user $dist
$selResSecond set user $dist
if {$dist > $distMax} {
set distMax $dist
}
if {$dist < $distMin} {
set distMin $dist
}
}
}
$selResFirst delete
$selResSecond delete
}
if {$MinSet != -1} {
set distMin $MinSet
}
if {$MaxSet != -1} {
set distMax $MaxSet
}
if {$trajMode == 1} {
return $listVecs
}
if {[llength $listVecs] > 0} {
set distRange [expr $distMax- $distMin]
DrawColoredArrow $molFirst $listVecs $distRange $arrowRes
ColorScaleBar $distRange
}
puts "done"
}
#procedure to draw the progress of a trajectory using PorcupineDiff
proc ::porcupineplot::Traj {selTraj args} {
variable distMax
variable distMin
variable NrColors
set argc [llength $args]
set minTraj 100000
set maxTraj 0
set resOffset 0
set resSel residue
set MinSet -1
set MaxSet -1
set arrowRes 20
set listVecs {}
set numFrames [molinfo top get numframes]
set frFirst 0
set frLast [expr $numFrames- 1]
#read arguments
if {$argc > 0} {
for {set i 0} {$i < $argc} {incr i} {
set arg [lindex $args $i]
#offset for resid between the selections
if { [string match "-resoffset" $arg] } {
incr i
set resOffset [lindex $args $i]
#selection of a residue
} elseif { [string match "-ressel" $arg] } {
incr i
set resSel [lindex $args $i]
#first frame
} elseif { [string match "-first" $arg] } {
incr i
set frFirst [expr int([lindex $args $i])]
#last frame
} elseif { [string match "-last" $arg] } {
incr i
set frLast [expr int([lindex $args $i])]
#distance minimum
} elseif { [string match "-min" $arg] } {
incr i
set MinSet [expr double([lindex $args $i])]
#distance maximum
} elseif { [string match "-max" $arg] } {
incr i
set MaxSet [expr double([lindex $args $i])]
} elseif { [string match "-arrowres" $arg] } {
incr i
set arrowRes [expr int([lindex $args $i])]
}
}
}
if { $selTraj == "" } {
puts "Mandatory arguments:"
puts "1st argument: atom selection"
puts "Optional arguments:"
puts "-first: first frame"
puts "-last: last frame"
puts "-min: Range min"
puts "-max: Range max"
puts "-arrowres: arrow resolution"
puts "-ressel: residue selection"
return
}
set molTraj [$selTraj molid]
set textTraj [$selTraj text]
puts "looping over [expr $frLast-$frFirst+1] frames"
set sel1 [atomselect $molTraj $textTraj]
set sel2 [atomselect $molTraj $textTraj]
if {[$sel1 num] == 0} {
puts "Wrong selection: no atoms found"
return
}
for {set iFrame $frFirst} { $iFrame < $frLast } {incr iFrame} {
$sel1 frame $iFrame
$sel2 frame [expr $iFrame + 1]
set listVecTemp [Diff $sel1 $sel2 -resoffset $resOffset -min $MinSet -max $MaxSet -arrowres $arrowRes -ressel $resSel -traj]
if {$distMax > $maxTraj} {
set maxTraj $distMax
}
if {$distMin < $minTraj} {
set minTraj $distMin
}
foreach elem $listVecTemp {
lappend listVecs $elem
}
}
$sel1 delete
$sel2 delete
if {[llength $listVecs] > 0} {
if {$MaxSet != -1} {
set distMax $MaxSet
} else {
set distMax $maxTraj
}
if {$MinSet != -1} {
set distMin $MinSet
} else {
set distMin $minTraj
}
set distRange [expr $distMax- $distMin]
puts "drawing arrows"
DrawColoredArrow $molTraj $listVecs $distRange $arrowRes
puts "putting scale bar"
ColorScaleBar $distRange
}
puts "done"
}
#procedure to delete previously drawn arrows which have been stored in OldField
proc ::porcupineplot::Remove {} {
variable vecField
if {[info exists vecField] != 0 && $vecField != ""} {
foreach arrow $vecField {
draw delete [expr $arrow -1]
draw delete $arrow
}
set vecField {}
::ColorScaleBar::delete_color_scale_bar
}
}
proc ::porcupineplot::Help {} {
puts "\tporcupineplot contains 3 executable procedures:"
puts "Diff draws the linear interpolation between two indivual states; also between different VMD molecules"
puts "Traj draws linear interpolations between several states within one VMD molecule"
puts "Remove deletes the previously drawn arrows in the VMD window"
puts ""
puts "\tDiff:"
puts "first 2 are mandatory arguments:"
puts "1st atom selection as starting point in space"
puts "2nd atom selection as final point in space"
puts "Optional arguments:"
puts "-min: Range min"
puts "-max: Range max"
puts "-arrowres: arrow resolution"
puts "-ressel: residue selection (resid/residue)"
puts ""
puts "\tTraj:"
puts "Mandatory arguments:"
puts "1st argument: atom selection"
puts "Optional arguments:"
puts "-first: first frame"
puts "-last: last frame"
puts "-min: Range min"
puts "-max: Range max"
puts "-arrowres: arrow resolution"
puts "-ressel: residue selection"
}