Commits

Sam Adams  committed ecbb9e4

Abstracted CML import common functions

  • Participants
  • Parent commits a077898

Comments (0)

Files changed (3)

File chemistry-importer/pom.xml

+<?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>uk.ac.cam.ch.wwmm.chempound.chemistry</groupId>
+        <artifactId>chemistry</artifactId>
+        <version>0.1-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>chemistry-importer</artifactId>
+    <name>Chempound : Importer</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>uk.ac.cam.ch.wwmm.chempound.chemistry</groupId>
+            <artifactId>chemistry-common</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>uk.ac.cam.ch.wwmm.chempound</groupId>
+            <artifactId>chempound-api</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>sea36</groupId>
+            <artifactId>JmolImageGenerator</artifactId>
+            <version>1.0-SNAPSHOT</version>
+        </dependency>
+
+
+        <dependency>
+            <groupId>cml</groupId>
+            <artifactId>jumbo</artifactId>
+            <version>6.0.0-SNAPSHOT</version>
+        </dependency>
+
+    </dependencies>
+
+</project>

File chemistry-importer/src/main/java/uk/ac/cam/ch/wwmm/chempound/chemistry/CmlImporter.java

+package uk.ac.cam.ch.wwmm.chempound.chemistry;
+
+import nu.xom.Document;
+import nu.xom.Element;
+import nu.xom.ParsingException;
+import nu.xom.Serializer;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.xmlcml.cml.base.CMLBuilder;
+import org.xmlcml.cml.element.CMLMolecule;
+import sea36.image.jmol.ImageGeneratorJmol;
+import uk.ac.cam.ch.wwmm.chempound.ContentImporter;
+import uk.ac.cam.ch.wwmm.chempound.storage.InMemoryResource;
+import uk.ac.cam.ch.wwmm.chempound.storage.LocalResource;
+import uk.ac.cam.ch.wwmm.chempound.util.MimeType;
+
+import java.io.*;
+
+/**
+ * @author Sam Adams
+ */
+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(int imageDimension) {
+        this.imageDimension = imageDimension;
+    }
+
+    public int getThumbnailDimension() {
+        return thumbnailDimension;
+    }
+
+    public void setThumbnailDimension(int thumbnailDimension) {
+        this.thumbnailDimension = thumbnailDimension;
+    }
+
+
+
+    protected void createImages(LocalResource resource, String ext, File imageFile, File thumbnailFile) throws IOException {
+        File tempFile = File.createTempFile("mol", ext);
+        try {
+            write(resource, tempFile);
+
+            ImageGeneratorJmol imageGen = new ImageGeneratorJmol();
+            try {
+                imageGen.loadMolecule(tempFile);
+
+                createImage(imageFile, imageGen);
+                createThumbnail(thumbnailFile, imageGen);
+
+            } finally {
+                imageGen.shutdown();
+            }
+        } finally {
+            FileUtils.deleteQuietly(tempFile);
+        }
+    }
+
+    protected void createImages(CMLMolecule molecule, File imageFile, File thumbnailFile) throws IOException {
+        File tempFile = File.createTempFile("mol", "cml");
+        try {
+            write(molecule, tempFile);
+
+            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(File thumbnailFile, ImageGeneratorJmol imageGen) {
+        imageGen.setShowAxes(false);
+        imageGen.setShowUnitCell(false);
+        imageGen.setFrankOn(false);
+        imageGen.createImage(thumbnailFile, getThumbnailDimension(), getThumbnailDimension());
+    }
+
+    protected void createImage(File imageFile, ImageGeneratorJmol imageGen) {
+        imageGen.setShowAxes(true);
+        imageGen.setShowUnitCell(true);
+        imageGen.createImage(imageFile, getImageDimension(), getImageDimension());
+    }
+
+
+    protected static void write(Element element, File tempFile) throws IOException {
+        Element copy = (Element) element.copy();
+        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile));
+        try {
+            Serializer ser = new Serializer(out);
+            ser.write(new Document(copy));
+        } finally {
+            IOUtils.closeQuietly(out);
+        }
+
+    }
+
+    protected static void write(LocalResource resource, File file) throws IOException {
+        FileOutputStream out = new FileOutputStream(file);
+        try {
+            InputStream in = resource.openInputStream();
+            try {
+                IOUtils.copy(in, out);
+            } finally {
+                IOUtils.closeQuietly(in);
+            }
+        } finally {
+            IOUtils.closeQuietly(out);
+        }
+    }
+
+    protected static LocalResource createCmlResource(Element cml, String path) throws IOException {
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        Serializer ser = new Serializer(buffer);
+        Document doc = cml.getDocument() != null ? cml.getDocument() : new Document(cml);
+        ser.write(doc);
+        byte[] bytes = buffer.toByteArray();
+        return new InMemoryResource(path, CHEMICAL_CML, bytes);
+    }
+
+    protected static LocalResource createImageResource(File file, String path) throws IOException {
+        byte[] buffer = FileUtils.readFileToByteArray(file);
+        return new InMemoryResource(path, MimeType.IMAGE_PNG, buffer);
+    }
+
+
+    protected static LocalResource createCmlResource(Document doc, String path) throws IOException {
+        byte[] bytes = writeDocument(doc);
+        return new InMemoryResource(path, CHEMICAL_CML, bytes);
+    }
+
+
+    public static Document reloadCml(Element cml) throws IOException, ParsingException {
+        Document doc = cml.getDocument() == null ? new Document(cml) : cml.getDocument();
+        return reloadCml(doc);
+    }
+
+    public static Document reloadCml(Document doc) throws IOException, ParsingException {
+        byte[] buffer = writeDocument(doc);
+        return readCml(buffer);
+    }
+
+    public static Document readCml(byte[] buffer) throws IOException, ParsingException {
+        CMLBuilder builder = new CMLBuilder();
+        return builder.build(new ByteArrayInputStream(buffer));
+    }
+
+    public static byte[] writeDocument(Document doc) throws IOException {
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        Serializer ser = new Serializer(buffer);
+        ser.write(doc);
+        return buffer.toByteArray();
+    }
+
+
+}
         <module>chemistry-common</module>
         <module>chemistry-jmol-plugin</module>
         <module>chemistry-search-structure</module>
+        <module>chemistry-importer</module>
     </modules>
 
     <dependencyManagement>