Search in sources :

Example 1 with RawIteratingMOLReader

use of ambit2.core.io.RawIteratingMOLReader 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 2 with RawIteratingMOLReader

use of ambit2.core.io.RawIteratingMOLReader 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

FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 IIteratingChemObjectReader (org.openscience.cdk.io.iterator.IIteratingChemObjectReader)2 AmbitIOException (ambit2.base.exceptions.AmbitIOException)1 ICiteable (ambit2.base.interfaces.ICiteable)1 IStructureRecord (ambit2.base.interfaces.IStructureRecord)1 FileInputState (ambit2.core.io.FileInputState)1 RawIteratingCSVReader (ambit2.core.io.RawIteratingCSVReader)1 RawIteratingFolderReader (ambit2.core.io.RawIteratingFolderReader)1 RawIteratingMOLReader (ambit2.core.io.RawIteratingMOLReader)1 RawIteratingSDFReader (ambit2.core.io.RawIteratingSDFReader)1 RawIteratingWrapper (ambit2.core.io.RawIteratingWrapper)1 File (java.io.File)1 FileReader (java.io.FileReader)1 FilenameFilter (java.io.FilenameFilter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)1