def test_dihedral_angles_tpa(self):
        molecule = self.load_molecule("tpa.xyz")
        pattern = DihedralAnglePattern([
            CriteriaSet(atom_criteria(1, 6, 6, 1), tag="HCCH"),
            CriteriaSet(atom_criteria(1, 6, 6, 7), tag="HCCN"),
            CriteriaSet(atom_criteria(1, 6, 7, 6), tag="HCNC"),
        ])
        expected_results = {
            'HCCH':
            set([
                (16, 2, 1, 14), (15, 2, 1, 14), (16, 2, 1, 13), (15, 2, 1, 13),
                (17, 3, 2, 16), (19, 3, 2, 16), (18, 3, 2, 16), (17, 3, 2, 15),
                (19, 3, 2, 15), (18, 3, 2, 15), (23, 5, 4, 20), (22, 5, 4, 20),
                (23, 5, 4, 21), (22, 5, 4, 21), (25, 6, 5, 23), (26, 6, 5, 23),
                (24, 6, 5, 23), (25, 6, 5, 22), (26, 6, 5, 22), (24, 6, 5, 22),
                (29, 8, 7, 27), (30, 8, 7, 27), (29, 8, 7, 28), (30, 8, 7, 28),
                (31, 9, 8, 29), (33, 9, 8, 29), (32, 9, 8, 29), (31, 9, 8, 30),
                (33, 9, 8, 30), (32, 9, 8, 30), (37, 11, 10, 35),
                (36, 11, 10, 35), (37, 11, 10, 34), (36, 11, 10, 34),
                (39, 12, 11, 37), (40, 12, 11, 37), (38, 12, 11, 37),
                (39, 12, 11, 36), (40, 12, 11, 36), (38, 12, 11, 36)
            ]),
            'HCCN':
            set([(16, 2, 1, 0), (15, 2, 1, 0), (23, 5, 4, 0), (22, 5, 4, 0),
                 (29, 8, 7, 0), (30, 8, 7, 0), (37, 11, 10, 0),
                 (36, 11, 10, 0)]),
            'HCNC':
            set([(14, 1, 0, 10), (13, 1, 0, 10), (20, 4, 0, 10),
                 (21, 4, 0, 10), (27, 7, 0, 10), (28, 7, 0, 10),
                 (35, 10, 0, 1), (34, 10, 0, 1), (20, 4, 0, 1), (21, 4, 0, 1),
                 (27, 7, 0, 1), (28, 7, 0, 1), (35, 10, 0, 4), (34, 10, 0, 4),
                 (14, 1, 0, 4), (13, 1, 0, 4), (27, 7, 0, 4), (28, 7, 0, 4),
                 (35, 10, 0, 7), (34, 10, 0, 7), (14, 1, 0, 7), (13, 1, 0, 7),
                 (20, 4, 0, 7), (21, 4, 0, 7)])
        }
        test_results = dict((key, []) for key in expected_results)
        match_generator = GraphSearch(pattern, debug=False)
        for match in match_generator(molecule.graph):
            test_results[match.tag].append(
                tuple(
                    match.get_destination(index)
                    for index in xrange(len(match))))

        def iter_alternatives(test_item, key):
            yield test_item
            a, b, c, d = test_item
            if (molecule.numbers[a]
                    == molecule.numbers[d]) and (molecule.numbers[b]
                                                 == molecule.numbers[c]):
                yield d, c, b, a

        self.verify_graph_search(molecule.graph, expected_results,
                                 test_results, iter_alternatives)
    def test_rings_precursor(self):
        molecule = self.load_molecule("precursor.xyz")
        pattern = RingPattern(21)
        all_rings = {}
        match_generator = GraphSearch(pattern, debug=False)
        for match in match_generator(molecule.graph):
            l = all_rings.setdefault(len(match.ring_nodes), set([]))
            l.add(match.ring_nodes)

        for size, solutions in all_rings.iteritems():
            tag = '%i-ring' % size
            pattern = NRingPattern(size, [CriteriaSet(tag=tag)], strong=True)
            expected_results = {tag: solutions}

            test_results = {tag: []}
            match_generator = GraphSearch(pattern, debug=False)
            for match in match_generator(molecule.graph):
                test_results[match.tag].append(
                    tuple(
                        match.get_destination(index)
                        for index in xrange(len(match))))

            def iter_alternatives(test_item, key):
                for i in xrange(size):
                    test_item = (test_item[-1], ) + test_item[:-1]
                    yield test_item
                    yield tuple(reversed(test_item))

            self.verify_graph_search(molecule.graph, expected_results,
                                     test_results, iter_alternatives)
    def test_dihedral_angles_precursor(self):
        molecule = self.load_molecule("precursor.xyz")
        pattern = DihedralAnglePattern([CriteriaSet(tag="all")])
        # construct all dihedral angles:
        all_dihedrals = set([])
        for b, c in molecule.graph.pairs:
            for a in molecule.graph.neighbors[b]:
                if a != c:
                    for d in molecule.graph.neighbors[c]:
                        if d != b:
                            all_dihedrals.add((a, b, c, d))
        expected_results = {
            'all': all_dihedrals,
        }
        test_results = dict((key, []) for key in expected_results)
        match_generator = GraphSearch(pattern, debug=False)
        for match in match_generator(molecule.graph):
            test_results[match.tag].append(
                tuple(
                    match.get_destination(index)
                    for index in xrange(len(match))))

        def iter_alternatives(test_item, key):
            yield test_item
            a, b, c, d = test_item
            yield d, c, b, a

        self.verify_graph_search(molecule.graph, expected_results,
                                 test_results, iter_alternatives)
    def test_bonds_tpa(self):
        molecule = self.load_molecule("tpa.xyz")
        pattern = BondPattern([
            CriteriaSet(atom_criteria(1, 6), tag="HC"),
            CriteriaSet(atom_criteria(6, 6), tag="CC"),
            CriteriaSet(atom_criteria(6, 7), tag="CN"),
            CriteriaSet(atom_criteria(6, HasNumNeighbors(4)), tag="C-sp3"),
            CriteriaSet(atom_criteria(
                6, CritOr(HasAtomNumber(6), HasAtomNumber(7))),
                        tag="C-[CN]"),
            CriteriaSet(relation_criteria={0: BondLongerThan(1.3 * angstrom)},
                        tag="long"),
            CriteriaSet(atom_criteria(
                6, HasNeighbors(*atom_criteria(1, 1, 6, 7).values())),
                        tag="C-C(-NHH)"),
        ])
        expected_results = {
            "HC":
            set([(14, 1), (13, 1), (16, 2), (15, 2), (17, 3), (19, 3), (18, 3),
                 (20, 4), (21, 4), (23, 5), (22, 5), (25, 6), (26, 6), (24, 6),
                 (27, 7), (28, 7), (29, 8), (30, 8), (31, 9), (33, 9), (32, 9),
                 (35, 10), (34, 10), (37, 11), (36, 11), (39, 12), (40, 12),
                 (38, 12)]),
            "CC":
            set([(2, 1), (3, 2), (5, 4), (6, 5), (8, 7), (9, 8), (11, 10),
                 (12, 11)]),
            "CN":
            set([(10, 0), (1, 0), (4, 0), (7, 0)]),
            "C-sp3":
            set([(10, 0), (1, 0), (4, 0), (7, 0), (2, 1), (3, 2), (5, 4),
                 (6, 5), (8, 7), (9, 8), (11, 10), (12, 11)]),
            "C-[CN]":
            set([(10, 0), (1, 0), (4, 0), (7, 0), (2, 1), (3, 2), (5, 4),
                 (6, 5), (8, 7), (9, 8), (11, 10), (12, 11)]),
            "long":
            set([(10, 0), (1, 0), (4, 0), (7, 0), (2, 1), (3, 2), (5, 4),
                 (6, 5), (8, 7), (9, 8), (11, 10), (12, 11)]),
            "C-C(-NHH)":
            set([(5, 4), (7, 8), (1, 2), (10, 11)])
        }
        test_results = dict((key, []) for key in expected_results)
        match_generator = GraphSearch(pattern, debug=False)
        for match in match_generator(molecule.graph):
            test_results[match.tag].append(
                tuple(
                    match.get_destination(index)
                    for index in xrange(len(match))))

        def iter_alternatives(test_item, key):
            yield test_item
            a, b = test_item
            if molecule.numbers[a] == molecule.numbers[b] or key == "long":
                yield b, a

        self.verify_graph_search(molecule.graph, expected_results,
                                 test_results, iter_alternatives)
