Exemplo n.º 1
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)
Exemplo n.º 2
0
def optimize(atmsel, sched):
	#conjugate gradient
	for step in sched:
		step.optimize(atmsel, max_iterations=200, min_atom_shift=0.001) #md 
		refine(atmsel)
		cg = conjugate_gradients()
		cg.optimize(atmsel, max_iterations=200, min_atom_shift=0.001)
def make_alpha(pdbname,fasta_seq,start,end):
    # Set up environment
    e = environ()
    e.libs.topology.read('/home/zonganw/modeller/modeller-9.14/modlib/top_heav.lib')
    e.libs.parameters.read('/home/zonganw/modeller/modeller-9.14/modlib/par.lib')

    # Build an extended chain model from primary sequence, and write it out
    m = model(e)
    m.build_sequence(fasta_seq)
    #m.write(file='extended-chain.pdb')

    # Make stereochemical restraints on all atoms
    allatoms = selection(m)
    m.restraints.make(allatoms, restraint_type='STEREO', spline_on_site=False)
    m.restraints.add (secondary_structure.alpha(m.residue_range(start,end))  )

    # Get an optimized structure with CG, and write it out
    cg = conjugate_gradients()
    cg.optimize(allatoms, max_iterations=100000)
    m.orient()

    # add pdb remarks 
    m.remark = """REMARK    built from modeller""" 
    outputname = pdbname[:-4] + '_' + str(start) + '_' + str(end) + '.pdb'
    m.write(file=outputname)
Exemplo n.º 4
0
    def refine(self, atmsel, actions):
        """Refine the optimized model with MD and CG"""
        # Save the current model:
        if self.fit_in_refine != 'NO_FIT':
            self.write(file='TO_BE_REFINED.TMP')

        # Possibly skip selecting hot atoms only and optimize all atoms:
        if self.refine_hot_only:
            self.initial_refine_hot(atmsel)

        # Do simulated annealing MD:
        if self.md_level:
            self.md_level(atmsel, actions)

        # Possibly skip 'HOT CG' after MD:
        if self.refine_hot_only:
            self.final_refine_hot(atmsel)

        # Get a final conjugate gradients refined structure:
        cg = conjugate_gradients()
        cg.optimize(atmsel,
                    max_iterations=200,
                    output=self.optimize_output,
                    actions=actions)

        # Evaluate gross changes between the initial and final refined model:
        if 'NO_FIT' not in self.fit_in_refine:
            aln = alignment(self.env)
            mdl2 = read_model(file='TO_BE_REFINED.TMP')
            casel = selection(self).only_atom_types('CA')
            casel.superpose(mdl2, aln)
            casel = selection(self)
            casel.superpose(mdl2, aln)
            modfile.delete('TO_BE_REFINED.TMP')
Exemplo n.º 5
0
    def initial_refine_hot(self, atmsel):
        """Do some initial refinement of hotspots in the model"""
        viol_rc = physical.values(default=999)
        stereo_typ = (physical.bond, physical.angle, physical.dihedral,
                      physical.improper, physical.disulfide_distance,
                      physical.disulfide_angle, physical.disulfide_dihedral)
        homol_typ = (physical.ca_distance, physical.n_o_distance,
                     physical.omega_dihedral, physical.sd_mn_distance,
                     physical.phi_psi_dihedral, physical.sd_sd_distance)
        if self.rstrs_defined == 0:
            # Refine only hotspots that have badly violated stereochemical
            # restraints:
            for typ in stereo_typ:
                viol_rc[typ] = 4
        elif self.rstrs_defined == 1:
            # Refine hotspots that have badly violated stereochemical
            # restraints and the important homology-derived restraints:
            for typ in stereo_typ + homol_typ:
                viol_rc[typ] = 4
        elif self.rstrs_defined == 2:
            # Refine hotspots that have badly violated any kind of
            # restraints
            viol_rc['default'] = 4

        # Pick hot atoms (must pick whole residues because of sidechains):
        atmsel = atmsel.hot_atoms(pick_hot_cutoff=4.5, viol_report_cut=viol_rc)
        atmsel = atmsel.by_residue()
        # Pick all corresponding (violated and others) restraints:
        self.restraints.unpick_all()
        self.restraints.pick(atmsel)

        # Local optimization to prevent MD explosions:
        cg = conjugate_gradients()
        cg.optimize(atmsel, max_iterations=100, output=self.optimize_output)
Exemplo n.º 6
0
def optimize(atmsel, sched):
    #conjugate gradient
    for step in sched:
        step.optimize(atmsel, max_iterations=200, min_atom_shift=0.001)  #md
        refine(atmsel)
        cg = conjugate_gradients()
        cg.optimize(atmsel, max_iterations=200, min_atom_shift=0.001)
Exemplo n.º 7
0
def optimize(atmsel, sched):

     for step in sched:
         step.optimize(atmsel, max_iterations=200, min_atom_shift=0.001)

     refine(atmsel)  # MD SUMULATION
     cg = conjugate_gradients()
     cg.optimize(atmsel, max_iterations=200, min_atom_shift=0.001) # CG SIMULATION
