Esempio n. 1
0
def getSubstructureFromPath(refmol: Molecule, path: np.ndarray) -> Molecule:
    """Create a molecules object from a substructure of given molecule."""

    # initialize
    refat = refmol.get_atomic_numbers()
    refxyz = refmol.get_positions()

    # atoms from complex excluding old substrate
    atoms = []
    for elem in path:
        atom = Atom(symbol=refat[elem], position=refxyz[elem, :])
        atoms.append(atom)

    # create molecule from atoms
    return Molecule(symbols=atoms)
Esempio n. 2
0
def exchangeSubstructure(
    n: int,
    center: int,
    subnr: int,
    bonds: np.ndarray,
    ref: Molecule,
    newsub: Molecule,
    newSubBonds: np.ndarray,
    name: str,
    rotate: int,
    exclude: bool,
):
    """Exchange substructure (subnr) from ref with substrate."""

    # setup initial complex
    refxyz = ref.get_positions()
    refat = ref.get_atomic_numbers()
    refnat = ref.get_number_of_atoms()

    # match new and old substrate
    newat = newsub.get_atomic_numbers()
    newnat = newsub.get_number_of_atoms()

    # extract coordinates of central atom
    centralAtom = np.zeros(shape=(1, 3), dtype=np.float64)
    centralAtom = refxyz[center, :]

    for i in range(len(bonds[center])):
        if i == subnr:
            path = np.zeros(shape=(n, ), dtype=np.int32)
            # set all elements to -1
            path.fill(-1)
            partner = bonds[center][i]
            count = Counter()  # type: ignore
            count["step"] = -1
            getSubstructures(n, bonds, center, partner, path, count=count)
            # get rid of -1 elements
            path = [x for x in path if x != -1]
            path = np.array(path)
            # create molecule from given path
            oldsub = getSubstructureFromPath(ref, path)
            # get all bonding partner
            oldSubBonds = oldsub.get_bonds(partner="X")
            outxyz = np.zeros(shape=(newnat, 3), dtype=np.float64)
            outxyz = matchSubstrates(
                bonds,
                newsub,
                newSubBonds,
                oldsub,
                oldSubBonds,
                centralAtom,
            )

            # atoms from complex excluding old substrate
            atoms = []
            for i in range(refnat):
                if i in path:
                    continue
                atom = Atom(symbol=refat[i], position=refxyz[i, :])
                atoms.append(atom)

            # atoms from new substrate

            # Should we rotate the substrate around covalent bond?
            if rotate:
                theta = rotate
                origin = refxyz[center, :]
                partner = outxyz[0, :]
                outxyz2 = np.zeros(shape=outxyz.shape, dtype=np.float64)
                # reference shift
                refShift = getRodriguezRotation(partner, origin, partner,
                                                theta)
                shift = outxyz[0, :] - refShift
                for i in range(newnat):
                    outxyz2[i, :] = getRodriguezRotation(
                        outxyz[i, :],
                        origin,
                        partner,
                        theta,
                    )
                    outxyz2[i, :] += shift

                    atom = Atom(symbol=newat[i], position=outxyz2[i, :])
                    atoms.append(atom)
            else:
                for i in range(newnat):
                    atom = Atom(symbol=newat[i], position=outxyz[i, :])
                    atoms.append(atom)

            # create molecule from atoms
            mol = Molecule(symbols=atoms)

            # write new structure in xyz format
            mol.writeMolecule(name + ".xyz")

            # write constrain file
            refnat = ref.get_number_of_atoms()
            oldnat = oldsub.get_number_of_atoms()
            nat = refnat - oldnat
            writeTransitionMetalConstrains(nat, newnat, newSubBonds, exclude)