Beispiel #5
0
 def _add_graph_bonds(self, molecular_graph, offset, atom_types, molecule):
     # add bonds
     match_generator = GraphSearch(BondPattern([CriteriaSet()]))
     tmp = sorted([(
         match.get_destination(0),
         match.get_destination(1),
     ) for match in match_generator(molecular_graph)])
     new = len(tmp)
     if new > 0:
         prev = len(self.bonds)
         self.bonds.resize((prev + len(tmp), 2))
         self.bonds[-len(tmp):] = tmp
         self.bonds[-len(tmp):] += offset
Beispiel #6
0
 def _add_graph_dihedrals(self, molecular_graph, offset, atom_types,
                          molecule):
     # add dihedrals
     match_generator = GraphSearch(DihedralAnglePattern([CriteriaSet()]))
     tmp = sorted([(
         match.get_destination(0),
         match.get_destination(1),
         match.get_destination(2),
         match.get_destination(3),
     ) for match in match_generator(molecular_graph)])
     new = len(tmp)
     if new > 0:
         prev = len(self.dihedrals)
         self.dihedrals.resize((prev + len(tmp), 4))
         self.dihedrals[-len(tmp):] = tmp
         self.dihedrals[-len(tmp):] += offset
    def test_tetra_tpa(self):
        molecule = self.load_molecule("tpa.xyz")
        pattern = TetraPattern(
            [CriteriaSet(atom_criteria(6, 1, 6, 6, 1), tag="C-(HCCH)")],
            node_tags={
                0: 1,
                1: 1
            })  # node tags are just a silly example
        expected_results = {
            'C-(HCCH)':
            set([
                (8, 29, 9, 7, 30),
                (8, 30, 9, 7, 29),
                (2, 1, 15, 16, 3),
                (2, 3, 15, 16, 1),
                (11, 10, 36, 37, 12),
                (11, 12, 36, 37, 10),
                (5, 4, 22, 23, 6),
                (5, 6, 22, 23, 4),
            ]),
        }
        test_results = dict((key, []) for key in expected_results)
        match_generator = GraphSearch(pattern, debug=False)
        for match in match_generator(molecule.graph):
            test_results[match.tag].append(
                tuple(
                    match.get_destination(index)
                    for index in xrange(len(match))))

        def iter_alternatives(test_item, key):
            a, b, c, d, e = test_item
            yield test_item
            yield a, c, b, e, d
            if (molecule.numbers[b] == molecule.numbers[e]):
                yield a, e, c, d, b
                yield a, c, e, b, d
            if (molecule.numbers[c] == molecule.numbers[d]):
                yield a, b, d, c, e
                yield a, d, b, e, c
            if (molecule.numbers[b]
                    == molecule.numbers[e]) and (molecule.numbers[c]
                                                 == molecule.numbers[d]):
                yield a, e, d, c, b
                yield a, d, e, b, c

        self.verify_graph_search(molecule.graph, expected_results,
                                 test_results, iter_alternatives)
    def test_bending_angles_tpa(self):
        molecule = self.load_molecule("tpa.xyz")
        pattern = BendingAnglePattern([
            CriteriaSet(atom_criteria(1, 6, 1), tag="HCH"),
            CriteriaSet(atom_criteria(1, 6, 6), tag="HCC"),
            CriteriaSet(atom_criteria(6, 6, 6), tag="CCC"),
            CriteriaSet(atom_criteria(6, 6, 7), tag="CCN"),
            CriteriaSet(atom_criteria(6, 7, 6), tag="CNC"),
            CriteriaSet(atom_criteria(1, 6, 7), tag="HCN"),
        ])
        expected_results = {
            'HCC':
            set([(14, 1, 2), (13, 1, 2), (16, 2, 1), (15, 2, 1), (16, 2, 3),
                 (15, 2, 3), (17, 3, 2), (19, 3, 2), (18, 3, 2), (20, 4, 5),
                 (21, 4, 5), (23, 5, 6), (23, 5, 4), (22, 5, 6), (22, 5, 4),
                 (25, 6, 5), (26, 6, 5), (24, 6, 5), (27, 7, 8), (28, 7, 8),
                 (29, 8, 9), (30, 8, 9), (29, 8, 7), (30, 8, 7), (31, 9, 8),
                 (33, 9, 8), (32, 9, 8), (35, 10, 11), (34, 10, 11),
                 (37, 11, 12), (36, 11, 12), (37, 11, 10), (36, 11, 10),
                 (39, 12, 11), (40, 12, 11), (38, 12, 11)]),
            'CCN':
            set([(2, 1, 0), (5, 4, 0), (8, 7, 0), (11, 10, 0)]),
            'CNC':
            set([(1, 0, 10), (4, 0, 10), (7, 0, 10), (4, 0, 1), (7, 0, 1),
                 (7, 0, 4)]),
            'HCH':
            set([(13, 1, 14), (15, 2, 16), (19, 3, 17), (18, 3, 17),
                 (18, 3, 19), (21, 4, 20), (22, 5, 23), (26, 6, 25),
                 (24, 6, 25), (24, 6, 26), (28, 7, 27), (30, 8, 29),
                 (33, 9, 31), (32, 9, 31), (32, 9, 33), (34, 10, 35),
                 (36, 11, 37), (40, 12, 39), (38, 12, 39), (38, 12, 40)]),
            'HCN':
            set([(14, 1, 0), (13, 1, 0), (20, 4, 0), (21, 4, 0), (27, 7, 0),
                 (28, 7, 0), (35, 10, 0), (34, 10, 0)]),
            'CCC':
            set([(3, 2, 1), (4, 5, 6), (7, 8, 9), (10, 11, 12)])
        }
        test_results = dict((key, []) for key in expected_results)
        match_generator = GraphSearch(pattern, debug=False)
        for match in match_generator(molecule.graph):
            test_results[match.tag].append(
                tuple(
                    match.get_destination(index)
                    for index in xrange(len(match))))

        def iter_alternatives(test_item, key):
            yield test_item
            a, b, c = test_item
            if molecule.numbers[a] == molecule.numbers[c]:
                yield c, b, a

        self.verify_graph_search(molecule.graph, expected_results,
                                 test_results, iter_alternatives)
