示例#1
0
def Optimizemodel(pdb_file):
    """
	This functions returns a file with the optimized model from the input pdb. 
	It also returns the energies. 
	"""

    env = environ()
    env.io.atom_files_directory = ['../atom_files']
    env.edat.dynamic_sphere = True

    env.libs.topology.read(file='$(LIB)/top_heav.lib')
    env.libs.parameters.read(file='$(LIB)/par.lib')

    code, ext = pdb_file.split('.')
    mdl = complete_pdb(env, pdb_file)
    mdl.write(file=code + '.ini')

    # Select all atoms:
    atmsel = selection(mdl)
    mpdf2 = atmsel.energy()
    # Generate the restraints:
    #mdl.restraints.make(atmsel, restraint_type='improper', spline_on_site=False)
    #mdl.restraints.make(atmsel, restraint_type='bond', spline_on_site=False)
    #mdl.restraints.make(atmsel, restraint_type='sphere', spline_on_site=False)
    mdl.restraints.make(atmsel, restraint_type='stereo', spline_on_site=False)
    mdl.restraints.write(file=code + '.rsr')

    mpdf1 = atmsel.energy()

    # Create optimizer objects and set defaults for all further optimizations
    cg = conjugate_gradients(output='REPORT')
    md = molecular_dynamics(output='REPORT')

    # Open a file to get basic stats on each optimization
    trcfil = open(code + '.D00000001', 'w')

    # Run CG on the all-atom selection; write stats every 5 steps
    cg.optimize(atmsel, max_iterations=20, actions=actions.trace(5, trcfil))
    # Run MD; write out a PDB structure (called '1fas.D9999xxxx.pdb') every
    # 10 steps during the run, and write stats every 10 steps
    md.optimize(atmsel,
                temperature=300,
                max_iterations=50,
                actions=[
                    actions.write_structure(10, code + '.D9999%04d.pdb'),
                    actions.trace(10, trcfil)
                ])
    #refine(atmsel, code, trcfil)
    # Finish off with some more CG, and write stats every 5 steps
    cg.optimize(atmsel, max_iterations=20, actions=[actions.trace(5, trcfil)])

    mpdf = atmsel.energy()

    print("The initial energy of " + code + " is " + str(mpdf1[0]))
    print("The final energy of " + code + " is " + str(mpdf[0]))
    print("The final energy of " + code + " is " + str(mpdf2[0]))

    mdl.write(file=code + '_optimized.pdb')
示例#2
0
 def cluster(self, cluster_cut=1.5):
     """Cluster all output models, and output an optimized cluster average"""
     self.read_initial_model()
     aln = alignment(self.env)
     self.align_models(aln)
     if len(aln) == 0:
         log.error('cluster', 'No generated models - nothing to cluster!')
     aln.malign3d(gap_penalties_3d=(0, 3), fit=False)
     aln.append_model(mdl=self,
                      align_codes='cluster',
                      atom_files='cluster.opt')
     self.transfer_xyz(aln, cluster_cut=cluster_cut)
     self.write(file='cluster.ini')
     self.read_top_par()
     self.rd_restraints()
     self.create_topology(aln, sequence='cluster')
     atmsel = self._check_select_atoms()
     self.restraints.unpick_all()
     self.restraints.pick(atmsel)
     self.restraints.condense()
     edat = energy_data(copy=self.env.edat)
     edat.nonbonded_sel_atoms = 1
     atmsel.energy(output='LONG', edat=edat)
     cg = conjugate_gradients()
     cg.optimize(atmsel,
                 actions=actions.trace(5, 'cluster.deb'),
                 max_iterations=self.max_var_iterations)
     atmsel.energy()
     self.write(file='cluster.opt')
     aln.compare_structures(fit=True)
示例#3
0
 def get_optimize_actions(self):
     """Get actions to carry out during the initial optimization.
        The default is to write to a trace file every trace_output steps."""
     act = []
     if self.trace_output > 0:
         act.append(actions.trace(self.trace_output, self.tracefile))
     return act
示例#4
0
def optimize(directory, number):
    log.none()
    env = environ()
    env.io.atom_files_directory = ['../atom_files']
    env.edat.dynamic_sphere = True

    env.libs.topology.read(file='$(LIB)/top_heav.lib')
    env.libs.parameters.read(file='$(LIB)/par.lib')

    filename = directory + "/model_" + str(number) + ".pdb"
    #read model files
    code = directory + "/model_" + str(number)
    mdl = complete_pdb(env, filename)

    # Select all atoms:
    atmsel = selection(mdl)

    # Generate the restraints:
    mdl.restraints.make(atmsel, restraint_type='stereo', spline_on_site=False)

    # Create optimizer objects and set defaults for all further optimizations
    cg = conjugate_gradients(output='NO_REPORT')
    md = molecular_dynamics(output='NO_REPORT')

    if not os.path.exists(directory + '/optimization_stats'):
        os.makedirs(directory + '/optimization_stats')

    # Open a file to get basic stats on each optimization
    trcfil = open(
        directory + '/optimization_stats/model_' + str(number) + '.D00000001',
        'w')

    # Run CG on the all-atom selection; write stats every 5 steps
    cg.optimize(atmsel, max_iterations=20, actions=actions.trace(5, trcfil))
    # Run MD; write out a PDB structure (called '1fas.D9999xxxx.pdb') every
    # 10 steps during the run, and write stats every 10 steps
    md.optimize(atmsel,
                temperature=300,
                max_iterations=50,
                actions=[actions.trace(10, trcfil)])
    # Finish off with some more CG, and write stats every 5 steps
    cg.optimize(atmsel, max_iterations=20, actions=[actions.trace(5, trcfil)])

    #mpdf = atmsel.energy()

    mdl.write(file=code + '.pdb')
