Example #1
0
    def testSaveRef(self, format):
        # for rms_cur (not all formats save all identifiers)
        m = -1
        cmd.set('retain_order')

        cmd.fragment('ala', 'm1')
        cmd.copy('m2', 'm1')
        cmd.copy('m3', 'm1')

        cmd.rotate('y', 90, 'm2')
        cmd.align('m3', 'm2')

        # with ref=m3
        with testing.mktemp('.' + format) as filename:
            cmd.save(filename, 'm2', ref='m3')
            cmd.load(filename, 'm4')
            self.assertAlmostEqual(cmd.rms_cur('m4', 'm1', matchmaker=m), 0.00, delta=1e-2)
            self.assertAlmostEqual(cmd.rms_cur('m4', 'm2', matchmaker=m), 1.87, delta=1e-2)

        # without ref
        with testing.mktemp('.' + format) as filename:
            cmd.save(filename, 'm2')
            cmd.load(filename, 'm5')
            self.assertAlmostEqual(cmd.rms_cur('m5', 'm2', matchmaker=m), 0.00, delta=1e-2)
            self.assertAlmostEqual(cmd.rms_cur('m5', 'm1', matchmaker=m), 1.87, delta=1e-2)
Example #2
0
    def testSaveRef(self, format):
        # for rms_cur (not all formats save all identifiers)
        m = -1
        cmd.set('retain_order')

        cmd.fragment('ala', 'm1')
        cmd.copy('m2', 'm1')
        cmd.copy('m3', 'm1')

        cmd.rotate('y', 90, 'm2')
        cmd.align('m3', 'm2')

        # with ref=m3
        with testing.mktemp('.' + format) as filename:
            cmd.save(filename, 'm2', ref='m3')
            cmd.load(filename, 'm4')
            self.assertAlmostEqual(cmd.rms_cur('m4', 'm1', matchmaker=m), 0.00, delta=1e-2)
            self.assertAlmostEqual(cmd.rms_cur('m4', 'm2', matchmaker=m), 1.87, delta=1e-2)

        # without ref
        with testing.mktemp('.' + format) as filename:
            cmd.save(filename, 'm2')
            cmd.load(filename, 'm5')
            self.assertAlmostEqual(cmd.rms_cur('m5', 'm2', matchmaker=m), 0.00, delta=1e-2)
            self.assertAlmostEqual(cmd.rms_cur('m5', 'm1', matchmaker=m), 1.87, delta=1e-2)
Example #3
0
    def testSaveState(self, format, pymol_version):
        if pymol_version > testing.PYMOL_VERSION[1]:
            self.skipTest("version %f" % (pymol_version))

        # for rms_cur (not all formats save all identifiers)
        m = -1
        cmd.set('retain_order')

        # create a multistate object
        cmd.fragment('ala', 'm1')
        cmd.create('m1', 'm1', 1, 2)
        cmd.create('m1', 'm1', 1, 3)
        cmd.translate([5, 0, 0], 'm1', state=2)
        cmd.translate([0, 5, 0], 'm1', state=3)
        n_states = cmd.count_states('m1')

        with testing.mktemp('.' + format) as filename:
            if format == 'mae' and not pymol.invocation.options.incentive_product:
                format = 'cms'

            # explicit
            for state in range(1, n_states + 1):
                cmd.delete('m2')
                cmd.save(filename, 'm1', state=state)
                cmd.load(filename, 'm2', format=format)
                rms = cmd.rms_cur('m1', 'm2', state, 1, matchmaker=m)
                self.assertAlmostEqual(rms, 0.00, delta=1e-2)

            # current state
            for state in range(1, n_states + 1):
                cmd.frame(state)
                cmd.delete('m2')
                cmd.save(filename, 'm1')
                cmd.load(filename, 'm2', 1, format=format)
                rms = cmd.rms_cur('m1', 'm2', state, 1, matchmaker=m)
                self.assertAlmostEqual(rms, 0.00, delta=1e-2)

            if format in ('mol', 'cms'):
                # no multi support
                return

            # all states
            cmd.delete('m2')
            cmd.save(filename, 'm1', state=0)
            cmd.load(filename, 'm2', 1, discrete=1, multiplex=0)
            self.assertEqual(cmd.count_states('m2'), n_states)
            for state in range(1, n_states + 1):
                rms = cmd.rms_cur('m1',
                                  'm2 and state %d' % state,
                                  state,
                                  state,
                                  matchmaker=m)
                self.assertAlmostEqual(rms, 0.00, delta=1e-2)
Example #4
0
    def testSaveState(self, format, pymol_version):
        if pymol_version > testing.PYMOL_VERSION[1]:
            self.skipTest("version %f" % (pymol_version))

        # for rms_cur (not all formats save all identifiers)
        m = -1
        cmd.set('retain_order')

        # create a multistate object
        cmd.fragment('ala', 'm1')
        cmd.create('m1', 'm1', 1, 2)
        cmd.create('m1', 'm1', 1, 3)
        cmd.translate([5, 0, 0], 'm1', state=2)
        cmd.translate([0, 5, 0], 'm1', state=3)
        n_states = cmd.count_states('m1')

        with testing.mktemp('.' + format) as filename:
            if format == 'mae' and not pymol.invocation.options.incentive_product:
                format = 'cms'

            # explicit
            for state in range(1, n_states + 1):
                cmd.delete('m2')
                cmd.save(filename, 'm1', state=state)
                cmd.load(filename, 'm2', format=format)
                rms = cmd.rms_cur('m1', 'm2', state, 1, matchmaker=m)
                self.assertAlmostEqual(rms, 0.00, delta=1e-2)

            # current state
            for state in range(1, n_states + 1):
                cmd.frame(state)
                cmd.delete('m2')
                cmd.save(filename, 'm1')
                cmd.load(filename, 'm2', 1, format=format)
                rms = cmd.rms_cur('m1', 'm2', state, 1, matchmaker=m)
                self.assertAlmostEqual(rms, 0.00, delta=1e-2)

            if format in ('mol', 'cms'):
                # no multi support
                return

            # all states
            cmd.delete('m2')
            cmd.save(filename, 'm1', state=0)
            cmd.load(filename, 'm2', 1, discrete=1, multiplex=0)
            self.assertEqual(cmd.count_states('m2'), n_states)
            for state in range(1, n_states + 1):
                rms = cmd.rms_cur('m1', 'm2 and state %d' % state,
                        state, state, matchmaker=m)
                self.assertAlmostEqual(rms, 0.00, delta=1e-2)
