Search in sources :

Example 1 with QueryCombinedStructure

use of ambit2.db.search.structure.QueryCombinedStructure 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 QueryCombinedStructure

use of ambit2.db.search.structure.QueryCombinedStructure in project ambit-mirror by ideaconsult.

the class CompoundResource method createQuery.

@Override
protected IQueryRetrieval<IStructureRecord> createQuery(Context context, Request request, Response response) throws ResourceException {
    media = getMediaParameter(request);
    Object key = null;
    try {
        Form form = request.getResourceRef().getQueryAsForm();
        try {
            includeMol = "true".equals(form.getFirstValue("mol"));
        } catch (Exception x) {
            includeMol = false;
        }
        DisplayMode defaultMode = null;
        try {
            defaultMode = DisplayMode.valueOf(form.getFirstValue("mode"));
        } catch (Exception x) {
        }
        _dmode = defaultMode == null ? DisplayMode.singleitem : defaultMode;
        try {
            headless = Boolean.parseBoolean(form.getFirstValue("headless"));
        } catch (Exception x) {
            headless = false;
        }
        key = request.getAttributes().get(OpenTox.URI.compound.getKey());
        if (key == null) {
            boolean byAlias = true;
            String condition = form.getFirstValue(QueryResource.condition);
            String casesens = form.getFirstValue(QueryResource.caseSensitive);
            String[] keys = form.getValuesArray(QueryResource.search_param);
            String[] properties = form.getValuesArray(OpenTox.params.sameas.toString());
            if ((properties == null) || (properties.length == 0)) {
                properties = form.getValuesArray(QueryResource.property);
                condition = (condition == null) ? "=" : condition;
                byAlias = false;
            } else
                condition = (condition == null) ? "=" : condition;
            if (keys != null) {
                _dmode = defaultMode == null ? DisplayMode.table : defaultMode;
                /*
					 * QueryCombinedStructure qcombined = new
					 * QueryCombinedStructure();
					 * qcombined.setCombine_as_and(true);
					 * qcombined.setChemicalsOnly(true);
					 * 
					 * IQueryRetrieval<IStructureRecord> query = qcombined;
					 */
                IQueryRetrieval<IStructureRecord> query = null;
                for (int i = 0; i < keys.length; i++) {
                    String theKey = Reference.decode(keys[i].trim());
                    String property = null;
                    try {
                        property = ((properties == null) || (i >= properties.length) || (properties[i] == null)) ? "" : Reference.decode(properties[i].trim());
                    } catch (Exception x) {
                        property = null;
                    }
                    casesens = CASProcessor.isValidFormat(theKey) ? "true" : casesens;
                    casesens = EINECS.isValidFormat(theKey) ? "true" : casesens;
                    casesens = theKey.startsWith(AmbitCONSTANTS.INCHI) ? "true" : casesens;
                    casesens = theKey.startsWith("AuxInfo=") ? "true" : casesens;
                    // check for smiles will be more time consuming, skip
                    // for now
                    IQueryRetrieval<IStructureRecord> q = createSingleQuery(property, condition, theKey, chemicalsOnly, byAlias, casesens == null ? false : "true".equals(casesens.toLowerCase()));
                    // keys.length==1);
                    query = q;
                    break;
                /*
						 * if (keys.length>1) qcombined.add(q); else query = q;
						 */
                }
                return query;
            } else
                return null;
        } else {
            try {
                IStructureRecord record = new StructureRecord();
                record.setIdchemical(Integer.parseInt(Reference.decode(key.toString())));
                return createQueryByID(record);
            } catch (NumberFormatException x) {
                String inchikey = key.toString().trim();
                if (inchikey.length() == 27) {
                    // assume InChIKey
                    QueryStructure q = new QueryStructure();
                    q.setChemicalsOnly(true);
                    q.setFieldname(ExactStructureSearchMode.inchikey);
                    q.setValue(inchikey.trim());
                    return q;
                }
                throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid resource id", x);
            }
        }
    } catch (ResourceException x) {
        throw x;
    } catch (Exception x) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x.getMessage(), x);
    }
}
Also used : Form(org.restlet.data.Form) QueryStructure(ambit2.db.search.structure.QueryStructure) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) NotFoundException(net.idea.modbcum.i.exceptions.NotFoundException) DisplayMode(ambit2.rest.DisplayMode) IStructureRecord(ambit2.base.interfaces.IStructureRecord) StructureRecord(ambit2.base.data.StructureRecord) IStructureRecord(ambit2.base.interfaces.IStructureRecord) ResourceException(org.restlet.resource.ResourceException)