Exemplo n.º 8
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')
Exemplo n.º 9
0
    def final_refine_hot(self, atmsel):
        """Do some final refinement of hotspots in the model"""
        # Get conjugate gradients refined hot spots:
        cg = conjugate_gradients()
        cg.optimize(atmsel, max_iterations=200, output=self.optimize_output)

        # Get all static restraints again and select all atoms
        self.restraints.unpick_all()
        self.restraints.pick(atmsel)
def optimize(atmsel, sched):
    #conjugate gradient
    for step in sched:  ## optimização VTFM normal (/applic/modeller/modeller-9.19/modlib/modeller/automodel/autosched.py)
        step.optimize(
            atmsel, max_iterations=200, min_atom_shift=0.001
        )  ## átomos seleccionados, máximo de iterações, critério de convergência
    #md
    refine(atmsel)
    cg = conjugate_gradients()  # criar os objectos de optimização
    cg.optimize(atmsel, max_iterations=200, min_atom_shift=0.001)
Exemplo n.º 11
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"
Exemplo n.º 12
0
def Optimizemodel(pdb_file, optimize):
    print("Calculating the energy of the model...")
    output_path_optimized = "/".join(pdb_file.split('/')[:-1])
    sys.stdout = open(os.devnull, 'w')
    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 = pdb_file.split("/")[-1]
    code = filename.split('.')[0]
    mdl = complete_pdb(env, pdb_file)
    mdl.write(file=output_path_optimized + "/" + code + '.ini')
    # Select all atoms:
    atmsel = selection(mdl)
    mpdf2 = atmsel.energy()
    sys.stdout = sys.__stdout__
    if optimize:
        print('Optimizing the model...')
        sys.stdout = open(os.devnull, 'w')
        # Generate the restraints:
        mdl.restraints.make(atmsel,
                            restraint_type='stereo',
                            spline_on_site=False)
        mdl.restraints.write(file=output_path_optimized + "/" + code + '.rsr')

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

        # Run CG on the all-atom selection
        cg.optimize(atmsel, max_iterations=20)
        # Run MD
        md.optimize(atmsel, temperature=300, max_iterations=50)
        # Finish off with some more CG
        cg.optimize(atmsel, max_iterations=20)

        mpdf = atmsel.energy()

        # Create .pdb
        mdl.write(file=output_path_optimized + "/" + code + '_optimized.pdb')

        # Remove interdmediate files
        os.remove(output_path_optimized + "/" + code + ".ini")
        os.remove(output_path_optimized + "/" + code + ".rsr")
        sys.stdout = sys.__stdout__
        return mpdf2[0], mpdf[0]

    else:
        os.remove(output_path_optimized + "/" + code + '.ini')
        return mpdf2[0]
Exemplo n.º 13
0
 def optimize_loop(self, atmsel, sched, actions):
     """Optimize the geometry of a single loop model"""
     for step in sched:
         step.optimize(atmsel,
                       max_iterations=self.loop.max_var_iterations,
                       output=self.optimize_output,
                       min_atom_shift=0.001,
                       actions=actions)
     if self.loop.md_level:
         self.loop.md_level(atmsel, actions)
     cg = conjugate_gradients()
     cg.optimize(atmsel,
                 max_iterations=1000,
                 output=self.optimize_output,
                 min_atom_shift=0.00001,
                 actions=actions)
Exemplo n.º 14
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')
Exemplo n.º 15
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)
Exemplo n.º 16
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')
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')
Exemplo n.º 18
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")
        else:
            sys.exit(
                "ERROR: I do not see a '.pdb' in your file. Make sure there is! Use the [-h] option for help!"
            )
    else:
        sys.exit("ERROR: " + str(sys.argv[1]) + " does not exist!")
else:
    sys.exit(
        "ERROR: You need to provide an input PDB file! Use the [-h] option for help!"
    )

# Running the program

from modeller import *  # Load standard Modeller classes
from modeller.scripts import complete_pdb
from modeller.optimizers import conjugate_gradients

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

mdl = complete_pdb(env, input_PDB, transfer_res_num=True)
rsr = mdl.restraints

allat = selection(mdl)  #Atom selection
cg = conjugate_gradients()
cg.optimize(allat, max_iterations=100, output='REPORT')
mdl.write(file=str(input_PDB)[:-4] + "_full.pdb")
# Example for model.build_sequence(), secondary_structure.alpha()

from modeller import *
from modeller.optimizers import conjugate_gradients

log.verbose()

# Set up environment
e = environ()
# use all-hydrogen topology:
e.libs.topology.read('${LIB}/top_allh.lib')
e.libs.parameters.read('${LIB}/par.lib')
e.io.hydrogen = True

# Build an extended chain model from primary sequence
m = model(e)
m.build_sequence('AAAAAAPAAAAAA')

# Make stereochemical restraints on all atoms
allatoms = selection(m)
m.restraints.make(allatoms, restraint_type='STEREO', spline_on_site=False)

# Constrain all residues to be alpha-helical
# (Could also use m.residue_range() rather than m.residues here.)
m.restraints.add(secondary_structure.alpha(m.residues))

# Get an optimized structure with CG, and write it out
cg = conjugate_gradients()
cg.optimize(allatoms, max_iterations=1000)
m.write(file='A6PA6_alpha.pdb')
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
Exemplo n.º 22
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)
Exemplo n.º 23
0
code = '1fas'
mdl = complete_pdb(env, code)
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 = 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)])
Exemplo n.º 24
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)