def sa(intra=False,rainbow=True): """ Superimpose all open models onto the first one. This may not work well with selections. Option intra can be set to True to enable intra_fit first, for working with multi-state (nmr) pdbs. [Thanks to Kyle Beauchamp for this one] """ AllObj=cmd.get_names("all") for x in AllObj: print(AllObj[0],x) if intra==True: cmd.intra_fit(x) if rainbow==True: cmd.util.chainbow(x) cmd.align(x,AllObj[0]) cmd.zoom()
def sa(intra=False, rainbow=True): """ Superimpose all open models onto the first one. This may not work well with selections. Option intra can be set to True to enable intra_fit first, for working with multi-state (nmr) pdbs. [Thanks to Kyle Beauchamp for this one] """ AllObj = cmd.get_names("all") for x in AllObj: print(AllObj[0], x) if intra == True: cmd.intra_fit(x) if rainbow == True: cmd.util.chainbow(x) cmd.align(x, AllObj[0]) cmd.zoom()
def testIntraFit(self): cmd.fragment("gly", "m1") cmd.create("m1", "m1", 1, 2) rms_list = cmd.intra_fit("m1") self.assertArrayEqual(rms_list, [-1.0, 0.0]) rms_list = cmd.intra_rms("m1") self.assertArrayEqual(rms_list, [-1.0, 0.0]) rms_list = cmd.intra_rms_cur("m1") self.assertArrayEqual(rms_list, [-1.0, 0.0])
def clean(pose): """Deletes everything and load the validated models""" cmd.delete('all') cmd.load('%s_Ca.pdb' % pose) cmd.load('%s_Cb.pdb' % pose) cmd.load('%s_CaCb.pdb' % pose) cmd.intra_fit('%s_Ca' % pose, 0, quiet=1) cmd.intra_fit('%s_Cb' % pose, 0, quiet=1) cmd.intra_fit('%s_CaCb' % pose, 0, quiet=1) cmd.dss('all') cmd.disable('%s_Ca' % pose) cmd.disable('%s_Cb' % pose)
def getRMSD(directory="temp"): # If the directory exists if os.path.exists(directory): files = [ f for f in listdir(directory) if isfile(join(directory, f)) and ( f.split(".")[-1] == "log" or f.split(".")[-1] == "out") and len(f.split(".")) > 1 ] # load all the pdb files for file in files: filesplit = file.split(".") full_file_name = join(directory, "%s_OPT.pdb" % filesplit[0]) cmd.load(full_file_name, "%s_copy" % cmd.get_object_list()[0]) # calculate for the reference object RMSD = cmd.intra_fit("%s_copy" % cmd.get_object_list()[0], 0) #print RMSD energies = [] for i in range(len(files)): out = getoutData(join(directory, files[i])) energies.append(out.ENERGY) # scale and convert hartree to J/mol. Then calculate Boltzman distribution hartree_to_Jmol = 2600 * 1000 # hartree to J/mol Jmol_to_kcalmol = 0.24 / 1000 # J/mol to kcal/mol min_energy = min(energies) for i in range(len(energies)): energies[i] = (energies[i] - min_energy) * hartree_to_Jmol * Jmol_to_kcalmol #write csv output = open("RMSD.csv", 'w') output.write("Structure;Energy;RMSD\n") message = "%s;%.4f;%.4f\n" % ( files[0], energies[0], 0 ) # RMSD = -1 for the same structure by default, but in our case 0 is more meningful output.write(message) for i in range(1, len(files)): message = "%s;%.4f;%.4f\n" % (files[i], energies[i], RMSD[i]) output.write(message) # terminate output.close() else: print("FATAL ERROR: Specified directory doesn't exist [%s]" % directory)
def filter_opt(directory="temp", setup_path="default", verbose="False"): # If the directory exists if os.path.exists(directory): # Log generation log = Log() log.write( "\n\n########################################\n########### F I L T E R - O ############\n########################################\n\n" ) #verbose if verbose.lower() in ['true', '1', 't', 'y', 'yes']: verbose = True else: verbose = False # Retrieve all the files in the directory which are output with .log extension from gaussian or .out extension from mopac files = [ f for f in listdir(directory) if isfile(join(directory, f)) and ( f.split(".")[-1] == "log" or f.split(".")[-1] == "out") and len(f.split(".")) > 1 ] if len(files) > 0: # get setup.ini setup = Setup(log, setup_path) if setup.isLoaded() == True: # if optimisation, remove identical conformers if setup.scf != "1SCF": log.write( "\n\nRemove identical structure with an RMSD cutoff of: %s Angstroms " % setup.rmsd_cutoff) #prepare bins bins = [ ] #first of the bin is the median of the bin, ie the representation of the bin energies = [] # Retrieve energy and create associated pdb for i in range(len(files)): fileData = getoutData(join(directory, files[i])) #there is an energy, there was no error if hasattr(fileData, "ENERGY"): export_pdb(directory, files[i], fileData) energies.append(fileData.ENERGY) # hartree else: #error: the optimisation probably failed for this conformer, we remove it and trigger a warning os.remove(join(directory, files[i])) log.write( "WARNING: No energy was found. Removed from conformers [%s]" % join(directory, files[i])) #reload files = [ f for f in listdir(directory) if isfile(join(directory, f)) and (f.split(".")[-1] == "log" or f.split(".")[-1] == "out" ) and len(f.split(".")) > 1 ] # load all the pdb files for file in files: filesplit = file.split(".") full_file_name = join(directory, "%s_OPT.pdb" % filesplit[0]) cmd.load(full_file_name, "%s_copy" % cmd.get_object_list()[0]) # calculate for each file for i in range(len(files)): log.write( "\n%s_copy__%s RMSD:" % (cmd.get_object_list()[0], i + 1), verbose) RMSD = cmd.intra_fit( "%s_copy" % cmd.get_object_list()[0], i + 1) log.write("%s" % RMSD, verbose) min_id = -1 min_rmsd = setup.rmsd_cutoff for j in range(len(bins)): current_rmsd = RMSD[bins[j][0]] # if under a certain cut-off, considers the same structure if current_rmsd < setup.rmsd_cutoff: # put the structure in the bin with the minimum cutoff if current_rmsd < min_rmsd: min_id = j min_rmsd = current_rmsd # check results, and do accordingly if min_id == -1: bins.append([ i ]) # doesn't belong to a bin, create a new bin log.write( "Create a new bin (#%s) for [%s]" % ((len(bins) - 1), files[i]), verbose) else: bins[min_id].append(i) #belongs to bin[min_id] log.write( "[%s] Added to bin #%s with RMSD of %-6.4f Angstroms" % (files[i], min_id, min_rmsd), verbose) #finally, filter by keeping one structure for each bin for i in range(len(bins)): min_energy = bins[i][0] for j in range(1, len(bins[i])): # keep the smallest energy conformation if energies[bins[i][j]] < energies[min_energy]: try: # remove previous files filename = files[min_energy].split(".")[0] # remove *.pdb, *.out or *.log os.remove( join(directory, "%s_OPT.pdb" % filename)) os.remove( join(directory, files[min_energy])) if setup.software == "MOPAC": # remove *.mop, *.arc os.remove( join(directory, "%s.mop" % filename)) os.remove( join(directory, "%s.arc" % filename)) elif setup.software == "GAUSSIAN": os.remove( join(directory, "%s.com" % filename)) except: log.write( "WARNING: Failed to remove [%s]" % join(directory, files[min_energy])) pass min_energy = bins[i][j] else: try: # remove these files filename = files[bins[i][j]].split(".")[0] # remove *.pdb, *.out or *.log os.remove( join(directory, "%s_OPT.pdb" % filename)) os.remove( join(directory, files[bins[i][j]])) if setup.software == "MOPAC": # remove *.mop, *.arc os.remove( join(directory, "%s.mop" % filename)) os.remove( join(directory, "%s.arc" % filename)) elif setup.software == "GAUSSIAN": os.remove( join(directory, "%s.com" % filename)) except: log.write( "WARNING: Failed to remove [%s]" % join(directory, files[bins[i][j]])) pass #remove loaded structures from Pymol cmd.delete("%s_copy" % cmd.get_object_list()[0]) # create output output = open("energies.txt", 'w') output.write( "*******************************************************************************\n" ) output.write( "** **\n" ) output.write( "** E N E R G I E S **\n" ) output.write( "** **\n" ) output.write( "*******************************************************************************\n" ) output.write( "* CALCULATION WITH %-30s *\n" % setup.software) if setup.software == "MOPAC": output.write("* MOPAC EXECUTIVE PATH: %-53s *\n" % setup.exe) output.write("* using force-field: %-56s *\n" % setup.SE) keywords = "%s charge=%s %s" % (setup.SE, setup.charge, setup.scf) output.write("* %-75s *\n" % keywords) elif setup.software == "GAUSSIAN": output.write( "* %%mem=%2.fGB *\n" % setup.memories) output.write( "* %%nprocshared=%2.f *\n" % setup.procsshared) output.write( "* # opt=(maxcycles=160) freq=noraman %-40s *\n" % setup.leveloftheory) output.write( "* %2.f %2.f *\n" % (setup.charge, setup.spin)) if setup.singlepointcalculation != "": output.write( "* --Link1-- *\n" ) output.write("* # geom=check guess=read %-51s *\n" % setup.singlepointcalculation) output.write( "* %2.f %2.f *\n" % (setup.charge, setup.spin)) output.write( "* Clusterisation cut-off was set at: %4.2f Angstroms *\n" % setup.rmsd_cutoff) output.write( "* Angle count: %-3.f | Atomic model: %-5s | Temperature: %4.f K *\n" % (setup.angle_count, setup.radii, setup.Temperature)) output.write( "*******************************************************************************\n" ) output.write(" File created on %s\n" % str(datetime.date.today())) output.write( "\n Structure Etot (Hartree)") files = [ f for f in listdir(directory) if isfile(join(directory, f)) and ( f.split(".")[-1] == "log" or f.split(".")[-1] == "out") and len(f.split(".")) > 1 ] for filename in files: out = getoutData(join(directory, filename)) message = "\n %-30s" % filename + " %12.6f" % out.ENERGY output.write(message) # terminate output.close() log.write( "----------------------------\n---- Normal Termination ----\n----------------------------\n" ) log.finalize() return True else: log.write( "Error: Failed to load setup.ini in [%s]. Fix it to continue." % setup_path) return False else: log.write("Error: No file to use in the directory [%s]" % directory) return False else: print("FATAL ERROR: Specified directory doesn't exist [%s]" % directory) return False
model = cmd.get_model('system') #for atom in model.atom: # print "%8d %4s %3s %5d %8.3f %8.3f %8.3f" % (atom.index, atom.name, atom.resn, int(atom.resi), atom.coord[0], atom.coord[1], atom.coord[2]) #pymol.finish_launching() cmd.viewport(width,height) #niterations = 10 # DEBUG # Load trajectory cmd.load_traj(trajectory_temporary_filename, object='system') # Align all states if solute == 'dna': cmd.intra_fit('name P') # DNA else: cmd.intra_fit('solute') # protein # Zoom viewport cmd.zoom('solute') cmd.orient('solute') # Create one-to-one mapping between states and frames. cmd.mset("1 -%d" % nframes) # Delete first frame cmd.mdelete("1") # Render movie
def mcm(pose, mc_steps, SASA, randomize): ################################# MCM Parameters ########################## T = 300. # Temperature k = 0.0019872041 # Boltzmann constant kT = k * T # probability to sample phi, psi or chi angles_prob = [1 / 3, 1 / 3, 1 / 3] accepted = 0 ########################################################################## # first, last = pose_from_pdb(pose) if first or last: print('Starting MCM') from energy import minimize, set_sasa, get_sasa sus_updates = cmd.get('suspend_updates') cmd.set('suspend_updates', 'on') # uncomment for debugging cmd.feedback('disable', 'executive', 'everything') pdb_conect = cmd.get('pdb_conect_all') cmd.set('pdb_conect_all', 1) glyco_bonds = get_glyco_bonds(first, last) con_matrix = writer(glyco_bonds) # Remove previous pdb files prev_files = glob.glob('mcm_*.pdb') for prev_file in prev_files: os.remove(prev_file) # set all paramenters for sasa-energy computation if SASA: params, points, const = set_sasa(n=1000) # randomize initial conformation if randomize: for i in range(len(con_matrix) - 1): bond = con_matrix[i] angle_values = np.random.uniform(-180, 180, size=2) set_psi(pose, bond, angle_values[0]) set_phi(pose, bond, angle_values[1]) for i in range(6): set_chi(pose, bond) # minimize energy of starting conformation and save it NRG_old = minimize(pose, nsteps=5000, rigid_geometry=False) NRG_min = NRG_old cmd.save('mcm_%08d.pdb' % accepted) # start MCM routine fd = open("mcm_log.txt", "w") print('# iterations remaining = %s' % (mc_steps)) for i in range(1, mc_steps + 1): if i % (mc_steps // 10) == 0: print('#remaining iterations = %s' % (mc_steps - i)) if True: sample_uniform(pose, con_matrix, angles_prob) NRG_new = minimize('tmp', nsteps=100, rigid_geometry=False) if SASA: solvatation_nrg = get_sasa(params, points, const, selection='all', probe=0)[0] NRG_new = NRG_new + solvatation_nrg if NRG_new < NRG_old: NRG_old = NRG_new fd.write('%8d%10.2f\n' % (accepted, NRG_new)) cmd.copy(pose, 'tmp') cmd.delete('tmp') cmd.save('mcm_%08d.pdb' % accepted) accepted += 1 else: delta = np.exp(-(NRG_new - NRG_old) / (kT)) if delta > np.random.uniform(0, 1): NRG_old = NRG_new fd.write('%8d%10.2f\n' % (accepted, NRG_new)) cmd.copy(pose, 'tmp') cmd.delete('tmp') cmd.save('mcm_%08d.pdb' % accepted) accepted += 1 cmd.delete('tmp') if NRG_new < NRG_min: NRG_min = NRG_new cmd.save('mcm_min.pdb') fd.close() cmd.delete('all') print('Savings all accepted conformations on a single file') de_builds = cmd.get('defer_builds_mode') cmd.set('defer_builds_mode', 5) for i in range(0, accepted): cmd.load('mcm_%08d.pdb' % i, 'mcm_trace') cmd.save('mcm_trace.pdb', 'all', state=0) cmd.delete('all') cmd.load('mcm_trace.pdb') cmd.intra_fit('mcm_trace') print('MCM completed') # restore settings cmd.set('suspend_updates', sus_updates) cmd.set('pdb_conect_all', pdb_conect) cmd.set('defer_builds_mode', de_builds)
# asdf for a in xrange(1, 14): print a, print "%8.3f" % cmd.fit("ref and resi %d" % a, "trg"), print "%8.3f" % cmd.rms("ref", "trg"), print "%8.3f" % cmd.rms_cur("ref", "trg") cmd.frame(10) print "%8.3f" % cmd.fit("ref", "trg") for a in xrange(1, 14): print a, print "%8.3f" % cmd.fit("ref and resi %d" % a, "trg"), print "%8.3f" % cmd.rms("ref", "trg"), print "%8.3f" % cmd.rms_cur("ref", "trg") a = 1 print "%8.3f" % cmd.fit("ref", "trg") for b in xrange(1, 11): cmd._dump_floats(cmd.intra_fit("trg and resi %d" % a, b)) cmd._dump_floats(cmd.intra_rms("trg", b)) cmd._dump_floats(cmd.intra_rms_cur("trg", b)) cmd.do("intra_fit (trg),10") cmd.do("intra_fit (trg and resi 1),10") cmd.do("intra_rms (trg),10") cmd.do("intra_rms_cur (trg),10") print "END-LOG"
for a in xrange(1, 14): print a, print "%8.3f" % cmd.fit("ref and resi %d" % a, "trg"), print "%8.3f" % cmd.rms("ref", "trg"), print "%8.3f" % cmd.rms_cur("ref", "trg") cmd.frame(10) print "%8.3f" % cmd.fit("ref", "trg") for a in xrange(1, 14): print a, print "%8.3f" % cmd.fit("ref and resi %d" % a, "trg"), print "%8.3f" % cmd.rms("ref", "trg"), print "%8.3f" % cmd.rms_cur("ref", "trg") a = 1 print "%8.3f" % cmd.fit("ref", "trg") for b in xrange(1, 11): cmd._dump_floats(cmd.intra_fit("trg and resi %d" % a, b)) cmd._dump_floats(cmd.intra_rms("trg", b)) cmd._dump_floats(cmd.intra_rms_cur("trg", b)) cmd.do("intra_fit (trg),10") cmd.do("intra_fit (trg and resi 1),10") cmd.do("intra_rms (trg),10") cmd.do("intra_rms_cur (trg),10") print "END-LOG"
else: stop = -1 args.frames = range(1, nframes + 1) for chunkid, chunk in enumerate(chunks): start, stop = min(chunk), max(chunk) cmd.reinitialize() cmd.load(args.top, 'inp') cmd.load_traj(args.traj, 'inp', state=1, start=start, stop=stop, selection=selection) if args.align is not None: if len(chunks) == 1: rmsds = cmd.intra_fit(selection, args.align) rmsds[rmsds == -1.] = 0. outrmsdfile = f"{os.path.splitext(args.out)[0]}_rmsd.txt" numpy.savetxt(outrmsdfile, rmsds, fmt="%.4f") states = numpy.where(numpy.isin(chunk, args.frames))[0] + 1 nstates = cmd.count_states('inp') if len(states) == nstates: cmd.create('out', selection=selection, source_state=0, target_state=-1) else: for s in states: sys.stdout.write(f'Getting state {s}/{max(states)}\r') sys.stdout.flush() cmd.create('out', selection=selection, source_state=s, target_state=-1)
def mcm(pose, mc_steps, SASA, randomize): ################################# MCM Parameters ########################## T = 300. # Temperature k = 0.0019872041 # Boltzmann constant angles_prob = [1/3, 1/3, 1/3] # probability to sample phi, psi or chi accepted = 0 ############################################################################ # first, last = pose_from_pdb(pose) if first or last: print('Starting MCM') from energy import minimize, set_sasa, get_sasa cmd.set('suspend_updates', 'on') cmd.feedback('disable', 'executive', 'everything') ##uncomment for debugging cmd.set('pdb_conect_all', 1) glyco_bonds = get_glyco_bonds(first, last) con_matrix = writer(glyco_bonds) # Remove previous pdb files prev_files = glob.glob('mcm_*.pdb') for prev_file in prev_files: os.remove(prev_file) # set all paramenters for sasa-energy computation if SASA: params, points, const = set_sasa(n=1000) ## randomize initial conformation if randomize: for i in range(len(con_matrix)-1): bond = con_matrix[i] angle_values = np.random.uniform(-180, 180, size=2) set_psi(pose, bond, angle_values[0]) set_phi(pose, bond, angle_values[1]) for i in range(6): set_chi(pose, bond) # minimize energy of starting conformation and save it NRG_old = minimize(pose, nsteps=5000, rigid_geometry=False) NRG_min = NRG_old cmd.save('mcm_%08d.pdb' % accepted) ## start MCM routine fd = open("mcm_log.txt", "w") print('# iterations remaining = %s' % (mc_steps)) for i in range(1, mc_steps+1): if i % (mc_steps//10) == 0: print('#remaining iterations = %s' % (mc_steps-i)) if True: sample_uniform(pose, con_matrix, angles_prob) NRG_new = minimize('tmp', nsteps=100, rigid_geometry=False) if SASA: solvatation_nrg = get_sasa(params, points, const, selection='all', probe=0)[0] NRG_new = NRG_new + solvatation_nrg if NRG_new < NRG_old: NRG_old = NRG_new fd.write('%8d%10.2f\n' % (accepted, NRG_new)) cmd.copy(pose, 'tmp') cmd.delete('tmp') cmd.save('mcm_%08d.pdb' % accepted) accepted += 1 else: delta = np.exp(-(NRG_new-NRG_old)/(T*k)) if delta > np.random.uniform(0, 1): NRG_old = NRG_new fd.write('%8d%10.2f\n' % (accepted, NRG_new)) cmd.copy(pose, 'tmp') cmd.delete('tmp') cmd.save('mcm_%08d.pdb' % accepted) accepted += 1 cmd.delete('tmp') if NRG_new < NRG_min: NRG_min = NRG_new cmd.save('mcm_min.pdb') fd.close() cmd.delete('all') print('Savings all accepted conformations on a single file') cmd.set('defer_builds_mode', 5) for i in range(0, accepted): cmd.load('mcm_%08d.pdb' % i, 'mcm_trace') cmd.save('mcm_trace.pdb', 'all', state=0) cmd.delete('all') cmd.load('mcm_trace.pdb') cmd.intra_fit('mcm_trace') print(' MCM completed') cmd.set('suspend_updates', 'off')
def avg_states(object='all', object_sel=None, first=1, last=0, newobj=None, fitverdict='no', verb=0, pairs=1, writefiles=1): """ ARGUMENTS: object (string [defaults to 'all']): Starting PyMOL molecular object consisting of >1 states to be averaged object_sel (string [defaults to "name CA"]): An optional subset of atoms on which to operate (e.g., "name CA and resi 20-50") first (int [defaults to 1]): number of the first state to include in averaging last (int [defaults to last state]): Number of the last state to include in averaging. A value of '0' (zero) means use the last state in the object. newobj (string [defaults to name of object with string "_avg_AtoZ" appended, where A and Z are the numbers of the first and last frames included in the averaging]): Desired name of the new output pymol object (i.e., averaged structure) fitverdict (string [defaults to "no", any value != "no" is taken as a "yes"]): Use the pymol function intra_fit() to fit all the states of "object & object_sel" before calculating the average structure and RMSDs?? 'no' = NO, !'no' = yes verb (int [defaults to 0]): Verbosity level of output. 0 = quiet, !0 = verbose (e.g., write position-specific RMSDs to standard output). pairs (int [default 1]): Also calculate average pairwise RMSD for each residue position?? 0 = no, !0 = yes writefiles (int [default 1]): Write the calculated RMSD data to plaintext files?? 0 = no, !0 = yes NOTES: The state specified as 'first' is taken as the reference state for the (optional) intra_fit() alignment. If no selection is given, the string "and name CA" will be appended to the object and the averaging and rmsd calculations will be restricted to "object and name CA" (i.e., C-alpha atoms). To avoid this default behavior, specify a valid pymol atom selection for the object_sel argument (e.g., "name CA" or "name CA and resi 20-50" or whatever). """ pair_file = None object = str(object) object_sel = str(object_sel) first = int(first) last = int(last) num_states_tot = cmd.count_states(object) if last < 1: last = num_states_tot num_states2avg = last - first + 1 if newobj is None or newobj == '': newobj = "%s_avg_%dto%d" % (object, first, last) cmd.create(newobj, object, first, 1) if writefiles: print('%s' % ('-' * 80)) datfileprefix = '%s_%dto%d' % (replace(object, ' ', '_'), first, last) avg_rmsd_file = datfileprefix + '.avg_rmsd.dat' avg_file = open(avg_rmsd_file, 'w') print('Opened "%s" file for writing residue-specific RMSDs to ' 'average structure...' % avg_rmsd_file) if pairs: pair_rmsd_file = datfileprefix + '.pair_rmsd.dat' pair_file = open(pair_rmsd_file, 'w') print('Opened "%s" file for writing averaged residue-specific ' 'pairwise RMSDs...' % pair_rmsd_file) print('%s' % ('-' * 80)) else: # just do this instead of evaluating conditionals each time avg_file = open('/dev/null', 'w') if pairs: pair_file = open('/dev/null', 'w') # through the atom loops (below)... obj_sel_string = (lambda o, sel: (o and sel and "%s and %s" % (o, sel)) or (o and not sel and "%s and name CA" % o))(object, object_sel) newobj_sel_string = (lambda o, sel: (o and sel and "%s and %s" % (o, sel)) or (o and not sel and "%s and name CA" % o))(newobj, object_sel) print('%s' % ('-' * 80)) print( 'Averaging %d states [%d, %d] of object "%s" (%d total states) ' 'to get new single-state object "%s"...' % (num_states2avg, first, last, obj_sel_string, num_states_tot, newobj)) print('%s' % ('-' * 80)) tmpobject = '%s_tmp4avg_%dto%d' % (object, first, last) i = 0 for eachstate in range(first, last + 1): i += 1 # because pymol states are indexed starting from 1 (not 0) cmd.create(tmpobject, obj_sel_string, eachstate, i) if fitverdict != 'no': print("-> proceeding WITH FITTING to reference state...") tmpobject_intrafit_rmsds = cmd.intra_fit(tmpobject) if verb: print(' intrafit_rmsds = ', ) print(tmpobject_intrafit_rmsds) else: print("-> proceeding WITHOUT FITTING to reference state...") # create an atom index map between original (complete) object and subset # to be averaged: atindex_map = [None] for at in cmd.get_model(newobj_sel_string).atom: atindex_map.append(at.index) if verb: print("-> atom index_map = ", ) print(atindex_map) newobj_chempy = cmd.get_model(tmpobject) for at in newobj_chempy.atom: this_at_idx = at.index sum_x = sum_y = sum_z = 0.0 # avg_x = avg_y = avg_z = 0.0 state_coords = [] # rmsd_sum = rmsd = 0.0 rmsd_sum = 0.0 for s in range(1, num_states2avg + 1): this_x = cmd.get_model(tmpobject, s).atom[this_at_idx - 1].coord[0] this_y = cmd.get_model(tmpobject, s).atom[this_at_idx - 1].coord[1] this_z = cmd.get_model(tmpobject, s).atom[this_at_idx - 1].coord[2] sum_x += this_x sum_y += this_y sum_z += this_z state_coords.append([this_x, this_y, this_z]) avg_x = sum_x / num_states2avg avg_y = sum_y / num_states2avg avg_z = sum_z / num_states2avg cmd.alter_state( 1, '{0:s} and id {1:d}'.format(newobj, atindex_map[this_at_idx]), 'x=%f' % avg_x) cmd.alter_state( 1, '{0:s} and id {1:d}'.format(newobj, atindex_map[this_at_idx]), 'y=%f' % avg_y) cmd.alter_state( 1, '{0:s} and id {1:d}'.format(newobj, atindex_map[this_at_idx]), 'z=%f' % avg_z) # position-specific RMSDs with respect to average structure: for somept in state_coords: rmsd_sum += pow(calcDist(somept, [avg_x, avg_y, avg_z]), 2.0) rmsd = sqrt(rmsd_sum / num_states2avg) # DEBUG: # DEBUG print('newobj = %sel / id = %d / b = %0.3f' # %(newobj,atindex_map[this_at_idx],rmsd) cmd.alter( '{0:s} and id {1:d}'.format(newobj, atindex_map[this_at_idx]), 'b=%0.3f' % rmsd) if verb: print('"%s" index = %d' % (obj_sel_string, this_at_idx)) print('rmsd = %0.3f' % rmsd) avg_file.write('{0:d}\t{1:0.3f}\n'.format(atindex_map[this_at_idx], rmsd)) # position-specific RMSDs averaged pairwise over the bundle: if pairs: for at in newobj_chempy.atom: this_at_idx = at.index loop_counter = 0 running_sum = 0.0 for s1 in range(1, num_states2avg + 1): for s2 in range(s1 + 1, num_states2avg + 1): # print('computing position %d, %d x %d'%(this_at_idx,s1,s2) pos1 = [ cmd.get_model(tmpobject, s1).atom[this_at_idx - 1].coord[0], cmd.get_model(tmpobject, s1).atom[this_at_idx - 1].coord[1], cmd.get_model(tmpobject, s1).atom[this_at_idx - 1].coord[2] ] pos2 = [ cmd.get_model(tmpobject, s2).atom[this_at_idx - 1].coord[0], cmd.get_model(tmpobject, s2).atom[this_at_idx - 1].coord[1], cmd.get_model(tmpobject, s2).atom[this_at_idx - 1].coord[2] ] loop_counter += 1 running_sum += pow(calcDist(pos1, pos2), 2.0) pairwise_rmsd = sqrt(running_sum / loop_counter) if verb: print('calculated pairwise RMSD for {0:d} pairs at ' 'position {0:d} = {1:0.3f}'.format( loop_counter, atindex_map[this_at_idx], pairwise_rmsd)) pair_file.write('{0:d}\t{1:0.3f}\n'.format( atindex_map[this_at_idx], pairwise_rmsd)) if writefiles: avg_file.close() if pairs: pair_file.close() cmd.delete(tmpobject) return newobj
] import numpy as np bb_rmsd_mean = np.mean(bb_rmsds) bb_rmsd_std = np.std(bb_rmsds) ha_rmsd_mean = np.mean(ha_rmsds) ha_rmsd_std = np.std(ha_rmsds) print('\n\n* POKY ENSEMBLE RMSD') print('* PDB FILE: ' + pdbpath) print('* Ordered residues by CYRANGE: ' + ordered) print('MODEL BB RMSD HA RMSD') for i in range(2, 21): m = '%2d <-> 1 ' % (i) m += '%6.3f ' % (bb_rmsds[i - 2]) m += '%6.3f ' % (ha_rmsds[i - 2]) print(m) print('--------------------------------------------') print('MEAN RMSD : %6.3f %6.3f' % (bb_rmsd_mean, ha_rmsd_mean)) print('STDEV RMSD: %6.3f %6.3f' % (bb_rmsd_std, ha_rmsd_std)) if s.show_message_yes_no('Save', 'Do you want to save the superimposed structure?'): out_path = s.save_filedialog('Save as', 'PDB (*.pdb);; mmCIF (*.cif);; Any (*)', '') if out_path == '': raise SystemExit cmd.intra_fit('for_rmsd & i. %s & n. N*+C*+O*+S*' % (oreg)) cmd.save(out_path, 'for_rmsd', state=0) s.show_message('Finished', 'Finished.')
import argparse # argparse.ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True) parser = argparse.ArgumentParser(description='') # parser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest]) parser.add_argument('-p', '--pdb') parser.add_argument('-t', '--traj') parser.add_argument('-s', '--sel', default='all') parser.add_argument('-r', '--refs', nargs='+') parser.add_argument('-o', '--out', default='rmsds.txt') args = parser.parse_args() cmd.load(args.pdb, 'trajin') cmd.load_traj(args.traj, 'trajin', state=1) nframes = cmd.count_states('trajin') print(f'Number of frames: {nframes}') for ref in args.refs: print(f'Loading {ref}') cmd.load(ref, 'trajin') cmd.remove('hydro and trajin') nref = len(args.refs) rmsds_all = [] for ind, frameref in enumerate([nframes + i for i in range(1, nref + 1)]): print(f'Computing RMSD for {args.refs[ind]}') rmsds = cmd.intra_fit(f'trajin and {args.sel}', state=frameref)[:nframes] rmsds_all.append(rmsds) rmsds_all = np.asarray(rmsds_all, dtype=object).T min_rmsd = rmsds_all.min(axis=1) args_min = rmsds_all.argmin(axis=1) args_min = np.asarray(args.refs)[args_min] np.savetxt(args.out, np.c_[rmsds_all, min_rmsd, args_min], fmt=' '.join(['%.4f', ] * (nref + 1)) + ' %s', header=' '.join([f'#{ref}' for ref in args.refs]) + ' #min_rmsd' + ' #min_rmsd_ref', comments='')
cmd.rewind() cmd.reset() cmd.delete("all") # Retain order and IDs for imported atoms cmd.set("retain_order", 1) cmd.set("pdb_retain_ids", 1) # Load PDB file cmd.load(pdb_filename, "complex") # Recognize helices and sheets cmd.dss() # Align everything cmd.intra_fit("all") # Set up display cmd.remove("resn WAT") # remove waters cmd.select("receptor", "(not resn MOL) and (not resn WAT) and (not hydrogen)") cmd.select("ligand", "resn MOL") cmd.deselect() cmd.hide("all") cmd.show("cartoon", "receptor") cmd.show("spheres", "ligand") util.cbay("ligand") cmd.color("green", "receptor") # Show surface # cmd.show('surface', 'receptor') # cmd.set('surface_color', 'white')
cmd.create('complex', 'complex', 1, iteration+1) cmd.alter_state(iteration+1, 'complex', '(x,y,z) = xyz_iter.next()', space=locals()) #for pdb_index in range(natoms): #if (pdb_index % 100)==0: print pdb_index #model_index = model_mapping[pdb_index] #model.atom[model_index].coord = (10 * ncfile.variables['positions'][iteration, replica, pdb_index, :]).squeeze().tolist() #for k in range(3): # model.atom[model_index].coord[k] = float(ncfile.variables['positions'][iteration, replica, pdb_index, k]) * 10.0 # convert to angstroms #cmd.load_model(model, 'complex', state=iteration+1) #cmd.load_model(model, 'complex') print "done" # Align all states cmd.intra_fit('all') cmd.hide('all') #cmd.rewind() cmd.select('receptor', 'not chain C and not hydrogen') cmd.select('mhc', 'chain A or chain B') cmd.select('tcr', 'chain D or chain E') cmd.select('ligand', 'chain C and not hydrogen') cmd.deselect() cmd.hide('all') cmd.show('cartoon', 'receptor') cmd.show('sticks', 'ligand') util.cbay('ligand') cmd.color('green', 'mhc') cmd.color('red', 'tcr')
from pymol import cmd from sys import argv # this script is run automatically by MCsim and will need to be changed for someone else's use numFrames = int(argv[1]) channel = int(argv[2]) pathPDB = argv[3] base = int(argv[4]) for idx in range(0, numFrames): cmd.load(pathPDB + "fine%03dv%i.pdb" % (idx, channel), "snap") cmd.intra_fit("snap") cmd.mset("1 -%d" % numFrames) cmd.show('spheres', 'resn DNA') #cmd.show('lines', 'resn DNA') cmd.hide('sticks', 'resn DNA') cmd.spectrum('count', 'rainbow', 'resn DNA') if base == 1: cmd.set("sphere_scale", 0.5) else: cmd.set("sphere_scale", 0.15) #cmd.mplay()
'(x,y,z) = xyz_iter.next()', space=locals()) #for pdb_index in range(natoms): #if (pdb_index % 100)==0: print pdb_index #model_index = model_mapping[pdb_index] #model.atom[model_index].coord = (10 * ncfile.variables['positions'][iteration, replica, pdb_index, :]).squeeze().tolist() #for k in range(3): # model.atom[model_index].coord[k] = float(ncfile.variables['positions'][iteration, replica, pdb_index, k]) * 10.0 # convert to angstroms #cmd.load_model(model, 'complex', state=iteration+1) #cmd.load_model(model, 'complex') print "done" # Align all states cmd.intra_fit('all') cmd.hide('all') #cmd.rewind() cmd.select('receptor', '(not resn MOL) and (not resn WAT) and (not hydrogen)') cmd.select('ligand', 'resn MOL and not hydrogen') cmd.select('ions', 'resn Na\+ or resn Cl\-') cmd.deselect() cmd.hide('all') cmd.show('spheres', 'receptor') util.cbaw('receptor') cmd.show('spheres', 'ligand') cmd.show('spheres', 'ions') util.cbay('ligand')
def avgStates(object='all',object_sel=None,first=1,last=0,newobj=None,fitverdict='no',\ verb=0,pairs=1,writefiles=1): ''' ARGUMENTS: object (string [defaults to 'all']): Starting PyMOL molecular object consisting of >1 states to be averaged object_sel (string [defaults to "name CA"]): An optional subset of atoms on which to operate (e.g., "name CA and resi 20-50") first (int [defaults to 1]): number of the first state to include in averaging last (int [defaults to last state]): Number of the last state to include in averaging. A value of '0' (zero) means use the last state in the object. newobj (string [defaults to name of object with string "_avg_AtoZ" appended, where A and Z are the numbers of the first and last frames included in the averaging]): Desired name of the new output pymol object (i.e., averaged structure) fitverdict (string [defaults to "no", any value != "no" is taken as a "yes"]): Use the pymol function intra_fit() to fit all the states of "object & object_sel" before calculating the average structure and RMSDs?? 'no' = NO, !'no' = yes verb (int [defaults to 0]): Verbosity level of output. 0 = quiet, !0 = verbose (e.g., write position-specific RMSDs to standard output). pairs (int [default 1]): Also calculate average pairwise RMSD for each residue position?? 0 = no, !0 = yes writefiles (int [default 1]): Write the calculated RMSD data to plaintext files?? 0 = no, !0 = yes NOTES: The state specified as 'first' is taken as the reference state for the (optional) intra_fit() alignment. If no selection is given, the string "and name CA" will be appended to the object and the averaging and rmsd calculations will be restricted to "object and name CA" (i.e., C-alpha atoms). To avoid this default behavior, specify a valid pymol atom selection for the object_sel argument (e.g., "name CA" or "name CA and resi 20-50" or whatever). ''' object = str(object) object_sel = str(object_sel) first=int(first) last=int(last) num_states_tot = cmd.count_states(object) if last<1: last=num_states_tot num_states2avg = last - first + 1 if newobj==None or newobj=='': newobj = "%s_avg_%dto%d"%(object,first,last) cmd.create(newobj,object,first,1) if writefiles: print '%s'%('-'*80) datfileprefix = '%s_%dto%d'%(replace(object,' ','_'),first,last) avg_rmsd_file = datfileprefix + '.avg_rmsd.dat' avg_file = open(avg_rmsd_file,'w') print 'Opened "%s" file for writing residue-specific RMSDs to average structure...'%avg_rmsd_file if pairs: pair_rmsd_file = datfileprefix + '.pair_rmsd.dat' pair_file = open(pair_rmsd_file,'w') print 'Opened "%s" file for writing averaged residue-specific pairwise RMSDs...'%pair_rmsd_file print '%s'%('-'*80) else: avg_file = open('/dev/null','w') # just do this instead of evaluating conditionals each time if pairs: pair_file = open('/dev/null','w') # through the atom loops (below)... obj_sel_string = (lambda o,s: (o and s and "%s and %s"%(o,s)) or \ (o and not s and "%s and name CA"%o))(object,object_sel) newobj_sel_string = (lambda o,s: (o and s and "%s and %s"%(o,s)) or \ (o and not s and "%s and name CA"%o))(newobj,object_sel) print '%s'%('-'*80) print 'Averaging %d states [%d, %d] of object "%s" (%d total states) to get new single-state object "%s"...' \ % (num_states2avg,first,last,obj_sel_string,num_states_tot,newobj) print '%s'%('-'*80) tmpobject='%s_tmp4avg_%dto%d'%(object,first,last) i=0 for eachstate in range(first,last+1): i+=1 # because pymol states are indexed starting from 1 (not 0) cmd.create(tmpobject,obj_sel_string,eachstate,i) if fitverdict != 'no': print "-> proceeding WITH FITTING to reference state..." tmpobject_intrafit_rmsds = cmd.intra_fit(tmpobject,1) if verb: print ' intrafit_rmsds = ', print tmpobject_intrafit_rmsds else: print "-> proceeding WITHOUT FITTING to reference state..." # create an atom index map between original (complete) object and subset to be averaged: atindex_map = [None] for at in cmd.get_model(newobj_sel_string,1).atom: atindex_map.append(at.index) if verb: print "-> atom index_map = ", print atindex_map newobj_chempy = cmd.get_model(tmpobject,1) for at in newobj_chempy.atom: this_at_idx = at.index sum_x = sum_y = sum_z = 0.0 avg_x = avg_y = avg_z = 0.0 state_coords=[] rmsd_sum = rmsd = 0.0 for s in range(1,num_states2avg+1): this_x = cmd.get_model(tmpobject,s).atom[this_at_idx-1].coord[0] this_y = cmd.get_model(tmpobject,s).atom[this_at_idx-1].coord[1] this_z = cmd.get_model(tmpobject,s).atom[this_at_idx-1].coord[2] sum_x += this_x sum_y += this_y sum_z += this_z state_coords.append([this_x,this_y,this_z]) avg_x = sum_x / num_states2avg avg_y = sum_y / num_states2avg avg_z = sum_z / num_states2avg cmd.alter_state(1,'%s and id %d'%(newobj,atindex_map[this_at_idx]),'x=%f'%avg_x) cmd.alter_state(1,'%s and id %d'%(newobj,atindex_map[this_at_idx]),'y=%f'%avg_y) cmd.alter_state(1,'%s and id %d'%(newobj,atindex_map[this_at_idx]),'z=%f'%avg_z) # position-specific RMSDs with respect to average structure: for somept in state_coords: rmsd_sum += pow(calcDist(somept,[avg_x,avg_y,avg_z]),2.0) rmsd = sqrt(rmsd_sum / num_states2avg) # DEBUG: # DEBUG print 'newobj = %s / id = %d / b = %0.3f'%(newobj,atindex_map[this_at_idx],rmsd) cmd.alter('%s and id %d'%(newobj,atindex_map[this_at_idx]),'b=%0.3f'%rmsd) if verb: print '"%s" index = %d'%(obj_sel_string,this_at_idx) print 'rmsd = %0.3f'%rmsd avg_file.write('%d\t%0.3f\n'%(atindex_map[this_at_idx],rmsd)) # position-specific RMSDs averaged pairwise over the bundle: if pairs: for at in newobj_chempy.atom: this_at_idx = at.index loop_counter = 0 running_sum = 0.0 for s1 in range(1,num_states2avg+1): for s2 in range(s1+1,num_states2avg+1): #DB print 'computing position %d, %d x %d'%(this_at_idx,s1,s2) pos1 = [ cmd.get_model(tmpobject,s1).atom[this_at_idx-1].coord[0], cmd.get_model(tmpobject,s1).atom[this_at_idx-1].coord[1], cmd.get_model(tmpobject,s1).atom[this_at_idx-1].coord[2] ] pos2 = [ cmd.get_model(tmpobject,s2).atom[this_at_idx-1].coord[0], cmd.get_model(tmpobject,s2).atom[this_at_idx-1].coord[1], cmd.get_model(tmpobject,s2).atom[this_at_idx-1].coord[2] ] loop_counter += 1 running_sum += pow(calcDist(pos1,pos2),2.0) # print '%s'%('.'*s2) pairwise_rmsd = sqrt(running_sum / loop_counter) if verb: print 'calculated pairwise RMSD for %d pairs at position %d = %0.3f' % \ (loop_counter,atindex_map[this_at_idx],pairwise_rmsd) pair_file.write('%d\t%0.3f\n'%(atindex_map[this_at_idx],pairwise_rmsd)) if writefiles: avg_file.close() if pairs: pair_file.close()