Beispiel #1
0
  /**
   * Internal - makes a map of the highlights for reaction mapping.
   *
   * @param reactants reaction reactants
   * @param products reaction products
   * @return the highlight map
   */
  private Map<IChemObject, Color> makeHighlightAtomMap(
      List<IAtomContainer> reactants, List<IAtomContainer> products) {
    Map<IChemObject, Color> colorMap = new HashMap<>();
    Map<Integer, Color> mapToColor = new HashMap<>();
    int colorIdx = -1;
    for (IAtomContainer mol : reactants) {
      int prevPalletIdx = colorIdx;
      for (IAtom atom : mol.atoms()) {
        int mapidx = accessAtomMap(atom);
        if (mapidx > 0) {
          if (prevPalletIdx == colorIdx) {
            colorIdx++; // select next color
            if (colorIdx >= atomMapColors.length)
              throw new IllegalArgumentException(
                  "Not enough colors to highlight atom mapping, please provide mode");
          }
          Color color = atomMapColors[colorIdx];
          colorMap.put(atom, color);
          mapToColor.put(mapidx, color);
        }
      }
      if (colorIdx > prevPalletIdx) {
        for (IBond bond : mol.bonds()) {
          IAtom a1 = bond.getAtom(0);
          IAtom a2 = bond.getAtom(1);
          Color c1 = colorMap.get(a1);
          Color c2 = colorMap.get(a2);
          if (c1 != null && c1 == c2) colorMap.put(bond, c1);
        }
      }
    }

    for (IAtomContainer mol : products) {
      for (IAtom atom : mol.atoms()) {
        int mapidx = accessAtomMap(atom);
        if (mapidx > 0) {
          colorMap.put(atom, mapToColor.get(mapidx));
        }
      }
      for (IBond bond : mol.bonds()) {
        IAtom a1 = bond.getAtom(0);
        IAtom a2 = bond.getAtom(1);
        Color c1 = colorMap.get(a1);
        Color c2 = colorMap.get(a2);
        if (c1 != null && c1 == c2) colorMap.put(bond, c1);
      }
    }

    return colorMap;
  }
Beispiel #2
0
  private IRenderingElement generate(IAtomContainer molecule, RendererModel model, int atomNum)
      throws CDKException {

    // tag the atom and bond ids
    String molId = molecule.getProperty(MarkedElement.ID_KEY);
    if (molId != null) {
      int atomId = 0, bondid = 0;
      for (IAtom atom : molecule.atoms())
        setIfMissing(atom, MarkedElement.ID_KEY, molId + "atm" + ++atomId);
      for (IBond bond : molecule.bonds())
        setIfMissing(bond, MarkedElement.ID_KEY, molId + "bnd" + ++bondid);
    }

    if (annotateAtomNum) {
      for (IAtom atom : molecule.atoms()) {
        if (atom.getProperty(StandardGenerator.ANNOTATION_LABEL) != null)
          throw new UnsupportedOperationException("Multiple annotation labels are not supported.");
        atom.setProperty(StandardGenerator.ANNOTATION_LABEL, Integer.toString(atomNum++));
      }
    } else if (annotateAtomVal) {
      for (IAtom atom : molecule.atoms()) {
        if (atom.getProperty(StandardGenerator.ANNOTATION_LABEL) != null)
          throw new UnsupportedOperationException("Multiple annotation labels are not supported.");
        atom.setProperty(
            StandardGenerator.ANNOTATION_LABEL, atom.getProperty(CDKConstants.COMMENT));
      }
    } else if (annotateAtomMap) {
      for (IAtom atom : molecule.atoms()) {
        if (atom.getProperty(StandardGenerator.ANNOTATION_LABEL) != null)
          throw new UnsupportedOperationException("Multiple annotation labels are not supported.");
        int mapidx = accessAtomMap(atom);
        if (mapidx > 0) {
          atom.setProperty(StandardGenerator.ANNOTATION_LABEL, Integer.toString(mapidx));
        }
      }
    }

    ElementGroup grp = new ElementGroup();
    for (IGenerator<IAtomContainer> gen : gens) grp.add(gen.generate(molecule, model));

    // cleanup
    if (annotateAtomNum || annotateAtomMap) {
      for (IAtom atom : molecule.atoms()) {
        atom.removeProperty(StandardGenerator.ANNOTATION_LABEL);
      }
    }

    return grp;
  }
