Search in sources :

Example 11 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException in project ambit-mirror by ideaconsult.

the class AssayTemplateEntryJSONReporter method processItem.

@Override
public Object processItem(TR item) throws AmbitException {
    try {
        Writer writer = getOutput();
        if (comma != null)
            writer.write(comma);
        writer.write(item.toJSON());
        comma = ",";
    } catch (Exception x) {
        logger.log(java.util.logging.Level.SEVERE, x.getMessage(), x);
    }
    return item;
}
Also used : Writer(java.io.Writer) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 12 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException 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 13 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException in project ambit-mirror by ideaconsult.

the class PropertyAnnotationRDFReporter method annotation2RDF.

public static void annotation2RDF(PropertyAnnotation item, OntModel jenaModel, Individual feature, String rootReference) throws AmbitException {
    Property predicate = null;
    if (item.getPredicate().startsWith("http")) {
        // there is a chance this
        // is an URI
        predicate = jenaModel.createProperty(item.getPredicate());
    } else {
        predicate = jenaModel.createProperty(String.format("http://www.opentox.org/api/1.1#%s", Reference.encode(item.getPredicate().replace(" ", "_"))));
    }
    String object = item.getObject().toString();
    if (item.getType().startsWith("^^")) {
        // xsd:string.
        try {
            // ot:ToxicCategory a rdfs:Datatype; rdfs:subClassOf xsd:string.
            String ctype = item.getType().replace("^^", "");
            Resource rdfDataType = jenaModel.createResource(OT.NS + ctype);
            jenaModel.add(rdfDataType, RDF.type, RDFS.Datatype);
            feature.addProperty(predicate, jenaModel.createTypedLiteral(object, rdfDataType.getURI()));
            try {
                ICategory.CategoryType.valueOf(ctype);
                jenaModel.add(rdfDataType, RDFS.subClassOf, jenaModel.getResource(OT.NS + "ToxicityCategory"));
            } catch (Exception x) {
                // x.printStackTrace();
                jenaModel.add(rdfDataType, RDFS.subClassOf, XSD.xstring);
            }
            return;
        } catch (Exception x) {
            x.printStackTrace();
        }
    // fallback to string
    } else if (item.getType().equals(OT.OTClass.ModelConfidenceFeature)) {
        feature.addOntClass(OT.OTClass.ModelConfidenceFeature.getOntClass(jenaModel));
        if (!object.startsWith("http"))
            object = String.format("%s%s", rootReference, object);
    }
    feature.addProperty(predicate, object);
}
Also used : Resource(com.hp.hpl.jena.rdf.model.Resource) Property(com.hp.hpl.jena.rdf.model.Property) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException)

Example 14 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException in project ambit-mirror by ideaconsult.

the class SubstanceCompositionJSONReporter method processItem.

/**
 * <pre>
 * 		{"source":0,	"target":2,	"value":0.28,	"value2":0.82},
 * </pre>
 */
@Override
public Object processItem(CompositionRelation item) throws AmbitException {
    try {
        if (item == null)
            return null;
        if (item.getFirstStructure() == null)
            return null;
        if (item.getSecondStructure() == null)
            return null;
        if (comma != null)
            getOutput().write(comma);
        StringWriter w = new StringWriter();
        cmpReporter.setOutput(w);
        cmpReporter.setComma(null);
        cmpReporter.processItem(item.getSecondStructure());
        getOutput().write(item.toJSON(substanceReporter.getURI(item.getFirstStructure()), w.toString()));
        comma = ",";
    } catch (Exception x) {
        logger.log(Level.WARNING, x.getMessage(), x);
    }
    return item;
}
Also used : StringWriter(java.io.StringWriter) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException)

Example 15 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException in project ambit-mirror by ideaconsult.

the class CallableFingerprintsModelCreator method createProcessors.

protected ProcessorsChain<IStructureRecord, IBatchStatistics, IProcessor> createProcessors() throws Exception {
    ProcessorsChain<IStructureRecord, IBatchStatistics, IProcessor> p1 = new ProcessorsChain<IStructureRecord, IBatchStatistics, IProcessor>();
    p1.add(new ProcessorStructureRetrieval());
    p1.add(new MoleculeReader());
    p1.add(new FingerprintGenerator(new Fingerprinter()));
    p1.add(new DefaultAmbitProcessor<BitSet, BitSet>() {

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

        public BitSet process(BitSet target) throws AmbitException {
            builder.getTrainingData().add(target);
            return target;
        }
    });
    p1.setAbortOnError(true);
    return p1;
}
Also used : IBatchStatistics(net.idea.modbcum.i.batch.IBatchStatistics) BitSet(java.util.BitSet) IProcessor(net.idea.modbcum.i.processors.IProcessor) FingerprintGenerator(ambit2.core.processors.structure.FingerprintGenerator) IStructureRecord(ambit2.base.interfaces.IStructureRecord) MoleculeReader(ambit2.core.processors.structure.MoleculeReader) Fingerprinter(org.openscience.cdk.fingerprint.Fingerprinter) ProcessorsChain(net.idea.modbcum.i.processors.ProcessorsChain) ProcessorStructureRetrieval(ambit2.db.processors.ProcessorStructureRetrieval) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Aggregations

AmbitException (net.idea.modbcum.i.exceptions.AmbitException)378 ArrayList (java.util.ArrayList)119 QueryParam (net.idea.modbcum.i.query.QueryParam)109 SQLException (java.sql.SQLException)85 Property (ambit2.base.data.Property)68 DbAmbitException (net.idea.modbcum.i.exceptions.DbAmbitException)56 IStructureRecord (ambit2.base.interfaces.IStructureRecord)48 ResourceException (org.restlet.resource.ResourceException)43 IOException (java.io.IOException)35 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)35 LiteratureEntry (ambit2.base.data.LiteratureEntry)21 StructureRecord (ambit2.base.data.StructureRecord)19 ResultSet (java.sql.ResultSet)19 SubstanceRecord (ambit2.base.data.SubstanceRecord)14 Template (ambit2.base.data.Template)13 ModelQueryResults (ambit2.core.data.model.ModelQueryResults)12 Form (org.restlet.data.Form)12 PredictedVarsTemplate (ambit2.base.data.PredictedVarsTemplate)11 StringWriter (java.io.StringWriter)11 OperationNotSupportedException (javax.naming.OperationNotSupportedException)11