Search in sources :

Example 1 with ISourceDataset

use of ambit2.base.data.ISourceDataset in project ambit-mirror by ideaconsult.

the class ChartResource method createImage.

protected BufferedImage createImage() throws ResourceException {
    BufferedImage image = null;
    Connection connection = null;
    try {
        DBConnection dbc = new DBConnection(getContext());
        connection = dbc.getConnection();
        ambit2.base.data.Template profile = new ambit2.base.data.Template();
        ProfileReader reader = new ProfileReader(getRequest().getRootRef(), profile, getApplication().getContext(), getToken(), getRequest().getCookies(), getRequest().getClientInfo() == null ? null : getRequest().getClientInfo().getAgent(), getRequest().getResourceRef().toString());
        reader.setCloseConnection(false);
        reader.setConnection(connection);
        for (String p : property) reader.process(new Reference(p));
        switch(mode) {
            case pie:
                {
                    Iterator<Property> i = profile.getProperties(true);
                    while (i.hasNext()) {
                        PieChartGenerator<ISourceDataset> chart = new PieChartGenerator<ISourceDataset>();
                        chart.setProperty(i.next());
                        chart.setConnection(connection);
                        chart.setLegend(legend);
                        chart.setThumbnail(thumbnail);
                        chart.setWidth(w);
                        chart.setHeight(h);
                        chart.setLogX(logX);
                        chart.setLogY(logY);
                        image = chart.process(dataset);
                        // ChartUtilities.writeImageMap(writer, name, info, useOverLibForToolTips)
                        break;
                    }
                    break;
                }
            case histogram:
                {
                    Iterator<Property> i = profile.getProperties(true);
                    while (i.hasNext()) {
                        HistogramChartGenerator chart = new HistogramChartGenerator();
                        chart.setLogX(logX);
                        chart.setLogY(logY);
                        chart.setMinX(minX);
                        chart.setMaxX(maxX);
                        chart.setPropertyX(i.next());
                        chart.setConnection(connection);
                        chart.setLegend(legend);
                        chart.setThumbnail(thumbnail);
                        chart.setWidth(w);
                        chart.setHeight(h);
                        image = chart.process(dataset);
                        // ChartUtilities.writeImageMap(writer, name, info, useOverLibForToolTips)
                        break;
                    }
                    break;
                }
            case xy:
                {
                    Property[] p = new Property[2];
                    int i = 0;
                    Iterator<Property> it = profile.getProperties(true);
                    while (it.hasNext()) {
                        p[i] = it.next();
                        i++;
                        if (i >= 2)
                            break;
                    }
                    PropertiesChartGenerator chart = new PropertiesChartGenerator();
                    chart.setLogX(logX);
                    chart.setLogY(logY);
                    chart.setThumbnail(thumbnail);
                    chart.setPropertyX(p[0]);
                    chart.setPropertyY(p.length < 2 ? p[0] : p[1]);
                    chart.setConnection(connection);
                    chart.setWidth(w);
                    chart.setHeight(h);
                    chart.setLegend(legend);
                    image = chart.process(dataset);
                    break;
                }
            case bar:
                {
                    Property[] p = new Property[2];
                    int i = 0;
                    Iterator<Property> it = profile.getProperties(true);
                    while (it.hasNext()) {
                        p[i] = it.next();
                        i++;
                        if (i >= 2)
                            break;
                    }
                    if (i == 0) {
                        FingerprintHistogramDataset chart = new FingerprintHistogramDataset();
                        chart.setLogX(logX);
                        chart.setLogY(logY);
                        chart.setConnection(connection);
                        chart.setLegend(legend);
                        chart.setParam(param);
                        chart.setThumbnail(thumbnail);
                        chart.setWidth(w);
                        chart.setHeight(h);
                        image = chart.process(dataset);
                    } else {
                        BarChartGeneratorDataset chart = new BarChartGeneratorDataset();
                        chart.setLogX(logX);
                        chart.setLogY(logY);
                        chart.setPropertyX(p[0]);
                        chart.setPropertyY(p.length < 2 ? p[0] : p[1]);
                        chart.setConnection(connection);
                        chart.setLegend(legend);
                        chart.setThumbnail(thumbnail);
                        chart.setWidth(w);
                        chart.setHeight(h);
                        image = chart.process(dataset);
                    }
                    break;
                }
            default:
                {
                    image = null;
                }
        }
    } catch (Exception x) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x.getMessage(), x);
    } finally {
        try {
            connection.close();
        } catch (Exception x) {
        }
    }
    if (image == null)
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
    return image;
}
Also used : DBConnection(ambit2.rest.DBConnection) ProfileReader(ambit2.rest.property.ProfileReader) Reference(org.restlet.data.Reference) Connection(java.sql.Connection) DBConnection(ambit2.rest.DBConnection) BufferedImage(java.awt.image.BufferedImage) ResourceException(org.restlet.resource.ResourceException) IOException(java.io.IOException) InvalidResourceIDException(ambit2.rest.error.InvalidResourceIDException) Template(org.restlet.routing.Template) PropertiesChartGenerator(ambit2.db.chart.PropertiesChartGenerator) ISourceDataset(ambit2.base.data.ISourceDataset) BarChartGeneratorDataset(ambit2.db.chart.BarChartGeneratorDataset) Iterator(java.util.Iterator) PieChartGenerator(ambit2.db.chart.PieChartGenerator) ResourceException(org.restlet.resource.ResourceException) HistogramChartGenerator(ambit2.db.chart.HistogramChartGenerator) FingerprintHistogramDataset(ambit2.db.chart.FingerprintHistogramDataset)