Example #5
0
def project(model, resi1_str, resi2_str, resi3_str, \
		symQ, syms_for_use, syms_for_del, sym_d=3, \
		segis_for_save='all', segis_for_del=''):
	all_segis = prepare(model, resi1_str, resi2_str, resi3_str, segis_for_save, segis_for_del)
	
	new_model, segi_count = make_segis(model, resi1, resi2, resi3, all_segis)
	cmd.alter(new_model, \
			'segi = new_segi(segi, sorted(all_segis))', \
			space={'new_segi': new_segi, 'all_segis': all_segis, 'sorted': sorted})

	if symQ:
		cmd.symexp('{}_sym'.format(model), \
				'{}_save'.format(model), \
				'({}_save)'.format(model), \
				sym_d)

		for i in range(len(syms_for_use)):
			cmd.alter('{}_sym{}'.format(model, syms_for_use[i]), 'segi=segi+"mod{}"'.format(i + 1))

		merge(model, ['{}_save'.format(model)] + ['{}_sym{}'.format(model, s) for s in syms_for_use])
	
	else: merge(model, ['{}_save'.format(model)])
	
	for sym in syms_for_del:
		cmd.delete('{}_sym{}'.format(model, sym))
	
	cmd.alter('all', 'chain="A"')

	cmd.save('{}_calc.cif'.format(model), '{}_calc'.format(model))
	print(cmd.rms_cur('{}_calc'.format(model), '{}_sym'.format(model)))
Example #6
0
 def testFit(self):
     cmd.fragment("gly", "m1")
     cmd.create("m2", "m1")
     rms = cmd.fit("m1", "m2")
     self.assertEqual(rms, 0.0)
     rms = cmd.rms("m1", "m2")
     self.assertEqual(rms, 0.0)
     rms = cmd.rms_cur("m1", "m2")
     self.assertEqual(rms, 0.0)
Example #7
0
 def testFit(self):
     cmd.fragment("gly", "m1")
     cmd.create("m2", "m1")
     rms = cmd.fit("m1", "m2")
     self.assertEqual(rms, 0.0)
     rms = cmd.rms("m1", "m2")
     self.assertEqual(rms, 0.0)
     rms = cmd.rms_cur("m1", "m2")
     self.assertEqual(rms, 0.0)
Example #8
0
def RMS_Calc(Ref_obj, OList):
    RMSList = [0] * len(OList)
    count = 0

    for obj in OList:
        rms_val = cmd.rms_cur(Ref_obj, obj)
        RMSList[count] = str(rms_val)
        count += 1

    # RMS values of ref_obj compared to all other objects
    return RMSList
Example #9
0
def rotation(rot_str, i, num):
    global UNK
    #   cmd.do('set retain_order,1')
    cmd.do(rot_str)

    # Save the rotation and the ligand
    cmd.save(i + '_' + str(num) + 'rot.pdb', i)

    # Save just the ligand
    cmd.save(i + '_UNK' + str(num) + '.pdb', i + ' and resn ' + UNK)
    #  print("UNK Rotation: " + i + '_UNK' + str(num) + '.pdb')

    # Calculates the RMS between the UNK and the obj1 UNK reference
    rms_val = cmd.rms_cur('obj1 and resn ' + UNK, i + ' and resn ' + UNK)

    return rms_val
Example #10
0
def rmsdCA(refMol, tgtMol, puttyview="Y", bfactorca="CA"):
    """
    Written up by Yufeng Tong 2020-01-29
    Inspired by rmsdByRes Zhenting Gao on 7/28/2016
    and by loadBfacts from PyMOLWiki

    USAGE

      rmsdCA refMol, tgtMol, puttyview="Y", bfactorca="CA"

      Calculate the RMSD of Calpha and backbone for each residue pairs from
        two chains of proteins with same residue numbers and close
        conformations for the two structures. The two proteins can be 
        mutants. Only residue numbers but not identity are matched.
      It will generate a putty representation of the target molecule by 
        default. You can specify whether to use the RMSD of Calpha or Backbone
        for the coloring of the putty display.
      I think it's not neccessary to calculate RMSD of the whole residue.
      A CSV file is saved of the calculated RMSD of Calpha and backbone atoms.
      A PDB file is saved with BFactor replaced by the RMSD.

    Workflow
      Read reference and target pdb files
      1. Select two objects for comparison
      2. Align two structures
      3. rmsdByRes refMol, tgtMol
    
    TODO:
      1. Save output files to the folder pdb files are loaded.
      2. Handle missing residues.
    """

    # Create temporary objects, exclude alternative conformation B
    cmd.delete("tgt_gzt rmsdPuttyScale")
    cmd.create("ref_gzt", refMol + " and polymer and not alt B")
    cmd.alter("ref_gzt", "chain='A'")
    cmd.alter("ref_gzt", "segi=''")
    cmd.create("tgt_gzt", tgtMol + " and polymer and not alt B")
    cmd.alter("tgt_gzt", "chain='A'")
    cmd.alter("tgt_gzt", "segi=''")
    cmd.alter("tgt_gzt", "b=-1.0")

    # parameters
    outputText = ""
    csvHeadline = ("refMol,refRes,refResID,target,residueName,residueId,"
                   "rmsdResCa,rmsdResBackbone\n")
    outputFile = 'rmsd%s_%s.csv' % (bfactorca, tgtMol)
    outputpdb = 'rmsdBFactor%s_%s.pdb' % (bfactorca, tgtMol)
    bfactors = []

    # select alpha carbon of selected residues in reference structure
    calpha_ref = cmd.get_model("ref_gzt and name CA")
    calpha_tgt = cmd.get_model("tgt_gzt and name CA")

    # build an index dict of {resi : index}
    ref_dict = {int(v.resi): k for k, v in enumerate(calpha_ref.atom)}
    tgt_dict = {int(v.resi): k for k, v in enumerate(calpha_tgt.atom)}

    # loop through residues in the target molecule and calculate per residue RMSDs.
    for k in sorted(ref_dict):
        if k in tgt_dict:
            ref_atom = calpha_ref.atom[ref_dict[k]]
            tgt_atom = calpha_tgt.atom[tgt_dict[k]]
            rmsdResCA = cmd.rms_cur(
                "ref_gzt and n. ca and i. " + ref_atom.resi,
                "tgt_gzt and n. ca and i. " + tgt_atom.resi,
                matchmaker=-1)
            rmsdResBb = cmd.rms_cur(
                "ref_gzt and n. ca+n+c+o and i. " + ref_atom.resi,
                "tgt_gzt and n. ca+n+c+o and i. " + tgt_atom.resi,
                matchmaker=-1)
            if bfactorca == "bb":
                bfactor = rmsdResBb
            else:
                bfactor = rmsdResCA
            cmd.alter("%s and i. %s" % ("tgt_gzt", tgt_atom.resi),
                      "b=%s" % bfactor)
            bfactors.append(bfactor)

            outputText += "%s,%s,%s,%s,%s,%s,%.3f,%.3f\n" % (
                refMol, ref_atom.resn, ref_atom.resi, tgtMol, tgt_atom.resn,
                tgt_atom.resi, rmsdResCA, rmsdResBb)

    print(outputText)
    max_b, min_b = max(bfactors), min(bfactors)
    print("RMSD: max:%.3f; min:%.3f" % (max_b, min_b))

    if puttyview == "Y":
        cmd.show_as("cartoon", "tgt_gzt")
        cmd.cartoon("putty", "tgt_gzt")
        cmd.set("cartoon_putty_scale_min", min_b, "tgt_gzt")
        cmd.set("cartoon_putty_scale_max", max_b, "tgt_gzt")
        cmd.set("cartoon_putty_transform", 0, "tgt_gzt")
        cmd.spectrum("b", "rainbow", "tgt_gzt and n. CA")
        cmd.ramp_new("rmsdPuttyScale", "tgt_gzt", [min_b, max_b], "rainbow")
        cmd.recolor()