示例#5
0
def optimize(pdb, pdb_path):
    print(1, pdb_path)
    # Environ data
    env = environ(0)
    env.io.atom_files_directory = ['../atom_files']
    env.edat.dynamic_sphere = True
    
    env.libs.topology.read(file='$(LIB)/top_heav.lib')
    env.libs.parameters.read(file='$(LIB)/par.lib')
    
    code = pdb.split('.')[0] 
    mdl = complete_pdb(env, pdb)
    mdl.write(file=code+'.ini')
    
    # Select all atoms:
    atmsel = selection(mdl)
    
    # Generate the restraints:
    mdl.restraints.make(atmsel, restraint_type='stereo', spline_on_site=False)
    mdl.restraints.write(file=code+'.rsr')
    
    mpdf_prior = atmsel.energy()
    
    # Create optimizer objects and set defaults for all further optimizations
    cg = conjugate_gradients(output='REPORT')
    md = molecular_dynamics(output='REPORT')
    
    # Open a file to get basic stats on each optimization
    trcfil = open(code+'.D00000001', 'w')
    
    # Run CG on the all-atom selection; write stats every 5 steps
    cg.optimize(atmsel, max_iterations=20, actions=actions.trace(5, trcfil))
    # Run MD; write out a PDB structure (called '1fas.D9999xxxx.pdb') every
    # 10 steps during the run, and write stats every 10 steps
    md.optimize(atmsel, temperature=300, max_iterations=50,
                actions=[actions.write_structure(10, code+'.D9999%04d.pdb'),
                         actions.trace(10, trcfil)])
    # Finish off with some more CG, and write stats every 5 steps
    cg.optimize(atmsel, max_iterations=20,
                actions=[actions.trace(5, trcfil)])
    
    mpdf_after = atmsel.energy()
    
    mdl.write(file=os.path.join(pdb_path, 'optimized.pdb'))
    return (mpdf_prior, mpdf_after)
示例#6
0
def refine_model(outputs, log_files, stats, refined_models, dope_profile):
    """Refines a model previously generated with the program according to the optimization parameters defined by
     Modeller."""
    blockPrint()
    import shutil
    env = environ()
    env.io.atom_files_directory = ['../atom_files']
    env.edat.dynamic_sphere = True

    env.libs.topology.read(file='$(LIB)/top_heav.lib')
    env.libs.parameters.read(file='$(LIB)/par.lib')

    dl = os.listdir(outputs)

    files_list = []
    dict_finalen = {}
    list_finalen = []

    for file in dl:
        if file.endswith(".pdb"):
            files_list.append(file)

    enablePrint()
    print("Analyzing ", len(files_list), "model(s)...")
    blockPrint()

    for file in files_list:
        blockPrint(file=log_files + str(file[:-4]) + '.log')
        print("\n", "##########", file, "##########", "\n")
        code = outputs + str(file)
        mdl = complete_pdb(env, code)
        atmsel = selection(mdl)
        cg = conjugate_gradients(output='REPORT')

        trcfil = open(stats + str(file) + '.stats', 'w')

        # Run CG on the all-atom selection; write stats every 5 steps
        cg.optimize(atmsel, max_iterations=20, actions=actions.trace(10, trcfil))
        mpdf2 = atmsel.energy()
        mdl.write(file=refined_models + str(file) + '.B')
        print("Final energy: ", mpdf2[0])
        dict_finalen[file] = mpdf2[0]
        list_finalen.append(mpdf2[0])

    for element in os.listdir(refined_models):
        if element.endswith('.B'):
            shutil.copy2(refined_models+element, outputs)

    enablePrint()

    print("The minimum final energy for all the models is:", min(list_finalen), "and corresponds to:",
          list(dict_finalen.keys())[list(dict_finalen.values()).index(min(list_finalen))] + '.B')

    blockPrint()
    return "Refined"
示例#7
0
def refine(atmsel, code, trcfil):
    # at T=1000, max_atom_shift for 4fs is cca 0.15 A.
    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,
                        actions=[
                            actions.write_structure(10,
                                                    code + '.D9999%04d.pdb'),
                            actions.trace(10, trcfil)
                        ])
            init_vel = False
