/**
   * Calculates the eccentric connectivity
   *
   * @param container Parameter is the atom container.
   * @return An IntegerResult value representing the eccentric connectivity index
   */
  @TestMethod("testCalculate_IAtomContainer")
  public DescriptorValue calculate(IAtomContainer container) {
    IAtomContainer local = AtomContainerManipulator.removeHydrogens(container);

    int natom = local.getAtomCount();
    int[][] admat = AdjacencyMatrix.getMatrix(local);
    int[][] distmat = PathTools.computeFloydAPSP(admat);

    int eccenindex = 0;
    for (int i = 0; i < natom; i++) {
      int max = -1;
      for (int j = 0; j < natom; j++) {
        if (distmat[i][j] > max) max = distmat[i][j];
      }
      int degree = local.getConnectedBondsCount(i);
      eccenindex += max * degree;
    }
    IntegerResult retval = new IntegerResult(eccenindex);
    return new DescriptorValue(
        getSpecification(),
        getParameterNames(),
        getParameters(),
        retval,
        getDescriptorNames(),
        null);
  }
Exemplo n.º 2
0
  @TestMethod("testCalculate_IAtomContainer")
  public DescriptorValue calculate(IAtomContainer container) {

    // removeHydrogens does a deep copy, so no need to clone
    IAtomContainer localAtomContainer = AtomContainerManipulator.removeHydrogens(container);
    CDKAtomTypeMatcher matcher = CDKAtomTypeMatcher.getInstance(container.getBuilder());
    Iterator<IAtom> atoms = localAtomContainer.atoms().iterator();
    while (atoms.hasNext()) {
      IAtom atom = atoms.next();
      IAtomType type;
      try {
        type = matcher.findMatchingAtomType(localAtomContainer, atom);
        AtomTypeManipulator.configure(atom, type);
      } catch (Exception e) {
        return getDummyDescriptorValue(new CDKException("Error in atom typing: " + e.getMessage()));
      }
    }
    CDKHydrogenAdder hAdder = CDKHydrogenAdder.getInstance(container.getBuilder());
    try {
      hAdder.addImplicitHydrogens(localAtomContainer);
    } catch (CDKException e) {
      return getDummyDescriptorValue(
          new CDKException("Error in hydrogen addition: " + e.getMessage()));
    }

    List subgraph3 = order3(localAtomContainer);
    List subgraph4 = order4(localAtomContainer);
    List subgraph5 = order5(localAtomContainer);
    List subgraph6 = order6(localAtomContainer);

    double order3s = ChiIndexUtils.evalSimpleIndex(localAtomContainer, subgraph3);
    double order4s = ChiIndexUtils.evalSimpleIndex(localAtomContainer, subgraph4);
    double order5s = ChiIndexUtils.evalSimpleIndex(localAtomContainer, subgraph5);
    double order6s = ChiIndexUtils.evalSimpleIndex(localAtomContainer, subgraph6);

    double order3v, order4v, order5v, order6v;
    try {
      order3v = ChiIndexUtils.evalValenceIndex(localAtomContainer, subgraph3);
      order4v = ChiIndexUtils.evalValenceIndex(localAtomContainer, subgraph4);
      order5v = ChiIndexUtils.evalValenceIndex(localAtomContainer, subgraph5);
      order6v = ChiIndexUtils.evalValenceIndex(localAtomContainer, subgraph6);
    } catch (CDKException e) {
      return getDummyDescriptorValue(
          new CDKException("Error in substructure search: " + e.getMessage()));
    }
    DoubleArrayResult retval = new DoubleArrayResult();
    retval.add(order3s);
    retval.add(order4s);
    retval.add(order5s);
    retval.add(order6s);

    retval.add(order3v);
    retval.add(order4v);
    retval.add(order5v);
    retval.add(order6v);

    return new DescriptorValue(
        getSpecification(), getParameterNames(), getParameters(), retval, getDescriptorNames());
  }
Exemplo n.º 3
0
  @Test(timeout = 1000)
  public void testPyrrole_Silent() throws Exception {
    String smiles = "c2ccc3n([H])c1ccccc1c3(c2)";
    SmilesParser smilesParser = new SmilesParser(SilentChemObjectBuilder.getInstance());
    IAtomContainer molecule = smilesParser.parseSmiles(smiles);

    molecule = fbot.kekuliseAromaticRings(molecule);
    Assert.assertNotNull(molecule);

    molecule = (IAtomContainer) AtomContainerManipulator.removeHydrogens(molecule);
    int doubleBondCount = 0;
    for (int i = 0; i < molecule.getBondCount(); i++) {
      IBond bond = molecule.getBond(i);
      Assert.assertTrue(bond.getFlag(CDKConstants.ISAROMATIC));
      if (bond.getOrder() == Order.DOUBLE) doubleBondCount++;
    }
    Assert.assertEquals(6, doubleBondCount);
  }
