use of ambit2.db.search.structure.QueryStructure in project ambit-mirror by ideaconsult.
the class CompoundLookup method createQuery.
/*
*/
@Override
protected IQueryRetrieval<IStructureRecord> createQuery(Context context, Request request, Response response) throws ResourceException {
// parse params
Object id = null;
try {
id = getRequest().getAttributes().get(resourceKey);
if (id == null)
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No structure identifier");
text = Reference.decode(id.toString().trim());
if ("".equals(text))
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Empty query");
} catch (Exception x) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, String.format("Invalid structure identifier", id));
}
id = null;
try {
id = getRequest().getAttributes().get(representationKey);
if (id == null)
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No structure representation");
rep_id = NCISearchProcessor.METHODS.valueOf(Reference.decode(id.toString()));
} catch (Exception x) {
rep_id = null;
}
Form form = getParams();
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;
}
try {
headless = Boolean.parseBoolean(form.getFirstValue("headless"));
} catch (Exception x) {
headless = false;
}
boolean casesens = "true".equals(form.getFirstValue(QueryResource.caseSensitive)) ? true : false;
String scparam = form.getFirstValue(QueryResource.condition);
setCondition(StringCondition.C_EQ);
if (scparam != null)
for (STRING_CONDITION sc : STRING_CONDITION.values()) {
if (scparam.equals(sc.getSQL())) {
setCondition(sc.getSQL());
break;
}
}
boolean retrieveProperties = "true".equals(form.getFirstValue(QueryResource.returnProperties)) ? true : false;
String url = null;
IQueryRetrieval<IStructureRecord> query = null;
if (isInchiKey(text))
try {
String inchikey = getFirstParam(form);
if ((inchikey == null) || (inchikey.trim().length() != 27))
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid InChI Key");
QueryStructure q = new QueryStructure();
q.setChemicalsOnly(true);
q.setFieldname(ExactStructureSearchMode.inchikey);
q.setValue(inchikey.trim());
searchType = _searchtype.inchikey;
query = q;
} catch (ResourceException x) {
throw x;
} catch (Exception x) {
throw new ResourceException(Status.SERVER_ERROR_BAD_GATEWAY, String.format("%s %s", url, x.getMessage()), x);
}
else if (isURL(text))
try {
url = getFirstParam(form);
if (url == null)
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No search parameter!");
Object q = CallableQueryProcessor.getQueryObject(new Reference(url), getRequest().getRootRef(), getApplication().getContext(), getCookies(), getAgent(), getRequest().getResourceRef().toString());
if (q == null) {
throw new ResourceException(Status.SERVER_ERROR_NOT_IMPLEMENTED, "TODO: retrieve compounds from foreign urls");
} else if (q instanceof AbstractStructureQuery) {
query = (IQueryRetrieval<IStructureRecord>) q;
}
} catch (ResourceException x) {
throw x;
} catch (Exception x) {
throw new ResourceException(Status.SERVER_ERROR_BAD_GATEWAY, String.format("%s %s", url, x.getMessage()), x);
}
else {
if (isSearchParam(text)) {
text = getFirstParam(form);
text_multi = getValuesArray(form);
}
Object idcompound = isAmbitID(text);
// query
if (idcompound != null) {
switch(searchType) {
case ambitid:
IStructureRecord record = new StructureRecord();
record.setIdchemical(((Integer) idcompound).intValue());
QueryStructureByID q = new QueryStructureByID();
q.setPageSize(1);
q.setChemicalsOnly(true);
q.setValue(record);
query = q;
break;
case inchikey:
String inchikey = idcompound.toString();
if ((inchikey == null) || (inchikey.trim().length() != 27))
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid InChI Key");
QueryStructure q1 = new QueryStructure();
q1.setChemicalsOnly(true);
q1.setFieldname(ExactStructureSearchMode.inchikey);
q1.setValue(inchikey.trim());
searchType = _searchtype.inchikey;
query = q1;
break;
default:
query = null;
break;
}
} else if ((text_multi != null) && (text_multi.length > 1)) {
query = getMultiTextQuery(null, casesens, retrieveProperties, text_multi);
} else if ((text == null) || "".equals(text)) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
} else if (CASProcessor.isValidFormat(text)) {
// number
if (CASNumber.isValid(text.trim())) {
searchType = _searchtype.cas;
query = getTextQuery(Property.getCASInstance(), casesens, retrieveProperties, text);
}
} else if (EINECS.isValidFormat(text)) {
// we'd better not search for invalid numbers
if (EINECS.isValid(text.trim())) {
searchType = _searchtype.einecs;
query = getTextQuery(Property.getEINECSInstance(), casesens, retrieveProperties, text);
}
} else {
if ("".equals(text)) {
// if inchi
} else if (isInChI(text)) {
QueryStructure q = new QueryStructure();
q.setChemicalsOnly(true);
q.setFieldname(ExactStructureSearchMode.inchi);
q.setValue(text);
query = q;
} else if (isMol(text)) {
String[] inchi = mol2inchi(text);
QueryStructure q = new QueryStructure();
q.setChemicalsOnly(true);
q.setFieldname(ExactStructureSearchMode.inchi);
// eventually search by inchikey?
q.setValue(inchi[0]);
query = q;
} else
try {
// inchi, inchikey
String[] inchi = smiles2inchi(text.trim());
QueryStructure q = new QueryStructure();
q.setChemicalsOnly(true);
q.setFieldname(ExactStructureSearchMode.inchi);
// eventually search by inchikey?
q.setValue(inchi[0]);
query = q;
} catch (InvalidSmilesException x) {
query = null;
// otherwise this was valid smiles, but inchi failed!
} catch (CDKException x) {
casesens = true;
query = null;
} catch (Exception x) {
casesens = true;
query = null;
}
}
}
if (query == null)
query = getTextQuery(null, casesens, retrieveProperties, text);
setPaging(form, query);
setTemplate(createTemplate(form));
setGroupProperties(context, request, response);
return query;
}
use of ambit2.db.search.structure.QueryStructure 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.QueryStructure in project ambit-mirror by ideaconsult.
the class PropertyImporter method setPropertyKey.
public void setPropertyKey(IStructureKey propertyKey) {
this.queryKey = propertyKey;
if ((propertyKey instanceof SmilesKey) || (propertyKey instanceof InchiKey)) {
query_property = new QueryStructure();
} else if ((propertyKey.getType() == Number.class) || (propertyKey.getType() == Integer.class) || (propertyKey.getType() == Double.class))
query_property = new QueryFieldNumeric();
else
query_property = new QueryField();
query_property.setId(-1);
}
use of ambit2.db.search.structure.QueryStructure in project ambit-mirror by ideaconsult.
the class QueryStructureTest method test.
@Test
public void test() throws Exception {
QueryStructure qf = new QueryStructure();
qf.setFieldname(ExactStructureSearchMode.smiles);
qf.setValue("CC");
qf.setCondition(StringCondition.getInstance("="));
qf.setId(1);
// Assert.assertEquals(String.format(QueryStructure.sqlSMILES, "", qf.getFieldname(),qf.getCondition(),qf.getFieldname(),qf.getCondition(),"")
// ,qf.getSQL());
List<QueryParam> params = qf.getParameters();
Assert.assertNotNull(params);
Assert.assertEquals(3, params.size());
Assert.assertEquals(Integer.class, params.get(0).getType());
Assert.assertEquals(String.class, params.get(1).getType());
Assert.assertEquals(1, params.get(0).getValue());
Assert.assertEquals("CC", params.get(1).getValue());
}
use of ambit2.db.search.structure.QueryStructure in project ambit-mirror by ideaconsult.
the class Structure_crud_test method createVerify.
@Override
protected void createVerify(IQueryUpdate<Object, IStructureRecord> query) throws Exception {
IDatabaseConnection c = getConnection();
QueryStructure q = new QueryStructure();
q.setFieldname(ExactStructureSearchMode.idchemical);
q.setValue("10");
RetrieveStructure r = new RetrieveStructure();
// structure
r.setFieldname(false);
QueryExecutor execR = new QueryExecutor();
execR.setConnection(c.getConnection());
QueryExecutor exec = new QueryExecutor();
exec.setConnection(c.getConnection());
ResultSet rs = exec.process(q);
int count = 0;
while (rs.next()) {
IStructureRecord record = q.getObject(rs);
r.setValue(record);
ResultSet rs1 = execR.process(r);
while (rs1.next()) {
record = r.getObject(rs1);
if ("content".equals(record.getContent()))
count++;
}
execR.closeResults(rs1);
}
exec.closeResults(rs);
Assert.assertEquals(1, count);
c.close();
}
Aggregations