Search in sources :

Example 1 with IIteratingChemObjectReader

use of org.openscience.cdk.io.iterator.IIteratingChemObjectReader 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 IIteratingChemObjectReader

use of org.openscience.cdk.io.iterator.IIteratingChemObjectReader in project ambit-mirror by ideaconsult.

the class FileInputState method getI5DReader.

public static IIteratingChemObjectReader getI5DReader(File file) throws FileNotFoundException, AmbitIOException, CDKException {
    try {
        Class clazz = Class.forName("net.idea.i5.io.I5DReader");
        Constructor constructor = clazz.getConstructor(File.class);
        return (IIteratingChemObjectReader) constructor.newInstance(file);
    } catch (Exception x) {
        return new I5ReaderSimple(new FileReader(file));
    }
}
Also used : IIteratingChemObjectReader(org.openscience.cdk.io.iterator.IIteratingChemObjectReader) Constructor(java.lang.reflect.Constructor) FileReader(java.io.FileReader) AmbitIOException(ambit2.base.exceptions.AmbitIOException) CDKException(org.openscience.cdk.exception.CDKException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with IIteratingChemObjectReader

use of org.openscience.cdk.io.iterator.IIteratingChemObjectReader in project ambit-mirror by ideaconsult.

the class RawIteratingFolderReader method getItemReader.

protected IRawReader<IStructureRecord> getItemReader(int index) throws Exception {
    String name = files[index].getName().toLowerCase();
    if (name.endsWith(FileInputState._FILE_TYPE.SDF_INDEX.getExtension())) {
        RawIteratingSDFReader r = new RawIteratingSDFReader(new FileReader(files[index]));
        r.setReference(LiteratureEntry.getInstance(files[index].getName(), "file:///" + files[index].getAbsolutePath()));
        return (IRawReader<IStructureRecord>) r;
    } else if (name.endsWith(FileInputState._FILE_TYPE.MOL_INDEX.getExtension())) {
        RawIteratingMOLReader r = new RawIteratingMOLReader(new FileReader(files[index]));
        r.setReference(LiteratureEntry.getInstance(files[index].getName(), "file:///" + files[index].getAbsolutePath()));
        return (IRawReader<IStructureRecord>) r;
    } else if (name.endsWith(FileInputState._FILE_TYPE.I5D_INDEX.getExtension())) {
        IIteratingChemObjectReader r = FileInputState.getI5DReader(files[index]);
        if (r instanceof ICiteable) {
            ((ICiteable) r).setReference(LiteratureEntry.getI5UUIDReference());
        }
        return (IRawReader<IStructureRecord>) r;
    } else if (name.endsWith(FileInputState._FILE_TYPE.ZIP_INDEX.getExtension())) {
        return new ZipReader(files[index]);
    } else if (name.endsWith(FileInputState._FILE_TYPE.GZ_INDEX.getExtension())) {
        // assuming gzipped sdf only...
        InputStreamReader reader = new InputStreamReader(new GZIPInputStream(new FileInputStream(files[index])));
        return new RawIteratingSDFReader(reader);
    } else
        throw new Exception("Unsupported format " + name);
}
Also used : InputStreamReader(java.io.InputStreamReader) IIteratingChemObjectReader(org.openscience.cdk.io.iterator.IIteratingChemObjectReader) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) IStructureRecord(ambit2.base.interfaces.IStructureRecord) ICiteable(ambit2.base.interfaces.ICiteable) FileReader(java.io.FileReader)

Example 4 with IIteratingChemObjectReader

use of org.openscience.cdk.io.iterator.IIteratingChemObjectReader in project ambit-mirror by ideaconsult.

the class I5ParserTest method testi5z.