Exemplo n.º 4
0
  @Test
  public void testLargeRingSystem() throws Exception {
    String smiles = "O=C1Oc6ccccc6(C(O)C1C5c2ccccc2CC(c3ccc(cc3)c4ccccc4)C5)";
    SmilesParser smilesParser = new SmilesParser(DefaultChemObjectBuilder.getInstance());
    IAtomContainer molecule = smilesParser.parseSmiles(smiles);

    molecule = fbot.kekuliseAromaticRings(molecule);
    Assert.assertNotNull(molecule);

    molecule = (IAtomContainer) AtomContainerManipulator.removeHydrogens(molecule);
    Assert.assertEquals(34, molecule.getAtomCount());

    // we should have 14 double bonds
    int doubleBondCount = 0;
    for (int i = 0; i < molecule.getBondCount(); i++) {
      IBond bond = molecule.getBond(i);
      if (bond.getOrder() == Order.DOUBLE) doubleBondCount++;
    }
    Assert.assertEquals(13, doubleBondCount);
  }
Exemplo n.º 5
0
  /** @cdk.bug 3506770 */
  @Test
  public void testLargeBioclipseUseCase() throws Exception {
    String smiles =
        "COc1ccc2[C@@H]3[C@H](COc2c1)C(C)(C)OC4=C3C(=O)C(=O)C5=C4OC(C)(C)[C@@H]6COc7cc(OC)ccc7[C@H]56";
    SmilesParser smilesParser = new SmilesParser(DefaultChemObjectBuilder.getInstance());
    IAtomContainer molecule = smilesParser.parseSmiles(smiles);

    molecule = fbot.kekuliseAromaticRings(molecule);
    Assert.assertNotNull(molecule);

    molecule = (IAtomContainer) AtomContainerManipulator.removeHydrogens(molecule);
    Assert.assertEquals(40, molecule.getAtomCount());

    // we should have 14 double bonds
    int doubleBondCount = 0;
    for (int i = 0; i < molecule.getBondCount(); i++) {
      IBond bond = molecule.getBond(i);
      if (bond.getOrder() == Order.DOUBLE) doubleBondCount++;
    }
    Assert.assertEquals(10, doubleBondCount);
  }
Exemplo n.º 6
0
  public static void main(final String[] args) {

    //        String mf;
    //        String molfile;

    try {
      //            BufferedReader reader = new BufferedReader(new FileReader("renderTest.mol"));
      //            StringBuffer   sbuff  = new StringBuffer();
      //            String         line;
      //
      //            while ( (line = reader.readLine()) != null ) {
      //                sbuff.append(line + "\n");
      //            }
      //            reader.close();
      //            molfile = sbuff.toString();
      //            System.out.println(molfile);

      // the graph
      SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
      PubChemWebService pw = new PubChemWebService();
      String PCID = "1148";
      IAtomContainer container = pw.getSingleMol(PCID);
      container = AtomContainerManipulator.removeHydrogens(container);
      //            IAtomContainer container = sp.parseSmiles("OC1C(O)=COC(CO)C1(O)");

      // Render.Draw(container, "test");

      IMolecule test = new Molecule(container);
      StructureDiagramGenerator sdg = new StructureDiagramGenerator();
      sdg.setMolecule(test);
      sdg.generateCoordinates();
      IMolecule mol = sdg.getMolecule();

      // adds implicit H atoms to molecule
      //    		CDKAtomTypeMatcher matcher = CDKAtomTypeMatcher.getInstance(mol.getBuilder());
      //    		for (IAtom atom : mol.atoms()) {
      //    			IAtomType type = matcher.findMatchingAtomType(mol, atom);
      //	    		AtomTypeManipulator.configure(atom, type);
      //			}
      //
      //    		CDKHydrogenAdder adder = CDKHydrogenAdder.getInstance(mol.getBuilder());
      //    		adder.addImplicitHydrogens(mol);
      //    		AtomContainerManipulator.convertImplicitToExplicitHydrogens(mol);

      // detects aromaticity; used to display aromatic ring systems correctly
      CDKHueckelAromaticityDetector.detectAromaticity(mol);

      Render.Draw(mol, "test");

      DisplayStructureVector test12 =
          new DisplayStructureVector(200, 200, "/home/swolf/", true, true);
      // test12.writeMOL2PNGFile(mol, "test_cdk12.png");
      //    		test12.writeMOL2PDFFile(mol, new File("/home/swolf/test_cdk12.pdf"));
      test12.writeMOL2SVGFile(mol, new File("/home/swolf/" + PCID + ".svg"));

      // ChemRenderer crender = new ChemRenderer();
      // crender.writeMOL2PNGFile(mol, new File("/home/swolf/test_new.png"));
      // crender.writeMOL2EPSFile(mol, new File("/home/swolf/test_new.eps"));
      // crender.writeMOL2PDFFile(mol, new File("/home/swolf/test_new.pdf"));

      // crender.writeMOL2SVGFile(molfile,new File("Output.svg"));

    } catch (Exception e) {
      System.out.println("Error: " + e.getMessage() + ".");
      e.printStackTrace();
    }
  }