def main(argv):

    if len(argv) < 2:
        Usage()
        exit(1)

    inputFile = argv[0]
    distPotentialFile = argv[1]
    minSeqSep = 6
    propertyFile = None
    w4Dist = 1.
    w4PhiPsi = 1.
    algorithm = 'CG'
    allAlgorithms = set(['CG', 'QN'])

    if not os.path.isfile(inputFile):
        print 'ERROR: the input file does not exist: ', inputFile
        exit(1)

    if not os.path.isfile(distPotentialFile):
        print 'ERROR: the input distance potential file does not exist: ', distPotentialFile
        exit(1)

    try:
        opts, args = getopt.getopt(
            argv[2:], "p:s:w:a:",
            ["propertyFile=", "minSeqSep=", "weight=", "algorithm="])
    except getopt.GetoptError:
        Usage()
        exit(1)

    for opt, arg in opts:
        if opt in ("-p", "propertyFile"):
            propertyFile = arg
            if not os.path.isfile(propertyFile):
                print 'ERROR: the file does not exist: ', propertyFile
                exit(1)

        elif opt in ("-s", "minSeqSep"):
            minSeqSep = np.int32(arg)
            if minSeqSep < 1:
                print 'ERROR: a positive integer is required for minSeqSep'
                exit(1)

        elif opt in ("-w", "weight"):
            fields = arg.split(':')
            if len(fields) < 1 or len(fields) > 2:
                print 'ERROR: wrong input format for weight factors'
                exit(1)

            w4Dist = np.float32(fields[0])
            if len(fields) == 2:
                w4PhiPsi = np.float32(fields[1])

        elif opt in ("-a", "algorithm"):
            algorithm = arg
            if algorithm not in allAlgorithms:
                print 'ERROR: only the following algorithms are allowed: ', allAlgorithms
                exit(1)
        else:
            Usage()
            exit(1)

    assert w4Dist >= 0
    assert w4PhiPsi >= 0

    inputIsPDB = True

    if inputFile.endswith('.pdb'):
        inputIsPDB = True
        pdbFile = inputFile
    elif inputFile.endswith('.seq') or inputFile.endswith('.fasta'):
        inputIsPDB = False
        seqFile = inputFile
    else:
        print 'ERROR: the input file shall be either a seqeunce file (ends with .seq or .fasta) or a PDB file (ends with .pdb)'
        exit(1)

    distf = open(distPotentialFile, 'r')
    name, seq4potential, distPotMatrices, distCutoffs = cPickle.load(distf)
    distf.close()

    seed = max(2, os.getpid() % 50000)
    env = environ(rand_seed=-seed)
    # Read parameters (needed to build models from internal coordinates)
    env.libs.topology.read('${LIB}/top_heav.lib')
    env.libs.parameters.read('${LIB}/par.lib')

    #env.edat.contact_shell = 20.
    #env.edat.energy_terms.append( MyRGEnergyTerm(atomName='CA', strength=100.0) )
    #env.edat.energy_terms.append( MyRGEnergyTerm(strength=10.0) )

    for response, pot in distPotMatrices.items():
        apt = Response2LabelName(response)
        print 'response: ', response
        if apt == 'CaCa':
            atomNames = ('CA', 'CA')
        elif apt == 'NO':
            atomNames = ('N', 'O')
        elif apt == 'CbCb':
            atomNames = ('CB', 'CB')
        else:
            continue
        """
		elif apt == 'CgCg':
			atomNames = ('CG', 'CG')
		elif apt == 'CaCg':
			atomNames = ('CA', 'CG')
		"""
        env.edat.energy_terms.append(
            MyPairwiseEnergyTerm(potMatrix=pot,
                                 atomNames=atomNames,
                                 distCutoff=distCutoffs[response],
                                 strength=4.0,
                                 minSeqSep=6))

    #print'energy terms:',  env.edat.energy_terms, len(env.edat.energy_terms)

    if not inputIsPDB:
        sequence = LoadFASTAFile(seqFile).upper()
        assert sequence == seq4potential.upper()
        # Build a model from one-letter codes, and write to a PDB file:
        mdl = model(env)
        mdl.build_sequence(sequence)
    else:
        mdl = model(env)
        mdl = complete_pdb(env, pdbFile)

        ## assure that the sequence in the PDB file is same as that in the potential file
        assert len(seq4potential) == len(mdl.residues)
        for res, mres in zip(seq4potential, mdl.residues):
            assert res == mres.code

    ## add stereo restraints and dihedral restraints
    sel = selection(mdl).only_atom_types('CB') | selection(
        mdl).only_mainchain()
    if not inputIsPDB:
        ## generate a random 3D model as initial
        dih = 'phi psi omega chi1 chi2 chi3 chi4 chi5'
        sel.rotate_dihedrals(change='RANDOMIZE', deviation=90.0, dihedrals=dih)
        #sel.randomize_xyz(deviation=(-1.5*math.sqrt(len(sequence)) ) )
        mdl.write(file=name + '-init.pdb')
    else:
        sel.randomize_xyz(deviation=0.5)
        mdl.write(file=name + '-init.pdb')

    mdl.restraints.make(sel, restraint_type='stereo', spline_on_site=False)
    if propertyFile is not None:
        rsrs = CreatePhiPsiConstraints(mdl, propertyFile, strength=2.0)
        for rsr in rsrs:
            mdl.restraints.add(rsr)

    mdl.restraints.write(name + '-rsr.txt')
    #exit(0)

    print "initial energy: ", sel.energy()

    ## minimize energy
    if algorithm == 'CG':
        optimizer = conjugate_gradients()
    else:
        optimizer = quasi_newton()

    optimizer.optimize(sel,
                       max_iterations=800,
                       actions=actions.trace(1, name + '-opt-logfile.txt'))

    print "final energy:", sel.energy()
    mdl.write(file=name + '-final.pdb')
示例#9
0
    def run(self):
        int_seed=randint(0,2**8)
        env = environ(rand_seed=int_seed)
        env.libs.topology.read(file='$(LIB)/top_heav.lib')
        env.libs.parameters.read(file='$(LIB)/par.lib')

        ###################### reading the density ######################
        den = density(env, file=self.path + '/' + self.em_map_file,
                 em_density_format=self.format, em_map_size=self.box_size,
                 density_type='GAUSS', voxel_size=self.apix,
                 resolution=self.res,px=self.x,py=self.y,pz=self.z)

        env.edat.density = den
        env.edat.dynamic_sphere = True

        ###################### read pdb file ######################
        aln = alignment(env)
        mdl2 = model(env, file=self.input_pdb_file)
        aln.append_model(mdl2, align_codes=self.input_pdb_file,
                   atom_files=self.input_pdb_file)
        mdl = model(env, file=self.input_pdb_file)
        aln.append_model(mdl, align_codes=self.code, atom_files=self.code)
        aln.align(gap_penalties_1d=(-600, -400))
        mdl.clear_topology()
        mdl.generate_topology(aln[self.input_pdb_file])
        mdl.transfer_xyz(aln)
        mdl.build(initialize_xyz=False, build_method='INTERNAL_COORDINATES')

        ####################### Remove chain names ######################
        for c in mdl.chains:
            c.name = ' '
        mdl.write(file=self.code+'_ini.pdb')

        ###################### Select the mobile atoms ######################
        sel_all = selection(mdl)
        sel_fixed=[]
        if(self.fixed_filename!=''):
            # read the list of fixed residues
            read_selection_list(self.path,mdl,sel_fixed,self.fixed_filename)
        # select the non fixed, those ones that are going to be optimized
        mobile_atoms = []
        for n in sel_all:
            mobile =True
            for m in sel_fixed:
                if n in m:
                    mobile=False
                    break
            if mobile:
                mobile_atoms.append(n)
