Search in sources :

Example 1 with ChemicalBySubstanceRelation

use of ambit2.db.substance.relation.ChemicalBySubstanceRelation 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 ChemicalBySubstanceRelation

use of ambit2.db.substance.relation.ChemicalBySubstanceRelation in project ambit-mirror by ideaconsult.

the class DatasetQueryCombined method createQuery.

@Override
protected IQueryRetrieval<IStructureRecord> createQuery(Context context, Request request, Response response) throws ResourceException {
    try {
        rdfwriter = RDF_WRITER.jena;
        IQueryRetrieval<IStructureRecord> freetextQuery = getScopeQuery(context, request, response);
        Form form = request.getResourceRef().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;
        }
        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;
        }
        try {
            includeMol = "true".equals(form.getFirstValue("mol"));
        } catch (Exception x) {
            includeMol = false;
        }
        Object key = form.getFirstValue(QueryResource.search_param);
        Object b64key = form.getFirstValue(QueryResource.b64search_param);
        if ((key == null) && (b64key == null)) {
            key = request.getAttributes().get(smartsKey);
            if (key == null) {
                if (freetextQuery == null)
                    throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Empty smarts");
                else {
                    setTemplate(createTemplate(context, request, response));
                    setGroupProperties(context, request, response);
                    return freetextQuery;
                }
            }
        }
        boolean fp1024_screenining = true;
        if (form.getFirstValue("fp1024") != null)
            try {
                fp1024_screenining = Boolean.parseBoolean(form.getFirstValue("fp1024"));
            } catch (Exception x) {
                fp1024_screenining = true;
            }
        boolean sk1024_screenining = true;
        if (form.getFirstValue("sk1024") != null)
            try {
                sk1024_screenining = Boolean.parseBoolean(form.getFirstValue("sk1024"));
            } catch (Exception x) {
                sk1024_screenining = true;
            }
        boolean read_atomprops = true;
        if (form.getFirstValue("atomproperties") != null)
            try {
                read_atomprops = Boolean.parseBoolean(form.getFirstValue("atomproperties"));
            } catch (Exception x) {
                read_atomprops = true;
            }
        String smarts = getSMILES(form, true);
        QuerySMARTS query = new QuerySMARTS();
        query.setFp1024_screening(fp1024_screenining);
        query.setSk1024_screening(sk1024_screenining);
        query.setUsePrecalculatedAtomProperties(read_atomprops);
        query.setChemicalsOnly(true);
        if (smarts.startsWith(AmbitCONSTANTS.INCHI))
            smarts = inchi2smiles(smarts);
        else {
            String smiles = name2smiles(smarts);
            if (smiles != null)
                smarts = smiles;
        }
        query.setValue(new FunctionalGroup(smarts, smarts, smarts));
        try {
            if (smarts != null)
                query.prepareScreening();
        } catch (Exception x) {
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x.getMessage(), x);
        }
        try {
            Object cmpid = request.getAttributes().get(DataResources.idcompound_resource);
            if (cmpid != null) {
                IStructureRecord record = new StructureRecord();
                record.setIdchemical(Integer.parseInt(Reference.decode(cmpid.toString())));
                QueryStructureByID scope = new QueryStructureByID();
                scope.setPageSize(1);
                scope.setChemicalsOnly(true);
                scope.setValue(record);
                QueryCombinedStructure combined = new QueryCombinedStructure();
                combined.setChemicalsOnly(true);
                combined.setCombine_as_and(true);
                combined.add(query);
                combined.setScope(scope);
                setTemplate(createTemplate(context, request, response));
                setGroupProperties(context, request, response);
                return combined;
            }
            /**
             * restriction by substance
             */
            if (filterBySubstance) {
                ChemicalBySubstanceRelation qa = new ChemicalBySubstanceRelation();
                QueryCombinedStructure qc = new QueryCombinedStructure();
                qc.add(query);
                qc.setChemicalsOnly(true);
                qc.setScope(qa);
                setTemplate(createTemplate(context, request, response));
                setGroupProperties(context, request, response);
                return qc;
            }
            /**
             * restriction to a dataset: ?dataset_uri=
             */
            Object datasetURI = OpenTox.params.dataset_uri.getFirstValue(form);
            ISourceDataset srcdataset = null;
            if (datasetURI != null)
                try {
                    srcdataset = getDataset(datasetURI.toString());
                    if (srcdataset instanceof SourceDataset) {
                        QueryDatasetByID scope = new QueryDatasetByID();
                        setTemplate(createTemplate(context, request, response));
                        setGroupProperties(context, request, response);
                        scope.setValue(srcdataset.getID());
                        QueryCombinedStructure combined = new DatasetQueryCombined();
                        combined.setCombine_as_and(true);
                        combined.add(query);
                        combined.setScope(scope);
                        return combined;
                    } else {
                    // TODO, resort to all db
                    }
                } catch (Exception x) {
                    srcdataset = null;
                }
            Object datasetid = request.getAttributes().get(DatasetResource.datasetKey);
            if (datasetid != null) {
                QueryDatasetByID scope = new QueryDatasetByID();
                this.dataset_id = Reference.decode(datasetid.toString());
                setTemplate(createTemplate(context, request, response));
                setGroupProperties(context, request, response);
                scope.setValue(new Integer(dataset_id));
                QueryCombinedStructure combined = new DatasetQueryCombined();
                combined.setCombine_as_and(true);
                combined.add(query);
                if (freetextQuery != null)
                    combined.add(freetextQuery);
                combined.setScope(scope);
                return combined;
            }
            folders = form.getValuesArray("folder");
            if ((folders != null) && (folders.length > 0)) {
                ChemicalByQueryFolder qa = new ChemicalByQueryFolder(folders);
                QueryCombinedStructure qc = new QueryCombinedStructure();
                qc.add(query);
                qc.setChemicalsOnly(true);
                qc.setScope(qa);
                setTemplate(createTemplate(context, request, response));
                setGroupProperties(context, request, response);
                return qc;
            }
            if (freetextQuery != null) {
                QueryCombinedStructure combined = new MyQueryCombined();
                combined.setChemicalsOnly(true);
                combined.setCombine_as_and(true);
                combined.add(query);
                combined.setScope(freetextQuery);
                setTemplate(createTemplate(context, request, response));
                setGroupProperties(context, request, response);
                return combined;
            }
        } catch (NumberFormatException x) {
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid resource id", x);
        } catch (Exception x) {
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x.getMessage(), x);
        }
        setTemplate(createTemplate(context, request, response));
        setGroupProperties(context, request, response);
        return query;
    } catch (ResourceException x) {
        throw x;
    } catch (Exception x) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x.getMessage(), x);
    }
}
Also used : ChemicalByQueryFolder(ambit2.db.search.structure.ChemicalByQueryFolder) Form(org.restlet.data.Form) ChemicalBySubstanceRelation(ambit2.db.substance.relation.ChemicalBySubstanceRelation) QueryDatasetByID(ambit2.db.search.structure.QueryDatasetByID) ResourceException(org.restlet.resource.ResourceException) InvalidResourceIDException(ambit2.rest.error.InvalidResourceIDException) QueryStructureByID(ambit2.db.search.structure.QueryStructureByID) IStructureRecord(ambit2.base.interfaces.IStructureRecord) QuerySMARTS(ambit2.db.search.structure.QuerySMARTS) ISourceDataset(ambit2.base.data.ISourceDataset) FunctionalGroup(ambit2.descriptors.FunctionalGroup) SubstanceEndpointsBundle(ambit2.base.data.substance.SubstanceEndpointsBundle) StructureRecord(ambit2.base.data.StructureRecord) IStructureRecord(ambit2.base.interfaces.IStructureRecord) ResourceException(org.restlet.resource.ResourceException) ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) QueryCombinedStructure(ambit2.db.search.structure.QueryCombinedStructure)

