Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
surface_docking.py
Go to the documentation of this file.
1 #!/usr/bin/python2.6
2 # Main protocol for PyRosettaSurface
3 # Emily Koo
4 
5 """
6 Usage: surface_docking.py @flags
7 flags is a text file containing the arguments (one on each line. see file in demo for example)
8 To see arguments, see surface_docking.py --help
9 """
10 # Import python modules
11 import os
12 from sys import argv
13 import argparse
14 import random
15 import datetime
16 
17 # Import Rosetta modules
18 from rosetta import *
19 from rosetta.protocols.rigid import *
20 import rosetta.core.scoring.solid_surface
21 import rosetta.core.scoring.constraints
22 import rosetta.core.io.raw_data
23 
24 # Import custom modules
25 from surf_param import *
26 from append_data import *
27 from constraints import *
28 from movers import *
29 
30 # Script options
31 parser = argparse.ArgumentParser(description='Surface Docking Script',fromfile_prefix_chars='@')
32 
33 parser.add_argument('-s', '--start', action="store", dest="s", metavar='INPUT_FILE', required=True, help='PDB file INPUT_FILE containing protein and surface')
34 parser.add_argument('--database', action="store", dest="database", metavar='DATABASE_FILE', required=True, help='Rosetta database path in PyRosetta')
35 parser.add_argument('--f3', '--frag3', action="store", dest="f3", metavar='FRAG3_FILE', required=False, default='', help='Use 3-mer fragment file FRAG3_FILE')
36 parser.add_argument('--f9', '--frag9', action="store", dest="f9", metavar='FRAG9_FILE', required=False, default='', help='Use 9-mer fragment file FRAG9_FILE')
37 parser.add_argument('-c', '--constraints', action='store_true', required=False, default=False, help='Use ssNMR constraints')
38 parser.add_argument('-d', '--disulf', action="store", dest="d", metavar='DISULF', required=False, default='', help='Use disulfide constraints file DISULF')
39 parser.add_argument('--nosmallshear', action='store_true', required=False, default=False, help='Disable small/shear refinements completely (e.g. for rigid-body docking ')
40 parser.add_argument('-n', '--nstruct', action="store", dest="n", metavar='N', required=True, type=int, help='Generate N decoys')
41 
42 args = parser.parse_args(['@flags'])
43 print args
44 input = args.s
45 path = args.database
46 constraints = args.constraints
47 decoy_num = int(args.n)
48 frag3 = args.f3
49 frag9 = args.f9
50 disulf_file = args.d
51 nosmallshear_ref = args.nosmallshear
52 
53 if path.startswith("$"):
54  newpath = os.path.expandvars(path)
55 elif path.startswith("~"):
56  newpath = os.path.expanduser(path)
57 else:
58  newpath = path
59 
60 # Rosetta options
61 opts = ['app','-database',newpath,'-ex1','-ex2aro','-multiple_processes_writing_to_one_directory',\
62  '-mute','core','-mute','protocols','-mute','basic']
63 
64 print opts
65 Rargs = rosetta.utility.vector1_string()
66 Rargs.extend(opts)
67 rosetta.core.init(Rargs)
68 
69 #========================== FUNCTIONS ===================================
70 def input_checker(input, constraints, frag3, frag9, disulf_file):
71  # Input Error Checking
72  if not exists(input):
73  print "Input file does not exist."
74  sys.exit()
75 
76  if constraints:
77  cst = 'ls *.cst > csts'
78  os.system(cst)
79 
80  if os.stat('csts').st_size == 0:
81  print "Constraint files do not exist."
82  rm = 'rm csts'
83  os.system(rm)
84  sys.exit()
85 
86  if frag3 == "" and frag9 != "" or frag3 != "" and frag9 == "":
87  print "You must have both fragment files or no fragment files."
88  sys.exit()
89 
90  elif frag3 != "" and frag9 != "":
91  if not exists(frag3) or not exists(frag9):
92  print "One or more fragment files do not exist."
93  sys.exit()
94 
95  if disulf_file != "":
96  if not exists(disulf_file):
97  print "Disulfide file does not exist."
98  sys.exit()
99 
100 def run(state):
101 # Repeat full-atom relax outer-cycle times while
102 # ramping fa_rep up and ramping max_angle down
103 
104  fa_relax.state = state
105 
106  if fa_relax.outer_cycles > 1:
107  rep = 0.02
108  rep_inc = (0.44-rep)/(fa_relax.outer_cycles-1)
109  else:
110  rep = 0.44
111  rep_inc = 0
112 
113  for outer_cycle in range(1, fa_relax.outer_cycles + 1):
114 
115  fa_relax.outer_cycle = outer_cycle
116  fa_relax.set_max_angle(30/(outer_cycle))
117  fa_relax.score_high.set_weight(fa_rep, rep)
118  rep += rep_inc
119 
120  fa_relax.apply(pose)
121 
122 def append_data(name, pose, scorefxn, PDB, state):
123 # Append additional data to the end of each PDB file
124 
125  append_scores(name, pose, scorefxn)
126  append_hbonds(name, pose, scorefxn)
127 
128  apply_constraints(pose, load_constraints(PDB)[state])
129  append_constraints(name, pose, load_constraints(PDB)[state], scorefxn)
130 
131 #======================= USER SETTINGS =======================================
132 
133 sol_cycles = randint(1,5)
134 sol_outer_cycles = 5
135 sol_inner_cycles = 5
136 ads_cycles = 5 - sol_cycles
137 ads_outer_cycles = 5
138 ads_inner_cycles = 5
139 
140 #========================= Pose and Variables Setup ==========================
141 
142 input_checker(input, constraints, frag3, frag9, disulf_file)
143 
144 print "Initializing poses..."
145 start_pose = Pose()
146 PDB = input[:-4]
147 pose_from_pdb(start_pose, input)
148 if disulf_file != '':
149  make_ads_disulf(start_pose, disulf_file)
150 
151 # Default score functions
152 elec = 1.0
153 score_high = create_score_function("score12")
154 score_high.set_weight(fa_elec, elec)
155 score_high.set_weight(fa_pair, 0.0) # fa_pair duplicates what fa_elec scores
156 score_high.set_weight(atom_pair_constraint, 1.0)
157 score_high.set_weight(dihedral_constraint, 1.0)
158 
159 score_pack = create_score_function("score12")
160 score_pack.set_weight(fa_elec, elec)
161 score_pack.set_weight(fa_rep, 0.44)
162 score_pack.set_weight(fa_pair, 0.0) # fa_pair duplicates what fa_elec scores
163 score_pack.set_weight(atom_pair_constraint, 1.0)
164 score_pack.set_weight(dihedral_constraint, 1.0)
165 
166 std_scorefxn = create_score_function("score12")
167 std_scorefxn.set_weight(fa_elec, elec)
168 std_scorefxn.set_weight(fa_rep, 0.44)
169 std_scorefxn.set_weight(fa_pair, 0.0) # fa_pair duplicates what fa_elec scores
170 std_scorefxn.set_weight(atom_pair_constraint, 1.0)
171 std_scorefxn.set_weight(dihedral_constraint, 1.0)
172 
173 switch_low = SwitchResidueTypeSetMover('centroid')
174 switch_high = SwitchResidueTypeSetMover('fa_standard')
175 
176 #=========================Job Distributor===============================
177 # Default procedure with fragment files:
178 # 1) Ab initio (Solution state)
179 # 2) Centroid relation (Solution state)
180 # 3) Full atom relaxation (Solution state)
181 # 4) Full atom relaxation (Adsorbed state)
182 
183 # Add folder name to output files
184 cwd1 = os.getcwd()
185 cwd2 = cwd1.split('/')
186 cwdn = len(cwd2)
187 dir = cwd2[cwdn - 1]
188 print 'Current folder: ', dir
189 
190 jd = PyJobDistributor("AdsState_" + PDB + "_" + dir, decoy_num, score_high)
191 jd1 = PyJobDistributor("SolState_" + PDB + "_" + dir, decoy_num, score_high)
192 
193 while (jd.job_complete == False):
194  #print "#######################################################"
195  #print "################# GENERATING DECOY", jd1.current_num,"##################"
196  #print "#######################################################"
197 
198  x = jd1.current_name
199  y = jd.current_name
200 
201  time_start = datetime.datetime.now()
202 
203  pose = Pose()
204 
205  if frag3 != '' and frag9 != '':
206  combined_pose = Pose()
207  combined_pose.assign(start_pose)
208  pose.assign(start_pose.split_by_chain()[2])
209 
210  switch_low.apply(pose)
211  # Disulfide file with just protein atoms
212  apply_disulf(pose, load_disulf(disulf_file))
213 
214  # 1) Abinitio
215  print "-------------Starting ab initio protocol-----------"
216  abinitio = Abinitio(PDB, frag3, frag9)
217  abinitio.apply(pose)
218  print "-------------- End ab initio protocol--------------"
219 
220  # 2) Centroid relaxation
221  print "--------Starting centroid relax protocol-----------"
222  cen_relax = CentroidRelax()
223  cen_relax.apply(pose)
224  print "---------End centroid relax protocol--------------"
225 
226  switch_high.apply(pose)
227 
228  # Combine protein pose with surface
229  combined_pose.copy_segment(pose.total_residue(), pose, combined_pose.num_jump() + 1, 1)
230  pose.assign(combined_pose)
231 
232  else:
233  pose.assign(start_pose)
234  switch_high.apply(pose)
235 
236  # Disulfide file including surface atoms
237  apply_disulf(pose, load_disulf(disulf_file+"_ads"))
238 
239  print ">> Starting full atom relaxation..."
240  # Initial settings
241  fa_relax = FullAtomRelax(score_high, score_pack, std_scorefxn, nosmallshear_ref)
242  fa_relax.set_params(pose)
243  fa_relax.loadSurf(input)
244  fa_relax.constraints = constraints
245 
246  # Full repack before starting
247  fa_relax.fullRepack(pose)
248 
249  print "-------------- Starting solution state refinement --------------"
250  # Solution state settings
251  fa_relax.name = x
252 
253  if constraints:
254  fa_relax.loadConstraints(PDB)
255  fa_relax.applyConstraints(pose, "sol")
256 
257  fa_relax.ref_cycles = sol_cycles
258  fa_relax.outer_cycles = sol_outer_cycles
259  fa_relax.inner_cycles = sol_inner_cycles
260 
261  # 3) Solution state refinement
262  for cycle in range(1, sol_cycles + 1):
263  fa_relax.curr_cycle = cycle
264  print "Solution state cycle ", cycle
265  run("sol")
266 
267  # Full repack at the end
268  fa_relax.fullRepack(pose)
269 
270  # Get solution state pose from FullAtomRelax protocol
271  sol_pose = Pose()
272  sol_pose.assign(pose)
273  print "-------------- End of solution state refinement --------------"
274 
275  # Slide protein into contact with surface
276  fa_relax._slideProt(pose)
277 
278  print "-------------- Starting adsorbed state refinement --------------"
279  # Adsorbed state settings
280  fa_relax.name = y
281 
282  if constraints:
283  fa_relax.applyConstraints(pose, "ads")
284 
285  fa_relax.ref_cycles = ads_cycles
286  fa_relax.outer_cycles = ads_outer_cycles
287  fa_relax.inner_cycles = ads_inner_cycles
288 
289  # 4) Adsorbed state refinement
290  for cycle in range(1, ads_cycles + 1):
291  fa_relax.curr_cycle = cycle
292  print "Adsorbed cycle ", cycle
293  run("ads")
294 
295  # Full repack at the end
296  fa_relax.fullRepack(pose)
297 
298  # 2 extra refinement cycles at rep = 0.44 for unbiased prediction
299  if fa_relax.constraints == 'False':
300  fa_relax.score_high.set_weight(fa_rep, 0.44)
301  fa_relax.set_max_angle(6)
302 
303  # Starting at 1 would lead to random orientation
304  for outer_cycle in range(2, 4):
305  fa_relax.outer_cycle = outer_cycle
306  fa_relax.apply(pose)
307 
308  fa_relax.fullRepack(pose)
309 
310  print "-------------- End of adsorbed state refinement --------------"
311 
312  # Re-save it so it's not overwritten by next job
313  curr_sol = str(x)
314  curr_ads = str(y)
315 
316  # Output PDBs
317  jd1.output_decoy(sol_pose)
318  jd.output_decoy(pose)
319 
320  append_surf_sol = "grep SURFA " + input + " >> " + curr_sol
321  append_surf_ads = "grep SURFA " + input + " >> " + curr_ads
322 
323  os.system(append_surf_sol)
324  os.system(append_surf_ads)
325 
326  append_data(curr_sol, sol_pose, score_high, PDB, 0)
327  append_data(curr_ads, pose, score_high, PDB, 1)
328 
329  time_end = datetime.datetime.now()
330  time_diff = time_end - time_start
331  #file = open(curr_ads + ".energies.txt", 'a')
332  #file.write("*\t"+str(time_diff))
333  #file.close()
Fstring::size_type len(Fstring const &s)
Length.
Definition: Fstring.hh:2207