def test_update_data_file(self): """ Test writing another CIF file """ # Create a initial data file -- # myDataList = [] ofh = open(self.get_fn("test-output-1.cif", written=True), "w") curContainer = DataContainer("myblock") aCat = DataCategory("pdbx_seqtool_mapping_ref") aCat.appendAttribute("ordinal") aCat.appendAttribute("entity_id") aCat.appendAttribute("auth_mon_id") aCat.appendAttribute("auth_mon_num") aCat.appendAttribute("pdb_chain_id") aCat.appendAttribute("ref_mon_id") aCat.appendAttribute("ref_mon_num") aCat.append((1, 2, 3, 4, 5, 6, 7)) aCat.append((1, 2, 3, 4, 5, 6, 7)) aCat.append((1, 2, 3, 4, 5, 6, 7)) aCat.append((1, 2, 3, 4, 5, 6, 7)) curContainer.append(aCat) myDataList.append(curContainer) pdbxW = PdbxWriter(ofh) pdbxW.write(myDataList) ofh.close() self.assertTrue( diff_files(self.get_fn('test-output-1.cif', saved=True), self.get_fn('test-output-1.cif', written=True))) # # Read and update the data - # myDataList = [] ifh = open(self.get_fn("test-output-1.cif", written=True), "r") pRd = PdbxReader(ifh) pRd.read(myDataList) ifh.close() # myBlock = myDataList[0] dest = open(self.get_fn('test_write_1.txt', written=True), 'w') myBlock.printIt(dest) myCat = myBlock.getObj('pdbx_seqtool_mapping_ref') myCat.printIt(dest) dest.close() for iRow in range(0, myCat.getRowCount()): myCat.setValue('some value', 'ref_mon_id', iRow) myCat.setValue(100, 'ref_mon_num', iRow) ofh = open(self.get_fn("test-output-2.cif", written=True), "w") pdbxW = PdbxWriter(ofh) pdbxW.write(myDataList) ofh.close() self.assertTrue( diff_files(self.get_fn('test-output-2.cif', saved=True), self.get_fn('test-output-2.cif', written=True))) self.assertTrue( diff_files(self.get_fn('test_write_1.txt', saved=True), self.get_fn('test_write_1.txt', written=True)))
def testUpdateDataFile(self): """ Test writing another CIF file """ # Create a initial data file -- # myDataList=[] ofh = open(get_fn("test-output-1.cif", written=True), "w") curContainer=DataContainer("myblock") aCat=DataCategory("pdbx_seqtool_mapping_ref") aCat.appendAttribute("ordinal") aCat.appendAttribute("entity_id") aCat.appendAttribute("auth_mon_id") aCat.appendAttribute("auth_mon_num") aCat.appendAttribute("pdb_chain_id") aCat.appendAttribute("ref_mon_id") aCat.appendAttribute("ref_mon_num") aCat.append((1,2,3,4,5,6,7)) aCat.append((1,2,3,4,5,6,7)) aCat.append((1,2,3,4,5,6,7)) aCat.append((1,2,3,4,5,6,7)) curContainer.append(aCat) myDataList.append(curContainer) pdbxW=PdbxWriter(ofh) pdbxW.write(myDataList) ofh.close() self.assertTrue(diff_files(get_saved_fn('test-output-1.cif'), get_fn('test-output-1.cif', written=True))) # # Read and update the data - # myDataList=[] ifh = open(get_fn("test-output-1.cif", written=True), "r") pRd=PdbxReader(ifh) pRd.read(myDataList) ifh.close() # myBlock=myDataList[0] dest = open(get_fn('test_write_1.txt', written=True), 'w') myBlock.printIt(dest) myCat=myBlock.getObj('pdbx_seqtool_mapping_ref') myCat.printIt(dest) dest.close() for iRow in range(0,myCat.getRowCount()): myCat.setValue('some value', 'ref_mon_id',iRow) myCat.setValue(100, 'ref_mon_num',iRow) ofh = open(get_fn("test-output-2.cif", written=True), "w") pdbxW=PdbxWriter(ofh) pdbxW.write(myDataList) ofh.close() self.assertTrue(diff_files(get_saved_fn('test-output-2.cif'), get_fn('test-output-2.cif', written=True))) self.assertTrue(diff_files(get_saved_fn('test_write_1.txt'), get_fn('test_write_1.txt', written=True)))
def test_ala_gas(self): """ Test for alanine dipeptide in gas-phase """ parm = get_fn('ala_gas.prmtop') rst7 = get_fn('ala_gas.rst7') output1 = self.get_fn('ala_gas.field', written=True) output2 = self.get_fn('ala_gas.config', written=True) amber = parmed.load_file(parm,rst7) amber.save(output1) self.assertTrue( diff_files(self.get_fn('ala_gas.field', saved=True), output1, absolute_error=1e-6) ) amber.save(output2) self.assertTrue( diff_files(self.get_fn('ala_gas.config', saved=True), output2, absolute_error=1e-6) )
def test_ksi_solv(self): """ Test for ksi protein in solution-phase """ parm = get_fn('ksi_solv.prmtop') rst7 = get_fn('ksi_solv.rst7') output1 = self.get_fn('ksi_solv.field', written=True) output2 = self.get_fn('ksi_solv.config', written=True) amber = parmed.load_file(parm,rst7) amber.save(output1) self.assertTrue( diff_files(self.get_fn('ksi_solv.field', saved=True), output1, absolute_error=1e-6) ) amber.save(output2) self.assertTrue( diff_files(self.get_fn('ksi_solv.config', saved=True), output2, absolute_error=1e-6) )
def testMol3SingleWrite(self): """ Tests writing mol3 file of single ResidueTemplate """ mol2 = formats.Mol2File.parse(get_fn('tripos9.mol2')) formats.Mol2File.write(mol2, get_fn('tripos9.mol3', written=True), mol3=True) self.assertTrue(diff_files(get_fn('tripos9.mol3', written=True), get_saved_fn('tripos9.mol3')))
def test_write_data_file(self): """ Test writing CIF file """ myDataList = [] ofh = open(self.get_fn("test-output.cif", written=True), "w") curContainer = DataContainer("myblock") aCat = DataCategory("pdbx_seqtool_mapping_ref") aCat.appendAttribute("ordinal") aCat.appendAttribute("entity_id") aCat.appendAttribute("auth_mon_id") aCat.appendAttribute("auth_mon_num") aCat.appendAttribute("pdb_chain_id") aCat.appendAttribute("ref_mon_id") aCat.appendAttribute("ref_mon_num") aCat.append((1, 2, 3, 4, 5, 6, 7)) aCat.append((1, 2, 3, 4, 5, 6, 7)) aCat.append((1, 2, 3, 4, 5, 6, 7)) aCat.append((1, 2, 3, 4, 5, 6, 7)) curContainer.append(aCat) myDataList.append(curContainer) pdbxW = PdbxWriter(ofh) pdbxW.write(myDataList) ofh.close() self.assertTrue( diff_files( self.get_fn('test-output.cif', saved=True), self.get_fn('test-output.cif', written=True), ))
def testMol3MultiWriteFromStructure(self): """ Tests writing mol3 file of multi residues from Structure """ mol2 = formats.Mol2File.parse(get_fn('test_multi.mol2'), structure=True) formats.Mol2File.write(mol2, get_fn('test_multistruct.mol3', written=True), mol3=True) self.assertTrue(diff_files(get_fn('test_multistruct.mol3', written=True), get_saved_fn('test_multistruct.mol3')))
def testMol3SingleWriteStruct(self): """ Tests writing mol3 file of single-residue Structure """ mol2 = formats.Mol2File.parse(get_fn('tripos9.mol2'), structure=True) formats.Mol2File.write(mol2, get_fn('tripos9struct.mol3', written=True), mol3=True) self.assertTrue(diff_files(get_fn('tripos9struct.mol3', written=True), get_saved_fn('tripos9struct.mol3')))
def testMol2MultiWrite(self): """ Tests writing mol2 file of multi residues from ResidueTemplateContainer """ mol2 = formats.Mol2File.parse(get_fn('test_multi.mol2')) formats.Mol2File.write(mol2, get_fn('test_multi.mol2', written=True)) self.assertTrue(diff_files(get_fn('test_multi.mol2', written=True), get_saved_fn('test_multi.mol2')))
def test_ksi_gas(self): """ Test for ksi protein in gas-phase """ parm = get_fn('ksi_gas.prmtop') rst7 = get_fn('ksi_gas.rst7') output1 = get_fn('ksi_gas.field', written=True) output2 = get_fn('ksi_gas.config', written=True) amber = parmed.load_file(parm, rst7) amber.save(output1) self.assertTrue( diff_files(get_saved_fn('ksi_gas.field'), output1, absolute_error=1e-6)) amber.save(output2) self.assertTrue( diff_files(get_saved_fn('ksi_gas.config'), output2, absolute_error=1e-6))
def test_simple(self): """ Tests converting simple Amber system to CHARMM PSF/parameters """ parm = load_file(self.get_fn('trx.prmtop'), self.get_fn('trx.inpcrd')) parm.save(self.get_fn('amber_to_charmm.psf', written=True)) params = charmm.CharmmParameterSet.from_structure(parm) params.write(stream=self.get_fn('amber_to_charmm.str', written=True)) self.assertTrue( diff_files( self.get_fn('amber_to_charmm.psf', saved=True), self.get_fn('amber_to_charmm.psf', written=True), ), ) self.assertTrue( diff_files( self.get_fn('amber_to_charmm.str', saved=True), self.get_fn('amber_to_charmm.str', written=True), absolute_error=1e-5, ), ) # Check the PSF file psf = load_file(self.get_fn('amber_to_charmm.psf', written=True)) psf.load_parameters( charmm.CharmmParameterSet(self.get_fn('amber_to_charmm.str', written=True)) ) for a1, a2 in zip(psf.atoms, parm.atoms): self.assertEqual(a1.name, a2.name) self.assertEqual(a1.atomic_number, a2.atomic_number) self.assertEqual(a1.mass, a2.mass) self.assertEqual(len(psf.bonds), len(parm.bonds)) self.assertEqual(len(psf.angles), len(parm.angles)) # Get number of unique torsions nnormal = nimp = 0 torsfound = set() for tor in parm.dihedrals: a1, a2, a3, a4 = tor.atom1, tor.atom2, tor.atom3, tor.atom4 if tor.improper: nimp += 1 continue if (a1, a2, a3, a4) in torsfound or (a4, a3, a2, a1) in torsfound: continue torsfound.add((a1, a2, a3, a4)) nnormal += 1 # Make sure that written psf only contains unique torsions. self.assertEqual(nnormal + nimp, len(psf.dihedrals))
def test_simple(self): """ Tests converting simple Amber system to CHARMM PSF/parameters """ parm = load_file(get_fn('trx.prmtop'), get_fn('trx.inpcrd')) parm.save(get_fn('amber_to_charmm.psf', written=True)) params = charmm.CharmmParameterSet.from_structure(parm) params.write(str=get_fn('amber_to_charmm.str', written=True)) self.assertTrue( diff_files(get_saved_fn('amber_to_charmm.psf'), get_fn('amber_to_charmm.psf', written=True) ) ) self.assertTrue( diff_files(get_saved_fn('amber_to_charmm.str'), get_fn('amber_to_charmm.str', written=True), absolute_error=1e-5, ) ) # Check the PSF file psf = load_file(get_fn('amber_to_charmm.psf', written=True)) psf.load_parameters( charmm.CharmmParameterSet(get_fn('amber_to_charmm.str', written=True)) ) for a1, a2 in zip(psf.atoms, parm.atoms): self.assertEqual(a1.name, a2.name) self.assertEqual(a1.atomic_number, a2.atomic_number) self.assertEqual(a1.mass, a2.mass) self.assertEqual(len(psf.bonds), len(parm.bonds)) self.assertEqual(len(psf.angles), len(parm.angles)) # Get number of unique torsions nnormal = nimp = 0 torsfound = set() for tor in parm.dihedrals: a1, a2, a3, a4 = tor.atom1, tor.atom2, tor.atom3, tor.atom4 if tor.improper: nimp += 1 continue if (a1, a2, a3, a4) in torsfound or (a4, a3, a2, a1) in torsfound: continue torsfound.add((a1, a2, a3, a4)) nnormal += 1 # Make sure that written psf only contains unique torsions. self.assertEqual(nnormal+nimp, len(psf.dihedrals))
def test_chamber(self): """ Tests converting standard Gromacs system into Chamber prmtop """ top = load_file(get_fn('1aki.charmm27.solv.top'), xyz=get_fn('1aki.charmm27.solv.gro')) parm = amber.ChamberParm.from_structure(top) parm.write_parm(get_fn('1aki.charmm27_fromgmx.parm7', written=True)) self.assertTrue(diff_files(get_fn('1aki.charmm27_fromgmx.parm7', written=True), get_saved_fn('1aki.charmm27_fromgmx.parm7'), relative_error=1e-8) )
def testGeometricCombiningRule(self): """ Tests converting geom. comb. rule from Gromacs to Amber """ top = load_file(os.path.join(get_fn('05.OPLS'), 'topol.top'), xyz=os.path.join(get_fn('05.OPLS'), 'conf.gro')) self.assertEqual(top.combining_rule, 'geometric') del top.rb_torsions[:] parm = amber.AmberParm.from_structure(top) self.assertEqual(parm.combining_rule, 'geometric') parm.write_parm(get_fn('opls.parm7', written=True)) self.assertTrue(diff_files(get_fn('opls.parm7', written=True), get_saved_fn('opls.parm7')) )
def test_chamber(self): """ Tests converting standard Gromacs system into Chamber prmtop """ fn = self.get_fn('1aki.charmm27_fromgmx.parm7', written=True) top = load_file(self.get_fn('1aki.charmm27.solv.top'), xyz=self.get_fn('1aki.charmm27.solv.gro')) self.assertGreater(len(top.urey_bradleys), 0) self.assertGreater(len(top.urey_bradley_types), 0) parm = amber.ChamberParm.from_structure(top) parm.write_parm(fn) self.assertTrue(diff_files(fn, self.get_fn('1aki.charmm27_fromgmx.parm7', saved=True), relative_error=1e-8)) parm.fill_LJ() self.assertTrue(0 in parm.LJ_14_radius) self.assertTrue(0 in parm.LJ_14_depth)
def testMol2MultiWrite(self): """ Tests writing mol2 file of multi residues from ResidueTemplateContainer """ mol2 = formats.Mol2File.parse(get_fn('test_multi.mol2')) formats.Mol2File.write(mol2, get_fn('test_multi.mol2', written=True)) formats.Mol2File.write(mol2, get_fn('test_multi_sep.mol2', written=True), split=True) self.assertTrue(diff_files(get_saved_fn('test_multi.mol2'), get_fn('test_multi.mol2', written=True))) self.assertTrue(diff_files(get_saved_fn('test_multi_sep.mol2'), get_fn('test_multi_sep.mol2', written=True))) mol22 = formats.Mol2File.parse(get_fn('test_multi_sep.mol2', written=True)) self.assertEqual(len(mol2), len(mol22)) self.assertEqual([r.name for r in mol2], [r.name for r in mol22]) for r1, r2 in zip(mol2, mol22): self.assertEqual(len(r1.bonds), len(r2.bonds)) self.assertEqual(len(r1.atoms), len(r2.atoms)) self.assertFalse(r1.head is None and r1.tail is None) self.assertTrue(r2.head is None and r2.tail is None) f = StringIO() formats.Mol2File.write(mol2, f, mol3=True, split=True) f.seek(0) mol3 = formats.Mol2File.parse(f) self.assertEqual(len(mol2), len(mol3)) self.assertEqual([r.name for r in mol2], [r.name for r in mol3]) for r1, r2 in zip(mol2, mol3): self.assertEqual(len(r1.bonds), len(r2.bonds)) self.assertEqual(len(r1.atoms), len(r2.atoms)) self.assertFalse(r1.head is None and r1.tail is None) self.assertFalse(r2.head is None and r2.tail is None) if r1.head is None: self.assertIs(r2.head, None) else: self.assertEqual(r1.head.name, r2.head.name) if r1.tail is None: self.assertIs(r2.tail, None) else: self.assertEqual(r1.tail.name, r2.tail.name)
def test_chamber(self): """ Tests converting standard Gromacs system into Chamber prmtop """ fn = get_fn('1aki.charmm27_fromgmx.parm7') top = load_file(get_fn('1aki.charmm27.solv.top'), xyz=get_fn('1aki.charmm27.solv.gro')) parm = amber.ChamberParm.from_structure(top) parm.write_parm(fn) self.assertTrue( diff_files(fn, get_saved_fn('1aki.charmm27_fromgmx.parm7'), relative_error=1e-8) ) parm.fill_LJ() self.assertTrue(0 in parm.LJ_14_radius) self.assertTrue(0 in parm.LJ_14_depth)
def test_geometric_combining_rule(self): """ Tests converting geom. comb. rule from Gromacs to Amber """ top = load_file(os.path.join(get_fn('05.OPLS'), 'topol.top'), xyz=os.path.join(get_fn('05.OPLS'), 'conf.gro')) self.assertEqual(top.combining_rule, 'geometric') del top.rb_torsions[:] parm = amber.AmberParm.from_structure(top) parm.box = None # Get rid of the unit cell self.assertEqual(parm.combining_rule, 'geometric') parm.write_parm(get_fn('opls.parm7', written=True)) self.assertTrue(diff_files(get_fn('opls.parm7', written=True), get_saved_fn('opls.parm7')) ) # Make sure recalculate_LJ works acoef = np.array(parm.parm_data['LENNARD_JONES_ACOEF']) bcoef = np.array(parm.parm_data['LENNARD_JONES_BCOEF']) parm.recalculate_LJ() np.testing.assert_allclose(acoef, parm.parm_data['LENNARD_JONES_ACOEF']) np.testing.assert_allclose(bcoef, parm.parm_data['LENNARD_JONES_BCOEF'])
def test_benzene_cyclohexane(self): """ Test converting binary liquid from Amber prmtop to Gromacs top """ parm = load_file( self.get_fn('benzene_cyclohexane_10_500.prmtop'), self.get_fn('benzene_cyclohexane_10_500.inpcrd'), ) top = gromacs.GromacsTopologyFile.from_structure(parm) self.assertEqual(top.combining_rule, 'lorentz') groname = self.get_fn('benzene_cyclohexane_10_500.gro', written=True) gromacs.GromacsGroFile.write(parm, groname, precision=8) gro = gromacs.GromacsGroFile.parse(groname) self.assertEqual(len(gro.atoms), len(parm.atoms)) np.testing.assert_allclose(gro.box, parm.box) for a1, a2 in zip(gro.atoms, parm.atoms): self.assertEqual(a1.residue.name, a2.residue.name) self.assertEqual(a1.residue.idx, a2.residue.idx) self.assertEqual(a1.name, a2.name) self.assertAlmostEqual(a1.xx, a2.xx) self.assertAlmostEqual(a1.xy, a2.xy) self.assertAlmostEqual(a1.xz, a2.xz) top.write(self.get_fn('benzene_cyclohexane_10_500.top', written=True)) saved = GromacsFile( self.get_fn('benzene_cyclohexane_10_500.top', saved=True)) written = GromacsFile( self.get_fn('benzene_cyclohexane_10_500.top', written=True)) self.assertTrue(diff_files(saved, written)) # Check that Gromacs topology is given the correct box information when # generated from a Structure gromacs.GromacsGroFile.write(top, groname, precision=8) gro = gromacs.GromacsGroFile.parse(groname) self.assertEqual(len(gro.atoms), len(parm.atoms)) for a1, a2 in zip(gro.atoms, parm.atoms): self.assertEqual(a1.residue.name, a2.residue.name) self.assertEqual(a1.residue.idx, a2.residue.idx) self.assertEqual(a1.name, a2.name) self.assertAlmostEqual(a1.xx, a2.xx) self.assertAlmostEqual(a1.xy, a2.xy) self.assertAlmostEqual(a1.xz, a2.xz) np.testing.assert_allclose(gro.box, parm.box) np.testing.assert_allclose(top.box, parm.box)
def testReadWriteHighPrecisionGroFile(self): """ Tests reading/writing high-precision GRO files """ gro = GromacsGroFile.parse(get_fn('1aki.ff99sbildn.gro')) GromacsGroFile.write(gro, get_fn('1aki.ff99sbildn_highprec.gro', written=True), precision=6) self.assertTrue(diff_files(get_saved_fn('1aki.ff99sbildn_highprec.gro'), get_fn('1aki.ff99sbildn_highprec.gro', written=True) ) ) gro2 = GromacsGroFile.parse(get_fn('1aki.ff99sbildn_highprec.gro', written=True)) gro3 = load_file(get_fn('1aki.ff99sbildn_highprec.gro', written=True)) self.assertIsInstance(gro3, Structure) for a1, a2, a3 in zip(gro.atoms, gro2.atoms, gro3.atoms): self.assertEqual(a1.name, a2.name) self.assertEqual(a3.name, a2.name) self.assertEqual(a1.xx, a2.xx) self.assertEqual(a3.xx, a2.xx) self.assertEqual(a1.xy, a2.xy) self.assertEqual(a3.xy, a2.xy) self.assertEqual(a1.xz, a2.xz) self.assertEqual(a3.xz, a2.xz)
def testWriteDataFile(self): """ Test writing CIF file """ myDataList=[] ofh = open(get_fn("test-output.cif", written=True), "w") curContainer=DataContainer("myblock") aCat=DataCategory("pdbx_seqtool_mapping_ref") aCat.appendAttribute("ordinal") aCat.appendAttribute("entity_id") aCat.appendAttribute("auth_mon_id") aCat.appendAttribute("auth_mon_num") aCat.appendAttribute("pdb_chain_id") aCat.appendAttribute("ref_mon_id") aCat.appendAttribute("ref_mon_num") aCat.append((1,2,3,4,5,6,7)) aCat.append((1,2,3,4,5,6,7)) aCat.append((1,2,3,4,5,6,7)) aCat.append((1,2,3,4,5,6,7)) curContainer.append(aCat) myDataList.append(curContainer) pdbxW=PdbxWriter(ofh) pdbxW.write(myDataList) ofh.close() self.assertTrue(diff_files(get_saved_fn('test-output.cif'), get_fn('test-output.cif', written=True)))
def test_benzene_cyclohexane(self): """ Test converting binary liquid from Amber prmtop to Gromacs top """ parm = load_file(get_fn('benzene_cyclohexane_10_500.prmtop'), get_fn('benzene_cyclohexane_10_500.inpcrd')) top = gromacs.GromacsTopologyFile.from_structure(parm) self.assertEqual(top.combining_rule, 'lorentz') groname = get_fn('benzene_cyclohexane_10_500.gro', written=True) gromacs.GromacsGroFile.write(parm, groname, precision=8) gro = gromacs.GromacsGroFile.parse(groname) self.assertEqual(len(gro.atoms), len(parm.atoms)) np.testing.assert_allclose(gro.box, parm.box) for a1, a2 in zip(gro.atoms, parm.atoms): self.assertEqual(a1.residue.name, a2.residue.name) self.assertEqual(a1.residue.idx, a2.residue.idx) self.assertEqual(a1.name, a2.name) self.assertAlmostEqual(a1.xx, a2.xx) self.assertAlmostEqual(a1.xy, a2.xy) self.assertAlmostEqual(a1.xz, a2.xz) top.write(get_fn('benzene_cyclohexane_10_500.top', written=True)) saved = GromacsFile(get_saved_fn('benzene_cyclohexane_10_500.top')) written = GromacsFile(get_fn('benzene_cyclohexane_10_500.top', written=True)) self.assertTrue(diff_files(saved, written)) # Check that Gromacs topology is given the correct box information when # generated from a Structure gromacs.GromacsGroFile.write(top, groname, precision=8) gro = gromacs.GromacsGroFile.parse(groname) self.assertEqual(len(gro.atoms), len(parm.atoms)) for a1, a2 in zip(gro.atoms, parm.atoms): self.assertEqual(a1.residue.name, a2.residue.name) self.assertEqual(a1.residue.idx, a2.residue.idx) self.assertEqual(a1.name, a2.name) self.assertAlmostEqual(a1.xx, a2.xx) self.assertAlmostEqual(a1.xy, a2.xy) self.assertAlmostEqual(a1.xz, a2.xz) np.testing.assert_allclose(gro.box, parm.box) np.testing.assert_allclose(top.box, parm.box)
def testPDBWriteFormat(self): """ Test PDB atom names are properly justified per PDB standard """ pdbfile = read_PDB(self.format_test) f = get_fn('pdb_format_test.pdb', written=True) pdbfile.write_pdb(f, write_anisou=True) self.assertTrue(diff_files(get_saved_fn('SCM_A_formatted.pdb'), f))