Search in sources :

Example 1 with SubstanceEndpointsBundle

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

the class StatisticsResource method createQuery.

@Override
protected Q createQuery(Context context, Request request, Response response) throws ResourceException {
    String[] datasetsURI = getParams().getValuesArray(OpenTox.params.dataset_uri.toString());
    Template t = new Template(String.format("%s%s/{%s}", getRequest().getRootRef(), DatasetStructuresResource.dataset, DatasetStructuresResource.datasetKey));
    setStatus(Status.SUCCESS_OK);
    mode = getSearchMode();
    switch(mode) {
        case dataset_intersection:
            {
                QueryCountDatasetIntersection q = null;
                for (int i = 0; i < datasetsURI.length; i++) {
                    if (q == null)
                        q = new QueryCountDatasetIntersection(null);
                    String datasetURI = datasetsURI[i];
                    Map<String, Object> vars = new HashMap<String, Object>();
                    t.parse(datasetURI, vars);
                    if (i == 0)
                        q.setFieldname(vars.get(DatasetStructuresResource.datasetKey).toString());
                    else
                        q.setValue(vars.get(DatasetStructuresResource.datasetKey).toString());
                }
                if (q == null)
                    throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Missing dataset_uri parameters!");
                return (Q) q;
            }
        case structures:
            {
                return (Q) new QueryCount<FACET>(mode.getURL());
            }
        case properties:
            {
                return (Q) new QueryCountProperties(mode.getURL());
            }
        case values:
            {
                return (Q) new QueryCountValues(mode.getURL());
            }
        case dataset:
            {
                return (Q) new QueryCountDataset(mode.getURL());
            }
        case models:
            {
                return (Q) new QueryCountModels(mode.getURL());
            }
        case substances:
            {
                return (Q) new QueryCountSubstances(mode.getURL());
            }
        case substancetypes:
            {
                return (Q) new QueryCountSubstanceTypes(mode.getURL());
            }
        case experiment_endpoints:
            {
                QueryCountEndpoints q = new QueryCountEndpoints(mode.getURL());
                q.setFieldname(getParams().getFirstValue("top"));
                q.setValue(getParams().getFirstValue("category"));
                q.setEndpoint(getParams().getFirstValue("search"));
                return (Q) q;
            }
        case protocol_applications:
            {
                QueryCountProtocolApplications q = new QueryCountProtocolApplications(mode.getURL());
                q.setPageSize(1000);
                // ?bundle_uri=
                // Object bundleURI =
                // OpenTox.params.bundle_uri.getFirstValue(getParams());
                Object bundleURI = getParams().getFirstValue("filterbybundle");
                if (bundleURI != null) {
                    Integer idbundle = getIdBundle(bundleURI, request);
                    q.setBundle(new SubstanceEndpointsBundle(idbundle));
                }
                q.setFieldname(getParams().getFirstValue("topcategory"));
                q.setValue(getParams().getFirstValue("category"));
                return (Q) q;
            }
        case data_availability:
            {
                QueryCountProtocolApplications q = new QueryCountProtocolApplications(mode.getURL(), QueryCountProtocolApplications._mode_related.data_availability);
                q.setPageSize(2000);
                q.setFieldname(getParams().getFirstValue("topcategory"));
                q.setValue(getParams().getFirstValue("category"));
                return (Q) q;
            }
        case study_summary:
            {
                QueryCountProtocolApplications q = new QueryCountProtocolApplications(mode.getURL(), QueryCountProtocolApplications._mode_related.detail);
                q.setPageSize(2000);
                // ?bundle_uri=
                // Object bundleURI =
                // OpenTox.params.bundle_uri.getFirstValue(getParams());
                /*
			 * Object bundleURI = getParams().getFirstValue("filterbybundle");
			 * if (bundleURI!=null) { Integer idbundle = getIdBundle(bundleURI,
			 * request); q.setBundle(new SubstanceEndpointsBundle(idbundle)); }
			 */
                q.setFieldname(getParams().getFirstValue("topcategory"));
                q.setValue(getParams().getFirstValue("category"));
                return (Q) q;
            }
        case interpretation_result:
            {
                QueryCountInterpretationResults q = new QueryCountInterpretationResults(mode.getURL());
                q.setFieldname(getParams().getFirstValue("top"));
                q.setValue(getParams().getFirstValue("category"));
                q.setInterpretation_result(getParams().getFirstValue("search"));
                return (Q) q;
            }
        case chemicals_in_dataset:
            {
                QueryCountChemicalInDataset q = null;
                for (int i = 0; i < datasetsURI.length; i++) {
                    if (q == null)
                        q = new QueryCountChemicalInDataset(mode.getURL());
                    String datasetURI = datasetsURI[i];
                    Map<String, Object> vars = new HashMap<String, Object>();
                    t.parse(datasetURI, vars);
                    q.setFieldname(vars.get(DatasetStructuresResource.datasetKey).toString());
                }
                if (q == null)
                    throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Missing dataset_uri parameters!");
                return (Q) q;
            }
        case bundles:
            {
                return (Q) new QueryCountBundles(mode.getURL());
            }
        default:
            {
                throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
            }
    }
}
Also used : QueryCountDataset(ambit2.db.update.dataset.QueryCountDataset) QueryCountInterpretationResults(ambit2.db.substance.QueryCountInterpretationResults) QueryCountSubstanceTypes(ambit2.db.substance.QueryCountSubstanceTypes) QueryCountDatasetIntersection(ambit2.db.update.dataset.QueryCountDatasetIntersection) QueryCountChemicalInDataset(ambit2.db.update.dataset.QueryCountChemicalInDataset) QueryCountModels(ambit2.db.model.QueryCountModels) QueryCountProtocolApplications(ambit2.db.substance.QueryCountProtocolApplications) Template(org.restlet.routing.Template) QueryCountSubstances(ambit2.db.substance.QueryCountSubstances) SubstanceEndpointsBundle(ambit2.base.data.substance.SubstanceEndpointsBundle) QueryCount(ambit2.db.update.dataset.QueryCount) ResourceException(org.restlet.resource.ResourceException) HashMap(java.util.HashMap) Map(java.util.Map) QueryCountProperties(ambit2.db.update.dataset.QueryCountProperties) QueryCountEndpoints(ambit2.db.substance.QueryCountEndpoints) QueryCountValues(ambit2.db.update.dataset.QueryCountValues) QueryCountBundles(ambit2.db.substance.QueryCountBundles)