Beispiel #3
0
  public static IAtomContainer getMcsAsNewContainer(IAtomContainer mol1, IAtomContainer mol2)
      throws CDKException, CloneNotSupportedException {
    Isomorphism mcs = new Isomorphism(org.openscience.cdk.smsd.interfaces.Algorithm.DEFAULT, true);
    mcs.init(mol1, mol2, true, true);
    mcs.setChemFilters(true, true, true);

    mol1 = mcs.getReactantMolecule();
    mol2 = mcs.getProductMolecule();

    IAtomContainer mcsmolecule =
        DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainer.class, mol1);

    List<IAtom> atomsToBeRemoved = new ArrayList<IAtom>();
    for (IAtom atom : mcsmolecule.atoms()) {
      int index = mcsmolecule.getAtomNumber(atom);
      if (!mcs.getFirstMapping().containsKey(index)) {
        atomsToBeRemoved.add(atom);
      }
    }

    for (IAtom atom : atomsToBeRemoved) {
      mcsmolecule.removeAtomAndConnectedElectronContainers(atom);
    }

    return mcsmolecule;
  }
  /**
   * Modules for cleaning a molecule
   *
   * @param molecule
   * @return cleaned AtomContainer
   */
  @TestMethod("testCheckAndCleanMolecule")
  public static IAtomContainer checkAndCleanMolecule(IAtomContainer molecule) {
    boolean isMarkush = false;
    for (IAtom atom : molecule.atoms()) {
      if (atom.getSymbol().equals("R")) {
        isMarkush = true;
        break;
      }
    }

    if (isMarkush) {
      System.err.println("Skipping Markush structure for sanity check");
    }

    // Check for salts and such
    if (!ConnectivityChecker.isConnected(molecule)) {
      // lets see if we have just two parts if so, we assume its a salt and just work
      // on the larger part. Ideally we should have a check to ensure that the smaller
      //  part is a metal/halogen etc.
      IMoleculeSet fragments = ConnectivityChecker.partitionIntoMolecules(molecule);
      if (fragments.getMoleculeCount() > 2) {
        System.err.println("More than 2 components. Skipped");
      } else {
        IMolecule frag1 = fragments.getMolecule(0);
        IMolecule frag2 = fragments.getMolecule(1);
        if (frag1.getAtomCount() > frag2.getAtomCount()) {
          molecule = frag1;
        } else {
          molecule = frag2;
        }
      }
    }
    configure(molecule);
    return molecule;
  }
 public String perceiveCDKAtomTypes(IMolecule mol)
             throws InvocationTargetException {
     
     ICDKMolecule cdkmol;
     
     try {
         cdkmol = cdk.asCDKMolecule(mol);
     } 
     catch ( BioclipseException e ) {
         e.printStackTrace();
         throw new InvocationTargetException(
                       e, "Error while creating a ICDKMolecule" );
     }
     
     IAtomContainer ac = cdkmol.getAtomContainer();
     CDKAtomTypeMatcher cdkMatcher 
         = CDKAtomTypeMatcher.getInstance(ac.getBuilder());
     
     StringBuffer result = new StringBuffer();
     int i = 1;
     for (IAtom atom : ac.atoms()) {
         IAtomType type = null;
         try {
             type = cdkMatcher.findMatchingAtomType(ac, atom);
         } 
         catch ( CDKException e ) {}
         result.append(i).append(':').append(
             type != null ? type.getAtomTypeName() : "null"
         ).append('\n'); // FIXME: should use NEWLINE here
         i++;
     }
     return result.toString();
 }
Beispiel #6
0
  /** Checks if the ringSet (created by SSSR) contains rings with exactly the same atoms. */
  static boolean checkForDuplicateRingsInSet(IRingSet ringset) {
    // Make a list of rings
    List<IAtomContainer> ringList = new ArrayList<IAtomContainer>();
    for (IAtomContainer atCont : ringset.atomContainers()) {
      ringList.add(atCont);
    }
    // Outer loop over rings
    for (IAtomContainer ring : ringList) {
      // Inner loop over rings
      for (IAtomContainer otherRing : ringList) {
        if (otherRing.hashCode() != ring.hashCode()
            && otherRing.getAtomCount() == ring.getAtomCount()) {

          // check if the two rings have all the same atoms in them -
          // this should not happen (="duplicate" rings)
          boolean sameAtoms = true;
          DUP_LOOP:
          for (IAtom at : ring.atoms()) {
            if (!otherRing.contains(at)) {
              sameAtoms = false;
              break DUP_LOOP;
            }
          }
          if (sameAtoms) {
            return true;
          }
        }
      }
    }
    return false;
  }
Beispiel #7
0
  /**
   * Performs the pharmacophore matching.
   *
   * @param atomContainer The target molecule. Must have 3D coordinates
   * @param initializeTarget If <i>true</i>, the target molecule specified in the first argument
   *     will be analyzed to identify matching pharmacophore groups. If <i>false</i> this is not
   *     performed. The latter case is only useful when dealing with conformers since for a given
   *     molecule, all conformers will have the same pharmacophore groups and only the constraints
   *     will change from one conformer to another.
   * @return true is the target molecule contains the query pharmacophore
   * @throws org.openscience.cdk.exception.CDKException if the query pharmacophore was not set or
   *     the query is invalid or if the molecule does not have 3D coordinates
   */
  public boolean matches(IAtomContainer atomContainer, boolean initializeTarget)
      throws CDKException {
    if (!GeometryUtil.has3DCoordinates(atomContainer))
      throw new CDKException("Molecule must have 3D coordinates");
    if (pharmacophoreQuery == null)
      throw new CDKException("Must set the query pharmacophore before matching");
    if (!checkQuery(pharmacophoreQuery))
      throw new CDKException(
          "A problem in the query. Make sure all pharmacophore groups of the same symbol have the same same SMARTS");
    String title = (String) atomContainer.getProperty(CDKConstants.TITLE);

    if (initializeTarget) pharmacophoreMolecule = getPharmacophoreMolecule(atomContainer);
    else {
      // even though the atoms comprising the pcore groups are
      // constant, their coords will differ, so we need to make
      // sure we get the latest set of effective coordinates
      for (IAtom iAtom : pharmacophoreMolecule.atoms()) {
        PharmacophoreAtom patom = (PharmacophoreAtom) iAtom;
        List<Integer> tmpList = new ArrayList<Integer>();
        for (int idx : patom.getMatchingAtoms()) tmpList.add(idx);
        Point3d coords = getEffectiveCoordinates(atomContainer, tmpList);
        patom.setPoint3d(coords);
      }
    }

    if (pharmacophoreMolecule.getAtomCount() < pharmacophoreQuery.getAtomCount()) {
      logger.debug("Target [" + title + "] did not match the query SMARTS. Skipping constraints");
      return false;
    }

    mappings = Pattern.findSubstructure(pharmacophoreQuery).matchAll(pharmacophoreMolecule);

    // XXX: doing one search then discarding
    return mappings.atLeast(1);
  }
