Example #1
0
 /**
  * Creates a bucky ball molecule.
  *
  * @return bucky ball molecule
  */
 private IAtomContainer createBuckyBall() throws CDKException {
   IAtomContainer molecule = null;
   String filename = "data/mdl/buckyball.mol";
   InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
   MDLV2000Reader reader = new MDLV2000Reader(ins, Mode.STRICT);
   molecule = (IAtomContainer) reader.read(new AtomContainer());
   Assert.assertTrue("Atom count is 60 ", molecule.getAtomCount() == 60);
   Assert.assertTrue("Bond count is 90 ", molecule.getBondCount() == 90);
   return molecule;
 }
 /*
  *  @cdk.bug 1241421
  */
 @Test
 public void testModelBuilder3D_bug_1241421() throws Exception {
   ModelBuilder3D mb3d = ModelBuilder3D.getInstance();
   String filename = "data/mdl/bug1241421.mol";
   InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
   MDLV2000Reader reader = new MDLV2000Reader(ins);
   ChemFile chemFile = (ChemFile) reader.read((ChemObject) new ChemFile());
   List containersList = ChemFileManipulator.getAllAtomContainers(chemFile);
   IMolecule ac = new NNMolecule((IAtomContainer) containersList.get(0));
   ac = mb3d.generate3DCoordinates(ac, false);
   checkAverageBondLength(ac);
 }
Example #3
0
  /** @cdk.bug 891021 */
  @Test
  public void testBug891021() throws Exception {
    IAtomContainer molecule = null;
    String filename = "data/mdl/too.many.rings.mol";
    InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
    MDLV2000Reader reader = new MDLV2000Reader(ins, Mode.STRICT);
    molecule = (IAtomContainer) reader.read((IChemObject) new AtomContainer());
    logger.debug("Testing " + filename);

    IRingSet ringSet = new SSSRFinder(molecule).findSSSR();
    logger.debug("Found ring set of size: " + ringSet.getAtomContainerCount());
    Assert.assertEquals(57, ringSet.getAtomContainerCount());
  }
 @Test
 public void testModelBuilder3D_reserpine() throws Exception {
   ModelBuilder3D mb3d = ModelBuilder3D.getInstance();
   String filename = "data/mdl/reserpine.mol";
   InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
   MDLV2000Reader reader = new MDLV2000Reader(ins);
   ChemFile chemFile = (ChemFile) reader.read((ChemObject) new ChemFile());
   List containersList = ChemFileManipulator.getAllAtomContainers(chemFile);
   IMolecule ac = new NNMolecule((IAtomContainer) containersList.get(0));
   ac = mb3d.generate3DCoordinates(ac, false);
   for (int i = 0; i < ac.getAtomCount(); i++) {
     Assert.assertNotNull(ac.getAtom(i).getPoint3d());
   }
   checkAverageBondLength(ac);
 }
  /** @cdk.bug 1315823 */
  @Test
  public void testModelBuilder3D_232() throws Exception {
    Assume.assumeTrue(runSlowTests());

    ModelBuilder3D mb3d = ModelBuilder3D.getInstance();
    String filename = "data/mdl/allmol232.mol";
    InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
    MDLV2000Reader reader = new MDLV2000Reader(ins);
    ChemFile chemFile = (ChemFile) reader.read((ChemObject) new ChemFile());
    List containersList = ChemFileManipulator.getAllAtomContainers(chemFile);
    IMolecule ac = new NNMolecule((IAtomContainer) containersList.get(0));
    addExplicitHydrogens(ac);
    ac = mb3d.generate3DCoordinates(ac, false);
    Assert.assertNotNull(ac.getAtom(0).getPoint3d());
    checkAverageBondLength(ac);
  }
