Esempio n. 1
0
    def process_cross_bonds(self):
        '''
        if cross bonds have been declared:
        1. tag the corresponding bonds within the tree as no-ops
        2. create a ghost-bond connection from exit_atom to start atom
        3. create a drawn duplicate of the cross bond
        4. append 2 and 3 as branch to the exit atom

        this is unfortunately all a little hackish.
        '''
        cross_bonds = self.options['cross_bond']

        for start1, end1 in cross_bonds:
            start = start1 - 1
            end = end1 - 1

            # retrieve the matching bond that's in the parse tree
            for combo in ((start, end), (end, start)):
                if combo in self.seen_bonds:
                    bond = self.bonds[combo]
                    break
            else: # referenced bond doesn't exist
                raise MCFError, "bond %s-%s doesn't exist" % (start1, end1)

            # very special case: the bond _might_ already be the very
            # last one to be rendered - then we just tag it
            if self.exit_bond.descendants and bond is self.exit_bond.descendants[-1]:
                bond.set_cross(last=True)
                continue

            # create a copy of the bond that will be rendered later
            bond_copy = bond.clone()

            # tag original bond as no-op
            bond.set_link()

            # modify bond copy
            bond_copy.set_cross()
            bond_copy.to_phantom = True     # don't render atom again
            bond_copy.descendants = []      # forget copied descendants

            if bond_copy.start_atom is not self.exit_atom: # usual case
                # create a pseudo bond from the exit atom to the start atom
                # pseudo bond will not be drawn, serves only to "move the pen"
                pseudo_bond = Bond(self.options,
                                self.exit_atom,
                                bond_copy.start_atom)

                pseudo_bond.set_link()
                pseudo_bond.to_phantom = True      # don't render the atom, either

                bond_copy.parent = pseudo_bond
                pseudo_bond.descendants.append(bond_copy)

                pseudo_bond.parent = self.exit_bond
                self.exit_bond.descendants.append(pseudo_bond)

            else: # occasionally, the molecule's exit atom may be the starting point
                  # of the elevated bond
                self.exit_bond.descendants.append(bond_copy)
Esempio n. 2
0
    def link_atoms(self, x, y):
        '''
        connect atoms with indexes x and y using a pseudo bond.
        Helper for connect_fragments
        '''
        start_atom = self.atoms[x]
        end_atom = self.atoms[y]

        bond = Bond(self.options, start_atom, end_atom)
        bond.set_link()

        self.bonds[(x, y)] = bond
        self.bonds[(y, x)] = bond.invert()

        start_atom.neighbors.append(y)
        end_atom.neighbors.append(x)