Example 3 with QueryCombinedStructure

use of ambit2.db.search.structure.QueryCombinedStructure in project ambit-mirror by ideaconsult.

the class QueryCombinedTest method test.

@Test
public void test() throws Exception {
    ReadStoredQuery qs = new ReadStoredQuery();
    qs.setName("test");
    QueryCombined qc = new QueryCombinedStructure();
    qc.setId(55);
    QueryStructureByID q = new QueryStructureByID(100);
    q.setCondition(NumberCondition.getInstance("<="));
    Assert.assertNotNull(q.getParameters().get(1).getValue());
    qc.add(q);
    // between 150 and 200
    QueryStructureByID q1 = new QueryStructureByID(150, 200);
    Assert.assertNotNull(q1.getParameters().get(1).getValue());
    Assert.assertNotNull(q1.getParameters().get(3).getValue());
    qc.add(q1);
    qc.setCombine_as_and(false);
    /*
		Assert.assertEquals("select ? as idquery,idchemical,idstructure,if(type_structure='NA',0,1) as selected,? as metric,? as text from structure where idstructure <= ? order by type_structure desc\nunion\nselect ? as idquery,idchemical,idstructure,if(type_structure='NA',0,1) as selected,? as metric,? as text from structure where idstructure between  ? and ? order by type_structure desc",
				qc.getSQL());
		*/
    Assert.assertNotNull(q.getParameters().get(1).getValue());
    List<QueryParam> params = qc.getParameters();
    Assert.assertNotNull(params);
    Assert.assertEquals(9, params.size());
    Object[] values = { 55, 1, null, 100, 55, 1, null, 150, 200 };
    for (int i = 0; i < params.size(); i++) {
        if (values[i] != null) {
            Assert.assertEquals(Integer.class, params.get(i).getType());
            Assert.assertEquals(values[i], params.get(i).getValue());
        }
    }
    qc.setScope(qs);
/*
		qc.setScope(null);
		qc.setCombine_as_and(true);
		assertEquals("select Q1.idquery,s.idchemical,idstructure,Q1.selected as selected,Q1.metric as metric from structure as s\njoin\n(select ? as idquery,idchemical,idstructure,1 as selected,1 as metric from structure where idstructure <= ?)\nas Q1\nusing (idstructure)\njoin\n(select ? as idquery,idchemical,idstructure,1 as selected,1 as metric from structure where idstructure between ? and ?)\nas Q2\nusing (idstructure)",qc.getSQL());
		System.out.println(qc.getSQL());
		
		QueryStored qs = new QueryStored();
		qs.setName("test");
		
		qc.setScope(qs);
		System.out.println(qc.getSQL());
		*/
}
Also used : QueryParam(net.idea.modbcum.i.query.QueryParam) ReadStoredQuery(ambit2.db.update.storedquery.ReadStoredQuery) QueryCombined(ambit2.db.search.QueryCombined) QueryStructureByID(ambit2.db.search.structure.QueryStructureByID) QueryCombinedStructure(ambit2.db.search.structure.QueryCombinedStructure) Test(org.junit.Test)

Example 4 with QueryCombinedStructure

use of ambit2.db.search.structure.QueryCombinedStructure in project ambit-mirror by ideaconsult.

the class ProcessorMissingDescriptorsQuery method process.

