/**
   * Modules for cleaning a molecule
   *
   * @param molecule
   * @return cleaned AtomContainer
   */
  @TestMethod("testCheckAndCleanMolecule")
  public static IAtomContainer checkAndCleanMolecule(IAtomContainer molecule) {
    boolean isMarkush = false;
    for (IAtom atom : molecule.atoms()) {
      if (atom.getSymbol().equals("R")) {
        isMarkush = true;
        break;
      }
    }

    if (isMarkush) {
      System.err.println("Skipping Markush structure for sanity check");
    }

    // Check for salts and such
    if (!ConnectivityChecker.isConnected(molecule)) {
      // lets see if we have just two parts if so, we assume its a salt and just work
      // on the larger part. Ideally we should have a check to ensure that the smaller
      //  part is a metal/halogen etc.
      IMoleculeSet fragments = ConnectivityChecker.partitionIntoMolecules(molecule);
      if (fragments.getMoleculeCount() > 2) {
        System.err.println("More than 2 components. Skipped");
      } else {
        IMolecule frag1 = fragments.getMolecule(0);
        IMolecule frag2 = fragments.getMolecule(1);
        if (frag1.getAtomCount() > frag2.getAtomCount()) {
          molecule = frag1;
        } else {
          molecule = frag2;
        }
      }
    }
    configure(molecule);
    return molecule;
  }
  /**
   * Generates a shortest path based BitSet fingerprint for the given AtomContainer.
   *
   * @param ac The AtomContainer for which a fingerprint is generated
   * @exception CDKException if there error in aromaticity perception or other CDK functions
   * @return A {@link BitSet} representing the fingerprint
   */
  @Override
  public IBitFingerprint getBitFingerprint(IAtomContainer ac) throws CDKException {

    IAtomContainer atomContainer = null;
    try {
      atomContainer = (IAtomContainer) ac.clone();
    } catch (CloneNotSupportedException ex) {
      logger.error("Failed to clone the molecule:", ex);
    }
    Aromaticity.cdkLegacy().apply(atomContainer);
    BitSet bitSet = new BitSet(fingerprintLength);
    if (!ConnectivityChecker.isConnected(atomContainer)) {
      IAtomContainerSet partitionedMolecules =
          ConnectivityChecker.partitionIntoMolecules(atomContainer);
      for (IAtomContainer container : partitionedMolecules.atomContainers()) {
        addUniquePath(container, bitSet);
      }
    } else {
      addUniquePath(atomContainer, bitSet);
    }
    return new BitSetFingerprint(bitSet);
  }