# Destroy temporary objects
    cmd.delete("ref_gzt")

    # Save data into csv and pdb
    f = open(outputFile, 'w+')
    f.write(csvHeadline)
    f.write(outputText)
    f.close()
    cmd.save(outputpdb, "tgt_gzt")

    print("Results are saved in %s and %s " % (outputFile, outputpdb))
Example #11
0
def tmalign(mobile,
            target,
            args='',
            exe='TMalign',
            ter=0,
            transform=1,
            object=None,
            quiet=0):
    """
DESCRIPTION

TMalign wrapper

Reference: Y. Zhang and J. Skolnick, Nucl. Acids Res. 2005 33, 2302-9
http://zhanglab.ccmb.med.umich.edu/TM-align/

USAGE

tmalign mobile, target [, args [, exe ]]

ARGUMENTS

mobile, target = string: atom selections

args = string: Extra arguments like -d0 5 -L 100

exe = string: Path to TMalign executable {default: TMalign}

ter = 0/1: If ter=0, then ignore chain breaks because TMalign will stop
at first TER record {default: 0}

SEE ALSO

tmscore, mmalign
    """
    import subprocess
    import tempfile
    import os
    import re

    ter, quiet = int(ter), int(quiet)

    mobile_filename = tempfile.mktemp('.pdb', 'mobile')
    target_filename = tempfile.mktemp('.pdb', 'target')
    matrix_filename = tempfile.mktemp('.txt', 'matrix')
    mobile_ca_sele = '(%s) and (not hetatm) and name CA and alt +A' % mobile
    target_ca_sele = '(%s) and (not hetatm) and name CA and alt +A' % target

    if ter:
        save = cmd.save
    else:
        save = save_pdb_without_ter
    save(mobile_filename, mobile_ca_sele)
    save(target_filename, target_ca_sele)

    exe = cmd.exp_path(exe)
    args = [exe, mobile_filename, target_filename, '-m', matrix_filename
            ] + args.split()

    try:
        process = subprocess.Popen(args, stdout=subprocess.PIPE)
        lines = process.stdout.readlines()
    except OSError:
        print('Cannot execute "%s", please provide full path to TMscore or '
              'TMalign executable' % exe)
        raise CmdException
    finally:
        os.remove(mobile_filename)
        os.remove(target_filename)

    # TMalign >= 2012/04/17
    if os.path.exists(matrix_filename):
        lines += open(matrix_filename).readlines()
        os.remove(matrix_filename)

    r = None
    re_score = re.compile(r'TM-score\s*=\s*(\d*\.\d*)')
    rowcount = 0
    matrix = []
    line_it = iter(lines)
    alignment = []
    for line in line_it:
        if 4 >= rowcount > 0:
            if rowcount >= 2:
                a = list(map(float, line.split()))
                matrix.extend(a[2:5])
                matrix.append(a[1])
            rowcount += 1
        elif line.lower().startswith(' -------- rotation matrix'):
            rowcount = 1
        elif line.startswith('(":" denotes'):
            alignment = []
            for i in range(3):
                alignment.append(line_it.next().rstrip())
        else:
            match = re_score.search(line)
            if match is not None:
                r = float(match.group(1))
        if not quiet:
            print(line.rstrip())

    if not quiet:
        for i in range(0, len(alignment[0]) - 1, 78):
            for line in alignment:
                print(line[i:i + 78])
            print('')

    assert len(matrix) == 3 * 4
    matrix.extend([0, 0, 0, 1])

    if int(transform):
        cmd.transform_selection('byobject (%s)' % mobile, matrix, homogenous=1)

    # alignment object
    if object is not None:
        mobile_idx, target_idx = [], []
        space = {'mobile_idx': mobile_idx, 'target_idx': target_idx}
        cmd.iterate(mobile_ca_sele,
                    'mobile_idx.append("%s`%d" % (model, index))',
                    space=space)
        cmd.iterate(target_ca_sele,
                    'target_idx.append("%s`%d" % (model, index))',
                    space=space)
        for i, aa in enumerate(alignment[0]):
            if aa == '-':
                mobile_idx.insert(i, None)
        for i, aa in enumerate(alignment[2]):
            if aa == '-':
                target_idx.insert(i, None)
        if len(mobile_idx) == len(target_idx) == len(alignment[2]):
            cmd.rms_cur(' '.join(idx
                                 for (idx, m) in zip(mobile_idx, alignment[1])
                                 if m in ':.'),
                        ' '.join(idx
                                 for (idx, m) in zip(target_idx, alignment[1])
                                 if m in ':.'),
                        matchmaker=4,
                        object=object)
        else:
            print('Could not load alignment object')

    if not quiet and r is not None:
        print('Found in output TM-score = %.4f' % r)

    return r
Example #12
0
    cmd.alter_state(a, "trg", "x=x+random()/2")
    cmd.alter_state(a, "trg", "y=y+random()/2")
    cmd.alter_state(a, "trg", "z=z+random()/2", quiet=0)


cmd.frame(1)
print "%8.3f" % cmd.fit("ref", "trg")

