Commits

Sam Adams committed cf6eb39

Refactored inchi generation

Comments (0)

Files changed (4)

crystallography-importer/src/main/java/net/chempound/crystal/importer/CrystalStructureImporter.java

 import com.hp.hpl.jena.vocabulary.RDFS;
 import net.chempound.chemistry.CmlImporter;
 import net.chempound.chemistry.ImageGenerator;
+import net.chempound.chemistry.InChIGenerator;
 import net.chempound.chemistry.cml.ChemicalMime;
 import net.chempound.chemistry.inchi.InchiTool;
 import net.chempound.content.DefaultDepositRequest;
     private static final Logger LOG = LoggerFactory.getLogger(CrystalStructureImporter.class);
 
     private final ImageGenerator imageGenerator;
+    private final InChIGenerator inchiGenerator;
 
     private boolean createImages = true;
 
     public CrystalStructureImporter() {
-        this(new ImageGenerator());
+        this(new ImageGenerator(), new InChIGenerator());
     }
 
     @Inject
-    public CrystalStructureImporter(final ImageGenerator imageGenerator) {
+    public CrystalStructureImporter(final ImageGenerator imageGenerator, final InChIGenerator inChIGenerator) {
         this.imageGenerator = imageGenerator;
+        this.inchiGenerator = inChIGenerator;
     }
 
     public void load(final CIF originalCif) throws Exception {
         return null;
     }
 
-    public static void attachIdentifiers(final CMLCml completeCml) {
-        final List<Node> nodes = CMLUtil.getQueryNodes(completeCml, "//cml:molecule", CML_XPATH);
-        if (nodes.isEmpty()) {
-            throw new IllegalArgumentException("Unable to find molecule!");
+    public void attachIdentifiers(final Element element) {
+            final List<Node> nodes = CMLUtil.getQueryNodes(element, "//cml:molecule", CML_XPATH);
+            if (nodes.isEmpty()) {
+                throw new IllegalArgumentException("Unable to find molecule!");
+            }
+
+            final CMLMolecule molecule = (CMLMolecule) nodes.get(0);
+
+            final ConnectionTableTool connectionTableTool = new ConnectionTableTool(molecule);
+            connectionTableTool.partitionIntoMolecules();
+
+            inchiGenerator.attachIdentifiers(molecule);
+
+            final Elements mols = molecule.getChildCMLElements("molecule");
+            for (int i = 0; i < mols.size(); i++) {
+                inchiGenerator.attachIdentifiers((CMLMolecule) mols.get(i));
+            }
         }
 
-        final CMLMolecule molecule = (CMLMolecule) nodes.get(0);
-
-        final ConnectionTableTool connectionTableTool = new ConnectionTableTool(molecule);
-        connectionTableTool.partitionIntoMolecules();
-
-        attachIdentifiers(molecule);
-
-        final Elements mols = molecule.getChildCMLElements("molecule");
-        for (int i = 0; i < mols.size(); i++) {
-            final CMLMolecule mol = (CMLMolecule) mols.get(i);
-            attachIdentifiers(mol);
-        }
-    }
-
-    private static 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 static void attachInchi(final CMLMolecule mol, final String inchi) {
-        final CMLIdentifier identifier = new CMLIdentifier();
-        identifier.setConvention("iupac:inchi");
-        identifier.setCMLValue(inchi);
-        mol.appendChild(identifier);
-    }
-
-    private static void attachInchiKey(final CMLMolecule mol, final String inchiKey) {
-        final CMLIdentifier identifier = new CMLIdentifier();
-        identifier.setConvention("iupac:inchiKey");
-        identifier.setCMLValue(inchiKey);
-        mol.appendChild(identifier);
-    }
 
 
     private LocalResource createCifResource(final CIF cif, final String path) throws IOException {

crystallography-importer/src/test/java/net/chempound/crystal/importer/CrystalStructureImporterTest.java

 import com.hp.hpl.jena.rdf.model.ModelFactory;
 import com.hp.hpl.jena.rdf.model.RDFReader;
 import net.chempound.chemistry.ImageGenerator;
+import net.chempound.chemistry.InChIGenerator;
 import net.chempound.rdf.IgnoringEmptyBaseUriErrorHandler;
 import net.chempound.storage.DepositResource;
 import net.chempound.util.MimeType;
 public class CrystalStructureImporterTest {
 
     private ImageGenerator imageGenerator;
+    private InChIGenerator inchiGenerator;
 
     static {
         CrystalPersonalities.init();
     @Before
     public void setUp() throws Exception {
         imageGenerator = stubImageGenerator();
+        inchiGenerator = new InChIGenerator();
     }
 
     @Test
     public void testCifImporter() throws IOException, CIFException, ParsingException {
         CIF cif = loadCif("/wn2432.cif");
 
-        CrystalStructureImporter importer = new CrystalStructureImporter(imageGenerator);
+        CrystalStructureImporter importer = new CrystalStructureImporter(imageGenerator, inchiGenerator);
         DepositRequest request = importer.generateDepositRequest(cif, "wn2432");
 
         Model model = getRdfModel(request);

crystallography-test-harness/src/test/java/net/chempound/crystal/CrystalIntegrationTest.java

 import com.hp.hpl.jena.xmloutput.impl.Abbreviated;
 import net.chempound.DefaultChempoundModule;
 import net.chempound.chemistry.ImageGenerator;
+import net.chempound.chemistry.InChIGenerator;
 import net.chempound.config.BaseUri;
 import net.chempound.config.ChempoundConfiguration;
 import net.chempound.config.DefaultChempoundConfiguration;
     private File workspace;
 
     private ImageGenerator imageGenerator;
+    private InChIGenerator inchiGenerator;
 
     @Before
     public void init() throws Exception {
 
         imageGenerator = stubImageGenerator();
+        inchiGenerator = new InChIGenerator();
 
         workspace = createWorkspace();
 
 
 
     private DepositReceipt performDeposit(CIF cif, String id) throws IOException, ParsingException, SWORDClientException, SWORDError, ProtocolViolationException {
-        CrystalStructureImporter importer = new CrystalStructureImporter(imageGenerator);
+        CrystalStructureImporter importer = new CrystalStructureImporter(imageGenerator, inchiGenerator);
         DepositRequest request = importer.generateDepositRequest(cif, id);
         Deposit deposit = createDeposit(id, request);
 

crystallography-test-harness/src/test/java/net/chempound/crystal/CrystalSearchIntegrationTest.java

 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.Module;
+import net.chempound.DefaultChempoundModule;
 import net.chempound.chemistry.ImageGenerator;
+import net.chempound.chemistry.InChIGenerator;
 import net.chempound.client.SwordUtil;
+import net.chempound.config.BaseUri;
+import net.chempound.config.ChempoundConfiguration;
+import net.chempound.config.DefaultChempoundConfiguration;
+import net.chempound.content.DepositRequest;
+import net.chempound.crystal.importer.CrystalStructureImporter;
+import net.chempound.webapp.DefaultChempoundWebModule;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.junit.AfterClass;
 import org.restlet.data.Status;
 import org.swordapp.client.Deposit;
 import org.swordapp.client.SWORDClient;
-import net.chempound.DefaultChempoundModule;
-import net.chempound.config.BaseUri;
-import net.chempound.config.ChempoundConfiguration;
-import net.chempound.config.DefaultChempoundConfiguration;
-import net.chempound.content.DepositRequest;
-import net.chempound.crystal.importer.CrystalStructureImporter;
-import net.chempound.webapp.DefaultChempoundWebModule;
 
 import java.io.File;
 import java.io.IOException;
     public static final String SWORD_ENDPOINT = "http://localhost:8715/sword/collection/";
 
     private static ImageGenerator imageGenerator;
+    private static InChIGenerator inchiGenerator;
+
     private static Component component;
     private static File workspace;
 
     public static void init() throws Exception {
 
         imageGenerator = stubImageGenerator();
+        inchiGenerator = new InChIGenerator();
 
         workspace = createWorkspace();
 
     private static void loadData(String id) throws Exception {
         InputStream in = CrystalSearchIntegrationTest.class.getResourceAsStream("/"+id+".cif");
         try {
-            CrystalStructureImporter importer = new CrystalStructureImporter(imageGenerator);
+            CrystalStructureImporter importer = new CrystalStructureImporter(imageGenerator, inchiGenerator);
             importer.setCreateImages(false);
             DepositRequest request = importer.generateDepositRequest(in, id);
             Deposit deposit = SwordUtil.createSwordDeposit(request);