Beispiel #8
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());
  }
Beispiel #9
0
 private boolean checkForNullAtoms(IAtomContainer mol) {
   for (IAtom a : mol.atoms()) {
     if (a == null) {
       return true;
     }
   }
   return false;
 }
Beispiel #10
0
 private boolean isChemicallyValid(IAtomContainer union) throws CDKException {
   for (IAtom atom : union.atoms()) {
     if ((union.getConnectedBondsCount(atom) + atom.getFormalCharge())
         > atom.getFormalNeighbourCount()) {
       return false;
     }
   }
   return true;
 }
  /**
   * Test to recognize if a IAtomContainer matcher correctly identifies the CDKAtomTypes.
   *
   * @param molecule The IAtomContainer to analyze
   * @throws CDKException
   */
  private void makeSureAtomTypesAreRecognized(IAtomContainer molecule) throws CDKException {

    Iterator<IAtom> atoms = molecule.atoms().iterator();
    CDKAtomTypeMatcher matcher = CDKAtomTypeMatcher.getInstance(molecule.getBuilder());
    while (atoms.hasNext()) {
      IAtom nextAtom = atoms.next();
      Assert.assertNotNull(
          "Missing atom type for: " + nextAtom, matcher.findMatchingAtomType(molecule, nextAtom));
    }
  }
Beispiel #12
0
 private boolean checkForGenericAtoms(IAtomContainer mol) {
   for (IAtom atom : mol.atoms()) {
     if (atom instanceof PseudoAtom) return true;
     String className = atom.getClass().getName();
     if (className.equalsIgnoreCase("org.openscience.cdk.PseudoAtom")) {
       return true;
     }
   }
   return false;
 }
Beispiel #13
0
 /**
  * Place all hydrogens connected to atoms which have already been laid out.
  *
  * @param container atom container
  * @param bondLength bond length to user
  */
 @TestMethod("testBug933572,testH2")
 public void placeHydrogens2D(final IAtomContainer container, final double bondLength) {
   logger.debug("placing hydrogens on all atoms");
   for (IAtom atom : container.atoms()) {
     // only place hydrogens for atoms which have coordinates
     if (atom.getPoint2d() != null) {
       placeHydrogens2D(container, atom, bondLength);
     }
   }
   logger.debug("hydrogen placement complete");
 }
Beispiel #14
0
 /**
  * Reset the coordinates to their position before rendering.
  *
  * @param mols molecules
  * @param scales how molecules were scaled
  */
 private static void resetCoords(Iterable<IAtomContainer> mols, List<Double> scales) {
   Iterator<Double> it = scales.iterator();
   for (IAtomContainer mol : mols) {
     final double factor = it.next();
     if (!Double.isNaN(factor)) {
       GeometryUtil.scaleMolecule(mol, 1 / factor);
     } else {
       for (IAtom atom : mol.atoms()) atom.setPoint2d(null);
     }
   }
 }
 /**
  * @param atomContainer
  * @param getNoneAssesments
  * @return
  */
 @Override
 public Map<IAtom, IStereoAndConformation> getTetrahedralChiralities(
     IAtomContainer atomContainer, boolean getNoneAssesments) {
   Map<IAtom, IStereoAndConformation> chiralities = new HashMap<>();
   WedgeStereoLifter lifter = new WedgeStereoLifter();
   for (IAtom atom : atomContainer.atoms()) {
     IStereoAndConformation chirality = getChirality2D(lifter, atom, atomContainer);
     if (getNoneAssesments || chirality != NONE) {
       chiralities.put(atom, chirality);
     }
   }
   return chiralities;
 }
