def CheckIdentical(totalchains): # cmd.reinitialize() chainresns = [] for chain in totalchains: label = 'chain ' + chain stored.residues = [] cmd.iterate(selector.process(label), 'stored.residues.append(resn)') chainresns.append(stored.residues) dupchain = [] for x in chainresns: dupchain.append(chainresns.count(x)) # print(chainresns) print(chainresns.count(x)) max_index = dupchain.index(max(dupchain)) finalchains = [] finalchains.append(totalchains[max_index]) indexc = 0 for x in range(len(chainresns)): if x != max_index and chainresns[x] == chainresns[max_index]: finalchains.append(totalchains[x]) print(finalchains) return finalchains
def sculpt_relax(selection, backbone=1, neighbors=0, model=None, cycles=100, state=0, quiet=1): ''' DESCRIPTION Relax the given selection. SO FAR ONLY SUPPORTS SELECTIONS WITHIN THE SAME MODEL! Do 100 iterations, 75 of them with all terms but low VDW weights, and 25 with only local geometry terms. With default VDW weights and atom clashes, the structure gets distorted very easily! USAGE sculpt_relax selection [, backbone [, neighbors [, model [, cycles ]]]] ''' from pymol import selector backbone, neighbors = int(backbone), int(neighbors) cycles, state, quiet = int(cycles), int(state), int(quiet) sele = selector.process(selection) org = cmd.get_object_list(sele)[0] if model is None: model = org elif model != org: sele = sele.replace('(%s)' % org, '(%s)' % model) cmd.protect() cmd.deprotect(sele) if not backbone: cmd.protect('name CA+C+N+O+OXT') cmd.sculpt_activate(model, state) cmd.set('sculpt_vdw_weight', 0.25, model) # Low VDW forces cmd.set('sculpt_field_mask', 0x1FF, model) # Default if neighbors: cmd.sculpt_iterate(model, state, int(cycles * 0.25)) cmd.deprotect('byres (%s within 6.0 of (%s))' % (model, sele)) if not backbone: cmd.protect('name CA+C+N+O+OXT') cmd.sculpt_iterate(model, state, cycles=int(cycles * 0.50)) else: cmd.sculpt_iterate(model, state, int(cycles * 0.75)) cmd.set('sculpt_field_mask', 0x01F, model) # Local Geometry Only cmd.sculpt_iterate(model, state, int(cycles * 0.25)) cmd.unset('sculpt_vdw_weight', model) cmd.unset('sculpt_field_mask', model) cmd.sculpt_deactivate(model) cmd.deprotect()
def get_sasa_mmtk(selection, state=-1, hydrogens='auto', quiet=1): ''' DESCRIPTION Get solvent accesible surface area using MMTK.MolecularSurface http://dirac.cnrs-orleans.fr/MMTK/ This command is very picky with missing atoms and wrong atom naming. SEE ALSO stub2ala, get_sasa, get_sasa_ball ''' try: import MMTK except ImportError: print(' ImportError: please install MMTK') raise CmdException from MMTK.PDB import PDBConfiguration from MMTK.Proteins import Protein from MMTK.MolecularSurface import surfaceAndVolume try: from cStringIO import StringIO except ImportError: from io import StringIO selection = selector.process(selection) state, quiet = int(state), int(quiet) radius = cmd.get_setting_float('solvent_radius') if hydrogens == 'auto': if cmd.count_atoms('(%s) and hydro' % selection) > 0: hydrogens = 'all' else: hydrogens = 'no_hydrogens' elif hydrogens == 'none': hydrogens = 'no_hydrogens' conf = PDBConfiguration(StringIO(cmd.get_pdbstr(selection))) system = Protein(conf.createPeptideChains(hydrogens)) try: area, volume = surfaceAndVolume(system, radius * 0.1) except: print(' Error: MMTK.MolecularSurface.surfaceAndVolume failed') raise CmdException if not quiet: print(' get_sasa_mmtk: %.3f Angstroms^2 (volume: %.3f Angstroms^3).' % (area * 1e2, volume * 1e3)) return area * 1e2
def FindPymolType(AtomID): global proteinpath pbase = os.path.basename(proteinpath) pname = pbase.split('.')[0] # print(molpath) cmd.load(proteinpath) stored.name = [] cmd.iterate(selector.process(pname + " and id " + str(AtomID)), 'stored.name.append(name)') cmd.delete(pname) if stored.name == []: return "" else: return stored.name[0]
def select_user_selection(sele, name="sele"): cmd.select(name, selector.process(sele))
def normalmodes_mmtk(selection, cutoff=12.0, ff='Deformation', first=7, last=10, prefix='mmtk', states=7, factor=-1, quiet=1): ''' DESCRIPTION Fast normal modes for large proteins using an elastic network model (CA only) Based on: http://dirac.cnrs-orleans.fr/MMTK/using-mmtk/mmtk-example-scripts/normal-modes/ ''' try: import MMTK except ImportError: print('Failed to import MMTK, please add to PYTHONPATH') raise CmdException selection = selector.process(selection) cutoff = float(cutoff) first, last = int(first), int(last) states, factor, quiet = int(states), float(factor), int(quiet) from math import log from chempy import cpv from MMTK import InfiniteUniverse from MMTK.PDB import PDBConfiguration from MMTK.Proteins import Protein from MMTK.NormalModes import NormalModes from MMTK.ForceFields import DeformationForceField, CalphaForceField from MMTK.FourierBasis import FourierBasis, estimateCutoff from MMTK.NormalModes import NormalModes, SubspaceNormalModes model = 'calpha' ff = ff.lower() if 'deformationforcefield'.startswith(ff): forcefield = DeformationForceField(cutoff=cutoff / 10.) elif 'calphaforcefield'.startswith(ff): forcefield = CalphaForceField(cutoff=cutoff / 10.) elif 'amber94forcefield'.startswith(ff): from MMTK.ForceFields import Amber94ForceField forcefield = Amber94ForceField() model = 'all' else: raise NotImplementedError('unknown ff = ' + str(ff)) if not quiet: print(' Forcefield:', forcefield.__class__.__name__) if model == 'calpha': selection = '(%s) and polymer and name CA' % (selection) f = StringIO(cmd.get_pdbstr(selection)) conf = PDBConfiguration(f) items = conf.createPeptideChains(model) universe = InfiniteUniverse(forcefield) universe.protein = Protein(*items) nbasis = max(10, universe.numberOfAtoms() / 5) cutoff, nbasis = estimateCutoff(universe, nbasis) if not quiet: print(" Calculating %d low-frequency modes." % nbasis) if cutoff is None: modes = NormalModes(universe) else: subspace = FourierBasis(universe, cutoff) modes = SubspaceNormalModes(universe, subspace) natoms = modes.array.shape[1] frequencies = modes.frequencies if factor < 0: factor = log(natoms) if not quiet: print(' set factor to %.2f' % (factor)) if True: # cmd.count_atoms(selection) != natoms: import tempfile, os from MMTK import DCD filename = tempfile.mktemp(suffix='.pdb') sequence = DCD.writePDB(universe, None, filename) z = [a.index for a in sequence] selection = cmd.get_unused_name('_') cmd.load(filename, selection, zoom=0) os.remove(filename) if cmd.count_atoms(selection) != natoms: print('hmm... still wrong number of atoms') def eigenfacs_iter(mode): x = modes[mode - 1].array return iter(x.take(z, 0)) for mode in range(first, min(last, len(modes)) + 1): name = prefix + '%d' % mode cmd.delete(name) if not quiet: print(' normalmodes: object "%s" for mode %d with freq. %.6f' % \ (name, mode, frequencies[mode-1])) for state in range(1, states + 1): cmd.create(name, selection, 1, state, zoom=0) cmd.alter_state( state, name, '(x,y,z) = cpv.add([x,y,z], cpv.scale(next(myit), myfac))', space={ 'cpv': cpv, 'myit': eigenfacs_iter(mode), 'next': next, 'myfac': 1e2 * factor * ((state - 1.0) / (states - 1.0) - 0.5) }) cmd.delete(selection) if model == 'calpha': cmd.set('ribbon_trace_atoms', 1, prefix + '*') cmd.show_as('ribbon', prefix + '*') else: cmd.show_as('lines', prefix + '*')
def save_pdb(filename, selection='(all)', state=-1, symm=1, ss=1, aniso=0, seqres=0, quiet=1): ''' DESCRIPTION Save the coordinates of a selection as pdb including the secondary structure information and, if possible, the unit cell. The latter requires the selction of a single object USAGE save_pdb filename, selection [, state [, symm [, ss [, aniso ]]]] ARGUMENTS filename = string: file path to be written selection = string: atoms to save {default: (all)} Note: to include the unit cell information you need to select a single object state = integer: state to save {default: -1 (current state)} symm = 0 or 1: save symmetry info if possible {default: 1} ss = 0 or 1: save secondary structure info {default: 1} aniso = 0 or 1: save ANISO records {default: 0} SEE ALSO save ''' selection = selector.process(selection) state, quiet = int(state), int(quiet) symm, ss = int(symm), int(ss) filename = cmd.exp_path(filename) f = open(filename, 'w') print('REMARK 200 Generated with PyMOL and psico'.ljust(80), file=f) # Write sequence if int(seqres): f.write(get_pdb_seqres(selection)) # Write the CRYST1 line if possible if symm: try: obj1 = cmd.get_object_list(selection)[0] sym = cmd.get_symmetry(obj1) if len(sym) != 7: raise f.write("CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-10s%4d\n" % tuple(sym + [1])) if not quiet: print(' Info: Wrote unit cell and space group info') except: if not quiet: print(' Info: No crystal information') # Write secondary structure if ss: try: sss = get_pdb_sss(selection, state, quiet) if not sss: raise f.write(sss) if not quiet: print(' Info: Wrote secondary structure info') except: if not quiet: print(' Info: No secondary structure information') # Write coordinates of selection pdbstr = cmd.get_pdbstr(selection, state) # fix END records if state == 0 and cmd.get_version()[1] < 1.6: pdbstr = '\n'.join( line for line in pdbstr.splitlines() if line != 'END') + '\nEND\n' # anisotropic b-factors from . import pymol_version if int(aniso) and pymol_version < 1.6 and \ cmd.get_model('first (%s)' % selection).atom[0].u_aniso[0] != 0.0: def mergeaniso(): atom_it = iter(cmd.get_model(selection, state).atom) for line in pdbstr.splitlines(True): yield line if line[:6] in ['ATOM ', 'HETATM']: yield 'ANISOU' + line[6:28] + \ ''.join('%7.0f' % (u*1e4) for u in atom_it.next().u_aniso) + \ line[70:] pdbstr = ''.join(mergeaniso()) f.write(pdbstr) f.close() if not quiet: print('Wrote PDB to \'' + filename + '\'')
prefix='mmtk', states=7, factor=-1, quiet=1, async=-1): ''' DESCRIPTION Fast normal modes for large proteins using an elastic network model (CA only) Based on: http://dirac.cnrs-orleans.fr/MMTK/using-mmtk/mmtk-example-scripts/normal-modes/ ''' try: import MMTK except ImportError: print 'Failed to import MMTK, please add to PYTHONPATH' raise CmdException selection = selector.process(selection) cutoff = float(cutoff) first, last = int(first), int(last) states, factor, quiet = int(states), float(factor), int(quiet) from math import log from chempy import cpv from MMTK import InfiniteUniverse from MMTK.PDB import PDBConfiguration from MMTK.Proteins import Protein from MMTK.NormalModes import NormalModes from MMTK.ForceFields import DeformationForceField, CalphaForceField from MMTK.FourierBasis import FourierBasis, estimateCutoff from MMTK.NormalModes import NormalModes, SubspaceNormalModes
def dispmap(molecule1="NIL", molecule2="NIL", mindist=30.0, mindelta=15.0, resi1=str(0), resi2=str(0), atom='CA', listlength=5, showsticks='yes'): if molecule1 == "NIL": assert len( cmd.get_names() ) != 0, "Did you forget to load a molecule? There are no objects in pymol." molecule1 = cmd.get_names()[0] if molecule2 == "NIL": assert len( cmd.get_names() ) != 0, "Did you forget to load a molecule? There are no objects in pymol." molecule2 = cmd.get_names()[1] print("You passed in %s and %s" % (molecule1, molecule2)) # Open filenames filename = str(molecule1) + "-" + str(molecule2) + "-" + str( atom) + "-dist" backbonefilename = str(molecule1) + "-" + str(molecule2) + "-" + str( atom) + "backbone-dist.txt" outfile = open(filename + ".txt", "w") backboneoutfile = open(filename + "-backbone.txt", "w") gnuoutfile = open(filename + ".plt", "w") print(("I have opened matrix %s for you\n" % (filename + ".txt"))) # Sorting for interesting residues for Obj1 and Obj2. # Input is a string, and need to be sorted. resi1 = resi1.split('.') resi2 = resi2.split('.') resi1List = [] resi2List = [] for i in resi1: if '-' in i: tmp = i.split('-') resi1List.extend(list(range(int(tmp[0]), int(tmp[-1]) + 1))) if '-' not in i: resi1List.append(int(i)) for i in resi2: if '-' in i: tmp = i.split('-') resi2List.extend(list(range(int(tmp[0]), int(tmp[-1]) + 1))) if '-' not in i: resi2List.append(int(i)) resi1List.sort() resi2List.sort() # Only take the lines where atom is specified in input Object3 = molecule1 + " and name " + str(atom) Object4 = molecule2 + " and name " + str(atom) # Open 2 name lists # Append residue and atom name to the lists stored.OpenPDB = [] stored.ClosedPDB = [] cmd.iterate(Object3, "stored.OpenPDB.append((resi, name, resn))") cmd.iterate(Object4, "stored.ClosedPDB.append((resi, name, resn))") # Open 2 x,y,z position lists # Append atom position stored.OpenPos = [] stored.ClosedPos = [] cmd.iterate_state(1, selector.process(Object3), "stored.OpenPos.append((x,y,z))") cmd.iterate_state(1, selector.process(Object4), "stored.ClosedPos.append((x,y,z))") # Sometimes residues gets skipped in X-ray crys, because of low signal or sim. This leads to number conflict. # Make ordered lists according to residue number. Find largest residue number via -1 OpenOrderedPDB = [] ClosedOrderedPDB = [] OpenOrderedPos = [] ClosedOrderedPos = [] BackboneDisp = [] # First fill lists with zeros for i in range(int(stored.OpenPDB[-1][0]) + 1): OpenOrderedPDB.append([0, 0, 0]) for i in range(int(stored.ClosedPDB[-1][0]) + 1): ClosedOrderedPDB.append([0, 0, 0]) for i in range(int(stored.OpenPDB[-1][0]) + 1): OpenOrderedPos.append((0, 0, 0)) for i in range(int(stored.ClosedPDB[-1][0]) + 1): ClosedOrderedPos.append((0, 0, 0)) for i in range(int(stored.OpenPDB[-1][0]) + 1): BackboneDisp.append([i, 0, "NIL", atom]) # Fill in data the right places j = 0 for i in stored.OpenPDB: OpenOrderedPDB[int(i[0])] = [int(i[0]), i[1], i[2]] OpenOrderedPos[int(i[0])] = stored.OpenPos[j] j = j + 1 j = 0 for i in stored.ClosedPDB: ClosedOrderedPDB[int(i[0])] = [int(i[0]), i[1], i[2]] ClosedOrderedPos[int(i[0])] = stored.ClosedPos[j] j = j + 1 # Make a list with the missing residues MissingRes = [] for index, resi in enumerate(OpenOrderedPDB): if abs(OpenOrderedPDB[index][0] - ClosedOrderedPDB[index][0]) != 0: MissingRes.append( abs(OpenOrderedPDB[index][0] - ClosedOrderedPDB[index][0])) print("Following residues miss in one of the files, and are discarded for") print("further calculations") print(MissingRes) print("") # Make the data matrix CalcMatrix = create_nXn_matrix(len(OpenOrderedPos)) print(("Calculating a %s X %s distance Matrix" % (len(OpenOrderedPos), len(ClosedOrderedPos)))) # Make a list with 10 most negative/positive distances MaxNegDist = [] MaxPosDist = [] for i in range(int(listlength)): MaxNegDist.append([0, 0, 0, 0, 0, 0, 0]) MaxPosDist.append([0, 0, 0, 0, 0, 0, 0]) # Calculate distances for i in range(len(OpenOrderedPos)): for j in range(len(ClosedOrderedPos)): if OpenOrderedPos[i][0] != 0 and ClosedOrderedPos[j][ 0] != 0 and OpenOrderedPDB[i][ 0] not in MissingRes and ClosedOrderedPDB[j][ 0] not in MissingRes: distOpenOpen = distance(OpenOrderedPos, OpenOrderedPos, i, j) distClosedClosed = distance(ClosedOrderedPos, ClosedOrderedPos, i, j) distOpenClosed = distance(OpenOrderedPos, ClosedOrderedPos, i, j) DeltaDist = distOpenClosed - distOpenOpen if i == j: BackboneDisp[i] = [ i, DeltaDist, OpenOrderedPDB[i][2], atom ] # Test if distance is larger than threshold if distOpenOpen >= float( mindist) and distClosedClosed >= float( mindist) and abs(DeltaDist) >= float(mindelta): CalcMatrix[i][j] = str(round(DeltaDist, 0)) if DeltaDist < 0 and DeltaDist < MaxNegDist[-1][0] and ( i in resi1List or resi1List[-1] == 0) and (j in resi2List or resi2List[-1] == 0): MaxNegDist[-1][0] = DeltaDist MaxNegDist[-1][1] = i MaxNegDist[-1][2] = j MaxNegDist[-1][3] = distOpenOpen MaxNegDist[-1][4] = distOpenClosed MaxNegDist[-1][5] = str(OpenOrderedPDB[i][2]) MaxNegDist[-1][6] = str(ClosedOrderedPDB[j][2]) MaxNegDist = sorted(MaxNegDist) if DeltaDist > 0 and DeltaDist > MaxPosDist[-1][0] and ( i in resi1List or resi1List[-1] == 0) and (j in resi2List or resi2List[-1] == 0): MaxPosDist[-1][0] = DeltaDist MaxPosDist[-1][1] = i MaxPosDist[-1][2] = j MaxPosDist[-1][3] = distOpenOpen MaxPosDist[-1][4] = distOpenClosed MaxPosDist[-1][5] = str(OpenOrderedPDB[i][2]) MaxPosDist[-1][6] = str(ClosedOrderedPDB[j][2]) MaxPosDist = sorted(MaxPosDist, reverse=True) print("I made a datamatrix and backbone.txt file for you") print(("matrix: %s backbone: %s" % (filename + ".txt", filename + "-backbone.txt"))) print( "I made a gnuplot file for you, to view the datamatrix and the backbone displacement" ) print(("filename: %s\n" % (filename + ".plt"))) # Print distance matrix line01 = "# Input 1: %s and Input 2: %s" % (molecule1, molecule2) line02 = "# Find for: %s with min. residue-residue dist: %s Angstrom" % ( atom, mindist) line03 = "# Looking for min. displacement dist: %s Angstrom" % (mindelta) line04 = "# I give nr# suggestions: %s, and do I show sticks in pymol?: %s" % ( listlength, showsticks) line05 = "# I look for suggestions in the range: ([0]=>means all residues)" line06 = "# for Input 1: %s and for Input 2: %s" % (resi1, resi2) line07 = "# Mutation info is BLOSUM62 log-odds likelihood score and PAM250 is probability in % for evolutionary distance" line08 = "###########################################################################################################" line09 = "# Max Negative and positive distances # Mutation info #" line10 = "# Obj.1 Obj.2 Delta Op-Op Cl-Cl # Obj.1 Obj.2 Delta Op-Op Cl-Cl # Res.1 Res.2 # Res.1 Res.2 #" line11 = "# Res.1 Res.2 -Dist Dist Dist # Res.1 Res.2 +Dist Dist Dist # B62/PAM250% # B62/PAM250% #" outfile.write(line01 + '\n') outfile.write(line02 + '\n') outfile.write(line03 + '\n') outfile.write(line04 + '\n') outfile.write(line05 + '\n') outfile.write(line06 + '\n') outfile.write(line07 + '\n') outfile.write(line08 + '\n') outfile.write(line09 + '\n') outfile.write(line08 + '\n') outfile.write(line10 + '\n') outfile.write(line11 + '\n') outfile.write(line08 + '\n') print(line01) print(line02) print(line03) print(line04) print(line05) print(line06) print(line07) print(line08) print(line09) print(line08) print(line10) print(line11) print(line08) for i in range(len(MaxNegDist)): text = "# %3s%3s %3s%3s %5s %5s %5s # %3s%3s %3s%3s %5s %5s %5s # %2s/%2s %2s/%2s # %2s/%2s %2s/%2s #" % ( MaxNegDist[i][5], MaxNegDist[i][1], MaxNegDist[i][6], MaxNegDist[i][2], round(MaxNegDist[i][0], 1), round(MaxNegDist[i][3], 1), round( MaxNegDist[i][4], 1), MaxPosDist[i][5], MaxPosDist[i][1], MaxPosDist[i][6], MaxPosDist[i][2], round( MaxPosDist[i][0], 1), round(MaxPosDist[i][3], 1), round(MaxPosDist[i][4], 1), cysb62(shortaa(str( MaxNegDist[i][5]))), pam250(shortaa(str(MaxNegDist[i][5]))), cysb62(shortaa(str( MaxNegDist[i][6]))), pam250(shortaa(str(MaxNegDist[i][6]))), cysb62(shortaa(str( MaxPosDist[i][5]))), pam250(shortaa(str(MaxPosDist[i][5]))), cysb62(shortaa(str( MaxPosDist[i][6]))), pam250(shortaa(str(MaxPosDist[i][6])))) outfile.write(text + '\n') print(text) for i in range(len(CalcMatrix)): writing = "" for j in range(len(CalcMatrix)): if str(CalcMatrix[i][j]) == "0.0": writing = writing + " " + "?" else: writing = writing + " " + str(CalcMatrix[i][j]) # Add break line writing = writing + " " + "\n" outfile.write(writing) outfile.close() for i in range(len(BackboneDisp)): line = "%3s %4s %3s %3s" % (BackboneDisp[i][0], round(BackboneDisp[i][1], 1), BackboneDisp[i][2], BackboneDisp[i][3]) backboneoutfile.write(line + '\n') backboneoutfile.close() # Make gnuplot plot file gnuoutfile.write("reset" + "\n") gnuoutfile.write("cd " + "'" + os.getcwd() + "'" + "\n") gnuoutfile.write("\n") gnuoutfile.write( "#Title hacks \\n is newline, and 0,1 is x,y offset adjustment" + "\n") gnuoutfile.write('set title "Protein ' + str(atom) + ' Displacement matrix map \\n ResRes min. ' + str(mindist) + ' Ang, ' + 'Delta min. ' + str(mindelta) + ' Ang"' + "\n") gnuoutfile.write("# x is column" + "\n") gnuoutfile.write("set xlabel 'Res nr. for " + str(molecule2) + "'" + "\n") gnuoutfile.write("# y is row" + "\n") gnuoutfile.write("set ylabel 'Res nr. for " + str(molecule1) + "'" + "\n") gnuoutfile.write("\n") gnuoutfile.write("#set xrange [300:550]; set yrange [0:400]" + "\n") gnuoutfile.write("#set xtics 50" + "\n") gnuoutfile.write("#set ytics 50" + "\n") gnuoutfile.write("#set mxtics 5" + "\n") gnuoutfile.write("#set mytics 5" + "\n") gnuoutfile.write("set size ratio 1" + "\n") gnuoutfile.write("unset key" + "\n") gnuoutfile.write("\n") gnuoutfile.write("set cbrange [-30:30]" + "\n") gnuoutfile.write("set palette defined (-30 'blue', 0 'white', 30 'red')" + "\n") gnuoutfile.write("set pm3d map" + "\n") gnuoutfile.write("\n") gnuoutfile.write("#set term postscript eps enhanced color" + "\n") gnuoutfile.write('#set output "' + filename + '.eps"' + "\n") gnuoutfile.write("set term png" + "\n") gnuoutfile.write('set output "' + filename + '.png"' + "\n") gnuoutfile.write("splot '" + str(filename + ".txt") + "' matrix" + "\n") gnuoutfile.write("\n") gnuoutfile.write("#For the backbone displacement" + "\n") gnuoutfile.write("\n") gnuoutfile.write('set title "Protein ' + str(atom) + ' Backbone displacement"' + "\n") gnuoutfile.write("set xlabel 'Residue number'" + "\n") gnuoutfile.write("set ylabel '" + str(atom) + " displacement (Ang)'" + "\n") gnuoutfile.write("\n") gnuoutfile.write("#set xrange [0:550]; set yrange [0:40]" + "\n") gnuoutfile.write("#set xtics 50" + "\n") gnuoutfile.write("#set ytics 10" + "\n") gnuoutfile.write("#set mxtics 5" + "\n") gnuoutfile.write("#set mytics 5" + "\n") gnuoutfile.write("set size ratio 0.75" + "\n") gnuoutfile.write("unset key" + "\n") gnuoutfile.write("\n") gnuoutfile.write("#set term postscript eps enhanced color" + "\n") gnuoutfile.write('#set output "' + filename + '-backbone.eps"' + "\n") gnuoutfile.write("set term png" + "\n") gnuoutfile.write('set output "' + filename + '-backbone.png"' + "\n") gnuoutfile.write("plot '" + str(filename + "-backbone.txt") + "' using 1:2 title 'Backbone displacement' with lines" + "\n") gnuoutfile.close() # Create stick residue objects for i in range(len(MaxNegDist)): name = str(i) + "_" + str(round(MaxNegDist[i][0], 1)) + "_" + shortaa( str(MaxNegDist[i][5])) + str(MaxNegDist[i][1]) + shortaa( str(MaxNegDist[i][6])) + str(MaxNegDist[i][2]) selection = str(molecule1) + " and resi " + str( MaxNegDist[i][1]) + "+" + str(MaxNegDist[i][2]) + " or " + str( molecule2) + " and resi " + str(MaxNegDist[i][2]) # print selection cmd.create(name, selection) if showsticks == 'yes' or showsticks == 'y': cmd.show("sticks", name) for i in range(len(MaxPosDist)): name = str(i) + "_" + str(round(MaxPosDist[i][0], 1)) + "_" + shortaa( str(MaxPosDist[i][5])) + str(MaxPosDist[i][1]) + shortaa( str(MaxPosDist[i][6])) + str(MaxPosDist[i][2]) selection = str(molecule1) + " and resi " + str( MaxPosDist[i][1]) + "+" + str(MaxPosDist[i][2]) + " or " + str( molecule2) + " and resi " + str(MaxPosDist[i][2]) # print selection cmd.create(name, selection) if showsticks == 'yes' or showsticks == 'y': cmd.show("sticks", name)
def optAlign( sel1, sel2 ): """ optAlign performs the Kabsch alignment algorithm upon the alpha-carbons of two selections. Example: optAlign MOL1 and i. 20-40, MOL2 and i. 102-122 Example 2: optAlign 1GGZ and i. 4-146 and n. CA, 1CLL and i. 4-146 and n. CA Two RMSDs are returned. One comes from the Kabsch algorithm and the other from PyMol based upon your selections. By default, this program will optimally align the ALPHA CARBONS of the selections provided. To turn off this feature remove the lines between the commented "REMOVE ALPHA CARBONS" below. @param sel1: First PyMol selection with N-atoms @param sel2: Second PyMol selection with N-atoms """ cmd.reset() # make the lists for holding coordinates # partial lists stored.sel1 = [] stored.sel2 = [] # full lists stored.mol1 = [] stored.mol2 = [] # -- CUT HERE sel1 += " and N. CA" sel2 += " and N. CA" # -- CUT HERE # Get the selected coordinates. We # align these coords. cmd.iterate_state(1, selector.process(sel1), "stored.sel1.append([x,y,z])") cmd.iterate_state(1, selector.process(sel2), "stored.sel2.append([x,y,z])") # get molecule name mol1 = cmd.identify(sel1,1)[0][0] mol2 = cmd.identify(sel2,1)[0][0] # Get all molecule coords. We do this because # we have to rotate the whole molcule, not just # the aligned selection cmd.iterate_state(1, mol1, "stored.mol1.append([x,y,z])") cmd.iterate_state(1, mol2, "stored.mol2.append([x,y,z])") # check for consistency assert len(stored.sel1) == len(stored.sel2) L = len(stored.sel1) assert L > 0 # must alway center the two proteins to avoid # affine transformations. Center the two proteins # to their selections. COM1 = numpy.sum(stored.sel1,axis=0) / float(L) COM2 = numpy.sum(stored.sel2,axis=0) / float(L) stored.sel1 -= COM1 stored.sel2 -= COM2 # Initial residual, see Kabsch. E0 = numpy.sum( numpy.sum(stored.sel1 * stored.sel1,axis=0),axis=0) + numpy.sum( numpy.sum(stored.sel2 * stored.sel2,axis=0),axis=0) # # This beautiful step provides the answer. V and Wt are the orthonormal # bases that when multiplied by each other give us the rotation matrix, U. # S, (Sigma, from SVD) provides us with the error! Isn't SVD great! V, S, Wt = numpy.linalg.svd( numpy.dot( numpy.transpose(stored.sel2), stored.sel1)) # we already have our solution, in the results from SVD. # we just need to check for reflections and then produce # the rotation. V and Wt are orthonormal, so their det's # are +/-1. reflect = float(str(float(numpy.linalg.det(V) * numpy.linalg.det(Wt)))) if reflect == -1.0: S[-1] = -S[-1] V[:,-1] = -V[:,-1] RMSD = E0 - (2.0 * sum(S)) RMSD = numpy.sqrt(abs(RMSD / L)) #U is simply V*Wt U = numpy.dot(V, Wt) # rotate and translate the molecule stored.sel2 = numpy.dot((stored.mol2 - COM2), U) stored.sel2 = stored.sel2.tolist() # center the molecule stored.sel1 = stored.mol1 - COM1 stored.sel1 = stored.sel1.tolist() # let PyMol know about the changes to the coordinates cmd.alter_state(1,mol1,"(x,y,z)=stored.sel1.pop(0)") cmd.alter_state(1,mol2,"(x,y,z)=stored.sel2.pop(0)") print "RMSD=%f" % RMSD # make the alignment OBVIOUS cmd.hide('everything') cmd.show('ribbon', sel1 + ' or ' + sel2) cmd.color('gray70', mol1 ) cmd.color('paleyellow', mol2 ) cmd.color('red', 'visible') cmd.show('ribbon', 'not visible') cmd.center('visible') cmd.orient() cmd.zoom('visible')
def optAlign(sel1, sel2): """ optAlign performs the Kabsch alignment algorithm upon the alpha-carbons of two selections. Example: optAlign MOL1 and i. 20-40, MOL2 and i. 102-122 Example 2: optAlign 1GGZ and i. 4-146 and n. CA, 1CLL and i. 4-146 and n. CA Two RMSDs are returned. One comes from the Kabsch algorithm and the other from PyMol based upon your selections. By default, this program will optimally align the ALPHA CARBONS of the selections provided. To turn off this feature remove the lines between the commented "REMOVE ALPHA CARBONS" below. @param sel1: First PyMol selection with N-atoms @param sel2: Second PyMol selection with N-atoms """ cmd.reset() # make the lists for holding coordinates # partial lists stored.sel1 = [] stored.sel2 = [] # full lists stored.mol1 = [] stored.mol2 = [] # now put the coordinates into a list # partials # -- REMOVE ALPHA CARBONS sel1 = sel1 + " and N. CA" sel2 = sel2 + " and N. CA" # -- REMOVE ALPHA CARBONS cmd.iterate_state(1, selector.process(sel1), "stored.sel1.append([x,y,z])") cmd.iterate_state(1, selector.process(sel2), "stored.sel2.append([x,y,z])") # full molecule mol1 = cmd.identify(sel1, 1)[0][0] mol2 = cmd.identify(sel2, 1)[0][0] cmd.iterate_state(1, mol1, "stored.mol1.append([x,y,z])") cmd.iterate_state(1, mol2, "stored.mol2.append([x,y,z])") K = kabsch() U, T1, T2, RMSD, c1, c2 = K.align(stored.sel1, stored.sel2, []) stored.mol2 = map( lambda v: [ T2[0] + ((v[0] * U[0][0]) + (v[1] * U[1][0]) + (v[2] * U[2][0])), T2[1] + ((v[0] * U[0][1]) + (v[1] * U[1][1]) + (v[2] * U[2][1])), T2[2] + ( (v[0] * U[0][2]) + (v[1] * U[1][2]) + (v[2] * U[2][2])) ], stored.mol2) #stored.mol1 = map(lambda v:[ v[0]+T1[0], v[1]+T1[1], v[2]+T1[2] ], stored.mol1) stored.mol1 = map(lambda v: [v[0] + T1[0], v[1] + T1[1], v[2] + T1[2]], stored.mol1) cmd.alter_state(1, mol1, "(x,y,z)=stored.mol1.pop(0)") cmd.alter_state(1, mol2, "(x,y,z)=stored.mol2.pop(0)") cmd.alter('all', "segi=''") cmd.alter('all', "chain=''") print "RMSD=%f" % cmd.rms_cur(sel1, sel2) print "MY RMSD=%f" % RMSD cmd.hide('everything') cmd.show('ribbon', sel1 + ' or ' + sel2) cmd.color('gray70', mol1) cmd.color('paleyellow', mol2) cmd.color('red', 'visible') cmd.show('ribbon', 'not visible') cmd.center('visible') cmd.orient() cmd.zoom('visible')
def optAlign( sel1, sel2 ): """ optAlign performs the Kabsch alignment algorithm upon the alpha-carbons of two selections. Example: optAlign MOL1 and i. 20-40, MOL2 and i. 102-122 Example 2: optAlign 1GGZ and i. 4-146 and n. CA, 1CLL and i. 4-146 and n. CA Two RMSDs are returned. One comes from the Kabsch algorithm and the other from PyMol based upon your selections. By default, this program will optimally align the ALPHA CARBONS of the selections provided. To turn off this feature remove the lines between the commented "REMOVE ALPHA CARBONS" below. @param sel1: First PyMol selection with N-atoms @param sel2: Second PyMol selection with N-atoms """ cmd.reset() # make the lists for holding coordinates # partial lists stored.sel1 = [] stored.sel2 = [] # full lists stored.mol1 = [] stored.mol2 = [] # now put the coordinates into a list # partials # -- REMOVE ALPHA CARBONS sel1 = sel1 + " and N. CA" sel2 = sel2 + " and N. CA" # -- REMOVE ALPHA CARBONS cmd.iterate_state(1, selector.process(sel1), "stored.sel1.append([x,y,z])") cmd.iterate_state(1, selector.process(sel2), "stored.sel2.append([x,y,z])") # full molecule mol1 = cmd.identify(sel1,1)[0][0] mol2 = cmd.identify(sel2,1)[0][0] cmd.iterate_state(1, mol1, "stored.mol1.append([x,y,z])") cmd.iterate_state(1, mol2, "stored.mol2.append([x,y,z])") K = kabsch() U, T1, T2, RMSD, c1, c2 = K.align(stored.sel1, stored.sel2, []) stored.mol2 = map(lambda v:[T2[0]+((v[0]*U[0][0])+(v[1]*U[1][0])+(v[2]*U[2][0])),T2[1]+((v[0]*U[0][1])+(v[1]*U[1][1])+(v[2]*U[2][1])),T2[2]+((v[0]*U[0][2])+(v[1]*U[1][2])+(v[2]*U[2][2]))],stored.mol2) #stored.mol1 = map(lambda v:[ v[0]+T1[0], v[1]+T1[1], v[2]+T1[2] ], stored.mol1) stored.mol1 = map(lambda v:[ v[0]+T1[0], v[1]+T1[1], v[2]+T1[2] ], stored.mol1) cmd.alter_state(1,mol1,"(x,y,z)=stored.mol1.pop(0)") cmd.alter_state(1,mol2,"(x,y,z)=stored.mol2.pop(0)") cmd.alter( 'all',"segi=''") cmd.alter('all', "chain=''") print "RMSD=%f" % cmd.rms_cur(sel1, sel2) print "MY RMSD=%f" % RMSD cmd.hide('everything') cmd.show('ribbon', sel1 + ' or ' + sel2) cmd.color('gray70', mol1 ) cmd.color('paleyellow', mol2 ) cmd.color('red', 'visible') cmd.show('ribbon', 'not visible') cmd.center('visible') cmd.orient() cmd.zoom('visible')
def save_pdb(filename, selection='(all)', state=-1, symm=1, ss=1, aniso=0, seqres=0, quiet=1): ''' DESCRIPTION Save the coordinates of a selection as pdb including the secondary structure information and, if possible, the unit cell. The latter requires the selction of a single object USAGE save_pdb filename, selection [, state [, symm [, ss [, aniso ]]]] ARGUMENTS filename = string: file path to be written selection = string: atoms to save {default: (all)} Note: to include the unit cell information you need to select a single object state = integer: state to save {default: -1 (current state)} symm = 0 or 1: save symmetry info if possible {default: 1} ss = 0 or 1: save secondary structure info {default: 1} aniso = 0 or 1: save ANISO records {default: 0} SEE ALSO save ''' _assert_package_import() from . import pymol_version selection = selector.process(selection) state, quiet = int(state), int(quiet) symm, ss = int(symm), int(ss) filename = cmd.exp_path(filename) f = open(filename, 'w') print('REMARK 200 Generated with PyMOL and psico'.ljust(80), file=f) # Write sequence if int(seqres): f.write(get_pdb_seqres(selection)) # Write the CRYST1 line if possible if symm: try: obj1 = cmd.get_object_list(selection)[0] sym = cmd.get_symmetry(obj1) if len(sym) != 7: raise f.write("CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-10s%4d\n" % tuple(sym + [1])) if not quiet: print(' Info: Wrote unit cell and space group info') except: if not quiet: print(' Info: No crystal information') # Write secondary structure if ss: try: sss = get_pdb_sss(selection, state, quiet) if not sss: raise f.write(sss) if not quiet: print(' Info: Wrote secondary structure info') except: if not quiet: print(' Info: No secondary structure information') # Write coordinates of selection pdbstr = cmd.get_pdbstr(selection, state) # fix END records if state == 0 and pymol_version < 1.6: pdbstr = '\n'.join(line for line in pdbstr.splitlines() if line != 'END') + '\nEND\n' # anisotropic b-factors if int(aniso) and pymol_version < 1.6 and \ cmd.get_model('first (%s)' % selection).atom[0].u_aniso[0] != 0.0: def mergeaniso(): atom_it = iter(cmd.get_model(selection, state).atom) for line in pdbstr.splitlines(True): yield line if line[:6] in ['ATOM ', 'HETATM']: yield 'ANISOU' + line[6:28] + \ ''.join('%7.0f' % (u*1e4) for u in next(atom_it).u_aniso) + \ line[70:] pdbstr = ''.join(mergeaniso()) f.write(pdbstr) f.close() if not quiet: print('Wrote PDB to \''+filename+'\'')
def save_pdb(filename, selection="(all)", state=-1, symm=1, ss=1, aniso=0, quiet=1): """ DESCRIPTION Save the coordinates of a selection as pdb including the secondary structure information and, if possible, the unit cell. The latter requires the selction of a single object USAGE save_pdb filename, selection [, state [, symm [, ss [, aniso ]]]] ARGUMENTS filename = string: file path to be written selection = string: atoms to save {default: (all)} Note: to include the unit cell information you need to select a single object state = integer: state to save {default: -1 (current state)} symm = 0 or 1: save symmetry info if possible {default: 1} ss = 0 or 1: save secondary structure info {default: 1} aniso = 0 or 1: save ANISO records {default: 0} SEE ALSO save """ selection = selector.process(selection) state, quiet = int(state), int(quiet) symm, ss = int(symm), int(ss) filename = cmd.exp_path(filename) f = open(filename, "w") print >> f, "REMARK 200 Generated with PyMOL and psico".ljust(80) # Write the CRYST1 line if possible if symm: try: obj1 = cmd.get_object_list(selection)[0] sym = cmd.get_symmetry(obj1) if len(sym) != 7: raise f.write("CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-10s%4d\n" % tuple(sym + [1])) if not quiet: print " Info: Wrote unit cell and space group info" except: if not quiet: print " Info: No crystal information" # Write secondary structure if ss: try: sss = get_pdb_sss(selection, state, quiet) if not sss: raise f.write(sss) if not quiet: print " Info: Wrote secondary structure info" except: if not quiet: print " Info: No secondary structure information" # Write coordinates of selection pdbstr = cmd.get_pdbstr(selection, state) # fix END records if state == 0 and cmd.get_version()[1] < 1.6: pdbstr = "\n".join(line for line in pdbstr.splitlines() if line != "END") + "\nEND\n" # anisotropic b-factors if int(aniso) and cmd.get_model("first (%s)" % selection).atom[0].u_aniso[0] != 0.0: def mergeaniso(): atom_it = iter(cmd.get_model(selection, state).atom) for line in pdbstr.splitlines(True): yield line if line[:6] in ["ATOM ", "HETATM"]: yield "ANISOU" + line[6:28] + "".join("%7.0f" % (u * 1e4) for u in atom_it.next().u_aniso) + line[ 70: ] pdbstr = "".join(mergeaniso()) f.write(pdbstr) f.close() if not quiet: print "Wrote PDB to '" + filename + "'"
def cealign(sel1, sel2): """ Rough CE Structure-based Alignment of two protein structures Overview: cealign will try its best to align the alpha-carbon atoms provided in both selections sel1 and sel2. The algorithm follows the paper written by Shindyalov and Bourne. A few modifications to the algo- rithm are introduced, partly due to lazyness, and partly due to improving speed and accuracy while not conflicting with the lazyness requirement. :-) Params: \@param sel1: (string) valid PyMol selection string of protein 1 to align \@param sel2: (string) valid PyMol selection string of protein 2 to align Returns: \@return: (string) the CE-score and the RMSD of the alignment Side-Effects: \@note: rotates and translates the objects (proteins) provided in the selections, sel1 and sel2, to represent the alignment. Probably will also change their representation to more clearly show the aligned segments. Requires: Requires the Kabsch algorithm for protein optimal superposition. Don't worry, I already wrote this as a foray into the academic: you may download and install it from the PyMol wiki at: http://www.pymolwiki.org/index.php/Kabsch Bugs: Many, I'm sure. Please forward bugs/comments to [email protected] """ ######################################################################### # CE specific defines. Don't change these unless you know # what you're doing. See the documentation. ######################################################################### # WINDOW SIZE # make sure you set this variable in cealign.py, as well! winSize = 8 # FOR AVERAGING winSum = (winSize - 1) * (winSize - 2) / 2 # max gap size gapMax = 30 # make the lists for holding coordinates # partial lists stored.sel1 = [] stored.sel2 = [] # full lists stored.mol1 = [] stored.mol2 = [] # now put the coordinates into a list # partials # -- REMOVE ALPHA CARBONS sel1 = sel1 + " and n. CA" sel2 = sel2 + " and n. CA" # -- REMOVE ALPHA CARBONS cmd.iterate_state(1, selector.process(sel1), "stored.sel1.append([x,y,z])") cmd.iterate_state(1, selector.process(sel2), "stored.sel2.append([x,y,z])") # full molecule mol1 = cmd.identify(sel1, 1)[0][0] mol2 = cmd.identify(sel2, 1)[0][0] # put all atoms from MOL1 & MOL2 into stored.mol1 cmd.iterate_state(1, mol1, "stored.mol1.append([x,y,z])") cmd.iterate_state(1, mol2, "stored.mol2.append([x,y,z])") if (len(stored.mol1) == 0): print "ERROR: Your first selection was empty." return if (len(stored.mol2) == 0): print "ERROR: Your second selection was empty." return # call the C function alignString = ccealign((stored.sel1, stored.sel2)) if (len(alignString) == 1): if (len(alignString[0]) == 0): print "\n\nERROR: There was a problem with CEAlign's C Module. The return value was blank." print "ERROR: This is obviously bad. Please inform a CEAlign developer.\n\n" return bestPathID = -1 bestPathScore = 100000 bestStr1 = "" bestStr2 = "" # for each of the 20 possible alignments returned # we check each one for the best CE-Score and keep # that one. The return val of ccealign is a list # of lists of pairs. for curAlignment in alignString: seqCount = len(curAlignment) matA = None matB = None if (seqCount == 0): continue for AFP in curAlignment: first, second = AFP if (matA == None and matB == None): matA = [stored.sel1[first - 1]] matB = [stored.sel2[second - 1]] else: matA.append(stored.sel1[first - 1]) matB.append(stored.sel2[second - 1]) curScore = simpAlign(matA, matB, mol1, mol2, stored.mol1, stored.mol2, align=0, L=len(matA)) ######################################################################### # if you want the best RMSD, not CE Score uncomment here down ######################################################################### #if ( curScore < bestPathScore ): #bestPathScore = curScore #bestMatA = matA #bestMatB = matB ######################################################################### # if you want the best RMSD, not CE Score uncomment here up ######################################################################### ######################################################################### # if you want a proven, "better" alignment use the CE-score instead # Uncomment here down for CE-Score ######################################################################### internalGaps = 0.0 for g in range(0, seqCount - 1): if (not curAlignment[g][0] + 1 == curAlignment[g + 1][0]): internalGaps += curAlignment[g + 1][0] if (not curAlignment[g][1] + 1 == curAlignment[g + 1][1]): internalGaps += curAlignment[g + 1][1] aliLen = float(len(curAlignment)) numGap = internalGaps curScore = float((curScore / aliLen) * (1.0 + (numGap / aliLen))) if (curScore < bestPathScore): bestPathScore = curScore bestMatA = matA bestMatB = matB ######################################################################### # if you want a proven, "better" alignment use the CE-score instead # Uncomment here UP for CE-Score ######################################################################### # align the best one string simpAlign(bestMatA, bestMatB, mol1, mol2, stored.mol1, stored.mol2, align=1, L=len(bestMatA))
async=-1): ''' DESCRIPTION Fast normal modes for large proteins using an elastic network model (CA only) Based on: http://dirac.cnrs-orleans.fr/MMTK/using-mmtk/mmtk-example-scripts/normal-modes/ ''' try: import MMTK except ImportError: print('Failed to import MMTK, please add to PYTHONPATH') raise CmdException selection = selector.process(selection) cutoff = float(cutoff) first, last = int(first), int(last) states, factor, quiet = int(states), float(factor), int(quiet) from math import log from chempy import cpv from MMTK import InfiniteUniverse from MMTK.PDB import PDBConfiguration from MMTK.Proteins import Protein from MMTK.NormalModes import NormalModes from MMTK.ForceFields import DeformationForceField, CalphaForceField from MMTK.FourierBasis import FourierBasis, estimateCutoff from MMTK.NormalModes import NormalModes, SubspaceNormalModes
def PDB_Info(file, nonidentical): # Need to load the file so we can do things with it # cmd.do('set retain_order,1') cmd.load(file, 'obj') # The file will be a docked protein .pdb chains = ChainOrder(file) ChainNum = len(chains) # Contains sublists, each with [ChainNum, [ListofChains]], the list of chains which corresponds to the chainNum ListofChains = [] # Chain order; go through .pdb and get the order # For each chain, check the number of res; keep a list of the chains stored.residues = [] print(chains) ichains = CheckIdentical(chains) if nonidentical: chains = ichains print("identicalchains") print(ichains) # Keeps a list of the identical chains for chain in chains: label = 'chain ' + chain print(label + 'chain') # Res_min, res_max stored.residues = [] cmd.iterate(selector.process(label), 'stored.residues.append(resv)') val = float(stored.residues[-1] - stored.residues[0]) minval = stored.residues[0] maxval = stored.residues[-1] # print("val: " + str(val)) if len(ListofChains) == 0: # Add the first chain to the list ListofChains.append([[minval, maxval], [label]]) # The list is not empty, but the chain num corresponds else: # Loop through the ListofChains list x = 0 Added = 0 while x in range(len(ListofChains)): chaindiff = float(ListofChains[x][0][1]) - float( ListofChains[x][0][0]) if chaindiff == val: # Add val to the list ListofChains[x][1].append(label) Added = 1 break x += 1 # Had not been added yet, need to add a new entry if Added == 0: ListofChains.append([[minval, maxval], [label]]) max = 0 minval = 0 maxval = 0 chainstrings = [] for item in ListofChains: if len(item[1]) > max: minval = item[0][0] maxval = item[0][1] max = maxval - minval if Testing: print("min: " + str(minval) + " max: " + str(maxval)) chainstrings = item[1] newchains = [] for x in chainstrings: newchains.append(x.split()[1]) cmd.delete('obj') global o_num, minofres, maxofres, ListChains o_num = len(chains) minofres = minval maxofres = maxval ListChains = chains return minval, maxval, chains
def select_alpha_carbons(sele, name='alpha_c'): cmd.select(name, selector.process(sele + " and n. ca")) return name
def affineStretch(selection, stretch): # stretch molecule using affine transformations stored.altered = [] cmd.iterate_state(1, selector.process(selection), f"stored.altered.append([x,{stretch}*y,z])") cmd.alter_state(1, selection, "(x,y,z) = stored.altered.pop(0)")
def LigandFileSort(filelist, maindir, Ligand_path, Complex_path, ppath): # Check the first entry of the list for the type. # If the type is pdb then just copy over # print("ligand file sort") # cmd.do('set retain_order,1') for file in filelist: # Splits on the '.' into the name and ext file_name, file_ext = file.split('.') # copy file from source to destination New_ligpath = os.path.join(Ligand_path, file_name + '.' + file_ext) old_ligpath = os.path.join(maindir, file_name + '.' + file_ext) copyfile(old_ligpath, New_ligpath) New_ppath = os.path.join(Ligand_path, os.path.basename(ppath)) copyfile(ppath, New_ppath) # Check if pdb, if it isn't, make pdb and delete other file pfilewext = os.path.basename(ppath) pfile, pext = pfilewext.split('.') if pext != 'pdb': os.chdir(Ligand_path) toPDB(ppath) os.remove(os.path.basename(ppath)) if filelist[0].split('.')[1] == 'pdb': pdbfiles = filelist else: # Change directories to the ligand files and convert all to pdb. os.chdir(Ligand_path) pdbfiles = [] for file in filelist: # Splits on the '.' into the name and ext file_name, file_ext = file.split('.') pdbfile = toPDB(file) pdbfiles.append(pdbfile) for nonpdb in filelist: os.remove(nonpdb) os.chdir(Ligand_path) stored.residues = [] cmd.load(pdbfiles[0]) file_name = pdbfiles[0].split('.')[0] cmd.iterate(selector.process(file_name), 'stored.residues.append(resn)') UNK_var = stored.residues[0] cmd.delete(file_name) # Now deal with changing to complexes. # Copy over the files to the complex directory # switch to the complex directory os.chdir(Complex_path) for ligand in pdbfiles: # new complex path new_complex_path = os.path.join(Complex_path, ligand) new_ligand_path = os.path.join(Ligand_path, ligand) # print("new complex path: " + new_complex_path) # copy file copyfile(new_ligand_path, new_complex_path) # print("copy file done. " + ligand) # modify to complex MakeComplex(ligand, ppath) # Change dir back os.chdir(maindir) return UNK_var, pdbfiles
def optAlignRNA( sel1, sel2 ): """ optAlignRNA performs the Kabsch alignment algorithm upon the C1' carbons of two selections. Example: optAlignRNA 1JU7 and i. 1-16 and n. C1', 1CLL and i. 4-146 and n. C1' Two RMSDs are returned. One comes from the Kabsch algorithm and the other from PyMOL based upon your selections. This function can be run in a for loop to fit multiple structures with a common prefix name: for x in cmd.get_names(): optAlignRNA(x, "1JU7_0001") or get the rmsds for all combinations, do the following: [[optAlignRNA(x, y) for x in cmd.get_names()] for y in cmd.get_names()] """ cmd.reset() # make the lists for holding coordinates # partial lists stored.sel1 = [] stored.sel2 = [] # full lists stored.mol1 = [] stored.mol2 = [] # -- CUT HERE sel1 += " and N. C1'" sel2 += " and N. C1'" # -- CUT HERE # Get the selected coordinates. We # align these coords. cmd.iterate_state(1, selector.process(sel1), "stored.sel1.append([x,y,z])") cmd.iterate_state(1, selector.process(sel2), "stored.sel2.append([x,y,z])") # get molecule name mol1 = cmd.identify(sel1,1)[0][0] mol2 = cmd.identify(sel2,1)[0][0] # Get all molecule coords. We do this because # we have to rotate the whole molcule, not just # the aligned selection cmd.iterate_state(1, mol1, "stored.mol1.append([x,y,z])") cmd.iterate_state(1, mol2, "stored.mol2.append([x,y,z])") # check for consistency assert len(stored.sel1) == len(stored.sel2) L = len(stored.sel1) assert L > 0 # must alway center the two proteins to avoid # affine transformations. Center the two proteins # to their selections. COM1 = numpy.sum(stored.sel1,axis=0) / float(L) COM2 = numpy.sum(stored.sel2,axis=0) / float(L) stored.sel1 -= COM1 stored.sel2 -= COM2 # Initial residual, see Kabsch. E0 = numpy.sum( numpy.sum(stored.sel1 * stored.sel1,axis=0),axis=0) + numpy.sum( numpy.sum(stored.sel2 * stored.sel2,axis=0),axis=0) # # This beautiful step provides the answer. V and Wt are the orthonormal # bases that when multiplied by each other give us the rotation matrix, U. # S, (Sigma, from SVD) provides us with the error! Isn't SVD great! V, S, Wt = numpy.linalg.svd( numpy.dot( numpy.transpose(stored.sel2), stored.sel1)) # we already have our solution, in the results from SVD. # we just need to check for reflections and then produce # the rotation. V and Wt are orthonormal, so their det's # are +/-1. reflect = float(str(float(numpy.linalg.det(V) * numpy.linalg.det(Wt)))) if reflect == -1.0: S[-1] = -S[-1] V[:,-1] = -V[:,-1] RMSD = E0 - (2.0 * sum(S)) RMSD = numpy.sqrt(abs(RMSD / L)) #U is simply V*Wt U = numpy.dot(V, Wt) # rotate and translate the molecule stored.sel2 = numpy.dot((stored.mol2 - COM2), U) stored.sel2 = stored.sel2.tolist() # center the molecule stored.sel1 = stored.mol1 - COM1 stored.sel1 = stored.sel1.tolist() # let PyMol know about the changes to the coordinates cmd.alter_state(1,mol1,"(x,y,z)=stored.sel1.pop(0)") cmd.alter_state(1,mol2,"(x,y,z)=stored.sel2.pop(0)") #print("Moved: %s Reference: %s RMSD = %f" % mol1, mol2, RMSD) print("% s, % s,% 5.3f" % (mol1, mol2, RMSD)) # make the alignment OBVIOUS cmd.hide("everything") cmd.show("ribbon", sel1 + " or " + sel2) cmd.color("gray70", mol1 ) cmd.color("magenta", mol2 ) cmd.color("red", "visible") cmd.show("ribbon", "not visible") cmd.center("visible") cmd.orient() cmd.zoom("visible")
def dispmap(molecule1="NIL", molecule2="NIL", mindist=30.0, mindelta=15.0, resi1=str(0), resi2=str(0), atom='CA', listlength=5, showsticks='yes'): if molecule1 == "NIL": assert len(cmd.get_names()) != 0, "Did you forget to load a molecule? There are no objects in pymol." molecule1 = cmd.get_names()[0] if molecule2 == "NIL": assert len(cmd.get_names()) != 0, "Did you forget to load a molecule? There are no objects in pymol." molecule2 = cmd.get_names()[1] print("You passed in %s and %s" % (molecule1, molecule2)) # Open filenames filename = str(molecule1) + "-" + str(molecule2) + "-" + str(atom) + "-dist" backbonefilename = str(molecule1) + "-" + str(molecule2) + "-" + str(atom) + "backbone-dist.txt" outfile = open(filename + ".txt", "w") backboneoutfile = open(filename + "-backbone.txt", "w") gnuoutfile = open(filename + ".plt", "w") print(("I have opened matrix %s for you\n" % (filename + ".txt"))) # Sorting for interesting residues for Obj1 and Obj2. # Input is a string, and need to be sorted. resi1 = resi1.split('.') resi2 = resi2.split('.') resi1List = [] resi2List = [] for i in resi1: if '-' in i: tmp = i.split('-') resi1List.extend(list(range(int(tmp[0]), int(tmp[-1]) + 1))) if '-' not in i: resi1List.append(int(i)) for i in resi2: if '-' in i: tmp = i.split('-') resi2List.extend(list(range(int(tmp[0]), int(tmp[-1]) + 1))) if '-' not in i: resi2List.append(int(i)) resi1List.sort() resi2List.sort() # Only take the lines where atom is specified in input Object3 = molecule1 + " and name " + str(atom) Object4 = molecule2 + " and name " + str(atom) # Open 2 name lists # Append residue and atom name to the lists stored.OpenPDB = [] stored.ClosedPDB = [] cmd.iterate(Object3, "stored.OpenPDB.append((resi, name, resn))") cmd.iterate(Object4, "stored.ClosedPDB.append((resi, name, resn))") # Open 2 x,y,z position lists # Append atom position stored.OpenPos = [] stored.ClosedPos = [] cmd.iterate_state(1, selector.process(Object3), "stored.OpenPos.append((x,y,z))") cmd.iterate_state(1, selector.process(Object4), "stored.ClosedPos.append((x,y,z))") # Sometimes residues gets skipped in X-ray crys, because of low signal or sim. This leads to number conflict. # Make ordered lists according to residue number. Find largest residue number via -1 OpenOrderedPDB = [] ClosedOrderedPDB = [] OpenOrderedPos = [] ClosedOrderedPos = [] BackboneDisp = [] # First fill lists with zeros for i in range(int(stored.OpenPDB[-1][0]) + 1): OpenOrderedPDB.append([0, 0, 0]) for i in range(int(stored.ClosedPDB[-1][0]) + 1): ClosedOrderedPDB.append([0, 0, 0]) for i in range(int(stored.OpenPDB[-1][0]) + 1): OpenOrderedPos.append((0, 0, 0)) for i in range(int(stored.ClosedPDB[-1][0]) + 1): ClosedOrderedPos.append((0, 0, 0)) for i in range(int(stored.OpenPDB[-1][0]) + 1): BackboneDisp.append([i, 0, "NIL", atom]) # Fill in data the right places j = 0 for i in stored.OpenPDB: OpenOrderedPDB[int(i[0])] = [int(i[0]), i[1], i[2]] OpenOrderedPos[int(i[0])] = stored.OpenPos[j] j = j + 1 j = 0 for i in stored.ClosedPDB: ClosedOrderedPDB[int(i[0])] = [int(i[0]), i[1], i[2]] ClosedOrderedPos[int(i[0])] = stored.ClosedPos[j] j = j + 1 # Make a list with the missing residues MissingRes = [] for index, resi in enumerate(OpenOrderedPDB): if abs(OpenOrderedPDB[index][0] - ClosedOrderedPDB[index][0]) != 0: MissingRes.append(abs(OpenOrderedPDB[index][0] - ClosedOrderedPDB[index][0])) print("Following residues miss in one of the files, and are discarded for") print("further calculations") print(MissingRes) print("") # Make the data matrix CalcMatrix = create_nXn_matrix(len(OpenOrderedPos)) print(("Calculating a %s X %s distance Matrix" % (len(OpenOrderedPos), len(ClosedOrderedPos)))) # Make a list with 10 most negative/positive distances MaxNegDist = [] MaxPosDist = [] for i in range(int(listlength)): MaxNegDist.append([0, 0, 0, 0, 0, 0, 0]) MaxPosDist.append([0, 0, 0, 0, 0, 0, 0]) # Calculate distances for i in range(len(OpenOrderedPos)): for j in range(len(ClosedOrderedPos)): if OpenOrderedPos[i][0] != 0 and ClosedOrderedPos[j][0] != 0 and OpenOrderedPDB[i][0] not in MissingRes and ClosedOrderedPDB[j][0] not in MissingRes: distOpenOpen = distance(OpenOrderedPos, OpenOrderedPos, i, j) distClosedClosed = distance(ClosedOrderedPos, ClosedOrderedPos, i, j) distOpenClosed = distance(OpenOrderedPos, ClosedOrderedPos, i, j) DeltaDist = distOpenClosed - distOpenOpen if i == j: BackboneDisp[i] = [i, DeltaDist, OpenOrderedPDB[i][2], atom] # Test if distance is larger than threshold if distOpenOpen >= float(mindist) and distClosedClosed >= float(mindist) and abs(DeltaDist) >= float(mindelta): CalcMatrix[i][j] = str(round(DeltaDist, 0)) if DeltaDist < 0 and DeltaDist < MaxNegDist[-1][0] and (i in resi1List or resi1List[-1] == 0) and (j in resi2List or resi2List[-1] == 0): MaxNegDist[-1][0] = DeltaDist MaxNegDist[-1][1] = i MaxNegDist[-1][2] = j MaxNegDist[-1][3] = distOpenOpen MaxNegDist[-1][4] = distOpenClosed MaxNegDist[-1][5] = str(OpenOrderedPDB[i][2]) MaxNegDist[-1][6] = str(ClosedOrderedPDB[j][2]) MaxNegDist = sorted(MaxNegDist) if DeltaDist > 0 and DeltaDist > MaxPosDist[-1][0] and (i in resi1List or resi1List[-1] == 0) and (j in resi2List or resi2List[-1] == 0): MaxPosDist[-1][0] = DeltaDist MaxPosDist[-1][1] = i MaxPosDist[-1][2] = j MaxPosDist[-1][3] = distOpenOpen MaxPosDist[-1][4] = distOpenClosed MaxPosDist[-1][5] = str(OpenOrderedPDB[i][2]) MaxPosDist[-1][6] = str(ClosedOrderedPDB[j][2]) MaxPosDist = sorted(MaxPosDist, reverse=True) print("I made a datamatrix and backbone.txt file for you") print(("matrix: %s backbone: %s" % (filename + ".txt", filename + "-backbone.txt"))) print("I made a gnuplot file for you, to view the datamatrix and the backbone displacement") print(("filename: %s\n" % (filename + ".plt"))) # Print distance matrix line01 = "# Input 1: %s and Input 2: %s" % (molecule1, molecule2) line02 = "# Find for: %s with min. residue-residue dist: %s Angstrom" % (atom, mindist) line03 = "# Looking for min. displacement dist: %s Angstrom" % (mindelta) line04 = "# I give nr# suggestions: %s, and do I show sticks in pymol?: %s" % (listlength, showsticks) line05 = "# I look for suggestions in the range: ([0]=>means all residues)" line06 = "# for Input 1: %s and for Input 2: %s" % (resi1, resi2) line07 = "# Mutation info is BLOSUM62 log-odds likelihood score and PAM250 is probability in % for evolutionary distance" line08 = "###########################################################################################################" line09 = "# Max Negative and positive distances # Mutation info #" line10 = "# Obj.1 Obj.2 Delta Op-Op Cl-Cl # Obj.1 Obj.2 Delta Op-Op Cl-Cl # Res.1 Res.2 # Res.1 Res.2 #" line11 = "# Res.1 Res.2 -Dist Dist Dist # Res.1 Res.2 +Dist Dist Dist # B62/PAM250% # B62/PAM250% #" outfile.write(line01 + '\n') outfile.write(line02 + '\n') outfile.write(line03 + '\n') outfile.write(line04 + '\n') outfile.write(line05 + '\n') outfile.write(line06 + '\n') outfile.write(line07 + '\n') outfile.write(line08 + '\n') outfile.write(line09 + '\n') outfile.write(line08 + '\n') outfile.write(line10 + '\n') outfile.write(line11 + '\n') outfile.write(line08 + '\n') print(line01) print(line02) print(line03) print(line04) print(line05) print(line06) print(line07) print(line08) print(line09) print(line08) print(line10) print(line11) print(line08) for i in range(len(MaxNegDist)): text = "# %3s%3s %3s%3s %5s %5s %5s # %3s%3s %3s%3s %5s %5s %5s # %2s/%2s %2s/%2s # %2s/%2s %2s/%2s #" % (MaxNegDist[i][5], MaxNegDist[i][1], MaxNegDist[i][6], MaxNegDist[i][2], round(MaxNegDist[i][0], 1), round(MaxNegDist[i][3], 1), round(MaxNegDist[i][4], 1), MaxPosDist[i][5], MaxPosDist[i][1], MaxPosDist[i][6], MaxPosDist[i][2], round(MaxPosDist[i][0], 1), round(MaxPosDist[i][3], 1), round(MaxPosDist[i][4], 1), cysb62(shortaa(str(MaxNegDist[i][5]))), pam250(shortaa(str(MaxNegDist[i][5]))), cysb62(shortaa(str(MaxNegDist[i][6]))), pam250(shortaa(str(MaxNegDist[i][6]))), cysb62(shortaa(str(MaxPosDist[i][5]))), pam250(shortaa(str(MaxPosDist[i][5]))), cysb62(shortaa(str(MaxPosDist[i][6]))), pam250(shortaa(str(MaxPosDist[i][6])))) outfile.write(text + '\n') print(text) for i in range(len(CalcMatrix)): writing = "" for j in range(len(CalcMatrix)): if str(CalcMatrix[i][j]) == "0.0": writing = writing + " " + "?" else: writing = writing + " " + str(CalcMatrix[i][j]) # Add break line writing = writing + " " + "\n" outfile.write(writing) outfile.close() for i in range(len(BackboneDisp)): line = "%3s %4s %3s %3s" % (BackboneDisp[i][0], round(BackboneDisp[i][1], 1), BackboneDisp[i][2], BackboneDisp[i][3]) backboneoutfile.write(line + '\n') backboneoutfile.close() # Make gnuplot plot file gnuoutfile.write("reset" + "\n") gnuoutfile.write("cd " + "'" + os.getcwd() + "'" + "\n") gnuoutfile.write("\n") gnuoutfile.write("#Title hacks \\n is newline, and 0,1 is x,y offset adjustment" + "\n") gnuoutfile.write('set title "Protein ' + str(atom) + ' Displacement matrix map \\n ResRes min. ' + str(mindist) + ' Ang, ' + 'Delta min. ' + str(mindelta) + ' Ang"' + "\n") gnuoutfile.write("# x is column" + "\n") gnuoutfile.write("set xlabel 'Res nr. for " + str(molecule2) + "'" + "\n") gnuoutfile.write("# y is row" + "\n") gnuoutfile.write("set ylabel 'Res nr. for " + str(molecule1) + "'" + "\n") gnuoutfile.write("\n") gnuoutfile.write("#set xrange [300:550]; set yrange [0:400]" + "\n") gnuoutfile.write("#set xtics 50" + "\n") gnuoutfile.write("#set ytics 50" + "\n") gnuoutfile.write("#set mxtics 5" + "\n") gnuoutfile.write("#set mytics 5" + "\n") gnuoutfile.write("set size ratio 1" + "\n") gnuoutfile.write("unset key" + "\n") gnuoutfile.write("\n") gnuoutfile.write("set cbrange [-30:30]" + "\n") gnuoutfile.write("set palette defined (-30 'blue', 0 'white', 30 'red')" + "\n") gnuoutfile.write("set pm3d map" + "\n") gnuoutfile.write("\n") gnuoutfile.write("#set term postscript eps enhanced color" + "\n") gnuoutfile.write('#set output "' + filename + '.eps"' + "\n") gnuoutfile.write("set term png" + "\n") gnuoutfile.write('set output "' + filename + '.png"' + "\n") gnuoutfile.write("splot '" + str(filename + ".txt") + "' matrix" + "\n") gnuoutfile.write("\n") gnuoutfile.write("#For the backbone displacement" + "\n") gnuoutfile.write("\n") gnuoutfile.write('set title "Protein ' + str(atom) + ' Backbone displacement"' + "\n") gnuoutfile.write("set xlabel 'Residue number'" + "\n") gnuoutfile.write("set ylabel '" + str(atom) + " displacement (Ang)'" + "\n") gnuoutfile.write("\n") gnuoutfile.write("#set xrange [0:550]; set yrange [0:40]" + "\n") gnuoutfile.write("#set xtics 50" + "\n") gnuoutfile.write("#set ytics 10" + "\n") gnuoutfile.write("#set mxtics 5" + "\n") gnuoutfile.write("#set mytics 5" + "\n") gnuoutfile.write("set size ratio 0.75" + "\n") gnuoutfile.write("unset key" + "\n") gnuoutfile.write("\n") gnuoutfile.write("#set term postscript eps enhanced color" + "\n") gnuoutfile.write('#set output "' + filename + '-backbone.eps"' + "\n") gnuoutfile.write("set term png" + "\n") gnuoutfile.write('set output "' + filename + '-backbone.png"' + "\n") gnuoutfile.write("plot '" + str(filename + "-backbone.txt") + "' using 1:2 title 'Backbone displacement' with lines" + "\n") gnuoutfile.close() # Create stick residue objects for i in range(len(MaxNegDist)): name = str(i) + "_" + str(round(MaxNegDist[i][0], 1)) + "_" + shortaa(str(MaxNegDist[i][5])) + str(MaxNegDist[i][1]) + shortaa(str(MaxNegDist[i][6])) + str(MaxNegDist[i][2]) selection = str(molecule1) + " and resi " + str(MaxNegDist[i][1]) + "+" + str(MaxNegDist[i][2]) + " or " + str(molecule2) + " and resi " + str(MaxNegDist[i][2]) # print selection cmd.create(name, selection) if showsticks == 'yes' or showsticks == 'y': cmd.show("sticks", name) for i in range(len(MaxPosDist)): name = str(i) + "_" + str(round(MaxPosDist[i][0], 1)) + "_" + shortaa(str(MaxPosDist[i][5])) + str(MaxPosDist[i][1]) + shortaa(str(MaxPosDist[i][6])) + str(MaxPosDist[i][2]) selection = str(molecule1) + " and resi " + str(MaxPosDist[i][1]) + "+" + str(MaxPosDist[i][2]) + " or " + str(molecule2) + " and resi " + str(MaxPosDist[i][2]) # print selection cmd.create(name, selection) if showsticks == 'yes' or showsticks == 'y': cmd.show("sticks", name)
def save_pdb(filename, selection='(all)', state=-1, symm=1, ss=1, aniso=0, seqres=0, quiet=1, *, _self=cmd): ''' DESCRIPTION Save the coordinates of a selection as pdb including the secondary structure information and, if possible, the unit cell. The latter requires the selction of a single object USAGE save_pdb filename, selection [, state [, symm [, ss [, aniso ]]]] ARGUMENTS filename = string: file path to be written selection = string: atoms to save {default: (all)} Note: to include the unit cell information you need to select a single object state = integer: state to save {default: -1 (current state)} symm = 0 or 1: save symmetry info if possible {default: 1} ss = 0 or 1: save secondary structure info {default: 1} aniso = 0 or 1: save ANISO records {default: 0} SEE ALSO save ''' _assert_package_import() selection = selector.process(selection) state, quiet = int(state), int(quiet) symm, ss = int(symm), int(ss) filename = cmd.exp_path(filename) f = open(filename, 'w') print('REMARK 200 Generated with PyMOL and psico'.ljust(80), file=f) # Write sequence if int(seqres): f.write(get_pdb_seqres(selection)) # Write the CRYST1 line if possible if symm: try: obj1 = _self.get_object_list(selection)[0] sym = _self.get_symmetry(obj1) if len(sym) != 7: raise f.write("CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-10s%4d\n" % tuple(sym + [1])) if not quiet: print(' Info: Wrote unit cell and space group info') except: if not quiet: print(' Info: No crystal information') # Write secondary structure if ss: try: sss = get_pdb_sss(selection, state, quiet) if not sss: raise f.write(sss) if not quiet: print(' Info: Wrote secondary structure info') except: if not quiet: print(' Info: No secondary structure information') # Write coordinates of selection pdbstr = _self.get_pdbstr(selection, state) f.write(pdbstr) f.close() if not quiet: print('Wrote PDB to \'' + filename + '\'')