Example 2 with ISourceDataset

use of ambit2.base.data.ISourceDataset in project ambit-mirror by ideaconsult.

the class ChartResource method getDataset.

protected ISourceDataset getDataset(String uri) throws InvalidResourceIDException {
    Map<String, Object> vars = new HashMap<String, Object>();
    Template template = OpenTox.URI.dataset.getTemplate(getRequest().getRootRef());
    String id = null;
    try {
        template.parse(uri, vars);
        id = vars.get(OpenTox.URI.dataset.getKey()).toString();
    } catch (Exception x) {
        return null;
    }
    if (id != null)
        try {
            Integer idnum = new Integer(Reference.decode(id.toString()));
            SourceDataset dataset = new SourceDataset();
            dataset.setID(idnum);
            return dataset;
        } catch (NumberFormatException x) {
            if (id.toString().startsWith(DatasetStructuresResource.QR_PREFIX)) {
                String key = id.toString().substring(DatasetStructuresResource.QR_PREFIX.length());
                try {
                    ISourceDataset dataset = new StoredQuery();
                    dataset.setID(Integer.parseInt(key.toString()));
                    return dataset;
                } catch (NumberFormatException xx) {
                    throw new InvalidResourceIDException(id);
                }
            }
        } catch (Exception x) {
            throw new InvalidResourceIDException(id);
        }
    return null;
}
Also used : StoredQuery(ambit2.db.search.StoredQuery) ISourceDataset(ambit2.base.data.ISourceDataset) HashMap(java.util.HashMap) ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) InvalidResourceIDException(ambit2.rest.error.InvalidResourceIDException) ResourceException(org.restlet.resource.ResourceException) IOException(java.io.IOException) InvalidResourceIDException(ambit2.rest.error.InvalidResourceIDException) Template(org.restlet.routing.Template)

Example 3 with ISourceDataset

use of ambit2.base.data.ISourceDataset in project ambit-mirror by ideaconsult.

the class MetadatasetResource method createObjectIterator.

@Override
protected RDFObjectIterator<M> createObjectIterator(Representation entity) throws ResourceException {
    RDFMetaDatasetIterator iterator = new RDFMetaDatasetIterator(entity, entity.getMediaType()) {

        @Override
        protected ISourceDataset createRecord() {
            return new SourceDataset();
        }
    };
    iterator.setForceReadRDFLocalObjects(true);
    iterator.setBaseReference(getRequest().getRootRef());
    return iterator;
}
Also used : ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) RDFMetaDatasetIterator(ambit2.rest.rdf.RDFMetaDatasetIterator)

Example 4 with ISourceDataset