Beispiel #9
0
 def _add_graph_impropers(self, molecular_graph, offset, atom_types,
                          molecule):
     # add improper dihedrals, only when center has three bonds
     match_generator = GraphSearch(
         OutOfPlanePattern(
             [CriteriaSet(vertex_criteria={0: HasNumNeighbors(3)}, )],
             vertex_tags={1: 1}))
     tmp = sorted([(
         match.get_destination(0),
         match.get_destination(1),
         match.get_destination(2),
         match.get_destination(3),
     ) for match in match_generator(molecular_graph)])
     new = len(tmp)
     if new > 0:
         prev = len(self.impropers)
         self.impropers.resize((prev + len(tmp), 4))
         self.impropers[-len(tmp):] = tmp
         self.impropers[-len(tmp):] += offset
    def test_rings_5ringOH(self):
        molecule = self.load_molecule("5ringOH.xyz")
        pattern = NRingPattern(10, [CriteriaSet(tag="all")])
        expected_results = {
            'all': set([(14, 25, 11, 26, 21, 29, 18, 28, 17, 27)])
        }

        test_results = dict((key, []) for key in expected_results)
        match_generator = GraphSearch(pattern, debug=False)
        for match in match_generator(molecule.graph):
            test_results[match.tag].append(
                tuple(
                    match.get_destination(index)
                    for index in xrange(len(match))))

        def iter_alternatives(test_item, key):
            for i in xrange(10):
                test_item = (test_item[-1], ) + test_item[:-1]
                yield test_item
                yield tuple(reversed(test_item))

        self.verify_graph_search(molecule.graph, expected_results,
                                 test_results, iter_alternatives)
