def test_mcf_graphs_no_match(self): from mogli import Molecule, LGFIOConfig, maximal_common_fragments, GenerationType mol1, mol2 = Molecule(), Molecule() config = LGFIOConfig('label', 'atomType') mol1.read_lgf(ETHANE_1) mol2.read_lgf(GENERIC_1, config) t1, frag_noopt, _, _ = maximal_common_fragments( mol1, mol2, 1, TIMEOUT, GenerationType.NO_OPT) t2, frag_deg1, _, _ = maximal_common_fragments(mol1, mol2, 1, TIMEOUT, GenerationType.DEG_1) t3, frag_uncon, _, _ = maximal_common_fragments( mol1, mol2, 1, TIMEOUT, GenerationType.UNCON) t4, frag_uncon_deg1, _, _ = maximal_common_fragments( mol1, mol2, 1, TIMEOUT, GenerationType.UNCON_DEG_1) self.assertTrue(t1) self.assertTrue(t2) self.assertTrue(t3) self.assertTrue(t4) self.assertEqual(len(frag_noopt), 0) self.assertEqual(len(frag_deg1), 0) self.assertEqual(len(frag_uncon), 0) self.assertEqual(len(frag_uncon_deg1), 0)
def test_pack_canonization(self): from mogli import Molecule, Canonization, pack_canonization, unpack_canonization mol = Molecule() mol.read_lgf(ETHANE_1) c1 = Canonization(mol) c2 = unpack_canonization(pack_canonization(c1)) self.assertEqual(c1.get_colors(), c2.get_colors()) self.assertEqual(c1.get_canonization(), c2.get_canonization()) self.assertEqual(c1.get_node_order(), c2.get_node_order())
def test_atomic_fragments(self): from mogli import Molecule, atomic_fragments mol = Molecule() mol.read_lgf(ETHANE_1) frag0, _ = atomic_fragments(mol, 0) frag1, _ = atomic_fragments(mol, 1) frag2, _ = atomic_fragments(mol, 2) self.assertEqual(len(frag0), 8) self.assertEqual(len(frag1), 8) self.assertEqual(len(frag2), 8)
def test_pack_fragment(self): from mogli import Molecule, GenerationType, maximal_common_fragments, pack_fragment, unpack_fragment mol1 = Molecule() mol2 = Molecule() mol1.read_lgf(ETHANE_1) mol2.read_lgf(ETHANE_2) t1, frag, _, _ = maximal_common_fragments(mol1, mol2, 1, TIMEOUT, GenerationType.NO_OPT) self.assertTrue(t1) frag = sorted(frag, key=lambda x: x.get_atom_count(), reverse=True) f1 = frag[0] f2 = unpack_fragment(pack_fragment(f1)) self.assertEqual(f1.get_atom_count(), f2.get_atom_count()) self.assertEqual(f1.get_core_atom_count(), f2.get_core_atom_count()) def s(e): id1 = f1.get_id(f1.get_u(e)) id2 = f1.get_id(f1.get_v(e)) return (id1, id2) if id1 < id2 else (id2, id1) edges1 = sorted([s(e) for e in f1.get_edge_iter()]) edges2 = sorted([s(e) for e in f2.get_edge_iter()]) self.assertEqual(edges1, edges2) for v1 in f1.get_node_iter(): v2 = f2.get_node_by_id(f1.get_id(v1)) self.assertEqual(f1.is_core(v1), f2.is_core(v2))
def test_pack_fcanonization(self): from mogli import Molecule, GenerationType, maximal_common_fragments, \ FragmentCanonization, pack_fcanonization, unpack_fcanonization mol1 = Molecule() mol2 = Molecule() mol1.read_lgf(ETHANE_1) mol2.read_lgf(ETHANE_2) t1, frag, _, _ = maximal_common_fragments(mol1, mol2, 1, TIMEOUT, GenerationType.NO_OPT) self.assertTrue(t1) frag = sorted(frag, key=lambda x: x.get_atom_count(), reverse=True) f1 = FragmentCanonization(frag[0]) foo = pack_fcanonization(f1) f2 = unpack_fcanonization(foo) self.assertEqual(f1.get_colors(), f2.get_colors()) self.assertEqual(f1.get_canonization(), f2.get_canonization()) self.assertEqual(f1.get_node_order(), f2.get_node_order()) self.assertEqual(f1.get_core_nodes(), f2.get_core_nodes())
def test_mcf_subisomorphic_graphs_2(self): from mogli import Molecule, maximal_common_fragments, GenerationType mol1, mol2 = Molecule(), Molecule() mol1.read_lgf(ETHYL) mol2.read_lgf(ETHANE_2) t1, frag_noopt, _, _ = maximal_common_fragments( mol1, mol2, 1, TIMEOUT, GenerationType.NO_OPT) t2, frag_deg1, _, _ = maximal_common_fragments(mol1, mol2, 1, TIMEOUT, GenerationType.DEG_1) t3, frag_uncon, _, _ = maximal_common_fragments( mol1, mol2, 1, TIMEOUT, GenerationType.UNCON) t4, frag_uncon_deg1, _, _ = maximal_common_fragments( mol1, mol2, 1, TIMEOUT, GenerationType.UNCON_DEG_1) self.assertTrue(t1) self.assertTrue(t2) self.assertTrue(t3) self.assertTrue(t4) self.assertTrue(len(frag_noopt) > 0) self.assertTrue(len(frag_deg1) > 0) self.assertTrue(len(frag_uncon) > 0) self.assertTrue(len(frag_uncon_deg1) > 0) frag_noopt = sorted(frag_noopt, key=lambda x: x.get_atom_count(), reverse=True) frag_deg1 = sorted(frag_deg1, key=lambda x: x.get_atom_count(), reverse=True) frag_uncon = sorted(frag_uncon, key=lambda x: x.get_atom_count(), reverse=True) frag_uncon_deg1 = sorted(frag_uncon_deg1, key=lambda x: x.get_atom_count(), reverse=True) self.assertEqual(frag_noopt[0].get_atom_count(), 2) self.assertEqual(frag_deg1[0].get_atom_count(), 2) self.assertEqual(frag_uncon[0].get_atom_count(), 2) self.assertEqual(frag_uncon_deg1[0].get_atom_count(), 2)
def test_subgraph_isomorphism(self): from mogli import Molecule, are_subgraph_isomorphic mol1 = Molecule() mol2 = Molecule() mol1.read_lgf(ETHYL) mol2.read_lgf(ETHANE_2) self.assertTrue(are_subgraph_isomorphic(mol1, mol2)[0]) self.assertFalse(are_subgraph_isomorphic(mol2, mol1)[0])
def test_mcf_isomorphic_graphs_max(self): from mogli import Molecule, maximal_common_fragments, GenerationType mol1, mol2 = Molecule(), Molecule() mol1.read_lgf(ETHANE_1) mol2.read_lgf(ETHANE_2) t1, frag_noopt, _, _ = maximal_common_fragments(mol1, mol2, 1, TIMEOUT, GenerationType.NO_OPT, maximum=True) t2, frag_deg1, _, _ = maximal_common_fragments(mol1, mol2, 1, TIMEOUT, GenerationType.DEG_1, maximum=True) t3, frag_uncon, _, _ = maximal_common_fragments(mol1, mol2, 1, TIMEOUT, GenerationType.UNCON, maximum=True) t4, frag_uncon_deg1, _, _ = maximal_common_fragments( mol1, mol2, 1, TIMEOUT, GenerationType.UNCON_DEG_1, maximum=True) self.assertTrue(t1) self.assertTrue(t2) self.assertTrue(t3) self.assertTrue(t4) self.assertTrue(len(frag_noopt) > 0) self.assertTrue(len(frag_deg1) > 0) self.assertTrue(len(frag_uncon) > 0) self.assertTrue(len(frag_uncon_deg1) > 0) for f in frag_noopt: self.assertEqual(f.get_atom_count(), mol1.get_atom_count()) for f in frag_deg1: self.assertEqual(f.get_atom_count(), mol1.get_atom_count()) for f in frag_uncon: self.assertEqual(f.get_atom_count(), mol1.get_atom_count()) for f in frag_uncon_deg1: self.assertEqual(f.get_atom_count(), mol1.get_atom_count())
def test_mcf_isomorphic_graphs_large(self): from mogli import Molecule, maximal_common_fragments, GenerationType mol1, mol2 = Molecule(), Molecule() mol1.read_lgf(PACLITAXEL) mol2.read_lgf(PACLITAXEL) t1, _, _, _ = maximal_common_fragments(mol1, mol2, 1, TIMEOUT, GenerationType.NO_OPT) t2, frag_uncon_deg1, _, _ = maximal_common_fragments( mol1, mol2, 1, TIMEOUT_BIG, GenerationType.UNCON_DEG_1, maximum=True) self.assertFalse(t1) self.assertTrue(t2) self.assertTrue(len(frag_uncon_deg1) > 0) frag_uncon_deg1 = sorted(frag_uncon_deg1, key=lambda x: x.get_atom_count(), reverse=True) self.assertEqual(frag_uncon_deg1[0].get_atom_count(), mol1.get_atom_count())
def test_isomorphism(self): from mogli import Molecule mol1 = Molecule() mol2 = Molecule() mol3 = Molecule() mol1.read_lgf(ETHANE_1) mol2.read_lgf(ETHANE_2) mol3.read_lgf(ETHYL) self.assertTrue(mol1.is_isomorphic(mol2)) self.assertFalse(mol1.is_isomorphic(mol3))
def test_canonization(self): from mogli import Molecule, LGFIOConfig, Canonization mol1 = Molecule() mol2 = Molecule() mol3 = Molecule() config = LGFIOConfig("label", "atomType") mol1.read_lgf(ETHANE_1) mol2.read_lgf(ETHANE_2) mol3.read_lgf(GENERIC_1, config) c1 = Canonization(mol1) c2 = Canonization(mol2) c3 = Canonization(mol3) self.assertEqual(c1.get_canonization(), c2.get_canonization()) self.assertEqual(c1.get_colors(), c2.get_colors()) self.assertNotEqual(c1.get_canonization(), c3.get_canonization()) self.assertNotEqual(c1.get_colors(), c3.get_colors())
def test_read_lgf_properties(self): from mogli import LGFIOConfig, Molecule config = LGFIOConfig("label", "atomType") config.add_string_node_prop("label2")\ .add_bool_node_prop("isC")\ .add_int_node_prop("iNum")\ .add_double_node_prop("dNum") mol = Molecule() mol.read_lgf(ETHANE_PROPS, config) self.assertEqual(mol.get_atom_count(), 8) props = mol.get_properties() self.assertEqual(len(props), 4) self.assertIn("label2", props) self.assertIn("isC", props) self.assertIn("iNum", props) self.assertIn("dNum", props) v = mol.get_node_by_id(0) self.assertEqual(mol.get_property(v, "label2"), "C1") self.assertTrue(mol.get_property(v, "isC")) self.assertEqual(mol.get_property(v, "iNum"), 1) self.assertEqual(mol.get_property(v, "dNum"), 1.0) v = mol.get_node_by_id(1) self.assertEqual(mol.get_property(v, "label2"), "C2") self.assertTrue(mol.get_property(v, "isC")) self.assertEqual(mol.get_property(v, "iNum"), 2) self.assertEqual(mol.get_property(v, "dNum"), 2.0) v = mol.get_node_by_id(2) self.assertEqual(mol.get_property(v, "label2"), "H3") self.assertFalse(mol.get_property(v, "isC")) self.assertEqual(mol.get_property(v, "iNum"), 3) self.assertEqual(mol.get_property(v, "dNum"), 3.0) v = mol.get_node_by_id(3) self.assertEqual(mol.get_property(v, "label2"), "H4") self.assertFalse(mol.get_property(v, "isC")) self.assertEqual(mol.get_property(v, "iNum"), 4) self.assertEqual(mol.get_property(v, "dNum"), 4.0) v = mol.get_node_by_id(4) self.assertEqual(mol.get_property(v, "label2"), "H5") self.assertFalse(mol.get_property(v, "isC")) self.assertEqual(mol.get_property(v, "iNum"), 5) self.assertEqual(mol.get_property(v, "dNum"), 5.0) v = mol.get_node_by_id(5) self.assertEqual(mol.get_property(v, "label2"), "H6") self.assertFalse(mol.get_property(v, "isC")) self.assertEqual(mol.get_property(v, "iNum"), 6) self.assertEqual(mol.get_property(v, "dNum"), 6.0) v = mol.get_node_by_id(6) self.assertEqual(mol.get_property(v, "label2"), "H7") self.assertFalse(mol.get_property(v, "isC")) self.assertEqual(mol.get_property(v, "iNum"), 7) self.assertEqual(mol.get_property(v, "dNum"), 7.0) v = mol.get_node_by_id(7) self.assertEqual(mol.get_property(v, "label2"), "H8") self.assertFalse(mol.get_property(v, "isC")) self.assertEqual(mol.get_property(v, "iNum"), 8) self.assertEqual(mol.get_property(v, "dNum"), 8.0)
def test_read_lgf(self): from mogli import Molecule mol = Molecule() mol.read_lgf(ETHANE_1) self.assertEqual(mol.get_atom_count(), 8) c1 = mol.get_node_by_id(0) c2 = mol.get_node_by_id(1) props = mol.get_properties() self.assertIn('label2', props) self.assertEqual(mol.get_property(c1, 'label2'), 'C1') self.assertEqual(mol.get_property(c2, 'label2'), 'C2') c1_edges = {(0, 1), (0, 2), (0, 3), (0, 4)} c2_edges = {(0, 1), (1, 5), (1, 6), (1, 7)} edges = c1_edges | c2_edges for e in mol.get_inc_edge_iter(c1): id1 = mol.get_id(mol.get_u(e)) id2 = mol.get_id(mol.get_v(e)) edge = (id1, id2) if id1 < id2 else (id2, id1) self.assertIn(edge, c1_edges) for e in mol.get_inc_edge_iter(c2): id1 = mol.get_id(mol.get_u(e)) id2 = mol.get_id(mol.get_v(e)) edge = (id1, id2) if id1 < id2 else (id2, id1) self.assertIn(edge, c2_edges) for e in mol.get_edge_iter(): id1 = mol.get_id(mol.get_u(e)) id2 = mol.get_id(mol.get_v(e)) edge = (id1, id2) if id1 < id2 else (id2, id1) self.assertIn(edge, edges)
def test_pack_molecule(self): from mogli import Molecule, LGFIOConfig, pack_molecule, unpack_molecule mol1 = Molecule() mol2 = Molecule() config = LGFIOConfig('label', 'atomType') config.add_string_node_prop("label2")\ .add_bool_node_prop("isC")\ .add_int_node_prop("iNum")\ .add_double_node_prop("dNum") mol1.read_lgf(ETHANE_PROPS, config) mol2 = unpack_molecule(pack_molecule(mol1)) self.assertEqual(mol1.get_atom_count(), mol2.get_atom_count()) def s(e): id1 = mol1.get_id(mol1.get_u(e)) id2 = mol1.get_id(mol1.get_v(e)) return (id1, id2) if id1 < id2 else (id2, id1) edges1 = sorted([s(e) for e in mol1.get_edge_iter()]) edges2 = sorted([s(e) for e in mol2.get_edge_iter()]) self.assertEqual(edges1, edges2) props = mol2.get_properties() self.assertIn('label2', props) self.assertIn('isC', props) self.assertIn('iNum', props) self.assertIn('dNum', props) for v1 in mol1.get_node_iter(): v2 = mol2.get_node_by_id(mol1.get_id(v1)) self.assertEqual(mol1.get_property(v1, 'label2'), mol2.get_property(v2, 'label2')) self.assertEqual(mol1.get_property(v1, 'isC'), mol2.get_property(v2, 'isC')) self.assertEqual(mol1.get_property(v1, 'iNum'), mol2.get_property(v2, 'iNum')) self.assertEqual(mol1.get_property(v1, 'dNum'), mol2.get_property(v2, 'dNum'))
def test_mcf_custom_periodic_table(self): import copy from mogli import Molecule, PeriodicTable, LGFIOConfig, maximal_common_fragments, GenerationType custom_table = PeriodicTable( PeriodicTable.get_default()).make_equivalent(4, 7) mol1, mol2, mol3, mol4 = Molecule(), Molecule(), Molecule( custom_table), Molecule(custom_table) config = LGFIOConfig('label', 'atomType') mol1.read_lgf(GENERIC_1, config) mol2.read_lgf(GENERIC_2, config) mol3.read_lgf(GENERIC_1, config) mol4.read_lgf(GENERIC_2, config) t1, frag_default, _, _ = maximal_common_fragments( mol1, mol2, 1, TIMEOUT, GenerationType.UNCON_DEG_1, True) t2, frag_custom1, _, _ = maximal_common_fragments( mol3, mol4, 1, TIMEOUT, GenerationType.NO_OPT, True) t3, frag_custom2, _, _ = maximal_common_fragments( mol3, mol4, 1, TIMEOUT, GenerationType.DEG_1, True) t4, frag_custom3, _, _ = maximal_common_fragments( mol3, mol4, 1, TIMEOUT, GenerationType.UNCON, True) t5, frag_custom4, _, _ = maximal_common_fragments( mol3, mol4, 1, TIMEOUT, GenerationType.UNCON_DEG_1, True) self.assertTrue(t1) self.assertTrue(t2) self.assertTrue(t3) self.assertTrue(t4) self.assertTrue(t5) self.assertTrue(len(frag_default) > 0) self.assertTrue(len(frag_custom1) > 0) self.assertTrue(len(frag_custom2) > 0) self.assertTrue(len(frag_custom3) > 0) self.assertTrue(len(frag_custom4) > 0) self.assertEqual(frag_default[0].get_atom_count(), 5) self.assertEqual(frag_custom1[0].get_atom_count(), mol1.get_atom_count()) self.assertEqual(frag_custom2[0].get_atom_count(), mol1.get_atom_count()) self.assertEqual(frag_custom3[0].get_atom_count(), mol1.get_atom_count()) self.assertEqual(frag_custom4[0].get_atom_count(), mol1.get_atom_count())
def test_fcanonization(self): from mogli import Molecule, LGFIOConfig, GenerationType, maximal_common_fragments, FragmentCanonization mol1 = Molecule() mol2 = Molecule() mol3 = Molecule() mol4 = Molecule() config = LGFIOConfig("label", "atomType") mol1.read_lgf(ETHANE_1) mol2.read_lgf(ETHANE_1) mol3.read_lgf(ETHANE_1) mol4.read_lgf(ETHYL, config) t1, frag1, _, _ = maximal_common_fragments(mol1, mol2, 1, TIMEOUT, GenerationType.NO_OPT) t2, frag2, _, _ = maximal_common_fragments(mol1, mol3, 1, TIMEOUT, GenerationType.NO_OPT) t3, frag3, _, _ = maximal_common_fragments(mol1, mol4, 1, TIMEOUT, GenerationType.NO_OPT) self.assertTrue(t1) self.assertTrue(t2) self.assertTrue(t3) self.assertTrue(len(frag1) > 0) self.assertTrue(len(frag2) > 0) self.assertTrue(len(frag3) > 0) frag1 = sorted(frag1, key=lambda x: x.get_atom_count(), reverse=True) frag2 = sorted(frag2, key=lambda x: x.get_atom_count(), reverse=True) frag3 = sorted(frag3, key=lambda x: x.get_atom_count(), reverse=True) f1 = FragmentCanonization(frag1[0]) f2 = FragmentCanonization(frag2[0]) f3 = FragmentCanonization(frag3[0]) self.assertEqual(f1.get_canonization(), f2.get_canonization()) self.assertEqual(f1.get_colors(), f2.get_colors()) self.assertNotEqual(f1.get_canonization(), f3.get_canonization()) self.assertNotEqual(f1.get_colors(), f3.get_colors())
def test_hash_canonization(self): from mogli import Molecule, LGFIOConfig, Canonization, hash_canonization mol1 = Molecule() mol2 = Molecule() mol3 = Molecule() config = LGFIOConfig("label", "atomType") mol1.read_lgf(ETHANE_1) mol2.read_lgf(ETHANE_2) mol3.read_lgf(GENERIC_1, config) c1 = hash_canonization(Canonization(mol1)) c2 = hash_canonization(Canonization(mol2)) c3 = hash_canonization(Canonization(mol3)) self.assertNotEqual(c1, c2) self.assertNotEqual(c1, c3)