Aggregations

ISourceDataset (ambit2.base.data.ISourceDataset)2 SourceDataset (ambit2.base.data.SourceDataset)2 SubstanceEndpointsBundle (ambit2.base.data.substance.SubstanceEndpointsBundle)2 ChemicalByQueryFolder (ambit2.db.search.structure.ChemicalByQueryFolder)2 QueryCombinedStructure (ambit2.db.search.structure.QueryCombinedStructure)2 ChemicalBySubstanceRelation (ambit2.db.substance.relation.ChemicalBySubstanceRelation)2 InvalidResourceIDException (ambit2.rest.error.InvalidResourceIDException)2 Form (org.restlet.data.Form)2 ResourceException (org.restlet.resource.ResourceException)2 StructureRecord (ambit2.base.data.StructureRecord)1 IStructureRecord (ambit2.base.interfaces.IStructureRecord)1 QueryAtomEnvironment (ambit2.db.search.structure.QueryAtomEnvironment)1 QueryDatasetByID (ambit2.db.search.structure.QueryDatasetByID)1 QuerySMARTS (ambit2.db.search.structure.QuerySMARTS)1 QuerySimilarity (ambit2.db.search.structure.QuerySimilarity)1 QuerySimilarityBitset (ambit2.db.search.structure.QuerySimilarityBitset)1 QueryStructureByID (ambit2.db.search.structure.QueryStructureByID)1 ChemicalByDataset (ambit2.db.update.structure.ChemicalByDataset)1 FunctionalGroup (ambit2.descriptors.FunctionalGroup)1 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)1