Commit 4b38b27a authored by Piotr Gawron's avatar Piotr Gawron
Browse files

Merge branch '178-mirna-show-all-duplicated-entries' into 'master'

Resolve "MiRNA - Show all - duplicated entries"

Closes #178

See merge request !75
parents e0b15dbc 09a9a2dd
......@@ -448,12 +448,14 @@ public class PermanentDatabaseLevelCache extends XmlParser implements PermanentD
try {
Document document = getXmlDocumentFromString(entry.getValue());
Calendar currentDate = Calendar.getInstance();
if (currentDate.before(entry.getExpires())) {
return document.getFirstChild();
} else {
if (currentDate.after(entry.getExpires())) {
cacheRefreshService.submit(new RefreshTask(query, type));
return document.getFirstChild();
}
Node result = null;
if (document != null) {
result = document.getFirstChild();
}
return result;
} catch (InvalidXmlSchemaException e) {
logger.warn("Invalid xml for query: " + query);
logger.warn("xml: " + entry.getValue());
......
......@@ -8,6 +8,7 @@ import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.lang3.SerializationException;
import org.apache.log4j.Logger;
import org.w3c.dom.Node;
/**
......@@ -21,6 +22,11 @@ import org.w3c.dom.Node;
*/
public class XmlSerializer<T> {
/**
* Default class logger.
*/
private final Logger logger = Logger.getLogger(XmlSerializer.class);
/**
* Class of the object that DAO works on.
*/
......@@ -74,7 +80,8 @@ public class XmlSerializer<T> {
} catch (JAXBException e) {
throw new SerializationException(e);
} catch (Exception e) {
throw new SerializationException(e);
logger.error(e, e);
return null;
}
return sw.toString();
}
......@@ -95,8 +102,9 @@ public class XmlSerializer<T> {
return (T) jaxbUnmarshaller.unmarshal(node);
} catch (JAXBException e) {
throw new SerializationException(e);
} catch (Exception e) { //the library can throw NPE sometimes...
throw new SerializationException(e);
} catch (Exception e) { // the library can throw NPE sometimes...
logger.error(e, e);
return null;
}
}
......
......@@ -2,12 +2,15 @@ package lcsb.mapviewer.annotation.data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang3.StringUtils;
import lcsb.mapviewer.common.comparator.StringComparator;
import lcsb.mapviewer.model.map.MiriamData;
/**
......@@ -17,7 +20,7 @@ import lcsb.mapviewer.model.map.MiriamData;
*
*/
@XmlRootElement
public class Chemical implements Serializable {
public class Chemical implements Serializable, TargettingStructure {
/**
*
......@@ -269,4 +272,36 @@ public class Chemical implements Serializable {
return result.toString();
}
/**
* Comparator of the objects by their name.
*
* @author Piotr Gawron
*
*/
public static class NameComparator implements Comparator<Chemical> {
/**
* Default string comparator.
*/
private StringComparator stringComparator = new StringComparator();
@Override
public int compare(Chemical arg0, Chemical arg1) {
return stringComparator.compare(arg0.getChemicalName(), arg1.getChemicalName());
}
}
@Override
public Collection<MiriamData> getSources() {
List<MiriamData> sources = new ArrayList<>();
sources.add(getCasID());
sources.add(getChemicalId());
return sources;
}
@Override
public Collection<Target> getTargets() {
return getInferenceNetwork();
}
}
......@@ -2,6 +2,7 @@ package lcsb.mapviewer.annotation.data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
......@@ -9,6 +10,7 @@ import javax.xml.bind.annotation.XmlRootElement;
import org.apache.log4j.Logger;
import lcsb.mapviewer.common.comparator.StringComparator;
import lcsb.mapviewer.model.map.MiriamData;
/**
......@@ -18,7 +20,7 @@ import lcsb.mapviewer.model.map.MiriamData;
*
*/
@XmlRootElement
public class Drug implements Serializable {
public class Drug implements Serializable, TargettingStructure {
/**
*
......@@ -309,4 +311,22 @@ public class Drug implements Serializable {
this.approved = approved;
}
/**
* Comparator of the objects by their name.
*
* @author Piotr Gawron
*
*/
public static class NameComparator implements Comparator<Drug> {
/**
* Default string comparator.
*/
private StringComparator stringComparator = new StringComparator();
@Override
public int compare(Drug arg0, Drug arg1) {
return stringComparator.compare(arg0.getName(), arg1.getName());
}
}
}
\ No newline at end of file
......@@ -2,10 +2,15 @@ package lcsb.mapviewer.annotation.data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import lcsb.mapviewer.common.comparator.StringComparator;
import lcsb.mapviewer.model.map.MiriamData;
/**
* Object representing element obtained from Toxigenomic database.
*
......@@ -13,7 +18,7 @@ import javax.xml.bind.annotation.XmlRootElement;
*
*/
@XmlRootElement
public class MiRNA implements Serializable {
public class MiRNA implements Serializable, TargettingStructure {
/**
*
......@@ -100,4 +105,28 @@ public class MiRNA implements Serializable {
this.targets.add(target);
}
@Override
public Collection<MiriamData> getSources() {
return new ArrayList<>();
}
/**
* Comparator of the objects by their name.
*
* @author Piotr Gawron
*
*/
public static class NameComparator implements Comparator<MiRNA> {
/**
* Default string comparator.
*/
private StringComparator stringComparator = new StringComparator();
@Override
public int compare(MiRNA arg0, MiRNA arg1) {
return stringComparator.compare(arg0.getName(), arg1.getName());
}
}
}
package lcsb.mapviewer.annotation.data;
import java.util.Collection;
import lcsb.mapviewer.model.map.MiriamData;
/**
* Interface for all externall objects that interact with elements on the map.
*
* @author Piotr Gawron
*
*/
public interface TargettingStructure {
/**
*
* @return list of {@link MiriamData annotations} that describe source of this
* object
*/
Collection<MiriamData> getSources();
/**
*
* @return list of {@link Target targets} with which object is interacting
*/
Collection<Target> getTargets();
}
......@@ -181,7 +181,7 @@ public class MiRNAParser extends CachableInterface implements IExternalService {
* @throws MiRNASearchException
* thrown when there is a problem with accessing mirna database
*/
public List<MiRNA> getMiRnasByNames(List<String> names) throws MiRNASearchException {
public List<MiRNA> getMiRnasByNames(Collection<String> names) throws MiRNASearchException {
List<MiRNA> miRNAs = new ArrayList<>();
List<String> idsToRemove = new ArrayList<>();
MiRNA miRNA = null;
......@@ -419,7 +419,7 @@ public class MiRNAParser extends CachableInterface implements IExternalService {
* thrown when there is a problem with accessing mirna database
*/
public List<MiRNA> getMiRnaListByTargets(Collection<MiriamData> targetsMiriam) throws MiRNASearchException {
List<String> mirnaNames = new ArrayList<>();
Set<String> mirnaNames = new HashSet<>();
Set<MiriamData> targets = new HashSet<>();
for (MiriamData miriamData : targetsMiriam) {
if (!MiriamType.HGNC_SYMBOL.equals(miriamData.getDataType()) && !MiriamType.ENTREZ.equals(miriamData.getDataType())) {
......
......@@ -183,7 +183,11 @@ public class PubmedParser extends CachableInterface implements IExternalService
result.setId(id + "");
if (result.getTitle() != null && !result.getTitle().trim().isEmpty()) {
setCacheValue(queryString, articleSerializer.objectToString(result));
try {
setCacheValue(queryString, articleSerializer.objectToString(result));
} catch (SerializationException e) {
logger.warn("Problem with serialization of the string: " + queryString);
}
} else {
result = null;
}
......
......@@ -11,11 +11,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.annotation.data.Article;
import lcsb.mapviewer.annotation.data.Target;
import lcsb.mapviewer.annotation.services.MiriamConnector;
import lcsb.mapviewer.annotation.services.PubmedParser;
import lcsb.mapviewer.annotation.services.PubmedSearchException;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.model.map.BioEntity;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.model.Model;
......@@ -24,6 +26,7 @@ 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.view.AnnotationView;
......@@ -43,6 +46,9 @@ public abstract class BaseRestImpl {
@Autowired
private PubmedParser pubmedParser;
@Autowired
private ElementMatcher elementMatcher;
protected Map<String, Object> okStatus() {
Map<String, Object> result = new HashMap<>();
return result;
......@@ -189,4 +195,48 @@ public abstract class BaseRestImpl {
this.userService = userService;
}
protected List<Map<String, Object>> prepareTargets(Collection<Target> targets, List<Model> models) {
List<Map<String, Object>> result = new ArrayList<>();
for (Target target : targets) {
result.add(prepareTarget(target, models));
}
return result;
}
protected Map<String, Object> prepareTarget(Target target, List<Model> models) {
Map<String, Object> result = new HashMap<>();
result.put("name", target.getName());
result.put("references", createAnnotations(target.getReferences()));
result.put("targetParticipants", createAnnotations(target.getGenes()));
List<Map<String, Object>> targetedObjects = new ArrayList<>();
for (Model model : models) {
for (BioEntity object : model.getBioEntities()) {
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(BioEntity 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());
}
}
}
package lcsb.mapviewer.api.projects.chemicals;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
......@@ -12,27 +11,23 @@ import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.annotation.data.Chemical;
import lcsb.mapviewer.annotation.data.MeSH;
import lcsb.mapviewer.annotation.services.MeSHParser;
import lcsb.mapviewer.annotation.services.TaxonomyBackend;
import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.BioEntity;
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")
......@@ -54,7 +49,7 @@ public class ChemicalRestImpl extends BaseRestImpl {
private IUserService userService;
@Autowired
private ElementMatcher elementMatcher;
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);
......@@ -75,8 +70,7 @@ public class ChemicalRestImpl extends BaseRestImpl {
if (organism == null) {
organism = TaxonomyBackend.HUMAN_TAXONOMY;
}
ChemicalView chemical = chemicalService
.getByName(query, new DbSearchCriteria().model(model).organisms(organism).colorSet(0).disease(project.getDisease()));
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));
......@@ -126,30 +120,42 @@ public class ChemicalRestImpl extends BaseRestImpl {
this.modelService = modelService;
}
private Map<String, Object> prepareChemical(ChemicalView chemical, Set<String> columnsSet, List<Model> models) {
private 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.getUniqueId();
value = chemical.getChemicalId();
} else if (column.equals("name")) {
value = chemical.getName();
value = chemical.getChemicalName();
} else if (column.equals("references")) {
List<Map<String, Object>> references = new ArrayList<>();
references.add(createAnnotation(chemical.getSourceLink()));
references.add(createAnnotation(chemical.getCasLink()));
references.add(createAnnotation(chemical.getChemicalId()));
references.add(createAnnotation(chemical.getCasID()));
value = references;
} else if (column.equals("directevidencereferences")) {
value = chemical.getDirectEvidencePubs();
value = createAnnotations(chemical.getDirectEvidencePublication());
} else if (column.equals("description")) {
value = chemical.getDescription();
value = description;
} else if (column.equals("directevidence")) {
value = chemical.getDirectEvedince();
value = chemical.getDirectEvidence().getValue();
} else if (column.equals("synonyms")) {
value = chemical.getSynonyms();
value = synonyms;
} else if (column.equals("targets")) {
value = prepareTargets(chemical.getTargetRows(), models);
value = prepareTargets(chemical.getInferenceNetwork(), models);
} else {
value = "Unknown column";
}
......@@ -158,54 +164,6 @@ public class ChemicalRestImpl extends BaseRestImpl {
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", createAnnotations(target.getReferences()));
List<Object> participants = new ArrayList<>();
for (GeneRow row : target.getProteins()) {
participants.add(createAnnotation(row.getAnnotation()));
}
result.put("targetParticipants", participants);
List<Map<String, Object>> targetedObjects = new ArrayList<>();
for (Model model : models) {
for (BioEntity object : model.getBioEntities()) {
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(BioEntity 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("")) {
......@@ -278,14 +236,13 @@ public class ChemicalRestImpl extends BaseRestImpl {
Set<String> columnSet = createChemicalColumnSet(columns);
List<ChemicalView> chemicals = chemicalService
.getForTargets(targets, new DbSearchCriteria().model(model).organisms(organism).disease(project.getDisease()));
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 (ChemicalView chemical : chemicals) {
for (Chemical chemical : chemicals) {
result.add(prepareChemical(chemical, columnSet, models));
}
......
package lcsb.mapviewer.api.projects.drugs;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
......@@ -12,16 +11,13 @@ import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.annotation.data.Article;
import lcsb.mapviewer.annotation.data.Drug;
import lcsb.mapviewer.annotation.services.TaxonomyBackend;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.BioEntity;
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;
......@@ -29,11 +25,7 @@ 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.T