/**
   * Read a Reaction from a file in MDL RXN format
   *
   * @return The Reaction that was read from the MDL file.
   */
  private IReaction readReaction(IChemObjectBuilder builder) throws CDKException {
    IReaction reaction = builder.newReaction();
    try {
      input.readLine(); // first line should be $RXN
      input.readLine(); // second line
      input.readLine(); // third line
      input.readLine(); // fourth line
    } catch (IOException exception) {
      logger.debug(exception);
      throw new CDKException("Error while reading header of RXN file", exception);
    }

    int reactantCount = 0;
    int productCount = 0;
    try {
      String countsLine = input.readLine();
      /* this line contains the number of reactants
      and products */
      StringTokenizer tokenizer = new StringTokenizer(countsLine);
      reactantCount = Integer.valueOf(tokenizer.nextToken()).intValue();
      logger.info("Expecting " + reactantCount + " reactants in file");
      productCount = Integer.valueOf(tokenizer.nextToken()).intValue();
      logger.info("Expecting " + productCount + " products in file");
    } catch (Exception exception) {
      logger.debug(exception);
      throw new CDKException("Error while counts line of RXN file", exception);
    }

    // now read the reactants
    try {
      for (int i = 1; i <= reactantCount; i++) {
        StringBuffer molFile = new StringBuffer();
        input.readLine(); // announceMDLFileLine
        String molFileLine = "";
        do {
          molFileLine = input.readLine();
          molFile.append(molFileLine);
          molFile.append(System.getProperty("line.separator"));
        } while (!molFileLine.equals("M  END"));

        // read MDL molfile content
        // Changed this to mdlv2000 reader
        MDLV2000Reader reader =
            new MDLV2000Reader(new StringReader(molFile.toString()), super.mode);
        IMolecule reactant = (IMolecule) reader.read(builder.newMolecule());

        // add reactant
        reaction.addReactant(reactant);
      }
    } catch (CDKException exception) {
      // rethrow exception from MDLReader
      throw exception;
    } catch (Exception exception) {
      logger.debug(exception);
      throw new CDKException("Error while reading reactant", exception);
    }

    // now read the products
    try {
      for (int i = 1; i <= productCount; i++) {
        StringBuffer molFile = new StringBuffer();
        input.readLine(); // String announceMDLFileLine =
        String molFileLine = "";
        do {
          molFileLine = input.readLine();
          molFile.append(molFileLine);
          molFile.append(System.getProperty("line.separator"));
        } while (!molFileLine.equals("M  END"));

        // read MDL molfile content
        MDLV2000Reader reader = new MDLV2000Reader(new StringReader(molFile.toString()));
        IMolecule product = (IMolecule) reader.read(builder.newMolecule());

        // add reactant
        reaction.addProduct(product);
      }
    } catch (CDKException exception) {
      // rethrow exception from MDLReader
      throw exception;
    } catch (Exception exception) {
      logger.debug(exception);
      throw new CDKException("Error while reading products", exception);
    }

    // now try to map things, if wanted
    logger.info("Reading atom-atom mapping from file");
    // distribute all atoms over two AtomContainer's
    IAtomContainer reactingSide = builder.newAtomContainer();
    java.util.Iterator molecules = reaction.getReactants().molecules().iterator();
    while (molecules.hasNext()) {
      reactingSide.add((IMolecule) molecules.next());
    }
    IAtomContainer producedSide = builder.newAtomContainer();
    molecules = reaction.getProducts().molecules().iterator();
    while (molecules.hasNext()) {
      producedSide.add((IMolecule) molecules.next());
    }

    // map the atoms
    int mappingCount = 0;
    //        IAtom[] reactantAtoms = reactingSide.getAtoms();
    //        IAtom[] producedAtoms = producedSide.getAtoms();
    for (int i = 0; i < reactingSide.getAtomCount(); i++) {
      for (int j = 0; j < producedSide.getAtomCount(); j++) {
        IAtom eductAtom = reactingSide.getAtom(i);
        IAtom productAtom = producedSide.getAtom(j);
        if (eductAtom.getID() != null && eductAtom.getID().equals(productAtom.getID())) {
          reaction.addMapping(builder.newMapping(eductAtom, productAtom));
          mappingCount++;
          break;
        }
      }
    }
    logger.info("Mapped atom pairs: " + mappingCount);

    return reaction;
  }
