Search in sources :

Example 1 with ChemicalByDataset

use of ambit2.db.update.structure.ChemicalByDataset 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 2 with ChemicalByDataset

use of ambit2.db.update.structure.ChemicalByDataset in project ambit-mirror by ideaconsult.

the class DatasetResource method createQueryIntersection.

protected Q createQueryIntersection(Context context, Request request, Response response) throws ResourceException {
    Form form = getResourceRef(request).getQueryAsForm();
    String[] datasetsURI = form.getValuesArray(dataset_intersection_uri);
    if ((datasetsURI != null) && (datasetsURI.length > 0)) {
        QueryCombinedStructure qc = new QueryCombinedStructure() {

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

            @Override
            protected String getMainSQL() {
                return "select idchemical from chemicals\n";
            }

            @Override
            protected String groupBy() {
                return "";
            }
        };
        qc.setChemicalsOnly(true);
        qc.setCombine_as_and(true);
        try {
            ChemicalByDataset cd = new ChemicalByDataset(new Integer(getRequest().getAttributes().get(datasetKey).toString()));
            qc.add(cd);
        } catch (Exception x) {
        }
        Template t = new Template(String.format("%s%s/{%s}", getRequest().getRootRef(), DatasetStructuresResource.dataset, DatasetStructuresResource.datasetKey));
        for (String datasetURI : datasetsURI) {
            Map<String, Object> vars = new HashMap<String, Object>();
            t.parse(datasetURI, vars);
            try {
                qc.add(new ChemicalByDataset(new Integer(vars.get(DatasetStructuresResource.datasetKey).toString())));
            } catch (Exception x) {
            }
        }
        return (Q) qc;
    }
    return null;
}
Also used : ChemicalByDataset(ambit2.db.update.structure.ChemicalByDataset) Form(org.restlet.data.Form) HashMap(java.util.HashMap) ResourceException(org.restlet.resource.ResourceException) QueryCombinedStructure(ambit2.db.search.structure.QueryCombinedStructure) Template(org.restlet.routing.Template)

Example 3 with ChemicalByDataset

use of ambit2.db.update.structure.ChemicalByDataset in project ambit-mirror by ideaconsult.

the class DatasetResource method createQueryComplement.

/**
 * Finds compounds which are in the dataset {dataset_id} , but not in the
 * datasets , given in "complement" query Two sets can also be "subtracted".
 * The relative complement of A in B (also called the set theoretic
 * difference of B and A), denoted by \A, or (B-A) is the set of all
 * elements which are members of B, but not members of A
 *
 * @param context
 * @param request
 * @param response
 * @return
 * @throws ResourceException
 */
protected Q createQueryComplement(Context context, Request request, Response response) throws ResourceException {
    Form form = getResourceRef(request).getQueryAsForm();
    String[] datasetsURI = form.getValuesArray(dataset_complement_uri);
    if ((datasetsURI != null) && (datasetsURI.length > 0)) {
        QueryComplement qc = new QueryComplement();
        qc.setChemicalsOnly(true);
        try {
            ChemicalByDataset cd = new ChemicalByDataset(new Integer(getRequest().getAttributes().get(datasetKey).toString()));
            qc.setScope(cd);
        } catch (Exception x) {
        }
        Template t = new Template(String.format("%s%s/{%s}", getRequest().getRootRef(), DatasetStructuresResource.dataset, DatasetStructuresResource.datasetKey));
        for (String datasetURI : datasetsURI) {
            Map<String, Object> vars = new HashMap<String, Object>();
            t.parse(datasetURI, vars);
            try {
                qc.add(new ChemicalByDataset(new Integer(vars.get(DatasetStructuresResource.datasetKey).toString())));
            } catch (Exception x) {
            }
        }
        return (Q) qc;
    }
    return null;
}
Also used : ChemicalByDataset(ambit2.db.update.structure.ChemicalByDataset) Form(org.restlet.data.Form) QueryComplement(ambit2.db.search.structure.QueryComplement) HashMap(java.util.HashMap) ResourceException(org.restlet.resource.ResourceException) Template(org.restlet.routing.Template)

Aggregations

ChemicalByDataset (ambit2.db.update.structure.ChemicalByDataset)3 Form (org.restlet.data.Form)3 ResourceException (org.restlet.resource.ResourceException)3 QueryCombinedStructure (ambit2.db.search.structure.QueryCombinedStructure)2 HashMap (java.util.HashMap)2 Template (org.restlet.routing.Template)2 ISourceDataset (ambit2.base.data.ISourceDataset)1 SourceDataset (ambit2.base.data.SourceDataset)1 SubstanceEndpointsBundle (ambit2.base.data.substance.SubstanceEndpointsBundle)1 ChemicalByQueryFolder (ambit2.db.search.structure.ChemicalByQueryFolder)1 QueryAtomEnvironment (ambit2.db.search.structure.QueryAtomEnvironment)1 QueryComplement (ambit2.db.search.structure.QueryComplement)1 QuerySimilarity (ambit2.db.search.structure.QuerySimilarity)1 QuerySimilarityBitset (ambit2.db.search.structure.QuerySimilarityBitset)1 ChemicalBySubstanceRelation (ambit2.db.substance.relation.ChemicalBySubstanceRelation)1 InvalidResourceIDException (ambit2.rest.error.InvalidResourceIDException)1 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)1