# 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))
Example #13
0
def run_comparison(references=None, conformers=None):

    references_file = args.references
    conformers_file = args.conformers

    print(' -- -- -- -- -- LOADING REFERENCES IN {} -- -- -- -- -- '.format(
        references_file))
    print(' -- -- -- -- -- LOADING CONFORMERS IN {} -- -- -- -- -- '.format(
        conformers_file))

    lowest_rmsd = []
    #out_macro_refs		= Chem.SDWriter('Macrocycles_references.sdf')
    out_RMSD = Chem.SDWriter('Aligment_RMSD.sdf')
    #out_macro_confs		= Chem.SDWriter('Macrocycles_conformers.sdf')

    print(' -- -- -- -- -- STARTING ANALYSIS -- -- -- -- -- ')
    ref_index = 1

    for ref in Chem.SDMolSupplier(references_file):

        print(ref_index, ')', ref.GetProp('_Name').split('_')[0])

        macrocycle_atoms = []
        all_cycles = Chem.GetSymmSSSR(ref)
        cycles_size = [i for i in all_cycles if len(i) >= 8]
        for element in cycles_size:
            macrocycle_atoms += list(element)
        all_atoms = [i.GetIdx() for i in ref.GetAtoms()]
        atoms_to_remove = (list(set(all_atoms) - set(macrocycle_atoms)))

        macrocycle = Chem.RWMol(ref)
        for i in sorted(atoms_to_remove, reverse=True):
            macrocycle.RemoveAtom(i)

        m_ref = macrocycle.GetMol()
        m_ref.UpdatePropertyCache()

        macrocycle_atoms = sorted(list(set(macrocycle_atoms)))
        print('Initial Num Atoms:', len(all_atoms))
        print('Macrocycle length:', len(macrocycle_atoms))

        m_ref_smiles = Chem.MolFragmentToSmiles(ref,
                                                macrocycle_atoms,
                                                kekuleSmiles=True)
        m_ref_smiles = Chem.MolFromSmiles(m_ref_smiles, sanitize=False)

        ref_index = ref_index + 1
        mol_index = 0
        table = pd.DataFrame()

        for mol in Chem.SDMolSupplier(conformers_file):
            if ref.GetProp('_Name').split('_')[0] == mol.GetProp(
                    '_Name').split('_')[0]:

                table.loc[mol_index, 'Conformer'] = [mol.GetProp('_Name')]

                ref_atoms = ref.GetSubstructMatch(m_ref_smiles)
                mol_atoms = mol.GetSubstructMatch(m_ref_smiles)
                amap = zip(mol_atoms, ref_atoms)
                rms_macrocycle = AllChem.GetBestRMS(mol, ref, map=[list(amap)])

                mol.SetProp('RMSD_macrocycle', str(rms_macrocycle))
                table.loc[mol_index, 'RMSD_macrocycle'] = [rms_macrocycle]

                macrocycle_atoms = []
                all_cycles = Chem.GetSymmSSSR(mol)
                cycles_size = [i for i in all_cycles if len(i) >= 8]
                for element in cycles_size:
                    macrocycle_atoms += list(element)
                all_atoms = [i.GetIdx() for i in mol.GetAtoms()]
                atoms_to_remove = (
                    list(set(all_atoms) - set(macrocycle_atoms)))

                macrocycle = Chem.RWMol(mol)
                for i in sorted(atoms_to_remove, reverse=True):
                    macrocycle.RemoveAtom(i)

                m_mol = macrocycle.GetMol()
                m_mol.UpdatePropertyCache()

                #m_mol=Chem.MolFragmentToSmiles(mol,macrocycle_atoms,kekuleSmiles=True)
                #m_mol=Chem.MolFromSmiles(m_mol,sanitize=False)

                radious_macro = Descriptors3D.RadiusOfGyration(m_mol)
                table.loc[mol_index, 'RoG_macrocycle'] = radious_macro

                tt_macro = rdMolDescriptors.GetTopologicalTorsionFingerprint(
                    m_mol)
                table.loc[mol_index,
                          'TF_macrocycle'] = [tt_macro.GetTotalVal()]

                r_list = Chem.TorsionFingerprints.CalculateTorsionLists(m_ref)
                r_angles = Chem.TorsionFingerprints.CalculateTorsionAngles(
                    m_ref, r_list[0], r_list[1])
                c_list = Chem.TorsionFingerprints.CalculateTorsionLists(m_mol)
                c_angles = Chem.TorsionFingerprints.CalculateTorsionAngles(
                    m_mol, c_list[0], c_list[1])

                if len(r_angles) == len(c_angles):
                    torsion_macro = Chem.TorsionFingerprints.CalculateTFD(
                        r_angles, c_angles)
                    table.loc[mol_index, 'TFD_macrocycle'] = [torsion_macro]
                else:
                    table.loc[mol_index, 'TFD_macrocycle'] = ['NA']

                cmd.read_molstr(Chem.MolToMolBlock(ref), 'ref')
                cmd.read_molstr(Chem.MolToMolBlock(mol), 'mol')
                rmsd = cmd.rms_cur('ref', 'mol')
                cmd.deselect()
                cmd.delete('all')

                mol.SetProp('RMSD_heavy_atoms', str(rmsd))
                table.loc[mol_index, 'RMSD_heavy_atoms'] = [rmsd]

                out_RMSD.write(mol)

                radious = Descriptors3D.RadiusOfGyration(mol)
                table.loc[mol_index, 'RoG_heavy_atoms'] = radious

                tt = rdMolDescriptors.GetTopologicalTorsionFingerprint(mol)
                table.loc[mol_index, 'TF_heavy_atoms'] = [tt.GetTotalVal()]

                r_list = Chem.TorsionFingerprints.CalculateTorsionLists(ref)
                r_angles = Chem.TorsionFingerprints.CalculateTorsionAngles(
                    ref, r_list[0], r_list[1])
                c_list = Chem.TorsionFingerprints.CalculateTorsionLists(mol)
                c_angles = Chem.TorsionFingerprints.CalculateTorsionAngles(
                    mol, c_list[0], c_list[1])

                if len(r_angles) == len(c_angles):
                    torsion = Chem.TorsionFingerprints.CalculateTFD(
                        r_angles, c_angles)
                    table.loc[mol_index, 'TFD_heavy_atoms'] = [torsion]
                else:
                    table.loc[mol_index, 'TFD_heavy_atoms'] = ['NA']

                mol_index = mol_index + 1

        if len(table.index) > 0:
            sort = table.sort_values('RMSD_macrocycle', ascending=True)
            sort = sort.reset_index(drop=True)
            sort.to_csv(ref.GetProp('_Name') + '.csv')

            sort['Nconf'] = len(sort.index)
            print('Number of conformers analyzed:', len(sort.index))
            print('data in file:', ref.GetProp('_Name') + '.csv')
            print('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \n')

            sort['Span_Rog_macrocycle'] = float(
                max(sort['RoG_macrocycle']) - min(sort['RoG_macrocycle']))
            sort['Span_Rog_heavy_atoms'] = float(
                max(sort['RoG_heavy_atoms']) - min(sort['RoG_heavy_atoms']))

            lowest_rmsd.append(sort.loc[0])
        else:
            print('No reference or conformers found in input files for {}'.
                  format(ref.GetProp('_Name')))
            print(' ************************************ \n')

    #out_macro_refs.close()
    out_RMSD.close()
    #out_macro_confs.close()

    print('SAVING DATA OF LOWEST RMSD OF CONFORMERS')

    summary = pd.DataFrame(lowest_rmsd)
    summary = summary.reset_index(drop=True)
    summary.to_csv('Lowest_RMSD_Data.csv')

    print('Lowest RMSD Data in file: Lowest_RMSD_Data.csv')
    print('***************************************************\n')
    print('Structures in files: Alignment_RMSD.sdf')
    print('***************************************************\n')
    print(
        'CALCULATION OF {} OUT OF {} REFERENCES DONE, FILES SAVED. THANK YOU FOR USING THIS SCRIPT \n'
        .format(len(summary.index), len(Chem.SDMolSupplier(references_file))))
