1. Chempound
  2. Untitled project
  3. chemistry

Commits

Sam Adams  committed 598e4fa

Refactoring chemistry importers

  • Participants
  • Parent commits 4b5a719
  • Branches default

Comments (0)

Files changed (11)

File chemistry-common/pom.xml

View file
 
         <dependency>
             <groupId>net.chempound</groupId>
+            <artifactId>chempound-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>net.chempound</groupId>
             <artifactId>chempound-rdf-cml</artifactId>
         </dependency>
 

File chemistry-common/src/main/java/net/chempound/chemistry/Cml2RdfConverter.java

View file
 
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.Multimap;
-import com.hp.hpl.jena.rdf.model.*;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.Property;
+import com.hp.hpl.jena.rdf.model.RDFNode;
+import com.hp.hpl.jena.rdf.model.Resource;
 import nu.xom.Element;
 import nu.xom.Elements;
 import nu.xom.Node;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.xmlcml.cml.base.CMLConstants;
 import org.xmlcml.cml.base.CMLElement;
 import org.xmlcml.cml.base.CMLUtil;
  */
 public class Cml2RdfConverter {
 
+    private static final Logger LOG = LoggerFactory.getLogger(Cml2RdfConverter.class);
+
     protected void addParameters(final Model model, final Resource parentNode, final List<CMLParameter> parameters) {
         for (final CMLParameter parameter : parameters) {
             final QName dictRef = getDictRefUri(parameter);
                 final Property prop = getProperty(model, dictRef);
                 addScalar(model, resource, prop, (CMLScalar) value);
             } else {
-                System.err.println("** Missing dictRef **");
-                System.err.println(property.toXML());
-                System.err.println("*********************");
+                LOG.warn("Missing dictRef: " + property.toXML());
             }
         } else {
             final QName dictRef = getDictRefUri(property);
                 final Property prop = getHasProperty(model, dictRef);
                 resource.addLiteral(prop, true);
             } else {
-                System.err.println("** Missing dictRef **");
-                System.err.println(property.toXML());
-                System.err.println("*********************");
+                LOG.warn("Missing dictRef: " + property.toXML());
             }
         }
     }
         final String localName = dictRef.substring(i+1);
         final String uri = context.getNamespaceURI(prefix);
         if (uri == null) {
-            System.out.println("!! No dictRef URI: "+dictRef);
+            LOG.warn("No dictRef URI: " + dictRef);
             return null;
         }
         return new QName(uri, localName, prefix);

File chemistry-common/src/main/java/net/chempound/chemistry/cml/ChemicalMime.java

View file
+package net.chempound.chemistry.cml;
+
+import net.chempound.util.MimeType;
+
+/**
+ * @author Sam Adams
+ */
+public class ChemicalMime {
+
+    public static final MimeType CHEMICAL_CML = new MimeType("chemical/x-cml", "Chemical Markup Language");
+
+    public static final MimeType CHEMICAL_CIF = new MimeType("chemical/x-cif", "Crystallographic Information Framework");
+
+    public static final MimeType CHEMICAL_GAUSSIAN_INPUT = new MimeType("chemical/x-gaussian-input", "Gaussian Input");
+    public static final MimeType CHEMICAL_GAUSSIAN_LOG = new MimeType("chemical/x-gaussian-log", "Gaussian Log");
+
+    public static final MimeType CHEMICAL_NWCHEM = new MimeType("chemical/x-log+nwchem", "NWChem Log File");
+
+}

File chemistry-common/src/main/java/net/chempound/chemistry/cml/CmlIO.java

View file
+package net.chempound.chemistry.cml;
+
+import nu.xom.Document;
+import nu.xom.Element;
+import nu.xom.Serializer;
+import org.apache.commons.io.IOUtils;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+/**
+ * @author Sam Adams
+ */
+public class CmlIO {
+
+    public static void write(final Element element, final File file) throws IOException {
+        final Element copy = (Element) element.copy();
+        write(new Document(copy), file);
+    }
+
+    public static void write(final Document doc, final File file) throws IOException {
+        final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
+        try {
+            final Serializer ser = new Serializer(out);
+            ser.write(doc);
+        } finally {
+            IOUtils.closeQuietly(out);
+        }
+    }
+
+}

