use of ambit2.db.search.structure.ChemicalByQueryFolder 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.ChemicalByQueryFolder 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);
}
}
Aggregations