PageRenderTime 142ms CodeModel.GetById 130ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/rcdkjar/src/org/guha/rcdk/test/MiscTest.java

http://github.com/rajarshi/cdkr
Java | 251 lines | 171 code | 38 blank | 42 comment | 5 complexity | d82f66fad6c6d67fc90d753f72401e67 MD5 | raw file
  1package org.guha.rcdk.test;
  2
  3import junit.framework.Assert;
  4import junit.framework.TestCase;
  5import org.guha.rcdk.util.Misc;
  6import org.openscience.cdk.CDKConstants;
  7import org.openscience.cdk.ChemFile;
  8import org.openscience.cdk.DefaultChemObjectBuilder;
  9import org.openscience.cdk.aromaticity.CDKHueckelAromaticityDetector;
 10import org.openscience.cdk.config.IsotopeFactory;
 11import org.openscience.cdk.config.Isotopes;
 12import org.openscience.cdk.exception.CDKException;
 13import org.openscience.cdk.exception.InvalidSmilesException;
 14import org.openscience.cdk.formula.MassToFormulaTool;
 15import org.openscience.cdk.formula.MolecularFormulaRange;
 16import org.openscience.cdk.formula.rules.ElementRule;
 17import org.openscience.cdk.formula.rules.IRule;
 18import org.openscience.cdk.formula.rules.ToleranceRangeRule;
 19import org.openscience.cdk.interfaces.IAtomContainer;
 20import org.openscience.cdk.interfaces.IAtomContainerSet;
 21import org.openscience.cdk.interfaces.IChemObjectBuilder;
 22import org.openscience.cdk.interfaces.IMolecularFormulaSet;
 23import org.openscience.cdk.io.MDLV2000Reader;
 24import org.openscience.cdk.io.SDFWriter;
 25import org.openscience.cdk.io.SMILESReader;
 26import org.openscience.cdk.silent.SilentChemObjectBuilder;
 27import org.openscience.cdk.smiles.SmilesParser;
 28import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
 29import org.openscience.cdk.tools.manipulator.ChemFileManipulator;
 30
 31import java.io.FileNotFoundException;
 32import java.io.FileReader;
 33import java.io.IOException;
 34import java.io.StringWriter;
 35import java.util.ArrayList;
 36import java.util.List;
 37import java.util.Map;
 38
 39public class MiscTest extends TestCase {
 40    String home = "/Users/guhar/";
 41
 42    public void testRcdkBugReport() throws Exception {
 43
 44        IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
 45        IsotopeFactory ifac = Isotopes.getInstance();;
 46
 47        Double tolerance = 0.0005;
 48        MassToFormulaTool mfTool = new MassToFormulaTool(builder);
 49        List<IRule> rules = new ArrayList<IRule>();
 50
 51        ToleranceRangeRule trule = new ToleranceRangeRule();
 52        trule.setParameters(new Object[]{tolerance, tolerance});
 53        rules.add(trule);
 54
 55        ElementRule erule = new ElementRule();
 56        Object[] params = new Object[1];
 57        MolecularFormulaRange mfRange = new MolecularFormulaRange();
 58        mfRange.addIsotope(ifac.getMajorIsotope("C"), 0, 20);
 59        mfRange.addIsotope(ifac.getMajorIsotope("H"), 0, 24);
 60        mfRange.addIsotope(ifac.getMajorIsotope("O"), 0, 4);
 61        mfRange.addIsotope(ifac.getMajorIsotope("N"), 0, 1);
 62        params[0] = mfRange;
 63        erule.setParameters(params);
 64        rules.add(erule);
 65
 66        mfTool.setRestrictions(rules);
 67        IMolecularFormulaSet mfset = mfTool.generate(55.05423);
 68        System.out.println("mfset.size() = " + mfset.size());
 69
 70
 71    }
 72
 73    public void testLoadMolecules() {
 74        String[] fname = {home + "src/cdkr/rcdk/data/dan001.hin",
 75                home + "src/cdkr/rcdk/data/dan007.xyz",
 76                home + "src/cdkr/rcdk/data/dan008.hin"};
 77        IAtomContainer[] acs = null;
 78        try {
 79            acs = Misc.loadMolecules(fname, true, true, true);
 80        } catch (CDKException e) {
 81            e.printStackTrace();
 82        } catch (IOException e) {
 83            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
 84        }
 85
 86        Assert.assertEquals(3, acs.length);
 87
 88    }
 89
 90    public void testTotalCharge() throws InvalidSmilesException {
 91        String smi = "[H]C(COC(=O)CCCCCCCCCCC)(COP(O)(=O)OCC[N+](C)(C)C)OC(=O)CCCCCCCCCCCC";
 92        SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
 93        IAtomContainer mol = sp.parseSmiles(smi);
 94        double tc = AtomContainerManipulator.getTotalCharge(mol);
 95        System.out.println("tc = " + tc);
 96    }
 97
 98    public void testLoadMolsFromSmi() {
 99        IAtomContainer[] acs = null;
100        try {
101            acs = Misc.loadMolecules(new String[]{"/Users/rguha/src/R/trunk/rcdk/data/big.smi"}, true, true, true);
102        } catch (CDKException e) {
103            e.printStackTrace();
104        } catch (IOException e) {
105            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
106        }
107
108        assert acs != null;
109        Assert.assertEquals(277, acs.length);
110
111    }
112
113    public void testLoadMolsFromSmi2() {
114        IAtomContainer[] acs = null;
115        try {
116            acs = Misc.loadMolecules(new String[]{"/Users/guhar/Downloads/mcriobs.smi"}, true, true, true);
117        } catch (CDKException e) {
118            e.printStackTrace();
119        } catch (IOException e) {
120            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
121        }
122
123        assert acs != null;
124        Assert.assertEquals(2000, acs.length);
125
126    }
127
128    public void testWriteMoleculesDirectly() throws InvalidSmilesException, CDKException, IOException {
129        SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
130        IAtomContainer mol = sp.parseSmiles("CCCCCCC");
131        mol.setProperty(CDKConstants.TITLE, "FooMolecule");
132        mol.setProperty("foo", "bar");
133
134
135        StringWriter sw = new StringWriter();
136        SDFWriter writer = new SDFWriter(sw);
137        writer.write(mol);
138        writer.close();
139        Assert.assertNotNull(sw.toString());
140        Assert.assertFalse(sw.toString().equals(""));
141        Assert.assertTrue(sw.toString().indexOf("FooMolecule") == 0);
142        Assert.assertTrue(sw.toString().indexOf("<foo>") > 0);
143    }
144
145    public void testWriteMolecules() throws Exception {
146        SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
147        IAtomContainer mol = sp.parseSmiles("CCCCCCC");
148        mol.setProperty(CDKConstants.TITLE, "FooMolecule");
149        mol.setProperty("foo", "bar");
150
151        Misc.writeMoleculesInOneFile(new IAtomContainer[]{mol}, "/Users/rguha/foo.sdf", 0);
152    }
153
154    public void testWriteMoleculesWithAromaticity() throws Exception {
155        SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
156        IAtomContainer mol = sp.parseSmiles("c1ccccc1CC");
157        CDKHueckelAromaticityDetector.detectAromaticity(mol);
158        mol.setProperty(CDKConstants.TITLE, "FooMolecule");
159        mol.setProperty("foo", "bar");
160
161        Misc.writeMoleculesInOneFile(new IAtomContainer[]{mol}, "/Users/guhar/foo.sdf", 0);
162    }
163
164//    public void testjunk() throws FileNotFoundException, CDKException, CloneNotSupportedException {
165//        ISimpleChemObjectReader reader = new MDLV2000Reader(new FileReader("/Users/rguha/tmp/frog.sdf"));
166//        IChemFile content = (IChemFile) reader.read(DefaultChemObjectBuilder.getInstance().newChemFile();
167//        List<IAtomContainer> c = ChemFileManipulator.getAllAtomContainers(content);
168//
169//        IAtomContainer m1 = c.get(0);
170//        IAtomContainer m2 = c.get(1);
171//
172//        m1 = AtomContainerManipulator.removeHydrogens(m1);
173//        m2 = AtomContainerManipulator.removeHydrogens(m2);
174//
175//        List<IAtomContainer> maps = UniversalIsomorphismTester.getOverlaps(m1, m2);
176//        System.out.println("maps.size() = " + maps.size());
177//        IAtomContainer mcss = null;
178//        int natom = -1;
179//        for (IAtomContainer map : maps) {
180//            if (map.getAtomCount() > natom) {
181//                natom = map.getAtomCount();
182//                mcss = (IAtomContainer) map.clone();
183//            }
184//        }
185//        System.out.println("mcss.getAtomCount() = " + mcss.getAtomCount());
186////        KabschAlignment ka = new KabschAlignment();
187//    }
188
189//    public IAtomContainer getneedle(IAtomContainer a, IAtomContainer q) throws CDKException {
190////        IAtomContainer needle = DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainer.class);
191//        IAtomContainer needle = DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainer.class);
192//        Vector idlist = new Vector();
193//
194//        List l = UniversalIsomorphismTester.getSubgraphMaps(a, q);
195//        List maplist = (List) l.get(0);
196//        for (Iterator i = maplist.iterator(); i.hasNext(); ) {
197//            RMap rmap = (RMap) i.next();
198//            idlist.add(new Integer(rmap.getId1()));
199//        }
200//
201//        HashSet hs = new HashSet(idlist);
202//        for (Iterator i = hs.iterator(); i.hasNext(); ) {
203//            needle.addBond(a.getBond(((Integer) i.next()).intValue()));
204//        }
205//        return needle;
206//    }
207
208    public void testExactMass() throws InvalidSmilesException {
209        SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
210        IAtomContainer mol = sp.parseSmiles("CCCCCCC");
211        double d = AtomContainerManipulator.getTotalExactMass(mol);
212        System.out.println("d = " + d);
213    }
214
215    public void testGetProps() throws FileNotFoundException, CDKException {
216        String[] fname = {home + "src/cdkr/data/kegg.sdf"};
217        IAtomContainer[] acs = null;
218        try {
219            acs = Misc.loadMolecules(fname, true, true, true);
220        } catch (CDKException e) {
221            e.printStackTrace();
222        } catch (IOException e) {
223            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
224        }
225
226        Assert.assertEquals(10, acs.length);
227
228        MDLV2000Reader reader = new MDLV2000Reader(new FileReader(fname[0]));
229        ChemFile o = reader.read(DefaultChemObjectBuilder.getInstance().newInstance(ChemFile.class));
230        List<IAtomContainer> mols = ChemFileManipulator.getAllAtomContainers(o);
231        Map<Object, Object> props = mols.get(0).getProperties();
232        for (Map.Entry entry : props.entrySet()) {
233            System.out.println(entry.getKey() + " " + entry.getValue());
234        }
235    }
236
237    public void testSmilesReader() throws FileNotFoundException, CDKException {
238        SMILESReader reader = new SMILESReader(new FileReader("/Users/guhar/src/cdkr/data/nonkekulizable.smi"));
239        IAtomContainerSet mols = reader.read(DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainerSet.class));
240        Assert.assertNotNull(mols);
241        Assert.assertTrue(mols.getAtomContainerCount() == 1);
242    }
243
244    public void testfoo() throws CDKException {
245        IChemObjectBuilder dcob = DefaultChemObjectBuilder.getInstance();
246        SmilesParser sp = new SmilesParser(dcob);
247        sp.parseSmiles("CCCC");
248    }
249
250
251}