Sam Adams avatar Sam Adams committed 2078094

Added Cif/Gau/Nwchem commands

Comments (0)

Files changed (8)

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

 import net.chempound.client.cli.ChempoundClientCLI;
 import net.chempound.client.cli.command.Command;
 import uk.ac.cam.ch.wwmm.quixote.client.cif.DepositCifCommand;
+import uk.ac.cam.ch.wwmm.quixote.client.gaussian.DepositGaussianCommand;
+import uk.ac.cam.ch.wwmm.quixote.client.nwchem.DepositNWChemCommand;
 
 /**
  * @author Sam Adams
         super.installCommands(jCommander);
 
         addCommand(jCommander, "deposit-cif", new DepositCifCommand());
+        addCommand(jCommander, "deposit-gaussian", new DepositGaussianCommand());
+        addCommand(jCommander, "deposit-nwchem", new DepositNWChemCommand());
     }
 }

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.DepositBuilder;
+import net.chempound.client.SwordUtil;
 import net.chempound.client.cli.command.Command;
 import net.chempound.client.cli.command.DepositCommand;
 import net.chempound.client.cli.options.GlobalOptions;
+import net.chempound.crystal.importer.CrystalStructureImporter;
+import net.chempound.crystal.rdf.CrystalStructurePersonalities;
+import net.chempound.rdf.ChempoundPersonalities;
+import org.apache.commons.io.FilenameUtils;
+import org.swordapp.client.Deposit;
+import org.swordapp.client.DepositReceipt;
+import org.swordapp.client.SWORDClient;
+
+import java.io.File;
 
 /**
  * @author Sam Adams
  */
 public class DepositCifCommand extends DepositCommand {
 
-    private final DepositCifOptions options;
+    static {
+        CrystalStructurePersonalities.init();
+    }
+
+    private final DepositCifOptions depositOptions;
 
     public DepositCifCommand() {
         super(new DepositCifOptions());
-        this.options = (DepositCifOptions) super.depositOptions;
+        this.depositOptions = (DepositCifOptions) super.depositOptions;
     }
 
     @Override
     public void invoke(final GlobalOptions globalOptions) {
-        throw new UnsupportedOperationException();
+        CrystalStructureImporter importer = new CrystalStructureImporter();
+
+        if (depositOptions.title != null) {
+            importer.setTitle(depositOptions.title);
+        }
+        if (depositOptions.slug != null) {
+            importer.setId(depositOptions.slug);
+        }
+
+        final String filename = depositOptions.files.get(0);
+        final File cifFile = globalOptions.workingDirectory == null ? new File(filename) : new File(globalOptions.workingDirectory, filename);
+        importer.setCifResource(FilenameUtils.getName(filename), cifFile);
+
+        final SWORDClient swordClient = new SWORDClient();
+        final String url = globalOptions.repository;
+        try {
+            final Deposit deposit = SwordUtil.createSwordDeposit(importer.build());
+            final DepositReceipt receipt = swordClient.deposit(url, deposit);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
     }
 
 }

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

+package uk.ac.cam.ch.wwmm.quixote.client.gaussian;
+
+import net.chempound.client.SwordUtil;
+import net.chempound.client.cli.command.DepositCommand;
+import net.chempound.client.cli.options.GlobalOptions;
+import net.chempound.compchem.GaussianLogImporter;
+import net.chempound.compchem.rdf.CompChemPersonalities;
+import net.chempound.crystal.importer.CrystalStructureImporter;
+import net.chempound.crystal.rdf.CrystalStructurePersonalities;
+import org.apache.commons.io.FilenameUtils;
+import org.swordapp.client.Deposit;
+import org.swordapp.client.DepositReceipt;
+import org.swordapp.client.SWORDClient;
+
+import java.io.File;
+
+/**
+ * @author Sam Adams
+ */
+public class DepositGaussianCommand extends DepositCommand {
+
+    static {
+        CompChemPersonalities.init();
+    }
+
+    private final DepositGaussianOptions depositOptions;
+
+    public DepositGaussianCommand() {
+        super(new DepositGaussianOptions());
+        this.depositOptions = (DepositGaussianOptions) super.depositOptions;
+    }
+
+    @Override
+    public void invoke(final GlobalOptions globalOptions) {
+        GaussianLogImporter importer = new GaussianLogImporter();
+
+//        if (depositOptions.title != null) {
+//            importer.setTitle(depositOptions.title);
+//        }
+        if (depositOptions.slug != null) {
+            importer.setId(depositOptions.slug);
+        }
+
+        final String filename = depositOptions.files.get(0);
+        final File cifFile = globalOptions.workingDirectory == null ? new File(filename) : new File(globalOptions.workingDirectory, filename);
+        importer.setLogFile(cifFile, FilenameUtils.getName(filename));
+
+        final SWORDClient swordClient = new SWORDClient();
+        final String url = globalOptions.repository;
+        try {
+            final Deposit deposit = SwordUtil.createSwordDeposit(importer.generateDepositRequest());
+            final DepositReceipt receipt = swordClient.deposit(url, deposit);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+}

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

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

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

+package uk.ac.cam.ch.wwmm.quixote.client.nwchem;
+
+import net.chempound.client.SwordUtil;
+import net.chempound.client.cli.command.DepositCommand;
+import net.chempound.client.cli.options.GlobalOptions;
+import net.chempound.compchem.GaussianLogImporter;
+import net.chempound.compchem.NWChemLogImporter;
+import net.chempound.compchem.rdf.CompChemPersonalities;
+import org.apache.commons.io.FilenameUtils;
+import org.swordapp.client.Deposit;
+import org.swordapp.client.DepositReceipt;
+import org.swordapp.client.SWORDClient;
+
+import java.io.File;
+
+/**
+ * @author Sam Adams
+ */
+public class DepositNWChemCommand extends DepositCommand {
+
+    static {
+        CompChemPersonalities.init();
+    }
+
+    private final DepositNWChemOptions depositOptions;
+
+    public DepositNWChemCommand() {
+        super(new DepositNWChemOptions());
+        this.depositOptions = (DepositNWChemOptions) super.depositOptions;
+    }
+
+    @Override
+    public void invoke(final GlobalOptions globalOptions) {
+        NWChemLogImporter importer = new NWChemLogImporter();
+
+//        if (depositOptions.title != null) {
+//            importer.setTitle(depositOptions.title);
+//        }
+        if (depositOptions.slug != null) {
+            importer.setId(depositOptions.slug);
+        }
+
+        final String filename = depositOptions.files.get(0);
+        final File cifFile = globalOptions.workingDirectory == null ? new File(filename) : new File(globalOptions.workingDirectory, filename);
+        importer.setLogFile(cifFile, FilenameUtils.getName(filename));
+
+        final SWORDClient swordClient = new SWORDClient();
+        final String url = globalOptions.repository;
+        try {
+            final Deposit deposit = SwordUtil.createSwordDeposit(importer.generateDepositRequest());
+            final DepositReceipt receipt = swordClient.deposit(url, deposit);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+}

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

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

src/test/java/uk/ac/cam/ch/wwmm/quixote/client/QuixoteClientCLIIntegrationTest.java

+package uk.ac.cam.ch.wwmm.quixote.client;
+
+/**
+ * @author Sam Adams
+ */
+public class QuixoteClientCLIIntegrationTest {
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.