diff --git a/src/main/org/openscience/cdk/formula/IsotopeContainer.java b/src/main/org/openscience/cdk/formula/IsotopeContainer.java new file mode 100644 index 00000000000..1e940753174 --- /dev/null +++ b/src/main/org/openscience/cdk/formula/IsotopeContainer.java @@ -0,0 +1,125 @@ +package org.openscience.cdk.formula; + +import org.openscience.cdk.annotations.TestClass; +import org.openscience.cdk.annotations.TestMethod; +import org.openscience.cdk.interfaces.IMolecularFormula; +import org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator; + + +/** + * This class defines a isotope container. It contains in principle a + * IMolecularFormula, a mass and intensity/abundance value. + * + * @author Miguel Rojas Cherto + * + * @cdk.module formula + */ +@TestClass("org.openscience.cdk.formula.IsotopeContainerTest") +public class IsotopeContainer{ + private IMolecularFormula form; + private double masOs; + private double inte; + + /** + * Constructor of the IsotopeContainer object. + */ + public IsotopeContainer(){ + + } + /** + * Constructor of the IsotopeContainer object setting a IMolecularFormula + * object and intensity value. + * + * @param formula The formula of this container + * @param intensity The intensity of this container + */ + @TestMethod("testIsotopeContainer_IMolecularFormula_double") + public IsotopeContainer(IMolecularFormula formula, double intensity){ + form = formula; + if(formula != null) + masOs = MolecularFormulaManipulator.getTotalExactMass(formula); + inte = intensity; + } + /** + * Constructor of the IsotopeContainer object setting a mass + * and intensity value. + * + * @param mass The mass of this container + * @param intensity The intensity of this container + */ + @TestMethod("testIsotopeContainer_double_double") + public IsotopeContainer(double mass, double intensity){ + masOs = mass; + inte = intensity; + } + /** + * Set IMolecularFormula object of this container. + * + * @param formula The IMolecularFormula of the this container + */ + @TestMethod("testSetFormula_IMolecularFormula") + public void setFormula(IMolecularFormula formula){ + form = formula; + } + /** + * Set the mass value of this container. + * + * @param mass The mass of the this container + */ + @TestMethod("testSetMass_double") + public void setMass(double mass){ + masOs = mass; + } + + /** + * Set the intensity value of this container. + * + * @param intensity The intensity of the this container + */ + @TestMethod("testSetIntensity_double") + public void setIntensity(double intensity){ + inte = intensity; + } + /** + * Get the IMolecularFormula object of this container. + * + * @return The IMolecularformula of the this container + */ + @TestMethod("testGetFormula") + public IMolecularFormula getFormula(){ + return form; + } + /** + * Get the mass value of this container. + * + * @return The mass of the this container + */ + @TestMethod("testGetMass") + public double getMass(){ + return masOs; + } + + /** + * Get the intensity value of this container. + * + * @return The intensity of the this container + */ + @TestMethod("testGetIntensity") + public double getIntensity(){ + return inte; + } + /** + * Clones this IsotopeContainer object and its content. + * + * @return The cloned object + */ + @TestMethod("testClone") + public Object clone() throws CloneNotSupportedException { + IsotopeContainer isoClone = new IsotopeContainer(); + isoClone.setFormula(getFormula()); + isoClone.setIntensity(getIntensity()); + isoClone.setMass(getMass()); + return isoClone; + } + +} diff --git a/src/main/org/openscience/cdk/formula/IsotopePattern.java b/src/main/org/openscience/cdk/formula/IsotopePattern.java new file mode 100644 index 00000000000..c06fba77086 --- /dev/null +++ b/src/main/org/openscience/cdk/formula/IsotopePattern.java @@ -0,0 +1,132 @@ +package org.openscience.cdk.formula; + +import java.util.ArrayList; +import java.util.List; + +import org.openscience.cdk.annotations.TestClass; +import org.openscience.cdk.annotations.TestMethod; + + + +/** + * This class defines the properties of a deisotoped + * pattern distribution. A isotope pattern is a set of + * compounds with different set of isotopes. + * + * @author Miguel Rojas Cherto + * + * @cdk.module formula + */ +@TestClass("org.openscience.cdk.formula.IsotopePatternTest") +public class IsotopePattern { + + private List isotopeCList = new ArrayList(); + + private int monoIsotopePosition; + + private double chargI=0; + + /** + * Constructor of the IsotopePattern object. + */ + public IsotopePattern(){ + + } + /** + * Set the mono isotope object. + * + * @param isoContainer The IsotopeContainer object + */ + @TestMethod("testSetMonoIsotope_IsotopeContainer") + public void setMonoIsotope(IsotopeContainer isoContainer){ + isotopeCList.add(isoContainer); + monoIsotopePosition = isotopeCList.indexOf(isoContainer); + } + /** + * Add an isotope object. + * + * @param isoContainer The IsotopeContainer object + */ + @TestMethod("testAddIsotope_IsotopeContainer") + public void addIsotope(IsotopeContainer isoContainer){ + isotopeCList.add(isoContainer); + } + /** + * Returns the mono-isotope peak that form this isotope pattern. + * + * @return The IsotopeContainer acting as mono-isotope + */ + @TestMethod("testGetMonoIsotope") + public IsotopeContainer getMonoIsotope(){ + return isotopeCList.get(monoIsotopePosition); + } + + /** + * Returns the all isotopes that form this isotope pattern. + * + * @return The IsotopeContainer acting as mono-isotope + */ + @TestMethod("testGetIsotopes") + public List getIsotopes(){ + return isotopeCList; + } + /** + * Returns the a isotopes given a specific position. + * + * @param The position + * @return The isotope + */ + @TestMethod("testGetIsotope_int") + public IsotopeContainer getIsotope(int position){ + return isotopeCList.get(position); + } + /** + * Returns the number of isotopes in this pattern. + * + * @return The number of isotopes + */ + @TestMethod("testGetNumberOfIsotopes") + public int getNumberOfIsotopes(){ + return isotopeCList.size(); + } + + /** + * Set the charge in this pattern. + * + * @param charge The charge value + */ + @TestMethod("testSetCharge_double") + public void setCharge(double charge) { + chargI = charge; + + } + + /** + * Get the charge in this pattern. + * + * @return The charge value + */ + @TestMethod("testGetCharge") + public double getCharge() { + return chargI; + + } + /** + * Clones this IsotopePattern object and its content. + * + * @return The cloned object + */ + @TestMethod("testClone") + public Object clone() throws CloneNotSupportedException { + IsotopePattern isoClone = new IsotopePattern(); + IsotopeContainer isoHighest = getMonoIsotope(); + for(IsotopeContainer isoContainer: isotopeCList){ + if(isoHighest.equals(isoContainer)) + isoClone.setMonoIsotope((IsotopeContainer) isoContainer.clone()); + else + isoClone.addIsotope((IsotopeContainer) isoContainer.clone()); + } + isoClone.setCharge(getCharge()); + return isoClone; + } +} diff --git a/src/main/org/openscience/cdk/formula/IsotopePatternGenerator.java b/src/main/org/openscience/cdk/formula/IsotopePatternGenerator.java index 1aec472ea54..87c99e58c6f 100644 --- a/src/main/org/openscience/cdk/formula/IsotopePatternGenerator.java +++ b/src/main/org/openscience/cdk/formula/IsotopePatternGenerator.java @@ -20,22 +20,15 @@ */ package org.openscience.cdk.formula; -import java.io.IOException; -import java.io.OptionalDataException; -import java.util.ArrayList; -import java.util.Hashtable; +import java.util.HashMap; import java.util.Iterator; -import java.util.List; -import java.util.Map; +import java.util.Set; import org.openscience.cdk.annotations.TestClass; -import org.openscience.cdk.annotations.TestMethod; import org.openscience.cdk.config.IsotopeFactory; -import org.openscience.cdk.interfaces.IAtom; import org.openscience.cdk.interfaces.IChemObjectBuilder; import org.openscience.cdk.interfaces.IIsotope; import org.openscience.cdk.interfaces.IMolecularFormula; -import org.openscience.cdk.interfaces.IMolecularFormulaSet; import org.openscience.cdk.tools.LoggingTool; import org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator; @@ -43,7 +36,7 @@ * Generates all Combinatorial chemical isotopes given a structure. * * @cdk.module formula - * @author miguelrojasch + * @author Miguel Rojas Cherto * @cdk.created 2007-11-20 * @cdk.svnrev $Revision$ * @@ -53,17 +46,20 @@ @TestClass("org.openscience.cdk.formula.IsotopePatternGeneratorTest") public class IsotopePatternGenerator{ + private IChemObjectBuilder builder = null; + private IsotopeFactory isoFactory; + private IsotopePattern abundance_Mass = null; + private LoggingTool logger = new LoggingTool(IsotopePatternGenerator.class); - private IsotopeFactory isotopeFactory; - - /** Minimal abundance of the isotopes to be added in the combinatorial search.*/ - private double minAbundance = 10.0; + /** Minimal abundance of the isotopes to be added in the combinatorial search.*/ + private double minAbundance = .1; + /** * Constructor for the IsotopeGenerator. */ public IsotopePatternGenerator(){ - this(10.0); + this(0.1); } /** @@ -71,322 +67,217 @@ public IsotopePatternGenerator(){ * * @param minAb Minimal abundance of the isotopes to be added * in the combinatorial search + * */ public IsotopePatternGenerator(double minAb){ minAbundance = minAb; - logger.info("Generating all Isotope structures with IsotopeGenerator"); - } - - private void ensureIsotopeFactory(IChemObjectBuilder builder) { - if (isotopeFactory == null) { - try { - isotopeFactory = IsotopeFactory.getInstance(builder); - } catch (OptionalDataException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } + logger.info("Generating all Isotope structures with IsotopeGenerator"); } - /** - * Get all the isotope distribution given a IMolecularFormula. - * - * @param molFor The IMolecularFormula to start - * @return A List containing the different abundance distribution - */ - @TestMethod("testGetIsotopeDistribution_IMolecularFormula") - public List getIsotopeDistribution(IMolecularFormula molFor){ - - List distribution = getIsotopeDistribution(getIsotopes(molFor)); - - return distribution; - } - /** - * Get all the isotope distribution given the IMolecularFormulaSet containing the - * occurrence of each combination. - * - * @param molForSet The IMolecularFormulaSet to start - * @return A List containing the different abundance distribution - */ - @TestMethod("testGetIsotopeDistribution_IMolecularFormulaSet") - public List getIsotopeDistribution(IMolecularFormulaSet molForSet){ - List distribution = new ArrayList(); - double abT = MolecularFormulaManipulator.getTotalNaturalAbundance(molForSet.getMolecularFormula(0)); - - for(IMolecularFormula formula : molForSet.molecularFormulas()){ - double ab = MolecularFormulaManipulator.getTotalNaturalAbundance(formula); - double occurrence = ((Double)formula.getProperty("occurrence")); - ab *= occurrence; - distribution.add(ab/abT*100.0); - } - return distribution; - } - /** - * Get all the isotope masses distribution given the IMolecularFormulaSet. - * - * @param molForSet The IMolecularFormulaSet to start - * @return A List containing the different mass distribution - */ - @TestMethod("testGetMassDistribution_IMolecularFormulaSet") - public List getMassDistribution(IMolecularFormulaSet molForSet){ - List distribution = new ArrayList(); - - for(IMolecularFormula formula : molForSet.molecularFormulas()){ - double mm = MolecularFormulaManipulator.getTotalExactMass(formula); - distribution.add(mm); - } - return distribution; - } - /** * Get all combinatorial chemical isotopes given a structure. * * @param molFor The IMolecularFormula to start - * @return A IMolecularFormulaSet containing the different combinations + * @return A IsotopePattern object containing the different combinations */ - @TestMethod("testGetIsotopes_IMolecularFormula") - public IMolecularFormulaSet getIsotopes(IMolecularFormula molFor){ - ensureIsotopeFactory(molFor.getBuilder()); + public IsotopePattern getIsotopes(IMolecularFormula molFor){ - /** FormulaSet to return*/ - IMolecularFormulaSet molForSet = molFor.getBuilder().newMolecularFormulaSet(); - /** all isotopes found*/ - List isotopicAtoms = new ArrayList(); - /** Atoms with isotopes*/ - List atomWithIsotopes = new ArrayList(); - /** Atoms with isotopes*/ - List isotopicAtomsV = new ArrayList(); - - /*Number of permutations*/ - int nC = 1; - - /*search atoms which have more than one isotope and they have the minimum abundance*/ - int countt = 0; - Iterator itI2 = molFor.isotopes().iterator(); - while(itI2.hasNext()){ - IIsotope isotope = itI2.next(); - for(int z = 0 ; z < molFor.getIsotopeCount(isotope); z++){ - IAtom atom = molFor.getBuilder().newAtom(isotope.getSymbol()); - List isotopicAtoms2 = new ArrayList(); - - IIsotope[] isotopes = isotopeFactory.getIsotopes(atom.getSymbol()); - int count = 0; - for (int i = 0; i < isotopes.length; i++) - if (isotopes[i].getNaturalAbundance() > minAbundance ) - count++; - - for (int i = 0; i < isotopes.length; i++){ - if (isotopes[i].getNaturalAbundance() > minAbundance ){ - isotopicAtoms.add(isotopes[i]); - isotopicAtoms2.add(isotopes[i]); - } - } - - atomWithIsotopes.add(countt,atom); - isotopicAtomsV.add(countt,isotopicAtoms2); - countt ++; - nC = nC*count; - } - } - if(isotopicAtoms.size() != 0) - molForSet = mixer(molFor, isotopicAtomsV, isotopicAtoms, nC); - else - molForSet.addMolecularFormula(molFor); - - - return orderAccordingMass(molForSet); - } - /** - * Put in order the IMolecularFormulaSet according their mass. - * - * @param molForSet The IMolecularFormulaSet - * @return The IMolecularFormulaSet ordered - */ - private IMolecularFormulaSet orderAccordingMass(IMolecularFormulaSet molForSet) { - IMolecularFormulaSet newMolForSet = molForSet.getBuilder().newMolecularFormulaSet(); - int countMFSet = molForSet.size(); - for(int i = 0 ; i < countMFSet; i++){ - double massMin = 10000; - IMolecularFormula molForToAdd = null; - for(int j = 0 ; j < molForSet.size(); j++){ - IMolecularFormula molFor = molForSet.getMolecularFormula(j); - double mass = MolecularFormulaManipulator.getTotalExactMass(molFor); - if( massMin > mass){ - massMin = mass; - molForToAdd = molFor; + if(builder==null){ + try { + isoFactory = IsotopeFactory.getInstance(molFor.getBuilder()); + builder = molFor.getBuilder(); + } catch (Exception e) { + e.printStackTrace(); + } + } + String mf = MolecularFormulaManipulator.getString(molFor,true); + + // Divide the chemical formula into tokens (element and coefficients) + HashMap tokens = new HashMap(); + IMolecularFormula molecularFormula = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula(mf, builder); + for(IIsotope isos :molecularFormula.isotopes()) + tokens.put(isos.getSymbol(), molecularFormula.getIsotopeCount(isos)); + + int atomCount; + for(IIsotope isos : molecularFormula.isotopes()){ + String elementSymbol = isos.getSymbol(); + atomCount = tokens.get(elementSymbol); + + for (int i = 0; i < atomCount; i++) { + if (!calculateAbundanceAndMass(elementSymbol)) { } } - newMolForSet.addMolecularFormula(molForToAdd); - molForSet.removeMolecularFormula(molForToAdd); } - return newMolForSet; + + IsotopePattern isoP = IsotopePatternManipulator.sortAndNormalizedByIntensity(abundance_Mass); + isoP = cleanAbundance(isoP, minAbundance); + IsotopePattern isoPattern = IsotopePatternManipulator.sortByMass(isoP); + + return isoPattern; + } /** - * Combine all possible isotopes. - * - * @param molFor IMolecularFormula to analyze - * @param isotopicAtoms An arrayList containing all isotopes - * @param atomWithIsotopes An arrayList containing atoms which have isotopes - * @param nc Number of combinations + * Calculates the mass and abundance of all isotopes generated by adding one + * atom. Receives the periodic table element and calculate the isotopes, if + * there exist a previous calculation, add these new isotopes. In the + * process of adding the new isotopes, remove those that has an abundance + * less than setup parameter minAbundance, and remove duplicated masses. * - * @return The IMolecularFormulaSet + * @param elementSymbol The chemical element symbol + * @return */ - private IMolecularFormulaSet mixer(IMolecularFormula molFor, ListisotopicAtomsV, List isotopicAtoms, int nC){ - IMolecularFormulaSet molForSet = molFor.getBuilder().newMolecularFormulaSet(); - + private boolean calculateAbundanceAndMass(String elementSymbol) { - int[][] ordreComb = new int[100][isotopicAtomsV.size()]; - List ordreCombList = new ArrayList(); - List atomsCombList = new ArrayList(); - - Map massV = new Hashtable(); - int column[] = new int[isotopicAtomsV.size()]; + IIsotope[] isotopes = isoFactory.getIsotopes(elementSymbol); - for (int j = 0; j < isotopicAtomsV.size(); j++){ - - column[j] = 1; + if (isotopes == null) + return false; + + if (isotopes.length == 0) + return false; + + double mass, previousMass, abundance, totalAbundance, newAbundance; + + HashMap isotopeMassAndAbundance = new HashMap(); + IsotopePattern currentISOPattern = new IsotopePattern(); + + // Generate isotopes for the current atom (element) + for (int i = 0; i < isotopes.length; i++) { + mass = isotopes[i].getExactMass(); + abundance = isotopes[i].getNaturalAbundance(); + currentISOPattern.addIsotope(new IsotopeContainer(mass, abundance)); } - // create a matrix with the necessary order - double abundRef = 0; - for (int i = 0; i < nC; i++){ - //+++++++++++++++++++++++++++++++++++++++++++++++++++ - /*order of the combinations between isolations*/ - int[] ordreTmp = new int[column.length]; - /*order of the Atoms as string*/ - String[] atomsTmp = new String[column.length]; - - for (int j = 0; j < isotopicAtomsV.size(); j++) - ordreTmp[j] = column[j]; - - - double massEx = calculateMass(ordreTmp,isotopicAtomsV,isotopicAtoms); - double abund = calculateAbund(ordreTmp,isotopicAtomsV,isotopicAtoms); - - boolean flag = true; - if(i==0) - abundRef = abund; - else{ - double abundNor = abund/abundRef; - if(abundNor < 0.0001 ) - flag = false; - - } - - if(flag){ - String massEx_String = reduceDigits(Double.toString(massEx)); - if(!massV.containsKey(massEx_String)){ - - massV.put(massEx_String, 1); - int[] ordreNewTmp = new int[column.length]; - for (int k = 0; k < isotopicAtomsV.size(); k++) - ordreNewTmp[k] = ordreTmp[k]; - ordreCombList.add(ordreNewTmp); - for (int j = 0; j < ordreComb[0].length; j++) - atomsTmp[j] = ((IIsotope)isotopicAtomsV.get(j).get(0)).getSymbol(); - atomsCombList.add(atomsTmp); - }else{ - int occurr = massV.get(massEx_String); - massV.put(massEx_String, occurr+1); + + + // Verify if there is a previous calculation. If it exists, add the new + // isotopes + if (abundance_Mass == null) { + + abundance_Mass = currentISOPattern; + return true; + + } else { + for (int i = 0; i < abundance_Mass.getNumberOfIsotopes(); i++) { + totalAbundance = abundance_Mass.getIsotopes().get(i).getIntensity(); + + if (totalAbundance == 0) + continue; + + for (int j = 0; j < currentISOPattern.getNumberOfIsotopes(); j++) { + + abundance = currentISOPattern.getIsotopes().get(j).getIntensity(); + mass = abundance_Mass.getIsotopes().get(i).getMass(); + + if (abundance == 0) + continue; + + newAbundance = totalAbundance * abundance * 0.01f; + mass += currentISOPattern.getIsotopes().get(j).getMass(); + + // Filter duplicated masses + previousMass = searchMass(isotopeMassAndAbundance.keySet(), + mass); + if (isotopeMassAndAbundance.containsKey(previousMass)) { + newAbundance += isotopeMassAndAbundance + .get(previousMass); + mass = previousMass; + } + + // Filter isotopes too small + if (isNotZero(newAbundance)) { + isotopeMassAndAbundance.put(mass, newAbundance); + } + previousMass = 0; } } - column[isotopicAtomsV.size() - 1]++; - //+++++++++++++++++++++++++++++++++++++++++++++++++++ - - - // control of the end of each column - for (int k = isotopicAtomsV.size() - 1; k >= 0; k--){ - if (column[k] > isotopicAtomsV.get(k).size()){ - column[k] = 1; - if(k-1 >= 0) - column[k - 1]++; - } + + Iterator itr = isotopeMassAndAbundance.keySet().iterator(); + int i = 0; + abundance_Mass = new IsotopePattern(); + while (itr.hasNext()) { + mass = itr.next(); + abundance_Mass.addIsotope(new IsotopeContainer(mass, isotopeMassAndAbundance.get(mass))); + i++; } - } - - /*set the correct isotope for each structure*/ - for (int i = 0; i < ordreCombList.size(); i++){ - - /*Create the IMolecularFormula*/ - IMolecularFormula molForClon = molFor.getBuilder().newMolecularFormula(); - int[] ordreTmp = ordreCombList.get(i); - String[] atomsStringTmp = atomsCombList.get(i); - for (int j = 0; j < ordreTmp.length; j++){ - - IIsotope isotope = molFor.getBuilder().newIsotope(atomsStringTmp[j]); - - isotope.setExactMass(((IIsotope)isotopicAtomsV.get(j).get(ordreTmp[j]-1)).getExactMass()); - isotope.setNaturalAbundance(((IIsotope)isotopicAtomsV.get(j).get(ordreTmp[j]-1)).getNaturalAbundance()); - - molForClon.addIsotope(isotope); - } - - /*Put the occurrence of this MolecularFormula which was found*/ - double massEx = calculateMass(ordreTmp,isotopicAtomsV,isotopicAtoms); - String massEx_String = reduceDigits(Double.toString(massEx)); - - double prob = massV.get(massEx_String); - Map hash = new Hashtable(); - hash.put("occurrence", prob); - molForClon.setProperties(hash); - - molForSet.addMolecularFormula(molForClon); - - } - return molForSet; + return true; + } - + /** - * Reduce the digits after pint of this Double convert in String to 8 - * - * @param string The String to reduce - * @return The String reduced + * Search the key mass in this Set. + * + * @param keySet The Set object + * @param mass The mass to look for + * @return The key value */ - private String reduceDigits(String string) { - int posi = 0; - for (int ss = 0; ss < string.length(); ss ++) { - if (string.charAt(ss) == '.') - posi = ss; - } - int maxSize = string.length(); - if(maxSize-posi > 9) - return string.substring(0,posi+8); - else - return string; + private double searchMass(Set keySet, double mass) { + double TOLERANCE = 0.00005f; + double diff; + for (double key : keySet) { + diff = Math.abs(key - mass); + if (diff < TOLERANCE) + return key; + } + + return 0.0d; } /** - * Calculate of the Mass. + * Detection if the value is zero. * - * @param ordreTmp - * @param isotopicAtoms - * - * @return The mass total + * @param number The number to analyze + * @return TRUE, if it zero */ - private double calculateMass(int[] ordreTmp,ListisotopicAtomsV,List isotopicAtoms) { - double massTotal = 0; - for (int j = 0; j < ordreTmp.length; j++){ - double mass = ((IIsotope)isotopicAtomsV.get(j).get(ordreTmp[j]-1)).getExactMass(); - massTotal += mass; - } - return massTotal; + private boolean isNotZero(double number) { + double pow = (double) Math.pow(10, 6); + int fraction = (int) (number * pow); + + if (fraction <= 0) + return false; + + return true; } + /** - * Calculate of the Abundance. - * - * @param ordreTmp - * @param isotopicAtoms + * Normalize the intensity (relative abundance) of all isotopes in relation + * of the most abundant isotope. * - * @return The abundance total + * @param isopattern The IsotopePattern object + * @param minAbundance The minimum abundance + * @return The IsotopePattern cleaned */ - private double calculateAbund(int[] ordreTmp,ListisotopicAtomsV,List isotopicAtoms) { - double abundanceTotal = 1.0; - for (int j = 0; j < ordreTmp.length; j++){ - double abund = ((IIsotope)isotopicAtomsV.get(j).get(ordreTmp[j]-1)).getNaturalAbundance(); - abundanceTotal = abundanceTotal* abund; - + private IsotopePattern cleanAbundance(IsotopePattern isopattern, + double minAbundance) { + + double intensity, biggestIntensity = 0.0f; + + for (IsotopeContainer sc : isopattern.getIsotopes()) { + + intensity = sc.getIntensity(); + if (intensity > biggestIntensity) + biggestIntensity = intensity; + } - return abundanceTotal/Math.pow(100,ordreTmp.length); + + for (IsotopeContainer sc : isopattern.getIsotopes()) { + + intensity = sc.getIntensity(); + intensity /= biggestIntensity; + if (intensity < 0) + intensity = 0; + + sc.setIntensity(intensity); + + } + + IsotopePattern sortedIsoPattern = new IsotopePattern(); + sortedIsoPattern.setMonoIsotope(new IsotopeContainer(isopattern.getIsotopes().get(0).getMass(),isopattern.getIsotopes().get(0).getIntensity())); + for (int i = 1; i < isopattern.getNumberOfIsotopes();i++) { + if (isopattern.getIsotopes().get(i).getIntensity() >= (minAbundance )) + sortedIsoPattern.addIsotope(new IsotopeContainer(isopattern.getIsotopes().get(i).getMass(),isopattern.getIsotopes().get(i).getIntensity())); + } + return sortedIsoPattern; + } -} +} \ No newline at end of file diff --git a/src/main/org/openscience/cdk/formula/IsotopePatternManipulator.java b/src/main/org/openscience/cdk/formula/IsotopePatternManipulator.java new file mode 100644 index 00000000000..2bae6be732c --- /dev/null +++ b/src/main/org/openscience/cdk/formula/IsotopePatternManipulator.java @@ -0,0 +1,151 @@ +package org.openscience.cdk.formula; + +import java.util.List; + +import org.openscience.cdk.annotations.TestClass; +import org.openscience.cdk.annotations.TestMethod; + +/** + * Class to manipulate IsotopePattern objects. + * + * @author Miguel Rojas Cherto + * + * @cdk.module formula + */ +@TestClass("org.openscience.cdk.formula.IsotopePatternManipulatorTest") +public class IsotopePatternManipulator { + + /** + * Return the isotope pattern normalized to the highest abundance + * + * @param isotopeP The IsotopePattern object to normalize + * @return The IsotopePattern normalized + */ + @TestMethod("testNormalize_IsotopePattern") + public static IsotopePattern normalize(IsotopePattern isotopeP){ + IsotopeContainer isoHighest = null; + + double biggestAbundance = 0; + /*Extraction of the isoContainer with the highest abundance*/ + for(IsotopeContainer isoContainer: isotopeP.getIsotopes()){ + double abundance = isoContainer.getIntensity(); + if(biggestAbundance < abundance){ + biggestAbundance = abundance; + isoHighest = isoContainer; + } + } + /*Normalize*/ + IsotopePattern isoNormalized = new IsotopePattern(); + for(IsotopeContainer isoContainer: isotopeP.getIsotopes()){ + double inten = isoContainer.getIntensity()/isoHighest.getIntensity(); + IsotopeContainer icClone; + try { + icClone = (IsotopeContainer) isoContainer.clone(); + icClone.setIntensity(inten); + if(isoHighest.equals(isoContainer)) + isoNormalized.setMonoIsotope(icClone); + else + isoNormalized.addIsotope(icClone); + + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + + } + isoNormalized.setCharge(isotopeP.getCharge()); + return isoNormalized; + } + /** + * Return the isotope pattern sorted and normalized by intensity + * to the highest abundance. + * + * @param isotopeP The IsotopePattern object to sort + * @return The IsotopePattern sorted + */ + @TestMethod("testSortAndNormalizedByIntensity_IsotopePattern") + public static IsotopePattern sortAndNormalizedByIntensity(IsotopePattern isotopeP){ + IsotopePattern isoNorma = normalize(isotopeP); + return sortByIntensity(isoNorma); + } + /** + * Return the isotope pattern sorted by intensity + * to the highest abundance. + * + * @param isotopeP The IsotopePattern object to sort + * @return The IsotopePattern sorted + */ + @TestMethod("testSortByIntensity_IsotopePattern") + public static IsotopePattern sortByIntensity(IsotopePattern isotopeP){ + try { + IsotopePattern isoSort = new IsotopePattern(); + List listISO = ((IsotopePattern)isotopeP.clone()).getIsotopes(); + + int length = listISO.size()-1; + for(int i = length ; i >= 0 ; i--){ + double intensity = 0; + IsotopeContainer isoHighest = null; + for(IsotopeContainer isoContainer: listISO){ + if(isoContainer.getIntensity() > intensity){ + isoHighest = isoContainer; + intensity = isoContainer.getIntensity(); + } + } + if(i == length) + isoSort.setMonoIsotope((IsotopeContainer) isoHighest.clone()); + else + isoSort.addIsotope((IsotopeContainer) isoHighest.clone()); + + listISO.remove(isoHighest); + + } + isoSort.setCharge(isotopeP.getCharge()); + return isoSort; + + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + + return null; + } + + /** + * Return the isotope pattern sorted by mass + * to the highest abundance. + * + * @param isotopeP The IsotopePattern object to sort + * @return The IsotopePattern sorted + */ + @TestMethod("testSortByMass_IsotopePattern") + public static IsotopePattern sortByMass(IsotopePattern isotopeP){ + try { + IsotopePattern isoSort = new IsotopePattern(); + List listISO = ((IsotopePattern)isotopeP.clone()).getIsotopes(); + + int length = listISO.size()-1; + for(int i = length ; i >= 0 ; i--){ + double mass = 100000; + IsotopeContainer isoHighest = null; + for(IsotopeContainer isoContainer: listISO){ + if(isoContainer.getMass() < mass){ + isoHighest = isoContainer; + mass = isoContainer.getMass(); + } + } + if(i == length) + isoSort.setMonoIsotope((IsotopeContainer) isoHighest.clone()); + else + isoSort.addIsotope((IsotopeContainer) isoHighest.clone()); + + listISO.remove(isoHighest); + + } + isoSort.setCharge(isotopeP.getCharge()); + return isoSort; + + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + + return null; + } +} diff --git a/src/main/org/openscience/cdk/formula/IsotopePatternSimilarity.java b/src/main/org/openscience/cdk/formula/IsotopePatternSimilarity.java new file mode 100644 index 00000000000..77be95a118a --- /dev/null +++ b/src/main/org/openscience/cdk/formula/IsotopePatternSimilarity.java @@ -0,0 +1,127 @@ +package org.openscience.cdk.formula; + +import org.openscience.cdk.annotations.TestClass; +import org.openscience.cdk.annotations.TestMethod; + + +/** + * This class gives a score hit of similarity between two different + * isotope abundance pattern. + * + * @author Miguel Rojas Cherto + * + * @cdk.module formula + */ +@TestClass("org.openscience.cdk.formula.IsotopePatternSimilarityTest") +public class IsotopePatternSimilarity{ + + private double chargeToAdd; + + private double tolerance_ppm = 1; + + private static double massE = 0.0005485; + + /** + * Constructor for the IsotopePatternSimilarity object. + */ + public IsotopePatternSimilarity() { + } + + /** + * Set the tolerance of the mass accuracy. + * + * @param tolerance The tolerance value + */ + @TestMethod("testSeTolerance_double") + public void seTolerance(double tolerance) { + tolerance_ppm = tolerance; + } + /** + * Get the tolerance of the mass accuracy. + * + * @return The tolerance value + */ + @TestMethod("testGetTolerance") + public double getTolerance(){ + return tolerance_ppm; + } + /** + * Compare the IMolecularFormula with a isotope + * abundance pattern. + * + * + * @param isoto1 The Isotope pattern reference (predicted) + * @param isoto2 The Isotope pattern reference (detected) + * @return The hit score of similarity + */ + @TestMethod("testCompare_IsotopePattern_IsotopePattern") + public double compare(IsotopePattern isoto1, IsotopePattern isoto2){ + + IsotopePattern iso1 = IsotopePatternManipulator.sortAndNormalizedByIntensity(isoto1); + IsotopePattern iso2 = IsotopePatternManipulator.sortAndNormalizedByIntensity(isoto2); + + /*charge to add*/ + if(isoto1.getCharge() == 1) + chargeToAdd = -massE; + else if(isoto1.getCharge() == -1) + chargeToAdd = massE; + else + chargeToAdd = 0; + + double diffMass, diffAbun, factor, totalFactor = 0d; + double score = 0d, tempScore; + // Maximum number of isotopes to be compared according predicted isotope + // pattern. It is assumed that this will have always more isotopeContainers + int length = iso1.getNumberOfIsotopes(); + + for (int i = 0 ; i< length; i++){ + IsotopeContainer isoContainer = iso1.getIsotopes().get(i); + factor = isoContainer.getIntensity(); + totalFactor += factor; + + // Search for the closest isotope in the second pattern (detected) to the + // current isotope (predicted pattern) + int closestDp = getClosestDataDiff(isoContainer, iso2); + if (closestDp == -1) + continue; + + diffMass = isoContainer.getMass()+chargeToAdd - iso2.getIsotopes().get(closestDp).getMass(); + diffMass = Math.abs(diffMass); + + diffAbun = 1.0d - (isoContainer.getIntensity() / iso2.getIsotopes().get(closestDp).getIntensity()); + diffAbun = Math.abs(diffAbun); + + tempScore = 1 - (diffMass + diffAbun); + + if (tempScore < 0) + tempScore = 0; + + score += (tempScore * factor); + + } + + return score / totalFactor; + } + + /** + * Search and find the closest difference in an array in terms of mass and + * intensity. Always return the position in this List. + * + * @param diffValue The difference to look for + * @param normMass A List of normalized masses + * @return The position in the List + */ + private int getClosestDataDiff(IsotopeContainer isoContainer, IsotopePattern pattern) { + double diff = 100; + int posi = -1; + for(int i = 0 ; i < pattern.getNumberOfIsotopes(); i++) { + double tempDiff = Math.abs((isoContainer.getMass() + chargeToAdd) - pattern.getIsotopes().get(i).getMass()); + if (tempDiff <= (tolerance_ppm/isoContainer.getMass()) && tempDiff < diff){ + diff = tempDiff; + posi = i; + } + } + + return posi; + } +} diff --git a/src/main/org/openscience/cdk/formula/rules/IsotopePatternRule.java b/src/main/org/openscience/cdk/formula/rules/IsotopePatternRule.java index 7bb8c043d53..110e7348e4f 100644 --- a/src/main/org/openscience/cdk/formula/rules/IsotopePatternRule.java +++ b/src/main/org/openscience/cdk/formula/rules/IsotopePatternRule.java @@ -24,16 +24,16 @@ package org.openscience.cdk.formula.rules; import java.io.IOException; -import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import org.openscience.cdk.exception.CDKException; +import org.openscience.cdk.formula.IsotopeContainer; +import org.openscience.cdk.formula.IsotopePattern; import org.openscience.cdk.formula.IsotopePatternGenerator; +import org.openscience.cdk.formula.IsotopePatternManipulator; +import org.openscience.cdk.formula.IsotopePatternSimilarity; import org.openscience.cdk.interfaces.IMolecularFormula; -import org.openscience.cdk.interfaces.IMolecularFormulaSet; import org.openscience.cdk.tools.LoggingTool; -import org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator; /** * This class validate if the Isotope Pattern from a given IMolecularFormula * correspond with other to compare. @@ -54,7 +54,7 @@ * * * @cdk.module formula - * @author miguelrojasch + * @author Miguel Rojas Cherto * @cdk.created 2007-11-20 */ public class IsotopePatternRule implements IRule{ @@ -65,10 +65,11 @@ public class IsotopePatternRule implements IRule{ /** Accuracy on the mass measuring isotope pattern*/ private double toleranceMass = 0.001; - /** Representation of a spectrum */ - private List pattern; + private IsotopePattern pattern; IsotopePatternGenerator isotopeGe; + + private IsotopePatternSimilarity is; /** * Constructor for the IsotopePatternRule object. @@ -77,8 +78,10 @@ public class IsotopePatternRule implements IRule{ * @throws ClassNotFoundException If an error occurs during tom typing */ public IsotopePatternRule() { - isotopeGe = new IsotopePatternGenerator(0.10); - + isotopeGe = new IsotopePatternGenerator(0.01); + is = new IsotopePatternSimilarity(); + is.seTolerance(toleranceMass); + logger = new LoggingTool(this); } @@ -101,8 +104,12 @@ public void setParameters(Object[] params) throws CDKException { if(!(params[1] instanceof Double )) throw new CDKException("The parameter two must be of type Double"); - pattern = (List) params[0]; - toleranceMass = (Double) params[1]; + pattern = new IsotopePattern(); + for(double[] listISO:(List) params[0]){ + pattern.addIsotope(new IsotopeContainer(listISO[0],listISO[1])); + } + + is.seTolerance((Double) params[1]); } /** @@ -132,81 +139,12 @@ public Object[] getParameters() { public double validate(IMolecularFormula formula) throws CDKException { logger.info("Start validation of ",formula); - IMolecularFormulaSet formulaSet = isotopeGe.getIsotopes(formula); - double score = extractScore(formulaSet); - return score; - } - /** - * Extract a score function looking for similarities between isotopes patterns. - * In this algorithm, only the most intensively simulated isotopic peak per nominal - * mass have been considered and used for intensity correlation. - * - * @param molecularFormulaSet The IMolecularFormulaSet with to compare the isotope pattern - * @return The score function value - */ - private double extractScore(IMolecularFormulaSet formulaSet) { - double score = 0.0; - - String stringMF = MolecularFormulaManipulator.getString(formulaSet.getMolecularFormula(0)); - IMolecularFormula molecularFormulaA = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula(stringMF, formulaSet.getBuilder()); - double massA = MolecularFormulaManipulator.getTotalExactMass(molecularFormulaA); - double windowsAccuracy = toleranceMass; - List inteExperimUnit = new ArrayList(); - List intePatternUnit = new ArrayList(); - for(int i = 1 ; i < 5 ; i++){ - // looking highest intensity per nominal mass - double inteH = 0; - double massUnit = massA + i; - - // around predicted - for(IMolecularFormula molecularFormula: formulaSet.molecularFormulas()){ - double massS = MolecularFormulaManipulator.getTotalExactMass(molecularFormula); - if((massUnit-windowsAccuracy < massS)&(massS < massUnit + windowsAccuracy )){ - double occurrence = ((Double)molecularFormula.getProperties().get("occurrence")); - double intensity = MolecularFormulaManipulator.getTotalNaturalAbundance(molecularFormula)*occurrence; - if(intensity > inteH){ - inteH = intensity; - } - } - } - if(inteH != 0){ - intePatternUnit.add(inteH); - inteH = 0; - // around experimental - for(int j = 0; j < pattern.size(); j++){ - double intensity = pattern.get(j)[1]; - double massS = pattern.get(j)[0]; - if((massUnit-windowsAccuracy < massS)&(massS < massUnit + windowsAccuracy )){ - if(intensity > inteH){ - inteH = intensity; - } - } - } - inteExperimUnit.add(inteH); - - } - - } - - double sumN = 0.0; - for(int j = 0 ; j < intePatternUnit.size(); j++) - sumN += intePatternUnit.get(j)*inteExperimUnit.get(j); - - double sumD = 0.0; - for(Iterator it = intePatternUnit.iterator(); it.hasNext();) - sumD += Math.pow(it.next(), 2); - - double normalization = sumN/sumD; - - if(sumN == 0) - return 0.0; - - sumN = 0.0; - for(int j = 0 ; j < intePatternUnit.size(); j++) - sumN += Math.pow(intePatternUnit.get(j)*inteExperimUnit.get(j)/normalization,2); - score = (1-Math.pow(sumN/sumD,0.5)); + + IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(0.1); + IsotopePattern patternIsoPredicted = isotopeGe.getIsotopes(formula); + IsotopePattern patternIsoNormalize = IsotopePatternManipulator.normalize(patternIsoPredicted); - return score; - } + return is.compare(pattern, patternIsoNormalize); + } } diff --git a/src/test/org/openscience/cdk/formula/IsotopeContainerTest.java b/src/test/org/openscience/cdk/formula/IsotopeContainerTest.java new file mode 100644 index 00000000000..399ee222294 --- /dev/null +++ b/src/test/org/openscience/cdk/formula/IsotopeContainerTest.java @@ -0,0 +1,160 @@ +package org.openscience.cdk.formula; + +import org.junit.Assert; +import org.junit.Test; +import org.openscience.cdk.CDKTestCase; +import org.openscience.cdk.interfaces.IChemObjectBuilder; +import org.openscience.cdk.interfaces.IMolecularFormula; +import org.openscience.cdk.nonotify.NoNotificationChemObjectBuilder; + +/** + * Class testing the IsotopeContainer class. + * + * @cdk.module test-formula + */ +public class IsotopeContainerTest extends CDKTestCase{ + + private static IChemObjectBuilder builder = NoNotificationChemObjectBuilder.getInstance(); + + /** + * Constructor for the IsotopeContainerTest object. + * + */ + public IsotopeContainerTest() { + super(); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testIsotopeContainer() { + IsotopeContainer isoC = new IsotopeContainer(); + Assert.assertNotNull(isoC); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testIsotopeContainer_IMolecularFormula_double() { + IMolecularFormula formula = builder.newMolecularFormula(); + double intensity = 130.00; + IsotopeContainer isoC = new IsotopeContainer(formula,intensity); + + Assert.assertNotNull(isoC); + Assert.assertEquals(formula,isoC.getFormula()); + Assert.assertEquals(intensity,isoC.getIntensity(),0.001); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testIsotopeContainer_double_double() { + double mass = 130.00; + double intensity = 500000.00; + IsotopeContainer isoC = new IsotopeContainer(mass,intensity); + + Assert.assertNotNull(isoC); + Assert.assertEquals(mass,isoC.getMass(),0.001); + Assert.assertEquals(intensity,isoC.getIntensity(),0.001); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testSetFormula_IMolecularFormula() { + IsotopeContainer isoC = new IsotopeContainer(); + IMolecularFormula formula = builder.newMolecularFormula(); + isoC.setFormula(formula); + Assert.assertNotNull(isoC); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testSetMass_double() { + IsotopeContainer isoC = new IsotopeContainer(); + isoC.setMass(130.00); + Assert.assertNotNull(isoC); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testSetIntensity_double() { + IsotopeContainer isoC = new IsotopeContainer(); + isoC.setIntensity(5000000.0); + Assert.assertNotNull(isoC); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testGetFormula() { + IsotopeContainer isoC = new IsotopeContainer(); + IMolecularFormula formula = builder.newMolecularFormula(); + isoC.setFormula(formula); + Assert.assertEquals(formula,isoC.getFormula()); + } + + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testGetMass() { + IsotopeContainer isoC = new IsotopeContainer(); + double mass = 130.00; + isoC.setMass(mass); + Assert.assertEquals(mass,isoC.getMass(),0.001); + + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testGetIntensity() { + IsotopeContainer isoC = new IsotopeContainer(); + double intensity = 130.00; + isoC.setIntensity(intensity); + Assert.assertEquals(intensity,isoC.getIntensity(),0.001); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testClone() throws Exception{ + IsotopeContainer isoC = new IsotopeContainer(); + IMolecularFormula formula = builder.newMolecularFormula(); + isoC.setFormula(formula); + double mass = 130.00; + isoC.setMass(mass); + double intensity = 130.00; + isoC.setIntensity(intensity); + + IsotopeContainer clone = (IsotopeContainer) isoC.clone(); + Assert.assertEquals(mass,clone.getMass(),0.001); + Assert.assertEquals(intensity,clone.getIntensity(),0.001); + Assert.assertEquals(formula,clone.getFormula()); + + } + +} diff --git a/src/test/org/openscience/cdk/formula/IsotopePatternGeneratorTest.java b/src/test/org/openscience/cdk/formula/IsotopePatternGeneratorTest.java index 42f975825ba..c0df468b2b4 100644 --- a/src/test/org/openscience/cdk/formula/IsotopePatternGeneratorTest.java +++ b/src/test/org/openscience/cdk/formula/IsotopePatternGeneratorTest.java @@ -23,16 +23,11 @@ */ package org.openscience.cdk.formula; -import java.util.ArrayList; -import java.util.List; - import org.junit.Assert; import org.junit.Test; import org.openscience.cdk.CDKTestCase; -import org.openscience.cdk.exception.CDKException; import org.openscience.cdk.interfaces.IChemObjectBuilder; import org.openscience.cdk.interfaces.IMolecularFormula; -import org.openscience.cdk.interfaces.IMolecularFormulaSet; import org.openscience.cdk.nonotify.NoNotificationChemObjectBuilder; import org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator; @@ -74,21 +69,47 @@ public IsotopePatternGeneratorTest(){ Assert.assertNotNull(isotopeGe); } + + /** + * A unit test for JUnit: + * + * @return Description of the Return Value + */ + @Test + public void testGetIsotopes_IMolecularFormula(){ + IMolecularFormula molFor = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula("C41H79N8O3P1", builder); + IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(.1); + IsotopePattern isos = isotopeGe.getIsotopes(molFor); + Assert.assertEquals(2,isos.getNumberOfIsotopes(),0.001); + } + /** + * A unit test for JUnit: + * + * @return Description of the Return Value + */ + @Test + public void testGetIsotopes_IMolecularFormula_withoutONE(){ + IMolecularFormula molFor = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula("C41H79N8O3P", builder); + IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(.01); + IsotopePattern isos = isotopeGe.getIsotopes(molFor); + Assert.assertEquals(6,isos.getNumberOfIsotopes(),0.001); + } + /** * A unit test for JUnit: Isotopes of the Bromine. * * @return Description of the Return Value */ @Test - public void testGetIsotopes_IMolecularFormula() throws CDKException { + public void testGetIsotopes1(){ IMolecularFormula molFor = new MolecularFormula(); molFor.addIsotope(builder.newIsotope("Br")); molFor.addIsotope(builder.newIsotope("Br")); - IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(10.0); - IMolecularFormulaSet molFormSet = isotopeGe.getIsotopes(molFor); + IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(.1); + IsotopePattern isoPattern = isotopeGe.getIsotopes(molFor); - Assert.assertEquals(3, molFormSet.size()); + Assert.assertEquals(3, isoPattern.getNumberOfIsotopes()); } /** @@ -97,40 +118,27 @@ public void testGetIsotopes_IMolecularFormula() throws CDKException { * @return Description of the Return Value */ @Test - public void testCalculateIsotopesAllBromine() throws CDKException { + public void testCalculateIsotopesAllBromine(){ + // RESULTS ACCORDING PAGE: http://www2.sisweb.com/mstools/isotope.htm + double[] massResults = {157.836669,159.834630,161.832580}; + double[] abundResults = {.512,1.00,.487}; + IMolecularFormula molFor = new MolecularFormula(); molFor.addIsotope(builder.newIsotope("Br")); molFor.addIsotope(builder.newIsotope("Br")); - IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(10.0); - IMolecularFormulaSet molFormSet = isotopeGe.getIsotopes(molFor); + IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(.1); + IsotopePattern isoPattern = isotopeGe.getIsotopes(molFor); - Assert.assertEquals(3, molFormSet.size()); + Assert.assertEquals(3, isoPattern.getNumberOfIsotopes()); - double mm = MolecularFormulaManipulator.getTotalExactMass(molFormSet.getMolecularFormula(0)); - Assert.assertEquals(157.8366742, mm, 0.0000001); - mm = MolecularFormulaManipulator.getTotalExactMass(molFormSet.getMolecularFormula(1)); - Assert.assertEquals(159.8346277, mm, 0.0000001); - mm = MolecularFormulaManipulator.getTotalExactMass(molFormSet.getMolecularFormula(2)); - Assert.assertEquals(161.8325812, mm, 0.0000001); + Assert.assertEquals(massResults[0], isoPattern.getIsotope(0).getMass(), 0.01); + Assert.assertEquals(massResults[1], isoPattern.getIsotope(1).getMass(), 0.01); + Assert.assertEquals(massResults[2], isoPattern.getIsotope(2).getMass(), 0.01); - - double sum = 0.0; - double occurrence = ((Double)molFormSet.getMolecularFormula(0).getProperty("occurrence")); - double ab = MolecularFormulaManipulator.getTotalNaturalAbundance(molFormSet.getMolecularFormula(0)); - sum += ab; - Assert.assertEquals(0.25694761, ab, 0.0000001); - occurrence = ((Double)molFormSet.getMolecularFormula(1).getProperty("occurrence")); - ab = MolecularFormulaManipulator.getTotalNaturalAbundance(molFormSet.getMolecularFormula(1))*occurrence; - sum += ab; - Assert.assertEquals(0.4999047, ab, 0.0000001); - occurrence = ((Double)molFormSet.getMolecularFormula(2).getProperty("occurrence")); - ab = MolecularFormulaManipulator.getTotalNaturalAbundance(molFormSet.getMolecularFormula(2)); - sum += ab; - Assert.assertEquals(0.24314761, ab, 0.0000001); - - - Assert.assertEquals(1.0, sum, 0.0000001); + Assert.assertEquals(abundResults[0], isoPattern.getIsotope(0).getIntensity(), 0.01); + Assert.assertEquals(abundResults[1], isoPattern.getIsotope(1).getIntensity(), 0.01); + Assert.assertEquals(abundResults[2], isoPattern.getIsotope(2).getIntensity(), 0.01); } @@ -140,38 +148,27 @@ public void testCalculateIsotopesAllBromine() throws CDKException { * @return Description of the Return Value */ @Test - public void testCalculateIsotopesIodemethylidyne() throws CDKException { + public void testCalculateIsotopesIodemethylidyne(){ + // RESULTS ACCORDING PAGE: http://www2.sisweb.com/mstools/isotope.htm + double[] massResults = {138.904480,139.907839}; + double[] abundResults = {1.00,.011}; + IMolecularFormula molFor = new MolecularFormula(); molFor.addIsotope(builder.newIsotope("C")); molFor.addIsotope(builder.newIsotope("I")); Assert.assertEquals(2, molFor.getIsotopeCount()); - - IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(0.10); - - IMolecularFormulaSet molFormSet = isotopeGe.getIsotopes(molFor); - - Assert.assertEquals(2, molFormSet.size()); - - double mm = MolecularFormulaManipulator.getTotalExactMass(molFormSet.getMolecularFormula(0)); - Assert.assertEquals(138.904473, mm, 0.0000001); - mm = MolecularFormulaManipulator.getTotalExactMass(molFormSet.getMolecularFormula(1)); - Assert.assertEquals(139.9078278399, mm, 0.0000001); - - double sum = 0.0; - double occurrence = ((Double)molFormSet.getMolecularFormula(0).getProperty("occurrence")); - double ab = MolecularFormulaManipulator.getTotalNaturalAbundance(molFormSet.getMolecularFormula(0)); - sum += ab; - Assert.assertEquals(0.9893, ab, 0.0000001); - occurrence = ((Double)molFormSet.getMolecularFormula(1).getProperty("occurrence")); - ab = MolecularFormulaManipulator.getTotalNaturalAbundance(molFormSet.getMolecularFormula(1))*occurrence; - sum += ab; - Assert.assertEquals(0.01070, ab, 0.0000001); + IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(.01); + IsotopePattern isoPattern = isotopeGe.getIsotopes(molFor); + Assert.assertEquals(2, isoPattern.getNumberOfIsotopes()); - - Assert.assertEquals(1.0, sum, 0.0000001); + Assert.assertEquals(massResults[0], isoPattern.getIsotope(0).getMass(), 0.01); + Assert.assertEquals(massResults[1], isoPattern.getIsotope(1).getMass(), 0.01); + + Assert.assertEquals(abundResults[0], isoPattern.getIsotope(0).getIntensity(), 0.01); + Assert.assertEquals(abundResults[1], isoPattern.getIsotope(1).getIntensity(), 0.01); } /** @@ -180,40 +177,27 @@ public void testCalculateIsotopesIodemethylidyne() throws CDKException { * @return Description of the Return Value */ @Test - public void testCalculateIsotopesnCarbono() throws CDKException { + public void testCalculateIsotopesnCarbono(){ + // RESULTS ACCORDING PAGE: http://www2.sisweb.com/mstools/isotope.htm + double[] massResults = {120.000000,121.003360,122.006709}; + double[] abundResults = {1.00,.108,0.005}; + IMolecularFormula molFor = new MolecularFormula(); molFor.addIsotope(builder.newIsotope("C"),10); - IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(0.10); + IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(0.0010); + IsotopePattern isoPattern = isotopeGe.getIsotopes(molFor); - IMolecularFormulaSet molFormSet = isotopeGe.getIsotopes(molFor); + Assert.assertEquals(3, isoPattern.getNumberOfIsotopes()); - Assert.assertEquals(3, molFormSet.size()); - - double sum = 0.0; - for(int i = 0; i < molFormSet.size();i++){ - double occurrence = ((Double)molFormSet.getMolecularFormula(i).getProperty("occurrence")); - double ab = MolecularFormulaManipulator.getTotalNaturalAbundance(molFormSet.getMolecularFormula(i)); - sum += ab*occurrence; - - } - Assert.assertEquals(1.0, sum, 0.001); + Assert.assertEquals(massResults[0], isoPattern.getIsotope(0).getMass(), 0.01); + Assert.assertEquals(massResults[1], isoPattern.getIsotope(1).getMass(), 0.01); + Assert.assertEquals(massResults[2], isoPattern.getIsotope(2).getMass(), 0.01); - - double mm = MolecularFormulaManipulator.getTotalExactMass(molFormSet.getMolecularFormula(0)); - Assert.assertEquals(120.0, mm, 0.0000001); - mm = MolecularFormulaManipulator.getTotalExactMass(molFormSet.getMolecularFormula(1)); - Assert.assertEquals(121.00335484, mm, 0.0000001); - - double ab = MolecularFormulaManipulator.getTotalNaturalAbundance(molFormSet.getMolecularFormula(0)); - double occurrence = ((Double)molFormSet.getMolecularFormula(0).getProperty("occurrence")); - ab *= occurrence; - Assert.assertEquals(0.898007762480552, ab, 0.0000001); - ab = MolecularFormulaManipulator.getTotalNaturalAbundance(molFormSet.getMolecularFormula(1)); - occurrence = ((Double)molFormSet.getMolecularFormula(1).getProperty("occurrence")); - ab *= occurrence; - Assert.assertEquals(0.09712607963754075, ab, 0.0000001); + Assert.assertEquals(abundResults[0], isoPattern.getIsotope(0).getIntensity(), 0.01); + Assert.assertEquals(abundResults[1], isoPattern.getIsotope(1).getIntensity(), 0.01); + Assert.assertEquals(abundResults[2], isoPattern.getIsotope(2).getIntensity(), 0.01); } @@ -223,139 +207,36 @@ public void testCalculateIsotopesnCarbono() throws CDKException { * @return Description of the Return Value */ @Test - public void testCalculateIsotopesOrthinine() throws CDKException { - IMolecularFormula molFor = new MolecularFormula(); - molFor.addIsotope(builder.newIsotope("C"),5); - molFor.addIsotope(builder.newIsotope("H"),13); - molFor.addIsotope(builder.newIsotope("N"),2); - molFor.addIsotope(builder.newIsotope("O"),2); - - IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(0.10); - - IMolecularFormulaSet molFormSet = isotopeGe.getIsotopes(molFor); - - Assert.assertEquals(5, molFormSet.size()); - - double sum = 0.0; - for(int i = 0; i < molFormSet.size();i++){ - double occurrence = ((Double)molFormSet.getMolecularFormula(i).getProperty("occurrence")); - double ab = MolecularFormulaManipulator.getTotalNaturalAbundance(molFormSet.getMolecularFormula(i)); - sum += ab*occurrence; - } - Assert.assertEquals(1.0, sum, 0.01); - - - double mm = MolecularFormulaManipulator.getTotalExactMass(molFormSet.getMolecularFormula(0)); - Assert.assertEquals(133.0977, mm, 0.01); - mm = MolecularFormulaManipulator.getTotalExactMass(molFormSet.getMolecularFormula(1)); - Assert.assertEquals(134.0947, mm, 0.01); - mm = MolecularFormulaManipulator.getTotalExactMass(molFormSet.getMolecularFormula(2)); - Assert.assertEquals(134.101079, mm, 0.01); - - double ab1 = MolecularFormulaManipulator.getTotalNaturalAbundance(molFormSet.getMolecularFormula(0)); - double occurrence = ((Double)molFormSet.getMolecularFormula(0).getProperty("occurrence")); - ab1 *= occurrence; - Assert.assertEquals(100.00, ab1/ab1*100, 0.01); - double ab2 = MolecularFormulaManipulator.getTotalNaturalAbundance(molFormSet.getMolecularFormula(1)); - occurrence = ((Double)molFormSet.getMolecularFormula(1).getProperty("occurrence")); - ab2 *= occurrence; - Assert.assertEquals(0.7387184, ab2/ab1*100, 0.01); - double ab3 = MolecularFormulaManipulator.getTotalNaturalAbundance(molFormSet.getMolecularFormula(2)); - occurrence = ((Double)molFormSet.getMolecularFormula(2).getProperty("occurrence")); - ab3 *= occurrence; - Assert.assertEquals(5.4078, (ab3/ab1)*100, 0.01); - - } - /** - * A unit test for JUnit. - * - * @return Description of the Return Value - */ - @Test - public void testGetIsotopeDistribution_IMolecularFormula() throws CDKException { + public void testCalculateIsotopesOrthinine(){ + // RESULTS ACCORDING PAGE: http://www2.sisweb.com/mstools/isotope.htm + double[] massResults = {133.097720,134.094750,134.101079,134.103990,135.101959,135.104430}; + double[] abundResults = {1.00,.006,.054,0.002,0.004,0.001}; + IMolecularFormula molFor = new MolecularFormula(); molFor.addIsotope(builder.newIsotope("C"),5); molFor.addIsotope(builder.newIsotope("H"),13); molFor.addIsotope(builder.newIsotope("N"),2); molFor.addIsotope(builder.newIsotope("O"),2); - IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(0.10); - - List dist = isotopeGe.getIsotopeDistribution(molFor); - Assert.assertEquals(5, dist.size()); - - List distTest = new ArrayList(); - distTest.add(100.00); - distTest.add(0.7387184); - distTest.add(5.4078); - - for(int i = 0 ; i < 3; i ++) - Assert.assertEquals(distTest.get(i), dist.get(i), 0.01); - - - } - /** - * A unit test for JUnit. - * - * @return Description of the Return Value - */ - @Test - public void testGetIsotopeDistribution_IMolecularFormulaSet() throws CDKException { - IMolecularFormula molFor = new MolecularFormula(); - molFor.addIsotope(builder.newIsotope("C"),5); - molFor.addIsotope(builder.newIsotope("H"),13); - molFor.addIsotope(builder.newIsotope("N"),2); - molFor.addIsotope(builder.newIsotope("O"),2); - - IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(0.10); - - IMolecularFormulaSet molFormSet = isotopeGe.getIsotopes(molFor); - - Assert.assertEquals(5, molFormSet.size()); - - List dist = isotopeGe.getIsotopeDistribution(molFormSet); - Assert.assertEquals(5, dist.size()); - - List distTest = new ArrayList(); - distTest.add(100.00); - distTest.add(0.7387184); - distTest.add(5.4078); - - for(int i = 0 ; i < 3; i ++) - Assert.assertEquals(distTest.get(i), dist.get(i), 0.01); - - } - - /** - * A unit test for JUnit. - * - * @return Description of the Return Value - */ - @Test - public void testGetMassDistribution_IMolecularFormulaSet() throws CDKException { - IMolecularFormula molFor = new MolecularFormula(); - molFor.addIsotope(builder.newIsotope("C"),5); - molFor.addIsotope(builder.newIsotope("H"),13); - molFor.addIsotope(builder.newIsotope("N"),2); - molFor.addIsotope(builder.newIsotope("O"),2); - - IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(0.10); - - IMolecularFormulaSet molFormSet = isotopeGe.getIsotopes(molFor); - - Assert.assertEquals(5, molFormSet.size()); - - List dist = isotopeGe.getMassDistribution(molFormSet); - Assert.assertEquals(5, dist.size()); - - List distTest = new ArrayList(); - distTest.add(133.0977); - distTest.add(134.0947); - distTest.add(134.101079); - - for(int i = 0 ; i < 3; i ++) - Assert.assertEquals(distTest.get(i), dist.get(i), 0.01); - + IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(0.0010); + IsotopePattern isoPattern = isotopeGe.getIsotopes(molFor); + + Assert.assertEquals(6, isoPattern.getNumberOfIsotopes()); + + Assert.assertEquals(massResults[0], isoPattern.getIsotope(0).getMass(), 0.01); + Assert.assertEquals(massResults[1], isoPattern.getIsotope(1).getMass(), 0.01); + Assert.assertEquals(massResults[2], isoPattern.getIsotope(2).getMass(), 0.01); + Assert.assertEquals(massResults[3], isoPattern.getIsotope(3).getMass(), 0.01); + Assert.assertEquals(massResults[4], isoPattern.getIsotope(4).getMass(), 0.01); + Assert.assertEquals(massResults[5], isoPattern.getIsotope(5).getMass(), 0.01); + + Assert.assertEquals(abundResults[0], isoPattern.getIsotope(0).getIntensity(), 0.01); + Assert.assertEquals(abundResults[1], isoPattern.getIsotope(1).getIntensity(), 0.01); + Assert.assertEquals(abundResults[2], isoPattern.getIsotope(2).getIntensity(), 0.01); + Assert.assertEquals(abundResults[3], isoPattern.getIsotope(3).getIntensity(), 0.01); + Assert.assertEquals(abundResults[4], isoPattern.getIsotope(4).getIntensity(), 0.01); + Assert.assertEquals(abundResults[5], isoPattern.getIsotope(5).getIntensity(), 0.01); + } } diff --git a/src/test/org/openscience/cdk/formula/IsotopePatternManipulatorTest.java b/src/test/org/openscience/cdk/formula/IsotopePatternManipulatorTest.java new file mode 100644 index 00000000000..7face4fd52d --- /dev/null +++ b/src/test/org/openscience/cdk/formula/IsotopePatternManipulatorTest.java @@ -0,0 +1,142 @@ +package org.openscience.cdk.formula; + +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.openscience.cdk.CDKTestCase; + +/** + * Class testing the IsotopePatternManipulator class. + * + * @cdk.module test-formula + */ +public class IsotopePatternManipulatorTest extends CDKTestCase{ + + public IsotopePatternManipulatorTest() { + super(); + } + /** + * Junit test + * + * @throws Exception + */ + @Test + public void testNormalize_IsotopePattern() { + IsotopePattern spExp = new IsotopePattern(); + spExp.setMonoIsotope(new IsotopeContainer(156.07770, 2)); + spExp.addIsotope(new IsotopeContainer(157.08059, 0.0006)); + spExp.addIsotope(new IsotopeContainer(157.07503, 0.0002)); + spExp.addIsotope(new IsotopeContainer(158.08135, 0.004)); + spExp.setCharge(1); + + IsotopePattern isoNorma = IsotopePatternManipulator.normalize(spExp); + List listISO = isoNorma.getIsotopes(); + Assert.assertEquals(1,isoNorma.getMonoIsotope().getIntensity(),0.00001); + Assert.assertEquals(1,listISO.get(0).getIntensity(),0.00001); + Assert.assertEquals(0.0003,listISO.get(1).getIntensity(),0.00001); + Assert.assertEquals(0.0001,listISO.get(2).getIntensity(),0.00001); + Assert.assertEquals(0.002,listISO.get(3).getIntensity(),0.00001); + + Assert.assertEquals(156.07770,isoNorma.getMonoIsotope().getMass(),0.00001); + Assert.assertEquals(156.07770,listISO.get(0).getMass(),0.00001); + Assert.assertEquals(157.08059,listISO.get(1).getMass(),0.00001); + Assert.assertEquals(157.07503,listISO.get(2).getMass(),0.00001); + Assert.assertEquals(158.08135,listISO.get(3).getMass(),0.00001); + + Assert.assertEquals(1,isoNorma.getCharge(),0.00001); + + } + + /** + * Junit test + * + * @throws Exception + */ + @Test + public void testSortByIntensity_IsotopePattern() { + IsotopePattern spExp = new IsotopePattern(); + spExp.setMonoIsotope(new IsotopeContainer(157.07503, 0.0001)); + spExp.addIsotope(new IsotopeContainer(156.07770, 1)); + spExp.addIsotope(new IsotopeContainer(157.08059, 0.0003)); + spExp.addIsotope(new IsotopeContainer(158.08135, 0.002)); + spExp.setCharge(1); + + IsotopePattern isoNorma = IsotopePatternManipulator.sortByIntensity(spExp); + List listISO = isoNorma.getIsotopes(); + Assert.assertEquals(156.07770,isoNorma.getMonoIsotope().getMass(),0.00001); + Assert.assertEquals(156.07770,listISO.get(0).getMass(),0.00001); + Assert.assertEquals(158.08135,listISO.get(1).getMass(),0.00001); + Assert.assertEquals(157.08059,listISO.get(2).getMass(),0.00001); + Assert.assertEquals(157.07503,listISO.get(3).getMass(),0.00001); + + Assert.assertEquals(1,isoNorma.getMonoIsotope().getIntensity(),0.00001); + Assert.assertEquals(1,listISO.get(0).getIntensity(),0.00001); + Assert.assertEquals(0.002,listISO.get(1).getIntensity(),0.00001); + Assert.assertEquals(0.0003,listISO.get(2).getIntensity(),0.001); + Assert.assertEquals(0.0001,listISO.get(3).getIntensity(),0.00001); + + Assert.assertEquals(1,isoNorma.getCharge(),0.00001); + } + /** + * Junit test + * + * @throws Exception + */ + @Test + public void testSortAndNormalizedByIntensity_IsotopePattern() { + IsotopePattern spExp = new IsotopePattern(); + spExp.addIsotope(new IsotopeContainer(157.07503, 0.0002)); + spExp.setMonoIsotope(new IsotopeContainer(156.07770, 2)); + spExp.addIsotope(new IsotopeContainer(158.08135, 0.004)); + spExp.addIsotope(new IsotopeContainer(157.08059, 0.0006)); + spExp.setCharge(1); + + IsotopePattern isoNorma = IsotopePatternManipulator.sortAndNormalizedByIntensity(spExp); + List listISO = isoNorma.getIsotopes(); + Assert.assertEquals(156.07770,isoNorma.getMonoIsotope().getMass(),0.00001); + Assert.assertEquals(156.07770,listISO.get(0).getMass(),0.00001); + Assert.assertEquals(158.08135,listISO.get(1).getMass(),0.00001); + Assert.assertEquals(157.08059,listISO.get(2).getMass(),0.00001); + Assert.assertEquals(157.07503,listISO.get(3).getMass(),0.00001); + + Assert.assertEquals(1,isoNorma.getMonoIsotope().getIntensity(),0.00001); + Assert.assertEquals(1,listISO.get(0).getIntensity(),0.00001); + Assert.assertEquals(0.002,listISO.get(1).getIntensity(),0.00001); + Assert.assertEquals(0.0003,listISO.get(2).getIntensity(),0.00001); + Assert.assertEquals(0.0001,listISO.get(3).getIntensity(),0.00001); + + Assert.assertEquals(1,isoNorma.getCharge(),0.001); + } + + /** + * Junit test + * + * @throws Exception + */ + @Test + public void testSortByMass_IsotopePattern() { + IsotopePattern spExp = new IsotopePattern(); + spExp.addIsotope(new IsotopeContainer(157.07503, 0.0002)); + spExp.setMonoIsotope(new IsotopeContainer(156.07770, 2)); + spExp.addIsotope(new IsotopeContainer(158.08135, 0.004)); + spExp.addIsotope(new IsotopeContainer(157.08059, 0.0006)); + spExp.setCharge(1); + + IsotopePattern isoNorma = IsotopePatternManipulator.sortByMass(spExp); + List listISO = isoNorma.getIsotopes(); + Assert.assertEquals(156.07770,isoNorma.getMonoIsotope().getMass(),0.001); + Assert.assertEquals(156.07770,listISO.get(0).getMass(),0.00001); + Assert.assertEquals(157.07503,listISO.get(1).getMass(),0.00001); + Assert.assertEquals(157.08059,listISO.get(2).getMass(),0.00001); + Assert.assertEquals(158.08135,listISO.get(3).getMass(),0.00001); + + Assert.assertEquals(2,isoNorma.getMonoIsotope().getIntensity(),0.001); + Assert.assertEquals(2,listISO.get(0).getIntensity(),0.001); + Assert.assertEquals(0.0002,listISO.get(1).getIntensity(),0.00001); + Assert.assertEquals(0.0006,listISO.get(2).getIntensity(),0.00001); + Assert.assertEquals(0.004,listISO.get(3).getIntensity(),0.00001); + + Assert.assertEquals(1,isoNorma.getCharge(),0.001); + } +} diff --git a/src/test/org/openscience/cdk/formula/IsotopePatternSimilarityTest.java b/src/test/org/openscience/cdk/formula/IsotopePatternSimilarityTest.java new file mode 100644 index 00000000000..5b554b8ab78 --- /dev/null +++ b/src/test/org/openscience/cdk/formula/IsotopePatternSimilarityTest.java @@ -0,0 +1,115 @@ +package org.openscience.cdk.formula; + +import org.junit.Assert; +import org.junit.Test; +import org.openscience.cdk.CDKTestCase; +import org.openscience.cdk.interfaces.IChemObjectBuilder; +import org.openscience.cdk.interfaces.IMolecularFormula; +import org.openscience.cdk.nonotify.NoNotificationChemObjectBuilder; +import org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator; + +/** + * Class testing the IsotopePatternSimilarity class. + * + * @cdk.module test-formula + */ +public class IsotopePatternSimilarityTest extends CDKTestCase{ + + private final static IChemObjectBuilder builder = NoNotificationChemObjectBuilder.getInstance(); + + public IsotopePatternSimilarityTest() { + super(); + } + + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testIsotopePatternSimilarity() { + IsotopePatternSimilarity is = new IsotopePatternSimilarity(); + Assert.assertNotNull(is); + } + + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testSeTolerance_double() { + IsotopePatternSimilarity is = new IsotopePatternSimilarity(); + is.seTolerance(0.001); + Assert.assertNotNull(is); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testGetTolerance() { + IsotopePatternSimilarity is = new IsotopePatternSimilarity(); + is.seTolerance(0.001); + Assert.assertEquals(0.001,is.getTolerance(),0.000001); + } + /** + * Histidine example + * + * @throws Exception + */ + @Test + public void testCompare_IsotopePattern_IsotopePattern() { + IsotopePatternSimilarity is = new IsotopePatternSimilarity(); + + IsotopePattern spExp = new IsotopePattern(); + spExp.setMonoIsotope(new IsotopeContainer(156.07770, 1)); + spExp.addIsotope(new IsotopeContainer(157.07503, 0.0004)); + spExp.addIsotope(new IsotopeContainer(157.08059, 0.0003)); + spExp.addIsotope(new IsotopeContainer(158.08135, 0.002)); + + IMolecularFormula formula = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula("C6H10N3O2", builder); + IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(0.1); + IsotopePattern patternIsoPredicted = isotopeGe.getIsotopes(formula); + IsotopePattern patternIsoNormalize = IsotopePatternManipulator.normalize(patternIsoPredicted); + double score = is.compare(spExp, patternIsoNormalize); + Assert.assertNotSame(0.0, score); + } + + /** + * Histidine example + * + * @throws Exception + */ + @Test + public void testSelectingMF() { + IsotopePatternSimilarity is = new IsotopePatternSimilarity(); + + IsotopePattern spExp = new IsotopePattern(); + spExp.setCharge(1); + spExp.setMonoIsotope(new IsotopeContainer(156.07770, 1)); + spExp.addIsotope(new IsotopeContainer(157.07503, 0.0101)); + spExp.addIsotope(new IsotopeContainer(157.08059, 0.074)); + spExp.addIsotope(new IsotopeContainer(158.08135, 0.0024)); + + double score = 0; + String mfString = ""; + String[] listMF = {"C4H8N6O","C2H12N4O4","C3H12N2O5","C6H10N3O2","CH10N5O4","C4H14NO5"}; + + for(int i = 0 ; i < listMF.length; i++){ + IMolecularFormula formula = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula(listMF[i], builder); + IsotopePatternGenerator isotopeGe = new IsotopePatternGenerator(0.01); + IsotopePattern patternIsoPredicted = isotopeGe.getIsotopes(formula); + + IsotopePattern patternIsoNormalize = IsotopePatternManipulator.normalize(patternIsoPredicted); + double tempScore = is.compare(spExp, patternIsoNormalize); + if(score < tempScore){ + mfString = MolecularFormulaManipulator.getString(formula); + score = tempScore; + } + } + Assert.assertEquals("C6H10N3O2", mfString); + } + +} diff --git a/src/test/org/openscience/cdk/formula/IsotopePatternTest.java b/src/test/org/openscience/cdk/formula/IsotopePatternTest.java new file mode 100644 index 00000000000..35f4280de64 --- /dev/null +++ b/src/test/org/openscience/cdk/formula/IsotopePatternTest.java @@ -0,0 +1,166 @@ +package org.openscience.cdk.formula; + +import org.junit.Assert; +import org.junit.Test; +import org.openscience.cdk.CDKTestCase; +import org.openscience.cdk.interfaces.IChemObjectBuilder; +import org.openscience.cdk.nonotify.NoNotificationChemObjectBuilder; + +/** + * Class testing the IsotopePattern class. + * + * @cdk.module test-formula + */ +public class IsotopePatternTest extends CDKTestCase{ + + private final static IChemObjectBuilder builder = NoNotificationChemObjectBuilder.getInstance(); + + public IsotopePatternTest() { + super(); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testIsotopePattern() { + IsotopePattern isoP = new IsotopePattern(); + Assert.assertNotNull(isoP); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testSetMonoIsotope_IsotopeContainer() { + IsotopePattern isoP = new IsotopePattern(); + isoP.setMonoIsotope(new IsotopeContainer()); + Assert.assertNotNull(isoP); + + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testAddIsotope_IsotopeContainer() { + IsotopePattern isoP = new IsotopePattern(); + isoP.addIsotope(new IsotopeContainer()); + Assert.assertNotNull(isoP); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testGetMonoIsotope() { + IsotopePattern isoP = new IsotopePattern(); + IsotopeContainer isoC = new IsotopeContainer(); + isoP.setMonoIsotope(isoC); + Assert.assertEquals(isoC,isoP.getMonoIsotope()); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testGetIsotopes() { + + IsotopePattern isoP = new IsotopePattern(); + IsotopeContainer iso1 = new IsotopeContainer(); + isoP.setMonoIsotope(iso1); + IsotopeContainer iso2 = new IsotopeContainer(); + isoP.addIsotope(iso2); + Assert.assertEquals(iso1,isoP.getIsotopes().get(0)); + Assert.assertEquals(iso2,isoP.getIsotopes().get(1)); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testGetIsotope_int() { + + IsotopePattern isoP = new IsotopePattern(); + IsotopeContainer iso1 = new IsotopeContainer(); + isoP.setMonoIsotope(iso1); + IsotopeContainer iso2 = new IsotopeContainer(); + isoP.addIsotope(iso2); + Assert.assertEquals(iso1,isoP.getIsotope(0)); + Assert.assertEquals(iso2,isoP.getIsotope(1)); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testGetNumberOfIsotopes() { + IsotopePattern isoP = new IsotopePattern(); + IsotopeContainer iso1 = new IsotopeContainer(); + isoP.setMonoIsotope(iso1); + IsotopeContainer iso2 = new IsotopeContainer(); + isoP.addIsotope(iso2); + Assert.assertEquals(2,isoP.getNumberOfIsotopes()); + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testSetCharge_double() { + IsotopePattern isoP = new IsotopePattern(); + isoP.setCharge(1.0); + Assert.assertEquals(1.0,isoP.getCharge(),0.000001); + + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testGetCharge() { + IsotopePattern isoP = new IsotopePattern(); + Assert.assertEquals(0,isoP.getCharge(),0.000001); + + } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test + public void testClone() throws Exception { + IsotopePattern spExp = new IsotopePattern(); + spExp.setMonoIsotope(new IsotopeContainer(156.07770, 1)); + spExp.addIsotope(new IsotopeContainer(157.07503, 0.0004)); + spExp.addIsotope(new IsotopeContainer(157.08059, 0.0003)); + spExp.addIsotope(new IsotopeContainer(158.08135, 0.002)); + spExp.setCharge(1); + + IsotopePattern clone = (IsotopePattern) spExp.clone(); + Assert.assertEquals(156.07770,clone.getMonoIsotope().getMass(),0.001); + Assert.assertEquals(156.07770,clone.getIsotopes().get(0).getMass(),0.001); + Assert.assertEquals(157.07503,clone.getIsotopes().get(1).getMass(),0.001); + Assert.assertEquals(157.08059,clone.getIsotopes().get(2).getMass(),0.001); + Assert.assertEquals(158.08135,clone.getIsotopes().get(3).getMass(),0.001); + + Assert.assertEquals(1,clone.getMonoIsotope().getIntensity(),0.001); + Assert.assertEquals(1,clone.getIsotopes().get(0).getIntensity(),0.001); + Assert.assertEquals(0.0004,clone.getIsotopes().get(1).getIntensity(),0.001); + Assert.assertEquals(0.0003,clone.getIsotopes().get(2).getIntensity(),0.001); + Assert.assertEquals(0.002,clone.getIsotopes().get(3).getIntensity(),0.001); + + Assert.assertEquals(1,clone.getCharge(),0.001); + + } + +} diff --git a/src/test/org/openscience/cdk/formula/rules/IsotopePatternRuleTest.java b/src/test/org/openscience/cdk/formula/rules/IsotopePatternRuleTest.java index a94c8b09847..485d7cc8984 100644 --- a/src/test/org/openscience/cdk/formula/rules/IsotopePatternRuleTest.java +++ b/src/test/org/openscience/cdk/formula/rules/IsotopePatternRuleTest.java @@ -122,35 +122,35 @@ public class IsotopePatternRuleTest extends FormulaRuleTest { Assert.assertEquals(0.0, rule.validate(formula),0.0001); } -// /** -// * A unit test suite for JUnit. -// * -// * @return The test suite -// */ -// @Test public void testDefaultValidTrue() throws Exception { -// -// IMolecularFormula formula = new MolecularFormula(); -// formula.addIsotope(ifac.getMajorIsotope("C"),5); -// formula.addIsotope( ifac.getMajorIsotope("H"),13); -// formula.addIsotope( ifac.getMajorIsotope("N"),2); -// formula.addIsotope( ifac.getMajorIsotope("O"),2); -// formula.setCharge(0.0); -// -// -// /** experimental results*/ -// -// List spectrum = new ArrayList(); -// spectrum.add(new double[]{133.0977 ,100.00}); -// spectrum.add(new double[]{134.09475,0.6}); -// spectrum.add(new double[]{134.1010 ,5.4}); -// -// IRule rule = new IsotopePatternRule(); -// Object[] params = new Object[2]; -// params[0] = spectrum; -// params[1] = 0.001; -// rule.setParameters(params); -// -// Assert.assertEquals(0.94930, rule.validate(formula),0.0001); -// } + /** + * A unit test suite for JUnit. + * + * @return The test suite + */ + @Test public void testDefaultValidTrue() throws Exception { + + IMolecularFormula formula = new MolecularFormula(); + formula.addIsotope(ifac.getMajorIsotope("C"),5); + formula.addIsotope( ifac.getMajorIsotope("H"),13); + formula.addIsotope( ifac.getMajorIsotope("N"),2); + formula.addIsotope( ifac.getMajorIsotope("O"),2); + formula.setCharge(0.0); + + + /** experimental results*/ + + List spectrum = new ArrayList(); + spectrum.add(new double[]{133.0977 ,100.00}); + spectrum.add(new double[]{134.09475,0.6}); + spectrum.add(new double[]{134.1010 ,5.4}); + + IRule rule = new IsotopePatternRule(); + Object[] params = new Object[2]; + params[0] = spectrum; + params[1] = 0.001; + rule.setParameters(params); + + Assert.assertEquals(0.94930, rule.validate(formula),0.001); + } }