Esempio n. 3
0
 /**
  * Returns unique unmapped fragments in the target molecule.
  *
  * @return unique fragments in the target molecule
  * @throws CloneNotSupportedException
  */
 public synchronized IAtomContainerSet getUniqueFragmentsInTarget()
     throws CloneNotSupportedException {
   IAtomContainer ac = (IAtomContainer) target.clone();
   List<IAtom> commonAtoms = Collections.synchronizedList(new ArrayList<IAtom>());
   for (IAtom atom : mapping.values()) {
     commonAtoms.add(ac.getAtom(getTargetIndex(atom)));
   }
   for (IAtom atom : commonAtoms) {
     ac.removeAtomAndConnectedElectronContainers(atom);
   }
   // now we probably have a set of disconnected components
   // so lets get a set of individual atom containers for
   // corresponding to each component
   return ConnectivityChecker.partitionIntoMolecules(ac);
 }
  /**
   * Choose any possible quadruple of the set of atoms in ac and establish all of the possible
   * bonding schemes according to Faulon's equations.
   */
  public static List sample(IMolecule ac) {
    logger.debug("RandomGenerator->mutate() Start");
    List structures = new ArrayList();

    int nrOfAtoms = ac.getAtomCount();
    double a11 = 0, a12 = 0, a22 = 0, a21 = 0;
    double b11 = 0, lowerborder = 0, upperborder = 0;
    double b12 = 0;
    double b21 = 0;
    double b22 = 0;
    double[] cmax = new double[4];
    double[] cmin = new double[4];
    IAtomContainer newAc = null;

    IAtom ax1 = null, ax2 = null, ay1 = null, ay2 = null;
    IBond b1 = null, b2 = null, b3 = null, b4 = null;
    // int[] choices = new int[3];
    /* We need at least two non-zero bonds in order to be successful */
    int nonZeroBondsCounter = 0;
    for (int x1 = 0; x1 < nrOfAtoms; x1++) {
      for (int x2 = x1 + 1; x2 < nrOfAtoms; x2++) {
        for (int y1 = x2 + 1; y1 < nrOfAtoms; y1++) {
          for (int y2 = y1 + 1; y2 < nrOfAtoms; y2++) {
            nonZeroBondsCounter = 0;
            ax1 = ac.getAtom(x1);
            ay1 = ac.getAtom(y1);
            ax2 = ac.getAtom(x2);
            ay2 = ac.getAtom(y2);

            /* Get four bonds for these four atoms */

            b1 = ac.getBond(ax1, ay1);
            if (b1 != null) {
              a11 = BondManipulator.destroyBondOrder(b1.getOrder());
              nonZeroBondsCounter++;
            } else {
              a11 = 0;
            }

            b2 = ac.getBond(ax1, ay2);
            if (b2 != null) {
              a12 = BondManipulator.destroyBondOrder(b2.getOrder());
              nonZeroBondsCounter++;
            } else {
              a12 = 0;
            }

            b3 = ac.getBond(ax2, ay1);
            if (b3 != null) {
              a21 = BondManipulator.destroyBondOrder(b3.getOrder());
              nonZeroBondsCounter++;
            } else {
              a21 = 0;
            }

            b4 = ac.getBond(ax2, ay2);
            if (b4 != null) {
              a22 = BondManipulator.destroyBondOrder(b4.getOrder());
              nonZeroBondsCounter++;
            } else {
              a22 = 0;
            }
            if (nonZeroBondsCounter > 1) {
              /* Compute the range for b11 (see Faulons formulae for details) */

              cmax[0] = 0;
              cmax[1] = a11 - a22;
              cmax[2] = a11 + a12 - 3;
              cmax[3] = a11 + a21 - 3;
              cmin[0] = 3;
              cmin[1] = a11 + a12;
              cmin[2] = a11 + a21;
              cmin[3] = a11 - a22 + 3;
              lowerborder = MathTools.max(cmax);
              upperborder = MathTools.min(cmin);
              for (b11 = lowerborder; b11 <= upperborder; b11++) {
                if (b11 != a11) {

                  b12 = a11 + a12 - b11;
                  b21 = a11 + a21 - b11;
                  b22 = a22 - a11 + b11;
                  logger.debug("Trying atom combination : " + x1 + ":" + x2 + ":" + y1 + ":" + y2);
                  try {
                    newAc = (IAtomContainer) ac.clone();
                    change(newAc, x1, y1, x2, y2, b11, b12, b21, b22);
                    if (ConnectivityChecker.isConnected(newAc)) {
                      structures.add(newAc);
                    } else {
                      logger.debug("not connected");
                    }
                  } catch (CloneNotSupportedException e) {
                    logger.error("Cloning exception: " + e.getMessage());
                    logger.debug(e);
                  }
                }
              }
            }
          }
        }
      }
    }
    return structures;
  }
  /**
   * Initiates the process for the given mechanism. The atoms to apply are mapped between reactants
   * and products.
   *
   * @param atomContainerSet
   * @param atomList The list of atoms taking part in the mechanism. Only allowed two atoms. The
   *     first atom is the atom which contains the ISingleElectron and the second third is the atom
   *     which will be removed the first atom
   * @param bondList The list of bonds taking part in the mechanism. Only allowed one bond. It is
   *     the bond which is moved
   * @return The Reaction mechanism
   */
  @TestMethod(value = "testInitiate_IAtomContainerSet_ArrayList_ArrayList")
  public IReaction initiate(
      IAtomContainerSet atomContainerSet, ArrayList<IAtom> atomList, ArrayList<IBond> bondList)
      throws CDKException {
    CDKAtomTypeMatcher atMatcher = CDKAtomTypeMatcher.getInstance(atomContainerSet.getBuilder());
    if (atomContainerSet.getAtomContainerCount() != 1) {
      throw new CDKException("RadicalSiteIonizationMechanism only expects one IMolecule");
    }
    if (atomList.size() != 3) {
      throw new CDKException("RadicalSiteIonizationMechanism expects three atoms in the ArrayList");
    }
    if (bondList.size() != 2) {
      throw new CDKException(
          "RadicalSiteIonizationMechanism only expect one bond in the ArrayList");
    }
    IAtomContainer molecule = atomContainerSet.getAtomContainer(0);
    IAtomContainer reactantCloned;
    try {
      reactantCloned = (IAtomContainer) molecule.clone();
    } catch (CloneNotSupportedException e) {
      throw new CDKException("Could not clone IMolecule!", e);
    }
    IAtom atom1 = atomList.get(0); // Atom containing the ISingleElectron
    IAtom atom1C = reactantCloned.getAtom(molecule.getAtomNumber(atom1));
    IAtom atom2 = atomList.get(1); // Atom
    IAtom atom2C = reactantCloned.getAtom(molecule.getAtomNumber(atom2));
    IAtom atom3 = atomList.get(2); // Atom to be saved
    IAtom atom3C = reactantCloned.getAtom(molecule.getAtomNumber(atom3));
    IBond bond1 = bondList.get(0); // Bond to increase the order
    int posBond1 = molecule.getBondNumber(bond1);
    IBond bond2 = bondList.get(1); // Bond to remove
    int posBond2 = molecule.getBondNumber(bond2);

    BondManipulator.increaseBondOrder(reactantCloned.getBond(posBond1));
    reactantCloned.removeBond(reactantCloned.getBond(posBond2));

    List<ISingleElectron> selectron = reactantCloned.getConnectedSingleElectronsList(atom1C);
    reactantCloned.removeSingleElectron(selectron.get(selectron.size() - 1));
    atom1C.setHybridization(null);
    AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(reactantCloned);
    IAtomType type = atMatcher.findMatchingAtomType(reactantCloned, atom1C);
    if (type == null) return null;

    atom2C.setHybridization(null);
    AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(reactantCloned);
    type = atMatcher.findMatchingAtomType(reactantCloned, atom2C);
    if (type == null) return null;

    reactantCloned.addSingleElectron(new SingleElectron(atom3C));
    atom3C.setHybridization(null);
    AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(reactantCloned);
    type = atMatcher.findMatchingAtomType(reactantCloned, atom3C);
    if (type == null) return null;

    IReaction reaction = DefaultChemObjectBuilder.getInstance().newInstance(IReaction.class);
    reaction.addReactant(molecule);

    /* mapping */
    for (IAtom atom : molecule.atoms()) {
      IMapping mapping =
          DefaultChemObjectBuilder.getInstance()
              .newInstance(
                  IMapping.class, atom, reactantCloned.getAtom(molecule.getAtomNumber(atom)));
      reaction.addMapping(mapping);
    }

    IAtomContainerSet moleculeSetP = ConnectivityChecker.partitionIntoMolecules(reactantCloned);
    for (int z = 0; z < moleculeSetP.getAtomContainerCount(); z++)
      reaction.addProduct((IAtomContainer) moleculeSetP.getAtomContainer(z));

    return reaction;
  }
  @TestMethod("testDetectAromaticity_IAtomContainer")
  public static boolean detectAromaticity(IAtomContainer atomContainer) throws CDKException {
    SpanningTree spanningTree = new SpanningTree(atomContainer);
    IAtomContainer ringSystems = spanningTree.getCyclicFragmentsContainer();
    if (ringSystems.getAtomCount() == 0) {
      // If there are no rings, then there cannot be any aromaticity
      return false;
    }
    // disregard all atoms we know that cannot be aromatic anyway
    for (IAtom atom : ringSystems.atoms())
      if (!atomIsPotentiallyAromatic(atom))
        ringSystems.removeAtomAndConnectedElectronContainers(atom);

    // FIXME: should not really mark them here
    Iterator<IAtom> atoms = ringSystems.atoms().iterator();
    while (atoms.hasNext()) atoms.next().setFlag(CDKConstants.ISINRING, true);
    Iterator<IBond> bonds = ringSystems.bonds().iterator();
    while (bonds.hasNext()) bonds.next().setFlag(CDKConstants.ISINRING, true);

    boolean foundSomeAromaticity = false;
    Iterator<IAtomContainer> isolatedRingSystems =
        ConnectivityChecker.partitionIntoMolecules(ringSystems).atomContainers().iterator();
    while (isolatedRingSystems.hasNext()) {
      IAtomContainer isolatedSystem = isolatedRingSystems.next();
      IRingSet singleRings = new SSSRFinder(isolatedSystem).findSSSR();
      Iterator<IAtomContainer> singleRingsIterator = singleRings.atomContainers().iterator();
      int maxRingSize = 20;
      boolean atLeastOneRingIsSprouted = false;
      boolean allRingsAreAromatic = true;
      // test single rings in SSSR
      while (singleRingsIterator.hasNext()) {
        IAtomContainer singleRing = singleRingsIterator.next();
        if (singleRing.getAtomCount() > maxRingSize) maxRingSize = singleRing.getAtomCount();
        if (isRingSystemSproutedWithNonRingDoubleBonds(atomContainer, singleRing)) {
          //					OK, this ring is not aromatic
          atLeastOneRingIsSprouted = true;
          allRingsAreAromatic = false;
        } else {
          // possibly aromatic
          boolean ringIsAromatic = isHueckelValid(singleRing);
          foundSomeAromaticity |= ringIsAromatic;
          allRingsAreAromatic &= ringIsAromatic;
          if (ringIsAromatic) markRingAtomsAndBondsAromatic(singleRing);
        }
      }
      // OK, what about the one larger ring (if no aromaticity found in SSSR)?
      if (!allRingsAreAromatic
          && !atLeastOneRingIsSprouted
          && singleRings.getAtomContainerCount() <= 3) {
        // every ring system consisting of more than two rings is too difficult
        Iterator<IAtomContainer> allRingsIterator =
            new AllRingsFinder()
                .findAllRingsInIsolatedRingSystem(isolatedSystem)
                .atomContainers()
                .iterator();
        while (allRingsIterator.hasNext()) {
          // there should be exactly three rings, of which only one has a size larger
          // than the two previous ones
          IAtomContainer ring = allRingsIterator.next();
          if (ring.getAtomCount() <= maxRingSize) {
            // possibly aromatic
            boolean ringIsAromatic = isHueckelValid(ring);
            foundSomeAromaticity |= ringIsAromatic;
            if (ringIsAromatic) markRingAtomsAndBondsAromatic(ring);
          }
        }
      }
    }

    return foundSomeAromaticity;
  }