use of ambit2.base.data.ISourceDataset in project ambit-mirror by ideaconsult.

the class SimilarityResource method createQuery.

@Override
protected Q createQuery(Context context, Request request, Response response) throws ResourceException {
    Form form = getResourceRef(getRequest()).getQueryAsForm();
    try {
        Object bundleURI = OpenTox.params.bundle_uri.getFirstValue(form);
        Integer idbundle = bundleURI == null ? null : getIdBundle(bundleURI, request);
        SubstanceEndpointsBundle bundle = new SubstanceEndpointsBundle(idbundle);
        bundles = new SubstanceEndpointsBundle[1];
        bundles[0] = bundle;
    } catch (Exception x) {
        bundles = null;
    }
    try {
        includeMol = "true".equals(form.getFirstValue("mol"));
    } catch (Exception x) {
        includeMol = false;
    }
    folders = form.getValuesArray("folder");
    filterBySubstance = false;
    try {
        String filter = form.getFirstValue("filterBySubstance");
        if (filter != null) {
            filter = filter.toLowerCase();
            filterBySubstance = "yes".equals(filter) || "on".equals(filter) || "true".equals(filter);
        }
    } catch (Exception x) {
        filterBySubstance = false;
    }
    QueryAtomEnvironment.q_modifier ae = null;
    try {
        String filter = form.getFirstValue("mode");
        if (filter != null) {
            filter = filter.toLowerCase();
            ae = QueryAtomEnvironment.q_modifier.valueOf(filter.replace("ae", ""));
        }
    } catch (Exception x) {
        ae = null;
    }
    mol = getMolecule(form);
    if ((mol == null) || (mol.getAtomCount() == 0))
        throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Empty molecule");
    threshold = 0.0;
    try {
        threshold = new Double(Reference.decode(form.getFirstValue("threshold")));
    } catch (Exception x) {
        threshold = 0.9;
    }
    QuerySimilarity q = null;
    if (ae != null) {
        q = new QueryAtomEnvironment(ae);
        try {
            q.setValue(getAE(mol));
        } catch (Exception x) {
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x);
        }
    } else
        try {
            q = new QuerySimilarityBitset();
            q.setValue(getBitset(mol));
        } catch (Exception x) {
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x);
        }
    q.setChemicalsOnly(true);
    q.setThreshold(threshold);
    q.setCondition(NumberCondition.getInstance(">"));
    q.setName("Similarity");
    try {
        q.setForceOrdering(((AmbitApplication) getApplication()).isSimilarityOrder());
    } catch (Exception x) {
        q.setForceOrdering(true);
    }
    try {
        if (filterBySubstance) {
            ChemicalBySubstanceRelation qa = new ChemicalBySubstanceRelation();
            QueryCombinedStructure qc = new QueryCombinedStructure();
            qc.add(q);
            qc.setChemicalsOnly(true);
            qc.setScope(qa);
            setTemplate(createTemplate(context, request, response));
            setGroupProperties(context, request, response);
            return (Q) qc;
        } else if ((folders != null) && (folders.length > 0)) {
            ChemicalByQueryFolder qa = new ChemicalByQueryFolder(folders);
            QueryCombinedStructure qc = new QueryCombinedStructure();
            qc.add(q);
            qc.setChemicalsOnly(true);
            qc.setScope(qa);
            setTemplate(createTemplate(context, request, response));
            setGroupProperties(context, request, response);
            return (Q) qc;
        } else {
            Object datasetURI = OpenTox.params.dataset_uri.getFirstValue(form);
            ISourceDataset srcdataset = null;
            if (datasetURI != null)
                try {
                    srcdataset = getDataset(datasetURI.toString());
                    QueryCombinedStructure qc = new QueryCombinedStructure();
                    qc.add(q);
                    qc.setChemicalsOnly(true);
                    if (srcdataset instanceof SourceDataset) {
                        ChemicalByDataset cd = new ChemicalByDataset(new Integer(srcdataset.getID()));
                        qc.setScope(cd);
                        setTemplate(createTemplate(context, request, response));
                        setGroupProperties(context, request, response);
                        return (Q) qc;
                    } else {
                    // TODO, resort to all db
                    }
                } catch (Exception x) {
                    srcdataset = null;
                }
            QueryCombinedStructure qc = null;
            try {
                this.dataset_id = Reference.decode(getRequest().getAttributes().get(DatasetResource.datasetKey).toString());
                qc = new QueryCombinedStructure();
                qc.add(q);
                qc.setChemicalsOnly(true);
                ChemicalByDataset cd = new ChemicalByDataset(new Integer(dataset_id));
                qc.setScope(cd);
                setTemplate(createTemplate(context, request, response));
                setGroupProperties(context, request, response);
                return (Q) qc;
            } catch (Exception x) {
                setTemplate(createTemplate(context, request, response));
                setGroupProperties(context, request, response);
                return (Q) q;
            }
        }
    } catch (Exception x) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x);
    }
}
Also used : ChemicalByQueryFolder(ambit2.db.search.structure.ChemicalByQueryFolder) QuerySimilarityBitset(ambit2.db.search.structure.QuerySimilarityBitset) Form(org.restlet.data.Form) ChemicalBySubstanceRelation(ambit2.db.substance.relation.ChemicalBySubstanceRelation) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) InvalidResourceIDException(ambit2.rest.error.InvalidResourceIDException) QueryAtomEnvironment(ambit2.db.search.structure.QueryAtomEnvironment) ISourceDataset(ambit2.base.data.ISourceDataset) ChemicalByDataset(ambit2.db.update.structure.ChemicalByDataset) SubstanceEndpointsBundle(ambit2.base.data.substance.SubstanceEndpointsBundle) ResourceException(org.restlet.resource.ResourceException) ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) QuerySimilarity(ambit2.db.search.structure.QuerySimilarity) QueryCombinedStructure(ambit2.db.search.structure.QueryCombinedStructure)