Exemplo n.º 7
0
  /**
   * Calculate the count of atoms of the largest chain in the supplied {@link IAtomContainer}.
   *
   * <p>
   *
   * <p>The method require two parameters:
   *
   * <ol>
   *   <li>if checkAromaticity is true, the method check the aromaticity,
   *   <li>if false, means that the aromaticity has already been checked
   * </ol>
   *
   * <p>
   *
   * <p>Same for checkRingSystem, if true the CDKConstant.ISINRING will be set
   *
   * @param atomContainer The {@link AtomContainer} for which this descriptor is to be calculated
   * @return the number of atoms in the largest chain of this AtomContainer
   * @see #setParameters
   */
  @TestMethod("testCalculate_IAtomContainer")
  public DescriptorValue calculate(IAtomContainer atomContainer) {
    IAtomContainer container;
    try {
      container = (IAtomContainer) atomContainer.clone();
    } catch (CloneNotSupportedException e) {
      return getDummyDescriptorValue(e);
    }

    // logger.debug("LargestChainDescriptor");
    boolean[] originalFlag4 = new boolean[container.getAtomCount()];
    for (int i = 0; i < originalFlag4.length; i++) {
      originalFlag4[i] = container.getAtom(i).getFlag(4);
    }
    if (checkRingSystem) {
      IRingSet rs;
      try {
        rs = new SpanningTree(container).getBasicRings();
      } catch (NoSuchAtomException e) {
        return getDummyDescriptorValue(e);
      }
      for (int i = 0; i < container.getAtomCount(); i++) {
        if (rs.contains(container.getAtom(i))) {
          container.getAtom(i).setFlag(CDKConstants.ISINRING, true);
        }
      }
    }

    if (checkAromaticity) {
      try {
        AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(container);
        CDKHueckelAromaticityDetector.detectAromaticity(container);
      } catch (CDKException e) {
        return getDummyDescriptorValue(e);
      }
    }

    // get rid of hydrogens in our local copy
    container = AtomContainerManipulator.removeHydrogens(container);

    int largestChainAtomsCount = 0;
    // IAtom[] atoms = container.getAtoms();
    ArrayList<IAtom> startSphere;
    ArrayList<IAtom> path;
    // Set all VisitedFlags to False
    for (int i = 0; i < container.getAtomCount(); i++) {
      container.getAtom(i).setFlag(CDKConstants.VISITED, false);
    }

    // logger.debug("Set all atoms to Visited False");
    for (int i = 0; i < container.getAtomCount(); i++) {
      IAtom atomi = container.getAtom(i);
      // chain sp3
      // logger.debug("atom:"+i+" maxBondOrder:"+container.getMaximumBondOrder(atoms[i])+"
      // Aromatic:"+atoms[i].getFlag(CDKConstants.ISAROMATIC)+"
      // Ring:"+atoms[i].getFlag(CDKConstants.ISINRING)+"
      // FormalCharge:"+atoms[i].getFormalCharge()+" Charge:"+atoms[i].getCharge()+"
      // Flag:"+atoms[i].getFlag(CDKConstants.VISITED));
      if ((!atomi.getFlag(CDKConstants.ISAROMATIC) && !atomi.getFlag(CDKConstants.ISINRING))
          & !atomi.getFlag(CDKConstants.VISITED)) {
        // logger.debug("...... -> containercepted");
        startSphere = new ArrayList<IAtom>();
        path = new ArrayList<IAtom>();
        startSphere.add(atomi);
        try {
          breadthFirstSearch(container, startSphere, path);
        } catch (CDKException e) {
          return getDummyDescriptorValue(e);
        }
        if (path.size() > largestChainAtomsCount) {
          largestChainAtomsCount = path.size();
        }
      }
    }

    return new DescriptorValue(
        getSpecification(),
        getParameterNames(),
        getParameters(),
        new IntegerResult(largestChainAtomsCount),
        getDescriptorNames());
  }
