Exemplo n.º 1
0
    def test_from_adjacency_list3(self):
        """
        adjlist: Test the Molecule.from_adjacency_list() method 3.
        """
        # molecule 3
        adjlist = """
1 *1 C u1 {2,S} {3,S} {4,S}
2    H u0 {1,S}
3    H u0 {1,S}
4 *2 N u0 p0 c+1 {1,S} {5,S} {6,D}
5    O u0 p3 c-1 {4,S}
6    O u0 p2 {4,D}
            """
        molecule = Molecule().from_adjacency_list(adjlist)

        self.assertTrue(molecule.multiplicity == 2)

        atom1 = molecule.atoms[0]
        atom2 = molecule.atoms[3]
        atom3 = molecule.atoms[4]
        atom4 = molecule.atoms[5]
        self.assertTrue(molecule.has_bond(atom2, atom1))
        self.assertTrue(molecule.has_bond(atom2, atom3))
        self.assertTrue(molecule.has_bond(atom2, atom4))
        self.assertFalse(molecule.has_bond(atom1, atom3))
        self.assertFalse(molecule.has_bond(atom1, atom4))
        bond21 = atom2.bonds[atom1]
        bond23 = atom2.bonds[atom3]
        bond24 = atom2.bonds[atom4]

        self.assertTrue(atom1.label == '*1')
        self.assertTrue(atom1.element.symbol == 'C')
        self.assertTrue(atom1.radical_electrons == 1)
        self.assertTrue(atom1.charge == 0)

        self.assertTrue(atom2.label == '*2')
        self.assertTrue(atom2.element.symbol == 'N')
        self.assertTrue(atom2.radical_electrons == 0)
        self.assertTrue(atom2.charge == 1)

        self.assertTrue(atom3.label == '')
        self.assertTrue(atom3.element.symbol == 'O')
        self.assertTrue(atom3.radical_electrons == 0)
        self.assertTrue(atom3.charge == -1)

        self.assertTrue(atom4.label == '')
        self.assertTrue(atom4.element.symbol == 'O')
        self.assertTrue(atom4.radical_electrons == 0)
        self.assertTrue(atom4.charge == 0)

        self.assertTrue(bond21.is_single())
        self.assertTrue(bond23.is_single())
        self.assertTrue(bond24.is_double())
Exemplo n.º 2
0
    def test_atom_mapping_2(self):
        """Test that to_rdkit_mol returns correct indices and atom mappings when hydrogens are removed."""
        adjlist = """
1 H u0 p0 c0 {2,S}
2 C u0 p0 c0 {1,S} {3,S} {4,S} {5,S}
3 H u0 p0 c0 {2,S}
4 H u0 p0 c0 {2,S}
5 O u0 p2 c0 {2,S} {6,S}
6 H u0 p0 c0 {5,S}
        """

        mol = Molecule().from_adjacency_list(adjlist)
        rdkitmol, rd_atom_indices = to_rdkit_mol(mol,
                                                 remove_h=True,
                                                 return_mapping=True)

        heavy_atoms = [at for at in mol.atoms if at.number != 1]
        for at1 in heavy_atoms:
            for at2 in heavy_atoms:
                if mol.has_bond(at1, at2):
                    try:
                        rdkitmol.GetBondBetweenAtoms(rd_atom_indices[at1],
                                                     rd_atom_indices[at2])
                    except RuntimeError:
                        self.fail(
                            "RDKit failed in finding the bond in the original atom!"
                        )