File chemistry-import-testutils/chemistry-import-testutils.iml

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false">
+    <output url="file://$MODULE_DIR$/target/classes" />
+    <output-test url="file://$MODULE_DIR$/target/test-classes" />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
+      <sourceFolder url="file://$MODULE_DIR$/target/generated-sources/annotations" isTestSource="false" />
+      <excludeFolder url="file://$MODULE_DIR$/target/classes" />
+      <excludeFolder url="file://$MODULE_DIR$/target/maven-archiver" />
+      <excludeFolder url="file://$MODULE_DIR$/target/surefire" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="module" module-name="chemistry-importer" />
+    <orderEntry type="module" module-name="chemistry-common" />
+    <orderEntry type="module" module-name="chempound-api" />
+    <orderEntry type="module" module-name="chempound-rdf-vocabs" />
+    <orderEntry type="library" name="Maven: com.hp.hpl.jena:jena:2.6.3" level="project" />
+    <orderEntry type="library" name="Maven: com.hp.hpl.jena:iri:0.8" level="project" />
+    <orderEntry type="library" name="Maven: com.ibm.icu:icu4j:3.4.4" level="project" />
+    <orderEntry type="library" scope="RUNTIME" name="Maven: com.hp.hpl.jena:iri:sources:0.8" level="project" />
+    <orderEntry type="library" name="Maven: xerces:xercesImpl:2.7.1" level="project" />
+    <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.6.1" level="project" />
+    <orderEntry type="library" scope="RUNTIME" name="Maven: org.slf4j:slf4j-log4j12:1.6.1" level="project" />
+    <orderEntry type="library" name="Maven: log4j:log4j:1.2.16" level="project" />
+    <orderEntry type="module" module-name="chempound-rdf-cml" />
+    <orderEntry type="library" name="Maven: com.hp.hpl.jena:arq:2.8.4" level="project" />
+    <orderEntry type="library" scope="RUNTIME" name="Maven: com.hp.hpl.jena:jena:sources:2.6.3" level="project" />
+    <orderEntry type="library" scope="RUNTIME" name="Maven: com.hp.hpl.jena:jena:test-jar:test-sources:2.6.3" level="project" />
+    <orderEntry type="library" name="Maven: org.codehaus.woodstox:wstx-asl:3.2.9" level="project" />
+    <orderEntry type="library" name="Maven: stax:stax-api:1.0.1" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.lucene:lucene-core:2.3.1" level="project" />
+    <orderEntry type="library" name="Maven: commons-io:commons-io:2.0" level="project" />
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.4" level="project" />
+    <orderEntry type="library" name="Maven: com.google.inject:guice:3.0" level="project" />
+    <orderEntry type="library" name="Maven: javax.inject:javax.inject:1" level="project" />
+    <orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" />
+    <orderEntry type="library" name="Maven: org.freemarker:freemarker:2.3.16" level="project" />
+    <orderEntry type="library" name="Maven: cml:cmlxom:3.1-SNAPSHOT" level="project" />
+    <orderEntry type="library" name="Maven: org.xml-cml:euclid:1.1-SNAPSHOT" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.commons:commons-math:2.2" level="project" />
+    <orderEntry type="library" name="Maven: joda-time:joda-time:1.6.2" level="project" />
+    <orderEntry type="library" name="Maven: org.ccil.cowan.tagsoup:tagsoup:1.2" level="project" />
+    <orderEntry type="library" name="Maven: xom:xom:1.2.5" level="project" />
+    <orderEntry type="library" name="Maven: xml-apis:xml-apis:1.3.03" level="project" />
+    <orderEntry type="library" name="Maven: xalan:xalan:2.7.0" level="project" />
+    <orderEntry type="library" name="Maven: net.sf.jni-inchi:jni-inchi:0.7" level="project" />
+    <orderEntry type="library" name="Maven: net.sf.jnati:jnati-deploy:0.3" level="project" />
+    <orderEntry type="library" name="Maven: net.sf.jnati:jnati-core:0.3" level="project" />
+    <orderEntry type="library" name="Maven: com.google.guava:guava-collections:r03" level="project" />
+    <orderEntry type="library" name="Maven: com.google.guava:guava-annotations:r03" level="project" />
+    <orderEntry type="library" name="Maven: com.google.guava:guava-primitives:r03" level="project" />
+    <orderEntry type="library" name="Maven: com.google.guava:guava-base:r03" level="project" />
+    <orderEntry type="library" name="Maven: sea36:JmolImageGenerator:1.0-SNAPSHOT" level="project" />
+    <orderEntry type="library" name="Maven: net.sourceforge.jmol:jmol:11.8" level="project" />
+    <orderEntry type="library" name="Maven: java3d:vecmath:1.5.1" level="project" />
+    <orderEntry type="library" name="Maven: commons-cli:commons-cli:1.0" level="project" />
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.0" level="project" />
+    <orderEntry type="library" name="Maven: commons-lang:commons-lang:1.0" level="project" />
+    <orderEntry type="library" name="Maven: junit:junit:4.8.2" level="project" />
+    <orderEntry type="library" name="Maven: com.lowagie:itext:1.4.8" level="project" />
+    <orderEntry type="library" name="Maven: com.acme:acme-jmol:1.0" level="project" />
+    <orderEntry type="library" name="Maven: java:java-plugin:1.5.0_11" level="project" />
+    <orderEntry type="library" name="Maven: cml:jumbo:6.0.0-SNAPSHOT" level="project" />
+    <orderEntry type="library" name="Maven: org.xml-cml:cmlxom:3.2-SNAPSHOT" level="project" />
+    <orderEntry type="library" name="Maven: org.mockito:mockito-core:1.8.5" level="project" />
+    <orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
+    <orderEntry type="library" name="Maven: org.objenesis:objenesis:1.0" level="project" />
+  </component>
+</module>
+

