Commits

Sam Adams committed 8668c16

Refactored inchi generation

  • Participants
  • Parent commits b2e353b

Comments (0)

Files changed (2)

chemistry-importer/src/main/java/net/chempound/chemistry/CmlImporter.java

     protected LocalResource imageResource;
     protected LocalResource thumbnailResource;
 
-
     protected static LocalResource createCmlResource(final Element cml, final String path) throws IOException {
         final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         final Serializer ser = new Serializer(buffer);

chemistry-importer/src/main/java/net/chempound/chemistry/InChIGenerator.java

+package net.chempound.chemistry;
+
+import net.chempound.chemistry.inchi.InchiTool;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.xmlcml.cml.element.CMLIdentifier;
+import org.xmlcml.cml.element.CMLMolecule;
+
+/**
+ * @author Sam Adams
+ */
+public class InChIGenerator {
+
+    private static final Logger LOG = LoggerFactory.getLogger(InChIGenerator.class);
+
+    public void attachIdentifiers(final CMLMolecule molecule) {
+        try {
+            final String inchi = InchiTool.generateInchi(molecule, null);
+            attachInchi(molecule, inchi);
+            final String inchiKey = InchiTool.generateInchiKey(inchi);
+            attachInchiKey(molecule, inchiKey);
+        } catch (Exception e) {
+            LOG.warn("Error generating InChI", e);
+        }
+    }
+
+    private void attachInchi(final CMLMolecule mol, final String inchi) {
+        final CMLIdentifier identifier = new CMLIdentifier();
+        identifier.setConvention("iupac:inchi");
+        identifier.setCMLValue(inchi);
+        mol.appendChild(identifier);
+    }
+
+    private void attachInchiKey(final CMLMolecule mol, final String inchiKey) {
+        final CMLIdentifier identifier = new CMLIdentifier();
+        identifier.setConvention("iupac:inchiKey");
+        identifier.setCMLValue(inchiKey);
+        mol.appendChild(identifier);
+    }
+
+}