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;
}
}
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));
}
}
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);
}
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);
}
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);
}
Aggregations