示例#1
0
  /** @cdk.bug 1535055 */
  @Test
  public void testBug1535055() throws Exception {
    SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
    IAtomContainer mol = sp.parseSmiles("COC(=O)c1ccc2c(c1)c1ccccc1[nH]2");
    CDKHueckelAromaticityDetector.detectAromaticity(mol);
    AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
    SmilesGenerator sg = new SmilesGenerator();
    sg.setUseAromaticityFlag(true);
    String s1 = sg.createSMILES(mol);

    String filename = "data/cml/bug1535055.cml";
    InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
    CMLReader reader = new CMLReader(ins);
    IChemFile chemFile = (IChemFile) reader.read(new ChemFile());

    // test the resulting ChemFile content
    Assert.assertNotNull(chemFile);
    IAtomContainer mol2 = ChemFileManipulator.getAllAtomContainers(chemFile).get(0);
    CDKHueckelAromaticityDetector.detectAromaticity(mol2);
    AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol2);
    String s2 = sg.createSMILES(mol2);

    Assert.assertTrue(s1.contains("[nH]"));
    Assert.assertTrue(s2.contains("[nH]"));
  }
 /**
  * Sets the given cml. Does not update other fields like for example smiles
  *
  * @param cml
  */
 public void setCML(String cml) {
   InputStream inputStream = new ByteArrayInputStream(cml.getBytes());
   CMLReader cmlReader = new CMLReader(inputStream);
   try {
     IChemFile readFile = (IChemFile) cmlReader.read(new ChemFile());
     setAtomContainer((AtomContainer) ChemFileManipulator.getAllAtomContainers(readFile).get(0));
   } catch (CDKException e) {
     throw new RuntimeException("failed to read atomContainer", e);
   }
 }
  /**
   * Gets the mol file from a specified KEGG ID.
   *
   * @param accessionID the accession id (e.g. C00509)
   * @return the string
   * @throws CDKException
   */
  public static IAtomContainer getMol(String accessionID, String keggPath, boolean getAll)
      throws CDKException {
    String ret = "";

    try {
      KEGGLocator locator = new KEGGLocator();
      KEGGPortType serv = locator.getKEGGPort();
      // get molecule by accession ID
      String str = "-f m cpd:" + accessionID;

      boolean webservice = false;
      String mol = "";

      try {
        // Open the file that is the first
        // command line parameter
        FileInputStream fstream = new FileInputStream(keggPath + accessionID + ".mol");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        // Read File Line By Line
        String line;
        while ((line = br.readLine()) != null) {
          // Print the content on the console
          mol += line + "\n";
        }
        // Close the input stream
        in.close();

      } catch (FileNotFoundException e) {
        webservice = true;
      }

      // if this file is not found use webservice...otherwise use local molfile
      if (webservice) ret = serv.bget(str);
      else ret = mol;

    } catch (Exception e) {
      System.err.println(e.toString());
    }

    MDLReader reader;
    List<IAtomContainer> containersList;

    reader = new MDLReader(new StringReader(ret));
    ChemFile chemFile = (ChemFile) reader.read((ChemObject) new ChemFile());
    containersList = ChemFileManipulator.getAllAtomContainers(chemFile);
    IAtomContainer molecule = containersList.get(0);

    if (getAll) return molecule;
    if (!MolecularFormulaTools.isBiologicalCompound(molecule)) molecule = null;

    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);
 }
 public IAtomContainer getAtomContainer() {
   if (atomContainer == null) {
     InputStream in = FileStoreKeeper.FILE_STORE.retrieve(fileStoreKey);
     CMLReader cmlReader = new CMLReader(in);
     try {
       IChemFile readFile = (IChemFile) cmlReader.read(new ChemFile());
       setAtomContainer((AtomContainer) ChemFileManipulator.getAllAtomContainers(readFile).get(0));
     } catch (CDKException e) {
       throw new RuntimeException("failed to read atomContainer", e);
     }
   }
   return atomContainer;
 }
示例#6
0
 /**
  * A unit test for JUnit
  *
  * @exception Exception Description of the Exception
  */
 @Test
 public void testResolveOverlap2() throws Exception {
   logger.debug("Test case with neither bond nor atom overlap");
   String filename = "data/cml/overlaptest2.cml";
   InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
   CMLReader reader = new CMLReader(ins);
   IChemFile chemFile = (IChemFile) reader.read(new ChemFile());
   IAtomContainer atomContainer =
       (IAtomContainer) ChemFileManipulator.getAllAtomContainers(chemFile).get(0);
   // MoleculeViewer2D.display(new AtomContainer(atomContainer), false);
   double score = new OverlapResolver().getOverlapScore(atomContainer, new Vector(), new Vector());
   Assert.assertEquals(0.0, score, 0.0001);
   logger.debug("End of test case with neither bond nor atom overlap");
 }
 @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);
  }
