def test_boundary_checks_mat4(self): m = geom.Mat4() m[0, 0] = m[0, 0] m[0, 1] = m[0, 1] m[0, 2] = m[0, 2] m[0, 3] = m[0, 3] m[1, 0] = m[1, 0] m[1, 1] = m[1, 1] m[1, 2] = m[1, 2] m[1, 3] = m[1, 3] m[2, 0] = m[2, 0] m[2, 1] = m[2, 1] m[2, 2] = m[2, 2] m[2, 3] = m[2, 3] m[3, 0] = m[3, 0] m[3, 1] = m[3, 1] m[3, 2] = m[3, 2] m[3, 3] = m[3, 3] self.assertRaises(IndexError, m.__setitem__, (-1, 0), 1) self.assertRaises(IndexError, m.__setitem__, (4, 0), 1) self.assertRaises(IndexError, m.__setitem__, (0, -1), 1) self.assertRaises(IndexError, m.__setitem__, (-1, 4), 1) self.assertRaises(IndexError, m.__getitem__, (-1, 0)) self.assertRaises(IndexError, m.__getitem__, (4, 0)) self.assertRaises(IndexError, m.__getitem__, (0, -1)) self.assertRaises(IndexError, m.__getitem__, (0, 4))
def test_data(self): self.assertEqual(geom.Vec2(1, 2).data, [1, 2]) self.assertEqual(geom.Vec3(1, 2, 3).data, [1, 2, 3]) self.assertEqual(geom.Vec4(1, 2, 3, 4).data, [1, 2, 3, 4]) self.assertEqual(geom.Mat2(1, 2, 3, 4).data, [1, 2, 3, 4]) self.assertEqual( geom.Mat3(1, 2, 3, 4, 5, 6, 7, 8, 9).data, [1, 2, 3, 4, 5, 6, 7, 8, 9]) self.assertEqual( geom.Mat4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16).data, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
def _ParseTmScore(lines): tf1=[float(i.strip()) for i in lines[23].split()] tf2=[float(i.strip()) for i in lines[24].split()] tf3=[float(i.strip()) for i in lines[25].split()] rot=geom.Mat3(tf1[2], tf1[3], tf1[4], tf2[2], tf2[3], tf2[4], tf3[2], tf3[3], tf3[4]) tf=geom.Mat4(rot) tf.PasteTranslation(geom.Vec3(tf1[1], tf2[1], tf3[1])) result=TMScoreResult(float(lines[14].split()[-1].strip()), float(lines[16].split()[2].strip()), float(lines[17].split()[1].strip()), float(lines[18].split()[1].strip()), float(lines[19].split()[1].strip()), float(lines[27].split()[-1].strip()), tf) return result
def test_starts_from_last_water_rnum(self): m = mol.CreateEntity() e = m.EditXCS(mol.BUFFERED_EDIT) c = e.InsertChain("A") e.SetChainType(c, mol.CHAINTYPE_WATER) e.AppendResidue(c, "HOH") pdbizer = mol.alg.PDBize() transformations = geom.Mat4List() transformations.append(geom.Mat4()) seqs = seq.CreateSequenceList() pdbizer.Add(m.Select(''), transformations, seqs) pdbizer.Add(m.Select(''), transformations, seqs) pdbized = pdbizer.Finish() self.assertEqual([c.name for c in pdbized.chains], ["-"]) residues = pdbized.residues self.assertEqual( [r.number for r in residues], [mol.ResNum(1, 'A'), mol.ResNum(1, 'B')])
def _PDBize(biounit, asu, seqres=None, min_polymer_size=10, transformation=False): pdbizer = mol.alg.PDBize(min_polymer_size=min_polymer_size) chains = biounit.GetChainList() c_intvls = biounit.GetChainIntervalList() o_intvls = biounit.GetOperationsIntervalList() ss = seqres if not ss: ss = seq.CreateSequenceList() # create list of operations # for cartesian products, operations are stored in a list, multiplied with # the next list of operations and re-stored... until all lists of operations # are multiplied in an all-against-all manner. operations = biounit.GetOperations() for i in range(0, len(c_intvls)): trans_matrices = geom.Mat4List() l_operations = operations[o_intvls[i][0]:o_intvls[i][1]] if len(l_operations) > 0: for op in l_operations[0]: rot = geom.Mat4() rot.PasteRotation(op.rotation) trans = geom.Mat4() trans.PasteTranslation(op.translation) tr = geom.Mat4() tr = trans * rot trans_matrices.append(tr) for op_n in range(1, len(l_operations)): tmp_ops = geom.Mat4List() for o in l_operations[op_n]: rot = geom.Mat4() rot.PasteRotation(o.rotation) trans = geom.Mat4() trans.PasteTranslation(o.translation) tr = geom.Mat4() tr = trans * rot for t_o in trans_matrices: tp = t_o * tr tmp_ops.append(tp) trans_matrices = tmp_ops # select chains into a view as basis for each transformation assu = asu.Select('cname='+','.join(mol.QueryQuoteName(name) \ for name in \ chains[c_intvls[i][0]:c_intvls[i][1]])) pdbizer.Add(assu, trans_matrices, ss) pdb_bu = pdbizer.Finish(transformation) if transformation: return pdb_bu, pdb_bu.GetTransform().GetMatrix() return pdb_bu
def _ParseTmAlign(lines,lines_matrix): info_line=lines[12].split(',') aln_length=int(info_line[0].split('=')[1].strip()) rmsd=float(info_line[1].split('=')[1].strip()) tm_score=float(lines[14].split('=')[1].split('(')[0].strip()) tf1=[float(i.strip()) for i in lines_matrix[2].split()] tf2=[float(i.strip()) for i in lines_matrix[3].split()] tf3=[float(i.strip()) for i in lines_matrix[4].split()] rot=geom.Mat3(tf1[2], tf1[3], tf1[4], tf2[2], tf2[3], tf2[4], tf3[2], tf3[3], tf3[4]) tf=geom.Mat4(rot) tf.PasteTranslation(geom.Vec3(tf1[1], tf2[1], tf3[1])) seq1 = seq.CreateSequence("1",lines[18].strip()) seq2 = seq.CreateSequence("2",lines[20].strip()) alignment = seq.CreateAlignment() alignment.AddSequence(seq2) alignment.AddSequence(seq1) return ost.bindings.TMAlignResult(rmsd, tm_score, aln_length, tf, alignment)
def test_numbers_water_molecules_with_ins_codes(self): m = mol.CreateEntity() e = m.EditXCS(mol.BUFFERED_EDIT) c = e.InsertChain("A") e.SetChainType(c, mol.CHAINTYPE_WATER) for i in range(27): e.AppendResidue(c, "HOH") pdbizer = mol.alg.PDBize() transformations = geom.Mat4List() transformations.append(geom.Mat4()) seqs = seq.CreateSequenceList() pdbizer.Add(m.Select(''), transformations, seqs) pdbized = pdbizer.Finish() self.assertEqual([c.name for c in pdbized.chains], ["-"]) residues = pdbized.residues for i in range(26): self.assertEqual(residues[i].number.num, 1) self.assertEqual(residues[i].number.ins_code, chr(ord('A') + i)) self.assertEqual(residues[26].number.num, 2) self.assertEqual(residues[26].number.ins_code, 'A')
def _ParseiAlign(lines): info_line = lines[18].split(',') is_score = float(info_line[0].split('=')[1].strip()) aln_residues = int(lines[19].split('=')[1].strip()) aln_contacts = int(lines[20].split('=')[1].strip()) info_line = lines[21].split(',') rmsd = float(info_line[0].split('=')[1].strip()) tf1 = [float(i.strip()) for i in lines[25][1:].split()] tf2 = [float(i.strip()) for i in lines[26][1:].split()] tf3 = [float(i.strip()) for i in lines[27][1:].split()] rot = geom.Mat3(tf1[2], tf1[3], tf1[4], tf2[2], tf2[3], tf2[4], tf3[2], tf3[3], tf3[4]) tf = geom.Mat4(rot) tf.PasteTranslation(geom.Vec3(tf1[1], tf2[1], tf3[1])) seq1 = seq.CreateSequence("1", lines[32].strip()) seq2 = seq.CreateSequence("2", lines[34].strip()) alignment = seq.CreateAlignment() alignment.AddSequence(seq2) alignment.AddSequence(seq1) return iAlignResult(rmsd, tf, alignment, is_score, aln_residues, aln_contacts)
def test_repr(self): v = geom.Vec2(1, 2) v2 = eval(repr(v)) self.assertTrue(geom.Equal(v, v2)) v = geom.Vec3(1, 2, 3) v2 = eval(repr(v)) self.assertTrue(geom.Equal(v, v2)) v = geom.Vec4(1, 2, 3, 4) v2 = eval(repr(v)) self.assertTrue(geom.Equal(v, v2)) m = geom.Mat2(1, 2, 3, 4) m2 = eval(repr(m)) self.assertTrue(geom.Equal(m, m2)) m = geom.Mat3(1, 2, 3, 4, 5, 6, 7, 8, 9) m2 = eval(repr(m)) self.assertTrue(geom.Equal(m, m2)) m = geom.Mat4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) m2 = eval(repr(m)) self.assertTrue(geom.Equal(m, m2))
def GetTransform(self): transform = geom.Mat4() transform.PasteTranslation(self.shift) return transform * geom.Mat4(self.rotation)
import math from ost import geom filename = 'fragment.pdb' if len(sys.argv) > 1: filename = sys.argv[1] ent = io.LoadEntity(filename) edi = ent.EditXCS() m = geom.Mat4() m.PasteTranslation(-ent.bounds.min) edi.ApplyTransform(m) edi.UpdateICS() frag = gfx.Entity('frag', ent) sym_ops = gfx.SymmetryOpList() for i in range(12): m = geom.EulerTransformation(0, i * 2.0 * math.pi / 12.0, 0) sym_ops.append(gfx.SymmetryOp(m, geom.Vec3(0, 0, 10))) sym = gfx.SymmetryNode('sym', sym_ops) scene.Add(sym) sym.Add(frag) scene.SetCenter(geom.Vec3())
def FitToScreen(gfx_ent, width=None, height=None, margin=0.05): """ Setup camera such that it is centered on the graphical entity and the entity fits the entire viewport. The longest axes of the entity are aligned along the x- and y- axes of the screen. :param gfx_ent: The graphical entity :type gfx_ent: str or :class:`Entity` """ from ost import geom import math def _XYZ(view, axes): """ returns the vectors in x, y and z direction respectively. The smallest vector is in z, then y, and the largest along x. """ rows = [axes.GetRow(i) for i in range(3)] lengths = [] for axe in rows: min_proj = geom.Dot(axe, view.atoms[0].pos) max_proj = min_proj for atom in view.atoms[1:]: proj = geom.Dot(axe, atom.pos) min_proj = min(proj, min_proj) max_proj = max(proj, max_proj) lengths.append(max_proj - min_proj) def cmp_x(rhs, lhs): # replaced cmp when porting to Python 3 #return cmp(lhs[1], rhs[1]) return (lhs[1] > rhs[1]) - (lhs[1] < rhs[1]) sorted_axes = sorted(zip(rows, lengths), key=functools.cmp_to_key(cmp_x)) return [r * l for r, l in sorted_axes] scene = Scene() if not isinstance(gfx_ent, Entity): gfx_ent = scene[str(gfx_ent)] width = width and width or scene.viewport.width height = height and height or scene.viewport.height atom_positions = geom.Vec3List([atom.pos for atom in gfx_ent.view.atoms]) axes = atom_positions.principal_axes sorted_axes = _XYZ(gfx_ent.view, axes) x_bigger_than_y = geom.Length(sorted_axes[0]) > geom.Length(sorted_axes[1]) if x_bigger_than_y: if width > height: x_axes = geom.Normalize(sorted_axes[0]) y_axes = geom.Normalize(sorted_axes[1]) else: x_axes = geom.Normalize(sorted_axes[1]) y_axes = geom.Normalize(sorted_axes[0]) else: if width > height: x_axes = geom.Normalize(sorted_axes[1]) y_axes = geom.Normalize(sorted_axes[0]) else: x_axes = geom.Normalize(sorted_axes[0]) y_axes = geom.Normalize(sorted_axes[1]) z_axes = geom.Normalize(geom.Cross(x_axes, y_axes)) rotation = geom.Mat3(x_axes[0], x_axes[1], x_axes[2], y_axes[0], y_axes[1], y_axes[2], z_axes[0], z_axes[1], z_axes[2]) rtc = geom.Mat4(rotation) center = gfx_ent.center aspect = float(width) / float(height) factor_y = 1.0 / math.tan(math.radians(scene.fov)) factor_x = factor_y / aspect z_off = geom.Length(sorted_axes[2]) * 0.5 rtc[0, 3] = center[0] rtc[1, 3] = center[1] rtc[2, 3] = center[2] rtc[3, 0] = 0 rtc[3, 1] = 0 rtc[3, 2] = -(max(factor_x * (1 + margin) * geom.Length(sorted_axes[0]), factor_y * (1 + margin) * geom.Length(sorted_axes[1])) + z_off) scene.SetRTC(rtc)