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);
}
}
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);
}
}
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());
*/
}
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;
}
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;
}
Aggregations