Example 5 with ISourceDataset

use of ambit2.base.data.ISourceDataset in project ambit-mirror by ideaconsult.

the class StructureRelationJSONReporter method header.

@Override
public void header(Writer output, Q query) {
    try {
        output.write("{\n");
        Reference datasetURI = baseReference.clone();
        datasetURI.addSegment(OpenTox.URI.dataset.name());
        Object dataset = (query instanceof AbstractQuery) ? ((AbstractQuery) query).getValue() : null;
        if (dataset instanceof IStoredQuery)
            datasetURI.addSegment(String.format("R%d", ((IStoredQuery) dataset).getID()));
        else if (dataset instanceof SourceDataset)
            datasetURI.addSegment(Integer.toString(((ISourceDataset) dataset).getID()));
        output.write(String.format("\"%s\":\"%s\",\n", jsonFeature.datasetURI.jsonname(), datasetURI));
        output.write("\"links\":[");
    } catch (Exception x) {
        x.printStackTrace();
    }
}
Also used : IStoredQuery(ambit2.db.search.IStoredQuery) ISourceDataset(ambit2.base.data.ISourceDataset) Reference(org.restlet.data.Reference) AbstractQuery(ambit2.db.search.AbstractQuery) ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException)

Aggregations

ISourceDataset (ambit2.base.data.ISourceDataset)28 SourceDataset (ambit2.base.data.SourceDataset)16 ResourceException (org.restlet.resource.ResourceException)12 InvalidResourceIDException (ambit2.rest.error.InvalidResourceIDException)8 IStructureRecord (ambit2.base.interfaces.IStructureRecord)6 IStoredQuery (ambit2.db.search.IStoredQuery)6 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)6 Test (org.junit.Test)6 QueryExecutor (ambit2.db.search.QueryExecutor)5 StoredQuery (ambit2.db.search.StoredQuery)5 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)5 ITable (org.dbunit.dataset.ITable)5 RetrieveDatasets (ambit2.db.readers.RetrieveDatasets)4 ResultSet (java.sql.ResultSet)4 Form (org.restlet.data.Form)4 Reference (org.restlet.data.Reference)4 Property (ambit2.base.data.Property)3 StructureRecord (ambit2.base.data.StructureRecord)3 QueryCombinedStructure (ambit2.db.search.structure.QueryCombinedStructure)3 Template (org.restlet.routing.Template)3