ValueError: Invalid placeholder in string
ValueError: Invalid placeholder in string
我尝试通过使用 python 字符串模板为 mutate_model.py 脚本 (http://salilab.org/modeller/wiki/Mutate%20model) 创建自己的模板,我在其中替换了这五个变量的值 Model、resType、resPos、pdb ,chain to replce 并用值写入一个新文件,但我收到如下错误:
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
提前致谢
这是错误的,因为您的字符串中有一个无效的模板标识符
env.libs.parameters.read(file='$(LIB)/par.lib') # Notice the $(LIB)
来自文档
$$ is an escape; it is replaced with a single $.
Any other appearance of $ in the string will result in a ValueError being raised.
您需要使用
$$(LIB)
此外,您的变量大小写不匹配
mdl1.write(file=$modelname+$restyp1+$respos1+$chain+'.tmp')#change
但是您传递的是 resType1
和 resPos1
。您需要传入 restype1
和 respos1
我尝试通过使用 python 字符串模板为 mutate_model.py 脚本 (http://salilab.org/modeller/wiki/Mutate%20model) 创建自己的模板,我在其中替换了这五个变量的值 Model、resType、resPos、pdb ,chain to replce 并用值写入一个新文件,但我收到如下错误:
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
提前致谢
这是错误的,因为您的字符串中有一个无效的模板标识符
env.libs.parameters.read(file='$(LIB)/par.lib') # Notice the $(LIB)
来自文档
$$ is an escape; it is replaced with a single $.
Any other appearance of $ in the string will result in a ValueError being raised.
您需要使用
$$(LIB)
此外,您的变量大小写不匹配
mdl1.write(file=$modelname+$restyp1+$respos1+$chain+'.tmp')#change
但是您传递的是 resType1
和 resPos1
。您需要传入 restype1
和 respos1