Search in sources :

Example 1 with IInputState

use of ambit2.core.io.IInputState in project ambit-mirror by ideaconsult.

the class CallableFileImport method importFile.

public TaskResult importFile(File file) throws Exception {
    try {
        // if target dataset is not defined, create new dataset
        final SourceDataset dataset = targetDataset != null ? targetDataset : datasetMeta(file);
        if (targetDataset == null)
            dataset.setId(-1);
        final BatchDBProcessor<String> batch = new BatchDBProcessor<String>() {

            /**
             */
            private static final long serialVersionUID = -7971761364143510120L;

            @Override
            public Iterator<String> getIterator(IInputState target) throws AmbitException {
                try {
                    File file = ((FileInputState) target).getFile();
                    RDFIteratingReader i = getRDFIterator(file, getReporter().getBaseReference().toString());
                    if (i == null) {
                        IIteratingChemObjectReader ni = getNanoCMLIterator(file, getReporter().getBaseReference().toString());
                        if (ni == null)
                            return super.getIterator(target);
                        else
                            return ni;
                    } else {
                        /*
			     * RDFMetaDatasetIterator datasets = null; try {
			     * datasets = new
			     * RDFMetaDatasetIterator(i.getJenaModel());
			     * datasets
			     * .setBaseReference(getReporter().getBaseReference
			     * ()); while (datasets.hasNext()) { SourceDataset d
			     * = datasets.next(); dataset.setId(d.getId());
			     * dataset.setName(d.getName());
			     * dataset.setTitle(d.getTitle());
			     * dataset.setURL(d.getURL()); } } catch (Exception
			     * x) { x.printStackTrace(); } finally { try {
			     * datasets.close();} catch (Exception x) {} }
			     */
                        return i;
                    }
                } catch (AmbitException x) {
                    throw x;
                } catch (Exception x) {
                    throw new AmbitException(x);
                }
            }

            @Override
            public void onItemProcessed(String input, Object output, IBatchStatistics stats) {
                super.onItemProcessed(input, output, stats);
                if (firstCompoundOnly && (stats.getRecords(RECORDS_STATS.RECORDS_PROCESSED) >= 1)) {
                    cancelled = true;
                    if (output != null)
                        if ((output instanceof ArrayList) && ((ArrayList) output).size() > 0) {
                            if (((ArrayList) output).get(0) instanceof IStructureRecord)
                                recordImported = (IStructureRecord) ((ArrayList) output).get(0);
                        } else if (output instanceof IStructureRecord)
                            recordImported = (IStructureRecord) output;
                }
            }
        };
        batch.setReference(dataset.getReference());
        batch.setConnection(connection);
        final RepositoryWriter writer = new RepositoryWriter();
        writer.setUseExistingStructure(isPropertyOnly());
        writer.setPropertyKey(getMatcher());
        writer.setDataset(dataset);
        final ProcessorsChain<String, IBatchStatistics, IProcessor> chain = new ProcessorsChain<String, IBatchStatistics, IProcessor>();
        chain.add(writer);
        batch.setProcessorChain(chain);
        writer.setConnection(connection);
        FileInputState fin = new FileInputState(file);
        IBatchStatistics stats = batch.process(fin);
        if (firstCompoundOnly) {
            if (recordImported == null)
                throw new Exception("No compound imported");
            if (compoundReporter == null)
                compoundReporter = new ConformerURIReporter("", null, false);
            try {
                batch.close();
            } catch (Exception xx) {
            }
            return new TaskResult(compoundReporter.getURI(recordImported));
        } else {
            ReadDataset q = new ReadDataset();
            q.setValue(dataset);
            QueryExecutor<ReadDataset> x = new QueryExecutor<ReadDataset>();
            x.setConnection(connection);
            ResultSet rs = x.process(q);
            ISourceDataset newDataset = null;
            while (rs.next()) {
                newDataset = q.getObject(rs);
                break;
            }
            x.closeResults(rs);
            x.setConnection(null);
            if (newDataset == null)
                throw new ResourceException(Status.SUCCESS_NO_CONTENT);
            if (reporter == null)
                reporter = new DatasetURIReporter<IQueryRetrieval<ISourceDataset>, ISourceDataset>();
            try {
                batch.close();
            } catch (Exception xx) {
            }
            return new TaskResult(reporter.getURI(newDataset));
        }
    } catch (ResourceException x) {
        throw x;
    } catch (Exception x) {
        throw new ResourceException(new Status(Status.SERVER_ERROR_INTERNAL, x.getMessage()));
    } finally {
        try {
            connection.close();
        } catch (Exception x) {
        }
        connection = null;
    }
}
Also used : RDFIteratingReader(ambit2.rest.dataset.RDFIteratingReader) ArrayList(java.util.ArrayList) RepositoryWriter(ambit2.db.processors.RepositoryWriter) IStructureRecord(ambit2.base.interfaces.IStructureRecord) ISourceDataset(ambit2.base.data.ISourceDataset) ProcessorsChain(net.idea.modbcum.i.processors.ProcessorsChain) IInputState(ambit2.core.io.IInputState) QueryExecutor(ambit2.db.search.QueryExecutor) ResultSet(java.sql.ResultSet) ResourceException(org.restlet.resource.ResourceException) Status(org.restlet.data.Status) IIteratingChemObjectReader(org.openscience.cdk.io.iterator.IIteratingChemObjectReader) IBatchStatistics(net.idea.modbcum.i.batch.IBatchStatistics) IProcessor(net.idea.modbcum.i.processors.IProcessor) BatchDBProcessor(ambit2.db.processors.BatchDBProcessor) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) CDKException(org.openscience.cdk.exception.CDKException) ResourceException(org.restlet.resource.ResourceException) ConformerURIReporter(ambit2.rest.structure.ConformerURIReporter) ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) File(java.io.File) FileInputState(ambit2.core.io.FileInputState) ReadDataset(ambit2.db.update.dataset.ReadDataset) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DatasetURIReporter(ambit2.rest.dataset.DatasetURIReporter)