Exemplo n.º 8
0
  /**
   * Process spectra in the following way: Fragment each hit in KEGG starting with the lowest
   * collision energy If hits were found they are used for the next measurement as input and the
   * original molecule is not used.
   *
   * @param folder the folder
   * @param file the file
   */
  private void processSpectra(String folder, String file, int treeDepth) {

    this.scoreMap = new HashMap<Integer, ArrayList<String>>();

    if (blackList.contains(file)) {
      completeLog += "Blacklisted Molecule: " + file;
      histogramReal += "\n" + file + "\tBLACKLIST\t";
      histogram += "\n" + file + "\tBLACKLIST\t";
      histogramCompare += "\n" + file + "\tBLACKLIST\t";
      return;
    }

    double exactMass = spectra.get(0).getExactMass();

    // timing
    long timeStart = System.currentTimeMillis();

    HashMap<Double, Vector<String>> realScoreMap = new HashMap<Double, Vector<String>>();
    int mode = spectra.get(0).getMode();

    // instantiate and read in CID-KEGG.txt
    String keggIdentifier = spectra.get(0).getKEGG();

    completeLog +=
        "\n\n============================================================================";
    completeLog +=
        "\nFile: " + spectra.get(0).getTrivialName() + " (KEGG Entry: " + keggIdentifier + ")";

    // get candidates from kegg webservice...with with a given mzppm and mzabs
    Vector<String> candidates =
        KeggWebservice.KEGGbyMass(exactMass, (mzabs + PPMTool.getPPMDeviation(exactMass, mzppm)));

    try {
      GetKEGGIdentifier keggID = new GetKEGGIdentifier(folder + "CID-KEGG/CID-KEGG.txt");
      // now find the corresponding KEGG entry
      if (keggID.existInKEGG(spectra.get(0).getCID()))
        keggIdentifier = keggID.getKEGGID(spectra.get(0).getCID());
    } catch (IOException e) {
      System.out.println(e.getMessage());
      completeLog += "Error! Message: " + e.getMessage();
    }

    // comparison histogram
    if (keggIdentifier.equals("none"))
      histogramCompare += "\n" + file + "\t" + keggIdentifier + "\t\t" + exactMass;
    else
      histogramCompare +=
          "\n" + file + "\t" + keggIdentifier + "\t" + candidates.size() + "\t" + exactMass;

    // list of peaks which are contained in the real molecule
    Vector<Peak> listOfPeaksCorresponding = new Vector<Peak>();
    // list of peaks which are not contained in the real molecule
    Vector<Peak> listOfPeaks = new Vector<Peak>();

    // loop over all hits
    for (int c = 0; c < candidates.size(); c++) {

      this.foundHits = new Vector<IAtomContainer>();

      // get mol file from kegg....remove "cpd:"
      String candidate = KeggWebservice.KEGGgetMol(candidates.get(c).substring(4), this.keggPath);
      IAtomContainer molecule = null;
      try {
        // write string to disk
        new File(folder + file + "_Mol").mkdir();
        File outFile = new File(folder + file + "_Mol/" + candidates.get(c).substring(4) + ".mol");
        FileWriter out = new FileWriter(outFile);
        out.write(candidate);
        out.close();
        // now fragment the retrieved molecule
        molecule = Molfile.Read(folder + file + "_Mol/" + candidates.get(c).substring(4) + ".mol");
        // now create a new folder to write the .mol files into
        new File(folder + file).mkdir();
        boolean status = new File(folder + file + "/" + candidates.get(c).substring(4)).mkdir();
      } catch (IOException e) {
        completeLog += "Error: " + candidates.get(c).substring(4) + " Message: " + e.getMessage();
      } catch (CDKException e) {
        completeLog += "Error: " + candidates.get(c).substring(4) + " Message: " + e.getMessage();
      }

      try {
        // add hydrogens
        CDKAtomTypeMatcher matcher = CDKAtomTypeMatcher.getInstance(molecule.getBuilder());

        for (IAtom atom : molecule.atoms()) {
          IAtomType type = matcher.findMatchingAtomType(molecule, atom);
          AtomTypeManipulator.configure(atom, type);
        }
        CDKHydrogenAdder hAdder = CDKHydrogenAdder.getInstance(molecule.getBuilder());
        hAdder.addImplicitHydrogens(molecule);
        AtomContainerManipulator.convertImplicitToExplicitHydrogens(molecule);
      }
      // there is a bug in cdk?? error happens when there is a S or Ti in the molecule
      catch (IllegalArgumentException e) {
        completeLog += "Error: " + candidates.get(c).substring(4) + " Message: " + e.getMessage();
        // skip it
        continue;
      } catch (CDKException e) {
        completeLog += "Error: " + candidates.get(c).substring(4) + " Message: " + e.getMessage();
        // skip it
        continue;
      }

      // get peak list....it is reset if this is not the first run
      Vector<Peak> peakList = spectra.get(0).getPeakList();
      // create a new instance
      Fragmenter fragmenter =
          new Fragmenter(
              (Vector<Peak>) peakList.clone(),
              mzabs,
              mzppm,
              mode,
              breakAromaticRings,
              quickRedundancy,
              false,
              false);

      double combinedScore = 0;
      int combinedHits = 0;
      int combinedPeakCount = 0;
      String peaks = "";
      combinedPeakCount = peakList.size();
      int count = 0;

      boolean first = true;
      // loop over the different collision energies
      for (WrapperSpectrum spectrum : spectra) {

        List<IAtomContainer> l = null;
        long start = System.currentTimeMillis();

        try {
          if (first) {
            try {
              l = fragmenter.generateFragmentsInMemory(molecule, true, treeDepth);
              count++;
            } catch (OutOfMemoryError e) {
              System.out.println("OUT OF MEMORY ERROR! " + candidates.get(c).substring(4));
              completeLog +=
                  "Error: " + candidates.get(c).substring(4) + " Message: " + e.getMessage();
              continue;
            }
            first = false;
          } else {
            peakList = spectrum.getPeakList();
            combinedPeakCount += peakList.size();
            // set the current peak list...
            fragmenter.setPeakList((Vector<Peak>) peakList.clone());
            try {
              //				        	l = fragmenter.generateFragmentsHierarchical(foundHits, true);
              System.err.println("REMOVED....no improvement!");
              System.exit(1);
              count++;
            } catch (OutOfMemoryError e) {
              System.out.println("OUT OF MEMORY ERROR! " + candidates.get(c).substring(4));
              completeLog +=
                  "Error: " + candidates.get(c).substring(4) + " Message: " + e.getMessage();
              continue;
            }
          }

          long time = System.currentTimeMillis() - start;
          System.out.println("Benötigte Zeit: " + time);
          System.out.println("Got " + l.size() + " fragments");
          System.out.println("Needed " + fragmenter.getNround() + " calls to generateFragments()");

          new File(
                  folder
                      + file
                      + "/"
                      + candidates.get(c).substring(4)
                      + "/"
                      + spectrum.getCollisionEnergy())
              .mkdir();

          for (int i = 0; i < l.size(); i++) {

            try {
              // write fragments to disk
              FileWriter w =
                  new FileWriter(
                      new File(
                          folder
                              + file
                              + "/"
                              + candidates.get(c).substring(4)
                              + "/"
                              + spectrum.getCollisionEnergy()
                              + "/frag_"
                              + i
                              + ".mol"));
              MDLWriter mw = new MDLWriter(w);
              mw.write(new Molecule(l.get(i)));
              mw.close();
            } catch (IOException e) {
              System.out.println("IOException: " + e.toString());
              completeLog +=
                  "Error: " + candidates.get(c).substring(4) + " Message: " + e.getMessage();
            } catch (Exception e) {
              System.out.println(e.toString());
              completeLog +=
                  "Error: " + candidates.get(c).substring(4) + " Message: " + e.getMessage();
            }
          }

          // Draw molecule and its fragments
          if (showDiagrams) Render.Draw(molecule, l, "Original Molecule");

          if (pdf) {
            // Create PDF Output
            l.add(0, molecule);
            DisplayStructure ds1 = null;
            // create pdf subfolder
            new File(folder + file + "/" + candidates.get(c) + "pdf/").mkdir();
            ds1 =
                new WritePDFTable(
                    true,
                    300,
                    300,
                    0.9,
                    2,
                    false,
                    false,
                    folder + file + "/" + candidates.get(c) + "pdf/");
            for (int i = 0; i < l.size(); i++) {
              // ds = new displayStructure(false, 300, 300, 0.9, false, "PDF",
              // "/home/basti/WorkspaceJava/TandemMSLookup/fragmenter/Test");
              assert ds1 != null;
              ds1.drawStructure(l.get(i), i);
            }

            if (ds1 != null) ds1.close();
          }

          // now read the saved mol files
          List<IAtomContainer> fragments =
              Molfile.Readfolder(
                  folder
                      + file
                      + "/"
                      + candidates.get(c).substring(4)
                      + "/"
                      + spectrum.getCollisionEnergy());

          List<IAtomContainer> fragmentsList = new ArrayList<IAtomContainer>();
          for (int i = 0; i < fragments.size(); i++) {
            fragmentsList.add(fragments.get(i));
          }

          // get the original peak list again
          // spectrum = new SpectrumWrapper(folder + file + ".txt");
          peakList = (Vector<Peak>) spectrum.getPeakList().clone();
          // clean up peak list
          CleanUpPeakList cList = new CleanUpPeakList(peakList);
          Vector<Peak> cleanedPeakList = cList.getCleanedPeakList(spectrum.getExactMass());

          // now find corresponding fragments to the mass
          AssignFragmentPeak afp = new AssignFragmentPeak();
          afp.setHydrogenTest(hydrogenTest);
          afp.assignFragmentPeak(
              fragments, cleanedPeakList, mzabs, mzppm, spectrum.getMode(), false);
          Vector<PeakMolPair> hits = afp.getHits();
          Vector<PeakMolPair> hitsAll = afp.getAllHits();

          // add them to the list of already found fragments....they were found in the previous run
          // with a smaller collission energy
          for (PeakMolPair peakMolPair : hitsAll) {
            foundHits.add(peakMolPair.getFragment());
          }

          combinedHits += ((combinedHits + hits.size()) - combinedHits);
          // Render.Draw(molecule, foundHits, "Found Hits");

          // now "real" scoring
          Scoring score = new Scoring(cleanedPeakList);
          double currentScore = score.computeScoring(afp.getHitsMZ());
          combinedScore += currentScore;

          // check for last run and create the data for the log file
          if (count == spectra.size()) {
            // save score in hashmap...if there are several hits with the same score --> vector of
            // strings
            if (realScoreMap.containsKey(combinedScore)) {
              Vector<String> tempList = realScoreMap.get(combinedScore);
              tempList.add(candidates.get(c).substring(4));
              realScoreMap.put(combinedScore, tempList);
            } else {
              Vector<String> temp = new Vector<String>();
              temp.add(candidates.get(c).substring(4));
              realScoreMap.put(combinedScore, temp);
            }

            // save score in hashmap...if there are several hits with the same
            // amount of identified peaks --> ArrayList
            if (scoreMap.containsKey(combinedHits)) {
              ArrayList<String> tempList = scoreMap.get(combinedHits);
              tempList.add(candidates.get(c).substring(4));
              scoreMap.put(combinedHits, tempList);
            } else {
              ArrayList<String> temp = new ArrayList<String>();
              temp.add(candidates.get(c).substring(4));
              scoreMap.put(combinedHits, temp);
            }
          }

          // get all the identified peaks
          for (int i = 0; i < hits.size(); i++) {
            peaks += hits.get(i).getPeak().getMass() + " ";
            listOfPeaks.add(hits.get(i).getPeak());
            if (keggIdentifier.equals(candidates.get(c).substring(4)))
              listOfPeaksCorresponding.add(hits.get(i).getPeak());
          }

          List<IAtomContainer> hitsListTest = new ArrayList<IAtomContainer>();
          for (int i = 0; i < hits.size(); i++) {
            List<IAtomContainer> hitsList = new ArrayList<IAtomContainer>();
            hitsList.add(AtomContainerManipulator.removeHydrogens(hits.get(i).getFragment()));
            hitsListTest.add(hits.get(i).getFragment());
            // Render.Highlight(AtomContainerManipulator.removeHydrogens(molecule), hitsList ,
            // Double.toString(hits.get(i).getPeak()));
          }
          if (showDiagrams)
            Render.Draw(molecule, hitsListTest, "Fragmente von: " + candidates.get(c));
        } catch (CDKException e) {
          System.out.println("CDK error!" + e.getMessage());
          completeLog += "CDK Error! " + e.getMessage() + "File: " + candidates.get(c).substring(4);
        } catch (FileNotFoundException e) {
          System.out.println("File not found" + e.getMessage());
          completeLog +=
              "File not found error! " + e.getMessage() + "File: " + candidates.get(c).substring(4);
        } catch (IOException e) {
          System.out.println("IO error: " + e.getMessage());
          completeLog += "IO Error! " + e.getMessage() + "File: " + candidates.get(c).substring(4);
        } catch (Exception e) {
          System.out.println("Error" + e.getMessage());
          completeLog += "Error! " + e.getMessage() + "File: " + candidates.get(c).substring(4);
        } catch (OutOfMemoryError e) {
          System.out.println("Out of memory: " + e.getMessage() + "\n" + e.getStackTrace());
          System.gc();
          completeLog +=
              "Out of memory! " + e.getMessage() + "File: " + candidates.get(c).substring(4);
        }
      }

      // write things to log file
      foundPeaks += combinedHits;
      allPeaks += combinedPeakCount;
      completeLog +=
          "\nFile: "
              + candidates.get(c).substring(4)
              + "\t #Peaks: "
              + combinedPeakCount
              + "\t #Found: "
              + combinedHits;
      completeLog += "\tPeaks: " + peaks;
    }

    // easy scoring
    Integer[] keylist = new Integer[scoreMap.keySet().size()];
    Object[] keys = scoreMap.keySet().toArray();

    for (int i = 0; i < keys.length; i++) {
      keylist[i] = Integer.parseInt(keys[i].toString());
    }

    Arrays.sort(keylist);
    String scoreList = "";
    int rank = 0;
    for (int i = keylist.length - 1; i >= 0; i--) {
      boolean check = false;
      for (int j = 0; j < scoreMap.get(keylist[i]).size(); j++) {
        scoreList += "\n" + keylist[i] + " - " + scoreMap.get(keylist[i]).get(j);
        if (keggIdentifier.equals(scoreMap.get(keylist[i]).get(j))) {
          check = true;
        }
        // worst case: count all which are better or have a equal position
        rank++;
      }
      if (check) {
        histogram += "\n" + file + "\t" + keggIdentifier + "\t" + rank + "\t" + exactMass;
      }
    }

    if (keggIdentifier.equals("none")) {
      histogram += "\n" + file + "\t" + keggIdentifier + "\t\t" + exactMass;
    }

    completeLog += "\n\n*****************Scoring*****************************";
    completeLog += "Supposed to be: " + keggIdentifier;
    completeLog += scoreList;
    completeLog += "\n*****************************************************\n\n";
    // easy scoring end

    // real scoring
    Double[] keysScore = new Double[realScoreMap.keySet().size()];
    keysScore = realScoreMap.keySet().toArray(keysScore);

    Arrays.sort(keysScore);
    String scoreListReal = "";
    rank = 0;
    for (int i = keysScore.length - 1; i >= 0; i--) {
      boolean check = false;
      for (int j = 0; j < realScoreMap.get(keysScore[i]).size(); j++) {
        scoreListReal += "\n" + keysScore[i] + " - " + realScoreMap.get(keysScore[i]).get(j);
        if (keggIdentifier.compareTo(realScoreMap.get(keysScore[i]).get(j)) == 0) {
          check = true;
        }
        // worst case: count all which are better or have a equal position
        rank++;
      }
      if (check) {
        histogramReal += "\n" + file + "\t" + keggIdentifier + "\t" + rank + "\t" + exactMass;
      }
    }

    if (keggIdentifier.equals("none")) {
      histogramReal += "\n" + file + "\t" + keggIdentifier + "\t\t" + exactMass;
    }

    // timing
    long timeEnd = System.currentTimeMillis() - timeStart;
    sumTime += timeEnd;

    completeLog += "\n\n*****************Scoring(Real)*****************************";
    completeLog += "Supposed to be: " + keggIdentifier;
    completeLog += "\nTime: " + timeEnd;
    completeLog += scoreListReal;
    completeLog += "\n*****************************************************\n\n";

    // write the data for peak histogram to log file
    for (int i = 0; i < listOfPeaks.size(); i++) {
      histogramPeaksAll += listOfPeaks.get(i) + "\n";
    }

    // filter the peaks which are contained in the all peaks list. (exclusive)
    for (int i = 0; i < listOfPeaksCorresponding.size(); i++) {
      for (int j = 0; j < listOfPeaks.size(); j++) {
        Double valueA = listOfPeaks.get(j).getMass();
        Double valueB = listOfPeaksCorresponding.get(i).getMass();
        if (valueA.compareTo(valueB) == 0) {
          listOfPeaks.remove(j);
        }
      }
    }

    for (int i = 0; i < listOfPeaks.size(); i++) {
      histogramPeaks += listOfPeaks.get(i) + " ";
    }

    for (int i = 0; i < listOfPeaksCorresponding.size(); i++) {
      histogramPeaksReal += listOfPeaksCorresponding.get(i) + " ";
    }
  }