File chemistry-import-testutils/pom.xml

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>net.chempound.chemistry</groupId>
+        <artifactId>chemistry</artifactId>
+        <version>0.1-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>chemistry-import-testutils</artifactId>
+    <name>Chempound : Chemistry Importer Test Utils</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>net.chempound.chemistry</groupId>
+            <artifactId>chemistry-importer</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+        </dependency>
+    </dependencies>
+
+</project>

File chemistry-import-testutils/src/main/java/net/chempound/chemistry/testutil/ImageGeneratorMocker.java

View file
+package net.chempound.chemistry.testutil;
+
+import net.chempound.chemistry.ImageGenerator;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import org.xmlcml.cml.element.CMLMolecule;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+
+/**
+ * @author Sam Adams
+ */
+public class ImageGeneratorMocker {
+
+    public static ImageGenerator mockImageGenerator() throws IOException {
+        final ImageGenerator imageGenerator = mock(ImageGenerator.class);
+
+        doAnswer(new Answer() {
+            @Override
+            public Object answer(final InvocationOnMock invocation) throws Throwable {
+                final File imageFile = (File) invocation.getArguments()[1];
+                final File thumbnailFile = (File) invocation.getArguments()[2];
+                imageFile.createNewFile();
+                thumbnailFile.createNewFile();
+                return null;
+            }
+        }).when(imageGenerator).createImages(any(CMLMolecule.class), any(File.class), any(File.class));
+        return imageGenerator;
+    }
+
+}

File chemistry-importer/pom.xml

View file
     </parent>
 
     <artifactId>chemistry-importer</artifactId>
-    <name>Chempound : Importer</name>
+    <name>Chempound : Chemistry Importer</name>
 
     <dependencies>
         <dependency>

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

View file
 package net.chempound.chemistry;
 