Example #14
0
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')
Example #15
0
def tmalign(mobile,
            target,
            mobile_state=1,
            target_state=1,
            args='',
            exe='TMalign',
            ter=0,
            transform=1,
            object=None,
            quiet=0):
    '''
DESCRIPTION

    TMalign wrapper. You may also use this as a TMscore or MMalign wrapper
    if you privide the corresponding executable with the "exe" argument.

    Reference: Y. Zhang and J. Skolnick, Nucl. Acids Res. 2005 33, 2302-9
    http://zhanglab.ccmb.med.umich.edu/TM-align/

ARGUMENTS

    mobile, target = string: atom selections

    mobile_state, target_state = int: object states {default: 1}

    args = string: Extra arguments like -d0 5 -L 100

    exe = string: Path to TMalign (or TMscore, MMalign) executable
    {default: TMalign}

    ter = 0/1: If ter=0, then ignore chain breaks because TMalign will stop
    at first TER record {default: 0}
    '''
    import subprocess, tempfile, os, re
    from .exporting import save_pdb_without_ter

    ter, quiet = int(ter), int(quiet)

    mobile_filename = tempfile.mktemp('.pdb', 'mobile')
    target_filename = tempfile.mktemp('.pdb', 'target')
    matrix_filename = tempfile.mktemp('.txt', 'matrix')
    mobile_ca_sele = '(%s) and (not hetatm) and name CA and alt +A' % (mobile)
    target_ca_sele = '(%s) and (not hetatm) and name CA and alt +A' % (target)

    if ter:
        save = cmd.save
    else:
        save = save_pdb_without_ter
    save(mobile_filename, mobile_ca_sele, state=mobile_state)
    save(target_filename, target_ca_sele, state=target_state)

    exe = cmd.exp_path(exe)
    args = [exe, mobile_filename, target_filename, '-m', matrix_filename
            ] + args.split()

    try:
        process = subprocess.Popen(args, stdout=subprocess.PIPE)
        lines = process.stdout.readlines()
    except OSError:
        print(
            'Cannot execute "%s", please provide full path to TMscore or TMalign executable'
            % (exe))
        raise CmdException
    finally:
        os.remove(mobile_filename)
        os.remove(target_filename)

    # TMalign >= 2012/04/17
    if os.path.exists(matrix_filename):
        lines += open(matrix_filename).readlines()
        os.remove(matrix_filename)

    r = None
    re_score = re.compile(r'TM-score\s*=\s*(\d*\.\d*)')
    rowcount = 0
    matrix = []
    line_it = iter(lines)
    headercheck = False
    alignment = []
    for line in line_it:
        if 4 >= rowcount > 0:
            if rowcount >= 2:
                a = list(map(float, line.split()))
                matrix.extend(a[2:5])
                matrix.append(a[1])
            rowcount += 1
        elif not headercheck and line.startswith(' * '):
            a = line.split(None, 2)
            if len(a) == 3:
                headercheck = a[1]
        elif line.lower().startswith(' -------- rotation matrix'):
            rowcount = 1
        elif line.startswith('(":" denotes'):
            alignment = [line_it.next().rstrip() for i in range(3)]
        else:
            match = re_score.search(line)
            if match is not None:
                r = float(match.group(1))
        if not quiet:
            print(line.rstrip())

    if not quiet:
        for i in range(0, len(alignment[0]) - 1, 78):
            for line in alignment:
                print(line[i:i + 78])
            print('')

    assert len(matrix) == 3 * 4
    matrix.extend([0, 0, 0, 1])

    if int(transform):
        for model in cmd.get_object_list('(' + mobile + ')'):
            cmd.transform_object(model, matrix, state=0, homogenous=1)

    # alignment object
    if object is not None:
        mobile_idx, target_idx = [], []
        space = {'mobile_idx': mobile_idx, 'target_idx': target_idx}
        cmd.iterate(mobile_ca_sele,
                    'mobile_idx.append("%s`%d" % (model, index))',
                    space=space)
        cmd.iterate(target_ca_sele,
                    'target_idx.append("%s`%d" % (model, index))',
                    space=space)
        for i, aa in enumerate(alignment[0]):
            if aa == '-':
                mobile_idx.insert(i, None)
        for i, aa in enumerate(alignment[2]):
            if aa == '-':
                target_idx.insert(i, None)
        if (len(mobile_idx) == len(target_idx) == len(alignment[2])):
            cmd.rms_cur(' '.join(idx
                                 for (idx, m) in zip(mobile_idx, alignment[1])
                                 if m in ':.'),
                        ' '.join(idx
                                 for (idx, m) in zip(target_idx, alignment[1])
                                 if m in ':.'),
                        cycles=0,
                        matchmaker=4,
                        object=object)
        else:
            print('Could not load alignment object')

    if not quiet:
        if headercheck:
            print('Finished Program:', headercheck)
        if r is not None:
            print('Found in output TM-score = %.4f' % (r))

    return r
Example #16
0
def tmalign(mobile, target, args='', exe='TMalign', ter=0, transform=1, object=None, quiet=0):
	'''
DESCRIPTION

    TMalign wrapper

    Reference: Y. Zhang and J. Skolnick, Nucl. Acids Res. 2005 33, 2302-9
    http://zhanglab.ccmb.med.umich.edu/TM-align/

USAGE

    tmalign mobile, target [, args [, exe ]]

ARGUMENTS

    mobile, target = string: atom selections

    args = string: Extra arguments like -d0 5 -L 100

    exe = string: Path to TMalign executable {default: TMalign}

    ter = 0/1: If ter=0, then ignore chain breaks because TMalign will stop
    at first TER record {default: 0}

SEE ALSO

    tmscore, mmalign
	'''
	import subprocess, tempfile, os, re

	ter, quiet = int(ter), int(quiet)

	mobile_filename = tempfile.mktemp('.pdb', 'mobile')
	target_filename = tempfile.mktemp('.pdb', 'target')
	mobile_ca_sele = '(%s) and (not hetatm) and name CA and alt +A' % (mobile)
	target_ca_sele = '(%s) and (not hetatm) and name CA and alt +A' % (target)

	if ter:
		save = cmd.save
	else:
		save = save_pdb_without_ter
	save(mobile_filename, mobile_ca_sele)
	save(target_filename, target_ca_sele)

	exe = cmd.exp_path(exe)
	args = [exe, mobile_filename, target_filename] + args.split()

	try:
		process = subprocess.Popen(args, stdout=subprocess.PIPE)
	except OSError:
		print 'Cannot execute "%s", please provide full path to TMscore or TMalign executable' % (exe)
		raise CmdException

	r = None
	re_score = re.compile(r'TM-score\s*=\s*(\d*\.\d*)')
	rowcount = 0
	matrix = []
	line_it = iter(process.stdout)
	for line in line_it:
		if 4 >= rowcount > 0:
			if rowcount >= 2:
				a = map(float, line.split())
				matrix.extend(a[2:5])
				matrix.append(a[1])
			rowcount += 1
		elif line.lower().startswith(' -------- rotation matrix'):
			rowcount = 1
		elif line.startswith('(":" denotes'):
			alignment = [line_it.next().rstrip() for i in range(3)]
		else:
			match = re_score.search(line)
			if match is not None:
				r = float(match.group(1))
		if not quiet:
			print line.rstrip()
		
	if not quiet:
		for i in range(0, len(alignment[0])-1, 78):
			for line in alignment:
				print line[i:i+78]
			print ''

	assert len(matrix) == 3*4
	matrix.extend([0,0,0,1])

	if int(transform):
		cmd.transform_selection('byobject (%s)' % (mobile), matrix, homogenous=1)
	
	# alignment object
	if object is not None:
		mobile_idx, target_idx = [], []
		space = {'mobile_idx': mobile_idx, 'target_idx': target_idx}
		cmd.iterate(mobile_ca_sele, 'mobile_idx.append("%s`%d" % (model, index))', space=space)
		cmd.iterate(target_ca_sele, 'target_idx.append("%s`%d" % (model, index))', space=space)
		for i, aa in enumerate(alignment[0]):
			if aa == '-':
				mobile_idx.insert(i, None)
		for i, aa in enumerate(alignment[2]):
			if aa == '-':
				target_idx.insert(i, None)
		if (len(mobile_idx) == len(target_idx) == len(alignment[2])):
			cmd.rms_cur(
					' '.join(idx for (idx, m) in zip(mobile_idx, alignment[1]) if m in ':.'),
					' '.join(idx for (idx, m) in zip(target_idx, alignment[1]) if m in ':.'),
					cycles=0, matchmaker=4, object=object)
		else:
			print 'Could not load alignment object'

	if not quiet and r is not None:
		print 'Found in output TM-score = %.4f' % (r)

	os.remove(mobile_filename)
	os.remove(target_filename)

	return r
