Commit 72d73186 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

api for drug targets

parent 564d34e2
......@@ -405,6 +405,12 @@ public class RestAnnotationParserTest extends CellDesignerTestFunctions {
@Override
public void addMiriamData(Collection<MiriamData> miriamData) {
}
@Override
public int getId() {
// TODO Auto-generated method stub
return 0;
}
});
fail("Exception expected");
} catch (NotImplementedException e) {
......
......@@ -124,4 +124,6 @@ public interface AnnotatedObject extends Serializable {
*
*/
void setName(String name);
int getId();
}
package lcsb.mapviewer.api.drug;
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("/drug")
public class DrugController extends BaseController {
@Autowired
private DrugRestImpl drugController;
@RequestMapping(value = "/getDrugsByQuery", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getDrugsByQuery(@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 drugController.getDrugsByQuery(token, projectId, columns, query);
}
@RequestMapping(value = "/getDrugsByTarget", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getDrugsByTarget(@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 drugController.getDrugsByTarget(token, projectId, targetType, targetId, columns);
}
}
\ No newline at end of file
package lcsb.mapviewer.api.drug;
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.persist.dao.map.ReactionDao;
import lcsb.mapviewer.persist.dao.map.species.ElementDao;
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.TargetView;
import lcsb.mapviewer.services.search.db.drug.DrugView;
import lcsb.mapviewer.services.search.db.drug.IDrugService;
import lcsb.mapviewer.services.view.AuthenticationToken;
@Transactional(value = "txManager")
public class DrugRestImpl {
Logger logger = Logger.getLogger(DrugRestImpl.class);
@Autowired
private IDrugService drugService;
@Autowired
private IModelService modelService;
@Autowired
private IUserService userService;
@Autowired
private ElementMatcher elementMatcher;
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;
}
DrugView drug = drugService.getByName(query, new DbSearchCriteria().model(model).organisms(organism).colorSet(0));
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(DrugView 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 = drug.getDrugLinks();
} 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.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());
result.put("targetParticipants", target.getProteins());
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> 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<DrugView> drugs = drugService.getForTargets(targets, new DbSearchCriteria().model(model).organisms(organism));
List<Map<String, Object>> result = new ArrayList<>();
List<Model> models = getModels(model);
for (DrugView drug : drugs) {
result.add(prepareDrug(drug, columnSet, models));
}
return result;
}
}
......@@ -13,6 +13,7 @@
<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"/>
<bean id="OverlayRestImpl" class="lcsb.mapviewer.api.overlay.OverlayRestImpl"/>
<bean id="ProjectRestImpl" class="lcsb.mapviewer.api.project.ProjectRestImpl"/>
......
package lcsb.mapviewer.services.search;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.annotation.data.TargetType;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.map.AnnotatedObject;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.species.Complex;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Gene;
import lcsb.mapviewer.model.map.species.Protein;
import lcsb.mapviewer.model.map.species.Rna;
import lcsb.mapviewer.model.map.species.Species;
import lcsb.mapviewer.services.search.db.GeneRow;
import lcsb.mapviewer.services.search.db.TargetView;
import lcsb.mapviewer.services.view.AnnotationViewFactory;
public class ElementMatcher {
/**
* Default class logger.
*/
private final Logger logger = Logger.getLogger(ElementMatcher.class);
/**
* Factory object used for creation of
* {@link lcsb.mapviewer.services.view.AnnotationView} elements.
*/
@Autowired
private AnnotationViewFactory annotationViewFactory;
/**
* Check if the target should be applied to the element.
*
* @param target
* drug target to check
* @param element
* {@link Element} to check
* @return <code>true</code> if drug target can target element,
* <code>false</code> otherwise
*/
public boolean elementMatch(TargetView target, AnnotatedObject element) {
if (target.getType().equals(TargetType.COMPLEX_PROTEIN.getCommonName())) {
MiriamData targetAnnotation = annotationViewFactory.viewToObject(target.getAnnotation());
if (MiriamType.CHEMBL_TARGET.equals(targetAnnotation.getDataType())) {
if (element.getMiriamData().contains(targetAnnotation)) {
return true;
}
}
Collection<String> ids = new HashSet<String>();
for (GeneRow row : target.getProteins()) {
ids.add(row.getAnnotation().getName());
}
if (element instanceof Complex) {
Complex complex = (Complex) element;
Set<Species> speciesSet = complex.getAllSimpleChildren();
if (speciesSet.size() != ids.size()) {
return false;
}
for (Element id : speciesSet) {
if (!ids.contains(id.getName())) {
return false;
}
}
return true;
}
return false;
} else if (target.getType().equals(TargetType.SINGLE_PROTEIN.getCommonName())) {
MiriamData targetAnnotation = null;
if (target.getAnnotation() != null) {
targetAnnotation = annotationViewFactory.viewToObject(target.getAnnotation());
} else if (target.getProteins().size() > 0) {
targetAnnotation = annotationViewFactory.viewToObject(target.getProteins().get(0).getAnnotation());
} else {
logger.warn("Invalid target found: " + target);
}
if (targetAnnotation != null && element.getMiriamData().contains(targetAnnotation)) {
return true;
}
if (element instanceof Protein || element instanceof Rna || element instanceof Gene) {
if (target.getProteins().size() > 0) {
String hgncId = target.getProteins().get(0).getAnnotation().getName();
if (element.getName().equalsIgnoreCase(hgncId)) {
return true;
}
}
}
return false;
} else if (target.getType().equals(TargetType.PROTEIN_FAMILY.getCommonName())) {
if (element instanceof Protein || element instanceof Rna || element instanceof Gene) {
String hgncId = target.getProteins().get(0).getAnnotation().getName();
if (element.getName().equalsIgnoreCase(hgncId)) {
return true;
} else if (element.getMiriamData().contains(new MiriamData(MiriamType.HGNC_SYMBOL, hgncId))) {
return true;
}
for (GeneRow protein : target.getProteins()) {
hgncId = protein.getAnnotation().getName();
if (element.getName().equalsIgnoreCase(hgncId)) {
return true;
} else if (element.getMiriamData().contains(new MiriamData(MiriamType.HGNC_SYMBOL, hgncId))) {
return true;
}
}
}
return false;
} else if (target.getType().equals(TargetType.OTHER.getCommonName())) {
// in other case just compare names
String name = target.getProteins().get(0).getAnnotation().getName();
return element.getName().equalsIgnoreCase(name);
} else {
throw new InvalidArgumentException("Unknown drug target type: " + target.getType());
}
}
/**
* @return the annotationViewFactory
* @see #annotationViewFactory
*/
public AnnotationViewFactory getAnnotationViewFactory() {
return annotationViewFactory;
}
/**
* @param annotationViewFactory the annotationViewFactory to set
* @see #annotationViewFactory
*/
public void setAnnotationViewFactory(AnnotationViewFactory annotationViewFactory) {
this.annotationViewFactory = annotationViewFactory;
}
}
......@@ -64,6 +64,9 @@ public abstract class SearchResultFactory<T, S extends ISearchResultView> extend
@Autowired
private AnnotationViewFactory annotationViewFactory;
@Autowired
private ElementMatcher elementMatcher;
/**
* Returns detailed information about element respective to the set of targets
* (which should be connected to single drug/mirna/chemical).
......@@ -111,92 +114,12 @@ public abstract class SearchResultFactory<T, S extends ISearchResultView> extend
return null;
}
/**
* Check if the target should be applied to the element.
*
* @param target
* drug target to check
* @param element
* {@link Element} to check
* @return <code>true</code> if drug target can target element,
* <code>false</code> otherwise
*/
protected boolean elementMatch(TargetView target, Element element) {
if (target.getType().equals(TargetType.COMPLEX_PROTEIN.getCommonName())) {
MiriamData targetAnnotation = annotationViewFactory.viewToObject(target.getAnnotation());
if (MiriamType.CHEMBL_TARGET.equals(targetAnnotation.getDataType())) {
if (element.getMiriamData().contains(targetAnnotation)) {
return true;
}
}
Collection<String> ids = new HashSet<String>();
for (GeneRow row : target.getProteins()) {
ids.add(row.getAnnotation().getName());
}
if (element instanceof Complex) {
Complex complex = (Complex) element;
Set<Species> speciesSet = complex.getAllSimpleChildren();
if (speciesSet.size() != ids.size()) {
return false;
}
for (Element id : speciesSet) {
if (!ids.contains(id.getName())) {
return false;
}
}
return true;
}
return false;
} else if (target.getType().equals(TargetType.SINGLE_PROTEIN.getCommonName())) {
MiriamData targetAnnotation = null;
if (target.getAnnotation() != null) {
targetAnnotation = annotationViewFactory.viewToObject(target.getAnnotation());
} else if (target.getProteins().size() > 0) {
targetAnnotation = annotationViewFactory.viewToObject(target.getProteins().get(0).getAnnotation());
} else {
logger.warn("Invalid target found: " + target);
}
if (targetAnnotation != null && element.getMiriamData().contains(targetAnnotation)) {
return true;
}
if (element instanceof Protein || element instanceof Rna || element instanceof Gene) {
if (target.getProteins().size() > 0) {
String hgncId = target.getProteins().get(0).getAnnotation().getName();
if (element.getName().equalsIgnoreCase(hgncId)) {
return true;
}
}
}
return false;
} else if (target.getType().equals(TargetType.PROTEIN_FAMILY.getCommonName())) {
if (element instanceof Protein || element instanceof Rna || element instanceof Gene) {
String hgncId = target.getProteins().get(0).getAnnotation().getName();
if (element.getName().equalsIgnoreCase(hgncId)) {
return true;
} else if (element.getMiriamData().contains(new MiriamData(MiriamType.HGNC_SYMBOL, hgncId))) {
return true;
}
for (GeneRow protein : target.getProteins()) {
hgncId = protein.getAnnotation().getName();
if (element.getName().equalsIgnoreCase(hgncId)) {
return true;