Logo Search packages:      
Sourcecode: cdk version File versions  Download package

IReaction org::openscience::cdk::io::MDLRXNReader::readReaction ( IChemObjectBuilder  builder ) throws CDKException [inline, private]

Read a Reaction from a file in MDL RXN format

Returns:
The Reaction that was read from the MDL file.

Definition at line 289 of file MDLRXNReader.java.

References org::openscience::cdk::interfaces::IAtomContainer::add(), org::openscience::cdk::interfaces::IReaction::addMapping(), org::openscience::cdk::interfaces::IReaction::addProduct(), org::openscience::cdk::interfaces::IReaction::addReactant(), org::openscience::cdk::tools::LoggingTool::debug(), org::openscience::cdk::interfaces::IAtomContainer::getAtom(), org::openscience::cdk::interfaces::IAtomContainer::getAtomCount(), org::openscience::cdk::interfaces::IChemObject::getID(), org::openscience::cdk::interfaces::IReaction::getProducts(), org::openscience::cdk::interfaces::IReaction::getReactants(), org::openscience::cdk::tools::LoggingTool::info(), org::openscience::cdk::interfaces::IMoleculeSet::molecules(), and org::openscience::cdk::io::MDLReader::read().

                                                                                   {
      logger.debug("Reading new reaction");
        int linecount = 0;
      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();
            linecount++;
            if (countsLine == null) {
                return null;
            }
            logger.debug("Line " + linecount + ": " + countsLine);
            if (countsLine.startsWith("$$$$")) {
                logger.debug("File is empty, returning empty reaction");
                return reaction;
            }
            /* 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
                MDLReader reader = new MDLReader(
                  new StringReader(molFile.toString()));
                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
                MDLReader reader = new MDLReader(
                    new StringReader(molFile.toString()),
                    super.mode
                );
                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();
        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;
    }

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index