示例#9
0
 /**
  * A unit test for JUnit
  *
  * @exception Exception Description of the Exception
  */
 @Test
 public void testResolveOverlap4() throws Exception {
   double overlapScore = 0;
   logger.debug("Test case with atom clash");
   String filename = "data/cml/overlaptest.cml";
   InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
   CMLReader reader = new CMLReader(ins);
   IChemFile chemFile = (IChemFile) reader.read(new ChemFile());
   IAtomContainer atomContainer =
       (IAtomContainer) ChemFileManipulator.getAllAtomContainers(chemFile).get(0);
   // MoleculeViewer2D.display(new AtomContainer(atomContainer), false);
   OverlapResolver or = new OverlapResolver();
   overlapScore = or.resolveOverlap(atomContainer, null);
   // MoleculeViewer2D.display(new AtomContainer(atomContainer), false);
   Assert.assertEquals(0.0, overlapScore, 0.0001);
   logger.debug("End of test case with atom clash");
 }
示例#10
0
  @Test
  public void checkUniqueMolecularDescriptorNames() throws Exception {
    DescriptorEngine engine = new DescriptorEngine(DescriptorEngine.MOLECULAR);
    List<DescriptorSpecification> specs = engine.getDescriptorSpecifications();

    // we work with a simple molecule with 3D coordinates
    String filename = "data/mdl/lobtest2.sdf";
    InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
    ISimpleChemObjectReader reader = new MDLV2000Reader(ins);
    ChemFile content = (ChemFile) reader.read(new ChemFile());
    List cList = ChemFileManipulator.getAllAtomContainers(content);
    IAtomContainer ac = (IAtomContainer) cList.get(0);
    AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(ac);

    engine.process(ac);

    int ncalc = 0;
    List<String> descNames = new ArrayList<String>();
    for (DescriptorSpecification spec : specs) {
      DescriptorValue value = (DescriptorValue) ac.getProperty(spec);
      if (value == null) continue;
      ncalc++;
      String[] names = value.getNames();
      descNames.addAll(Arrays.asList(names));
    }

    List<String> dups = new ArrayList<String>();
    Set<String> uniqueNames = new HashSet<String>();
    for (String name : descNames) {
      if (!uniqueNames.add(name)) dups.add(name);
    }
    Assert.assertEquals(specs.size(), ncalc);
    Assert.assertEquals(descNames.size(), uniqueNames.size());
    if (dups.size() != 0) {
      System.out.println("Following names were duplicated");
      for (String dup : dups) {
        System.out.println("dup = " + dup);
      }
    }
  }
示例#11
0
  @Test
  public void testMDMoleculeCustomizationRoundtripping() throws Exception {
    StringWriter writer = new StringWriter();

    CMLWriter cmlWriter = new CMLWriter(writer);
    cmlWriter.registerCustomizer(new MDMoleculeCustomizer());
    MDMolecule molecule = makeMDBenzene();
    cmlWriter.write(molecule);

    String serializedMol = writer.toString();
    logger.debug("****************************** testMDMoleculeCustomizationRoundtripping()");
    logger.debug(serializedMol);
    logger.debug("******************************");
    logger.debug("****************************** testMDMoleculeCustomization Write first");
    logger.debug(serializedMol);
    logger.debug("******************************");

    CMLReader reader = new CMLReader(new ByteArrayInputStream(serializedMol.getBytes()));
    reader.registerConvention("md:mdMolecule", new MDMoleculeConvention(new ChemFile()));
    IChemFile file = (IChemFile) reader.read(new ChemFile());
    List containers = ChemFileManipulator.getAllAtomContainers(file);
    Assert.assertEquals(1, containers.size());

    Object molecule2 = containers.get(0);
    Assert.assertTrue(molecule2 instanceof MDMolecule);
    MDMolecule mdMol = (MDMolecule) molecule2;

    Assert.assertEquals(6, mdMol.getAtomCount());
    Assert.assertEquals(6, mdMol.getBondCount());

    List residues = mdMol.getResidues();
    Assert.assertEquals(2, residues.size());
    Assert.assertEquals(3, ((Residue) residues.get(0)).getAtomCount());
    Assert.assertEquals(3, ((Residue) residues.get(1)).getAtomCount());
    Assert.assertEquals("myResidue1", ((Residue) residues.get(0)).getName());
    Assert.assertEquals("myResidue2", ((Residue) residues.get(1)).getName());
    Assert.assertEquals(0, ((Residue) residues.get(0)).getNumber());
    Assert.assertEquals(1, ((Residue) residues.get(1)).getNumber());

    List chargeGroup = mdMol.getChargeGroups();
    Assert.assertEquals(2, chargeGroup.size());
    Assert.assertEquals(2, ((ChargeGroup) chargeGroup.get(0)).getAtomCount());
    Assert.assertEquals(4, ((ChargeGroup) chargeGroup.get(1)).getAtomCount());
    Assert.assertNotNull(((ChargeGroup) chargeGroup.get(0)).getSwitchingAtom());
    Assert.assertEquals("a2", ((ChargeGroup) chargeGroup.get(0)).getSwitchingAtom().getID());
    Assert.assertNotNull(((ChargeGroup) chargeGroup.get(1)).getSwitchingAtom());
    Assert.assertEquals("a5", ((ChargeGroup) chargeGroup.get(1)).getSwitchingAtom().getID());

    Assert.assertEquals(2, ((ChargeGroup) chargeGroup.get(0)).getNumber());
    Assert.assertEquals(3, ((ChargeGroup) chargeGroup.get(1)).getNumber());

    writer = new StringWriter();

    cmlWriter = new CMLWriter(writer);
    cmlWriter.registerCustomizer(new MDMoleculeCustomizer());
    cmlWriter.write(mdMol);

    String serializedMDMol = writer.toString();
    logger.debug("****************************** testMDMoleculeCustomizationRoundtripping()");
    logger.debug(serializedMol);
    logger.debug("******************************");
    logger.debug("****************************** testMDMoleculeCustomization Write second");
    logger.debug(serializedMDMol);
    logger.debug("******************************");

    Assert.assertEquals(serializedMol, serializedMDMol);
  }
