Search in sources :

Example 1 with IChemObjectReaderErrorHandler

use of org.openscience.cdk.io.IChemObjectReaderErrorHandler 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 2 with IChemObjectReaderErrorHandler

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

the class DataSet method getReader.

public static IIteratingChemObjectReader<IAtomContainer> getReader(InputStream in, String extension) throws CDKException, AmbitIOException {
    FileInputState instate = new FileInputState();
    IIteratingChemObjectReader<IAtomContainer> reader;
    if (extension.endsWith(FileInputState._FILE_TYPE.SDF_INDEX.getExtension())) {
        reader = new InteractiveIteratingMDLReader(in, SilentChemObjectBuilder.getInstance());
        ((InteractiveIteratingMDLReader) reader).setSkip(true);
    } else
        reader = instate.getReader(in, extension);
    reader.setReaderMode(Mode.RELAXED);
    reader.setErrorHandler(new IChemObjectReaderErrorHandler() {

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

        @Override
        public void handleError(String message, int row, int colStart, int colEnd) {
            System.out.println(message);
        }

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

        @Override
        public void handleError(String message) {
            System.out.println(message);
        }
    });
    return reader;
}
Also used : IChemObjectReaderErrorHandler(org.openscience.cdk.io.IChemObjectReaderErrorHandler) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InteractiveIteratingMDLReader(ambit2.core.io.InteractiveIteratingMDLReader) FileInputState(ambit2.core.io.FileInputState) AmbitIOException(ambit2.base.exceptions.AmbitIOException) CDKException(org.openscience.cdk.exception.CDKException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with IChemObjectReaderErrorHandler

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

the class SLNCli method getReader.

public IIteratingChemObjectReader<IAtomContainer> getReader(InputStream in, String extension) throws CDKException, AmbitIOException {
    FileInputState instate = new FileInputState();
    IIteratingChemObjectReader<IAtomContainer> reader;
    if (extension.endsWith(FileInputState._FILE_TYPE.SDF_INDEX.getExtension())) {
        reader = new InteractiveIteratingMDLReader(in, SilentChemObjectBuilder.getInstance());
        ((InteractiveIteratingMDLReader) reader).setSkip(true);
    } else
        reader = instate.getReader(in, extension);
    reader.setReaderMode(Mode.RELAXED);
    reader.setErrorHandler(new IChemObjectReaderErrorHandler() {

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

        @Override
        public void handleError(String message, int row, int colStart, int colEnd) {
            System.out.println(message);
        }

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

        @Override
        public void handleError(String message) {
            System.out.println(message);
        }
    });
    return reader;
}
Also used : IChemObjectReaderErrorHandler(org.openscience.cdk.io.IChemObjectReaderErrorHandler) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InteractiveIteratingMDLReader(ambit2.core.io.InteractiveIteratingMDLReader) FileInputState(ambit2.core.io.FileInputState) AmbitIOException(ambit2.base.exceptions.AmbitIOException) CDKException(org.openscience.cdk.exception.CDKException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with IChemObjectReaderErrorHandler

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

the class CallableSubstanceI5Query method getReader.

private IZReader getReader(File i5z) throws Exception {
    IChemObjectReaderErrorHandler errhandler = new IChemObjectReaderErrorHandler() {

        @Override
        public void handleError(String message, int row, int colStart, int colEnd, Exception exception) {
            localLogger.log(Level.WARNING, String.format("%s [row %d colStart %d colEnd %d] %s", message, row, colStart, colEnd, exception.getMessage()));
        }

        @Override
        public void handleError(String message, int row, int colStart, int colEnd) {
            localLogger.log(Level.WARNING, String.format("%s [row %d colStart %d colEnd %d]", message, row, colStart, colEnd));
        }

        @Override
        public void handleError(String message, Exception exception) {
            localLogger.log(Level.WARNING, exception.getMessage());
        }

        @Override
        public void handleError(String message) {
            localLogger.log(Level.WARNING, message);
        }
    };
    IZReader reader = iuclidversion.getReader(i5z, errhandler);
    reader.setErrorHandler(errhandler);
    return reader;
}
Also used : IChemObjectReaderErrorHandler(org.openscience.cdk.io.IChemObjectReaderErrorHandler) IZReader(net.idea.i5.io.IZReader) ResourceException(org.restlet.resource.ResourceException) ImportSubstanceException(ambit2.rest.exception.ImportSubstanceException) AmbitIOException(ambit2.base.exceptions.AmbitIOException)

Example 5 with IChemObjectReaderErrorHandler

use of org.openscience.cdk.io.IChemObjectReaderErrorHandler 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)

Aggregations

IChemObjectReaderErrorHandler (org.openscience.cdk.io.IChemObjectReaderErrorHandler)8 AmbitIOException (ambit2.base.exceptions.AmbitIOException)6 FileInputState (ambit2.core.io.FileInputState)6 InteractiveIteratingMDLReader (ambit2.core.io.InteractiveIteratingMDLReader)5 FileNotFoundException (java.io.FileNotFoundException)5 CDKException (org.openscience.cdk.exception.CDKException)5 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)5 IStructureRecord (ambit2.base.interfaces.IStructureRecord)2 File (java.io.File)2 ResourceException (org.restlet.resource.ResourceException)2 LiteratureEntry (ambit2.base.data.LiteratureEntry)1 Property (ambit2.base.data.Property)1 EffectRecord (ambit2.base.data.study.EffectRecord)1 Protocol (ambit2.base.data.study.Protocol)1 StructureRecordValidator (ambit2.base.data.study.StructureRecordValidator)1 IInputState (ambit2.core.io.IInputState)1 RawIteratingFolderReader (ambit2.core.io.RawIteratingFolderReader)1 SubstanceStudyParser (ambit2.core.io.json.SubstanceStudyParser)1 BatchDBProcessor (ambit2.db.processors.BatchDBProcessor)1 DBSubstanceWriter (ambit2.db.substance.processor.DBSubstanceWriter)1