/
find_selection.tcl
222 lines (206 loc) · 7.08 KB
/
find_selection.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
# find a selection residue range for rosetta in given PDB
# Author: Maximilian Scheurer, April 2016
namespace eval ::FindSelection {
namespace export find_selection
namespace export get_anchor_residue
set version 0.1
set description "Convert VMD selection to Rosetta selection"
}
package provide FindSelection $FindSelection::version
proc find_selection {args} \
{
return [eval ::FindSelection::find_selection $args]
}
proc get_anchor_residue {args} \
{
return [eval ::FindSelection::get_anchor_residue $args]
}
proc ::FindSelection::get_anchor_residue {MOL selection} \
{
set searchmol [mol new $MOL.pdb]
set searchsel [atomselect $searchmol "$selection"]
set vmd_resids [lsort -unique -integer [$searchsel get residue]]
set ros_resids []
foreach vmd_resid $vmd_resids {
lappend ros_resids [expr $vmd_resid +1]
}
mol delete $searchmol
if {[llength $ros_resids] > 1} {
puts "WARNING: Please specify only ONE anchor residue! Will use the first residue in the list only!"
}
if {[llength $ros_resids] == 0} {
puts "ERROR: atom selection for anchor residue is empty! Exiting."
exit
}
return [lindex $ros_resids 0]
}
#config should contain bb and chi settings for span for each selection, such as [list {0 1} {1 1}]
# first: chi, second: bb
proc ::FindSelection::find_selection {MOL selections config {offset 4} args} \
{
puts "Trying to find selections."
puts $selections
set seltexts []
foreach seltext $selections {
lappend seltexts $seltext
}
set searchmol [mol new $MOL.pdb]
set final_spans []
set final_configurations []
foreach findseltext $seltexts cf $config {
set searchsel [atomselect $searchmol "$findseltext"]
set vmd_resids [lsort -unique -integer [$searchsel get residue]]
set ros_resids []
foreach vmd_resid $vmd_resids {
lappend ros_resids [expr $vmd_resid +1]
}
set counter -1
set span_closed 1
set current_span []
set all_spans []
for {set i 0} {$i < [llength $ros_resids]} {incr i} {
if {$i == 0 && $counter == -1} {
set counter [lindex $ros_resids $i]
lappend current_span $counter
} elseif {[lindex $ros_resids $i] == [expr $counter + 1]} {
set counter [lindex $ros_resids $i]
} else {
lappend current_span $counter
lappend all_spans $current_span
set current_span []
set counter [lindex $ros_resids $i]
lappend current_span $counter
}
if {$i == [expr [llength $ros_resids] -1]} {
lappend current_span $counter
lappend all_spans $current_span
}
}
set nSpans [llength $all_spans]
for {set i 0} {$i < $nSpans} {incr i} {
lappend final_configurations [list $cf]
}
lappend final_spans $all_spans
}
set out_ros [open "$MOL-spans.txt" w]
set span_config []
set single_spans []
set out_spans ""
foreach span_list $final_spans cf $final_configurations {
foreach single_span $span_list {
lappend single_spans $single_span
puts $out_ros "{\"Span\" [lindex $single_span 0] [lindex $single_span 1] [lindex $cf 0] [lindex $cf 1]} " nonewline
append out_spans "{\"Span\" [lindex $single_span 0] [lindex $single_span 1] [lindex $cf 0] [lindex $cf 1]} "
lappend span_config "\"Span\" [lindex $single_span 0] [lindex $single_span 1] [lindex $cf 0] [lindex $cf 1]"
}
}
#turn on for debugging
puts $span_config
set minimal_length [expr $offset * 2.0]
#sort spans
set sorted_spans [lsort -integer -index 0 $single_spans]
set tot_resnum [llength [lsort -unique -integer [[atomselect $searchmol all] get residue]]]
set tot_chainnum [llength [lsort -unique [[atomselect $searchmol all] get chain]]]
set exclude_string ""
set current_start 1
set before_start 1
set current_stop 1
set before_end 1
set span_open 0
set constraint ""
for {set i 1} {$i <= $tot_resnum} {incr i} {
# puts $i
foreach span $sorted_spans {
if {$i == [lindex $span 0] && $i != 1} {
set beg [lindex $span 0]
set end [lindex $span 1]
set spanlength [expr $end - $beg + 1]
set middle [expr int(floor(double($spanlength)/2.0))]
if {$spanlength <= $minimal_length} {
# puts "span limit length!"
set current_stop [expr $i - 1 + $middle] ;#[expr $i - 1 + $middle]
append exclude_string "$current_start-$current_stop,"
# puts "$current_start-$current_stop"
set before_start $current_start
set current_start [expr $end + 2 - $middle]
set before_end $end
set i $current_start ;# i is increased to $current_start + 1
} else {
set current_stop [expr $i - 1 + $offset]
append exclude_string "$current_start-$current_stop,"
# puts "$current_start-$current_stop"
set before_start $current_start
set current_start [expr $end + 1 - $offset]
set before_end $end
set i $current_start ;# i is increased to $current_start + 1
}
# puts "beginning of span found: $span"
# puts "span length: $spanlength"
} elseif {$i == 1 && $i == [lindex $span 0]} {
set end [lindex $span 1]
if {[expr $end - $offset] >= 1} {
set end_f [expr $end - $offset]
} else {
set end_f $end
}
set current_start [expr $end_f + 1]
}
}
if {$i == $tot_resnum && $i != $before_end} {
if {$current_start > $i} {
append exclude_string "1"
} else {
append exclude_string "$current_start-$i"
}
# puts "$current_start-$i"
}
}
set current_start 1
set before_start 1
set current_stop 1
set before_end 1
set span_open 0
set constraint ""
for {set i 1} {$i <= $tot_resnum} {incr i} {
# puts $i
foreach span $sorted_spans {
if {$i == [lindex $span 0] && $i != 1} {
set beg [lindex $span 0]
set end [lindex $span 1]
set spanlength [expr $end - $beg + 1]
set current_stop [expr $i - 1]
append constraint "{$current_start $current_stop} "
# puts "$current_start-$current_stop"
set before_start $current_start
set current_start [expr $end + 1]
set before_end $end
set i $current_start ;# i is increased to $current_start + 1
# puts "beginning of span found: $span"
# puts "span length: $spanlength"
} elseif {$i == 1 && $i == [lindex $span 0]} {
set end [lindex $span 1]
set current_start [expr $end + 1]
}
}
if {$i == $tot_resnum && $i != $before_end} {
append constraint "{$current_start $i}"
# puts "$current_start-$i"
}
}
# build fix chains!!!
set chain_config ""
for {set i 1} {$i <= $tot_chainnum} {incr i} {
append chain_config "{\"Chain\" $i 0 0} "
}
puts $out_ros $exclude_string
mol delete $searchmol
close $out_ros
puts "finished finding selections."
set lastchar [string range $exclude_string end end]
if {$lastchar == ","} {
return [list $out_spans [string range $exclude_string 0 [expr [string length $exclude_string]-2]] $chain_config $constraint]
}
return [list $out_spans $exclude_string $chain_config $constraint]
#return [list $out_spans [string range $exclude_string 0 [expr [string length $exclude_string]-2]] $chain_config $constraint]
# return [list $out_spans [string range $exclude_string 0 [expr $end-1]] $chain_config $constraint]
}