Exemplo n.º 9
0
  /**
   * calculates the kier shape indices for an atom container
   *
   * @param container AtomContainer
   * @return kier1, kier2 and kier3 are returned as arrayList of doubles
   * @throws CDKException Possible Exceptions
   */
  @Override
  public DescriptorValue calculate(IAtomContainer container) {
    IAtomContainer atomContainer;
    try {
      atomContainer = (IAtomContainer) container.clone();
    } catch (CloneNotSupportedException e) {
      DoubleArrayResult kierValues = new DoubleArrayResult(3);
      kierValues.add(Double.NaN);
      kierValues.add(Double.NaN);
      kierValues.add(Double.NaN);
      return new DescriptorValue(
          getSpecification(),
          getParameterNames(),
          getParameters(),
          kierValues,
          getDescriptorNames());
    }
    atomContainer = AtomContainerManipulator.removeHydrogens(atomContainer);

    // org.openscience.cdk.interfaces.IAtom[] atoms = atomContainer.getAtoms();
    java.util.List firstAtomNeighboors;
    java.util.List secondAtomNeighboors;
    java.util.List thirdAtomNeighboors;
    DoubleArrayResult kierValues = new DoubleArrayResult(3);
    double bond1;
    double bond2;
    double bond3;
    double kier1;
    double kier2;
    double kier3;
    double atomsCount = atomContainer.getAtomCount();
    ArrayList<Double> singlePaths = new ArrayList<Double>();
    ArrayList<String> doublePaths = new ArrayList<String>();
    ArrayList<String> triplePaths = new ArrayList<String>();
    double[] sorterFirst = new double[2];
    double[] sorterSecond = new double[3];
    String tmpbond2;
    String tmpbond3;

    for (int a1 = 0; a1 < atomsCount; a1++) {
      bond1 = 0;
      firstAtomNeighboors = atomContainer.getConnectedAtomsList(atomContainer.getAtom(a1));
      for (int a2 = 0; a2 < firstAtomNeighboors.size(); a2++) {
        bond1 =
            atomContainer.getBondNumber(
                atomContainer.getAtom(a1), (IAtom) firstAtomNeighboors.get(a2));
        if (!singlePaths.contains(new Double(bond1))) {
          singlePaths.add(bond1);
          java.util.Collections.sort(singlePaths);
        }
        secondAtomNeighboors =
            atomContainer.getConnectedAtomsList((IAtom) firstAtomNeighboors.get(a2));
        for (int a3 = 0; a3 < secondAtomNeighboors.size(); a3++) {
          bond2 =
              atomContainer.getBondNumber(
                  (IAtom) firstAtomNeighboors.get(a2), (IAtom) secondAtomNeighboors.get(a3));
          if (!singlePaths.contains(new Double(bond2))) {
            singlePaths.add(bond2);
          }
          sorterFirst[0] = bond1;
          sorterFirst[1] = bond2;
          java.util.Arrays.sort(sorterFirst);

          tmpbond2 = sorterFirst[0] + "+" + sorterFirst[1];

          if (!doublePaths.contains(tmpbond2) && (bond1 != bond2)) {
            doublePaths.add(tmpbond2);
          }
          thirdAtomNeighboors =
              atomContainer.getConnectedAtomsList((IAtom) secondAtomNeighboors.get(a3));
          for (int a4 = 0; a4 < thirdAtomNeighboors.size(); a4++) {
            bond3 =
                atomContainer.getBondNumber(
                    (IAtom) secondAtomNeighboors.get(a3), (IAtom) thirdAtomNeighboors.get(a4));
            if (!singlePaths.contains(new Double(bond3))) {
              singlePaths.add(bond3);
            }
            sorterSecond[0] = bond1;
            sorterSecond[1] = bond2;
            sorterSecond[2] = bond3;
            java.util.Arrays.sort(sorterSecond);

            tmpbond3 = sorterSecond[0] + "+" + sorterSecond[1] + "+" + sorterSecond[2];
            if (!triplePaths.contains(tmpbond3)) {
              if ((bond1 != bond2) && (bond1 != bond3) && (bond2 != bond3)) {
                triplePaths.add(tmpbond3);
              }
            }
          }
        }
      }
    }

    if (atomsCount == 1) {
      kier1 = 0;
      kier2 = 0;
      kier3 = 0;
    } else {
      kier1 =
          (((atomsCount) * ((atomsCount - 1) * (atomsCount - 1)))
              / (singlePaths.size() * singlePaths.size()));
      if (atomsCount == 2) {
        kier2 = 0;
        kier3 = 0;
      } else {
        if (doublePaths.size() == 0) kier2 = Double.NaN;
        else
          kier2 =
              (((atomsCount - 1) * ((atomsCount - 2) * (atomsCount - 2)))
                  / (doublePaths.size() * doublePaths.size()));
        if (atomsCount == 3) {
          kier3 = 0;
        } else {
          if (atomsCount % 2 != 0) {
            if (triplePaths.size() == 0) kier3 = Double.NaN;
            else
              kier3 =
                  (((atomsCount - 1) * ((atomsCount - 3) * (atomsCount - 3)))
                      / (triplePaths.size() * triplePaths.size()));
          } else {
            if (triplePaths.size() == 0) kier3 = Double.NaN;
            else
              kier3 =
                  (((atomsCount - 3) * ((atomsCount - 2) * (atomsCount - 2)))
                      / (triplePaths.size() * triplePaths.size()));
          }
        }
      }
    }

    kierValues.add(kier1);
    kierValues.add(kier2);
    kierValues.add(kier3);
    return new DescriptorValue(
        getSpecification(), getParameterNames(), getParameters(), kierValues, getDescriptorNames());
  }