Example 2 with IInputState

use of ambit2.core.io.IInputState in project ambit-mirror by ideaconsult.

the class FileSelector method actionPerformed.

public void actionPerformed(ActionEvent e) {
    DelimitersPanel accessory = new DelimitersPanel();
    File file = MyIOUtilities.selectFile(JOptionPane.getFrameForComponent(this), "Select file", Preferences.getProperty(Preferences.DEFAULT_DIR), getObject().getSupportedExtensions(), getObject().getSupportedExtDescriptions(), getObject() instanceof IInputState, accessory);
    if (file != null) {
        getObject().setFile(file, accessory.getFormat());
        try {
            if (file.getParent() != null) {
                Preferences.setProperty(Preferences.DEFAULT_DIR, file.getParent());
                Preferences.saveProperties(getClass().getName());
            }
        } catch (Exception x) {
        }
        filename.setText(getObject().getFilename());
        filename.setToolTipText(getObject().getFilename());
    }
}
Also used : DelimitersPanel(ambit2.ui.DelimitersPanel) IInputState(ambit2.core.io.IInputState) File(java.io.File)

Example 3 with IInputState

use of ambit2.core.io.IInputState in project ambit-mirror by ideaconsult.

the class CallableSubstanceImporter method createBatch.

@Override
protected AbstractBatchProcessor createBatch(FileInputState target) throws Exception {
    if (target == null)
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
    final BatchDBProcessor<String> batch = new BatchDBProcessor<String>() {

        /**
         */
        private static final long serialVersionUID = 5712170806359764006L;

        @Override
        public Iterator<String> getIterator(IInputState target) throws AmbitException {
            try {
                IRawReader<IStructureRecord> reader = null;
                File file = ((FileInputState) target).getFile();
                String ext = file.getName().toLowerCase();
                if (ext.endsWith(FileInputState._FILE_TYPE.I5Z_INDEX.getExtension())) {
                    if (writer instanceof DBSubstanceWriter)
                        if (writer instanceof DBSubstanceWriter) {
                            ((DBSubstanceWriter) writer).setSplitRecord(true);
                            ((DBSubstanceWriter) writer).setI5mode(true);
                        }
                    reader = new I5ZReader(file);
                    ((I5ZReader) reader).setQASettings(getQASettings());
                } else if (ext.endsWith(FileInputState._FILE_TYPE.I6Z_INDEX.getExtension())) {
                    if (writer instanceof DBSubstanceWriter)
                        if (writer instanceof DBSubstanceWriter) {
                            ((DBSubstanceWriter) writer).setSplitRecord(true);
                            ((DBSubstanceWriter) writer).setI5mode(true);
                        }
                    reader = new I6ZReader(file);
                    ((I6ZReader) reader).setQASettings(getQASettings());
                } else if (ext.endsWith(FileInputState._FILE_TYPE.CSV_INDEX.getExtension())) {
                    if (writer instanceof DBSubstanceWriter)
                        ((DBSubstanceWriter) writer).setSplitRecord(false);
                    LiteratureEntry reference = new LiteratureEntry(originalname, originalname);
                    reader = new CSV12SubstanceReader(new CSV12Reader(new FileReader(file), reference, "FCSV-"));
                } else if (ext.endsWith(".rdf")) {
                    if (writer instanceof DBSubstanceWriter) {
                        ((DBSubstanceWriter) writer).setSplitRecord(false);
                        ((DBSubstanceWriter) writer).setImportBundles(true);
                    }
                    reader = new NanoWikiRDFReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), null, "RDF/XML");
                } else if (ext.endsWith(".ttl")) {
                    if (writer instanceof DBSubstanceWriter) {
                        ((DBSubstanceWriter) writer).setSplitRecord(false);
                        ((DBSubstanceWriter) writer).setImportBundles(true);
                    }
                    reader = new ENanoMapperRDFReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), "ENM3");
                } else if (FileInputState._FILE_TYPE.XLSX_INDEX.hasExtension(ext) || FileInputState._FILE_TYPE.XLS_INDEX.hasExtension(ext)) {
                    if (configFile == null)
                        throw new AmbitException("XLSX/XLSX file import requires a JSON configuration file");
                    final StructureRecordValidator validator = new StructureRecordValidator(file.getName(), true, "XLSX");
                    reader = new GenericExcelParser(new FileInputStream(file), configFile, FileInputState._FILE_TYPE.XLSX_INDEX.hasExtension(ext)) {

                        public Object next() {
                            Object record = super.next();
                            try {
                                if (record instanceof IStructureRecord)
                                    record = validator.process((IStructureRecord) record);
                            } catch (Exception x) {
                            }
                            return record;
                        }
                    };
                    if (writer instanceof DBSubstanceWriter) {
                        ((DBSubstanceWriter) writer).setSplitRecord(false);
                    /*
							 * ((DBSubstanceWriter) writer)
							 * .setClearComposition(false); ((DBSubstanceWriter)
							 * writer) .setClearMeasurements(false);
							 */
                    }
                } else if (ext.endsWith(".json")) {
                    if (writer instanceof DBSubstanceWriter)
                        ((DBSubstanceWriter) writer).setSplitRecord(false);
                    reader = new SubstanceStudyParser(new InputStreamReader(new FileInputStream(file), "UTF-8")) {

                        protected EffectRecord createEffectRecord(Protocol protocol) {
                            try {
                                I5_ROOT_OBJECTS category = I5_ROOT_OBJECTS.valueOf(protocol.getCategory() + "_SECTION");
                                return category.createEffectRecord();
                            } catch (Exception x) {
                                return super.createEffectRecord(protocol);
                            }
                        }
                    };
                    if (writer instanceof DBSubstanceWriter) {
                        ((DBSubstanceWriter) writer).setClearComposition(false);
                        ((DBSubstanceWriter) writer).setClearMeasurements(false);
                    }
                } else {
                    throw new AmbitException("Unsupported format " + file);
                }
                reader.setErrorHandler(new IChemObjectReaderErrorHandler() {

                    @Override
                    public void handleError(String message, int row, int colStart, int colEnd, Exception exception) {
                    }

                    @Override
                    public void handleError(String message, int row, int colStart, int colEnd) {
                    }

                    @Override
                    public void handleError(String message, Exception exception) {
                    }

                    @Override
                    public void handleError(String message) {
                    }
                });
                return reader;
            } catch (AmbitException x) {
                throw x;
            } catch (Exception x) {
                throw new AmbitException(x);
            }
        }
    };
    return batch;
}
Also used : DBSubstanceWriter(ambit2.db.substance.processor.DBSubstanceWriter) GenericExcelParser(net.enanomapper.parser.GenericExcelParser) LiteratureEntry(ambit2.base.data.LiteratureEntry) StructureRecordValidator(ambit2.base.data.study.StructureRecordValidator) I5_ROOT_OBJECTS(net.idea.i5.io.I5_ROOT_OBJECTS) CSV12Reader(net.idea.loom.nm.csv.CSV12Reader) I5ZReader(net.idea.i5.io.I5ZReader) SubstanceStudyParser(ambit2.core.io.json.SubstanceStudyParser) I6ZReader(net.idea.i6.io.I6ZReader) CSV12SubstanceReader(net.idea.loom.nm.csv.CSV12SubstanceReader) IStructureRecord(ambit2.base.interfaces.IStructureRecord) IChemObjectReaderErrorHandler(org.openscience.cdk.io.IChemObjectReaderErrorHandler) ENanoMapperRDFReader(net.idea.loom.nm.nanowiki.ENanoMapperRDFReader) IInputState(ambit2.core.io.IInputState) ResourceException(org.restlet.resource.ResourceException) FileReader(java.io.FileReader) Protocol(ambit2.base.data.study.Protocol) InputStreamReader(java.io.InputStreamReader) BatchDBProcessor(ambit2.db.processors.BatchDBProcessor) FileInputStream(java.io.FileInputStream) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) EffectRecord(ambit2.base.data.study.EffectRecord) File(java.io.File) FileInputState(ambit2.core.io.FileInputState) NanoWikiRDFReader(net.idea.loom.nm.nanowiki.NanoWikiRDFReader) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 4 with IInputState

