Commits

Sam Adams committed 578490d

Initial commit

Comments (0)

Files changed (6)

+
+# maven
+^target/
+/target/
+
+# intellij
+^.idea/
+\.iml$
+
+# eclipse
+^\.classpath$
+/\.classpath$
+^\.project$
+/\.project$
+^\.settings/
+/\.settings/
+
+# temp files
+~$
+\.old$
+^temp/
+/temp/
+^workspace/
+<?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">
+
+    <parent>
+        <groupId>net.chempound</groupId>
+        <artifactId>chempound-parent</artifactId>
+        <version>1-SNAPSHOT</version>
+    </parent>
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>uk.ac.cam.ch.wwmm.quixote</groupId>
+    <artifactId>quixote-cli</artifactId>
+    <version>0.1-SNAPSHOT</version>
+
+    <name>Quixote CLI</name>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>net.chempound.client</groupId>
+            <artifactId>client-cli</artifactId>
+            <version>0.1-SNAPSHOT</version>
+        </dependency>
+
+        <dependency>
+            <groupId>net.chempound.crystallography</groupId>
+            <artifactId>crystallography-importer</artifactId>
+            <version>0.1-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>net.chempound.compchem</groupId>
+            <artifactId>compchem-importer</artifactId>
+            <version>0.1-SNAPSHOT</version>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+    </dependencies>
+
+    <repositories>
+        <repository>
+            <id>ucc-repo</id>
+            <url>https://maven.ch.cam.ac.uk/m2repo</url>
+        </repository>
+    </repositories>
+
+</project>

src/main/java/uk/ac/cam/ch/wwmm/quixote/client/QuixoteCLI.java

+package uk.ac.cam.ch.wwmm.quixote.client;
+
+import net.chempound.client.cli.ChempoundClientCLI;
+
+/**
+ * @author Sam Adams
+ */
+public class QuixoteCLI extends ChempoundClientCLI {
+
+    public static void main(String[] args) {
+        QuixoteCLI quixoteCLI = new QuixoteCLI();
+        quixoteCLI.runMain(args);
+    }
+
+}

src/main/java/uk/ac/cam/ch/wwmm/quixote/client/cif/CifDepositBuilder.java