示例#12
0
文件: Misc.java 项目: egonw/cdkr
  /**
   * Loads one or more files into IAtomContainer objects.
   *
   * <p>This method does not need knowledge of the format since it is autodetected. Note that if
   * aromaticity detection or atom typing is specified and fails for a specific molecule, that
   * molecule will be set to <i>null</i>
   *
   * @param filenames An array of String's containing the filenames of the structures we want to
   *     load
   * @param doAromaticity If true, then aromaticity perception is performed
   * @param doTyping If true, atom typing and configuration is performed. This will use the internal
   *     CDK atom typing scheme
   * @return An array of AtoContainer's
   * @throws CDKException if there is an error when reading a file
   */
  public static IAtomContainer[] loadMolecules(
      String[] filenames, boolean doAromaticity, boolean doTyping, boolean doIsotopes)
      throws CDKException, IOException {
    Vector<IAtomContainer> v = new Vector<IAtomContainer>();
    IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();
    try {
      int i;
      int j;

      for (i = 0; i < filenames.length; i++) {
        File input = new File(filenames[i]);
        ReaderFactory readerFactory = new ReaderFactory();
        ISimpleChemObjectReader reader = readerFactory.createReader(new FileReader(input));

        if (reader == null) { // see if it's a SMI file
          if (filenames[i].endsWith(".smi")) {
            reader = new SMILESReader(new FileReader(input));
          }
        }
        IChemFile content = (IChemFile) reader.read(builder.newInstance(IChemFile.class));
        if (content == null) continue;

        List<IAtomContainer> c = ChemFileManipulator.getAllAtomContainers(content);

        // we should do this loop in case we have files
        // that contain multiple molecules
        v.addAll(c);
      }

    } catch (Exception e) {
      e.printStackTrace();
      throw new CDKException(e.toString());
    }

    // convert the vector to a simple array
    IAtomContainer[] retValues = new IAtomContainer[v.size()];
    for (int i = 0; i < v.size(); i++) {
      retValues[i] = v.get(i);
    }

    // before returning, lets make see if we
    // need to perceive aromaticity and atom typing
    if (doAromaticity) {
      for (int i = 0; i < retValues.length; i++) {
        try {
          CDKHueckelAromaticityDetector.detectAromaticity(retValues[i]);
        } catch (CDKException e) {
          retValues[i] = null;
        }
      }
    }

    if (doTyping) {
      for (int i = 0; i < retValues.length; i++) {
        try {
          AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(retValues[i]);
        } catch (CDKException e) {
          retValues[i] = null;
        }
      }
    }

    if (doIsotopes) {
      IsotopeFactory ifac = IsotopeFactory.getInstance(DefaultChemObjectBuilder.getInstance());
      for (IAtomContainer retValue : retValues) {
        ifac.configureAtoms(retValue);
      }
    }

    return retValues;
  }