def _ParseRotationAndShift(lines): t = [l.split() for l in lines] rot = geom.Mat3() shift = geom.Vec3() for i, x in enumerate(t): rot[(i, 0)] = +float(x[2]) rot[(i, 1)] = +float(x[6]) rot[(i, 2)] = +float(x[10]) shift[i] = float(x[14]) return rot, shift
def test_boundary_checks_vec3(self): v = geom.Vec3() v[0] = v[0] v[1] = v[1] v[2] = v[2] self.assertRaises(IndexError, v.__setitem__, (-1), 1) self.assertRaises(IndexError, v.__setitem__, (3), 1) self.assertRaises(IndexError, v.__getitem__, (-1)) self.assertRaises(IndexError, v.__getitem__, (3))
def _ParseModifiedPDB(filename): """Read mpdb file produced by antechamber and return tuple of: - EntityHandle with connectivity, atom types (property 'type') and charges - Residue name as extracted from the mpdb file A RuntimeError is raised if the file can contains multiple residues. """ eh = mol.CreateEntity() rname = '' edi = eh.EditXCS(mol.BUFFERED_EDIT) chain = edi.InsertChain('A') bond_list = [] # get all atoms and bonds from file with open(filename, 'r') as in_file: for line in in_file: # atom or connectivity # -> fixed column format assumed for both if line.startswith('ATOM'): aname = line[12:17].strip() # extract res. name and ensure uniqueness if not rname: rname = line[17:20].strip() r = edi.AppendResidue(chain, rname) elif rname != line[17:20].strip(): raise RuntimeError("More than one residue in file " + filename +\ ". Cannot parse!") # extract and store type and charge charge = float(line[54:66]) atype = line[78:80].strip() a = edi.InsertAtom(r, aname, geom.Vec3()) a.SetStringProp('type', atype) a.SetCharge(charge) elif line.startswith('CONECT'): ai1 = int(line[6:11]) # max. 4 bond partners... for i in range(4): try: j = 11 + 5 * i ai2 = int(line[j:j + 5]) # only unique bonds added to list s = set([ai1, ai2]) if not s in bond_list: bond_list.append(s) except: # exception thrown for empty strings or non-integers # -> skip continue # set all bonds in entity for indices in bond_list: indices = list(indices) a1 = r.atoms[indices[0] - 1] a2 = r.atoms[indices[1] - 1] edi.Connect(a1, a2) # finalize edi.UpdateICS() return eh, rname
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 _ParseLGAOutput(output, residue_count): result = GDTResult(geom.Mat3(), geom.Vec3(), 0.0, 0.0) found_gdt_section = False found_transform_section = False for index, line in enumerate(output): if line.startswith('GLOBAL_DISTANCE_TEST'): next_lines = output[index + 1:index + 5] result.gdt_ts, result.gdt_ha = _ParseGDTSection( next_lines, residue_count) found_gdt_section = True if line.startswith('Unitary ROTATION matrix'): next_lines = output[index + 1:index + 4] result.rotation, result.shift = _ParseRotationAndShift(next_lines) found_transform_section = True break assert found_transform_section and found_gdt_section return result
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 _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 test_primlist(self): pl=gfx.PrimList("foo") pl.AddPoint([0,0,0]) pl.AddPoint(geom.Vec3(1,2,3),color=gfx.RED) pl.AddLine([0,0,0],[1,2,3]) pl.AddLine(geom.Vec3(0,0,0),geom.Vec3(1,2,3),color=gfx.BLUE) pl.AddSphere([0,0,0],radius=2.0) pl.AddSphere(geom.Vec3(1,2,3),color=gfx.RED,radius=3.0) pl.AddCyl([0,0,0],[1,2,3],radius=0.5,color=gfx.YELLOW) pl.AddCyl(geom.Vec3(0,0,0),geom.Vec3(1,2,3),radius1=0.5,radius2=0.1,color1=gfx.BLUE,color2=gfx.GREEN) pl.AddText("foo",[0,2,3]) pl.AddText("bar",[-2,0,0],color=gfx.WHITE,point_size=8) if has_numpy: pl.AddMesh(numpy.zeros((5,3),dtype=numpy.float32), numpy.zeros((5,3),dtype=numpy.float32), numpy.zeros((5,4),dtype=numpy.float32), numpy.zeros((2,3),dtype=numpy.uint32)) pl.AddMesh(numpy.zeros((7,3),dtype=numpy.float32), None, None, numpy.zeros((4,3),dtype=numpy.uint32)) # Passing wrong data type should fail with self.assertRaises(Exception): pl.AddMesh(numpy.zeros((5, 3), dtype=numpy.uint32), numpy.zeros((5, 3), dtype=numpy.float32), numpy.zeros((5, 4), dtype=numpy.float32), numpy.zeros((2, 3), dtype=numpy.uint32)) with self.assertRaises(Exception): pl.AddMesh(numpy.zeros((5, 3), dtype=numpy.float32), numpy.zeros((5, 3), dtype=numpy.uint32), numpy.zeros((5, 4), dtype=numpy.float32), numpy.zeros((2, 3), dtype=numpy.uint32)) with self.assertRaises(Exception): pl.AddMesh(numpy.zeros((5, 3), dtype=numpy.float32), numpy.zeros((5, 3), dtype=numpy.float32), numpy.zeros((5, 4), dtype=numpy.uint32), numpy.zeros((2, 3), dtype=numpy.uint32)) with self.assertRaises(Exception): pl.AddMesh(numpy.zeros((5, 3), dtype=numpy.float32), numpy.zeros((5, 3), dtype=numpy.float32), numpy.zeros((5, 4), dtype=numpy.float32), numpy.zeros((2, 3), dtype=numpy.float32))
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 testPositionContainer(self): container = LinearPositionContainer() self.assertEqual(container.GetNumElements(), 0) # define a few position lists to play with pos_1 = geom.Vec3List() pos_2 = geom.Vec3List() pos_3 = geom.Vec3List() pos_1.append(geom.Vec3(-10.234, -45.333, -45.338)) pos_1.append(geom.Vec3(10.234, 45.333, 45.338)) pos_1.append(geom.Vec3(10.234, 45.333, 45.338)) pos_1.append(geom.Vec3(10.234, 45.333, 45.338)) pos_1.append(geom.Vec3(10.234, 45.333, 45.338)) pos_1.append(geom.Vec3(10.234, 45.333, 45.338)) pos_2.append(geom.Vec3(50000.34, 32.43, 33.2222)) pos_2.append(geom.Vec3(-50000.34, -32.43, -33.2222)) pos_2.append(geom.Vec3(50000.34, 32.43, 33.2222)) pos_3.append(geom.Vec3(87.909, 65.222, 1000.555)) pos_3.append(geom.Vec3(87.909, 65.222, 1000.555)) # get the rounded target values (the accuracy with which they) # are stored in the container) pos_1_target = geom.Vec3(10.23, 45.33, 45.34) pos_2_target = geom.Vec3(50000.3, 32.4, 33.2) # reduced accuracy! pos_3_target = geom.Vec3(87.91, 65.22, 1000.56) pos_1_target_list = geom.Vec3List() pos_2_target_list = geom.Vec3List() pos_3_target_list = geom.Vec3List() for i in range(6): pos_1_target_list.append(pos_1_target) pos_1_target_list[0] = -pos_1_target_list[0] for i in range(3): pos_2_target_list.append(pos_2_target) pos_2_target_list[1] = -pos_2_target_list[1] pos_3_target_list.append(pos_3_target) pos_3_target_list.append(pos_3_target) # add it container.AddPositions(pos_1) container.AddPositions(pos_2) container.AddPositions(pos_3) extracted_pos_list = geom.Vec3List() extracted_pos = geom.Vec3() # check whether stuff has properly been added self.assertEqual(container.GetNumElements(), len(pos_1) + len(pos_2) + len(pos_3)) container.GetPositions((0, len(pos_1)), extracted_pos_list) self.assertTrue(ComparePosList(pos_1_target_list, extracted_pos_list)) container.GetPositions((len(pos_1), len(pos_1) + len(pos_2)), extracted_pos_list) self.assertTrue(ComparePosList(pos_2_target_list, extracted_pos_list)) container.GetPositions( (len(pos_1) + len(pos_2), len(pos_1) + len(pos_2) + len(pos_3)), extracted_pos_list) self.assertTrue(ComparePosList(pos_3_target_list, extracted_pos_list)) # check whether errors get thrown for invalid ranges dummy_list = geom.Vec3List() dummy_pos = geom.Vec3() self.assertRaises(Exception, container.GetPositions, (5, 5), dummy_list) self.assertRaises(Exception, container.GetPositions, (5, 4), dummy_list) self.assertRaises(Exception, container.GetPositions, (5, 100), dummy_list) self.assertRaises(Exception, container.GetPositions, (100, 120), dummy_list) self.assertRaises(Exception, container.GetPosition, 100, dummy_pos) self.assertRaises(Exception, container.ClearRange, (5, 5)) self.assertRaises(Exception, container.ClearRange, (5, 4)) self.assertRaises(Exception, container.ClearRange, (5, 100)) self.assertRaises(Exception, container.ClearRange, (100, 120)) # lets remove pos_2_list and check, whether the result is as expected container.ClearRange((len(pos_1), len(pos_1) + len(pos_2))) self.assertEqual(container.GetNumElements(), len(pos_1) + len(pos_3)) container.GetPositions((0, len(pos_1)), extracted_pos_list) self.assertTrue(ComparePosList(pos_1_target_list, extracted_pos_list)) container.GetPositions((len(pos_1), len(pos_1) + len(pos_3)), extracted_pos_list) self.assertTrue(ComparePosList(pos_3_target_list, extracted_pos_list)) # check the single position getter pos_13_target_list = geom.Vec3List() for item in pos_1_target_list: pos_13_target_list.append(item) for item in pos_3_target_list: pos_13_target_list.append(item) eps = 0.00001 for i in range(len(pos_13_target_list)): container.GetPosition(i, extracted_pos) self.assertTrue( geom.Distance(extracted_pos, pos_13_target_list[i]) < eps) # save / load and check for equality container.Save("test_position_container.dat") loaded_container = LinearPositionContainer.Load( "test_position_container.dat") self.assertEqual(container, loaded_container) if os.path.exists("test_position_container.dat"): os.remove("test_position_container.dat")
def test_(self): if not has_numpy: return entity = mol.CreateEntity() ed = entity.EditXCS() ch = ed.InsertChain("X") re = ed.AppendResidue(ch, "ALA") a0 = ed.InsertAtom(re, "A", geom.Vec3(0, 0, 0)) self.assertEqual(a0.GetIndex(), 0) a1 = ed.InsertAtom(re, "B", geom.Vec3(1, 0, 0)) self.assertEqual(a1.GetIndex(), 1) a2 = ed.InsertAtom(re, "C", geom.Vec3(2, 0, 0)) self.assertEqual(a2.GetIndex(), 2) a3 = ed.InsertAtom(re, "D", geom.Vec3(3, 0, 0)) self.assertEqual(a3.GetIndex(), 3) self.assertTrue(dd(a0.pos, geom.Vec3(0, 0, 0))) self.assertTrue(dd(a1.pos, geom.Vec3(1, 0, 0))) self.assertTrue(dd(a2.pos, geom.Vec3(2, 0, 0))) self.assertTrue(dd(a3.pos, geom.Vec3(3, 0, 0))) ed.SetAtomTransformedPos( entity.GetAtomList(), numpy.array([[0, 1, 0], [0, 2, 0], [0, 3, 0], [0, 4, 0]], dtype=numpy.float32)) self.assertTrue(dd(a0.pos, geom.Vec3(0, 1, 0))) self.assertTrue(dd(a1.pos, geom.Vec3(0, 2, 0))) self.assertTrue(dd(a2.pos, geom.Vec3(0, 3, 0))) self.assertTrue(dd(a3.pos, geom.Vec3(0, 4, 0))) na = entity.positions self.assertTrue(dd(v2v(na[0]), geom.Vec3(0, 1, 0))) self.assertTrue(dd(v2v(na[1]), geom.Vec3(0, 2, 0))) self.assertTrue(dd(v2v(na[2]), geom.Vec3(0, 3, 0))) self.assertTrue(dd(v2v(na[3]), geom.Vec3(0, 4, 0))) ed.SetAtomTransformedPos([3, 99, 2], numpy.array( [[0, 0, -3], [-1, -1, -1], [0, 0, -2]], dtype=numpy.float32)) self.assertTrue(dd(a0.pos, geom.Vec3(0, 1, 0))) self.assertTrue(dd(a1.pos, geom.Vec3(0, 2, 0))) self.assertTrue(dd(a2.pos, geom.Vec3(0, 0, -2))) self.assertTrue(dd(a3.pos, geom.Vec3(0, 0, -3)))
def v2v(v): return geom.Vec3(float(v[0]), float(v[1]), float(v[2]))
def col_delta(c1,c2): return geom.Distance(geom.Vec3(c1[0],c1[1],c1[2]),geom.Vec3(c2[0],c2[1],c2[2]))