Example 2 with SubstanceEndpointsBundle

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

the class StudySearchResource method createQuery.

@Override
protected QueryCountProtocolApplications createQuery(Context context, Request request, Response response) throws ResourceException {
    try {
        Form form = getRequest().getResourceRef().getQueryAsForm();
        mergeDatasets = Boolean.parseBoolean(form.getFirstValue("mergeDatasets"));
    } catch (Exception x) {
        mergeDatasets = false;
    }
    Object bundleURI = OpenTox.params.bundle_uri.getFirstValue(getParams());
    if (bundleURI != null) {
        Integer idbundle = getIdBundle(bundleURI, request);
        bundle = new SubstanceEndpointsBundle(idbundle);
    }
    QueryCountProtocolApplications q = super.createQuery(context, request, response);
    try {
        q.setMode(_mode_related.valueOf(getParams().getFirstValue("selected")));
    } catch (Exception x) {
        q.setMode(_mode_related.endpoints);
    }
    return q;
}
Also used : Form(org.restlet.data.Form) SubstanceEndpointsBundle(ambit2.base.data.substance.SubstanceEndpointsBundle) QueryCountProtocolApplications(ambit2.db.substance.QueryCountProtocolApplications) ResourceException(org.restlet.resource.ResourceException)

Example 3 with SubstanceEndpointsBundle

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

the class UserByURIResource method readBundle.

