def _parse_fragment_tree(self, el): fragments = [] for el_fragments in el.findall('fragments'): for el_f in el_fragments: fragments.append(self._parse_fragment_tree(el_f)) atoms = [] for el_atoms in el.findall('atoms'): for el_a in el_atoms: atom_classes = {"dummy": im.dummy, "unknown": im.unknown, "element": im.element} descr = atom_classes[el_a.get('type')](el_a.get('name')) atoms.append((el_a.get('label'), im.atom(descr, int(el_a.get('nsites', '1'))))) bonds = [] for el_bonds in el.findall('bonds'): for el_b in el_bonds: a1, a2 = el_b.get('atoms').split() bonds.append((a1, a2, el_b.get('order'))) label = el.get('label') species = el.get('species') polymer_type = el.get('polymer_type', None) if polymer_type is None: return label, im.fragment(species, fragments, atoms, bonds) else: assert len(atoms) == 0 return label, im.polymer(species, fragments, bonds, polymer_type)
def test_equality(self): same_mol = make_water_fragment() changed_bond_order = M.fragment("water", (), (("H1", M.atom(M.element("H"))), ("H2", M.atom(M.element("H"))), ("O", M.atom(M.element("O")))), (("O", "H2", "single"), ("O", "H1", "single"))) changed_atom_order = M.fragment("water", (), (("O", M.atom(M.element("O"))), ("H1", M.atom(M.element("H"))), ("H2", M.atom(M.element("H")))), (("O", "H1", "single"), ("O", "H2", "single"))) self.assertEqual(self.mol, self.mol) self.assertEqual(self.mol, same_mol) self.assertEqual(self.mol, changed_bond_order) self.assertNotEqual(self.mol, changed_atom_order)
def test_bonds(self): carbon = M.atom(M.element("C")) # Bond specified by only one atom self.assertRaises(ValueError, lambda: M.fragment('m', (), (('C1', carbon), ('C2', carbon)), (('C1', ),))) # Bond specified by two atoms but no bond order self.assertRaises(ValueError, lambda: M.fragment('m', (), (('C1', carbon), ('C2', carbon)), (('C1', 'C2'),))) # Bond specified by two identical atoms self.assertRaises(ValueError, lambda: M.fragment('m', (), (('C1', carbon), ('C2', carbon)), (('C1', 'C1', ''),))) # Bond specified by an atom name that is undefined self.assertRaises(ValueError, lambda: M.fragment('m', (), (('C1', carbon), ('C2', carbon)), (('C1', 'C3', ''),))) # Bond specified at the wrong fragment level f = M.fragment('x', (), (('C1', carbon), ('C2', carbon)), ()) self.assertRaises(ValueError, lambda: M.fragment('m', (('x', f),), (('C3', carbon),), (('x.C1', 'x.C2', ''),)))
def _make_molecule(self): C = M.element('C') H = M.element('H') N = M.element('N') O = M.element('O') peptide_group = M.fragment('peptide', (), (('CA', M.atom(C)), ('HA', M.atom(H)), ('H', M.atom(H)), ('N', M.atom(N)), ('C', M.atom(C)), ('O', M.atom(O))), (('N', 'H', "single"), ('N', 'CA', "single"), ('CA', 'HA', "single"), ('CA', 'C', "single"), ('C', 'O', "double"))) ala_sidechain = M.fragment('ala_sidechain', (), (('CB', M.atom(C)), ('HB1', M.atom(H)), ('HB2', M.atom(H)), ('HB3', M.atom(H))), (('CB', 'HB1', "single"), ('CB', 'HB2', "single"), ('CB', 'HB3', "single"),)) ala = M.fragment('alanine', (('peptide', peptide_group), ('sidechain', ala_sidechain)), (), (('peptide.CA', 'sidechain.CB', "single"),)) return M.polymer('alanine_dipeptide', (('ALA1', ala), ('ALA2', ala)), (('ALA1.peptide.C', 'ALA2.peptide.N', "single"),), 'polypeptide')
def test_universe(self): mol = M.fragment("water", (), (("H1", M.atom(M.element("H"), 8)), ("H2", M.atom(M.element("H"), 8)), ("O", M.atom(M.element("O"), 2))), (("H1", "O", "single"), ("H2", "O", "single"))) self.assertRaises(TypeError, lambda: M.universe(0, [(mol, 'water', 10)])) self.assertRaises(ValueError, lambda: M.universe('strange', [(mol, 'water', 10)])) self.assertRaises(ValueError, lambda: M.universe('strange', [(mol, 10)])) self.assertRaises(TypeError, lambda: M.universe('infinite', mol)) self.assertRaises(ValueError, lambda: M.universe('infinite', [("water", 10)])) self.assertRaises(TypeError, lambda: M.universe('infinite', [mol])) self.assertRaises(ValueError, lambda: M.universe('infinite', [(10, mol)])) self.assertRaises(ValueError, lambda: M.universe('infinite', [(mol, 'water', 10)], [(IN.zeros((3,3), N.float64), IN.zeros((3,), N.float64))]))
def __init__(self, mode='r'): self.file = h5py.File(self.filename, mode) def close(self): self.file.close() def __enter__(self): return self.file def __exit__(self, type, value, traceback): self.close() return False water = M.fragment("water", (), (("H1", M.atom(M.element("H"), 1)), ("H2", M.atom(M.element("H"), 1)), ("O", M.atom(M.element("O"), 1))), (("H1", "O", "single"), ("H2", "O", "single"))) class WriteReadTest(unittest.TestCase): def setUp(self): TestHDF5File.initialize() def tearDown(self): TestHDF5File.cleanup() def test_infinite(self): universe = M.universe('infinite', [(water, 'water', 5)]) nsites = universe.number_of_sites conf1 = M.Configuration(universe,
def make_water_fragment(nsites=1): return M.fragment("water", (), (("H1", M.atom(M.element("H"), nsites)), ("H2", M.atom(M.element("H"), nsites)), ("O", M.atom(M.element("O"), nsites))), (("H1", "O", "single"), ("H2", "O", "single")))
def test_fragment(self): carbon = M.atom(M.element("C")) # Illegal fragments self.assertRaises(TypeError, lambda: M.fragment('m', None, (("C", carbon),), ())) self.assertRaises(TypeError, lambda: M.fragment('m', [1, 2], (("C", carbon),), ())) self.assertRaises(TypeError, lambda: M.fragment('m', (("C", carbon),), (), ())) # Illegal atoms self.assertRaises(TypeError, lambda: M.fragment('m', (), None, ())) self.assertRaises(TypeError, lambda: M.fragment('m', (), [1, 2], ())) self.assertRaises(TypeError, lambda: M.fragment('m', (), (carbon,), ())) self.assertRaises(ValueError, lambda: M.fragment('m', (), (("C", carbon), ("C", carbon)), ())) # Illegal bond lists self.assertRaises(TypeError, lambda: M.fragment('m', (), (("C", carbon),), None)) self.assertRaises(TypeError, lambda: M.fragment('m', (), (("C", carbon),), [1, 2, 3])) self.assertRaises(TypeError, lambda: M.fragment('m', (), (("C", carbon),), (('X', 'X')))) self.assertRaises(TypeError, lambda: M.fragment('m', (), (("C", carbon),), (['X', 'X', 'single'])))