예제 #1
0
def restrain_ca_distances_to_template(template_residues,
                                      restrained_residues,
                                      distance_cutoff=8,
                                      spring_constant=500):
    '''
    Creates a "web" of distance restraints between nearby CA atoms, restraining
    one set of residues to the same spatial organisation as another.

    Args:
        * template_residues:
            - a :class:`chimerax.atomic.Residues` instance. All residues must be
              from a single model, but need no be contiguous
        * restrained_residues:
            - a :class:`chimerax.atomic.Residues` instance. All residues must be
              from a single model (which may or may not be the same model as for
              `template_residues`). May be the same array as `template_residues`
              (which will just restrain all distances to their current values).
        * distance_cutoff (default = 8):
            - for each CA atom in `restrained_residues`, a distance restraint
              will be created between it and every other CA atom where the
              equivalent atom in `template_residues` is within `distance_cutoff`
              of its template equivalent.
        * spring_constant (default = 500):
            - the strength of each restraint, in :math:`kJ mol^{-1} nm^{-2}`
    '''
    from chimerax.isolde import session_extensions as sx
    if len(template_residues) != len(restrained_residues):
        raise TypeError(
            'Template and restrained residue arrays must be the same length!')
    template_us = template_residues.unique_structures
    if len(template_us) != 1:
        raise TypeError('Template residues must be from a single model!')
    restrained_us = restrained_residues.unique_structures
    if len(restrained_us) != 1:
        raise TypeError('Restrained residues must be from a single model!')
    restrained_model = restrained_us[0]
    template_cas = template_residues.atoms[template_residues.atoms.names ==
                                           'CA']
    restrained_cas = restrained_residues.atoms[restrained_residues.atoms.names
                                               == 'CA']
    template_coords = template_cas.coords
    drm = sx.get_distance_restraint_mgr(restrained_model)
    from chimerax.geometry import find_close_points, distance
    for i, rca1 in enumerate(restrained_cas):
        query_coord = numpy.array([template_coords[i]])
        indices = find_close_points(query_coord, template_coords,
                                    distance_cutoff)[1]
        indices = indices[indices != i]
        for ind in indices:
            rca2 = restrained_cas[ind]
            dr = drm.add_restraint(rca1, rca2)
            dr.spring_constant = spring_constant
            dr.target = distance(query_coord[0], template_coords[ind])
            dr.enabled = True
예제 #2
0
파일: util.py 프로젝트: tristanic/isolde
def any_atom_restrained(model, atoms):
    import numpy
    from chimerax.isolde import session_extensions as sx
    prm = sx.get_position_restraint_mgr(model, create=False)
    drm = sx.get_distance_restraint_mgr(model, create=False)
    adrm = sx.get_adaptive_distance_restraint_mgr(model, create=False)
    if prm is not None:
        prs = prm.get_restraints(atoms)
        if numpy.any(prs.enableds):
            return True
    if drm is not None:
        drs = drm.atoms_restraints(atoms)
        if numpy.any(drs.enableds):
            return True
    if adrm is not None:
        adrs = adrm.atoms_restraints(atoms)
        if numpy.any(adrs.enableds):
            return True
    return False
예제 #3
0
def restrain_small_ligands(model,
                           distance_cutoff=4,
                           heavy_atom_limit=3,
                           spring_constant=5000,
                           bond_to_carbon=False):
    '''
    Residues with a small number of heavy atoms can be problematic in MDFF if
    unrestrained, since if knocked out of density they tend to simply keep
    going. It is best to restrain them with distance restraints to suitable
    surrounding atoms or, failing that, to their starting positions.

    Args:
        * model:
            - a :class:`chimerax.atomic.AtomicStructure` instance
        * distance_cutoff (default = 3.5):
            - radius in Angstroms to look for candidate heavy atoms for distance
              restraints. If no candidates are found, a position restraint will
              be applied instead.
        * heavy_atom_limit (default = 3):
            - Only residues with a number of heavy atoms less than or equal to
              `heavy_atom_limit` will be restrained
        * spring_constant (default = 500):
            - strength of each restraint, in :math:`kJ mol^{-1} nm^{-2}`
        * bond_to_carbon (default = `False`):
            - if `True`, only non-carbon heavy atoms will be restrained using
              distance restraints.
    '''
    from chimerax.atomic import Residue, Residues
    residues = model.residues
    ligands = residues[residues.polymer_types == Residue.PT_NONE]
    small_ligands = Residues([
        r for r in ligands
        if len(r.atoms[r.atoms.element_names != 'H']) < heavy_atom_limit
    ])
    from .. import session_extensions as sx
    drm = sx.get_distance_restraint_mgr(model)
    prm = sx.get_position_restraint_mgr(model)
    all_heavy_atoms = model.atoms[model.atoms.element_names != 'H']
    if not bond_to_carbon:
        all_heavy_atoms = all_heavy_atoms[all_heavy_atoms.element_names != 'C']
    all_heavy_coords = all_heavy_atoms.coords

    from chimerax.geometry import find_close_points, distance
    for r in small_ligands:
        r_heavy_atoms = r.atoms[r.atoms.element_names != 'H']
        if not bond_to_carbon:
            r_non_carbon_atoms = r_heavy_atoms[
                r_heavy_atoms.element_names != 'C']
            if not len(r_non_carbon_atoms):
                # No non-carbon heavy atoms. Apply position restraints
                prs = prm.get_restraints(r_heavy_atoms)
                prs.targets = prs.atoms.coords
                prs.spring_constants = spring_constant
                prs.enableds = True
                continue
            r_heavy_atoms = r_non_carbon_atoms
        r_indices = all_heavy_atoms.indices(r_heavy_atoms)
        r_coords = r_heavy_atoms.coords
        applied_drs = False
        for ra, ri, rc in zip(r_heavy_atoms, r_indices, r_coords):
            _, found_i = find_close_points([rc], all_heavy_coords,
                                           distance_cutoff)
            found_i = found_i[found_i != ri]
            num_drs = 0
            for fi in found_i:
                if fi in r_indices:
                    continue
                dr = drm.add_restraint(ra, all_heavy_atoms[fi])
                dr.spring_constant = spring_constant
                dr.target = distance(rc, all_heavy_coords[fi])
                dr.enabled = True
                num_drs += 1
                # applied_drs = True
        if num_drs < 3:
            # Really need at least 3 distance restraints (probably 4, actually,
            # but we don't want to be *too* restrictive) to be stable in 3D
            # space. If we have fewer than that, add position restraints to be
            # sure.
            prs = prm.add_restraints(r_heavy_atoms)
            prs.targets = prs.atoms.coords
            prs.spring_constants = spring_constant
            prs.enableds = True