+import net.chempound.chemistry.cml.ChemicalMime;
 import nu.xom.Document;
 import nu.xom.Element;
 import nu.xom.ParsingException;
  */
 public abstract class CmlImporter extends ContentImporter {
 
-    public static final MimeType CHEMICAL_CML = new MimeType("chemical/x-cml", "Chemical Markup Language");
-
-    private int imageDimension = 400;
-    private int thumbnailDimension = 100;
-
     protected LocalResource imageResource;
     protected LocalResource thumbnailResource;
 
 
-    public int getImageDimension() {
-        return imageDimension;
-    }
-
-    public void setImageDimension(final int imageDimension) {
-        this.imageDimension = imageDimension;
-    }
-
-    public int getThumbnailDimension() {
-        return thumbnailDimension;
-    }
-
-    public void setThumbnailDimension(final int thumbnailDimension) {
-        this.thumbnailDimension = thumbnailDimension;
-    }
-
-
-
-    protected void createImages(final LocalResource resource, final String ext, final File imageFile, final File thumbnailFile) throws IOException {
-        final File tempFile = File.createTempFile("mol", ext);
-        try {
-            write(resource, tempFile);
-
-            final ImageGeneratorJmol imageGen = new ImageGeneratorJmol();
-            try {
-                imageGen.loadMolecule(tempFile);
-
-                createImage(imageFile, imageGen);
-                createThumbnail(thumbnailFile, imageGen);
-
-            } finally {
-                imageGen.shutdown();
-            }
-        } finally {
-            FileUtils.deleteQuietly(tempFile);
-        }
-    }
-
-    protected void createImages(final CMLMolecule molecule, final File imageFile, final File thumbnailFile) throws IOException {
-        final File tempFile = File.createTempFile("mol", "cml");
-        try {
-            write(molecule, tempFile);
-
-            final ImageGeneratorJmol imageGen = new ImageGeneratorJmol();
-            try {
-                imageGen.loadMolecule(tempFile);
-                imageGen.setShowAxes(true);
-                imageGen.createImage(imageFile, getImageDimension(), getImageDimension());
-
-                imageGen.setShowAxes(false);
-                imageGen.setFrankOn(false);
-                imageGen.createImage(thumbnailFile, getThumbnailDimension(), getThumbnailDimension());
-            } finally {
-                imageGen.shutdown();
-            }
-        } finally {
-            FileUtils.deleteQuietly(tempFile);
-        }
-    }
-
-    protected void createThumbnail(final File thumbnailFile, final ImageGeneratorJmol imageGen) {
-        imageGen.setShowAxes(false);
-        imageGen.setShowUnitCell(false);
-        imageGen.setFrankOn(false);
-        imageGen.createImage(thumbnailFile, getThumbnailDimension(), getThumbnailDimension());
-    }
-
-    protected void createImage(final File imageFile, final ImageGeneratorJmol imageGen) {
-        imageGen.setShowAxes(true);
-        imageGen.setShowUnitCell(true);
-        imageGen.createImage(imageFile, getImageDimension(), getImageDimension());
-    }
-
-
-    protected static void write(final Element element, final File file) throws IOException {
-        final Element copy = (Element) element.copy();
-        write(new Document(copy), file);
-    }
-
-    protected static void write(final Document doc, final File file) throws IOException {
-        final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
-        try {
-            final Serializer ser = new Serializer(out);
-            ser.write(doc);
-        } finally {
-            IOUtils.closeQuietly(out);
-        }
-    }
-
-    protected static void write(final LocalResource resource, final File file) throws IOException {
-        final FileOutputStream out = new FileOutputStream(file);
-        try {
-            final InputStream in = resource.openInputStream();
-            try {
-                IOUtils.copy(in, out);
-            } finally {
-                IOUtils.closeQuietly(in);
-            }
-        } finally {
-            IOUtils.closeQuietly(out);
-        }
-    }
-
     protected static LocalResource createCmlResource(final Element cml, final String path) throws IOException {
         final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         final Serializer ser = new Serializer(buffer);
         final Document doc = cml.getDocument() != null ? cml.getDocument() : new Document(cml);
         ser.write(doc);
         final byte[] bytes = buffer.toByteArray();
-        return new InMemoryResource(path, CHEMICAL_CML, bytes);
+        return new InMemoryResource(path, ChemicalMime.CHEMICAL_CML, bytes);
     }
 
     protected static LocalResource createImageResource(final File file, final String path) throws IOException {
 
     protected static LocalResource createCmlResource(final Document doc, final String path) throws IOException {
         final byte[] bytes = writeDocument(doc);
-        return new InMemoryResource(path, CHEMICAL_CML, bytes);
+        return new InMemoryResource(path, ChemicalMime.CHEMICAL_CML, bytes);
     }
 
-
     public static Document reloadCml(final Element cml) throws IOException, ParsingException {
         final Document doc = cml.getDocument() == null ? new Document(cml) : cml.getDocument();
         return reloadCml(doc);

File chemistry-importer/src/main/java/net/chempound/chemistry/ImageGenerator.java

View file
+package net.chempound.chemistry;
+
+import net.chempound.chemistry.cml.CmlIO;
+import net.chempound.storage.LocalResource;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.xmlcml.cml.element.CMLMolecule;
+import sea36.image.jmol.ImageGeneratorJmol;
+
+import java.awt.*;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * @author Sam Adams
+ */
+public class ImageGenerator {
+
+    private static final Dimension DEFAULT_IMAGE_DIMENSION = new Dimension(400, 400);
+    private static final Dimension DEFAULT_THUMBNAIL_DIMENSION = new Dimension(100, 100);
+
+    private final Dimension imageDimension;
+    private final Dimension thumbnailDimension;
+
+    public ImageGenerator() {
+        this(DEFAULT_IMAGE_DIMENSION, DEFAULT_THUMBNAIL_DIMENSION);
+    }
+
+    public ImageGenerator(final Dimension imageDimenstion, final Dimension thumbnailDimension) {
+        this.imageDimension = imageDimenstion.getSize();
+        this.thumbnailDimension = thumbnailDimension.getSize();
+    }
+
+    public void createImages(final LocalResource resource, final String ext, final File imageFile, final File thumbnailFile) throws IOException {
+        final File tempFile = File.createTempFile("mol", ext);
+        try {
+            write(resource, tempFile);
+
+            createImages(tempFile, imageFile, thumbnailFile);
+        } finally {
+            FileUtils.deleteQuietly(tempFile);
+        }
+    }
+
+    public void createImages(final CMLMolecule molecule, final File imageFile, final File thumbnailFile) throws IOException {
+        final File tempFile = File.createTempFile("mol", "cml");
+        try {
+            CmlIO.write(molecule, tempFile);
+
+            createImages(tempFile, imageFile, thumbnailFile);
+        } finally {
+            FileUtils.deleteQuietly(tempFile);
+        }
+    }
+
+    private void createImages(final File inputFile, final File imageFile, final File thumbnailFile) throws IOException {
+        final ImageGeneratorJmol imageGen = new ImageGeneratorJmol();
+        try {
+            imageGen.loadMolecule(inputFile);
+
+            createImage(imageFile, imageGen);
+            createThumbnail(thumbnailFile, imageGen);
+
+        } finally {
+            imageGen.shutdown();
+        }
+    }
+
+    private void createThumbnail(final File thumbnailFile, final ImageGeneratorJmol imageGen) {
+        imageGen.setShowAxes(false);
+        imageGen.setShowUnitCell(false);
+        imageGen.setFrankOn(false);
+        imageGen.createImage(thumbnailFile, thumbnailDimension.width, thumbnailDimension.height);
+    }
+
+    private void createImage(final File imageFile, final ImageGeneratorJmol imageGen) {
+        imageGen.setShowAxes(true);
+        imageGen.setShowUnitCell(true);
+        imageGen.setFrankOn(true);
+        imageGen.createImage(imageFile, imageDimension.width, imageDimension.height);
+    }
+
+    protected static void write(final LocalResource resource, final File file) throws IOException {
+        final FileOutputStream out = new FileOutputStream(file);
+        try {
+            final InputStream in = resource.openInputStream();
+            try {
+                IOUtils.copy(in, out);
+            } finally {
+                IOUtils.closeQuietly(in);
+            }
+        } finally {
+            IOUtils.closeQuietly(out);
+        }
+    }
+
+}

File pom.xml

View file
         <module>chemistry-jmol-plugin</module>
         <module>chemistry-search-structure</module>
         <module>chemistry-importer</module>
+        <module>chemistry-import-testutils</module>
     </modules>
 
     <dependencyManagement>
                 <version>0.1-SNAPSHOT</version>
             </dependency>
             <dependency>
+                <groupId>net.chempound.chemistry</groupId>
+                <artifactId>chemistry-importer</artifactId>
+                <version>0.1-SNAPSHOT</version>
+            </dependency>
+            <dependency>
                 <groupId>net.chempound</groupId>
                 <artifactId>chempound-api</artifactId>
                 <version>0.1-SNAPSHOT</version>