for a in xrange(1, 11):
    cmd.create("trg", "ref", 1, a, quiet=0)
    cmd.alter_state(a, "trg", "x=x+random()/2")
    cmd.alter_state(a, "trg", "y=y+random()/2")
    cmd.alter_state(a, "trg", "z=z+random()/2", quiet=0)

cmd.frame(1)
print "%8.3f" % cmd.fit("ref", "trg")

# 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))
Example #18
0
native_pose = pose_from_pdb('3ixt.clean.pdb')
native_score = str(sf_dock(native_pose))	#load native complex for rmsd comparison


pymol_native_pose = '3ixt.clean.pdb'
cmd.load(pymol_native_pose,"native")

ofile.write("Native Score: " + native_score + '\n')

results = []
for file in sys.argv[2:]:	
	pose = pose_from_pdb(str(file))
	score = str(sf_dock(pose))
	cmd.load(file,"the_model")
	cmd.align("native","the_model")
	cmd.rms_cur('3ixt.clean','cleancomplex.5')
	cmd.delete("the_model")
	result = str(file) + ": " + score + " RMSD: " + str(rms) 
	results.append(result)

for x in range(len(results)):
	ofile.write(results[x] + '\n')	
	
	
	
ofile.close()	

'''
ERROR: Error in core::pack::dunbrack::RotamericSingleResidueDunbrackLibrary::get_bbs_from_rsd():
 Residues with rotamers dependent on a subset of backbone torsions must use NCAARotamerLibrarySpecifactions.  
 Could not get rotamer specification for ASN:AcetylatedNtermProteinFull.
Example #19
0
#Add the header line to outfile.
outfile.write(', '.join(header))
outfile.write('\n')

#Load the "root" PDB file (ie. the file that will be compared to all other pdb files.)
cmd.load(rmsdroot, "root")
#Iterate over all files in the directory.
for filename in sorted(os.listdir(os.getcwd())):
	#Initialize the list of scores.
	score_vals = []
	#If the file is a pdb file, load it as "decoy"
	if filename.endswith(".pdb"):
		cmd.load(filename, "decoy")
	else: continue
	#Calculate the RMSD between root and decoy over the atoms specified by selector.
	rmsd = cmd.rms_cur("root and "+selector, "decoy and "+selector)
	
	#Read the PDB file into lines.
	lines=open(filename).readlines()
	#Iterate over each scoreterm in scores.
	for score_term in scores:
		#Going backwards through the PDB file, find the line starting with the current scoreterm.
		#Exit if the scoreterm isn't found in the file.
		for line in reversed(lines):
			if line.startswith('#'):# assumes all score terms are at the end of the file, no comments after them
				sys.exit(score_term+" was not found in "+filename)
			#Split the line into the term name and the score.
			(this_term, score)=line.split()[:2]
			#If the term matches the desired scoreterm, store the score in score_vals and break (continue to next term).
			if this_term==score_term:
				score_vals.append(score)
Example #20
0
outfile.write(', '.join(header))
outfile.write('\n')

#Load the "root" PDB file (ie. the file that will be compared to all other pdb files.)
cmd.load(rmsdroot, "root")
#Iterate over all files in the directory.
for filename in sorted(os.listdir(os.getcwd())):
    #Initialize the list of scores.
    score_vals = []
    #If the file is a pdb file, load it as "decoy"
    if filename.endswith(".pdb"):
        cmd.load(filename, "decoy")
    else:
        continue
    #Calculate the RMSD between root and decoy over the atoms specified by selector.
    rmsd = cmd.rms_cur("root and " + selector, "decoy and " + selector)

    #Read the PDB file into lines.
    lines = open(filename).readlines()
    #Iterate over each scoreterm in scores.
    for score_term in scores:
        #Going backwards through the PDB file, find the line starting with the current scoreterm.
        #Exit if the scoreterm isn't found in the file.
        for line in reversed(lines):
            if line.startswith(
                    '#'
            ):  # assumes all score terms are at the end of the file, no comments after them
                sys.exit(score_term + " was not found in " + filename)
            #Split the line into the term name and the score.
            (this_term, score) = line.split()[:2]
            #If the term matches the desired scoreterm, store the score in score_vals and break (continue to next term).
Example #21
0
def rmsdByRes(referenceProteinChain, sel, targetProteinChain):
    """
  Update
    Zhenting Gao on 7/28/2016

  USAGE

    rmsf referenceProteinChain, targetProteinChain, selection [,byres=0], [reference_state=1]

    Calculate the RMSD for each residue pairs from two chains of the same protein from two crystal structures.

  Workflow
    Read reference and target pdb files
    Align two structures
        sel target, proA and chain A
            #define target protein chain
        sel refrence, proB and chain A
            #define reference protein chain
        align target, reference
            #automatical alignment
    Clean attributes
        otherwise rms_cur will fail


  """
    # Create temporary objects, exclude alternative conformation B
    cmd.create("ref_gzt", referenceProteinChain + " and polymer and not alt B")
    cmd.alter("ref_gzt", "chain='A'")
    cmd.alter("ref_gzt", "segi=''")
    cmd.create("target_gzt", targetProteinChain + " and polymer and not alt B")
    cmd.alter("target_gzt", "chain='A'")
    cmd.alter("target_gzt", "segi=''")
    #  cmd.align("target_gzt","ref_gzt",object="align")
    # parameters
    outputText = ""
    res2Check = [
        'HIS', 'ASP', 'ARG', 'PHE', 'GLN', 'GLU', 'LEU', 'ASN', 'TYR', 'VAL'
    ]

    # select alpha carbon of selected residues in reference structure
    calpha = cmd.get_model(sel + " and name CA and not alt B")

    for g in calpha.atom:
        #  print g.resi+g.resn
        if cmd.count_atoms("ref_gzt and polymer and resi " +
                           g.resi) == cmd.count_atoms(
                               "target_gzt and polymer and resi " + g.resi):
            rmsdRes = cmd.rms_cur("ref_gzt and polymer and resi " + g.resi,
                                  "target_gzt and polymer and resi " + g.resi)
            rmsdResCa = cmd.rms_cur(
                "ref_gzt and polymer and resi " + g.resi + " and name ca",
                "target_gzt and polymer and resi " + g.resi + " and name ca")
            rmsdResBackbone = cmd.rms_cur(
                "ref_gzt and polymer and resi " + g.resi +
                " and name ca+n+c+o", "target_gzt and polymer and resi " +
                g.resi + " and name ca+n+c+o")
            #  calculate minimum rmsd
            rmsdResMin = rmsdRes
            if g.resn in res2Check:
                flippedRes = flipAtomName("target_gzt and polymer and resi " +
                                          g.resi)
                rmsdFlippedRes = cmd.rms_cur(
                    "ref_gzt and polymer and resi " + g.resi, flippedRes)
                if rmsdFlippedRes < rmsdRes:
                    rmsdResMin = rmsdFlippedRes


#    print cmd.count_atoms("ref_gzt and polymer and resi "+g.resi),cmd.count_atoms("target_gzt and polymer and resi "+g.resi)
            outputText += "%s,%s,%s,%.3f,%.3f,%.3f,%.3f\n" % (
                targetProteinChain, g.resn, g.resi, rmsdRes, rmsdResCa,
                rmsdResBackbone, rmsdResMin)

    print outputText
    # Destroy temporary objects
    cmd.delete("ref_gzt target_gzt align res_gzt " + flippedRes)

    # Save data into csv
    outputFile = 'rmsdByRes_' + sel + '.csv'
    f = open(outputFile, 'a')
    if not is_non_zero_file(outputFile):
        f.write(
            "targe,residueName,residueId,allAtomRMSD,rmsdResCa,rmsdResBackbone,allAtomRMSDMin\n"
        )
    f.write(outputText)
    f.close()
    print "Results saved in " + outputFile
Example #22
0
ofile = open('lig_rmsd_rel.txt', 'w')

native_complex = 'nat_relaxed.pdb'

for file in sys.argv[2:]:
    if file != native_complex and file != 'natlig.pdb' and file != 'modlig.pdb':
        print file
        cmd.load(native_complex, 'native_complex')
        cmd.load(file, 'model_complex')
        cmd.align('native_complex', 'model_complex')
        cmd.select('natlig', 'native_complex and chain P')
        cmd.select('modlig', 'model_complex and chain C')
        cmd.save('natlig.pdb', 'natlig')
        cmd.save('modlig.pdb', 'modlig')
        cmd.delete('model_complex')
        cmd.delete('native_complex')
        cmd.load('natlig.pdb', 'native_ligand')
        cmd.load('modlig.pdb', 'model_ligand')
        cmd.alter('all', 'chain=""')
        cmd.alter('all', 'segi=""')
        cmd.select('natligca', 'native_ligand and name ca')
        cmd.select('modligca', 'model_ligand and name ca')
        rms_ca = cmd.rms_cur('natligca', 'modligca')
        ofile.write(file + ': ' + str(rms_ca) + '\n')
        cmd.delete('native_ligand')
        cmd.delete('model_ligand')
        os.remove('natlig.pdb')
        os.remove('modlig.pdb')

ofile.close()
Example #23
0
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')
Example #24
0
def tmalign(mobile, target, mobile_state=1, target_state=1, args='',
        exe='TMalign', ter=0, transform=1, object=None, quiet=0):
    '''