#        print "mobile atom ..",n
        print "number of mobile atoms ..",len(mobile_atoms)
        sel_mobile=selection(mobile_atoms) # selection of mobile residues

        ###################### RESTRAINTS FOR MOBILE ATOMS ############
        # STEREOCHEMICAL restraints (based on the model)
        mdl.restraints.make(sel_mobile, restraint_type='STEREO',
                                            spline_on_site=False)
        # MAINCHAIN homology restraints forcing to look in the library
        # (dih_lib_only = True) and ignore the template.
        mdl.restraints.make(sel_mobile, aln=aln,
               restraint_type='PHI-PSI_BINORMAL',spline_on_site=True,
                dih_lib_only=True)
        mdl.restraints.make(sel_mobile, aln=aln,
               restraint_type='OMEGA_DIHEDRAL',spline_on_site=True,
                dih_lib_only=True)
        # SIDECHAIN homology restraints forcing to look in the library
        # (dih_lib_only = True) and ignore the template.
        mdl.restraints.make(sel_mobile, aln=aln,
               restraint_type='CHI1_DIHEDRAL',spline_on_site=True,
                dih_lib_only=True)
        mdl.restraints.make(sel_mobile, aln=aln,
               restraint_type='CHI2_DIHEDRAL',spline_on_site=True,
                dih_lib_only=True)



        ###################### define rigid bodies ######################
        sel_rigid=[]
        rand_rigid=[]
        if(self.rigid_filename != ''):
            load_rigid(self.path,mdl,sel_rigid,rand_rigid,self.rigid_filename)
        for n in sel_rigid:
            include_it=True
            # Only add not fixed rigid bodies. Adding fixed ones is superfluous
            for m in sel_fixed:
                if(n in m):
                    include_it=False
                    break
            if(include_it):
                print "Considering rigid body ...",n
#        for at in n:
#          print "atom ...",at
                mdl.restraints.rigid_bodies.append(rigid_body(n))
        # Remove duplicated restraints
        mdl.restraints.unpick_redundant()
        # Write all the restraints to a file
        mdl.restraints.write(file=self.code+'.rsr')

        ###################### MOLECULAR DYNAMICS ANNEALING ################
        print "MD annealing"
        scal_for_annealing = physical.values(default=1.0, em_density=10000)
        cap=0.39
        timestep=5.0
        icount=0
        trc_file = open('MD'+self.run_num+'.trc', "a")
        ###################### loop for number of cycles
        a,b = 1,self.cycles
        while  a <= b:
            equil_its=self.equil_its_for_heating
            equil_equil=20
            equil_temps= self.equil_temps_for_heating
            trc_step=5
            init_vel = True
            # during heating simulations, FINAL structure is always returned.
            # If the MINIMAL would be returned, no heating would be achieved.
            MD =  molecular_dynamics(cap_atom_shift=cap, md_time_step=timestep,
                               md_return='FINAL', output='REPORT',
                               schedule_scale=scal_for_annealing)

            ###################### heating the system
            for (its, equil, temps) in [(equil_its, equil_equil, equil_temps)]:
                for temp in temps:
                    # optimize the mobile atoms
                    MD.optimize(sel_mobile, max_iterations=its,
                      temperature=temp, init_velocities=init_vel,
                      equilibrate=equil,
                      actions=[actions.trace(trc_step,trc_file)])
                    # print progress
                    scal = physical.values(default=0.0, em_density=1.0)
                    (molpdf, terms) = sel_all.energy(schedule_scale=scal)
                    print "HEATING: iteration number= %s  step= %d %d  "\
                    "temp= %d  EM score= %.3f" %(a,icount,its,int(temp),-molpdf)

                    icount=icount+equil_its
                init_vel=False

            ###################### cooling the system
            equil_its=self.equil_its_for_cooling
            equil_temps= self.equil_temps_for_cooling
            # during the cooling MD simulations the structure returned can be
            # FINAL or MINIMAL acording to the option selected
            MD =  molecular_dynamics(cap_atom_shift=cap, md_time_step=timestep,
                             md_return=self.md_return, output='REPORT',
                             schedule_scale=scal_for_annealing)

            for (its, equil, temps) in [(equil_its, equil_equil, equil_temps)]:
                for temp in temps:

                    MD.optimize(sel_mobile, max_iterations=its,
                      temperature=temp, init_velocities=init_vel,
                      equilibrate=equil,
                      actions=[actions.trace(trc_step,trc_file)])

                    scal = physical.values(default=0.0, em_density=1.0)
                    (molpdf, terms) = sel_all.energy(schedule_scale=scal)
                    print "COOLING: iteration number= %s  step= %d %d " \
                    "temp= %d  EM score= %.3f" %(a,icount,its,int(temp),-molpdf)

                    icount=icount+equil_its

            filename='md'+self.run_num+'_'+str(a)+'.pdb'
            sel_all.write(file=filename)
            a+=1

        trc_file.close()

        print "MD FINAL: step= %d: energy all (with scaling 1:10000)" % icount
        eval = sel_all.energy(schedule_scale=scal_for_annealing)

        ################## final minimization with and without CC restraints.
        ################## Optimize all atoms for refinement
        CG = conjugate_gradients()
        print " final conjugate_gradients"

        CG.optimize(sel_all, output='REPORT', max_iterations=200,
                schedule_scale=scal_for_annealing)
        eval = sel_all.energy(schedule_scale=scal_for_annealing)

        scal = physical.values(default=1.0, em_density=0.0)
        CG.optimize(sel_all, output='REPORT', max_iterations=200,
                schedule_scale=scal)
        eval = sel_all.energy(schedule_scale=scal)

        sel_all.write(file='final'+self.run_num+'_mdcg.pdb')

        os.system("rm -f *.MRC")