Example #2
0
  private IReaction readReaction(IChemObjectBuilder builder) throws CDKException {
    IReaction reaction = builder.newInstance(IReaction.class);
    readLine(); // first line should be $RXN
    readLine(); // second line
    readLine(); // third line
    readLine(); // fourth line

    int reactantCount = 0;
    int productCount = 0;
    boolean foundCOUNTS = false;
    while (isReady() && !foundCOUNTS) {
      String command = readCommand();
      if (command.startsWith("COUNTS")) {
        StringTokenizer tokenizer = new StringTokenizer(command);
        try {
          tokenizer.nextToken();
          reactantCount = Integer.valueOf(tokenizer.nextToken()).intValue();
          logger.info("Expecting " + reactantCount + " reactants in file");
          productCount = Integer.valueOf(tokenizer.nextToken()).intValue();
          logger.info("Expecting " + productCount + " products in file");
        } catch (Exception exception) {
          logger.debug(exception);
          throw new CDKException("Error while counts line of RXN file", exception);
        }
        foundCOUNTS = true;
      } else {
        logger.warn("Waiting for COUNTS line, but found: " + command);
      }
    }

    // now read the reactants
    for (int i = 1; i <= reactantCount; i++) {
      StringBuffer molFile = new StringBuffer();
      String announceMDLFileLine = readCommand();
      if (!announceMDLFileLine.equals("BEGIN REACTANT")) {
        String error = "Excepted start of reactant, but found: " + announceMDLFileLine;
        logger.error(error);
        throw new CDKException(error);
      }
      String molFileLine = "";
      while (!molFileLine.endsWith("END REACTANT")) {
        molFileLine = readLine();
        molFile.append(molFileLine);
        molFile.append(System.getProperty("line.separator"));
      }
      ;

      try {
        // read MDL molfile content
        MDLV3000Reader reader =
            new MDLV3000Reader(new StringReader(molFile.toString()), super.mode);
        IAtomContainer reactant =
            (IAtomContainer) reader.read(builder.newInstance(IAtomContainer.class));

        // add reactant
        reaction.addReactant(reactant);
      } catch (Exception exception) {
        String error = "Error while reading reactant: " + exception.getMessage();
        logger.error(error);
        logger.debug(exception);
        throw new CDKException(error, exception);
      }
    }

    // now read the products
    for (int i = 1; i <= productCount; i++) {
      StringBuffer molFile = new StringBuffer();
      String announceMDLFileLine = readCommand();
      if (!announceMDLFileLine.equals("BEGIN PRODUCT")) {
        String error = "Excepted start of product, but found: " + announceMDLFileLine;
        logger.error(error);
        throw new CDKException(error);
      }
      String molFileLine = "";
      while (!molFileLine.endsWith("END PRODUCT")) {
        molFileLine = readLine();
        molFile.append(molFileLine);
        molFile.append(System.getProperty("line.separator"));
      }
      ;

      try {
        // read MDL molfile content
        MDLV3000Reader reader = new MDLV3000Reader(new StringReader(molFile.toString()));
        IAtomContainer product =
            (IAtomContainer) reader.read(builder.newInstance(IAtomContainer.class));

        // add product
        reaction.addProduct(product);
      } catch (Exception exception) {
        String error = "Error while reading product: " + exception.getMessage();
        logger.error(error);
        logger.debug(exception);
        throw new CDKException(error, exception);
      }
    }

    return reaction;
  }
  /**
   * 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;
  }