def test_nn_orders(self): strat = OpenBabelNN() acetylene = strat.get_nn_info(self.acetylene, 0) self.assertEqual(acetylene[0]["weight"], 3) self.assertEqual(acetylene[1]["weight"], 1) # Currently, benzene bonds register either as double or single, # not aromatic # Instead of searching for aromatic bonds, we check that bonds are # detected in the same way from both sides self.assertEqual(strat.get_nn_info(self.benzene, 0)[0]["weight"], strat.get_nn_info(self.benzene, 1)[0]["weight"])
def test_nn_orders(self): strat = OpenBabelNN() acetylene = strat.get_nn_info(self.acetylene, 0) self.assertEqual(acetylene[0]["weight"], 3) self.assertEqual(acetylene[1]["weight"], 1) # Currently, benzene bonds register either as double or single, # not aromatic # Instead of searching for aromatic bonds, we check that bonds are # detected in the same way from both sides self.assertEqual(strat.get_nn_info(self.benzene, 0)[0]["weight"], strat.get_nn_info(self.benzene, 1)[0]["weight"])
def test_nn_length(self): strat = OpenBabelNN(order=False) benzene_bonds = strat.get_nn_info(self.benzene, 0) c_bonds = [b for b in benzene_bonds if str(b["site"].specie) == "C"] h_bonds = [b for b in benzene_bonds if str(b["site"].specie) == "H"] self.assertAlmostEqual(c_bonds[0]["weight"], 1.41, 2) self.assertAlmostEqual(h_bonds[0]["weight"], 1.02, 2) self.assertAlmostEqual( strat.get_nn_info(self.acetylene, 0)[0]["weight"], 1.19, 2)
def test_nn_length(self): strat = OpenBabelNN(order=False) benzene_bonds = strat.get_nn_info(self.benzene, 0) c_bonds = [b for b in benzene_bonds if str(b["site"].specie) == "C"] h_bonds = [b for b in benzene_bonds if str(b["site"].specie) == "H"] self.assertAlmostEqual(c_bonds[0]["weight"], 1.41, 2) self.assertAlmostEqual(h_bonds[0]["weight"], 1.02, 2) self.assertAlmostEqual(strat.get_nn_info(self.acetylene, 0)[0]["weight"], 1.19, 2)
def get_basic_functional_groups(self, func_groups=None): """ Identify functional groups that cannot be identified by the Ertl method of get_special_carbon and get_heteroatoms, such as benzene rings, methyl groups, and ethyl groups. TODO: Think of other functional groups that are important enough to be added (ex: do we need ethyl, butyl, propyl?) :param func_groups: List of strs representing the functional groups of interest. Default to None, meaning that all of the functional groups defined in this function will be sought. :return: list of sets of ints, representing groups of connected atoms """ strat = OpenBabelNN() hydrogens = { n for n in self.molgraph.graph.nodes if str(self.species[n]) == "H" } carbons = [ n for n in self.molgraph.graph.nodes if str(self.species[n]) == "C" ] if func_groups is None: func_groups = ["methyl", "phenyl"] results = [] if "methyl" in func_groups: for node in carbons: neighbors = strat.get_nn_info(self.molecule, node) hs = { n["site_index"] for n in neighbors if n["site_index"] in hydrogens } # Methyl group is CH3, but this will also catch methane if len(hs) >= 3: hs.add(node) results.append(hs) if "phenyl" in func_groups: rings_indices = [ set(sum(ring, ())) for ring in self.molgraph.find_rings() ] possible_phenyl = [r for r in rings_indices if len(r) == 6] for ring in possible_phenyl: # Phenyl group should have only one (0 for benzene) member whose # neighbors are not two carbons and one hydrogen num_deviants = 0 for node in ring: neighbors = strat.get_nn_info(self.molecule, node) neighbor_spec = sorted([ str(self.species[n["site_index"]]) for n in neighbors ]) if neighbor_spec != ["C", "C", "H"]: num_deviants += 1 if num_deviants <= 1: for node in ring: ring_group = copy.deepcopy(ring) neighbors = self.molgraph.graph[node] # Add hydrogens to the functional group for neighbor in neighbors.keys(): if neighbor in hydrogens: ring_group.add(neighbor) results.append(ring_group) return results
def get_basic_functional_groups(self, func_groups=None): """ Identify functional groups that cannot be identified by the Ertl method of get_special_carbon and get_heteroatoms, such as benzene rings, methyl groups, and ethyl groups. TODO: Think of other functional groups that are important enough to be added (ex: do we need ethyl, butyl, propyl?) :param func_groups: List of strs representing the functional groups of interest. Default to None, meaning that all of the functional groups defined in this function will be sought. :return: list of sets of ints, representing groups of connected atoms """ strat = OpenBabelNN() hydrogens = {n for n in self.molgraph.graph.nodes if str(self.species[n]) == "H"} carbons = [n for n in self.molgraph.graph.nodes if str(self.species[n]) == "C"] if func_groups is None: func_groups = ["methyl", "phenyl"] results = [] if "methyl" in func_groups: for node in carbons: neighbors = strat.get_nn_info(self.molecule, node) hs = {n["site_index"] for n in neighbors if n["site_index"] in hydrogens} # Methyl group is CH3, but this will also catch methane if len(hs) >= 3: hs.add(node) results.append(hs) if "phenyl" in func_groups: rings_indices = [set(sum(ring, ())) for ring in self.molgraph.find_rings()] possible_phenyl = [r for r in rings_indices if len(r) == 6] for ring in possible_phenyl: # Phenyl group should have only one (0 for benzene) member whose # neighbors are not two carbons and one hydrogen num_deviants = 0 for node in ring: neighbors = strat.get_nn_info(self.molecule, node) neighbor_spec = sorted([str(self.species[n["site_index"]]) for n in neighbors]) if neighbor_spec != ["C", "C", "H"]: num_deviants += 1 if num_deviants <= 1: for node in ring: neighbors = self.molgraph.graph[node] # Add hydrogens to the functional group for neighbor in neighbors.keys(): if neighbor in hydrogens: ring.add(neighbor) results.append(ring) return results