Commit fcc85ab2 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

get chemicals by query moved to controller

parent 07b24b49
package lcsb.mapviewer.annotation.data; package lcsb.mapviewer.annotation.data;
import java.io.Serializable; import java.io.Serializable;
import java.util.*; import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
...@@ -315,8 +318,14 @@ public class Chemical implements Serializable, TargettingStructure { ...@@ -315,8 +318,14 @@ public class Chemical implements Serializable, TargettingStructure {
} }
public void removeTargets(Set<Target> targetsToRemove) { public void removeTargets(Collection<Target> targetsToRemove) {
this.getTargets().removeAll(targetsToRemove); this.getTargets().removeAll(targetsToRemove);
} }
public void addTargets(List<? extends Target> projectTargets) {
for (Target target : projectTargets) {
addTarget(target);
}
}
} }
package lcsb.mapviewer.annotation.data;
import java.util.Comparator;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lcsb.mapviewer.common.comparator.StringComparator;
import lcsb.mapviewer.model.map.BioEntity;
import lcsb.mapviewer.modelutils.serializer.model.map.MinifiedBioEntitySerializer;
public class ProjectTarget extends Target {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final Comparator<? super ProjectTarget> COMPARATOR = new Comparator<ProjectTarget>() {
@Override
public int compare(ProjectTarget o1, ProjectTarget o2) {
Integer size1 = o1.bioEntities.size();
Integer size2 = o2.bioEntities.size();
if (size1 == size2) {
return new StringComparator().compare(o1.getName(), o2.getName());
}
return -size1.compareTo(size2);
}
};
@JsonSerialize(contentUsing = MinifiedBioEntitySerializer.class)
@JsonProperty("targetElements")
private List<BioEntity> bioEntities;
public ProjectTarget(Target target, List<BioEntity> bioEntities) {
setSource(target.getSource());
setName(target.getName());
setOrganism(target.getOrganism());
setAssociatedDisease(target.getAssociatedDisease());
setGenes(target.getGenes());
addReferences(target.getReferences());
setType(target.getType());
this.bioEntities = bioEntities;
}
public List<BioEntity> getBioEntities() {
return bioEntities;
}
public void setBioEntites(List<BioEntity> bioEntities) {
this.bioEntities = bioEntities;
}
}
package lcsb.mapviewer.annotation.data; package lcsb.mapviewer.annotation.data;
import java.io.Serializable; import java.io.Serializable;
import java.util.*; import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lcsb.mapviewer.common.exception.InvalidArgumentException; import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.map.MiriamData; import lcsb.mapviewer.model.map.MiriamData;
...@@ -33,33 +38,40 @@ public class Target implements Serializable { ...@@ -33,33 +38,40 @@ public class Target implements Serializable {
/** /**
* Database from which target was received. * Database from which target was received.
*/ */
@JsonIgnore
private MiriamData source; private MiriamData source;
/** /**
* Name of the target. * Name of the target.
*/ */
@JsonProperty("name")
private String name; private String name;
/** /**
* Organism in which target is located. * Organism in which target is located.
*/ */
@JsonIgnore
private MiriamData organism; private MiriamData organism;
@JsonIgnore
private MiriamData associatedDisease; private MiriamData associatedDisease;
/** /**
* List of genes located in a target. * List of genes located in a target.
*/ */
@JsonProperty("targetParticipants")
private List<MiriamData> genes = new ArrayList<>(); private List<MiriamData> genes = new ArrayList<>();
/** /**
* List of references describing target. * List of references describing target.
*/ */
@JsonProperty("references")
private List<MiriamData> references = new ArrayList<>(); private List<MiriamData> references = new ArrayList<>();
/** /**
* Type of target. * Type of target.
*/ */
@JsonIgnore
private TargetType type; private TargetType type;
/** /**
......
package lcsb.mapviewer.annotation.data.serializer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.PropertyFilter;
import lcsb.mapviewer.annotation.data.Chemical;
import lcsb.mapviewer.annotation.data.MeSH;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.modelutils.serializer.CustomExceptFilter;
public class ChemicalSerializer extends JsonSerializer<Chemical> {
private Logger logger = LogManager.getLogger();
private Function<MiriamData, MeSH> getMesh;
public ChemicalSerializer(Function<MiriamData, MeSH> getMesh) {
this.getMesh = getMesh;
}
@Override
public void serialize(final Chemical chemical, final JsonGenerator gen,
final SerializerProvider serializers)
throws IOException {
PropertyFilter filter = null;
FilterProvider provider = serializers.getConfig().getFilterProvider();
if (provider != null) {
filter = provider.findPropertyFilter("chemicalFilter", null);
}
String description = "Mesh term not available";
List<String> synonyms = new ArrayList<>();
MeSH mesh = getMesh.apply(chemical.getChemicalId());
if (mesh != null) {
description = mesh.getDescription();
synonyms = mesh.getSynonyms();
}
gen.writeStartObject();
for (String string : availableColumns()) {
String column = string.toLowerCase();
Object value = null;
switch (column) {
case "id":
case "idobject":
value = chemical.getChemicalId();
break;
case "name":
value = chemical.getChemicalName();
break;
case "references":
List<MiriamData> references = new ArrayList<>();
references.add(chemical.getChemicalId());
if (chemical.getCasID() != null) {
references.add(chemical.getCasID());
}
value = references;
break;
case "directevidencereferences":
value = chemical.getDirectEvidencePublication();
break;
case "description":
value = description;
break;
case "directevidence":
if (chemical.getDirectEvidence() != null) {
value = chemical.getDirectEvidence().getValue();
}
break;
case "synonyms":
value = synonyms;
break;
case "targets":
value = chemical.getTargets();
break;
default:
value = "Unknown column";
break;
}
writeField(gen, string, value, filter);
}
gen.writeEndObject();
}
private void writeField(JsonGenerator gen, String field, Object value, PropertyFilter filter) throws IOException {
if (filter == null ||
(filter instanceof CustomExceptFilter && ((CustomExceptFilter) filter).includeField(field))) {
gen.writeObjectField(field, value);
}
}
public static List<String> availableColumns() {
return Arrays.asList("name", "references", "description", "synonyms", "id", "directEvidenceReferences", "directEvidence", "targets");
}
}
\ No newline at end of file
package lcsb.mapviewer.annotation.data.serializer;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lcsb.mapviewer.annotation.data.ProjectTarget;
public class ProjectTargetSerializer extends JsonSerializer<ProjectTarget> {
@Override
public void serialize(final ProjectTarget target, final JsonGenerator gen,
final SerializerProvider serializers)
throws IOException {
gen.writeStartObject();
gen.writeStringField("name", target.getName());
gen.writeObjectField("references", target.getReferences());
gen.writeObjectField("targetParticipants", target.getGenes());
gen.writeObjectField("targetElements", target.getBioEntities());
gen.writeEndObject();
}
}
\ No newline at end of file
package lcsb.mapviewer.modelutils.serializer;
import java.util.Collections;
import java.util.HashSet;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter.FilterExceptFilter;
public class CustomExceptFilter extends FilterExceptFilter {
public CustomExceptFilter(String... propertyArray) {
super(new HashSet<>(propertyArray.length));
Collections.addAll(_propertiesToInclude, propertyArray);
}
public boolean includeField(String fieldName) {
return _propertiesToInclude.contains(fieldName);
}
/**
*
*/
private static final long serialVersionUID = 1L;
}
package lcsb.mapviewer.api.projects.chemicals; package lcsb.mapviewer.api.projects.chemicals;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import lcsb.mapviewer.annotation.data.Chemical;
import lcsb.mapviewer.annotation.services.TaxonomyBackend;
import lcsb.mapviewer.annotation.services.dapi.ChemicalSearchException; import lcsb.mapviewer.annotation.services.dapi.ChemicalSearchException;
import lcsb.mapviewer.api.*; import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.modelutils.serializer.CustomExceptFilter;
import lcsb.mapviewer.services.ObjectNotFoundException; import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException; import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IProjectService;
import lcsb.mapviewer.services.search.DbSearchCriteria;
import lcsb.mapviewer.services.search.chemical.IChemicalService;
@RestController @RestController
@RequestMapping(value = "/api/projects", produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "/api/projects", produces = MediaType.APPLICATION_JSON_VALUE)
public class ChemicalController extends BaseController { public class ChemicalController extends BaseController {
private Logger logger = LogManager.getLogger();
private ChemicalRestImpl chemicalController; private ChemicalRestImpl chemicalController;
private IProjectService projectService;
private IChemicalService chemicalService;
@Autowired @Autowired
public ChemicalController(ChemicalRestImpl chemicalController) { public ChemicalController(ChemicalRestImpl chemicalController, IProjectService projectService, IChemicalService chemicalService) {
this.chemicalController = chemicalController; this.chemicalController = chemicalController;
this.projectService = projectService;
this.chemicalService = chemicalService;
} }
@PreAuthorize("hasAnyAuthority('IS_ADMIN', 'READ_PROJECT:' + #projectId)") @PreAuthorize("hasAnyAuthority('IS_ADMIN', 'READ_PROJECT:' + #projectId)")
@GetMapping(value = "/{projectId}/chemicals:search") @GetMapping(value = "/{projectId}/chemicals:search")
public List<Map<String, Object>> getChemicalsByQuery( public MappingJacksonValue getChemicalsByQuery(
@PathVariable(value = "projectId") String projectId, @PathVariable(value = "projectId") String projectId,
@RequestParam(value = "columns", defaultValue = "") String columns, @RequestParam(value = "columns", defaultValue = "") String columns,
@RequestParam(value = "query", defaultValue = "") String query, @RequestParam(value = "query", defaultValue = "") String query,
@RequestParam(value = "target", defaultValue = "") String target) throws QueryException { @RequestParam(value = "target", defaultValue = "") String target) throws QueryException {
List<Chemical> data;
if (!query.equals("")) { if (!query.equals("")) {
return chemicalController.getChemicalsByQuery(projectId, columns, query); Project project = projectService.getProjectByProjectId(projectId);
if (project == null) {
throw new ObjectNotFoundException("Project with given id doesn't exist");
}
if (project.getDisease() == null) {
throw new QueryException("Project doesn't have disease associated to it");
}
MiriamData organism = project.getOrganism();
if (organism == null) {
organism = TaxonomyBackend.HUMAN_TAXONOMY;
}
data = Arrays.asList(
chemicalService.getByName(query, new DbSearchCriteria().project(project).organisms(organism).colorSet(0).disease(project.getDisease())));
} else if (target.contains(":")) { } else if (target.contains(":")) {
String targetType = target.split(":", -1)[0]; String targetType = target.split(":", -1)[0];
String targetId = target.split(":", -1)[1]; String targetId = target.split(":", -1)[1];
return chemicalController.getChemicalsByTarget(projectId, targetType, targetId, columns); data = chemicalController.getChemicalsByTarget(projectId, targetType, targetId, columns);
} else { } else {
return new ArrayList<>(); data = new ArrayList<>();
} }
MappingJacksonValue result = new MappingJacksonValue(data);
if (!columns.trim().isEmpty()) {
String[] columnList = columns.split(",");
SimpleFilterProvider provider = new SimpleFilterProvider();
provider.addFilter("chemicalFilter",
new CustomExceptFilter(columnList));
result.setFilters(provider);
}
return result;
} }
@PreAuthorize("hasAnyAuthority('IS_ADMIN', 'READ_PROJECT:' + #projectId)") @PreAuthorize("hasAnyAuthority('IS_ADMIN', 'READ_PROJECT:' + #projectId)")
......
...@@ -53,33 +53,6 @@ public class ChemicalRestImpl extends BaseRestImpl { ...@@ -53,33 +53,6 @@ public class ChemicalRestImpl extends BaseRestImpl {
this.elementService = elementService; this.elementService = elementService;
} }
public List<Map<String, Object>> getChemicalsByQuery(String projectId, String columns, String query)
throws QueryException {
Project project = getProjectService().getProjectByProjectId(projectId);
if (project == null) {
throw new ObjectNotFoundException("Project with given id doesn't exist");
}
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().project(project).organisms(organism).colorSet(0).disease(project.getDisease()));
if (chemical != null) {
result.add(prepareChemical(chemical, columnSet, project));
}
return result;
}
protected Map<String, Object> prepareChemical(Chemical chemical, Set<String> columnsSet, Project project) { protected Map<String, Object> prepareChemical(Chemical chemical, Set<String> columnsSet, Project project) {
Map<String, Object> result = new TreeMap<>(); Map<String, Object> result = new TreeMap<>();
...@@ -102,41 +75,41 @@ public class ChemicalRestImpl extends BaseRestImpl { ...@@ -102,41 +75,41 @@ public class ChemicalRestImpl extends BaseRestImpl {
String column = string.toLowerCase(); String column = string.toLowerCase();
Object value = null; Object value = null;
switch (column) { switch (column) {
case "id": case "id":
case "idobject": case "idobject":
value = chemical.getChemicalId(); value = chemical.getChemicalId();
break; break;
case "name": case "name":
value = chemical.getChemicalName(); value = chemical.getChemicalName();
break; break;
case "references": case "references":
List<MiriamData> references = new ArrayList<>(); List<MiriamData> references = new ArrayList<>();
references.add(chemical.getChemicalId()); references.add(chemical.getChemicalId());
if (chemical.getCasID() != null) { if (chemical.getCasID() != null) {
references.add(chemical.getCasID()); references.add(chemical.getCasID());
} }
value = references; value = references;
break; break;
case "directevidencereferences": case "directevidencereferences":
value = chemical.getDirectEvidencePublication(); value = chemical.getDirectEvidencePublication();
break; break;
case "description": case "description":
value = description; value = description;
break; break;
case "directevidence": case "directevidence":
if (chemical.getDirectEvidence() != null) { if (chemical.getDirectEvidence() != null) {
value = chemical.getDirectEvidence().getValue(); value = chemical.getDirectEvidence().getValue();