SIMPSON coding examples

Instant simulation of protein NMR spectra

by Thomas Vosegaard

This SIMPSON input file allows simulation of 1D, 2D TOCSY, and 2D NOESY spectra of proteins. It uses random-coil chemical shift prediction for H and HA. Relies on the libraries randomcoil-1.0 and protein-1.0 elsewhere on this site, or from downloading the zip below. A simulation is performed by typing

simpson sim.in <sequence>

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
398
lappend ::auto_path randomcoil protein
package require randomcoil
package require protein


proc Fsave {g base sfrq} {
   fsave $g ${base}.spe -binary
   fsave $g /tmp/[pid].ft2 -nmrpipe
   set ref [fget $g -ref]
   catch {
		file copy /tmp/[pid].ft2 /tmp/f[pid].ft2
      exec sethdr /tmp/f[pid].ft2 -xOBS $sfrq -yOBS $sfrq -xCAR [expr $ref/$sfrq] -yCAR [expr $ref/$sfrq]
      exec cat /tmp/f[pid].ft2 | nmrPipe -di > ${base}.ft2
      catch {exec pipe2ucsf ${base}.ft2 ${base}.ucsf}
      exec rm -f /tmp/f[pid].ft2 /tmp/[pid].ft2
   } err
	puts $err
}


proc grand {} {
   # Generate gaussian number
   set s 2
   while {$s >= 1} {
      set v1 [expr 2.0*rand()-1.0]
      set v2 [expr 2.0*rand()-1.0]
      set s [expr $v1*$v1 + $v2*$v2]
   }
   if {$s != 0} {
      set s [expr $v1*sqrt(-2.0*log($s)/$s)]
   }
   return $s
}


proc addcoupling {peaks cpl} {
   set j [lindex $cpl 0]
   set sh [lindex $cpl 1]
   set mult [lindex $cpl 2]

   for {set p 0} {$p < [llength $peaks]} {incr p} {
      set peak [lindex $peaks $p]
      set fr [lindex $peak 1]
      set int [lindex $peak 2]
   
      set d [expr [lindex $peak 3]-$sh]
      set m [expr ([lindex $peak 3]+$sh)/2.0 + ($fr-[lindex $peak 3])]
      set q [expr sqrt($d*$d + $j*$j)]
      if {[lindex $peak 1] > $sh} {
         set ihi [expr ($q-$j)/$q/2.0]
         set ilo [expr ($q+$j)/$q/2.0]
         set m [expr $m+$q/2.0]
      } else {
         set ihi [expr ($q+$j)/$q/2.0]
         set ilo [expr ($q-$j)/$q/2.0]
         set m [expr $m-$q/2.0]
      }
      
      if {$mult == 1} {
         set newpeak [list $peak $peak]
         lset newpeak 0 1 [expr $m-$j/2.0]
         lset newpeak 1 1 [expr $m+$j/2.0]
         lset newpeak 0 2 [expr $int*$ilo]
         lset newpeak 1 2 [expr $int*$ihi]
      } elseif {$mult == 2} {
         set newpeak [list $peak $peak $peak]
         lset newpeak 0 1 [expr $m-$j]
         lset newpeak 1 1 $m
         lset newpeak 2 1 [expr $m+$j]
         lset newpeak 0 2 [expr $int*($ilo*$ilo)]
         lset newpeak 1 2 [expr $int*($ilo*$ihi)*2.0]
         lset newpeak 2 2 [expr $int*($ihi*$ihi)]
      } elseif {$mult == 3} {
         set newpeak [list $peak $peak $peak $peak]
         lset newpeak 0 1 [expr $m-1.5*$j]
         lset newpeak 1 1 [expr $m-0.5*$j]
         lset newpeak 2 1 [expr $m+0.5*$j]
         lset newpeak 3 1 [expr $m+1.5*$j]
         lset newpeak 0 2 [expr $int*($ilo*$ilo*$ilo)]
         lset newpeak 1 2 [expr $int*($ilo*$ilo*$ihi)*3.0]
         lset newpeak 2 2 [expr $int*($ihi*$ihi*$ihi)*3.0]
         lset newpeak 3 2 [expr $int*($ihi*$ihi*$ihi)]
      }
      for {set i 0} {$i < [llength $newpeak]} {incr i} {
         lappend newpeaks [lindex $newpeak $i]
      }
   }
   return $newpeaks
}

