def identity_matrix_index(matrices): from Matrix import is_identity_matrix for i in range(len(matrices)): if is_identity_matrix(matrices[i]): return i return None
def identity_matrix_index(matrices): from Matrix import is_identity_matrix for i in range(len(matrices)): if is_identity_matrix(matrices[i]): return i return None
def create_symmetry_copies(mol, csys, tflist, cdist, rdist, exclude_identity=True): if exclude_identity: from Matrix import is_identity_matrix tflist = [tf for tf in tflist if not is_identity_matrix(tf)] close_contacts = not cdist is None close_centers = not rdist is None if not close_contacts and not close_centers: transforms = tflist # Use all transforms elif close_contacts and not close_centers: transforms = contacting_transforms(mol, csys, tflist, cdist) elif close_centers and not close_contacts: transforms = close_center_transforms(mol, csys, tflist, rdist) else: transforms = unique( contacting_transforms(mol, csys, tflist, cdist) + close_center_transforms(mol, csys, tflist, rdist)) if hasattr(mol, 'symmetry_copies'): remove_symmetry_copies(mol) copies = [] from chimera import openModels, replyobj from PDBmatrices import copy_molecule from Matrix import chimera_xform for tf in transforms: copy = copy_molecule(mol) copy.symmetry_xform = chimera_xform(tf) copies.append(copy) replyobj.status('Created symmetry copy %d of %d' % (len(copies), len(transforms))) if len(copies) == 0: return copies openModels.add(copies) replyobj.status('') mol.symmetry_copies = copies mol.symmetry_reference_model = csys # TODO: Set xform before opening so that code that detects open sees # the correct position. Currently not possible. Bug 4486. update_symmetry_positions(mol) return copies
def check_orthogonality(matrices, name, tolerance): from Matrix import transpose_matrix, multiply_matrices, is_identity_matrix for mindex, m in enumerate(matrices): mr = zero_translation(m) mrt = transpose_matrix(mr) p = multiply_matrices(mr, mrt) if not is_identity_matrix(p, tolerance): print ('%s matrix %d is not orthogonal, tolerance %.3g' % (name, mindex, tolerance)) print_matrix(m, '%10.5f') print ' matrix times transpose = ' print_matrix(p, '%10.5f')
def check_orthogonality(matrices, name, tolerance): from Matrix import transpose_matrix, multiply_matrices, is_identity_matrix for mindex, m in enumerate(matrices): mr = zero_translation(m) mrt = transpose_matrix(mr) p = multiply_matrices(mr, mrt) if not is_identity_matrix(p, tolerance): print('%s matrix %d is not orthogonal, tolerance %.3g' % (name, mindex, tolerance)) print_matrix(m, '%10.5f') print ' matrix times transpose = ' print_matrix(p, '%10.5f')
def create_symmetry_copies(mol, csys, tflist, cdist, rdist, exclude_identity = True): if exclude_identity: from Matrix import is_identity_matrix tflist = [tf for tf in tflist if not is_identity_matrix(tf)] close_contacts = not cdist is None close_centers = not rdist is None if not close_contacts and not close_centers: transforms = tflist # Use all transforms elif close_contacts and not close_centers: transforms = contacting_transforms(mol, csys, tflist, cdist) elif close_centers and not close_contacts: transforms = close_center_transforms(mol, csys, tflist, rdist) else: transforms = unique(contacting_transforms(mol, csys, tflist, cdist) + close_center_transforms(mol, csys, tflist, rdist)) if hasattr(mol, 'symmetry_copies'): remove_symmetry_copies(mol) copies = [] from chimera import openModels, replyobj from PDBmatrices import copy_molecule from Matrix import chimera_xform for tf in transforms: copy = copy_molecule(mol) copy.symmetry_xform = chimera_xform(tf) copies.append(copy) replyobj.status('Created symmetry copy %d of %d' % (len(copies), len(transforms))) if len(copies) == 0: return copies openModels.add(copies) replyobj.status('') mol.symmetry_copies = copies mol.symmetry_reference_model = csys # TODO: Set xform before opening so that code that detects open sees # the correct position. Currently not possible. Bug 4486. update_symmetry_positions(mol) return copies
def make_asu_copies(m, asu_list, crystal): xflist = [] names = [] from Matrix import is_identity_matrix, chimera_xform for asu in asu_list: if is_identity_matrix(asu.transform): continue xflist.append(chimera_xform(asu.transform)) name = '%s %s' % (m.name, '%d %d %d' % asu.unit_cell_index) if len(crystal.smtry_matrices) > 1: name += ' sym %d' % asu.smtry_index if len(crystal.ncs_matrices) > 1: name += ' ncs %d' % asu.ncs_index names.append(name) cmodels = make_molecule_copies(m, xflist, names) return cmodels
def make_asu_copies(m, asu_list, crystal): xflist = [] names = [] from Matrix import is_identity_matrix, chimera_xform for asu in asu_list: if is_identity_matrix(asu.transform): continue xflist.append(chimera_xform(asu.transform)) name = '%s %s' % (m.name, '%d %d %d' % asu.unit_cell_index) if len(crystal.smtry_matrices) > 1: name += ' sym %d' % asu.smtry_index if len(crystal.ncs_matrices) > 1: name += ' ncs %d' % asu.ncs_index names.append(name) cmodels = make_molecule_copies(m, xflist, names) return cmodels
def place_molecule_copies(m, tflist): path, file_type, default_file_type, prefixable_type = m.openedAs from Molecule import copy_molecule, transform_atom_positions from Matrix import is_identity_matrix for i,tf in enumerate(tflist): if is_identity_matrix(tf): continue name = m.name + (' #%d' % (i+1)) c = find_model_by_name(name) if c is None: c = copy_molecule(m) c.name = name transform_atom_positions(c.atoms, tf) chimera.openModels.add([c]) else: transform_atom_positions(c.atoms, tf, m.atoms)
def place_molecule_copies(m, tflist): path, file_type, default_file_type, prefixable_type = m.openedAs from Molecule import copy_molecule, transform_atom_positions from Matrix import is_identity_matrix for i, tf in enumerate(tflist): if is_identity_matrix(tf): continue name = m.name + (' #%d' % (i + 1)) c = find_model_by_name(name) if c is None: c = copy_molecule(m) c.name = name transform_atom_positions(c.atoms, tf) chimera.openModels.add([c]) else: transform_atom_positions(c.atoms, tf, m.atoms)
def surface_geometry(plist, tf, pad): surfaces = [] for p in plist: surfs = [] va, ta = p.maskedGeometry(p.Solid) na = p.normals if isinstance(pad, (float, int)) and pad != 0: varray, tarray = offset_surface(va, ta, na, pad) elif isinstance(pad, (list, tuple)) and len(pad) == 2: varray, tarray = slab_surface(va, ta, na, pad) else: varray, tarray = va, ta if not tf is None: from Matrix import xform_matrix, multiply_matrices, is_identity_matrix vtf = multiply_matrices(tf, xform_matrix(p.model.openState.xform)) if not is_identity_matrix(vtf): apply_transform(vtf, varray) surfaces.append((varray, tarray)) return surfaces
def surface_geometry(plist, tf, pad): surfaces = [] for p in plist: surfs = [] va, ta = p.maskedGeometry(p.Solid) na = p.normals if isinstance(pad, (float,int)) and pad != 0: varray, tarray = offset_surface(va, ta, na, pad) elif isinstance(pad, (list,tuple)) and len(pad) == 2: varray, tarray = slab_surface(va, ta, na, pad) else: varray, tarray = va, ta if not tf is None: from Matrix import xform_matrix, multiply_matrices, is_identity_matrix vtf = multiply_matrices(tf, xform_matrix(p.model.openState.xform)) if not is_identity_matrix(vtf): apply_transform(vtf, varray) surfaces.append((varray, tarray)) return surfaces
def group_symmetries(group, center, axis, mol): from Commands import CommandError g0 = group[:1].lower() if g0 in ('c', 'd'): # Cyclic or dihedral symmetry: C<n>, D<n> try: n = int(group[1:]) except ValueError: raise CommandError, 'Invalid symmetry group syntax "%s"' % group if n < 2: raise CommandError, 'Cn or Dn with n = %d < 2' % (n,) if g0 == 'c': tflist = cyclic_symmetries(n) else: tflist = dihedral_symmetries(n) elif g0 == 'i': # Icosahedral symmetry: i[,<orientation>] import Icosahedron as icos gfields = group.split(',') if len(gfields) == 1: orientation = '222' elif len(gfields) == 2: orientation = gfields[1] if not orientation in icos.coordinate_system_names: raise CommandError, ('Unknown icosahedron orientation "%s"' % orientation) else: raise CommandError, 'Invalid symmetry group syntax "%s"' % group tflist = icos.icosahedral_symmetry_matrices(orientation) elif g0 == 'h': # Helical symmetry: h,<repeat>,<rise>[,<angle>[,<n>,<offet>]] gfields = group.split(',') nf = len(gfields) if nf < 3 or nf > 6: raise CommandError, 'Invalid symmetry group syntax "%s"' % group try: param = [float(f) for f in gfields[1:]] except ValueError: raise CommandError, 'Invalid symmetry group syntax "%s"' % group if len(param) == 2: param.append(360.0) if len(param) == 3: param.append(int(param[0])) if len(param) == 4: param.append(0) repeat, rise, angle, n, offset = param tflist = helical_symmetry(repeat, rise, angle, n, offset) elif g0 == 't': # Translation symmetry: t,<n>,<distance> or t,<n>,<dx>,<dy>,<dz> gfields = group.split(',') nf = len(gfields) if nf != 3 and nf != 5: raise CommandError, 'Invalid symmetry group syntax "%s"' % group try: param = [float(f) for f in gfields[1:]] except ValueError: raise CommandError, 'Invalid symmetry group syntax "%s"' % group n = param[0] if n != int(n): raise CommandError, 'Invalid symmetry group syntax "%s"' % group if nf == 3: delta = (0,0,param[1]) else: delta = param[1:] tflist = translation_symmetry(n, delta) elif group.lower() == 'biomt': # BIOMT biological unit matrices from PDB file header. if not hasattr(mol, 'pdbHeaders'): msg = 'Molecule %s has no BIOMT matrices or PDB headers' % mol.name raise CommandError, msg from PDBmatrices import pdb_biomt_matrices tflist = pdb_biomt_matrices(mol.pdbHeaders) if len(tflist) == 0: msg = 'Molecule %s has no BIOMT matrices in PDB header' % mol.name raise CommandError, msg from Matrix import is_identity_matrix if len(tflist) == 1 and is_identity_matrix(tflist[0]): msg = 'Molecule %s has only identity BIOMT matrix in PDB header' % mol.name raise CommandError, msg else: raise CommandError, 'Unknown symmetry group "%s"' % group # Apply center and axis transformation. if tuple(center) != (0,0,0) or tuple(axis) != (0,0,1): import Matrix as m tf = m.multiply_matrices(m.vector_rotation_transform(axis, (0,0,1)), m.translation_matrix([-c for c in center])) tfinv = m.invert_matrix(tf) tflist = [m.multiply_matrices(tfinv, t, tf) for t in tflist] return tflist
mc = molecule_center(molecule) tflist = Crystal.pack_unit_cell(uc, gorigin, mc, tflist) # Make multiple unit cells #nc = self.number_of_cells() nc = (3, 3, 3) #if nc != (1,1,1) and uc: # Compute origin. oc_val = (1, 1, 1) oc = tuple((((o + n - 1) % n) - (n - 1)) for o, n in zip(oc_val, nc)) tflist = Crystal.unit_cell_translations(uc, oc, nc, tflist) from Molecule import copy_molecule, transform_atom_positions from Matrix import is_identity_matrix for i, tf in enumerate(tflist): if is_identity_matrix(tf): continue name = m.name + (' #%d' % (i + 1)) c = copy_molecule(m) c.name = name c.unit_cell_copy = m transform_atom_positions(c.atoms, tf) chimera.openModels.add([c]) # else: # transform_atom_positions(c.atoms, tf, m.atoms) c.openState.xform = m.openState.xform mlist.append(c) # Write PDB file with transformed copies of molecule import Midas out_path = 'unitcell.pdb'
def group_symmetries(group, center, axis, mol): from Commands import CommandError g0 = group[:1].lower() if g0 in ('c', 'd'): # Cyclic or dihedral symmetry: C<n>, D<n> try: n = int(group[1:]) except ValueError: raise CommandError, 'Invalid symmetry group syntax "%s"' % group if n < 2: raise CommandError, 'Cn or Dn with n = %d < 2' % (n, ) if g0 == 'c': tflist = cyclic_symmetries(n) else: tflist = dihedral_symmetries(n) elif g0 == 'i': # Icosahedral symmetry: i[,<orientation>] import Icosahedron as icos gfields = group.split(',') if len(gfields) == 1: orientation = '222' elif len(gfields) == 2: orientation = gfields[1] if not orientation in icos.coordinate_system_names: raise CommandError, ('Unknown icosahedron orientation "%s"' % orientation) else: raise CommandError, 'Invalid symmetry group syntax "%s"' % group tflist = icos.icosahedral_symmetry_matrices(orientation) elif g0 == 'h': # Helical symmetry: h,<repeat>,<rise>[,<angle>[,<n>,<offet>]] gfields = group.split(',') nf = len(gfields) if nf < 3 or nf > 6: raise CommandError, 'Invalid symmetry group syntax "%s"' % group try: param = [float(f) for f in gfields[1:]] except ValueError: raise CommandError, 'Invalid symmetry group syntax "%s"' % group if len(param) == 2: param.append(360.0) if len(param) == 3: param.append(int(param[0])) if len(param) == 4: param.append(0) repeat, rise, angle, n, offset = param tflist = helical_symmetry(repeat, rise, angle, n, offset) elif g0 == 't': # Translation symmetry: t,<n>,<distance> or t,<n>,<dx>,<dy>,<dz> gfields = group.split(',') nf = len(gfields) if nf != 3 and nf != 5: raise CommandError, 'Invalid symmetry group syntax "%s"' % group try: param = [float(f) for f in gfields[1:]] except ValueError: raise CommandError, 'Invalid symmetry group syntax "%s"' % group n = param[0] if n != int(n): raise CommandError, 'Invalid symmetry group syntax "%s"' % group if nf == 3: delta = (0, 0, param[1]) else: delta = param[1:] tflist = translation_symmetry(n, delta) elif group.lower() == 'biomt': # BIOMT biological unit matrices from PDB file header. if not hasattr(mol, 'pdbHeaders'): msg = 'Molecule %s has no BIOMT matrices or PDB headers' % mol.name raise CommandError, msg from PDBmatrices import pdb_biomt_matrices tflist = pdb_biomt_matrices(mol.pdbHeaders) if len(tflist) == 0: msg = 'Molecule %s has no BIOMT matrices in PDB header' % mol.name raise CommandError, msg from Matrix import is_identity_matrix if len(tflist) == 1 and is_identity_matrix(tflist[0]): msg = 'Molecule %s has only identity BIOMT matrix in PDB header' % mol.name raise CommandError, msg else: raise CommandError, 'Unknown symmetry group "%s"' % group # Apply center and axis transformation. if tuple(center) != (0, 0, 0) or tuple(axis) != (0, 0, 1): import Matrix as m tf = m.multiply_matrices(m.vector_rotation_transform(axis, (0, 0, 1)), m.translation_matrix([-c for c in center])) tfinv = m.invert_matrix(tf) tflist = [m.multiply_matrices(tfinv, t, tf) for t in tflist] return tflist