Exemplo n.º 10
0
 /** A unit test for JUnit */
 @Test
 public void testCisResorcinol() throws Exception {
   HydrogenPlacer hydrogenPlacer = new HydrogenPlacer();
   IAtomContainer mol1 = new AtomContainer();
   SmilesGenerator sg = new SmilesGenerator();
   mol1.addAtom(new Atom("O", new Point2d(3, 1)));
   // 1
   mol1.addAtom(new Atom("H", new Point2d(2, 0)));
   // 2
   mol1.addAtom(new Atom("C", new Point2d(2, 1)));
   // 3
   mol1.addAtom(new Atom("C", new Point2d(1, 1)));
   // 4
   mol1.addAtom(new Atom("C", new Point2d(1, 4)));
   // 5
   mol1.addAtom(new Atom("C", new Point2d(1, 5)));
   // 6
   mol1.addAtom(new Atom("C", new Point2d(1, 2)));
   // 7
   mol1.addAtom(new Atom("C", new Point2d(2, 2)));
   // 1
   mol1.addAtom(new Atom("O", new Point2d(3, 2)));
   // 2
   mol1.addAtom(new Atom("H", new Point2d(2, 3)));
   // 3
   mol1.addBond(0, 2, IBond.Order.SINGLE, IBond.Stereo.DOWN);
   // 1
   mol1.addBond(1, 2, IBond.Order.SINGLE, IBond.Stereo.UP);
   // 2
   mol1.addBond(2, 3, IBond.Order.SINGLE);
   // 3
   mol1.addBond(3, 4, IBond.Order.SINGLE);
   // 4
   mol1.addBond(4, 5, IBond.Order.SINGLE);
   // 5
   mol1.addBond(5, 6, IBond.Order.SINGLE);
   // 6
   mol1.addBond(6, 7, IBond.Order.SINGLE);
   // 3
   mol1.addBond(7, 8, IBond.Order.SINGLE, IBond.Stereo.UP);
   // 4
   mol1.addBond(7, 9, IBond.Order.SINGLE, IBond.Stereo.DOWN);
   // 5
   mol1.addBond(7, 2, IBond.Order.SINGLE);
   // 6
   try {
     addExplicitHydrogens(mol1);
     hydrogenPlacer.placeHydrogens2D(mol1, 1.0);
     IsotopeFactory ifac = IsotopeFactory.getInstance(mol1.getBuilder());
     ifac.configureAtoms(mol1);
   } catch (IOException ex) {
   } catch (ClassNotFoundException ex) {
   }
   String smiles1 = null;
   if (standAlone) {
     display(mol1);
   }
   try {
     smiles1 = sg.createSMILES(mol1, true, new boolean[mol1.getBondCount()]);
   } catch (Exception exc) {
     System.out.println(exc);
     if (!standAlone) {
       Assert.fail();
     }
   }
   if (standAlone) {
     System.err.println("SMILES 1: " + smiles1);
   }
   Assert.assertNotNull(smiles1);
   Assert.assertEquals(
       "[H]O[C@]1(C([H])([H])C([H])([H])C([H])([H])C([H])([H])[C@]1(O[H])([H]))([H])", smiles1);
   mol1 = AtomContainerManipulator.removeHydrogens(mol1);
   try {
     smiles1 = sg.createSMILES(mol1);
   } catch (Exception exc) {
     System.out.println(exc);
     if (!standAlone) {
       Assert.fail();
     }
   }
   if (standAlone) {
     System.err.println("SMILES 1: " + smiles1);
   }
   Assert.assertNotNull(smiles1);
   Assert.assertEquals("OC1CCCCC1(O)", smiles1);
 }