DESCRIPTION

    TMalign wrapper. You may also use this as a TMscore or MMalign wrapper
    if you privide the corresponding executable with the "exe" argument.

    Reference: Y. Zhang and J. Skolnick, Nucl. Acids Res. 2005 33, 2302-9
    http://zhanglab.ccmb.med.umich.edu/TM-align/

ARGUMENTS

    mobile, target = string: atom selections

    mobile_state, target_state = int: object states {default: 1}

    args = string: Extra arguments like -d0 5 -L 100

    exe = string: Path to TMalign (or TMscore, MMalign) executable
    {default: TMalign}

    ter = 0/1: If ter=0, then ignore chain breaks because TMalign will stop
    at first TER record {default: 0}
    '''
    import subprocess, tempfile, os, re
    from .exporting import save_pdb_without_ter

    ter, quiet = int(ter), int(quiet)

    mobile_filename = tempfile.mktemp('.pdb', 'mobile')
    target_filename = tempfile.mktemp('.pdb', 'target')
    matrix_filename = tempfile.mktemp('.txt', 'matrix')
    mobile_ca_sele = '(%s) and (not hetatm) and name CA and alt +A' % (mobile)
    target_ca_sele = '(%s) and (not hetatm) and name CA and alt +A' % (target)

    if ter:
        save = cmd.save
    else:
        save = save_pdb_without_ter
    save(mobile_filename, mobile_ca_sele, state=mobile_state)
    save(target_filename, target_ca_sele, state=target_state)

    exe = cmd.exp_path(exe)
    args = [exe, mobile_filename, target_filename, '-m', matrix_filename] + args.split()

    try:
        process = subprocess.Popen(args, stdout=subprocess.PIPE,
                universal_newlines=True)
        lines = process.stdout.readlines()
    except OSError:
        print('Cannot execute "%s", please provide full path to TMscore or TMalign executable' % (exe))
        raise CmdException
    finally:
        os.remove(mobile_filename)
        os.remove(target_filename)

    # TMalign >= 2012/04/17
    if os.path.exists(matrix_filename):
        lines += open(matrix_filename).readlines()
        os.remove(matrix_filename)

    r = None
    re_score = re.compile(r'TM-score\s*=\s*(\d*\.\d*)')
    rowcount = 0
    matrix = []
    line_it = iter(lines)
    headercheck = False
    alignment = []
    for line in line_it:
        if 4 >= rowcount > 0:
            if rowcount >= 2:
                a = list(map(float, line.split()))
                matrix.extend(a[2:5])
                matrix.append(a[1])
            rowcount += 1
        elif not headercheck and line.startswith(' * '):
            a = line.split(None, 2)
            if len(a) == 3:
                headercheck = a[1]
        elif line.lower().startswith(' -------- rotation matrix'):
            rowcount = 1
        elif line.startswith('(":" denotes'):
            alignment = [next(line_it).rstrip() for i in range(3)]
        else:
            match = re_score.search(line)
            if match is not None:
                r = float(match.group(1))
        if not quiet:
            print(line.rstrip())
        
    if not quiet:
        for i in range(0, len(alignment[0])-1, 78):
            for line in alignment:
                print(line[i:i+78])
            print('')

    assert len(matrix) == 3*4
    matrix.extend([0,0,0,1])

    if int(transform):
        for model in cmd.get_object_list('(' + mobile + ')'):
            cmd.transform_object(model, matrix, state=0, homogenous=1)
    
    # alignment object
    if object is not None:
        mobile_idx, target_idx = [], []
        space = {'mobile_idx': mobile_idx, 'target_idx': target_idx}
        cmd.iterate_state(mobile_state, mobile_ca_sele, 'mobile_idx.append("%s`%d" % (model, index))', space=space)
        cmd.iterate_state(target_state, target_ca_sele, 'target_idx.append("%s`%d" % (model, index))', space=space)
        for i, aa in enumerate(alignment[0]):
            if aa == '-':
                mobile_idx.insert(i, None)
        for i, aa in enumerate(alignment[2]):
            if aa == '-':
                target_idx.insert(i, None)
        if (len(mobile_idx) == len(target_idx) == len(alignment[2])):
            cmd.rms_cur(
                    ' '.join(idx for (idx, m) in zip(mobile_idx, alignment[1]) if m in ':.'),
                    ' '.join(idx for (idx, m) in zip(target_idx, alignment[1]) if m in ':.'),
                    cycles=0, matchmaker=4, object=object)
        else:
            print('Could not load alignment object')

    if not quiet:
        if headercheck:
            print('Finished Program:', headercheck)
        if r is not None:
            print('Found in output TM-score = %.4f' % (r))

    return r
Example #25
0
def rms_current(obj1,select1,obj2,select2):
  """
DESCRIPTION

  "rms_current" allows the superpositioning of object1 onto object2 using
  the atoms in selection1 and selection2 (side chains are ignored).  The
  residue names, residue numbers chain identifiers, segment identifiers,
  and alt ids of selection1 are changed to match those in selection2,
  temporarily.  This allows the normal "rms_cur" command to work.  They are
  reset after "rms_cur" is run and two new selections are created showing
  the selected atoms.

  Be careful when creating your selection strings. Within the
  selections, do not include the object name because the chain, residue
  name, residue number etc. of selection1 of object1 are converted to
  match those in selection2.  If the object names are included in the
  selections, no atoms will be selected since an atom cannot exist in
  both object1 and object2 at the same time.

  It is important that the beginning residue numbers specify the
  aligned residues, but the ending numbers are not critical.  The
  shorter of the two selections is used in the rms_cur calculation.

USAGE 

  rms_current object1, selection1, object2, selection2

  DO NOT include object names in selections!

EXAMPLES

  rms_current 1xuu, c. a & (i. 296-309 or i. 335-340), 1ame, i. 8-21 or i. 47-52

  """

  list_m = []
  list_n = []

  backbone = 'n. n+ca+c+o &! r. hoh+wat'
  select1 = '(%s) & %s' % (select1,backbone)
  select2 = '(%s) & %s' % (select2,backbone)
  m=cmd.get_model("%s & %s" % (obj1,select1))
  n=cmd.get_model("%s & %s" % (obj2,select2))

# for the atoms to be used in rms_cur:
# store id, chain, resn, resi, name, segi, alt
  for at in m.atom:
    list_m.append((at.id,at.chain,at.resn,at.resi,at.name,at.segi, at.alt))
  for at in n.atom:
    list_n.append((at.id,at.chain,at.resn,at.resi,at.name,at.segi, at.alt))

  if len(m.atom) <= len(n.atom):
    total = len(m.atom)
  else:
    total = len(n.atom)

# set a new segi for the atoms to be used in rms_cur command and to allow resetting later
  seg_rms="1rms"

# change the chain,resn,resi,segi and alt of select1 to match select2
  for i in range(total):
    cmd.do("alter %s & id %s, chain='%s'" % (obj1,list_m[i][0],list_n[i][1]))
    cmd.do("alter %s & id %s, resn='%s'" % (obj1,list_m[i][0],list_n[i][2]))
    cmd.do("alter %s & id %s, resi=%s" % (obj1,list_m[i][0],list_n[i][3]))
    cmd.do("alter %s & id %s, segi='%s'" % (obj1,list_m[i][0],seg_rms))
    cmd.do("alter %s & id %s, alt='%s'" % (obj1,list_m[i][0],list_n[i][6]))
# change the segid for obj2 and select2
    cmd.do("alter %s & id %s, segi='%s'" % (obj2,list_n[i][0],seg_rms))

  print "Calculating RMS between %s and %s\n     to %s and %s" % (obj1,select1,obj2,select2)

  print "Altered to:"
  print "%s & %s & segi %s\n" % (obj1,select2,seg_rms),
  print "%s & %s & segi %s\n" % (obj2,select2,seg_rms),
  print "--------------------------------------------\n"
  rms = cmd.rms_cur("%s & %s & segi %s" % (obj1,select2,seg_rms),"%s & %s & segi %s" % (obj2,select2,seg_rms) ,quiet=0)

  cmd.delete("%s_rms" % obj1)
  cmd.delete("%s_rms" % obj2)
# create new objects to show the rms atoms
  cmd.create("%s_rms" % obj1, "%s & %s & segi %s" % (obj1,select2,seg_rms))
  cmd.create("%s_rms" % obj2, "%s & %s & segi %s" % (obj2,select2,seg_rms))

# reset chain,resn,resi,segi & alt of obj1 & select1 from stored list
  for atoms_m in list_m:
    cmd.do("alter %s & id %s, chain='%s'" % (obj1,atoms_m[0],atoms_m[1]))
    cmd.do("alter %s & id %s, resn='%s'" % (obj1,atoms_m[0],atoms_m[2]))
    cmd.do("alter %s & id %s, resi=%s" % (obj1,atoms_m[0],atoms_m[3]))
    cmd.do("alter %s & id %s, segi='%s'" % (obj1,atoms_m[0],atoms_m[5]))
    cmd.do("alter %s & id %s, alt='%s'" % (obj1,atoms_m[0],atoms_m[6]))
# reset segi of obj2 & select2 from stored list
  for atoms_n in list_n:
    cmd.do("alter %s & id %s, segi='%s'" % (obj2,atoms_n[0],atoms_n[5]))

  print "RMSD for selection %s of %s onto \n                 selection %s of %s = %6.3f" % (select1, obj1, select2, obj2, rms)