Skip to content

Commit

Permalink
Updated molecular formula to use the new equals method from Isotope. …
Browse files Browse the repository at this point in the history
…This methods checks for equality of two instances using symbol, mass num, exact mass and natural abundance
  • Loading branch information
rajarshi authored and egonw committed Oct 17, 2010
1 parent 4945c3f commit e280bc2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 81 deletions.
70 changes: 13 additions & 57 deletions src/main/org/openscience/cdk/formula/MolecularFormula.java
Expand Up @@ -23,20 +23,18 @@
*/
package org.openscience.cdk.formula;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IIsotope;
import org.openscience.cdk.interfaces.IMolecularFormula;
import org.openscience.cdk.interfaces.IChemObjectBuilder;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
/**
* Class defining a molecular formula object. It maintains
* a list of list {@link IIsotope}.
Expand Down Expand Up @@ -120,20 +118,11 @@ public IMolecularFormula addIsotope(IIsotope isotope) {
*/
@TestMethod("testAddIsotope_IIsotope_int")
public IMolecularFormula addIsotope(IIsotope isotope, int count) {
boolean flag = false;
for (IIsotope thisIsotope : isotopes.keySet()) {
System.out.println("thisIsotope = " + thisIsotope);
if(isTheSame(isotope, thisIsotope)){
isotopes.put(thisIsotope, isotopes.get(thisIsotope) + count);
flag = true;
break;
}
}
if(!flag){
System.out.println("Saw "+isotope.getSymbol()+" for first time with count = "+count);
isotopes.put(isotope, count);
}

if (isotopes.containsKey(isotope)) {
isotopes.put(isotope, isotopes.get(isotope) + count);
} else {
isotopes.put(isotope, count);
}
return this;
}

Expand All @@ -147,12 +136,7 @@ public IMolecularFormula addIsotope(IIsotope isotope, int count) {
*/
@TestMethod("testContains_IIsotope")
public boolean contains(IIsotope isotope) {
for (IIsotope thisIsotope : isotopes()) {
if(isTheSame(thisIsotope, isotope)){
return true;
}
}
return false;
return isotopes.containsKey(isotope);
}

/**
Expand Down Expand Up @@ -204,7 +188,7 @@ public int getIsotopeCount() {
*/
private IIsotope getIsotope(IIsotope isotope){
for (IIsotope thisIsotope : isotopes()) {
if(isTheSame(isotope,thisIsotope))
if(isotope.equals(thisIsotope))
return thisIsotope;
}
return null;
Expand Down Expand Up @@ -371,34 +355,6 @@ public void setProperties(Map<Object, Object> properties){
lazyProperties().put(key, properties.get(key));
}
}
/**
* Compare to IIsotope. The method doesn't compare instance but if they
* have the same symbol, natural abundance and exact mass.
*
* @param isotopeOne The first Isotope to compare
* @param isotopeTwo The second Isotope to compare
* @return True, if both isotope are the same
*/
@TestMethod("testIsTheSame")
protected boolean isTheSame(IIsotope isotopeOne, IIsotope isotopeTwo) {

Double natAbund1 = isotopeOne.getNaturalAbundance();
Double natAbund2 = isotopeTwo.getNaturalAbundance();

Double exactMass1 = isotopeOne.getExactMass();
Double exactMass2 = isotopeTwo.getExactMass();

if (natAbund1 == null) natAbund1 = -1.0;
if (natAbund2 == null) natAbund2 = -1.0;
if (exactMass1 == null) exactMass1 = -1.0;
if (exactMass2 == null) exactMass2 = -1.0;

if(!isotopeOne.getSymbol().equals(isotopeTwo.getSymbol() ))
return false;
if(natAbund1.doubleValue() != natAbund2.doubleValue())
return false;
return exactMass1.doubleValue() == exactMass2.doubleValue();
}

public IChemObjectBuilder getBuilder() {
return DefaultChemObjectBuilder.getInstance();
Expand Down
25 changes: 1 addition & 24 deletions src/test/org/openscience/cdk/formula/MolecularFormulaTest.java
Expand Up @@ -24,14 +24,11 @@
*/
package org.openscience.cdk.formula;

import java.io.IOException;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.interfaces.AbstractMolecularFormulaTest;
import org.openscience.cdk.interfaces.IIsotope;
import org.openscience.cdk.interfaces.IMolecularFormula;

/**
Expand All @@ -57,25 +54,5 @@ public void testMolecularFormula() {

IMolecularFormula mf = getBuilder().newInstance(IMolecularFormula.class);
Assert.assertNotNull(mf);
}

@Test
public void testIsTheSame_IIsotope_IIsotope() throws IOException {
MolecularFormula mf = new MolecularFormula();
IIsotope carb = getBuilder().newInstance(IIsotope.class,"C");
IIsotope anotherCarb = getBuilder().newInstance(IIsotope.class,"C");
IIsotope h = getBuilder().newInstance(IIsotope.class,"H");

carb.setExactMass(12.0);
anotherCarb.setExactMass(12.0);
h.setExactMass(1.0);

carb.setNaturalAbundance(34.0);
anotherCarb.setNaturalAbundance(34.0);
h.setNaturalAbundance(99.0);

Assert.assertTrue(mf.isTheSame(carb, carb));
Assert.assertTrue(mf.isTheSame(carb, anotherCarb));
Assert.assertFalse(mf.isTheSame(carb, h));
}
}
}

0 comments on commit e280bc2

Please sign in to comment.