Commit 32e83843 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

api providing suggested drug query list

parent f1d306f4
......@@ -130,7 +130,7 @@ public abstract class DrugAnnotation extends CachableInterface {
}
List<String> list = getSuggestedQueryListWithoutCache(project, diseaseId);
result = StringUtils.join(list, "\n");
} else if (name.startsWith("http")) {
result = getWebPageContent(name);
} else {
......@@ -256,12 +256,12 @@ public abstract class DrugAnnotation extends CachableInterface {
this.taxonomyBackend = taxonomyBackend;
}
public List<String> getSuggestedQueryList(Project project, MiriamData diseaseMiriam) throws DrugSearchException {
String cacheQuery = PROJECT_SUGGESTED_QUERY_PREFIX + "\n" + project.getId() + "\n" + diseaseMiriam.getResource();
public List<String> getSuggestedQueryList(Project project, MiriamData organism) throws DrugSearchException {
String cacheQuery = PROJECT_SUGGESTED_QUERY_PREFIX + "\n" + project.getId() + "\n" + organism.getResource();
String cachedData = getCacheValue(cacheQuery);
List<String> result;
if (cachedData == null) {
result = getSuggestedQueryListWithoutCache(project, diseaseMiriam);
result = getSuggestedQueryListWithoutCache(project, organism);
cachedData = StringUtils.join(result, "\n");
setCacheValue(cacheQuery, cachedData);
} else {
......
......@@ -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.DrugSearchException;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.common.Configuration;
......@@ -21,25 +22,36 @@ import lcsb.mapviewer.services.SecurityException;
@RestController
public class DrugController extends BaseController {
@Autowired
private DrugRestImpl drugController;
@RequestMapping(value = "/projects/{projectId}/drugs:search", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getDrugsByQuery(//
@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 drugController.getDrugsByQuery(token, projectId, columns, query);
} else if (target.indexOf(":") >= 0) {
String targetType = target.split(":", -1)[0];
String targetId = target.split(":", -1)[1];
return drugController.getDrugsByTarget(token, projectId, targetType, targetId, columns);
} else {
return new ArrayList<>();
}
}
@Autowired
private DrugRestImpl drugController;
@RequestMapping(value = "/projects/{projectId}/drugs:search", method = { RequestMethod.GET }, produces = {
MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getDrugsByQuery(//
@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 drugController.getDrugsByQuery(token, projectId, columns, query);
} else if (target.indexOf(":") >= 0) {
String targetType = target.split(":", -1)[0];
String targetId = target.split(":", -1)[1];
return drugController.getDrugsByTarget(token, projectId, targetType, targetId, columns);
} else {
return new ArrayList<>();
}
}
@RequestMapping(value = "/projects/{projectId}/drugs/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, DrugSearchException {
return drugController.getSuggestedQueryList(projectId, token);
}
}
\ No newline at end of file
......@@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.annotation.data.Drug;
import lcsb.mapviewer.annotation.services.DrugSearchException;
import lcsb.mapviewer.annotation.services.TaxonomyBackend;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.QueryException;
......@@ -30,195 +31,203 @@ import lcsb.mapviewer.services.view.AuthenticationToken;
@Transactional(value = "txManager")
public class DrugRestImpl extends BaseRestImpl {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(DrugRestImpl.class);
@Autowired
private IDrugService drugService;
@Autowired
private IModelService modelService;
@Autowired
private IUserService userService;
public List<Map<String, Object>> getDrugsByQuery(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();
Set<String> columnSet = createDrugColumnSet(columns);
List<Map<String, Object>> result = new ArrayList<>();
MiriamData organism = project.getOrganism();
if (organism == null) {
organism = TaxonomyBackend.HUMAN_TAXONOMY;
}
Drug drug = drugService.getByName(query, new DbSearchCriteria().model(model).organisms(organism).colorSet(0));
if (drug != null) {
List<Model> models = getModels(model);
result.add(prepareDrug(drug, 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;
}
private Map<String, Object> prepareDrug(Drug drug, Set<String> columnsSet, List<Model> models) {
Map<String, Object> result = new HashMap<>();
for (String string : columnsSet) {
String column = string.toLowerCase();
Object value = null;
if (column.equals("id") || column.equals("idobject")) {
value = drug.getName();
} else if (column.equals("name")) {
value = drug.getName();
} else if (column.equals("references")) {
value = createAnnotations(drug.getSources());
} else if (column.equals("description")) {
value = drug.getDescription();
} else if (column.equals("bloodbrainbarrier")) {
value = drug.getBloodBrainBarrier();
} else if (column.equals("brandnames")) {
value = drug.getBrandNames();
} else if (column.equals("synonyms")) {
value = drug.getSynonyms();
} else if (column.equals("targets")) {
value = prepareTargets(drug.getTargets(), models);
} else {
value = "Unknown column";
}
result.put(string, value);
}
return result;
}
private Set<String> createDrugColumnSet(String columns) {
Set<String> columnsSet = new HashSet<>();
if (columns.equals("")) {
columnsSet.add("name");
columnsSet.add("references");
columnsSet.add("description");
columnsSet.add("bloodBrainBarrier");
columnsSet.add("brandNames");
columnsSet.add("synonyms");
columnsSet.add("id");
columnsSet.add("targets");
} else {
for (String str : columns.split(",")) {
columnsSet.add(str);
}
}
return columnsSet;
}
/**
* @return the drugService
* @see #drugService
*/
public IDrugService getDrugService() {
return drugService;
}
/**
* @param drugService
* the drugService to set
* @see #drugService
*/
public void setDrugService(IDrugService drugService) {
this.drugService = drugService;
}
public List<Map<String, Object>> getDrugsByTarget(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();
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 = createDrugColumnSet(columns);
List<Drug> drugs = drugService.getForTargets(targets, new DbSearchCriteria().model(model).organisms(organism));
List<Map<String, Object>> result = new ArrayList<>();
List<Model> models = getModels(model);
for (Drug drug : drugs) {
result.add(prepareDrug(drug, columnSet, models));
}
return result;
}
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(DrugRestImpl.class);
@Autowired
private IDrugService drugService;
@Autowired
private IModelService modelService;
@Autowired
private IUserService userService;
public List<Map<String, Object>> getDrugsByQuery(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();
Set<String> columnSet = createDrugColumnSet(columns);
List<Map<String, Object>> result = new ArrayList<>();
MiriamData organism = project.getOrganism();
if (organism == null) {
organism = TaxonomyBackend.HUMAN_TAXONOMY;
}
Drug drug = drugService.getByName(query, new DbSearchCriteria().model(model).organisms(organism).colorSet(0));
if (drug != null) {
List<Model> models = getModels(model);
result.add(prepareDrug(drug, 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;
}
private Map<String, Object> prepareDrug(Drug drug, Set<String> columnsSet, List<Model> models) {
Map<String, Object> result = new HashMap<>();
for (String string : columnsSet) {
String column = string.toLowerCase();
Object value = null;
if (column.equals("id") || column.equals("idobject")) {
value = drug.getName();
} else if (column.equals("name")) {
value = drug.getName();
} else if (column.equals("references")) {
value = createAnnotations(drug.getSources());
} else if (column.equals("description")) {
value = drug.getDescription();
} else if (column.equals("bloodbrainbarrier")) {
value = drug.getBloodBrainBarrier();
} else if (column.equals("brandnames")) {
value = drug.getBrandNames();
} else if (column.equals("synonyms")) {
value = drug.getSynonyms();
} else if (column.equals("targets")) {
value = prepareTargets(drug.getTargets(), models);
} else {
value = "Unknown column";
}
result.put(string, value);
}
return result;
}
private Set<String> createDrugColumnSet(String columns) {
Set<String> columnsSet = new HashSet<>();
if (columns.equals("")) {
columnsSet.add("name");
columnsSet.add("references");
columnsSet.add("description");
columnsSet.add("bloodBrainBarrier");
columnsSet.add("brandNames");
columnsSet.add("synonyms");
columnsSet.add("id");
columnsSet.add("targets");
} else {
for (String str : columns.split(",")) {
columnsSet.add(str);
}
}
return columnsSet;
}
/**
* @return the drugService
* @see #drugService
*/
public IDrugService getDrugService() {
return drugService;
}
/**
* @param drugService
* the drugService to set
* @see #drugService
*/
public void setDrugService(IDrugService drugService) {
this.drugService = drugService;
}
public List<Map<String, Object>> getDrugsByTarget(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();
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 = createDrugColumnSet(columns);
List<Drug> drugs = drugService.getForTargets(targets, new DbSearchCriteria().model(model).organisms(organism));
List<Map<String, Object>> result = new ArrayList<>();
List<Model> models = getModels(model);
for (Drug drug : drugs) {
result.add(prepareDrug(drug, columnSet, models));
}
return result;
}
public List<String> getSuggestedQueryList(String projectId, String token)
throws SecurityException, DrugSearchException {
AuthenticationToken authenticationToken = userService.getToken(token);
Project project = getProjectService().getProjectByProjectId(projectId, authenticationToken);
return drugService.getSuggestedQueryList(project, project.getOrganism());
}
}
package lcsb.mapviewer.services.search.db.drug;
import java.util.List;
import lcsb.mapviewer.annotation.data.Drug;
import lcsb.mapviewer.annotation.services.DrugSearchException;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.services.search.db.IDbSearchService;
/**
......@@ -11,4 +16,6 @@ import lcsb.mapviewer.services.search.db.IDbSearchService;
*/
public interface IDrugService extends IDbSearchService<Drug> {
List<String> getSuggestedQueryList(Project project, MiriamData disease) throws DrugSearchException;
}
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