示例#10
0
    def run(self):
        int_seed=randint(0,2**8)
        env = environ(rand_seed=int_seed)
        env.libs.topology.read(file='$(LIB)/top_heav.lib')
        env.libs.parameters.read(file='$(LIB)/par.lib')
        log.verbose()

        ###################### reading the density ######################
        den = density(env, file=self.path + '/' + self.em_map_file,
                 em_density_format=self.format, em_map_size=self.box_size,
                 density_type='GAUSS', voxel_size=self.apix,
                 resolution=self.res,px=self.x,py=self.y,pz=self.z)
        env.edat.density = den
        env.edat.dynamic_sphere = True

        ###################### read pdb file ######################
        aln = alignment(env)
        mdl2 = model(env, file=self.input_pdb_file)

        aln.append_model(mdl2, align_codes=self.input_pdb_file,
                   atom_files=self.input_pdb_file)
        mdl = model(env, file=self.input_pdb_file)
        aln.append_model(mdl, align_codes=self.code, atom_files=self.code)
        aln.align(gap_penalties_1d=(-600, -400))
        mdl.clear_topology()
        mdl.generate_topology(aln[self.input_pdb_file])
        mdl.transfer_xyz(aln)
        mdl.build(initialize_xyz=False, build_method='INTERNAL_COORDINATES')

        ####################### Remove chain names ######################
        for c in mdl.chains:
            c.name = ' '
        mdl.write(file=self.code+'_ini.pdb')

        ###################### Select the mobile atoms ######################
        sel_all = selection(mdl)
        sel_fixed=[]
        if(self.fixed_filename!=''):
            # read the list of fixed residues
            read_selection_list(self.path,mdl,sel_fixed,self.fixed_filename)
        # select the non fixed, those ones that are going to be optimized
        mobile_atoms = []
        for n in sel_all:
            mobile =True
            for m in sel_fixed:
                if n in m:
                    mobile=False
                    break
            if mobile:
                mobile_atoms.append(n)
#        print "mobile atom ..",n
        print("number of mobile atoms ..",len(mobile_atoms))
        sel_mobile=selection(mobile_atoms) # selection of mobile residues

        ###################### RESTRAINTS FOR MOBILE ATOMS ###############
        # STEREOCHEMICAL restraints (based on the model).
        mdl.restraints.make(sel_mobile,
                      restraint_type='STEREO', spline_on_site=False)

        # MAINCHAIN homology restraints forcing to look in the library
        # (dih_lib_only = True) and ignore the template.
        mdl.restraints.make(sel_mobile, aln=aln,
               restraint_type='PHI-PSI_BINORMAL',spline_on_site=True,
                dih_lib_only=True)
        mdl.restraints.make(sel_mobile, aln=aln,
               restraint_type='OMEGA_DIHEDRAL',spline_on_site=True,
                dih_lib_only=True)
        # SIDECHAIN homology restraints forcing to look in the library
        # (dih_lib_only = True) and ignore the template.
        mdl.restraints.make(sel_mobile, aln=aln,
               restraint_type='CHI1_DIHEDRAL',spline_on_site=True,
                dih_lib_only=True)
        mdl.restraints.make(sel_mobile, aln=aln,
               restraint_type='CHI2_DIHEDRAL',spline_on_site=True,
                dih_lib_only=True)


        sel_rigid=[]
        rand_rigid=[]
        if(self.rigid_filename != ''):
            load_rigid(self.path,mdl,sel_rigid,rand_rigid,self.rigid_filename)
        for n in sel_rigid:
            include_it=True
            # Only add not fixed rigid bodies. Adding fixed ones is superfluous
            for m in sel_fixed:
                if(n in m):
                    include_it=False
                    break
            if(include_it):
                print("Considering rigid body ...",n)
#        for at in n:
#          print "atom ...",at
                mdl.restraints.rigid_bodies.append(rigid_body(n))

        # Remove duplicated restraints
        mdl.restraints.unpick_redundant()
        # Write all the restraints to a file
        mdl.restraints.write(file=self.code+'.rsr')

        ###################### MODEL RANDOMIZATION ######################
        # Select the flexible atoms
        # flexible: "those that are not fixed neither belong to to a rigid body"
        # Only the flexible atoms are randomized
        flexible_atoms = []
        for n in sel_mobile:
            flexible = True
            for m in sel_rigid:
                if n in m:
                    flexible = False
                    break
            if(flexible):
#        print "flexible atom ...",n
                flexible_atoms.append(n)
        print("number of flexible atoms ...",len(flexible_atoms))
        sel_flex=selection(flexible_atoms)
        sel_flex.randomize_xyz(deviation=1.0)
        # randomize the rigid bodies if requested
        seed()
        max_trans = 10
        max_ang = 30
        for num in sel_rigid:
            if rand_rigid[sel_rigid.index(num)]==1:
                rand_rot_x = uniform(-1,1)
                rand_rot_y = uniform(-1,1)
                rand_rot_z = uniform(-1,1)
                rand_ang = uniform(0,max_ang)
                rand_trans_x = uniform(-max_trans,max_trans)
                rand_trans_y = uniform(-max_trans,max_trans)
                rand_trans_z = uniform(-max_trans,max_trans)
                num.rotate_mass_center([rand_rot_x,rand_rot_y,rand_rot_z],
                                                                  rand_ang)
                num.translate([rand_trans_x,rand_trans_y,rand_trans_z])
        # Write final randomized model
        mdl.write(file=self.code+'_rand.pdb')

        ###################### CG MINIMIZATION ######################
        print("Conjugate_gradients")
        print("directory num %s" % self.run_num)
        trc_step=5
        trc_file = open('CG'+self.run_num+'.trc', "a")
        i=0
        # trim the schedule
        self.sched.make_for_model(mdl)
        for step in self.sched:
            i = i+1
            step.optimize(sel_all, output='REPORT', max_iterations=200,
                      actions=actions.trace(trc_step,trc_file))
            # Write PDB file each optimization step
            pdb_file='cg'+self.run_num+'_'+str(i)+'.pdb'
            sel_all.write(file=pdb_file)
        trc_file.close()

        ###################### PRINT FINAL ENERGY AND CCC ######################
        print("final energy all ")
        scal = physical.values(default=1.0, em_density=10000)
        eval = sel_all.energy(schedule_scale=scal)
        print("final cc ")
        scal = physical.values(default=0.0, em_density=1.0)
        eval = sel_all.energy(schedule_scale=scal)
        # Write final model
        sel_all.write(file='final'+self.run_num+'_cg.pdb')
        os.system("rm -f *.MRC")
示例#11
0
def modeller_funcs(pdb_file, options):
    """
    This functions optimizes a protein structure saved in a pdb file.
    It optimizes the stereochemistry of the given model including non-bonded contacts.
    :param pdb_file: pdb file with the structure we want to optimize.
    :return: returns a pdb file with optimized structure.
    """

    env = environ()
    env.io.atom_files_directory = ['../atom_files']
    env.edat.dynamic_sphere = True

    env.libs.topology.read(file='$(LIB)/top_heav.lib')
    env.libs.parameters.read(file='$(LIB)/par.lib')

    path, ext = pdb_file.split('.')
    dir, code = path.split('/')

    mdl = complete_pdb(env, pdb_file)
    # mdl.write(file=code + '.ini')

    # Select all atoms:
    atmsel = selection(mdl)

    # Calculate initial energy and energy profile for the built structure
    mpdf_ini = atmsel.energy()
    z_score_ini = mdl.assess_normalized_dope()
    mdl_ep_ini = atmsel.get_dope_profile()
    mdl_ep_ini_smoothed = mdl_ep_ini.get_smoothed(window=50)
    energy_profile_txt_path = dir + '/' + code + '_DOPE_EnergyProfile.txt'
    mdl_ep_ini_smoothed.write_to_file(energy_profile_txt_path)
    print("\nModel energy")
    print("The unoptimized model energy of " + code + " is: " + str(mpdf_ini[0]))
    print("\nZ-score")
    print("The unoptimized Z-score of " + code + " is: " + str(z_score_ini))

    if options.optimize is None:
        energy_profile_plot(options, path, energy_profile_txt_path)

    else:
        # Create optimizer objects and set defaults for all further optimizations
        cg = conjugate_gradients(output='NO_REPORT')

        # Open a file to get basic stats on each optimization
        trcfil = open(dir + '/optimization_stats_' + code + '.txt', 'w')

        # Run CG on the all-atom selection; write stats every 5 steps
        cg.optimize(atmsel, max_iterations=20, actions=actions.trace(5, trcfil))

        # Finish off with some more CG, and write stats every 5 steps
        cg.optimize(atmsel, max_iterations=20,
                    actions=[actions.trace(5, trcfil)])

        mpdf = atmsel.energy()
        # Calculate the normalized Z-score for the model after optimization
        z_score = mdl.assess_normalized_dope()
        print("\nModel energy")
        print("The final energy of " + code + " is: " + str(mpdf[0]))
        print("\nZ-score")
        print("The final z-score of " + code + " is: " + str(z_score))

        # Getting the energy profile of our optimized model
        mdl_ep_fin = atmsel.get_dope_profile()

        # Smooth the energy profile by applying a smoothing window of 50
        mdl_ep_fin_smoothed = mdl_ep_fin.get_smoothed(window=50)
        energy_profile_txt_path_opt = path + '_optimized_DOPE_EnergyProfile.txt'
        mdl_ep_fin_smoothed.write_to_file(energy_profile_txt_path_opt)
        mdl.write(file=path + '_optimized.pdb')
        energy_profile_plot(options, path, energy_profile_txt_path, energy_profile_txt_path_opt)
示例#12
0
def Optimizemodel(pdb_file):
    """
	It creates a PDB file with the optimized model from the input pdb, 
	with its energies and restraint contributions. Also it will create pdbs on 
	every step of the Molecular Dynamics optimization. The energy is returned as
	the total value	of Modeller's objective function, molpdf. 
	It also shows the topt 10 contributors to the molpdf before and after optimization
	"""
    # Setting up
    env = environ()
    env.io.atom_files_directory = ['../atom_files']
    env.edat.dynamic_sphere = True

    env.libs.topology.read(file='$(LIB)/top_heav.lib')
    env.libs.parameters.read(file='$(LIB)/par.lib')

    code, ext = pdb_file.split('.')

    # Complete the pdb and make a model
    mdl = complete_pdb(env, pdb_file)
    mdl.write(file=code + '.ini')

    # Select all atoms from the model, make restraints and save them in a file
    atmsel = selection(mdl)
    mdl.restraints.make(atmsel, restraint_type='stereo', spline_on_site=False)
    mdl.restraints.write(file=code + '.rsr')

    # Check the energy before optimization and save it in a var
    initial_mpdf = atmsel.energy()

    # Create optimizer objects
    cg = conjugate_gradients(output='REPORT')
    md = molecular_dynamics(output='REPORT')

    # Open a file to get basic stats on each optimization
    stats_file = open(code + '_opt.stats', 'w')

    # Run MD. Write out a PDB structure every 10 steps during the run.
    # Write stats every 10 steps
    md.optimize(atmsel,
                temperature=300,
                max_iterations=50,
                actions=[
                    actions.write_structure(10, code + '.MD%04d.pdb'),
                    actions.trace(10, stats_file)
                ])

    # Run CG, and write stats every 5 steps
    cg.optimize(atmsel,
                max_iterations=50,
                actions=[actions.trace(5, stats_file)])

    # Final energy
    final_mpdf = atmsel.energy()

    # Assess DOPE
    atmsel.assess_dope(output='ENERGY_PROFILE NO_REPORT',
                       file='TvLDH.profile',
                       normalize_profile=True,
                       smoothing_window=15)

    # Print the energies and the contributions
    initial_cont_all = dict(initial_mpdf[1])
    top_init_conts = dict(
        sorted(initial_cont_all.items(), key=itemgetter(1), reverse=True)[:5])

    l.info("\n\nThe initial energy of " + code + " is " + str(initial_mpdf[0]))
    print("\n\nThe initial energy of " + code + " is " + str(initial_mpdf[0]))
    print("The top 10 initial contributions the restraints are:\n")
    for keys, values in top_init_conts.items():
        print(keys, ":", values)

    final_cont_all = dict(final_mpdf[1])
    top_final_conts = dict(
        sorted(final_cont_all.items(), key=itemgetter(1), reverse=True)[:5])

    l.info("\n\nThe final energy of " + code + " is " + str(final_mpdf[0]))
    print("\n\nThe final energy of " + code + " is " + str(final_mpdf[0]))
    print("Final contributions the restraints are:\n")
    for keys, values in top_final_conts.items():
        print(keys, ":", values)

    mdl.write(file=code + '_optimized.pdb')