Example #6
0
  @Test
  public void testLoopProblem() throws Exception {
    String filename = "data/mdl/ring_03419.mol";
    InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
    MDLV2000Reader reader = new MDLV2000Reader(ins, Mode.STRICT);
    IAtomContainer molecule = (IAtomContainer) reader.read((IChemObject) new AtomContainer());
    logger.debug("Testing " + filename);

    IRingSet ringSet = new SSSRFinder(molecule).findSSSR();
    logger.debug("Found ring set of size: " + ringSet.getAtomContainerCount());
    Assert.assertEquals(12, ringSet.getAtomContainerCount());
    for (int f = 0; f < ringSet.getAtomContainerCount(); f++) {
      IRing ring = (IRing) ringSet.getAtomContainer(f);
      logger.debug("ring: " + toString(ring, molecule));
    }
  }
Example #7
0
 /** A unit test for JUnit */
 @Test
 public void testCycloOctadien() {
   try {
     String filename = "data/mdl/cyclooctadien.mol";
     InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
     MDLV2000Reader reader = new MDLV2000Reader(ins, Mode.STRICT);
     IAtomContainer mol1 = reader.read(new AtomContainer());
     SmilesGenerator sg = new SmilesGenerator();
     String moleculeSmile = sg.createSMILES(mol1);
     Assert.assertEquals(moleculeSmile, "C=1CCC=CCCC=1");
   } catch (Exception exc) {
     exc.printStackTrace();
     System.out.println(exc);
     Assert.fail(exc.getMessage());
   }
 }
Example #8
0
 /**
  * A unit test for JUnit
  *
  * @cdk.bug 1089770
  */
 @Test
 public void testSFBug1089770_1() {
   try {
     String filename = "data/mdl/bug1089770-1.mol";
     InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
     MDLV2000Reader reader = new MDLV2000Reader(ins, Mode.STRICT);
     IAtomContainer mol1 = reader.read(new AtomContainer());
     SmilesGenerator sg = new SmilesGenerator();
     String moleculeSmile = sg.createSMILES(mol1);
     // logger.debug(filename + " -> " + moleculeSmile);
     Assert.assertEquals(moleculeSmile, "C1CCC=2CCCC=2(C1)");
   } catch (Exception exc) {
     exc.printStackTrace();
     System.out.println(exc);
     Assert.fail(exc.getMessage());
   }
 }
  /** Returns true if another IMolecule can be read. */
  public boolean hasNext() {
    if (!nextAvailableIsKnown) {
      hasNext = false;

      // now try to parse the next Molecule
      try {
        if ((currentLine = input.readLine()) != null) {
          currentFormat = (IChemFormat) MDLFormat.getInstance();
          StringBuffer buffer = new StringBuffer();
          while (currentLine != null && !currentLine.equals("M  END")) {
            // still in a molecule
            buffer.append(currentLine);
            buffer.append(System.getProperty("line.separator"));

            currentLine = input.readLine();

            // do MDL molfile version checking
            if (currentLine.contains("V2000") || currentLine.contains("v2000")) {
              currentFormat = (IChemFormat) MDLV2000Format.getInstance();
            } else if (currentLine.contains("V3000") || currentLine.contains("v3000")) {
              currentFormat = (IChemFormat) MDLV3000Format.getInstance();
            }
          }
          buffer.append(currentLine);
          buffer.append(System.getProperty("line.separator"));
          logger.debug("MDL file part read: ", buffer);
          ISimpleChemObjectReader reader = factory.createReader(currentFormat);
          reader.setReader(new StringReader(buffer.toString()));
          if (currentFormat instanceof MDLV2000Format) {
            reader.addChemObjectIOListener(this);
            ((MDLV2000Reader) reader).customizeJob();
          }
          nextMolecule = (IMolecule) reader.read(builder.newInstance(IMolecule.class));

          // note that a molecule may have 0 atoms, but still
          // be useful (by having SD tags for example), so just
          // check for null'ness rather than atom count
          hasNext = nextMolecule != null;

          // now read the data part
          currentLine = input.readLine();
          readDataBlockInto(nextMolecule);
        } else {
          hasNext = false;
        }
      } catch (Exception exception) {
        logger.error("Error while reading next molecule: " + exception.getMessage());
        logger.debug(exception);
        hasNext = false;
      }
      if (!hasNext) nextMolecule = null;
      nextAvailableIsKnown = true;
    }
    return hasNext;
  }
  /**
   * 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;
  }