proc main {} {
   global res atoms shift coupling lw intensity tocsy noesy
   set seq 0
   
   set sfrq 600
   set lwe 0
   
   set np 65536
   set sw [expr 10*$sfrq]
   set ref [expr 5*$sfrq]
   set f [fcreate -type spe -ref $ref -sw $sw -np $np]
      
   if {[llength $::argv] < 2} {
      puts "Usage: simpson $::argv0 "
      return
   }
   set random 0
   if {[lsearch $::argv "-random"] != -1} {
      set random 1
      puts "Adding random shifts"
   }
   set reload 0
   if {[lsearch $::argv "-reload"] != -1} {
      set reload 1
      puts "Reloading previous shifts"
   }
   set randomcoil 0
   if {[lsearch $::argv "-randomcoil"] != -1} {
      set randomcoil 1
      puts "Using random coil shifts"
   }
   set sequence [lindex $::argv 1]
   
   if {$reload} {
      set fp [open $sequence/log r]
      foreach l [split [read $fp] \n] {
         if {[llength $l] != 4} {continue}
         set nam "[lindex $l 0][lindex $l 1],[lindex $l 2]"
         set sh($nam) [lindex $l 3]
      }
      close $fp
   }
   
   # First loop through sequence, establish chemical shifts
   set i 1
   foreach s [split $sequence ""] {
      if {![info exists res($s)]} {
         puts "Unknown amino acid $s"
         return
      }
      
      lappend seq "$res($s)"
      set at $atoms($res($s))
      foreach a $at {
         set nam "$i$res($s),[lindex $a 0]"
         if {$i == 1 && [lindex $a 0] == "H"} {continue}
         if {$reload && [info exists sh($nam)]} {
         } else {
            set sh($nam) [lindex $shift($res($s),[lindex $a 0]) 0]
            if {$reload} {
               puts "Missing shift for $nam"
            }
         }
         set lw($nam) $lw(default)
         set int($nam) 1
         if {[info exists lw($res($s),[lindex $a 0])]} {
            set lw($nam) $lw($res($s),[lindex $a 0])
         } elseif {[info exists lw([lindex $a 0])]} {
            set lw($nam) $lw([lindex $a 0])
         }
         if {[info exists intensity($res($s),[lindex $a 0])]} {
            set int($nam) $intensity($res($s),[lindex $a 0])
         }
         if {$random} {
            set sh($nam) [expr $sh($nam) + [grand]*[lindex $shift($res($s),[lindex $a 0]) 1]/4.0]
         }
         set mult($nam) [lindex $a 2]
      }
      incr i
   }

   # Load random coil shifts if not reloaded
   if {$randomcoil} {
      foreach s [randomcoil::shift $sequence 25 6.5 1 0] {
         if {[info exists sh([lindex $s 0][lindex $seq [lindex $s 0]],H)]} {
            set nam "[lindex $s 0][lindex $seq [lindex $s 0]],H"
            set sh($nam) [lindex $s 5]
            puts $nam
            if {$random} {
               set sh($nam) [expr $sh($nam) + [grand]*[lindex $shift([lindex $seq [lindex $s 0]],H) 1]/4.0]
            }
         }
         if {[info exists sh([lindex $s 0][lindex $seq [lindex $s 0]],HA)]} {
            set nam "[lindex $s 0][lindex $seq [lindex $s 0]],HA"
            set sh($nam) [lindex $s 6]
            if {$random} {
               set sh($nam) [expr $sh($nam) + [grand]*[lindex $shift([lindex $seq [lindex $s 0]],HA) 1]/4.0]
            }
         } elseif {[info exists sh([lindex $s 0][lindex $seq [lindex $s 0]],HA2)]} {
            set nam "[lindex $s 0][lindex $seq [lindex $s 0]],HA2"
            set sh($nam) [expr [lindex $s 6]+0.03]
            if {$random} {
               set sh($nam) [expr [lindex $s 6]+0.03 + [grand]*[lindex $shift(gly,HA2) 1]/4.0]
            }
            set nam "[lindex $s 0][lindex $seq [lindex $s 0]],HA3"
            set sh($nam) [expr [lindex $s 6]-0.03]
            if {$random} {
               set sh($nam) [expr [lindex $s 6]-0.03 + [grand]*[lindex $shift(gly,HA2) 1]/4.0]
            }
         }
      }
   }

   # Establish couplings
   if {$randomcoil} {
      set rcj [randomcoil::jhnha $sequence]
   }

   set i 1
   foreach s [split $sequence ""] {
      set at $atoms($res($s))
      foreach a $at {
         set nam "$i$res($s),[lindex $a 0]"
         if {$i == 1 && [lindex $a 0] == "H"} {continue}
         set c $coupling($res($s))
         for {set j 0} {$j < [llength $c]} {incr j} {
            set other ""
            if {[lindex $c $j 0] == [lindex $a 0]} {
               set other "$i$res($s),[lindex $c $j 1]"
            } elseif {[lindex $c $j 1] == [lindex $a 0]} {
               set other "$i$res($s),[lindex $c $j 0]"
            }
            if {$other != "" && [info exists sh($nam)] && [info exists sh($other)]} {
               set jj [lindex $c $j 2]
               if {$randomcoil} {
                  if {([regexp {,H$} $nam] && [regexp {,HA} $other]) || ([regexp {,HA} $nam] && [regexp {,H$} $other])} {
                     set jj [lindex $rcj [expr $i-1] 1]
                     #puts "Using J $jj for $nam $other"
                  }
               }
               set m $mult($other)
               if {$res($s) == "phe" || $res($s) == "tyr"} {
                  if {[lsearch [lindex $c $j] HD] > -1 && [lsearch [lindex $c $j] HE]} {set m 1}
               }
               lappend coupl($nam) [list $jj [expr $sfrq*$sh($other)] $m]
            }
         }
      }
      incr i
   }


   # Calculate coupling patterns and establish TOCSY and NOESY cross peaks
   for {set i 1} {$i < [llength $seq]} {incr i} {
      set s [lindex $seq $i]
      set base "$i$s"
      for {set k 0} {$k < [llength $atoms($s)]} {incr k} {
         set a [lindex $atoms($s) $k]
         set nam "$base,[lindex $a 0]"
         if {[info exists sh($nam)]} {
            set peak [list [list $nam [expr $sfrq*$sh($nam)] $mult($nam) [expr $sfrq*$sh($nam)]]]
            puts [format "%2d %3s %-4s %6.3f" $i $s [lindex $a 0] $sh($nam)]
            if {[info exists coupl($nam)]} {
               for {set j 0} {$j < [llength $coupl($nam)]} {incr j} {
                  set peak [addcoupling $peak [lindex $coupl($nam) $j]]
               }
            }
            lappend peaks($nam) $peak
         }
         foreach t $tocsy($s) {
            set other ""
            if {[lindex $a 0] == [lindex $t 0]} {
               set other "$base,[lindex $t 1]"
            } elseif {[lindex $a 0] == [lindex $t 1]} {
               set other "$base,[lindex $t 0]"
            }
            if {$other != "" && [info exists sh($other)]} {
               lappend tocsy($nam) [list $other [expr [lindex $t 2]/100.0]]
            }
         }
         set isset 0
         if {[info exists noesy($nam)]} {puts $noesy($nam)}
         lappend noesy($nam) [list $nam 1]
         foreach t $noesy($s) {
            set other ""
            if {[lindex $a 0] == [lindex $t 0]} {
               set other "$base,[lindex $t 1]"
            } elseif {[lindex $a 0] == [lindex $t 1]} {
               set other "$base,[lindex $t 0]"
            }
            if {$other != "" && [info exists sh($other)]} {
               set isset 1
               lappend noesy($nam) [list $other [expr [lindex $t 2]/100.0]]
            }
         }
      }

      # Intra-residue couplings
      set nam "$base,H"
      if {$i > 1 && [info exists sh($nam)]} {
         set prev "[expr $i-1][lindex $seq [expr $i-1]]"
         set nam2 "${prev},H"
         if {[info exists sh($nam2)]} {
            lappend noesy($nam) [list $nam2 [expr $noesy(Hi,Hip)/100.0]]
            lappend noesy($nam2) [list $nam [expr $noesy(Hi,Hip)/100.0]]
         }
         set nam2 "${prev},HA"
         if {[info exists sh($nam2)]} {
            lappend noesy($nam) [list $nam2 [expr $noesy(HAi,Hip)/100.0]]
            lappend noesy($nam2) [list $nam [expr $noesy(HAi,Hip)/100.0]]
         }
         set nam2 "${prev},HA2"
         if {[info exists sh($nam2)]} {
            lappend noesy($nam) [list $nam2 [expr $noesy(HAi,Hip)/100.0]]
            lappend noesy($nam2) [list $nam [expr $noesy(HAi,Hip)/100.0]]
         }
         set nam2 "${prev},HA3"
         if {[info exists sh($nam2)]} {
            lappend noesy($nam) [list $nam2 [expr $noesy(HAi,Hip)/100.0]]
            lappend noesy($nam2) [list $nam [expr $noesy(HAi,Hip)/100.0]]
         }
         set nam2 "${prev},HB"
         if {[info exists sh($nam2)]} {
            lappend noesy($nam) [list $nam2 [expr $noesy(HBi,Hip)/100.0]]
            lappend noesy($nam2) [list $nam [expr $noesy(HBi,Hip)/100.0]]
         }
         set nam2 "${prev},HB2"
         if {[info exists sh($nam2)]} {
            lappend noesy($nam) [list $nam2 [expr $noesy(HBi,Hip)/100.0]]
            lappend noesy($nam2) [list $nam [expr $noesy(HBi,Hip)/100.0]]
         }
         set nam2 "${prev},HB3"
         if {[info exists sh($nam2)]} {
            lappend noesy($nam) [list $nam2 [expr $noesy(HBi,Hip)/100.0]]
            lappend noesy($nam2) [list $nam [expr $noesy(HBi,Hip)/100.0]]
         }
      }
   }
   
   # Establish 1D spectrum
   foreach i [lsort [array names peaks]] {
      foreach p [lindex $peaks($i) 0] {
         set fr [expr [lindex $p 1]]
         faddpeaks $f 0.0001 [list [list [lindex $p 1] [expr $int($i)*[lindex $p 2]/double($lw($i))] $lw($i) 0.8]]
      }
   }
   fset $f -sw [expr $sw/double($sfrq)]
   fset $f -ref [expr $ref/double($sfrq)]
   
   # Create output directory
   if {![file isdirectory $sequence]} {
      file mkdir $sequence
      puts "Directory does not exist. Creating directory $sequence"
   }

   set fp [open $sequence/log w]
   for {set i 1} {$i < [llength $seq]} {incr i} {
      set s [lindex $seq $i]
      foreach a $atoms($s) {
         set an [lindex $a 0]
         if {[info exists sh($i$s,$an)]} {
            puts $fp [format "%2d %3s %-4s %6.3f" $i $s $an $sh($i$s,$an)]
         }
      }
   }
   close $fp
   fset $f -sw [expr $sw/double($sfrq)] -ref [expr $ref/double($sfrq)]
   fsave $f $sequence/1d.spe -binary

   # 2D spectra
   set ft [fcreate -type spe -np 2048 -sw [expr 10*$sfrq] -ref [expr 5*$sfrq] -sw1 [expr 10*$sfrq] -ref1 [expr 5*$sfrq] -ni 2048]
   set fn [fdup $ft]
   set pl ""
   set maxpeak -1
   set minpeak 1
   foreach i [array names peaks] {
      foreach c $noesy($i) {
         set j [lindex $c 0]
         set w [lindex $c 1]
         unset pl
         foreach pi [lindex $peaks($i) 0] {
            foreach pj [lindex $peaks($j) 0] {
               set ww [expr $w*[lindex $pi 2]*[lindex $pj 2]/double($lwe+$lw($i))/double($lwe+$lw($j))]
               lappend pl [list [lindex $pi 1] [lindex $pj 1] $ww [expr $lwe+$lw($i)] [expr $lwe+$lw($j)] 1]
            }
         }
         faddpeaks $fn 0.001 $pl
      }
      foreach c $tocsy($i) {
         set j [lindex $c 0]
         set w [lindex $c 1]
         unset pl
         foreach pi [lindex $peaks($i) 0] {
            foreach pj [lindex $peaks($j) 0] {
               set ww [expr $w*[lindex $pi 2]*[lindex $pj 2]/double($lwe+$lw($i))/double($lwe+$lw($j))]
               if {$ww < $minpeak} {set minpeak $ww}
               if {$ww > $maxpeak} {set maxpeak $ww}
               lappend pl [list [lindex $pi 1] [lindex $pj 1] $ww [expr $lwe+$lw($i)] [expr $lwe+$lw($j)] 1]
            }
         }
         faddpeaks $ft 0.001 $pl
      }
   }

   Fsave $fn $sequence/noesy $sfrq
   Fsave $ft $sequence/tocsy $sfrq
}
File:protein.zip
Add comment

Snippets version 1.0.1