@Test
public void testi5z() throws Exception {
    URL url = getClass().getClassLoader().getResource("ambit2/core/data/i5z/RefSub_030913110311.i5z");
    IIteratingChemObjectReader ireader = FileInputState.getReader(new File(url.getFile()));
    ireader.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) {
            exception.printStackTrace();
        }

        @Override
        public void handleError(String message) {
        }
    });
    int count = 0;
    int foundCas = 0;
    int foundName = 0;
    RawIteratingFolderReader reader = (RawIteratingFolderReader) ireader;
    while (reader.hasNext()) {
        IStructureRecord record = reader.nextRecord();
        /*
			for (Property p : record.getProperties())
				System.out.println(String.format("%s [%s] = %s",p.getName(),p.getReference().getTitle(),record.getProperty(p)));
			System.out.println();
			*/
        count++;
        for (Property p : record.getRecordProperties()) {
            foundCas += record.getRecordProperty(p).equals("59-87-0") ? 1 : 0;
            foundName += record.getRecordProperty(p).equals("5-nitro-2-furaldehyde semicarbazone") ? 1 : 0;
            System.out.println(p.getName() + " = " + record.getRecordProperty(p));
        }
    // Assert.assertNotNull(record.getSmiles());
    // Assert.assertNotNull(record.getInchi());
    // Assert.assertNotNull(record.getProperty(Property.getI5UUIDInstance()));
    }
    reader.close();
    Assert.assertEquals(10, count);
    Assert.assertEquals(1, foundCas);
    Assert.assertEquals(1, foundName);
}
Also used : IChemObjectReaderErrorHandler(org.openscience.cdk.io.IChemObjectReaderErrorHandler) IStructureRecord(ambit2.base.interfaces.IStructureRecord) IIteratingChemObjectReader(org.openscience.cdk.io.iterator.IIteratingChemObjectReader) RawIteratingFolderReader(ambit2.core.io.RawIteratingFolderReader) File(java.io.File) Property(ambit2.base.data.Property) URL(java.net.URL) Test(org.junit.Test)

Example 5 with IIteratingChemObjectReader

use of org.openscience.cdk.io.iterator.IIteratingChemObjectReader in project ambit-mirror by ideaconsult.

the class MDLV2000ReaderExtendedTest method testMol_without_Mend.

@Test
public void testMol_without_Mend() throws Exception {
    IIteratingChemObjectReader reader = FileInputState.getReader(getClass().getClassLoader().getResourceAsStream("ambit2/core/data/mdl/test_no_mend.mol"), "test_no_mend.mol");
    int count = 0;
    while (reader.hasNext()) {
        Object o = reader.next();
        Assert.assertTrue(o instanceof IAtomContainer);
        Assert.assertEquals(23, ((IAtomContainer) o).getAtomCount());
        count++;
    }
    reader.close();
    Assert.assertEquals(1, count);
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IIteratingChemObjectReader(org.openscience.cdk.io.iterator.IIteratingChemObjectReader) IChemObject(org.openscience.cdk.interfaces.IChemObject) Test(org.junit.Test)

Aggregations

IIteratingChemObjectReader (org.openscience.cdk.io.iterator.IIteratingChemObjectReader)35 Test (org.junit.Test)25 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)15 InputStream (java.io.InputStream)12 IStructureRecord (ambit2.base.interfaces.IStructureRecord)10 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)8 ITable (org.dbunit.dataset.ITable)8 File (java.io.File)6 InputStreamReader (java.io.InputStreamReader)6 AmbitIOException (ambit2.base.exceptions.AmbitIOException)4 RawIteratingSDFReader (ambit2.core.io.RawIteratingSDFReader)4 RawIteratingWrapper (ambit2.core.io.RawIteratingWrapper)4 CDKException (org.openscience.cdk.exception.CDKException)4 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 DescriptorValue (org.openscience.cdk.qsar.DescriptorValue)3 Property (ambit2.base.data.Property)2 FileInputState (ambit2.core.io.FileInputState)2 IRawReader (ambit2.core.io.IRawReader)2 RawIteratingFolderReader (ambit2.core.io.RawIteratingFolderReader)2