use of ambit2.core.io.IInputState in project ambit-mirror by ideaconsult.

the class BatchDBProcessor method getIterator.

public Iterator<ITEMTYPE> getIterator(IInputState target) throws AmbitException {
    if (target instanceof FileInputState)
        try {
            File _file = ((FileInputState) target).getFile();
            if (_file.isDirectory()) {
                FilenameFilter filter = new FilenameFilter() {

                    public boolean accept(File dir, String name) {
                        return !name.startsWith(".");
                    }
                };
                return new RawIteratingFolderReader(_file.listFiles(filter));
            } else {
                InputStream stream = null;
                String filename = _file.getName();
                if (filename.endsWith(_FILE_TYPE.GZ_INDEX.getExtension())) {
                    String uncompressed = filename.replaceAll(_FILE_TYPE.GZ_INDEX.getExtension(), "");
                    try {
                        stream = new GZIPInputStream(new FileInputStream(_file));
                        filename = uncompressed;
                    } catch (IOException x) {
                        throw new AmbitIOException(x);
                    }
                } else
                    stream = new FileInputStream(_file);
                if (FileInputState._FILE_TYPE.SDF_INDEX.hasExtension(filename)) {
                    RawIteratingSDFReader reader = new RawIteratingSDFReader(new InputStreamReader(stream));
                    if (getReference() == null)
                        reader.setReference(LiteratureEntry.getInstance(filename, _file.getAbsolutePath()));
                    else
                        reader.setReference(getReference());
                    return reader;
                } else if (FileInputState._FILE_TYPE.MOL_INDEX.hasExtension(filename)) {
                    RawIteratingMOLReader reader = new RawIteratingMOLReader(new InputStreamReader(stream));
                    if (getReference() == null)
                        reader.setReference(LiteratureEntry.getInstance(filename, _file.getAbsolutePath()));
                    else
                        reader.setReference(getReference());
                    return reader;
                /* TEST and replace the wrapper with this */
                } else if (FileInputState._FILE_TYPE.CSV_INDEX.hasExtension(filename)) {
                    RawIteratingCSVReader reader = new RawIteratingCSVReader(new InputStreamReader(stream), CSVFormat.EXCEL);
                    configureReader(reader, target, _file);
                    return reader;
                } else if (FileInputState._FILE_TYPE.TXT_INDEX.hasExtension(filename)) {
                    RawIteratingCSVReader reader = new RawIteratingCSVReader(new InputStreamReader(stream), CSVFormat.TDF.withCommentMarker('#'));
                    configureReader(reader, target, _file);
                    return reader;
                } else {
                    IIteratingChemObjectReader ir = FileInputState.getReader(stream, filename);
                    if (ir == null)
                        throw new AmbitException("Unsupported format " + filename);
                    else {
                        if (ir instanceof RawIteratingCSVReader) {
                            configureReader(((RawIteratingCSVReader) ir), target, _file);
                        }
                        RawIteratingWrapper reader = new RawIteratingWrapper(ir);
                        if (getReference() == null)
                            reader.setReference(LiteratureEntry.getInstance(filename, _file.getAbsolutePath()));
                        else
                            reader.setReference(getReference());
                        return reader;
                    }
                }
            }
        } catch (IOException x) {
            throw new AmbitIOException(x);
        } catch (Exception x) {
            throw new AmbitException(x);
        }
    else
        throw new AmbitException("Not a file");
}
Also used : RawIteratingCSVReader(ambit2.core.io.RawIteratingCSVReader) RawIteratingSDFReader(ambit2.core.io.RawIteratingSDFReader) InputStreamReader(java.io.InputStreamReader) IIteratingChemObjectReader(org.openscience.cdk.io.iterator.IIteratingChemObjectReader) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RawIteratingFolderReader(ambit2.core.io.RawIteratingFolderReader) AmbitIOException(ambit2.base.exceptions.AmbitIOException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) AmbitIOException(ambit2.base.exceptions.AmbitIOException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) FilenameFilter(java.io.FilenameFilter) AmbitIOException(ambit2.base.exceptions.AmbitIOException) RawIteratingWrapper(ambit2.core.io.RawIteratingWrapper) RawIteratingMOLReader(ambit2.core.io.RawIteratingMOLReader) FileInputState(ambit2.core.io.FileInputState) File(java.io.File) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Aggregations

File (java.io.File)4 FileInputState (ambit2.core.io.FileInputState)3 IInputState (ambit2.core.io.IInputState)3 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)3 IStructureRecord (ambit2.base.interfaces.IStructureRecord)2 BatchDBProcessor (ambit2.db.processors.BatchDBProcessor)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 IIteratingChemObjectReader (org.openscience.cdk.io.iterator.IIteratingChemObjectReader)2 ResourceException (org.restlet.resource.ResourceException)2 ISourceDataset (ambit2.base.data.ISourceDataset)1 LiteratureEntry (ambit2.base.data.LiteratureEntry)1 SourceDataset (ambit2.base.data.SourceDataset)1 EffectRecord (ambit2.base.data.study.EffectRecord)1 Protocol (ambit2.base.data.study.Protocol)1 StructureRecordValidator (ambit2.base.data.study.StructureRecordValidator)1 AmbitIOException (ambit2.base.exceptions.AmbitIOException)1 RawIteratingCSVReader (ambit2.core.io.RawIteratingCSVReader)1 RawIteratingFolderReader (ambit2.core.io.RawIteratingFolderReader)1 RawIteratingMOLReader (ambit2.core.io.RawIteratingMOLReader)1