-
Notifications
You must be signed in to change notification settings - Fork 0
/
TruncAndCap.sh
290 lines (222 loc) · 9.32 KB
/
TruncAndCap.sh
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
#!/bin/bash
set -x
debug="True"
### 1) generate crude truncated receptor for residue selection
# MODIFY: change the location of the input files (parm, reference and trajin lines)
LigandRes="1"
complex_original_pdb="3uo4.pdb"
Radius=12
remove_single_residues="False"
regex_numeric='^[0-9]+$'
# clean pdb
grep -E '^ATOM|^HETATM' $complex_original_pdb > complex_orig.pdb
## extract cpptraj residue list
cpptraj -p complex_orig.pdb --resmask \* > cpptraj_reslist.txt
if [[ $LigandRes =~ $regex_numeric ]] ; then
LigandResName=$(awk -v ligandres_awk=$LigandRes '$6==ligandres_awk {print $2}' cpptraj_reslist.txt)
else
LigandResName=$LigandRes
fi
## test if pdb is configured with or without chainid -> we use this when truncating from predefined list
column5=$(head -1 complex_orig.pdb | awk '{print $5}')
if [[ $column5 =~ $regex_numeric ]] ; then
chainID=' '
starting_residue=$(head -1 complex_orig.pdb | awk '{print $5}')
last_residue=$(awk '{print $4 " " $6}' complex_orig.pdb | sort -u -n -k2,2 | grep -E '^GLY|^ALA|^VAL|^LEU|^ILE|^THR|^SER|^MET|^CYS|^PRO|^PHE|^TYR|^TRP|^HIS|^LYS|^ARG|^ASP|^GLU|^ASN|^GLN' | awk 'END {print $2}')
else
chainID=$column5
starting_residue=$(head -1 complex_orig.pdb | awk '{print $6}')
last_residue=$(awk '{print $4 " " $5 " " $6}' complex_orig.pdb | sort -u -n -k3,3 | grep -E '^GLY|^ALA|^VAL|^LEU|^ILE|^THR|^SER|^MET|^CYS|^PRO|^PHE|^TYR|^TRP|^HIS|^LYS|^ARG|^ASP|^GLU|^ASN|^GLN' | awk 'END {print $3}')
fi
# generate truncated protein
# generate truncated protein
cat <<eof> trunc.in
parm $complex_original_pdb
reference $complex_original_pdb
trajin $complex_original_pdb
strip !(:$LigandResName<:$Radius)
strip :$LigandResName
strip :WAT,HOH
strip :Na+
strip :Cl-
trajout trunc.pdb
run
quit
eof
cpptraj -i trunc.in -o trunc.out
### 2) create the list of protein residues present in the truncated version
if [[ $chainID == ' ' ]] ; then
grep 'ATOM' trunc.pdb | awk '{print $5}' | uniq | awk 'NR==1{first=$1;last=$1;next} $1 == last+1 {last=$1;next} {print first,last;first=$1;last=first} END{print first,last}' > resNo.txt
else
grep 'ATOM' trunc.pdb | awk '{print $6}' | uniq | awk 'NR==1{first=$1;last=$1;next} $1 == last+1 {last=$1;next} {print first,last;first=$1;last=first} END{print first,last}' > resNo.txt
fi
# grep : remove CRYST or TER lines
# awk '{print $5}' extracts residue numbers
# uniq : collaps redundant residue numbers (due to one residue number per atom)
# awk :
# NR==1{first=$1;last=$1;next}
# On the first line, initialize the variables first and last and skip to next line.
# $1 == last+1 {last=$1;next}
# If this line continues in the sequence from the last, update last and jump to the next line.
# print first,last;first=$1;last=first
# If we get here, we have a break in the sequence. Print out the range for the last sequence and reinitialize the variables for a new sequence.
# END{print first,last}
# After we get to the end of the file, print the final sequence.
### 3) we need to patch the residue list to add extra residues which will then be mutated into Me Caps
## 3.1) clean initial residue list (concatenate sequences where only 1 residue separates the peptides, eg 183 and 185)
# calculate differences between ending and starting residues
awk 'BEGIN { OFS = FS } NR == 1 { last = $2; print; next }{ $3 = $1 - last; last = $2 }1' resNo.txt > resNo_diff.txt
# get lines where difference between end and start < 2
awk '{if ($3 > 0 && $3 <= 2 && $3 != "") { print 1 } else { print 0 } }' resNo_diff.txt > lines_to_patch.txt
# patch residue list so gaps smaller than 2 residues are bridged by the deleted residue
# this should also work with subsequent gaps, eg 183 185 and 187 in the example
if [ -f resNo_final.txt ] ; then
rm resNo_final.txt
fi
lines=$(cat lines_to_patch.txt | wc -l )
for ((i=2 ; i<=$lines ; i+=1)); do
lineNo=$((i-1))
bool=$(awk -v var=$i 'NR==var { print $1} ' lines_to_patch.txt)
if [ $bool == 0 ] ; then
awk -v var=$((lineNo)) 'NR==var { print $0 }' resNo.txt >> resNo_final.txt
elif [ $bool == 1 ] ; then
startRes_curr=$(awk -v var=$((lineNo)) 'NR==var { print $1 }' resNo.txt)
bool_curr=1
while [ $bool_curr == 1 ] ; do
lineNo=$((lineNo+1))
bool_curr=$(awk -v var=$((lineNo)) 'NR==var { print $1 }' lines_to_patch.txt)
done
endRes_curr=$(awk -v var=$((lineNo-1)) 'NR==var { print $2 }' resNo.txt)
echo "$startRes_curr $endRes_curr" >> resNo_final.txt
i=$lineNo
fi
done
# check if last line has been patched
last_resNo_diff=$(tail -n 1 resNo_diff.txt | awk '{print $3}')
if [[ $last_resNo_diff > 2 ]] ; then
tail -n 1 resNo.txt >> resNo_final.txt
fi
#use final filter to remove single residues
if [ $remove_single_residues == "True" ] ; then
awk '!($1==$2)' resNo_final.txt > resNo_final_tmp.txt && mv resNo_final_tmp.txt resNo_final.txt
fi
## 3.2) create STRIP string for residues within distance threshold
while IFS=" " read -r startRes endRes ; do
if [ $startRes == $endRes ] ; then
startRes=$(awk -v ligandres_awk=$startRes '$6==ligandres_awk {print $1}' cpptraj_reslist.txt)
Res="$startRes,$Res"
else
startRes=$(awk -v ligandres_awk=$startRes '$6==ligandres_awk {print $1}' cpptraj_reslist.txt)
endRes=$(awk -v ligandres_awk=$endRes '$6==ligandres_awk {print $1}' cpptraj_reslist.txt)
Res="$startRes-$endRes,$Res"
fi
done < <(tac resNo_final.txt)
Res=$(echo $Res | sed s/,$// )
## 3.3) create STRIP string for Nterm residues
startRes=$(awk '{print $1}' resNo_final.txt | tac)
for res in $startRes ; do
NtermRes=$((res-1))
NtermRes=$(awk -v ligandres_awk=$NtermRes '$6==ligandres_awk {print $1}' cpptraj_reslist.txt)
# do a check for residue name, if startRes is ACE, there is no additional NtermRes to be included
res_name=$(awk -v res_awk=$res '$6==res_awk {print $2}' cpptraj_reslist.txt)
if [[ $res_name == "ACE" ]] ; then
AceRes="$AceRes"
else
AceRes="$NtermRes,$AceRes"
fi
done
AceRes=$(echo $AceRes | sed s/,$// )
## 3.4) create STRIP string for Cterm residues
endRes=$(awk '{print $2}' resNo_final.txt | tac)
for res in $endRes ; do
CtermRes=$((res+1))
CtermRes=$(awk -v ligandres_awk=$CtermRes '$6==ligandres_awk {print $1}' cpptraj_reslist.txt)
# do a check for residue name, if endRes is NME, there is no additional CtermRes to be included
res_name=$(awk -v res_awk=$res '$6==res_awk {print $2}' cpptraj_reslist.txt)
if [[ $res_name == "NME" ]] ; then
NmeRes="$NmeRes"
else
NmeRes="$CtermRes,$NmeRes"
fi
done
NmeRes=$(echo $NmeRes | sed s/,$// )
## 3.5) combine the necessary STRIP strings for a cpptraj input file and generate extended truncated pdb
# MODIFY: change (paths for) input files
cat <<eof> trunc_cap.in
parm $complex_original_pdb
reference $complex_original_pdb
trajin $complex_original_pdb
strip !(:$AceRes@C,O,CA|:$NmeRes@N,H,CA|:$Res|:$LigandResName)
strip :WAT,HOH
strip :Na+
strip :Cl-
trajout trunc_cap.pdb
run
quit
eof
cpptraj -i trunc_cap.in -o trunc_cap.out
## 3.6) rename Nterm residues to ACE
AceRes_list=$(echo "${AceRes//,/ }")
for NtermRes in $AceRes_list ; do
#NtermRes=$((res-1))
if [[ -z "${chainID// /}" ]] ; then
oldResName=$(awk -v var=$NtermRes '$5 == var {print $4}' trunc_cap.pdb | tail -n 1)
else
oldResName=$(awk -v var=$NtermRes '$6 == var {print $4}' trunc_cap.pdb | tail -n 1)
fi
if [ $NtermRes -lt 10 ] ; then
sed -i "s/$oldResName $chainID $NtermRes/ACE $chainID $NtermRes/g" trunc_cap.pdb
elif [ $NtermRes -lt 100 ] ; then
sed -i "s/$oldResName $chainID $NtermRes/ACE $chainID $NtermRes/g" trunc_cap.pdb
elif [ $NtermRes -lt 1000 ] ; then
sed -i "s/$oldResName $chainID $NtermRes/ACE $chainID $NtermRes/g" trunc_cap.pdb
else
sed -i "s/$oldResName ${chainID}$NtermRes/ACE ${chainID}$NtermRes/g" trunc_cap.pdb
fi
done
## 3.7) rename Cterm residues to NME
NmeRes_list=$(echo "${NmeRes//,/ }")
for CtermRes in $NmeRes_list ; do
#CtermRes=$((res+1))
if [[ -z "${chainID// /}" ]] ; then
oldResName=$(awk -v var=$CtermRes '$5 == var {print $4}' trunc_cap.pdb | tail -n 1)
else
oldResName=$(awk -v var=$CtermRes '$6 == var {print $4}' trunc_cap.pdb | tail -n 1)
fi
if [ $CtermRes -lt 10 ] ; then
sed -i "s/$oldResName $chainID $CtermRes/NME $chainID $CtermRes/g" trunc_cap.pdb
elif [ $CtermRes -lt 100 ] ; then
sed -i "s/$oldResName $chainID $CtermRes/NME $chainID $CtermRes/g" trunc_cap.pdb
elif [ $CtermRes -lt 1000 ] ; then
sed -i "s/$oldResName $chainID $CtermRes/NME $chainID $CtermRes/g" trunc_cap.pdb
else
sed -i "s/$oldResName ${chainID}$CtermRes/NME ${chainID}$CtermRes/g" trunc_cap.pdb
fi
done
### 3.8) rename CA for ACE and NME residues
for res in ${AceRes//,/ } ; do
if [ $res -lt 10 ] ; then
sed -i "s/CA ACE $res/CH3 ACE $res/g" trunc_cap.pdb
elif [ $res -lt 100 ] ; then
sed -i "s/CA ACE $res/CH3 ACE $res/g" trunc_cap.pdb
elif [ $res -lt 1000 ] ; then
sed -i "s/CA ACE $res/CH3 ACE $res/g" trunc_cap.pdb
else
sed -i "s/CA ACE $res/CH3 ACE $res/g" trunc_cap.pdb
fi
done
for res in ${NmeRes//,/ } ; do
if [ $res -lt 10 ] ; then
sed -i "s/CA NME $res/C NME $res/g" trunc_cap.pdb
elif [ $res -lt 100 ] ; then
sed -i "s/CA NME $res/C NME $res/g" trunc_cap.pdb
elif [ $res -lt 1000 ] ; then
sed -i "s/CA NME $res/C NME $res/g" trunc_cap.pdb
else
sed -i "s/CA NME $res/C NME $res/g" trunc_cap.pdb
fi
done
#### 4) remove unnecessary files
if [ $debug == "False" ] ; then
rm complex_orig.pdb cpptraj_reslist.txt trunc.in trunc.out trunc.pdb resNo.txt resNo_diff.txt lines_to_patch.txt trunc_cap.in trunc_cap.out # resNo.txt resNo_diff.txt resNo_final.txt lines_to_patch.txt trunc.in trunc.out trunc.pdb
fi