Beispiel #16
0
 /**
  * Returns common mapped fragment in the target molecule.
  *
  * @return common mapped fragment in the target molecule
  * @throws CloneNotSupportedException
  */
 public synchronized IAtomContainer getCommonFragmentInTarget() throws CloneNotSupportedException {
   IAtomContainer ac = (IAtomContainer) target.clone();
   List<IAtom> uniqueAtoms = Collections.synchronizedList(new ArrayList<IAtom>());
   for (IAtom atom : target.atoms()) {
     if (!mapping.containsValue(atom)) {
       uniqueAtoms.add(ac.getAtom(getTargetIndex(atom)));
     }
   }
   for (IAtom atom : uniqueAtoms) {
     ac.removeAtomAndConnectedElectronContainers(atom);
   }
   return ac;
 }
	/**
	 * Clean up chemical model ,removing duplicates empty molecules etc
	 * 
	 * @param chemModel
	 * @param avoidOverlap
	 * @throws CDKException
	 */
	public static void cleanUpChemModel(final IChemModel chemModel,
			final boolean avoidOverlap, final AbstractJChemPaintPanel panel)
			throws CDKException {
		JChemPaint.setReactionIDs(chemModel);
		JChemPaint.replaceReferencesWithClones(chemModel);

		// check the model is not completely empty
		if (ChemModelManipulator.getBondCount(chemModel) == 0
				&& ChemModelManipulator.getAtomCount(chemModel) == 0) {
			throw new CDKException(
					"Structure does not have bonds or atoms. Cannot depict structure.");
		}
		JChemPaint.removeDuplicateAtomContainers(chemModel);
		JChemPaint.checkCoordinates(chemModel);
		JChemPaint.removeEmptyAtomContainers(chemModel);

		if (avoidOverlap) {
			try {
				ControllerHub.avoidOverlap(chemModel);
			} catch (final Exception e) {
				JOptionPane.showMessageDialog(panel,
						GT._("Structure could not be generated"));
				throw new CDKException("Cannot depict structure");
			}
		}

		// We update implicit Hs in any case
		final CDKAtomTypeMatcher matcher = CDKAtomTypeMatcher
				.getInstance(chemModel.getBuilder());
		for (final IAtomContainer container : ChemModelManipulator
				.getAllAtomContainers(chemModel)) {
			for (final IAtom atom : container.atoms()) {
				if (!(atom instanceof IPseudoAtom)) {
					try {
						final IAtomType type = matcher.findMatchingAtomType(
								container, atom);
						if (type != null
								&& type.getFormalNeighbourCount() != null) {
							final int connectedAtomCount = container
									.getConnectedAtomsCount(atom);
							atom.setImplicitHydrogenCount(type
									.getFormalNeighbourCount()
									- connectedAtomCount);
						}
					} catch (final CDKException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
Beispiel #18
0
  @Test
  public void testRingFlags1() throws Exception {
    SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
    IAtomContainer molecule = sp.parseSmiles("c1ccccc1");
    new SSSRFinder(molecule).findSSSR();

    int count = 0;
    Iterator atoms = molecule.atoms().iterator();
    while (atoms.hasNext()) {
      IAtom atom = (IAtom) atoms.next();
      if (atom.getFlag(CDKConstants.ISINRING)) count++;
    }
    Assert.assertEquals("All atoms in benzene were not marked as being in a ring", 6, count);
  }
  /**
   * Method that actually does the work of convert the atomContainer to IMolecularFormula given a
   * IMolecularFormula.
   *
   * <p>The hydrogens must be implicit.
   *
   * @param atomContainer IAtomContainer object
   * @param formula IMolecularFormula molecularFormula to put the new Isotopes
   * @return the filled AtomContainer
   * @see #getMolecularFormula(IAtomContainer)
   */
  public static IMolecularFormula getMolecularFormula(
      IAtomContainer atomContainer, IMolecularFormula formula) {
    int charge = 0;
    IAtom hAtom = null;
    for (IAtom iAtom : atomContainer.atoms()) {
      formula.addIsotope(iAtom);
      if (iAtom.getFormalCharge() != null) charge += iAtom.getFormalCharge();

      if (iAtom.getImplicitHydrogenCount() != null && (iAtom.getImplicitHydrogenCount() > 0)) {
        if (hAtom == null) hAtom = atomContainer.getBuilder().newInstance(IAtom.class, "H");
        formula.addIsotope(hAtom, iAtom.getImplicitHydrogenCount());
      }
    }
    formula.setCharge(charge);
    return formula;
  }
  /**
   * The method returns partial charges assigned to an heavy atom through MMFF94 method. It is
   * needed to call the addExplicitHydrogensToSatisfyValency method from the class
   * tools.HydrogenAdder.
   *
   * @param atom The IAtom for which the DescriptorValue is requested
   * @param org AtomContainer
   * @return partial charge of parameter atom
   */
  @Override
  public DescriptorValue calculate(IAtom atom, IAtomContainer org) {

    if (atom.getProperty(CHARGE_CACHE) == null) {

      IAtomContainer copy;
      try {
        copy = org.clone();
      } catch (CloneNotSupportedException e) {
        return new DescriptorValue(
            getSpecification(),
            getParameterNames(),
            getParameters(),
            new DoubleResult(Double.NaN),
            NAMES);
      }

      for (IAtom a : org.atoms()) {
        if (a.getImplicitHydrogenCount() == null || a.getImplicitHydrogenCount() != 0) {
          logger.error("Hydrogens must be explict for MMFF charge calculation");
          return new DescriptorValue(
              getSpecification(),
              getParameterNames(),
              getParameters(),
              new DoubleResult(Double.NaN),
              NAMES);
        }
      }

      if (!mmff.assignAtomTypes(copy))
        logger.warn("One or more atoms could not be assigned an MMFF atom type");
      mmff.partialCharges(copy);
      mmff.clearProps(copy);

      // cache charges
      for (int i = 0; i < org.getAtomCount(); i++) {
        org.getAtom(i).setProperty(CHARGE_CACHE, copy.getAtom(i).getCharge());
      }
    }

    return new DescriptorValue(
        getSpecification(),
        getParameterNames(),
        getParameters(),
        new DoubleResult(atom.getProperty(CHARGE_CACHE, Double.class)),
        NAMES);
  }
 /** Tests if the electron count matches the H&uuml;ckel 4n+2 rule. */
 private static boolean isHueckelValid(IAtomContainer singleRing) throws CDKException {
   int electronCount = 0;
   for (IAtom ringAtom : singleRing.atoms()) {
     if (ringAtom.getHybridization() != CDKConstants.UNSET
             && (ringAtom.getHybridization() == Hybridization.SP2)
         || ringAtom.getHybridization() == Hybridization.PLANAR3) {
       // for example, a carbon
       // note: the double bond is in the ring, that has been tested earlier
       // FIXME: this does assume bond orders to be resolved too, when detecting
       // sprouting double bonds
       if ("N.planar3".equals(ringAtom.getAtomTypeName())) {
         electronCount += 2;
       } else if ("N.minus.planar3".equals(ringAtom.getAtomTypeName())) {
         electronCount += 2;
       } else if ("N.amide".equals(ringAtom.getAtomTypeName())) {
         electronCount += 2;
       } else if ("S.2".equals(ringAtom.getAtomTypeName())) {
         electronCount += 2;
       } else if ("S.planar3".equals(ringAtom.getAtomTypeName())) {
         electronCount += 2;
       } else if ("C.minus.planar".equals(ringAtom.getAtomTypeName())) {
         electronCount += 2;
       } else if ("O.planar3".equals(ringAtom.getAtomTypeName())) {
         electronCount += 2;
       } else if ("N.sp2.3".equals(ringAtom.getAtomTypeName())) {
         electronCount += 1;
       } else {
         if (factory == null) {
           factory =
               AtomTypeFactory.getInstance(
                   "org/openscience/cdk/dict/data/cdk-atom-types.owl", ringAtom.getBuilder());
         }
         IAtomType type = factory.getAtomType(ringAtom.getAtomTypeName());
         Object property = type.getProperty(CDKConstants.PI_BOND_COUNT);
         if (property != null && property instanceof Integer) {
           electronCount += ((Integer) property).intValue();
         }
       }
     } else if (ringAtom.getHybridization() != null
         && ringAtom.getHybridization() == Hybridization.SP3
         && getLonePairCount(ringAtom) > 0) {
       // for example, a nitrogen or oxygen
       electronCount += 2;
     }
   }
   return (electronCount % 4 == 2) && (electronCount > 2);
 }
Beispiel #22
0
 private synchronized IQuery build(IAtomContainer queryMolecule) {
   VFQueryBuilder result = new VFQueryBuilder();
   for (IAtom atom : queryMolecule.atoms()) {
     AtomMatcher matcher = createAtomMatcher(queryMolecule, atom);
     if (matcher != null) {
       result.addNode(matcher, atom);
     }
   }
   for (int i = 0; i < queryMolecule.getBondCount(); i++) {
     IBond bond = queryMolecule.getBond(i);
     IAtom atomI = bond.getAtom(0);
     IAtom atomJ = bond.getAtom(1);
     result.connect(
         result.getNode(atomI), result.getNode(atomJ), createBondMatcher(queryMolecule, bond));
   }
   return result;
 }
  /**
   * set the active center for this molecule. The active center will be those which correspond with
   * [A*]-B=C .
   *
   * <pre>
   * A: Atom with single electron
   * -: Single bond
   * B: Atom
   * =: Double bond
   * C: Atom
   *  </pre>
   *
   * @param reactant The molecule to set the activity
   * @throws CDKException
   */
  private void setActiveCenters(IAtomContainer reactant) throws CDKException {
    if (AtomContainerManipulator.getTotalNegativeFormalCharge(reactant)
        != 0 /*|| AtomContainerManipulator.getTotalPositiveFormalCharge(reactant) != 0*/) return;
    Iterator<IAtom> atoms = reactant.atoms().iterator();
    while (atoms.hasNext()) {
      IAtom atomi = atoms.next();
      if (reactant.getConnectedSingleElectronsCount(atomi) == 1) {

        Iterator<IBond> bondis = reactant.getConnectedBondsList(atomi).iterator();

        while (bondis.hasNext()) {
          IBond bondi = bondis.next();

          if (bondi.getOrder() == IBond.Order.SINGLE) {

            IAtom atomj = bondi.getConnectedAtom(atomi);
            if ((atomj.getFormalCharge() == CDKConstants.UNSET ? 0 : atomj.getFormalCharge()) == 0
                && reactant.getConnectedSingleElectronsCount(atomj) == 0) {

              Iterator<IBond> bondjs = reactant.getConnectedBondsList(atomj).iterator();
              while (bondjs.hasNext()) {
                IBond bondj = bondjs.next();

                if (bondj.equals(bondi)) continue;

                if (bondj.getOrder() == IBond.Order.DOUBLE) {

                  IAtom atomk = bondj.getConnectedAtom(atomj);
                  if ((atomk.getFormalCharge() == CDKConstants.UNSET ? 0 : atomk.getFormalCharge())
                          == 0
                      && reactant.getConnectedSingleElectronsCount(atomk) == 0) {

                    atomi.setFlag(CDKConstants.REACTIVE_CENTER, true);
                    atomj.setFlag(CDKConstants.REACTIVE_CENTER, true);
                    atomk.setFlag(CDKConstants.REACTIVE_CENTER, true);
                    bondi.setFlag(CDKConstants.REACTIVE_CENTER, true);
                    bondj.setFlag(CDKConstants.REACTIVE_CENTER, true);
                  }
                }
              }
            }
          }
        }
      }
    }
  }
 /**
  * Determines if the isolatedRingSystem has attached double bonds, which are not part of the ring
  * system itself, and not part of any other ring system. Exceptions: a N.sp2.3 nitrogen with a
  * double ring to an oxygen outwards.
  */
 private static boolean isRingSystemSproutedWithNonRingDoubleBonds(
     IAtomContainer fullContainer, IAtomContainer isolatedRingSystem) {
   Iterator<IAtom> atoms = isolatedRingSystem.atoms().iterator();
   while (atoms.hasNext()) {
     IAtom atom = atoms.next();
     Iterator<IBond> neighborBonds = fullContainer.getConnectedBondsList(atom).iterator();
     while (neighborBonds.hasNext()) {
       IBond neighborBond = neighborBonds.next();
       if (!neighborBond.getFlag(CDKConstants.ISINRING)
               && neighborBond.getOrder() == CDKConstants.BONDORDER_DOUBLE
           || neighborBond.getOrder() == CDKConstants.BONDORDER_TRIPLE) {
         if (!("N.sp2.3".equals(atom.getAtomTypeName())
             && "O.sp2".equals(neighborBond.getConnectedAtom(atom).getAtomTypeName())))
           return true;
       }
     }
   }
   return false;
 }
 /** @throws BioclipseException */
 private void calculateInchi() throws BioclipseException {
   try {
     IAtomContainer clone = (IAtomContainer) getAtomContainer().clone();
     // remove aromaticity flags
     for (IAtom atom : clone.atoms()) atom.setFlag(CDKConstants.ISAROMATIC, false);
     for (IBond bond : clone.bonds()) bond.setFlag(CDKConstants.ISAROMATIC, false);
     if (factory == null) {
       factory = InChIGeneratorFactory.getInstance();
     }
     InChIGenerator gen = factory.getInChIGenerator(clone);
     INCHI_RET status = gen.getReturnStatus();
     if (status == INCHI_RET.OKAY || status == INCHI_RET.WARNING) {
       InChI inchi = new InChI();
       inchi.setValue(gen.getInchi());
       inchi.setKey(gen.getInchiKey());
       cachedInchi = inchi;
     } else {
       throw new InvalidParameterException(
           "Error while generating InChI (" + status + "): " + gen.getMessage());
     }
   } catch (Exception e) {
     throw new BioclipseException("Could not create InChI: " + e.getMessage(), e);
   }
 }
Beispiel #26
0
  /**
   * Prepare the target molecule for analysis.
   *
   * <p>We perform ring perception and aromaticity detection and set up the appropriate properties.
   * Right now, this function is called each time we need to do a query and this is inefficient.
   *
   * @throws CDKException if there is a problem in ring perception or aromaticity detection, which
   *     is usually related to a timeout in the ring finding code.
   */
  private void initializeMolecule() throws CDKException {
    // Code copied from
    // org.openscience.cdk.qsar.descriptors.atomic.AtomValenceDescriptor;
    Map<String, Integer> valencesTable = new HashMap<String, Integer>();
    valencesTable.put("H", 1);
    valencesTable.put("Li", 1);
    valencesTable.put("Be", 2);
    valencesTable.put("B", 3);
    valencesTable.put("C", 4);
    valencesTable.put("N", 5);
    valencesTable.put("O", 6);
    valencesTable.put("F", 7);
    valencesTable.put("Na", 1);
    valencesTable.put("Mg", 2);
    valencesTable.put("Al", 3);
    valencesTable.put("Si", 4);
    valencesTable.put("P", 5);
    valencesTable.put("S", 6);
    valencesTable.put("Cl", 7);
    valencesTable.put("K", 1);
    valencesTable.put("Ca", 2);
    valencesTable.put("Ga", 3);
    valencesTable.put("Ge", 4);
    valencesTable.put("As", 5);
    valencesTable.put("Se", 6);
    valencesTable.put("Br", 7);
    valencesTable.put("Rb", 1);
    valencesTable.put("Sr", 2);
    valencesTable.put("In", 3);
    valencesTable.put("Sn", 4);
    valencesTable.put("Sb", 5);
    valencesTable.put("Te", 6);
    valencesTable.put("I", 7);
    valencesTable.put("Cs", 1);
    valencesTable.put("Ba", 2);
    valencesTable.put("Tl", 3);
    valencesTable.put("Pb", 4);
    valencesTable.put("Bi", 5);
    valencesTable.put("Po", 6);
    valencesTable.put("At", 7);
    valencesTable.put("Fr", 1);
    valencesTable.put("Ra", 2);
    valencesTable.put("Cu", 2);
    valencesTable.put("Mn", 2);
    valencesTable.put("Co", 2);

    // do all ring perception
    AllRingsFinder arf = new AllRingsFinder();
    IRingSet allRings;
    try {
      allRings = arf.findAllRings(atomContainer);
    } catch (CDKException e) {
      logger.debug(e.toString());
      throw new CDKException(e.toString(), e);
    }

    // sets SSSR information
    SSSRFinder finder = new SSSRFinder(atomContainer);
    IRingSet sssr = finder.findEssentialRings();

    for (IAtom atom : atomContainer.atoms()) {

      // add a property to each ring atom that will be an array of
      // Integers, indicating what size ring the given atom belongs to
      // Add SSSR ring counts
      if (allRings.contains(atom)) { // it's in a ring
        atom.setFlag(CDKConstants.ISINRING, true);
        // lets find which ring sets it is a part of
        List<Integer> ringsizes = new ArrayList<Integer>();
        IRingSet currentRings = allRings.getRings(atom);
        int min = 0;
        for (int i = 0; i < currentRings.getAtomContainerCount(); i++) {
          int size = currentRings.getAtomContainer(i).getAtomCount();
          if (min > size) min = size;
          ringsizes.add(size);
        }
        atom.setProperty(CDKConstants.RING_SIZES, ringsizes);
        atom.setProperty(CDKConstants.SMALLEST_RINGS, sssr.getRings(atom));
      } else {
        atom.setFlag(CDKConstants.ISINRING, false);
      }

      // determine how many rings bonds each atom is a part of
      int hCount;
      if (atom.getImplicitHydrogenCount() == CDKConstants.UNSET) hCount = 0;
      else hCount = atom.getImplicitHydrogenCount();

      List<IAtom> connectedAtoms = atomContainer.getConnectedAtomsList(atom);
      int total = hCount + connectedAtoms.size();
      for (IAtom connectedAtom : connectedAtoms) {
        if (connectedAtom.getSymbol().equals("H")) {
          hCount++;
        }
      }
      atom.setProperty(CDKConstants.TOTAL_CONNECTIONS, total);
      atom.setProperty(CDKConstants.TOTAL_H_COUNT, hCount);

      if (valencesTable.get(atom.getSymbol()) != null) {
        int formalCharge =
            atom.getFormalCharge() == CDKConstants.UNSET ? 0 : atom.getFormalCharge();
        atom.setValency(valencesTable.get(atom.getSymbol()) - formalCharge);
      }
    }

    for (IBond bond : atomContainer.bonds()) {
      if (allRings.getRings(bond).getAtomContainerCount() > 0) {
        bond.setFlag(CDKConstants.ISINRING, true);
      }
    }

    for (IAtom atom : atomContainer.atoms()) {
      List<IAtom> connectedAtoms = atomContainer.getConnectedAtomsList(atom);

      int counter = 0;
      IAtom any;
      for (IAtom connectedAtom : connectedAtoms) {
        any = connectedAtom;
        if (any.getFlag(CDKConstants.ISINRING)) {
          counter++;
        }
      }
      atom.setProperty(CDKConstants.RING_CONNECTIONS, counter);
    }

    // check for atomaticity
    try {
      AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(atomContainer);
      CDKHueckelAromaticityDetector.detectAromaticity(atomContainer);
    } catch (CDKException e) {
      logger.debug(e.toString());
      throw new CDKException(e.toString(), e);
    }
  }
    public String perceiveSybylAtomTypes(IMolecule mol)
                        throws InvocationTargetException {
        
        ICDKMolecule cdkmol;
        
        try {
            cdkmol = cdk.asCDKMolecule(mol);
        } 
        catch (BioclipseException e) {
            System.out.println("Error converting cdk10 to cdk");
            e.printStackTrace();
            throw new InvocationTargetException(e);
        }
        
        IAtomContainer ac = cdkmol.getAtomContainer();
        CDKAtomTypeMatcher cdkMatcher 
            = CDKAtomTypeMatcher.getInstance(ac.getBuilder());
        AtomTypeMapper mapper 
            = AtomTypeMapper.getInstance(
                 "org/openscience/cdk/dict/data/cdk-sybyl-mappings.owl" );

        IAtomType[] sybylTypes = new IAtomType[ac.getAtomCount()];
        
        int atomCounter = 0;
        int a=0;
        for (IAtom atom : ac.atoms()) {
            IAtomType type;
            try {
                type = cdkMatcher.findMatchingAtomType(ac, atom);
            } 
            catch (CDKException e) {
                type = null;
            }
            if (type==null) {
//                logger.debug("AT null for atom: " + atom);
                type = atom.getBuilder().newAtomType(atom.getSymbol());
                type.setAtomTypeName("X");
            }
            AtomTypeManipulator.configure(atom, type);
            a++;
        }
        try {
            CDKHueckelAromaticityDetector.detectAromaticity(ac);
//            System.out.println("Arom: " 
//                + CDKHueckelAromaticityDetector.detectAromaticity(ac) );
		    } 
        catch (CDKException e) {
			    logger.debug("Failed to perceive aromaticity: " + e.getMessage());
		    }
        for (IAtom atom : ac.atoms()) {
            String mappedType = mapper.mapAtomType(atom.getAtomTypeName());
            if ("C.2".equals(mappedType)
                    && atom.getFlag(CDKConstants.ISAROMATIC)) {
                mappedType = "C.ar";
            } 
            else if ("N.pl3".equals(mappedType)
                    && atom.getFlag(CDKConstants.ISAROMATIC)) {
                mappedType = "N.ar";
            }
            try {
                sybylTypes[atomCounter] = factory.getAtomType(mappedType);
		        } 
            catch (NoSuchAtomTypeException e) {
                // yes, setting null's here is important
                sybylTypes[atomCounter] = null; 
			      }
            atomCounter++;
        }
        StringBuffer result = new StringBuffer();
        // now that full perception is finished, we can set atom type names:
        for (int i = 0; i < sybylTypes.length; i++) {
            if (sybylTypes[i] != null) {
                ac.getAtom(i).setAtomTypeName(sybylTypes[i].getAtomTypeName());
            } 
            else {
                ac.getAtom(i).setAtomTypeName("X");
            }
            
            result.append(i).append(':').append(ac.getAtom(i).getAtomTypeName())
                  /*.append("\n")*/;

        }
        return result.toString();
    }
  /**
   * @return
   * @throws Exception
   */
  public int process() throws Exception {
    if (file == null) throw new Exception("File not assigned! Use -f command line option.");
    if (!file.exists()) throw new FileNotFoundException(file.getAbsolutePath());
    int records_read = 0;
    int records_processed = 0;
    int records_error = 0;

    InputStream in = new FileInputStream(file);
    /**
     * cdk-io module http://ambit.uni-plovdiv.bg:8083/nexus/index.html#nexus-
     * search;classname~IteratingMDLReader
     */
    IteratingSDFReader reader = null;

    SDFWriter writer = new SDFWriter(new OutputStreamWriter(System.out));

    try {

      reader = new IteratingSDFReader(in, DefaultChemObjectBuilder.getInstance());
      LOGGER.log(Level.INFO, String.format("Reading %s", file.getAbsoluteFile()));
      while (reader.hasNext()) {
        /** Note recent versions allow IAtomContainer molecule = reader.next(); */
        Object object = reader.next();
        IAtomContainer molecule = null;
        if (object instanceof IAtomContainer) molecule = (IAtomContainer) object;
        else break;

        records_read++;
        try {
          /** cdk-standard module */
          AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule);
          // CDKHueckelAromaticityDetector.detectAromaticity(molecule);
          for (IAtom atom : molecule.atoms())
            if (atom.getImplicitHydrogenCount() == null) {
              LOGGER.fine(
                  atom.getSymbol()
                      + "\t"
                      + atom.getAtomTypeName()
                      + "\t"
                      + atom.getImplicitHydrogenCount());
              atom.setImplicitHydrogenCount(0);
            }

          molecule = AtomContainerManipulator.copyAndSuppressedHydrogens(molecule);

          /** Generate SMILES and assign as properties */
          assignSMILES(molecule);
          molecule.setProperty("REACTION", "REACTANT");
          molecule.setProperty("SMIRKS", "");
          /** Apply reactions */
          writer.write(molecule);
          for (int r = 0; r < smirks.length; r++) {
            if (reactions[r] == null) {
              reactions[r] = smrkMan.parse(smirks[r][1]);
            }
            IAtomContainer reactant = molecule.clone();
            if (smrkMan.applyTransformation(reactant, reactions[r])) {
              AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(reactant);
              reactant.setProperty("REACTION", "PRODUCT OF " + smirks[r][0]);
              reactant.setProperty("SMIRKS", smirks[r][1]);
              try {
                assignSMILES(reactant);
              } catch (Exception x) {
                LOGGER.log(Level.WARNING, x.getMessage());
              }
              writer.write(reactant);
            }
          }
          records_processed++;
          ;
        } catch (Exception x) {
          System.err.println("*");
          records_error++;
          LOGGER.log(
              Level.SEVERE,
              String.format("[Record %d] Error %s\n", records_read, file.getAbsoluteFile()),
              x);
        }
      }
    } catch (Exception x) {
      LOGGER.log(
          Level.SEVERE,
          String.format("[Record %d] Error %s\n", records_read, file.getAbsoluteFile()),
          x);
    } finally {
      try {
        reader.close();
      } catch (Exception x) {
      }
      try {
        writer.close();
      } catch (Exception x) {
      }
    }
    LOGGER.log(
        Level.INFO,
        String.format(
            "[Records read/processed/error %d/%d/%d] %s",
            records_read, records_processed, records_error, file.getAbsoluteFile()));
    return records_read;
  }
  /**
   * 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) + " ";
    }
  }
  /**
   * Get all paths of lengths 0 to the specified length.
   *
   * <p>This method will find all paths upto length N starting from each atom in the molecule and
   * return the unique set of such paths.
   *
   * @param container The molecule to search
   * @return A map of path strings, keyed on themselves
   */
  private Integer[] findPaths(IAtomContainer container) {

    ShortestPathWalker walker = new ShortestPathWalker(container);
    // convert paths to hashes
    List<Integer> paths = new ArrayList<Integer>();
    int patternIndex = 0;

    for (String s : walker.paths()) {
      int toHashCode = s.hashCode();
      paths.add(patternIndex, toHashCode);
      patternIndex++;
    }

    /*
     * Add ring information
     */
    IRingSet sssr = Cycles.essential(container).toRingSet();
    RingSetManipulator.sort(sssr);
    for (Iterator<IAtomContainer> it = sssr.atomContainers().iterator(); it.hasNext(); ) {
      IAtomContainer ring = it.next();
      int toHashCode = String.valueOf(ring.getAtomCount()).hashCode();
      paths.add(patternIndex, toHashCode);
      patternIndex++;
    }
    /*
     * Check for the charges
     */
    List<String> l = new ArrayList<String>();
    for (Iterator<IAtom> it = container.atoms().iterator(); it.hasNext(); ) {
      IAtom atom = it.next();
      int charge = atom.getFormalCharge() == null ? 0 : atom.getFormalCharge();
      if (charge != 0) {
        l.add(atom.getSymbol().concat(String.valueOf(charge)));
      }
    }
    Collections.sort(l);
    int toHashCode = l.hashCode();
    paths.add(patternIndex, toHashCode);
    patternIndex++;

    l = new ArrayList<String>();
    /*
     * atom stereo parity
     */
    for (Iterator<IAtom> it = container.atoms().iterator(); it.hasNext(); ) {
      IAtom atom = it.next();
      int st = atom.getStereoParity() == null ? 0 : atom.getStereoParity();
      if (st != 0) {
        l.add(atom.getSymbol().concat(String.valueOf(st)));
      }
    }
    Collections.sort(l);
    toHashCode = l.hashCode();
    paths.add(patternIndex, toHashCode);
    patternIndex++;

    if (container.getSingleElectronCount() > 0) {
      StringBuilder radicalInformation = new StringBuilder();
      radicalInformation.append("RAD: ").append(String.valueOf(container.getSingleElectronCount()));
      paths.add(patternIndex, radicalInformation.toString().hashCode());
      patternIndex++;
    }
    if (container.getLonePairCount() > 0) {
      StringBuilder lpInformation = new StringBuilder();
      lpInformation.append("LP: ").append(String.valueOf(container.getLonePairCount()));
      paths.add(patternIndex, lpInformation.toString().hashCode());
      patternIndex++;
    }
    return paths.toArray(new Integer[paths.size()]);
  }