Permalink
Browse files

Commited a tool for calculating Morgan Numbers for a molecule.

Morgan numbers can be the basis for canonical naming, calculating
automorphism partitionings and thus symmetry properties of a molecule.
The current implementation is not tested (Although I created a test class
already, because I have to go now :-)) I'll test it soon.
 * Morgan, H. L.
 * The Generation of a Unique Machine Description for Chemical Structures -
 * A Technique Developed at Chemical Abstracts Service.
 * J. Chem. Doc. 1965, 5, 107-113.


git-svn-id: https://cdk.svn.sourceforge.net/svnroot/cdk/trunk/cdk@182 eb4e18e3-b210-0410-a6ab-dec725e4b171
  • Loading branch information...
1 parent fd6272c commit da34b9e43579ca61b7d1fced3588a1a4205d4f0c steinbeck committed Jun 26, 2001
View
5 org/openscience/cdk/test/CDKTests.java
@@ -41,8 +41,9 @@ public static Test suite ( )
TestSuite suite= new TestSuite("All CDK Tests");
suite.addTest(RingSearchTest.suite());
suite.addTest(ConnectivityCheckerTest.suite());
- suite.addTest(MFAnalyserTest.suite());
- suite.addTest(PathLengthTest.suite());
+ suite.addTest(MorganNumberToolsTest.suite());
+ //suite.addTest(MFAnalyserTest.suite());
+ //suite.addTest(PathLengthTest.suite());
//suite.addTest(StructureDiagramGeneratorTest.suite());
//suite.addTest(RandomStructureGeneratorTest.suite());
View
84 org/openscience/cdk/test/MorganNumberToolsTest.java
@@ -0,0 +1,84 @@
+/*
+ *
+ * $RCSfile$ $Author$ $Date$ $Revision$
+ *
+ * Copyright (C) 1997-2001 The Chemistry Development Kit (CDK) project
+ *
+ * Contact: steinbeck@ice.mpg.de, gezelter@maul.chem.nd.edu, egonw@sci.kun.nl
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+package org.openscience.cdk.test;
+
+import org.openscience.cdk.*;
+import org.openscience.cdk.tools.*;
+import java.util.*;
+import junit.framework.*;
+
+/**
+ * Checks the funcitonality of the MorganNumberTools
+ */
+
+public class MorganNumberToolsTest extends TestCase
+{
+ int[] reference = {0,0,0,0,0,0,0,0,0,0};
+ public MorganNumberToolsTest(String name)
+ {
+ super(name);
+ }
+
+ public void setUp()
+ {
+
+ }
+
+ public static Test suite() {
+ return new TestSuite(MorganNumberToolsTest.class);
+ }
+
+ public void testMorganNumbers()
+ {
+ Molecule mol = MoleculeFactory.makeAlphaPinene();
+ int[] morganNumbers = null;
+ try
+ {
+ morganNumbers = MorganNumbersTools.getMorganNumbers(mol);
+ }
+ catch(Exception exc)
+ {
+ fail();
+ }
+ assert(arraysAreEqual(morganNumbers, reference));
+ }
+
+ protected boolean arraysAreEqual(int[] array1, int[] array2)
+ {
+ if (array1.length != array2.length)
+ {
+ return false;
+ }
+ for (int f = 0; f < array1.length; f ++)
+ {
+ System.out.println(array1[f]);
+ if (array1[f] != array2[f])
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+}
View
26 org/openscience/cdk/tools/HOSECodeGenerator.java
@@ -61,7 +61,7 @@
protected int maxSphere = 0;
protected StringBuffer HOSECode = null;
- protected Molecule molecule;
+ protected AtomContainer atomContainer;
protected String[] sphereDelimiters =
@@ -81,13 +81,13 @@ public HOSECodeGenerator()
HOSECode = new StringBuffer();
}
- public String getHOSECode(Molecule molecule, Atom root, int noOfSpheres)
+ public String getHOSECode(AtomContainer ac, Atom root, int noOfSpheres)
{
- this.molecule = molecule;
+ this.atomContainer = ac;
maxSphere = noOfSpheres;
- for (int i = 0; i < molecule.getAtomCount(); i++)
+ for (int i = 0; i < ac.getAtomCount(); i++)
{
- molecule.getAtomAt(i).flags[VISITED] = false;
+ ac.getAtomAt(i).flags[VISITED] = false;
}
root.flags[VISITED] = true;
@@ -96,7 +96,7 @@ public String getHOSECode(Molecule molecule, Atom root, int noOfSpheres)
ranking of the first sphere, sinces the order of a node in a sphere
depends on the order the preceding node in its branch */
//makeRingList();
- ranking = new int[molecule.getDegree(root)];
+ ranking = new int[atomContainer.getDegree(root)];
HOSECode = new StringBuffer();
breadthFirstSearch(root);
fillUpSphereDelimiters();
@@ -112,7 +112,7 @@ public String getHOSECode(Molecule molecule, Atom root, int noOfSpheres)
private void breadthFirstSearch(Atom root)
{
sphere = 0;
- Atom[] conAtoms = molecule.getConnectedAtoms(root);
+ Atom[] conAtoms = atomContainer.getConnectedAtoms(root);
Atom atom;
Bond bond = null;
sphereNodes.removeAllElements();
@@ -121,7 +121,7 @@ private void breadthFirstSearch(Atom root)
atom = conAtoms[i];
try
{
- bond = molecule.getBond(root, atom);
+ bond = atomContainer.getBond(root, atom);
}
catch (Exception exc)
{
@@ -156,7 +156,7 @@ private void nextSphere(Vector sphereNodes)
if (!("&;#:,".indexOf(treeNode.symbol)>=0))
{
node = treeNode.number;
- conAtoms = molecule.getConnectedAtoms(node);
+ conAtoms = atomContainer.getConnectedAtoms(node);
if (conAtoms.length == 1)
{
nextSphereNodes.addElement(new TreeNode(",", node, null, 0, treeNode.score * 1000000));
@@ -167,17 +167,17 @@ private void nextSphere(Vector sphereNodes)
{
try
{
- if (debug) System.out.println("HOSECodeGenerator->nextSphere()->node.number:" + molecule.getAtomNumber(node));
+ if (debug) System.out.println("HOSECodeGenerator->nextSphere()->node.number:" + atomContainer.getAtomNumber(node));
toNode = conAtoms[j];
- if (debug) System.out.println("HOSECodeGenerator->nextSphere()->toNode.number:" + molecule.getAtomNumber(toNode));
+ if (debug) System.out.println("HOSECodeGenerator->nextSphere()->toNode.number:" + atomContainer.getAtomNumber(toNode));
if (!toNode.flags[VISITED])
{
- nextSphereNodes.addElement(new TreeNode(toNode.getElement().getSymbol(), node, toNode, molecule.getBond(node, toNode).getOrder(), treeNode.score * 1000000));
+ nextSphereNodes.addElement(new TreeNode(toNode.getElement().getSymbol(), node, toNode, atomContainer.getBond(node, toNode).getOrder(), treeNode.score * 1000000));
toNode.flags[VISITED] = true;
}
else if (!toNode.equals(treeNode.source))
{
- nextSphereNodes.addElement(new TreeNode("&", node, toNode, molecule.getBond(node, toNode).getOrder(), treeNode.score * 1000000));
+ nextSphereNodes.addElement(new TreeNode("&", node, toNode, atomContainer.getBond(node, toNode).getOrder(), treeNode.score * 1000000));
}
}
catch (Exception exc)
View
5 org/openscience/cdk/tools/MFAnalyser.java
@@ -31,10 +31,9 @@
import org.openscience.cdk.*;
import java.util.Vector;
-/** MFAnalyser.java
+/**
* Analyses a molecular formula given in String format and builds
- * set of de.mpg.ice.compchem.Node's reflecting the elements as given by
- * the molecular formula.
+ * an AtomContainer with the Atoms in the molecular formula.
*/
public class MFAnalyser{
View
69 org/openscience/cdk/tools/MorganNumbersTools.java
@@ -0,0 +1,69 @@
+/*
+ *
+ * $RCSfile$ $Author$ $Date$ $Revision$
+ *
+ * Copyright (C) 1997-2001 The Chemistry Development Kit (CDK) project
+ *
+ * Contact: steinbeck@ice.mpg.de, gezelter@maul.chem.nd.edu, egonw@sci.kun.nl
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+package org.openscience.cdk.tools;
+
+import org.openscience.cdk.*;
+import org.openscience.cdk.exception.*;
+import java.util.*;
+
+/**
+ * Tool for calculating Morgan Numbers:
+ * Morgan, H. L.
+ * The Generation of a Unique Machine Description for Chemical Structures -
+ * A Technique Developed at Chemical Abstracts Service.
+ * J. Chem. Doc. 1965, 5, 107-113.
+ */
+
+public class MorganNumbersTools implements CDKConstants
+{
+
+ public static int[] getMorganNumbers(Molecule mol) throws NoSuchAtomException
+ {
+ int [] morganMatrix, tempMorganMatrix;
+ int N = mol.getAtomCount();
+ morganMatrix = new int[N];
+ tempMorganMatrix = new int[N];
+ Atom[] atoms = null;
+ for (int f = 0; f < N; f++)
+ {
+ morganMatrix[f] = mol.getDegree(f);
+ tempMorganMatrix[f] = mol.getDegree(f);
+ }
+ for (int e = 0; e < N; e++)
+ {
+ for (int f = 0; f < N; f++)
+ {
+ morganMatrix[f] = 0;
+ atoms = mol.getConnectedAtoms(mol.getAtomAt(f));
+ for (int g = 0; g < atoms.length; g ++)
+ {
+ morganMatrix[f] += tempMorganMatrix[mol.getAtomNumber(atoms[g])];
+ }
+ }
+ System.arraycopy(morganMatrix, 0, tempMorganMatrix, 0, N);
+ }
+ return tempMorganMatrix;
+ }
+}
View
167 org/openscience/cdk/tools/SwissArmyKnife.java
@@ -1,24 +1,25 @@
-/* SwissArmyKnife.java
+/*
+ * SwissArmyKnife.java
*
* $RCSfile$ $Author$ $Date$ $Revision$
- *
+ *
* Copyright (C) 1997-2001 The Chemistry Development Kit (CDK) project
- *
+ *
* Contact: steinbeck@ice.mpg.de, geelter@maul.chem.nd.edu, egonw@sci.kun.nl
- *
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.openscience.cdk.tools;
@@ -27,117 +28,127 @@
import java.io.*;
import org.openscience.cdk.*;
-
-/**
- * A set of utilities which did not really fit into any other category
+/**
+ * A set of utilities which did not really fit into any other category
+ *
+ * @author steinbeck
+ * @created June 19, 2001
*/
-public class SwissArmyKnife
-{
+public class SwissArmyKnife {
- public static boolean debug = false; // minimum details
+ /**
+ * Description of the Field
+ */
+ public static boolean debug = false;
+ // minimum details
+ /**
+ * Description of the Field
+ */
public static int INFINITY = 1000000;
/**
- * Returns a string representation of a 2D int matrix
- * for printing or listing to the console
+ * Returns a string reporting the time passed between startTime and endTime, both given in milliseconds, in hours, minutes, seconds and milliseconds
*
- * @param contab The 2D int matrix for which a string representation is to be generatred
+ * @param startTime The start time in milliseconds
+ * @param endTime The end time in milliseconds
+ * @return A human readable representation of a timespan given in milliseconds
*/
- public static String printInt2D(int[][] contab)
- {
- String line = "";
- for (int f = 0; f < contab.length; f++)
- {
- for (int g = 0; g < contab.length; g++)
- {
- line += contab[f][g] + " ";
- }
- line += "\n";
- }
- return line;
+ public static String getDuration(long startTime, long endTime) {
+ long diff = endTime - startTime;
+ return getDuration(diff);
}
-
/**
- * Calculates the faculty for a given integer
+ * Returns a String reporting the time passed during a given number of milliseconds.
*
- * @param i The int value for which the faculty is to be returned
- * @return The faculty of i
+ * @param diff A time span in milliseconds
+ * @return A human readable representation of a timespan given in milliseconds
*/
- public static int faculty(int i)
- {
- if (i > 1) return i * faculty(i - 1);
- return 1;
+ public static String getDuration(long diff) {
+ GregorianCalendar calendar = new GregorianCalendar();
+ calendar.setTime(new Date(diff));
+ StringBuffer s = new StringBuffer();
+ if (calendar.get(Calendar.HOUR) > 1) {
+ s.append("hours: " + (calendar.get(Calendar.HOUR) - 1) + ", ");
+ }
+ if (calendar.get(Calendar.MINUTE) > 0) {
+ s.append("minutes: " + (calendar.get(Calendar.MINUTE)) + ", ");
+ }
+ if (calendar.get(Calendar.SECOND) > 0) {
+ s.append("seconds: " + (calendar.get(Calendar.SECOND)) + ", ");
+ }
+ if (calendar.get(Calendar.MILLISECOND) > 1) {
+ s.append("milliseconds: " + (calendar.get(Calendar.MILLISECOND)) + ", ");
+ }
+ s.append("total milliseconds: " + diff);
+ return s.toString();
}
-
+
+
+
/**
- * Returns a string reporting the time passed between startTime and endTime,
- * both given in milliseconds, in hours, minutes, seconds and milliseconds
+ * Returns a string representation of a 2D int matrix for printing or listing to the console
*
- * @param startTime The start time in milliseconds
- * @param endTime The end time in milliseconds
- * @return A human readable representation of a timespan given in milliseconds
+ * @param contab The 2D int matrix for which a string representation is to be generatred
+ * @return Description of the Returned Value
*/
- public static String getDuration(long startTime, long endTime)
- {
- long diff = endTime - startTime;
- return getDuration(diff);
+ public static String printInt2D(int[][] contab) {
+ String line = "";
+ for (int f = 0; f < contab.length; f++) {
+ for (int g = 0; g < contab.length; g++) {
+ line += contab[f][g] + " ";
+ }
+ line += "\n";
+ }
+ return line;
}
-
+
+
/**
- * Returns a String reporting the time passed during a given number of milliseconds.
+ * Calculates the faculty for a given integer
*
- * @param diff A time span in milliseconds
- * @return A human readable representation of a timespan given in milliseconds
+ * @param i The int value for which the faculty is to be returned
+ * @return The faculty of i
*/
- public static String getDuration(long diff)
- {
- GregorianCalendar calendar = new GregorianCalendar();
- calendar.setTime(new Date(diff));
- StringBuffer s = new StringBuffer();
- if (calendar.get(Calendar.HOUR) > 1) s.append("hours: " + (calendar.get(Calendar.HOUR) - 1) + ", ");
- if (calendar.get(Calendar.MINUTE) > 0) s.append("minutes: " + (calendar.get(Calendar.MINUTE)) + ", ");
- if (calendar.get(Calendar.SECOND) > 0) s.append("seconds: " + (calendar.get(Calendar.SECOND)) + ", ");
- if (calendar.get(Calendar.MILLISECOND) > 1) s.append("milliseconds: " + (calendar.get(Calendar.MILLISECOND)) + ", ");
- s.append("total milliseconds: " + diff);
- return s.toString();
+ public static int faculty(int i) {
+ if (i > 1) {
+ return i * faculty(i - 1);
+ }
+ return 1;
}
+
/**
- * Gets a Molecule and an array of element symbols. Counts how many of each of these
- * elements the molecule contains. Than it returns the elements followed by their
- * number as a string, i.e. C15H8N3.
+ * Gets a Molecule and an array of element symbols. Counts how many of each of these elements the molecule contains. Than it returns the elements followed by their number as a string, i.e. C15H8N3.
*
- * @param mol The Molecule to be searched
- * @param element The array of element symbols
- * @return The element formula as a string
+ * @param mol The Molecule to be searched
+ * @param elements Description of Parameter
+ * @return The element formula as a string
*/
- public static String generateElementFormula(Molecule mol, String[] elements)
- {
+ public static String generateElementFormula(Molecule mol, String[] elements) {
int num = elements.length;
StringBuffer formula = new StringBuffer();
int[] elementCount = new int[num];
- for (int i = 0; i < mol.getAtomCount(); i++)
- {
+ for (int i = 0; i < mol.getAtomCount(); i++) {
String symbol = mol.getAtomAt(i).getElement().getSymbol();
- for (int j = 0; j < num; j++)
- {
- if (elements[j].equals(mol.getAtomAt(i).getElement().getSymbol()))
- {
- elementCount[j] ++;
+ for (int j = 0; j < num; j++) {
+ if (elements[j].equals(mol.getAtomAt(i).getElement().getSymbol())) {
+ elementCount[j]++;
}
}
}
- for (int i = 0; i < num; i++)
- {
+ for (int i = 0; i < num; i++) {
formula.append(elements[i] + elementCount[i]);
}
return formula.toString();
}
+
+
+
}

0 comments on commit da34b9e

Please sign in to comment.