示例#13
0
        mdl.restraints.add(
            forms.gaussian(group=physical.xy_distance,
                           feature=features.distance(at['CA:' + str(p1_end)],
                                                     at['CA:' + str(p2_end)]),
                           mean=float(args.mean),
                           stdev=float(args.stdev)))

# Create optimizer objects and set defaults for all further optimizations
    cg = conjugate_gradients(output='REPORT')
    md = molecular_dynamics(output='REPORT')

    # Open a file to get basic stats on each optimization
    trcfil = open(out_name + '.ener', 'w')
    cg.optimize(atmsel,
                max_iterations=100,
                actions=[actions.trace(10, trcfil)])
    md.optimize(
        atmsel,
        max_iterations=run_md_steps,
        friction=float(args.friction),
        temperature=float(args.temp),
        init_velocities=True,
        cap_atom_shift=float(args.shift),
        md_time_step=float(args.time_step),
        #guide_time=40, guide_factor=1,
        actions=[
            actions.charmm_trajectory(100, filename=out_name + '.dcd'),
            actions.trace(100, trcfil)
        ])
    mpdf = atmsel.energy()
    mdl.write(file=out_name + '.pdb')
from modeller import *
from modeller.optimizers import actions
from modeller.scripts import complete_pdb

# Load our custom steepest descent optimizer
from cuser_optimizer import SteepestDescent

env = environ()
env.io.atom_files_directory = ['../atom_files']
env.libs.topology.read(file='$(LIB)/top_heav.lib')
env.libs.parameters.read(file='$(LIB)/par.lib')

# Read in the initial structure:
code = '1fdn'
mdl = complete_pdb(env, code)
atmsel = selection(mdl)

# Generate the restraints:
mdl.restraints.make(atmsel, restraint_type='stereo', spline_on_site=False)

# Optimize with our custom optimizer:
opt = SteepestDescent(max_iterations=80)
opt.optimize(atmsel, actions=actions.trace(5))
def calc(version, configfile, atomfile, inputsequencefile, outputfile):
    #a list, where the helical residues should be
    #TODO read out configfile
    try:
        global log
        helixrange = []
        config = open(configfile, 'r')
        for line in config:
            line = line.strip()
            values = line.split(',')
            pdbname = values[0]
            proteinname = values[1]
            residuename = values[2]
            startlinker = values[3]
            endlinker   = values[4]
            if (len(values)-3)%2 == 0:
                for helixbase in range(3, len(values)-1, 2):
                    helixrange.append((int(values[helixbase]), int(values[helixbase+1])))
            else:
                log.error("calc", "wrong number of parameters in configfile")
                config.close()
                return -2
        config.close()
    except:
        log.error("calc", "reading config file")
        return -3

    try:
        modelsegments = ('FIRST:' + residuename,'LAST:' + residuename) #from where to where in the PDB, default is first to last aa
        aligncodespdb = pdbname + residuename
        alnfile = proteinname + "-" + aligncodespdb + ".ali"

        log.minimal()
        env = environ()

        env.libs.topology.read('${MODINSTALL9v14}/'+version+'/modlib/top_heav.lib')
        env.libs.parameters.read('${MODINSTALL9v14}/'+version+'/modlib/par.lib')
    except:
        log.error("calc", "unable to setup environment")
        return -4

    try:
        aln = alignment(env)
        mdl = model(env, file= atomfile, model_segment=modelsegments) #the PDB file

        aln.append_model(mdl, align_codes=aligncodespdb, atom_files=atomfile)
        aln.append(file=inputsequencefile, align_codes=proteinname) #in here the sequence has been.
        aln.align2d()

        aln.write(file=alnfile, alignment_format='PIR')
    except:
        log.error("calc", "unable to write alignment file")
        return -5

    class cycModel(dopehr_loopmodel):
        def special_patches(self, aln):
            # Link between last residue (-1) and first (0) to make chain cyclic:
            self.patch(residue_type="LINK",
                        residues=(self.residues[-1], self.residues[0]))
                        
                        
                        
    class RestraintsModel(cycModel):
        def special_restraints(self, aln):
            for helix in helixrange:
                self.restraints.add(secondary_structure.alpha(self.residue_range(str(helix[0]), str(helix[1]))))

    class MyLoop(RestraintsModel):
        def select_loop_atoms(self):
            return selection(self.residue_range(startlinker, endlinker))


    # Disable default NTER and CTER patching
    env.patch_default = False 

       
    try:
        a = MyLoop(env, alnfile=alnfile,
                      knowns=pdbname + residuename, sequence=proteinname,
                      loop_assess_methods=(assess.DOPE,
                                      assess.normalized_dope,
                                      assess.DOPEHR,
                                      assess.GA341))
        # generate 10 models
        a.starting_model = 1
        a.ending_model = 2


        a.library_schedule    = autosched.slow
        a.max_var_iterations  = 5000
        a.md_level            = refine.slow
        a.repeat_optimization = 5
        a.max_molpdf = 1e6
        a.final_malign3d = True
        
        # generate 10*5 loopmodels
        a.loop.starting_model = 1
        a.loop.ending_model   = 4

        a.loop.library_schedule    = autosched.slow
        a.loop.max_var_iterations  = 5000
        a.loop.md_level       = refine.slow
        a.loop.repeat_optimization = 5
        a.loop.max_molpdf = 1e6
        a.loop.final_malign3d = True
    except:
        log.error("calc", "setting up loopmodal")
        return -6

    try:
        a.make()
    except:
        log.error("calc", "making loopmodal")
        return -7

    try:
        ok_models = [x for x in a.outputs if x['failure'] is None]

        # Rank the models by Energy Score
        ok_models.sort(key=lambda a: a['DOPE-HR score'])

        # Get top model
        bestmodel = ok_models[0]

        print "BEGIN***************************************************************************"
        print str(bestmodel['DOPE-HR score']) + ";" + str(bestmodel['Normalized DOPE score'])
        print "END*****************************************************************************"


        refmodel = complete_pdb(env, bestmodel["name"])

        refmodel_atms = selection(refmodel)
        refmodel.restraints.unpick_all()
        refmodel.restraints.condense()
    except:
        log.error("calc", "setting up refmodal")
        return -8

    try:
        refmodel.restraints.make(refmodel_atms, restraint_type='STEREO', spline_on_site=False)
    except:
        log.error("calc", "making refmodal")
        return -9

    try:
        for helix in helixrange:
            refmodel.restraints.add(secondary_structure.alpha(refmodel.residue_range(str(helix[0]), str(helix[1]))))
        sel = selection(refmodel)
    except:
        log.error("calc", "adding helixranges to restraints")
        return -10

    try:
        thefile = open("cg_mod_out.dat", "w")
    except:
        log.error("calc", "opening file cg_mod_out.dat")
        return -11
    
    try:
        cg_mod = conjugate_gradients()
        cg_mod.optimize(refmodel, min_atom_shift=0.0001, max_iterations=10000, actions=[actions.trace(5,thefile)])
    except:
        log.error("calc", "cg_mod.optimize")
        return -12

    try:
        refmodel.write(file=outputfile)
    except:
        log.error("calc", "writing outputfile %s" % outputfile)
        return -13

    return 0