public IQueryObject<IStructureRecord> process(Profile<Property> descriptors) throws AmbitException {
    QueryCombinedStructure query = new QueryCombinedStructure();
    query.setPageSize(maxRecords);
    query.setCombine_as_and(false);
    Iterator<Property> i = descriptors.getProperties(true);
    while (i.hasNext()) {
        try {
            Property p = i.next();
            if (p.isEnabled()) {
                QueryMissingDescriptor q = new QueryMissingDescriptor();
                q.setFieldname(LiteratureEntry.getInstance(p.getClazz().getName()));
                query.add(q);
            }
        } catch (Exception x) {
            x.printStackTrace();
        }
    }
    return query;
}
Also used : Property(ambit2.base.data.Property) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 5 with QueryCombinedStructure

use of ambit2.db.search.structure.QueryCombinedStructure in project ambit-mirror by ideaconsult.

the class DatasetCompoundResource method createQuery.

@Override
protected IQueryRetrieval<IStructureRecord> createQuery(Context context, Request request, Response response) throws ResourceException {
    setTemplate(createTemplate(context, request, response));
    IQueryRetrieval<IStructureRecord> q = super.createQuery(context, request, response);
    if (q == null)
        return null;
    DatasetStructuresResource ds = new DatasetStructuresResource();
    ds.init(context, request, response);
    ds.configureDatasetMembersPrefixOption(dataset_prefixed_compound_uri);
    ds.setIncludeMol(includeMol);
    IQueryRetrieval<IStructureRecord> datasetQuery = ds.createQuery(context, request, response);
    if (datasetQuery == null)
        return null;
    prefix = ds.getCompoundInDatasetPrefix();
    if (q instanceof QueryStructureByID) {
        Object id = request.getAttributes().get(DatasetResource.datasetKey);
        if (id == null)
            throw new InvalidResourceIDException("No dataset");
        ISourceDataset dataset = getDataset(id.toString());
        if (dataset == null)
            throw new InvalidResourceIDException("No dataset");
        ((QueryStructureByID) q).setFieldname(dataset);
        return q;
    }
    // else some other query
    QueryCombinedStructure combinedQuery = new QueryCombinedStructure();
    combinedQuery.add(q);
    combinedQuery.add(datasetQuery);
    combinedQuery.setCombine_queries(COMBINE.AND);
    return combinedQuery;
}
Also used : IStructureRecord(ambit2.base.interfaces.IStructureRecord) ISourceDataset(ambit2.base.data.ISourceDataset) InvalidResourceIDException(ambit2.rest.error.InvalidResourceIDException) QueryStructureByID(ambit2.db.search.structure.QueryStructureByID) QueryCombinedStructure(ambit2.db.search.structure.QueryCombinedStructure)

Aggregations

QueryCombinedStructure (ambit2.db.search.structure.QueryCombinedStructure)14 QueryStructure (ambit2.db.search.structure.QueryStructure)6 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)5 ResourceException (org.restlet.resource.ResourceException)5 QueryCombined (ambit2.db.search.QueryCombined)4 ISourceDataset (ambit2.base.data.ISourceDataset)3 SourceDataset (ambit2.base.data.SourceDataset)3 IStructureRecord (ambit2.base.interfaces.IStructureRecord)3 QueryDataset (ambit2.db.search.structure.QueryDataset)3 QueryFieldNumeric (ambit2.db.search.structure.QueryFieldNumeric)3 QuerySimilarityBitset (ambit2.db.search.structure.QuerySimilarityBitset)3 QueryStoredResults (ambit2.db.search.structure.QueryStoredResults)3 QueryStructureByID (ambit2.db.search.structure.QueryStructureByID)3 InvalidResourceIDException (ambit2.rest.error.InvalidResourceIDException)3 Test (org.junit.Test)3 Form (org.restlet.data.Form)3 Property (ambit2.base.data.Property)2 PropertyTemplateStats (ambit2.base.data.PropertyTemplateStats)2 StructureRecord (ambit2.base.data.StructureRecord)2 SubstanceEndpointsBundle (ambit2.base.data.substance.SubstanceEndpointsBundle)2