def calc_super_matrix(mobile, static): ''' DESCRIPTION Aligns two objects (or selections), returns the transformation matrix, and resets the matrix of the mobile object. Uses CEAlign PyMOL function for alignment. ARGUMENTS mobile = string: selection describing the mobile object whose rotation matrix will be reported static = string: selection describing the static object onto which the mobile object will be aligned REQUIRES: numpy ''' cmd.cealign(static, mobile) # cmd.super(mobile,static) T = cmd.get_object_matrix(mobile) R = numpy.identity(4) k = 0 for i in range(0, 4): for j in range(0, 4): R[i][j] = T[k] k += 1 return R
def calc_super_matrix(mobile, static): ''' DESCRIPTION Aligns two objects (or selections), returns the transformation matrix, and resets the matrix of the mobile object. Uses CEAlign PyMOL function for alignment. ARGUMENTS mobile = string: selection describing the mobile object whose rotation matrix will be reported static = string: selection describing the static object onto which the mobile object will be aligned REQUIRES: numpy ''' cmd.cealign(static, mobile) # cmd.super(mobile,static) T = cmd.get_object_matrix(mobile) R = numpy.identity(4) k = 0 for i in range(0, 4): for j in range(0, 4): R[i][j] = T[k] k += 1 return R
def show_motif(interaction, domain1, domain2, residues1, residues2): cmd.load("PDBFiles/%s.pdb" % domain1) cmd.load("PDBFiles/%s.pdb" % domain2) cmd.bg_color("white") cmd.hide("all") cmd.show("cartoon", "all") cmd.select("motif1", "none") cmd.select("motif1", "motif1 or (%s and n. CA and resi %s)" % (domain1, residues1)) cmd.select("motif2", "none") cmd.select("motif2", "motif2 or (%s and n. CA and resi %s)" % (domain2, residues2)) cmd.select("none") cmd.color("gray80", domain1) cmd.color("gray60", domain2) cmd.color("cyan", "motif1") cmd.color("magenta", "motif2") try: cmd.pair_fit("motif1", "motif2") cmd.center(domain1) cmd.png("PNGs/%s_%s_%s_PF.png" % (interaction, domain1, domain2), dpi=300) except CmdException: pass try: cmd.cealign("motif1", "motif2") cmd.center(domain1) cmd.png("PNGs/%s_%s_%s_CE.png" % (interaction, domain1, domain2), dpi=300) except CmdException: pass
def testCealign(self): cmd.load(self.datafile("1oky-frag.pdb"), "m1") cmd.load(self.datafile("1t46-frag.pdb"), "m2") r = cmd.cealign("m2", "m1", object="aln") self.assertAlmostEqual(r["RMSD"], 1.90375, delta=1e-4) alen = r["alignment_length"] self.assertEqual(alen, 40) self.assertEqual(alen, cmd.count_atoms("aln") / 2)
def testCealign(self): cmd.load(self.datafile("1oky-frag.pdb"), "m1") cmd.load(self.datafile("1t46-frag.pdb"), "m2") r = cmd.cealign("m2", "m1", object="aln") self.assertAlmostEqual(r["RMSD"], 1.90375, delta=1e-4) alen = r["alignment_length"] self.assertEqual(alen, 40) self.assertEqual(alen, cmd.count_atoms("aln") / 2)
def show_motif(value): cmd.delete("*") domain1 = align_pairs[value][0] domain2 = align_pairs[value][1] residues1 = align_pairs[value][2] residues2 = align_pairs[value][3] cmd.load("PDBFiles/%s.pdb" % domain1) cmd.load("PDBFiles/%s.pdb" % domain2) cmd.bg_color("white") cmd.hide("all") cmd.show("cartoon", "all") cmd.select("motif1", "none") cmd.select("motif1", "motif1 or (%s and n. CA and resi %s)" % (domain1, residues1)) cmd.select("motif2", "none") cmd.select("motif2", "motif2 or (%s and n. CA and resi %s)" % (domain2, residues2)) cmd.select("none") cmd.color("gray80", domain1) cmd.color("gray60", domain2) cmd.color("cyan", "motif1") cmd.color("magenta", "motif2") #cmd.seqview("on") try: cmd.pair_fit("motif1", "motif2", object="alignment") cmd.center(domain1) #cmd.png("PNGsRenumb/%s_%s_PF.png"%(domain1, domain2), dpi = 300) except CmdException: pass try: cmd.cealign("motif1", "motif2", object="alignment") cmd.center(domain1) #cmd.png("PNGsRenumb/%s_%s_CE.png"%(domain1, domain2), dpi = 300) except CmdException: pass
def align_all( subset = [], cealign = False ): """ Superimpose all open models onto the first one. This may not work well with selections. """ AllObj=cmd.get_names("all") for x in AllObj[1:]: print(AllObj[0],x) subset_tag = '' if isinstance( subset, int ): subset_tag = ' and resi %d' % subset elif isinstance( subset, list ) and len( subset ) > 0: subset_tag = ' and resi %d' % (subset[0]) for m in range( 1,len(subset)): subset_tag += '+%d' % subset[m] elif isinstance( subset, str ) and len( subset ) > 0: subset_tag = ' and %s' % subset if cealign: cmd.cealign(AllObj[0]+subset_tag, x+subset_tag) else: cmd.align(x+subset_tag,AllObj[0]+subset_tag) cmd.zoom()
def fibre_formation_AC(pdb_dir, model, oligo_dir, n_dimers, chains): """ Aligns chain A to C and B to D for successive tetramers. """ # loading and aligning tetramers cmd.load(os.path.join(pdb_dir, model), "{}_00".format(model[:-7])) for ii in range(1, n_dimers): cmd.load(os.path.join(pdb_dir, model), "{}_{:02d}".format(model[:-7], ii)) cmd.cealign("{}_{:02d} and chain C".format(model[:-7], ii - 1), "{}_{:02d} and chain A".format(model[:-7], ii)) # making fibre structure cmd.remove("Chain C+D") for ii in range(1, n_dimers): cmd.alter("{}_{:02d} and chain A".format(model[:-7], ii), "chain='{}'".format(chains[2 * ii])) cmd.alter("{}_{:02d} and chain B".format(model[:-7], ii), "chain='{}'".format(chains[2 * ii + 1])) cmd.select("fibre", "all") cmd.save(os.path.join(oligo_dir, "fibre_AC_{}".format(model)), "fibre") cmd.delete("all")
def measure_rmsd(pdbfile1, pdbfile2, mode='align'): """ To measure RMSD between two pdbfiles using pymol user can specify modes b/n align, cealign and super. default is align which is good for structurally similar pdbs """ pymol.finish_launching(["pymol", "-qc"]) cmd.load(pdbfile1, 'pdb_file1') cmd.load(pdbfile2, 'pdb_file2') if mode == 'align': res = cmd.align('pdb_file2', 'pdb_file1') RMSD = res[0] elif mode == 'cealign': res = cmd.cealign('pdb_file2', 'pdb_file1') RMSD = res['RMSD'] elif mode == 'super': res = cmd.super('pdb_file2', 'pdb_file1') RMSD = res[0] cmd.delete("*") return RMSD
def align_all(target=None, mobile_selection='name ca', target_selection='name ca', cutoff=2, cycles=5, cgo_object=0, method='align'): """ Aligns all models in a list to one target usage: align_all [target][target_selection=name ca][mobile_selection=name ca][cutoff=2][cycles=5][cgo_object=0][method='align'] where method can be align, super or cealign where target specifies the model id you want to align all others against, and target_selection, mobile_selection, cutoff and cycles are options passed to the align or super command. Options for method='align' or method='super': By default the selection is all C-alpha atoms and the cutoff is 2 and the number of cycles is 5. Setting cgo_object to 1, will cause the generation of an alignment object for each object. They will be named like <object>_on_<target>, where <object> and <target> will be replaced by the real object and target names. Example: align_all target=name1, mobile_selection=c. b & n. n+ca+c+o,target_selection=c. a & n. n+ca+c+o """ cutoff = int(cutoff) cycles = int(cycles) cgo_object = int(cgo_object) object_list = cmd.get_names() object_list.remove(target) rmsd = {} rmsd_list = [] for i in range(len(object_list)): if cgo_object: objectname = 'align_%s_on_%s' % (object_list[i], target) if method == 'align': rms = cmd.align('%s & %s' % (object_list[i], mobile_selection), '%s & %s' % (target, target_selection), cutoff=cutoff, cycles=cycles, object=objectname) elif method == 'super': rms = cmd.super('%s & %s' % (object_list[i], mobile_selection), '%s & %s' % (target, target_selection), cutoff=cutoff, cycles=cycles, object=objectname) elif method == 'cealign': rmsdict = cmd.cealign( '%s & %s' % (target, target_selection), '%s & %s' % (object_list[i], mobile_selection)) rms = [rmsdict['RMSD'], rmsdict['alignment_length'], 1, 0, 0] else: print( "only 'align', 'super' and 'cealign' are accepted as methods" ) sys.exit(-1) else: if method == 'align': rms = cmd.align('%s & %s' % (object_list[i], mobile_selection), '%s & %s' % (target, target_selection), cutoff=cutoff, cycles=cycles) elif method == 'super': rms = cmd.super('%s & %s' % (object_list[i], mobile_selection), '%s & %s' % (target, target_selection), cutoff=cutoff, cycles=cycles) elif method == 'cealign': rmsdict = cmd.cealign( '%s & %s' % (target, target_selection), '%s & %s' % (object_list[i], mobile_selection)) rms = [rmsdict['RMSD'], rmsdict['alignment_length'], 1, 0, 0] else: print( "only 'align', 'super' and 'cealign' are accepted as methods" ) sys.exit(-1) rmsd[object_list[i]] = (rms[0], rms[1]) rmsd_list.append((object_list[i], rms[0], rms[1])) rmsd_list.sort(lambda x, y: cmp(x[1], y[1])) # loop over dictionary and print out matrix of final rms values print("Aligning against:", target) for object_name in object_list: print("%s: %6.3f using %d atoms" % (object_name, rmsd[object_name][0], rmsd[object_name][1])) print("\nSorted from best match to worst:") for r in rmsd_list: print("%s: %6.3f using %d atoms" % r)
def ce_align(self, elements_to_align, output_file_name=None, use_seq_info=False): """ Actually performs the structural alignment. """ #---------------------------------------------------- # Run CE-alignment using the PyMOL built-in module. - #---------------------------------------------------- retain_order = True if retain_order: cmd.set("retain_order", 1) sel1 = elements_to_align[0].get_pymol_selector() sel2 = elements_to_align[1].get_pymol_selector() # Sets temporary names. tsel1 = "t" + elements_to_align[0].get_unique_index_header() tsel2 = "t" + elements_to_align[1].get_unique_index_header() cmd.set_name(sel1, tsel1) cmd.set_name(sel2, tsel2) # Actually performs the alignment. a = cmd.cealign(target=tsel1, mobile=tsel2, object="pymod_temp_cealign") # cmd.center('%s and %s' % (tsel1, tsel2)) # cmd.zoom('%s and %s' % (tsel1, tsel2)) # Updates the names of the chains PDB files and saves these new files. saved_file1 = sel1 + "_aligned.pdb" saved_file2 = sel2 + "_aligned.pdb" # elements_to_align[0].structure.chain_pdb_file_name = saved_file1 # elements_to_align[1].structure.chain_pdb_file_name = saved_file2 cmd.save(os.path.join(self.pymod.structures_dirpath, saved_file1), tsel1) cmd.save(os.path.join(self.pymod.structures_dirpath, saved_file2), tsel2) # Finally saves the structural alignment between the sequences. cmd.save( os.path.join(self.pymod.alignments_dirpath, output_file_name + ".aln"), "pymod_temp_cealign") cmd.delete("pymod_temp_cealign") # Converts it in .aln format. recs = SeqIO.parse( os.path.join(self.pymod.alignments_dirpath, output_file_name + ".aln"), "clustal") new_recs = [] for rec, pymod_element in zip( recs, (elements_to_align[0], elements_to_align[1])): new_rec_id = "_".join(rec.id[1:].split("_")[0:3]) new_rec_seq = str(rec.seq).replace( "?", "X") # Replaces modified residues. new_recs.append(SeqRecord(Seq(new_rec_seq), id=new_rec_id)) SeqIO.write( new_recs, os.path.join(self.pymod.alignments_dirpath, output_file_name + ".aln"), "clustal") # Sets the names of the objects back to original ones. cmd.set_name(tsel1, sel1) cmd.set_name(tsel2, sel2) if retain_order: cmd.set("retain_order", 0)
def align_allfiles(target='', files='*.pdb', res_sel='i. 1-', outfile='', name='', cutoff=2.0, cycles=5, method='align'): """ Aligns all models in a list of files to one target using either align or super method. Outputs: Rosetta energy terms,c-alpha, backbone, and all atom rmsd for each model sorted by c-alpha rmsd usage: align_allfiles target=target name,files=name.pdb,res_sel='i. 1-,outfile='file name' cutoff=2,cycles=5 method=align where target specifies the model id you want to align all others against, and cutoff and cycles are options passed to the align command. You can specify the files to load and align using a wildcard. You should specify same number of residues for the target and other models. if outfile is not given output is stddount By default all residues in the structure are aligned,cutoff is 2,number of cycles is 5, and align method is used. Example: pymol -cdq "run/$SCRIPTS/align_allfiles;align_allfiles <target name>,[none default arguments eg res_sel= i. 100-120,...]" """ cutoff = int(cutoff) cycles = int(cycles) cmd.load(target) # change 1 #print outfile, cutoff,cycles,res_sel #Define rmsd and residue selection for mobile and target all = 'all & %s' % res_sel ca = 'n. ca & %s' % res_sel bb = 'n. n+c+ca+o and %s' % res_sel #obtain files from folder and remove native #tar=tarfile.open('*.tgz','r:tgz') file_list = glob.glob(files) #file_list.remove(target) #file_list.sort() for file in file_list: if file == target: file_list.remove(file) else: file_list.sort() #print file_list # print len(file_list) extension = re.compile('(^.*[\/]|\.(pdb|ent|brk))') object_list = [] target = extension.sub('', target) # change 3 # Define rmsd storage variables rmsd_list = [] for i in range(len(file_list)): obj_name1 = extension.sub('', file_list[i]) object_list.append(extension.sub('', file_list[i])) # energy=popen('grep "total_energy"' file_list[i]) # print energy cmd.load(file_list[i], obj_name1) if method == 'align': rms_ca = cmd.align('%s & %s' % (object_list[i], ca), '%s & %s' % (target, ca), cutoff=cutoff, cycles=cycles) rms_bb = cmd.align('%s & %s' % (object_list[i], bb), '%s & %s' % (target, bb), cutoff=cutoff, cycles=cycles) rms_all = cmd.align('%s & %s' % (object_list[i], all), '%s & %s' % (target, all), cutoff=cutoff, cycles=cycles) #print '%s,%6.2f,%6.2f,%6.2f' %(object_list[i],rms_bb[0],rms_ca[0],rms_all[0]) elif method == 'super': rms_ca = cmd.super('%s & %s' % (object_list[i], ca), '%s & %s' % (target, ca), cutoff=cutoff, cycles=cycles) rms_bb = cmd.super('%s & %s' % (object_list[i], bb), '%s & %s' % (target, bb), cutoff=cutoff, cycles=cycles) rms_all = cmd.super('%s & %s' % (object_list[i], all), '%s & %s' % (target, all), cutoff=cutoff, cycles=cycles) #print '%s,%6.2f,%6.2f,%6.2f' %(object_list[i],rms_ca[0],rms_bb[0],rms_all[0]) elif method == 'cealign': rmsdict = cmd.cealign( '%s & %s' % (target, target_selection), '%s & %s' % (object_list[i], mobile_selection)) rms = [rmsdict['RMSD'], rmsdict['alignment_length'], 1, 0, 0] else: print "only 'align', 'super' and 'cealign' are accepted as methods" sys.exit(-1) #rms = cmd.align('%s & %s'%(object_list[i],mobile_selection),'%s & %s'%(target,target_selection),cutoff=cutoff,cycles=cycles,object=objectname) #else: #rms = cmd.align('%s & %s'%(object_list[i],mobile_selection),'%s & %s'%(target,target_selection),cutoff=cutoff,cycles=cycles) #rmsd[object_list[i]] = (rms_ca[0],rms_ca[1],rms_bb[0],rms_bb[1],rms_all[0],rms_all[1]) rmsd_list.append((object_list[i], rms_ca[0], rms_bb[0], rms_all[0])) cmd.delete(obj_name1) #print rmsd rmsd_list.sort( lambda x, y: cmp(x[2], y[2]) ) #compare ca rms you can assign other rms indexes to sort i.e index 2 for bb and index 3 for all atom # loop over dictionary and print out matrix of final rms values outfp = outfile table = False out = open(outfp + '_score_vs_rmsd.csv', 'w') out.write('model,score,ca,bb,all,fa_atr,fa_rep,fa_sol,fa_elec\n') for r in rmsd_list: for decoy in file_list: model = open(decoy, 'r') for line in model: line_split = line.split() if line_split[0] == '#BEGIN_POSE_ENERGIES_TABLE': table = True continue if table and line_split[0] == 'pose': score = float(line_split[-1]) fa_atr = float(line_split[1]) fa_rep = float(line_split[2]) fa_elec = float(line_split[5]) fa_sol = float(line_split[3]) if r[0] == decoy.replace('.pdb', ''): out.write( '%s,%6.2f,%6.2f,%6.2f,%6.2f,%6.2f,%6.2f,%6.2f,%6.2f\n' % (decoy, score, r[1], r[2], r[3], fa_atr, fa_rep, fa_sol, fa_elec)) print '%s,%6.2f,%6.2f,%6.2f,%6.2f,%6.2f,%6.2f,%6.2f,%6.2f\n' % ( decoy, score, r[1], r[2], r[3], fa_atr, fa_rep, fa_sol, fa_elec) out.close() #generate ensemble figure protocol = outfp.split('_')[1] cmd.bg_color(color='white') cmd.hide("all") cmd.show("cartoon") cmd.set("antialias", 1) cmd.set("ray_trace_mode", 0) cmd.set("depth_cue", 0) cmd.set("ray_trace_fog", 0) cmd.set("stick_radius", 0.1) cmd.set("cartoon_side_chain_helper", 1) cmd.set("cartoon_flat_sheets", 1) # cmd.set("cartoon_transparency",0.8) cmd.set("ray_shadow", 0) if protocol == 'loopmodel': cmd.rotate("x", 70) cmd.zoom("resi 143-163") cmd.rotate("z", 20) cmd.rotate("y", 45) cmd.move("y", 8) cmd.move("x", 3) cmd.save(outfp + 'ensemble.pse') cmd.delete("all") elif protocol == 'fixed' or protocol == 'backrub': util.cbab("chain h") util.cbac("chain l") util.cbam("chain g") cmd.turn("y", -140) cmd.turn("x", -10) cmd.turn("z", 10) cmd.move("y", -5) cmd.move("z", 30) cmd.png(outfp + '_ensemble.png', 2400, 2400, dpi=300, ray=1) cmd.save(outfp + 'ensemble.pse') cmd.delete("all") #generate score vs rmsd plot scorevsrmsd(outfp + '_score_vs_rmsd.csv', name=outfp)
def rmsd_by_residue(pdbid_mobile, pdbid_target): alignment_object = pdbid_mobile + '_aligned_to_' + pdbid_target alignment = cmd.cealign(target=pdbid_target + ' and chain A', mobile=pdbid_mobile + ' and (chain A)', target_state=1, mobile_state=1, object=alignment_object, gap_max=10, window=3) print alignment cmd.select(name=pdbid_target + '_alignment', selection='(' + pdbid_target + ') and ' + alignment_object) cmd.select(name=pdbid_mobile + '_alignment', selection='(' + pdbid_mobile + ') and ' + alignment_object) stored.target = [] stored.mobile = [] cmd.iterate(selection=pdbid_target + '_alignment', expression='stored.target.append([resn,resi,name,chain])') cmd.iterate(selection=pdbid_mobile + '_alignment', expression='stored.mobile.append([resn,resi,name,chain])') output_file_txt = open( 'rmsd-by-residue_target-' + pdbid_target + '_mobile-' + pdbid_mobile + '.txt', 'w') output_file_xvg = open( 'rmsd-by-residue_target-' + pdbid_target + '_mobile-' + pdbid_mobile + '.xvg', 'w') output_file_txt.write("n\ttarget(" + pdbid_target + ")\tmobile(" + pdbid_mobile + ")\tRMSD[A]\n") for i in range(0, len(stored.target)): target_res_name = stored.target[i][0] target_res_num = stored.target[i][1] target_atm_name = stored.target[i][2] target_chain = stored.target[i][3] mobile_res_name = stored.mobile[i][0] mobile_res_num = stored.mobile[i][1] mobile_atm_name = stored.mobile[i][2] mobile_chain = stored.mobile[i][3] distance = cmd.get_distance( atom1='(name ' + target_atm_name + ' ) and (resid ' + target_res_num + ') and (' + pdbid_target + ') and (chain ' + target_chain + ') and ((alt A) or (alt ""))', atom2='(name ' + mobile_atm_name + ' ) and (resid ' + mobile_res_num + ') and (' + pdbid_mobile + ') and (chain ' + mobile_chain + ') and ((alt A) or (alt ""))') print("%s\t%s-%s.%s:%s\t%s-%s.%s:%s\t%.3f\n" % (i + 1, target_res_name, target_res_num, target_atm_name, target_chain, mobile_res_name, mobile_res_num, mobile_atm_name, mobile_chain, distance)) output_file_txt.write( "%s\t%s-%s.%s:%s\t%s-%s.%s:%s\t%.3f\n" % (i + 1, target_res_name, target_res_num, target_atm_name, target_chain, mobile_res_name, mobile_res_num, mobile_atm_name, mobile_chain, distance)) output_file_xvg.write("%d\t%.3f\n" % (int(target_res_num), distance)) output_file_txt.write( "\n\nOverall RMSD = %.3f A\nAlignment length = %d\n\n" % (alignment['RMSD'], alignment['alignment_length'])) output_file_txt.close() output_file_xvg.close()