+package uk.ac.cam.ch.wwmm.quixote.client.cif;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.xmloutput.RDFXMLWriterI;
+import com.hp.hpl.jena.xmloutput.impl.Abbreviated;
+import net.chempound.client.DepositBuilder;
+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.swordapp.client.Deposit;
+import org.xmlcml.cml.base.CMLBuilder;
+import org.xmlcml.cml.element.CMLCml;
+import org.xmlcml.cml.element.CMLMolecule;
+import sea36.image.jmol.ImageGeneratorJmol;
+import net.chempound.crystal.importer.CrystalCml2RdfConverter;
+import net.chempound.storage.InMemoryResource;
+import net.chempound.storage.LocalResource;
+import net.chempound.util.MimeType;
+
+import java.io.*;
+import java.net.URI;
+
+import static net.chempound.chemistry.cml.ChemicalMime.*;
+
+/**
+ * @author Sam Adams
+ */
+public class CifDepositBuilder extends DepositBuilder {
+
+    private int imageDimension = 400;
+    private int thumbnailDimension = 100;
+
+
+    private LocalResource cifFile;
+    private LocalResource cmlFile;
+    private LocalResource rdfFile;
+    private LocalResource imageFile;
+    private LocalResource thumbnailFile;
+
+    public void setCifFile(final LocalResource cifFile) {
+        this.cifFile = cifFile;
+    }
+
+    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;
+    }
+
+    @Override
+    public Deposit build() throws Exception {
+        String id = getId();
+        if (cmlFile == null) {
+            createCml();
+        }
+        if (imageFile == null || thumbnailFile == null) {
+            createImages(id);
+        }
+        if (rdfFile == null) {
+            createRdf();
+        }
+
+        return super.build();
+    }
+
+    private String getId() {
+        if (getSlug() != null) {
+            return getSlug();
+        }
+        return null;
+    }
+
+    private LocalResource createCml() {
+        return null;  //To change body of created methods use File | Settings | File Templates.
+    }
+
+    private void createImages(final String id) throws IOException {
+        final File imageFile = File.createTempFile("image", "png");
+        try {
+            final File thumbnailFile = File.createTempFile("image_tn", "png");
+            try {
+                createImages(cifFile, "cif", imageFile, thumbnailFile);
+                this.imageFile = createImageResource(imageFile, id+".png");
+                this.thumbnailFile = createImageResource(thumbnailFile, id+"_tn.png");
+            } finally {
+                FileUtils.deleteQuietly(thumbnailFile);
+            }
+        } finally {
+            FileUtils.deleteQuietly(imageFile);
+        }
+    }
+
+    private void createRdf() {
+
+        final CMLCml completeCml = loadCml(cmlFile);
+
+        final CrystalCml2RdfConverter conv = new CrystalCml2RdfConverter();
+        final Model model = conv.createModel(completeCml, URI.create(""));
+
+        byte[] bytes = write(model);
+
+//        model.add(cifResource.getModel());
+//        model.add(cmlResource.getModel());
+
+
+    }
+
+    public static byte[] write(final Model model) {
+        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+
+        final RDFXMLWriterI w = new Abbreviated();
+        w.setProperty("allowBadURIs", true);
+        w.setProperty("prettyTypes", true);
+        w.write(model, buffer, null);
+
+        return buffer.toByteArray();
+    }
+
+    private CMLCml loadCml(final LocalResource cmlFile) {
+        return null;
+    }
+
+
+    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);
+    }
+
+    protected static LocalResource createImageResource(final File file, final String path) throws IOException {
+        final byte[] buffer = FileUtils.readFileToByteArray(file);
+        return new InMemoryResource(path, MimeType.IMAGE_PNG, buffer);
+    }
+
+
+    protected static LocalResource createCmlResource(final Document doc, final String path) throws IOException {
+        final byte[] bytes = writeDocument(doc);
+        return new InMemoryResource(path, 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);
+    }
+
+    public static Document reloadCml(final Document doc) throws IOException, ParsingException {
+        final byte[] buffer = writeDocument(doc);
+        return readCml(buffer);
+    }
+
+    public static Document readCml(final byte[] buffer) throws IOException, ParsingException {
+        final CMLBuilder builder = new CMLBuilder();
+        return builder.build(new ByteArrayInputStream(buffer));
+    }
+
+    public static byte[] writeDocument(final Document doc) throws IOException {
+        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        final Serializer ser = new Serializer(buffer);
+        ser.write(doc);
+        return buffer.toByteArray();
+    }
+
+}

src/main/java/uk/ac/cam/ch/wwmm/quixote/client/cif/DepositCifCommand.java

+package uk.ac.cam.ch.wwmm.quixote.client.cif;
+
+import net.chempound.client.cli.command.DepositCommand;
+import net.chempound.client.cli.options.GlobalOptions;
+
+/**
+ * @author Sam Adams
+ */
+public class DepositCifCommand extends DepositCommand {
+
+    private DepositCifOptions depositOptions;
+
+    public DepositCifCommand(final GlobalOptions globalOptions, final DepositCifOptions depositOptions) {
+        super(globalOptions, depositOptions);
+        this.depositOptions = depositOptions;
+    }
+
+
+
+}

src/main/java/uk/ac/cam/ch/wwmm/quixote/client/cif/DepositCifOptions.java

+package uk.ac.cam.ch.wwmm.quixote.client.cif;
+
+import net.chempound.client.cli.options.DepositOptions;
+
+/**
+ * @author Sam Adams
+ */
+public class DepositCifOptions extends DepositOptions {
+}