问题
I tried to make my own template for mutate_model.py script (http://salilab.org/modeller/wiki/Mutate%20model) by using python string template where I substituted values for these five variables Model,resType,resPos,pdb,chain to replce and write a new file with values but I am getting a error like below:
MyAttempt:
import os
import re
import sys
import itertools
from modeller import *
from docopt import docopt
from string import Template
from modeller.automodel import *
from os.path import join, getsize
from modeller.scripts import complete_pdb
Model="3o26"
resType="A"
resPos="275"
pdb="3o26.pdb"
chain="A"
dir=os.getcwd()
str = '''import sys
import os
from modeller import *
from modeller.optimizers import molecular_dynamics, conjugate_gradients
from modeller.automodel import autosched
def optimize(atmsel, sched):
for step in sched:
step.optimize(atmsel, max_iterations=200, min_atom_shift=0.001)
refine(atmsel)
cg = conjugate_gradients()
cg.optimize(atmsel, max_iterations=200, min_atom_shift=0.001)
def refine(atmsel):
md = molecular_dynamics(cap_atom_shift=0.39, md_time_step=4.0, md_return='FINAL')
init_vel = True
for (its, equil, temps) in ((200, 20, (150.0, 250.0, 400.0, 700.0, 1000.0)),
(200, 600,
(1000.0, 800.0, 600.0, 500.0, 400.0, 300.0))):
for temp in temps:
md.optimize(atmsel, init_velocities=init_vel, temperature=temp,
max_iterations=its, equilibrate=equil)
init_vel = False
def make_restraints(mdl1, aln):
rsr = mdl1.restraints
rsr.clear()
s = selection(mdl1)
for typ in ('stereo', 'phi-psi_binormal'):
rsr.make(s, restraint_type=typ, aln=aln, spline_on_site=True)
for typ in ('omega', 'chi1', 'chi2', 'chi3', 'chi4'):
rsr.make(s, restraint_type=typ+'_dihedral', spline_range=4.0,
spline_dx=0.3, spline_min_points = 5, aln=aln,
spline_on_site=True)
log.verbose()
env = environ(rand_seed=-49837)
env.io.hetatm = True
env.edat.dynamic_sphere=False
env.edat.dynamic_lennard=True
env.edat.contact_shell = 4.0
env.edat.update_dynamic = 0.39
env.libs.topology.read(file='$(LIB)/top_heav.lib')
env.libs.parameters.read(file='$(LIB)/par.lib')
mdl1 = model(env, file=$pdb_filename)
ali = alignment(env)
ali.append_model(mdl1, atom_files=$pdb_filename, align_codes=$modelname)
s = selection(mdl1.chains[$chain].residues[$respos1]])#change
s.mutate(residue_type=$restyp1)#change
ali.append_model(mdl1, align_codes=$modelname)
mdl1.clear_topology()
mdl1.generate_topology(ali[-1])
mdl1.transfer_xyz(ali)
mdl1.build(initialize_xyz=False, build_method='INTERNAL_COORDINATES')
mdl2 = model(env, file=$pdb_filename)
mdl1.res_num_from(mdl2,ali)
#WriteAndReadMutation
mdl1.write(file=$modelname+$restyp1+$respos1+$chain+'.tmp')#change
mdl1.read(file=$modelname+$restyp1+$respos1+$chain+'.tmp')#change
make_restraints(mdl1, ali)
mdl1.env.edat.nonbonded_sel_atoms=1
sched = autosched.loop.make_for_model(mdl1)
#MutationOptimization
s = selection(mdl1.atoms['CA:'+$respos1+':'+$chain].select_sphere(5))
mdl1.restraints.unpick_all()
mdl1.restraints.pick(s)
s.energy()
s.randomize_xyz(deviation=4.0)
mdl1.env.edat.nonbonded_sel_atoms=2
optimize(s,sched)
mdl1.env.edat.nonbonded_sel_atoms=1
optimize(s,sched)
s.energy()
mdl1.write(file="hi.txt")
os.remove($modelname+$restyp1+$respos1+$chain+'.tmp')'''
str = Template(str)
file = open(dir + '/' + 'mutate_models.py', 'w')
file.write(str.substitute(modelname=Model,resTyp1=resType,resPos1=resPos,pdb_filename=pdb,chain=chain))
file.close()
ERROR:
Traceback (most recent call last):
File "ex.py", line 116, in <module>
file.write(str.substitute(modelname=Model,resTyp1=resType,resPos1=resPos,pdb_filename=pdb,chain=chain))
File "/usr/lib/python2.7/string.py", line 172, in substitute
return self.pattern.sub(convert, self.template)
File "/usr/lib/python2.7/string.py", line 169, in convert
self._invalid(mo)
File "/usr/lib/python2.7/string.py", line 146, in _invalid
(lineno, colno))
ValueError: Invalid placeholder in string: line 44, col 30
Expected output :
Above mentioned five variables should be written by respective value and write a file
Thanks in advance
回答1:
It's erroring because you have an invalid template identifier in your string
env.libs.parameters.read(file='$(LIB)/par.lib') # Notice the $(LIB)
From the docs
$$ is an escape; it is replaced with a single $.
Any other appearance of $ in the string will result in a ValueError being raised.
You would need to use
$$(LIB)
Also, your variable case doesn't match
mdl1.write(file=$modelname+$restyp1+$respos1+$chain+'.tmp')#change
But you're passing in resType1
and resPos1
. You need to pass in restype1
and respos1
来源:https://stackoverflow.com/questions/37752903/valueerror-invalid-placeholder-in-string