protected SubstanceEndpointsBundle readBundle(int idbundle) throws ResourceException {
    SubstanceEndpointsBundle dataset = new SubstanceEndpointsBundle();
    dataset.setID(idbundle);
    Connection c = null;
    ResultSet rs = null;
    QueryExecutor xx = null;
    try {
        DBConnection dbc = new DBConnection(getContext(), getAmbitConfigFile());
        c = dbc.getConnection();
        Set<_published_status> status = new TreeSet<_published_status>();
        status.add(_published_status.published);
        ReadBundle read = new ReadBundle(status);
        read.setValue(dataset);
        xx = new QueryExecutor();
        xx.setConnection(c);
        rs = xx.process(read);
        while (rs.next()) {
            dataset = read.getObject(rs);
        }
        return dataset;
    } catch (Exception x) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x.getMessage(), x);
    } finally {
        try {
            rs.close();
        } catch (Exception x) {
        }
        try {
            c.close();
        } catch (Exception x) {
        }
        try {
            xx.close();
        } catch (Exception x) {
        }
    }
}
Also used : DBConnection(net.idea.restnet.db.DBConnection) UpdateBundle._published_status(ambit2.db.update.bundle.UpdateBundle._published_status) ReadBundle(ambit2.db.update.bundle.ReadBundle) SubstanceEndpointsBundle(ambit2.base.data.substance.SubstanceEndpointsBundle) QueryExecutor(ambit2.db.search.QueryExecutor) TreeSet(java.util.TreeSet) Connection(java.sql.Connection) DBConnection(net.idea.restnet.db.DBConnection) ResultSet(java.sql.ResultSet) ResourceException(org.restlet.resource.ResourceException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException)

Example 4 with SubstanceEndpointsBundle

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

the class UserByURIResource method createQuery.

@Override
protected ReadUser createQuery(Context context, Request request, Response response) throws ResourceException {
    if (!Method.GET.equals(request.getMethod()))
        return null;
    check(context, request, response);
    String usersdbname = getContext().getParameters().getFirstValue(AMBITConfig.users_dbname.name());
    Form form = request.getResourceRef().getQueryAsForm();
    Object search_value = null;
    try {
        search_value = form.getFirstValue(param_q);
        if (search_value == null)
            search_value = form.getFirstValue("search");
        if (search_value != null) {
            ReadUser query = new ReadUser();
            DBUser user = new DBUser();
            String s = String.format("^%s", search_value.toString());
            user.setLastname(s);
            user.setFirstname(s);
            query.setValue(user);
            addPublicGroup = true;
            return query;
        }
    } catch (Exception x) {
        search_value = null;
    }
    try {
        search_value = form.getFirstValue(param_bundle_uri);
        Integer idbundle = search_value == null ? null : getIdBundle(search_value, request);
        SubstanceEndpointsBundle bundle = readBundle(idbundle);
        ReadUserByBundleNumber query = new ReadUserByBundleNumber();
        query.setDatabaseName(usersdbname);
        query.setFieldname(bundle.getBundle_number().toString());
        search_value = form.getFirstValue(param_mode);
        if ("W".equals(search_value))
            query.setAllowWrite(true);
        else
            query.setAllowWrite(false);
        return query;
    } catch (Exception x) {
        search_value = null;
    }
    throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
}
Also used : ReadUser(net.idea.restnet.user.db.ReadUser) Form(org.restlet.data.Form) SubstanceEndpointsBundle(ambit2.base.data.substance.SubstanceEndpointsBundle) ReadUserByBundleNumber(ambit2.user.rest.ReadUserByBundleNumber) DBUser(net.idea.restnet.user.DBUser) ResourceException(org.restlet.resource.ResourceException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException)

Example 5 with SubstanceEndpointsBundle

use of ambit2.base.data.substance.SubstanceEndpointsBundle 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)

Aggregations

SubstanceEndpointsBundle (ambit2.base.data.substance.SubstanceEndpointsBundle)84 ResourceException (org.restlet.resource.ResourceException)30 Form (org.restlet.data.Form)27 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)22 SubstanceRecord (ambit2.base.data.SubstanceRecord)21 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)21 Test (org.junit.Test)21 IStructureRecord (ambit2.base.interfaces.IStructureRecord)19 ITable (org.dbunit.dataset.ITable)19 Reference (org.restlet.data.Reference)17 IQueryRetrieval (net.idea.modbcum.i.IQueryRetrieval)16 DbUnitTest (net.idea.restnet.db.test.DbUnitTest)15 TaskResult (net.idea.restnet.i.task.TaskResult)14 StructureRecord (ambit2.base.data.StructureRecord)10 DatasetURIReporter (ambit2.rest.dataset.DatasetURIReporter)10 Connection (java.sql.Connection)7 DBConnection (net.idea.restnet.db.DBConnection)7 ReadBundle (ambit2.db.update.bundle.ReadBundle)5 UpdateBundle._published_status (ambit2.db.update.bundle.UpdateBundle._published_status)5 SubstanceURIReporter (ambit2.rest.substance.SubstanceURIReporter)5