예제 #4
0
 def get_distance_restraint_problems(structure):
     from chimerax.isolde import session_extensions as sx
     drm = sx.get_distance_restraint_mgr(structure, create=True)
     distances = drm.all_restraints
     distances = distances[distances.enableds]
     return distances[distances.unsatisfieds]
예제 #5
0
파일: util.py 프로젝트: tristanic/isolde
def correct_position_and_distance_restraints(residue):
    rname = residue.name
    model = residue.structure
    from chimerax.isolde import session_extensions as sx
    if rname in rings:
        rname = 'aromatic'
    pairs = equivalent_heavy_atoms[rname]
    apairs = {residue.find_atom(a1name): residue.find_atom(a2name) for a1name, a2name in pairs.items()}
    prm = sx.get_position_restraint_mgr(model, create=False)
    if prm is not None:
        for a1, a2 in apairs.items():
            p1, p2 = [prm.get_restraint(a) for a in [a1, a2]]
            params = {}
            for p in [p1, p2]:
                if p is not None:
                    params[p] = [p.enabled, p.spring_constant, p.target]
            if not len(params):
                continue
            if p1 is None and p2 is not None:
                p1 = prm.add_restraint(a1)
            elif p2 is None and p1 is not None:
                p2 = prm.add_restraint(a2)
            p2params = params.get(p2, None)
            if p2params:
                p1.enabled, p1.spring_constant, p1.target = p2params
            p1params = params.get(p1, None)
            if p1params:
                p2.enabled, p2.spring_constant, p2.target = p1params

    drm = sx.get_distance_restraint_mgr(model, create=False)
    adrm = sx.get_adaptive_distance_restraint_mgr(model, create=False)
    if drm is not None:
        restraint_params = {}
        for a1, a2 in apairs.items():
            for a in (a1, a2):
                restraints = drm.atom_restraints(a)
                restraint_params[a] = [[r.atoms, r.enabled, r.target, r.spring_constant] for r in restraints]
                restraints.enableds=False
            for rparams in restraint_params[a1]:
                atoms = rparams[0]
                other = [a for a in atoms if a!= a1][0]
                if other != a2:
                    atoms = [a2, other]
                restraint = drm.add_restraint(*atoms)
                restraint.enabled, restraint.target, restraint.spring_constant = rparams[1:]
            for rparams in restraint_params[a2]:
                atoms = rparams[0]
                other = [a for a in atoms if a!= a2][0]
                if other != a1:
                    atoms = [a1, other]
                restraint = drm.add_restraint(*atoms)
                restraint.enabled, restraint.target, restraint.spring_constant = rparams[1:]
    
    if adrm is not None:
        restraint_params = {}
        for a1, a2 in apairs.items():
            for a in (a1, a2):
                restraints = adrm.atom_restraints(a)
                restraint_params[a] = [[r.atoms, r.enabled, r.target, r.kappa, r.alpha, r.tolerance, r.c] for r in restraints]
                restraints.enableds=False
            for rparams in restraint_params[a1]:
                atoms = rparams[0]
                other = [a for a in atoms if a!= a1][0]
                if other != a2:
                    atoms = [a2, other]
                r = adrm.add_restraint(*atoms)
                r.enabled, r.target, r.kappa, r.alpha, r.tolerance, r.c = rparams[1:]
            for rparams in restraint_params[a2]:
                atoms = rparams[0]
                other = [a for a in atoms if a!= a2][0]
                if other != a1:
                    atoms = [a1, other]
                r = adrm.add_restraint(*atoms)
                r.enabled, r.target, r.kappa, r.alpha, r.tolerance, r.c = rparams[1:]