Commit 7eb81245 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

api call for providing list of suggested queries for chemical database

parent e2a779a7
This diff is collapsed.
......@@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import lcsb.mapviewer.annotation.services.ChemicalSearchException;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.common.Configuration;
......@@ -21,25 +22,35 @@ import lcsb.mapviewer.services.SecurityException;
@RestController
public class ChemicalController extends BaseController {
@Autowired
private ChemicalRestImpl chemicalController;
@Autowired
private ChemicalRestImpl chemicalController;
@RequestMapping(value = "/projects/{projectId}/chemicals:search", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getChemicalsByQuery(//
@CookieValue(value = Configuration.AUTH_TOKEN) String token, //
@PathVariable(value = "projectId") String projectId, //
@RequestParam(value = "columns", defaultValue = "") String columns, //
@RequestParam(value = "query", defaultValue = "") String query, //
@RequestParam(value = "target", defaultValue = "") String target //
) throws SecurityException, QueryException {
if (!query.equals("")) {
return chemicalController.getChemicalsByQuery(token, projectId, columns, query);
} else if (target.indexOf(":") >= 0) {
String targetType = target.split(":", -1)[0];
String targetId = target.split(":", -1)[1];
return chemicalController.getChemicalsByTarget(token, projectId, targetType, targetId, columns);
} else {
return new ArrayList<>();
}
}
@RequestMapping(value = "/projects/{projectId}/chemicals:search", method = { RequestMethod.GET }, produces = {
MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getChemicalsByQuery(//
@CookieValue(value = Configuration.AUTH_TOKEN) String token, //
@PathVariable(value = "projectId") String projectId, //
@RequestParam(value = "columns", defaultValue = "") String columns, //
@RequestParam(value = "query", defaultValue = "") String query, //
@RequestParam(value = "target", defaultValue = "") String target //
) throws SecurityException, QueryException {
if (!query.equals("")) {
return chemicalController.getChemicalsByQuery(token, projectId, columns, query);
} else if (target.indexOf(":") >= 0) {
String targetType = target.split(":", -1)[0];
String targetId = target.split(":", -1)[1];
return chemicalController.getChemicalsByTarget(token, projectId, targetType, targetId, columns);
} else {
return new ArrayList<>();
}
}
@RequestMapping(value = "/projects/{projectId}/chemicals/suggestedQueryList", method = {
RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<String> getSuggestedQueryList(//
@PathVariable(value = "projectId") String projectId, //
@CookieValue(value = Configuration.AUTH_TOKEN) String token//
) throws SecurityException, ChemicalSearchException {
return chemicalController.getSuggestedQueryList(projectId, token);
}
}
\ No newline at end of file
......@@ -13,6 +13,8 @@ import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.annotation.data.Chemical;
import lcsb.mapviewer.annotation.data.MeSH;
import lcsb.mapviewer.annotation.services.ChemicalParser;
import lcsb.mapviewer.annotation.services.ChemicalSearchException;
import lcsb.mapviewer.annotation.services.MeSHParser;
import lcsb.mapviewer.annotation.services.TaxonomyBackend;
import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
......@@ -33,223 +35,234 @@ import lcsb.mapviewer.services.view.AuthenticationToken;
@Transactional(value = "txManager")
public class ChemicalRestImpl extends BaseRestImpl {
/**
* Default class logger.
*/
private Logger logger = Logger.getLogger(ChemicalRestImpl.class);
@Autowired
private IChemicalService chemicalService;
@Autowired
private IModelService modelService;
@Autowired
private IUserService userService;
@Autowired
private MeSHParser meSHParser;
public List<Map<String, Object>> getChemicalsByQuery(String token, String projectId, String columns, String query) throws SecurityException, QueryException {
AuthenticationToken authenticationToken = userService.getToken(token);
Model model = modelService.getLastModelByProjectId(projectId, authenticationToken);
if (model == null) {
throw new QueryException("Project with given id doesn't exist");
}
Project project = model.getProject();
if (project.getDisease() == null) {
throw new QueryException("Project doesn't have disease associated to it");
}
Set<String> columnSet = createChemicalColumnSet(columns);
List<Map<String, Object>> result = new ArrayList<>();
MiriamData organism = project.getOrganism();
if (organism == null) {
organism = TaxonomyBackend.HUMAN_TAXONOMY;
}
Chemical chemical = chemicalService.getByName(query, new DbSearchCriteria().model(model).organisms(organism).colorSet(0).disease(project.getDisease()));
if (chemical != null) {
List<Model> models = getModels(model);
result.add(prepareChemical(chemical, columnSet, models));
}
return result;
}
private List<Model> getModels(Model model) {
List<Model> models = new ArrayList<>();
models.add(model);
models.addAll(model.getSubmodels());
return models;
}
/**
* @return the userService
* @see #userService
*/
public IUserService getUserService() {
return userService;
}
/**
* @param userService
* the userService to set
* @see #userService
*/
public void setUserService(IUserService userService) {
this.userService = userService;
}
/**
* @return the modelService
* @see #modelService
*/
public IModelService getModelService() {
return modelService;
}
/**
* @param modelService
* the modelService to set
* @see #modelService
*/
public void setModelService(IModelService modelService) {
this.modelService = modelService;
}
protected Map<String, Object> prepareChemical(Chemical chemical, Set<String> columnsSet, List<Model> models) {
Map<String, Object> result = new HashMap<>();
String description = "Mesh term not available";
List<String> synonyms = new ArrayList<>();
try {
MeSH mesh = meSHParser.getMeSH(chemical.getChemicalId());
description = mesh.getDescription();
synonyms = mesh.getSynonyms();
} catch (AnnotatorException e) {
logger.error("Problem with accessing mesh database", e);
}
for (String string : columnsSet) {
String column = string.toLowerCase();
Object value = null;
if (column.equals("id") || column.equals("idobject")) {
value = chemical.getChemicalId();
} else if (column.equals("name")) {
value = chemical.getChemicalName();
} else if (column.equals("references")) {
List<Map<String, Object>> references = new ArrayList<>();
references.add(createAnnotation(chemical.getChemicalId()));
if (chemical.getCasID() != null) {
references.add(createAnnotation(chemical.getCasID()));
}
value = references;
} else if (column.equals("directevidencereferences")) {
value = createAnnotations(chemical.getDirectEvidencePublication());
} else if (column.equals("description")) {
value = description;
} else if (column.equals("directevidence")) {
if (chemical.getDirectEvidence() != null) {
value = chemical.getDirectEvidence().getValue();
}
} else if (column.equals("synonyms")) {
value = synonyms;
} else if (column.equals("targets")) {
value = prepareTargets(chemical.getInferenceNetwork(), models);
} else {
value = "Unknown column";
}
result.put(string, value);
}
return result;
}
protected Set<String> createChemicalColumnSet(String columns) {
Set<String> columnsSet = new HashSet<>();
if (columns.equals("")) {
columnsSet.add("name");
columnsSet.add("references");
columnsSet.add("description");
columnsSet.add("synonyms");
columnsSet.add("id");
columnsSet.add("directEvidenceReferences");
columnsSet.add("directEvidence");
columnsSet.add("targets");
} else {
for (String str : columns.split(",")) {
columnsSet.add(str);
}
}
return columnsSet;
}
/**
* @return the chemicalService
* @see #chemicalService
*/
public IChemicalService getChemicalService() {
return chemicalService;
}
/**
* @param chemicalService
* the chemicalService to set
* @see #chemicalService
*/
public void setChemicalService(IChemicalService chemicalService) {
this.chemicalService = chemicalService;
}
public List<Map<String, Object>> getChemicalsByTarget(String token, String projectId, String targetType, String targetId, String columns)
throws SecurityException, QueryException {
AuthenticationToken authenticationToken = userService.getToken(token);
Model model = modelService.getLastModelByProjectId(projectId, authenticationToken);
if (model == null) {
throw new QueryException("Project with given id doesn't exist");
}
Project project = model.getProject();
if (project.getDisease() == null) {
throw new QueryException("Project doesn't have disease associated to it");
}
Integer dbId = Integer.valueOf(targetId);
List<Element> targets = new ArrayList<>();
if (targetType.equals(ElementIdentifierType.ALIAS.getJsName())) {
Element element = model.getElementByDbId(dbId);
for (Model m : model.getSubmodels()) {
if (element == null) {
element = m.getElementByDbId(dbId);
}
}
if (element == null) {
throw new QueryException("Invalid element identifier for given project");
}
targets.add(element);
} else {
throw new QueryException("Targeting for the type not implemented");
}
MiriamData organism = project.getOrganism();
if (organism == null) {
organism = TaxonomyBackend.HUMAN_TAXONOMY;
}
Set<String> columnSet = createChemicalColumnSet(columns);
List<Chemical> chemicals = chemicalService.getForTargets(targets, new DbSearchCriteria().model(model).organisms(organism).disease(project.getDisease()));
List<Map<String, Object>> result = new ArrayList<>();
List<Model> models = getModels(model);
for (Chemical chemical : chemicals) {
result.add(prepareChemical(chemical, columnSet, models));
}
return result;
}
/**
* Default class logger.
*/
private Logger logger = Logger.getLogger(ChemicalRestImpl.class);
@Autowired
private IChemicalService chemicalService;
@Autowired
private ChemicalParser chemicalParser;
@Autowired
private IModelService modelService;
@Autowired
private IUserService userService;
@Autowired
private MeSHParser meSHParser;
public List<Map<String, Object>> getChemicalsByQuery(String token, String projectId, String columns, String query)
throws SecurityException, QueryException {
AuthenticationToken authenticationToken = userService.getToken(token);
Model model = modelService.getLastModelByProjectId(projectId, authenticationToken);
if (model == null) {
throw new QueryException("Project with given id doesn't exist");
}
Project project = model.getProject();
if (project.getDisease() == null) {
throw new QueryException("Project doesn't have disease associated to it");
}
Set<String> columnSet = createChemicalColumnSet(columns);
List<Map<String, Object>> result = new ArrayList<>();
MiriamData organism = project.getOrganism();
if (organism == null) {
organism = TaxonomyBackend.HUMAN_TAXONOMY;
}
Chemical chemical = chemicalService.getByName(query,
new DbSearchCriteria().model(model).organisms(organism).colorSet(0).disease(project.getDisease()));
if (chemical != null) {
List<Model> models = getModels(model);
result.add(prepareChemical(chemical, columnSet, models));
}
return result;
}
private List<Model> getModels(Model model) {
List<Model> models = new ArrayList<>();
models.add(model);
models.addAll(model.getSubmodels());
return models;
}
/**
* @return the userService
* @see #userService
*/
public IUserService getUserService() {
return userService;
}
/**
* @param userService
* the userService to set
* @see #userService
*/
public void setUserService(IUserService userService) {
this.userService = userService;
}
/**
* @return the modelService
* @see #modelService
*/
public IModelService getModelService() {
return modelService;
}
/**
* @param modelService
* the modelService to set
* @see #modelService
*/
public void setModelService(IModelService modelService) {
this.modelService = modelService;
}
protected Map<String, Object> prepareChemical(Chemical chemical, Set<String> columnsSet, List<Model> models) {
Map<String, Object> result = new HashMap<>();
String description = "Mesh term not available";
List<String> synonyms = new ArrayList<>();
try {
MeSH mesh = meSHParser.getMeSH(chemical.getChemicalId());
description = mesh.getDescription();
synonyms = mesh.getSynonyms();
} catch (AnnotatorException e) {
logger.error("Problem with accessing mesh database", e);
}
for (String string : columnsSet) {
String column = string.toLowerCase();
Object value = null;
if (column.equals("id") || column.equals("idobject")) {
value = chemical.getChemicalId();
} else if (column.equals("name")) {
value = chemical.getChemicalName();
} else if (column.equals("references")) {
List<Map<String, Object>> references = new ArrayList<>();
references.add(createAnnotation(chemical.getChemicalId()));
if (chemical.getCasID() != null) {
references.add(createAnnotation(chemical.getCasID()));
}
value = references;
} else if (column.equals("directevidencereferences")) {
value = createAnnotations(chemical.getDirectEvidencePublication());
} else if (column.equals("description")) {
value = description;
} else if (column.equals("directevidence")) {
if (chemical.getDirectEvidence() != null) {
value = chemical.getDirectEvidence().getValue();
}
} else if (column.equals("synonyms")) {
value = synonyms;
} else if (column.equals("targets")) {
value = prepareTargets(chemical.getInferenceNetwork(), models);
} else {
value = "Unknown column";
}
result.put(string, value);
}
return result;
}
protected Set<String> createChemicalColumnSet(String columns) {
Set<String> columnsSet = new HashSet<>();
if (columns.equals("")) {
columnsSet.add("name");
columnsSet.add("references");
columnsSet.add("description");
columnsSet.add("synonyms");
columnsSet.add("id");
columnsSet.add("directEvidenceReferences");
columnsSet.add("directEvidence");
columnsSet.add("targets");
} else {
for (String str : columns.split(",")) {
columnsSet.add(str);
}
}
return columnsSet;
}
/**
* @return the chemicalService
* @see #chemicalService
*/
public IChemicalService getChemicalService() {
return chemicalService;
}
/**
* @param chemicalService
* the chemicalService to set
* @see #chemicalService
*/
public void setChemicalService(IChemicalService chemicalService) {
this.chemicalService = chemicalService;
}
public List<Map<String, Object>> getChemicalsByTarget(String token, String projectId, String targetType,
String targetId, String columns) throws SecurityException, QueryException {
AuthenticationToken authenticationToken = userService.getToken(token);
Model model = modelService.getLastModelByProjectId(projectId, authenticationToken);
if (model == null) {
throw new QueryException("Project with given id doesn't exist");
}
Project project = model.getProject();
if (project.getDisease() == null) {
throw new QueryException("Project doesn't have disease associated to it");
}
Integer dbId = Integer.valueOf(targetId);
List<Element> targets = new ArrayList<>();
if (targetType.equals(ElementIdentifierType.ALIAS.getJsName())) {
Element element = model.getElementByDbId(dbId);
for (Model m : model.getSubmodels()) {
if (element == null) {
element = m.getElementByDbId(dbId);
}
}
if (element == null) {
throw new QueryException("Invalid element identifier for given project");
}
targets.add(element);
} else {
throw new QueryException("Targeting for the type not implemented");
}
MiriamData organism = project.getOrganism();
if (organism == null) {
organism = TaxonomyBackend.HUMAN_TAXONOMY;
}
Set<String> columnSet = createChemicalColumnSet(columns);
List<Chemical> chemicals = chemicalService.getForTargets(targets,
new DbSearchCriteria().model(model).organisms(organism).disease(project.getDisease()));
List<Map<String, Object>> result = new ArrayList<>();
List<Model> models = getModels(model);
for (Chemical chemical : chemicals) {
result.add(prepareChemical(chemical, columnSet, models));
}
return result;
}
public List<String> getSuggestedQueryList(String projectId, String token) throws SecurityException, ChemicalSearchException {
AuthenticationToken authenticationToken = userService.getToken(token);
Project project = getProjectService().getProjectByProjectId(projectId, authenticationToken);
return chemicalParser.getSuggestedQueryList(project, project.getDisease());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment