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