Beispiel #11
0
 def __init__(self, label, calculate_eg, atom_criteria):
     ValenceTerm.__init__(self, label, calculate_eg, BondPattern(
         [CriteriaSet(dict((index, criterion) for index, criterion in enumerate(atom_criteria)))]
     ))
Beispiel #12
0
    def add_molecular_graph(self,
                            molecular_graph,
                            atom_types=None,
                            charges=None,
                            split=True):
        # add atom numbers and molecule indices
        new = len(molecular_graph.numbers)
        if new == 0: return
        prev = len(self.numbers)
        offset = prev
        self.numbers.resize(prev + new)
        self.numbers[-new:] = molecular_graph.numbers
        if atom_types is None:
            atom_types = [
                periodic[number].symbol for number in molecular_graph.numbers
            ]
        self.atom_types.extend(atom_types)
        if charges is None:
            charges = [0.0] * len(molecular_graph.numbers)
        self.charges.extend(charges)
        self.molecules.resize(prev + new)
        # add names (autogenerated)
        if split:
            groups = molecular_graph.independent_nodes
            names = [
                self._get_name(molecular_graph, group) for group in groups
            ]
            group_indices = numpy.zeros(new, int)
            for group_index, group in enumerate(groups):
                for index in group:
                    group_indices[index] = group_index
            self.names.extend(
                [names[group_index] for group_index in group_indices])
            if prev == 0:
                self.molecules[:] = group_indices
            else:
                self.molecules[
                    -new:] = self.molecules[-new] + group_indices + 1
        else:
            if prev == 0:
                self.molecules[-new:] = 0
            else:
                self.molecules[-new:] = self.molecules[-new] + 1
            name = self._get_name(molecular_graph)
            self.names.extend([name] * new)

        # add bonds
        match_generator = GraphSearch(BondPattern([CriteriaSet()]))
        tmp = [(
            match.get_destination(0),
            match.get_destination(1),
        ) for match in match_generator(molecular_graph)]
        tmp.sort()
        new = len(tmp)
        if new > 0:
            prev = len(self.bonds)
            self.bonds.resize((prev + len(tmp), 2))
            self.bonds[-len(tmp):] = tmp
            self.bonds[-len(tmp):] += offset

        # add bends
        match_generator = GraphSearch(BendingAnglePattern([CriteriaSet()]))
        tmp = [(
            match.get_destination(0),
            match.get_destination(1),
            match.get_destination(2),
        ) for match in match_generator(molecular_graph)]
        tmp.sort()
        new = len(tmp)
        if new > 0:
            prev = len(self.bends)
            self.bends.resize((prev + len(tmp), 3))
            self.bends[-len(tmp):] = tmp
            self.bends[-len(tmp):] += offset

        # add dihedrals
        match_generator = GraphSearch(DihedralAnglePattern([CriteriaSet()]))
        tmp = [(
            match.get_destination(0),
            match.get_destination(1),
            match.get_destination(2),
            match.get_destination(3),
        ) for match in match_generator(molecular_graph)]
        tmp.sort()
        new = len(tmp)
        if new > 0:
            prev = len(self.dihedrals)
            self.dihedrals.resize((prev + len(tmp), 4))
            self.dihedrals[-len(tmp):] = tmp
            self.dihedrals[-len(tmp):] += offset