示例#16
0
# Select all atoms:
atmsel = selection(mdl)

# Generate the restraints:
mdl.restraints.make(atmsel, restraint_type='stereo', spline_on_site=False)
mdl.restraints.write(file=code+'.rsr')

mpdf = atmsel.energy()

# Create optimizer objects and set defaults for all further optimizations
cg = conjugate_gradients(output='REPORT')
md = molecular_dynamics(output='REPORT')

# Open a file to get basic stats on each optimization
trcfil = file(code+'.D00000001', 'w')

# Run CG on the all-atom selection; write stats every 5 steps
cg.optimize(atmsel, max_iterations=20, actions=actions.trace(5, trcfil))
# Run MD; write out a PDB structure (called '1fas.D9999xxxx.pdb') every
# 10 steps during the run, and write stats every 10 steps
md.optimize(atmsel, temperature=300, max_iterations=50,
            actions=[actions.write_structure(10, code+'.D9999%04d.pdb'),
                     actions.trace(10, trcfil)])
# Finish off with some more CG, and write stats every 5 steps
cg.optimize(atmsel, max_iterations=20,
            actions=[actions.trace(5, trcfil)])

mpdf = atmsel.energy()

mdl.write(file=code+'.B')
示例#17
0
def optimization(pdb_file, options):
    """This function needs as an input a PDB file (the model), and gives as an output the optimized model."""
    env = environ()
    env.io.atom_files_directory = ['../atom_files']
    env.edat.dynamic_sphere = True

    env.libs.topology.read(file='$(LIB)/top_heav.lib')
    env.libs.parameters.read(file='$(LIB)/par.lib')

    path, ext = pdb_file.split('.')
    list = path.split('/')
    #if results/4g83/4g83model.pdb list[0]=results list[1]=4g83 list[2]=4g83model
    dir = list[0] + "/" + list[1]
    code = list[2]

    mdl = complete_pdb(env, pdb_file)
    mdl.write(file=path + '.ini')

    atmsel = selection(mdl)

    mpdf_ini = atmsel.energy()
    z_score_ini = mdl.assess_normalized_dope()
    mdl_ep_ini = atmsel.get_dope_profile()
    mdl_ep_ini_smoothed = mdl_ep_ini.get_smoothed()
    energy_profile_txt_path = dir + '/' + code + '_DOPE_EnergyProfile.txt'
    mdl_ep_ini_smoothed.write_to_file(energy_profile_txt_path)
    print("The unoptimized model's energy of " + code + " is: " +
          str(mpdf_ini[0]))
    print("The unoptimized Z-score of " + code + " is: " + str(z_score_ini))

    energy_profile_txt_path_opt = None

    if options.optimize:
        cg = conjugate_gradients(output='REPORT')
        md = molecular_dynamics(output='REPORT')
        trcfil = open(path + '.D00000001', 'w')
        cg.optimize(atmsel,
                    max_iterations=20,
                    actions=actions.trace(5, trcfil))
        md.optimize(atmsel,
                    temperature=300,
                    max_iterations=50,
                    actions=[
                        actions.write_structure(10, path + '.D9999%04d.pdb'),
                        actions.trace(10, trcfil)
                    ])
        cg.optimize(atmsel,
                    max_iterations=20,
                    actions=[actions.trace(5, trcfil)])
        mpdf = atmsel.energy()
        z_score = mdl.assess_normalized_dope()
        print("The final model energy of " + path + " is " + str(mpdf[0]))
        print("The final model energy of " + path + " is " + str(mpdf_ini[0]))
        print("The final z-score of " + code + " is: " + str(z_score))

        mdl.write(file=path + '_optimized.pdb')

        mdl_final = atmsel.get_dope_profile()
        mdl_final_smoothed = mdl_final.get_smoothed(window=50)
        energy_profile_txt_path_opt = dir + '/' + code + '_optimized_DOPE_EnergyProfile.txt'
        mdl_final_smoothed.write_to_file(energy_profile_txt_path_opt)
        mdl.write(file=dir + '/' + code + '_optimized.pdb')

    energy_profile_plot(options, dir, code, energy_profile_txt_path,
                        energy_profile_txt_path_opt)