Commit 3a0efb1d authored by Piotr Gawron's avatar Piotr Gawron
Browse files

api for chemicals

parent 5817bd8b
package lcsb.mapviewer.api.chemical;
import java.awt.geom.Point2D;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.services.SecurityException;
@RestController
@RequestMapping("/chemical")
public class ChemicalController extends BaseController {
@Autowired
private ChemicalRestImpl chemicalController;
@RequestMapping(value = "/getChemicalsByQuery", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getChemicalsByQuery(@RequestParam(value = "token") String token, @RequestParam(value = "projectId") String projectId,
@RequestParam(value = "columns", defaultValue = "") String columns, @RequestParam(value = "query") String query)
throws SecurityException, QueryException {
return chemicalController.getChemicalsByQuery(token, projectId, columns, query);
}
@RequestMapping(value = "/getChemicalsByTarget", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getChemicalsByTarget(@RequestParam(value = "token") String token, @RequestParam(value = "projectId") String projectId,
@RequestParam(value = "targetType", defaultValue = "ALIAS") String targetType, @RequestParam(value = "targetId", defaultValue = "") String targetId,
@RequestParam(value = "columns", defaultValue = "") String columns) throws SecurityException, QueryException {
return chemicalController.getChemicalsByTarget(token, projectId, targetType, targetId, columns);
}
}
\ No newline at end of file
package lcsb.mapviewer.api.chemical;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.annotation.services.TaxonomyBackend;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.AnnotatedObject;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.services.SecurityException;
import lcsb.mapviewer.services.interfaces.IModelService;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.services.search.ElementMatcher;
import lcsb.mapviewer.services.search.data.ElementIdentifier.ElementIdentifierType;
import lcsb.mapviewer.services.search.db.DbSearchCriteria;
import lcsb.mapviewer.services.search.db.GeneRow;
import lcsb.mapviewer.services.search.db.TargetView;
import lcsb.mapviewer.services.search.db.chemical.ChemicalView;
import lcsb.mapviewer.services.search.db.chemical.IChemicalService;
import lcsb.mapviewer.services.view.AnnotationView;
import lcsb.mapviewer.services.view.AuthenticationToken;
@Transactional(value = "txManager")
public class ChemicalRestImpl {
Logger logger = Logger.getLogger(ChemicalRestImpl.class);
@Autowired
private IChemicalService chemicalService;
@Autowired
private IModelService modelService;
@Autowired
private IUserService userService;
@Autowired
private ElementMatcher elementMatcher;
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;
}
ChemicalView 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;
}
private Map<String, Object> prepareChemical(ChemicalView chemical, 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 = chemical.getUniqueId();
} else if (column.equals("name")) {
value = chemical.getName();
} else if (column.equals("references")) {
List<AnnotationView> references = new ArrayList<>();
references.add(chemical.getSourceLink());
references.add(chemical.getCasLink());
value = references;
} else if (column.equals("directevidencereferences")) {
value = chemical.getDirectEvidencePubs();
} else if (column.equals("description")) {
value = chemical.getDescription();
} else if (column.equals("directevidence")) {
value = chemical.getDirectEvedince();
} else if (column.equals("synonyms")) {
value = chemical.getSynonyms();
} else if (column.equals("targets")) {
value = prepareTargets(chemical.getTargetRows(), models);
} else {
value = "Unknown column";
}
result.put(string, value);
}
return result;
}
private List<Map<String, Object>> prepareTargets(Collection<TargetView> targets, List<Model> models) {
List<Map<String, Object>> result = new ArrayList<>();
for (TargetView target : targets) {
result.add(prepareTarget(target, models));
}
return result;
}
private Map<String, Object> prepareTarget(TargetView target, List<Model> models) {
Map<String, Object> result = new HashMap<>();
result.put("name", target.getName());
result.put("references", target.getReferences());
List<AnnotationView> participants = new ArrayList<>();
for (GeneRow row : target.getProteins()) {
participants.add(row.getAnnotation());
}
result.put("targetParticipants", participants);
List<Map<String, Object>> targetedObjects = new ArrayList<>();
for (Model model : models) {
for (AnnotatedObject object : model.getAnnotatedObjects()) {
if (elementMatcher.elementMatch(target, object)) {
Map<String, Object> elementMapping = new HashMap<>();
elementMapping.put("id", object.getId());
elementMapping.put("type", getType(object));
elementMapping.put("modelId", model.getId());
targetedObjects.add(elementMapping);
}
}
}
result.put("targetElements", targetedObjects);
return result;
}
private String getType(AnnotatedObject object) {
if (object instanceof Reaction) {
return ElementIdentifierType.REACTION.getJsName();
} else if (object instanceof Element) {
return ElementIdentifierType.ALIAS.getJsName();
} else {
throw new InvalidArgumentException("Unknown type of element " + object.getClass());
}
}
private 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("description");
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<ChemicalView> 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 (ChemicalView chemical : chemicals) {
result.add(prepareChemical(chemical, columnSet, models));
}
return result;
}
}
......@@ -11,6 +11,7 @@
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="ChemicalRestImpl" class="lcsb.mapviewer.api.chemical.ChemicalRestImpl"/>
<bean id="ConfigurationRestImpl" class="lcsb.mapviewer.api.configuration.ConfigurationRestImpl"/>
<bean id="CommentRestImpl" class="lcsb.mapviewer.api.comment.CommentRestImpl"/>
<bean id="DrugRestImpl" class="lcsb.mapviewer.api.drug.DrugRestImpl"/>
......
......@@ -27,58 +27,58 @@ public class ChemicalView extends AbstractView<Chemical> implements ISearchResul
/**
*
*/
private static final long serialVersionUID = 91486136745430139L;
private static final long serialVersionUID = 91486136745430139L;
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private static Logger logger = Logger.getLogger(ChemicalView.class);
private static Logger logger = Logger.getLogger(ChemicalView.class);
/**
* Is the chemical selected by the client or not.
*/
private boolean selected = false;
private boolean selected = false;
/**
* Name of the chemical.
*/
private String name;
private String name;
/**
* Detailed description of the MeSH object.
*/
private String description;
private String description;
/**
* List of synonyms.
*/
private String synonyms;
private List<String> synonyms = new ArrayList<>();
/**
* Link for cas id.
*/
private AnnotationView casLink;
private AnnotationView casLink;
/**
* The direct Evedince if any.
*/
private String directEvidence;
private String directEvidence;
/**
* List of links to extenral databases.
*/
private List<Article> directEvidencePubs = new ArrayList<>();
private List<Article> directEvidencePubs = new ArrayList<>();
/**
* Link for source.
*/
private AnnotationView sourceLink;
private AnnotationView sourceLink;
/**
* List of targets for the chemical.
*/
private List<TargetView> targetRows = new ArrayList<>();
private List<TargetView> targetRows = new ArrayList<>();
/***
* Constructor that initialize the data with information from the
......@@ -294,7 +294,7 @@ public class ChemicalView extends AbstractView<Chemical> implements ISearchResul
/**
* @return list of Synonyms.
*/
public String getSynonyms() {
public List<String> getSynonyms() {
return synonyms;
}
......@@ -302,7 +302,7 @@ public class ChemicalView extends AbstractView<Chemical> implements ISearchResul
* @param synonyms
* list of Synonyms.
*/
public void setSynonyms(String synonyms) {
public void setSynonyms(List<String> synonyms) {
this.synonyms = synonyms;
}
......
......@@ -106,15 +106,13 @@ public class ChemicalViewFactory extends SearchResultFactory<Chemical, ChemicalV
MeSH mesh = meSHParser.getMeSH(chemical.getChemicalId());
if (mesh != null) {
chemicalView.setDescription(mesh.getDescription());
chemicalView.setSynonyms(mesh.getSynonymsString());
chemicalView.setSynonyms(mesh.getSynonyms());
} else {
chemicalView.setDescription("Mesh term not available");
chemicalView.setSynonyms("N/A");
}
} catch (AnnotatorException e) {
logger.error("Problem with accessing mesh database", e);
chemicalView.setDescription("Mesh term not available");
chemicalView.setSynonyms("N/A");
}
}
......
Markdown is supported
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