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

Merge branch 'unit-tests' into 'master'

Additional unit tests



See merge request !4
parents 8a908bbf 1ec41e34
......@@ -38,7 +38,6 @@ public class MeSHParser extends CachableInterface implements IExternalService {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(MeSHParser.class);
/**
......@@ -135,10 +134,6 @@ public class MeSHParser extends CachableInterface implements IExternalService {
* thrown when there is a problem with accessing mesh db
*/
private MeSH getMeSHByIdFromDB(MiriamData meshID) throws IOException {
if (meshID == null || meshID.getResource() == null) {
throw new InvalidArgumentException("Mesh cannot be null");
}
MeSH result = new MeSH();
String page = getWebPageContent(URL_MESH_DATABASE + meshID.getResource());
......@@ -188,6 +183,9 @@ public class MeSHParser extends CachableInterface implements IExternalService {
j = page.indexOf("</TD>", i);
result.setMeSHId(cleanHtml(page.substring(i, j)));
}
if (result.getMeSHId() == null) {
return null;
}
return result;
}
......@@ -200,10 +198,11 @@ public class MeSHParser extends CachableInterface implements IExternalService {
status.setStatus(ExternalServiceStatusType.OK);
MiriamData meshId = new MiriamData(MiriamType.MESH_2012, "D010300");
MeSH mesh = getMeSH(meshId);
if (mesh == null || (mesh != null && !mesh.getMeSHId().equals(meshId.getResource()))) {
if (mesh == null || !meshId.getResource().equals(mesh.getMeSHId())) {
status.setStatus(ExternalServiceStatusType.CHANGED);
}
} catch (Exception e) {
logger.error(e, e);
status.setStatus(ExternalServiceStatusType.DOWN);
}
this.setCache(cacheCopy);
......
......@@ -25,15 +25,12 @@ import lcsb.mapviewer.annotation.services.annotators.GoAnnotator;
import lcsb.mapviewer.annotation.services.annotators.HgncAnnotator;
import lcsb.mapviewer.annotation.services.annotators.ReconAnnotator;
import lcsb.mapviewer.annotation.services.annotators.UniprotAnnotator;
import lcsb.mapviewer.common.EventStorageLoggerAppender;
import lcsb.mapviewer.common.IProgressUpdater;
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.MiriamType;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelData;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Species;
......@@ -167,13 +164,13 @@ public class ModelAnnotator {
/**
* Performs all possible and automatic annotations on the model.
*
* @param project
* project to update
* @param model
* model to update
* @param progressUpdater
* callback function used for updating progress of the function
*/
public void performAnnotations(Project project, final IProgressUpdater progressUpdater) {
performAnnotations(project, progressUpdater, null);
public void performAnnotations(Model model, final IProgressUpdater progressUpdater) {
performAnnotations(model, progressUpdater, null);
}
/**
......@@ -182,48 +179,37 @@ public class ModelAnnotator {
* @param annotators
* this map contains lists of {@link ElementAnnotator} objects that
* should be used for a given classes
* @param project
* project with models to update
* @param model
* model to update
* @param progressUpdater
* callback function used for updating progress of the function
*/
public void performAnnotations(Project project, final IProgressUpdater progressUpdater, Map<Class<?>, List<ElementAnnotator>> annotators) {
EventStorageLoggerAppender appender = new EventStorageLoggerAppender();
try {
Logger.getRootLogger().addAppender(appender);
progressUpdater.setProgress(0);
List<Model> models = new ArrayList<>();
for (ModelData model: project.getModels()) {
models.add(model.getModel());
models.addAll(model.getModel().getSubmodels());
}
final double size = models.size();
double counter = 0;
for (Model m : models) {
final double ratio = counter / size;
public void performAnnotations(Model model, final IProgressUpdater progressUpdater, Map<Class<?>, List<ElementAnnotator>> annotators) {
progressUpdater.setProgress(0);
List<Model> models = new ArrayList<Model>();
models.add(model);
models.addAll(model.getSubmodels());
final double size = models.size();
double counter = 0;
for (Model m : models) {
final double ratio = counter / size;
copyAnnotationFromOtherSpecies(m, new IProgressUpdater() {
@Override
public void setProgress(final double progress) {
progressUpdater.setProgress(ratio * IProgressUpdater.MAX_PROGRESS + (progress * COPYING_RATIO) / size);
}
});
annotateModel(m, new IProgressUpdater() {
copyAnnotationFromOtherSpecies(m, new IProgressUpdater() {
@Override
public void setProgress(final double progress) {
progressUpdater.setProgress(ratio * IProgressUpdater.MAX_PROGRESS + (progress * COPYING_RATIO) / size);
}
});
annotateModel(m, new IProgressUpdater() {
@Override
public void setProgress(double progress) {
progressUpdater
.setProgress(ratio * IProgressUpdater.MAX_PROGRESS + (COPYING_RATIO * IProgressUpdater.MAX_PROGRESS + progress * ANNOTATING_RATIO) / size);
}
}, annotators);
counter++;
}
Logger.getRootLogger().removeAppender(appender);
project.addLoggingInfo(appender);
} finally {
Logger.getRootLogger().removeAppender(appender);
@Override
public void setProgress(double progress) {
progressUpdater
.setProgress(ratio * IProgressUpdater.MAX_PROGRESS + (COPYING_RATIO * IProgressUpdater.MAX_PROGRESS + progress * ANNOTATING_RATIO) / size);
}
}, annotators);
counter++;
}
}
/**
......@@ -392,7 +378,7 @@ public class ModelAnnotator {
* {@link AnnotatedObject}
* @return list of improper annotations
*/
protected Collection<ImproperAnnotations> findImproperAnnotations(AnnotatedObject element, Collection<MiriamType> validClasses) {
protected List<ImproperAnnotations> findImproperAnnotations(AnnotatedObject element, Collection<MiriamType> validClasses) {
List<ImproperAnnotations> result = new ArrayList<>();
for (MiriamData md : element.getMiriamData()) {
boolean valid = false;
......@@ -512,7 +498,7 @@ public class ModelAnnotator {
* class for which list of annotators will be returned
* @return annotators names for a given class
*/
public Collection<String> getAvailableAnnotatorNames(Class<?> clazz) {
public List<String> getAvailableAnnotatorNames(Class<?> clazz) {
List<String> result = new ArrayList<>();
for (ElementAnnotator annotator : getAvailableAnnotators(clazz)) {
result.add(annotator.getCommonName());
......@@ -528,8 +514,8 @@ public class ModelAnnotator {
* class for which list of default annotators will be returned
* @return annotators names for a given class
*/
public Collection<String> getAvailableDefaultAnnotatorNames(Class<?> clazz) {
List<String> result = new ArrayList<String>();
public List<String> getAvailableDefaultAnnotatorNames(Class<?> clazz) {
List<String> result = new ArrayList<>();
for (ElementAnnotator annotator : getAvailableDefaultAnnotators(clazz)) {
result.add(annotator.getCommonName());
}
......@@ -544,8 +530,8 @@ public class ModelAnnotator {
* class for which list of default annotators will be returned
* @return annotators for a given class
*/
public Collection<ElementAnnotator> getAvailableDefaultAnnotators(Class<?> clazz) {
List<ElementAnnotator> result = new ArrayList<ElementAnnotator>();
public List<ElementAnnotator> getAvailableDefaultAnnotators(Class<?> clazz) {
List<ElementAnnotator> result = new ArrayList<>();
for (ElementAnnotator annotator : availableAnnotators) {
if (annotator.isAnnotatable(clazz) && annotator.isDefault()) {
result.add(annotator);
......@@ -563,7 +549,7 @@ public class ModelAnnotator {
* @return list of {@link ElementAnnotator annotators}
*/
public List<ElementAnnotator> getAnnotatorsFromCommonNames(List<String> list) {
List<ElementAnnotator> result = new ArrayList<ElementAnnotator>();
List<ElementAnnotator> result = new ArrayList<>();
for (String string : list) {
boolean added = false;
for (ElementAnnotator annotator : availableAnnotators) {
......
......@@ -6,11 +6,8 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
......@@ -94,30 +91,6 @@ public class BiocompendiumAnnotator extends ElementAnnotator implements IExterna
return result;
}
/**
* Returns a map with annotation for every {@link MiriamType#HGNC_SYMBOL}
* entry.
*
* @param miriamData
* list of hgnc symbols.
* @return a map with annotation for every {@link MiriamType#HGNC_SYMBOL}
* entry
* @throws IOException
* thrown when there is a problem with accessing external resource
*/
protected Map<MiriamData, String> getAnnotations(List<MiriamData> miriamData) throws IOException {
for (MiriamData md : miriamData) {
if (!MiriamType.HGNC_SYMBOL.equals(md.getDataType())) {
throw new InvalidArgumentException("Only " + MiriamType.HGNC_SYMBOL.getCommonName() + " miriam registry is supported.");
}
}
Map<MiriamData, String> result = new HashMap<>();
for (MiriamData md : miriamData) {
result.put(md, getAnnotation(md));
}
return result;
}
/**
* Returns String with annotations for species described by {@link MiriamData}
* with {@link MiriamType#HGNC_SYMBOL} entry.
......@@ -299,38 +272,6 @@ public class BiocompendiumAnnotator extends ElementAnnotator implements IExterna
return xml;
}
/**
* Pattern used to get hgnc symbol from response from venkata server.
*/
private Pattern hgncSymbolPattern = Pattern.compile("Symbol: ([a-zA-Z0-9_]+)");
/**
* Checks if the result from venkata server is valid (based on hgnc symbol
* information).
*
* @param hgnc
* {@link MiriamData} with {@link MiriamType#HGNC_SYMBOL}
* @return <code>true</code> if venkata annotation is valid,
* <code>false</code> otherwise
* @throws IOException
* throw when there is a problem with accessing external service
*/
public boolean validAnnotation(MiriamData hgnc) throws IOException {
if (!MiriamType.HGNC_SYMBOL.equals(hgnc.getDataType())) {
throw new InvalidArgumentException("Only " + MiriamType.HGNC_SYMBOL.getCommonName() + " miriam registry is supported.");
}
String annotation = getAnnotation(hgnc);
if (annotation == null) {
return false;
}
Matcher m = hgncSymbolPattern.matcher(annotation);
String id = null;
if (m.find()) {
id = m.group(1);
}
return hgnc.getResource().equals(id);
}
@Override
public ExternalServiceStatus getServiceStatus() {
ExternalServiceStatus status = new ExternalServiceStatus(getCommonName(), getUrl());
......@@ -358,9 +299,8 @@ public class BiocompendiumAnnotator extends ElementAnnotator implements IExterna
@Override
public void annotateElement(AnnotatedObject element) throws AnnotatorException {
if (isAnnotatable(element)) {
String annotationString;
try {
annotationString = getAnnotation(new MiriamData(MiriamType.HGNC_SYMBOL, element.getName()));
String annotationString = getAnnotation(new MiriamData(MiriamType.HGNC_SYMBOL, element.getName()));
if (annotationString != null) {
if (element.getMiriamData().size() == 0) {
element.addMiriamData(rap.getMiriamData(annotationString));
......
......@@ -12,8 +12,8 @@ import lcsb.mapviewer.model.map.AnnotatedObject;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Chemical;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Species;
/**
......@@ -326,6 +326,18 @@ public abstract class ElementAnnotator extends CachableInterface {
element.addMiriamData(new MiriamData(MiriamType.PUBCHEM, value));
}
/**
* Adds {@link MiriamType#COG} annotation to element.
*
* @param element
* element where annotation should be added
* @param value
* annotation identifier
*/
protected void addCogMiriam(AnnotatedObject element, String value) {
element.addMiriamData(new MiriamData(MiriamType.COG, value));
}
/**
* Sets {@link Reaction#subsystem}.
*
......@@ -374,6 +386,9 @@ public abstract class ElementAnnotator extends CachableInterface {
} else if (value.startsWith("R")) {
MiriamData md = new MiriamData(MiriamType.KEGG_REACTION, value);
element.addMiriamData(md);
} else if (value.startsWith("K")) {
MiriamData md = new MiriamData(MiriamType.KEGG_ORTHOLOGY, value);
element.addMiriamData(md);
} else {
logger.warn(prefix + "Unknown Kegg identifier type (only Kegg compounds and reactions are supported): \"" + value + "\"");
}
......@@ -393,7 +408,7 @@ public abstract class ElementAnnotator extends CachableInterface {
if (element.getFormula() == null || element.getFormula().isEmpty() || element.getFormula().equals(value)) {
element.setFormula(value);
} else {
logger.warn(prefix + "Formula in db different: \"" + element.getFormula() + "\", " + value);
logger.warn(prefix + "Formula in db different: \"" + element.getFormula() + "\", \"" + value + "\"");
}
}
......@@ -427,10 +442,10 @@ public abstract class ElementAnnotator extends CachableInterface {
*/
protected void setMcs(Reaction element, String value, String prefix) {
if (element.getMechanicalConfidenceScore() == null || element.getMechanicalConfidenceScore() == 0
|| element.getMechanicalConfidenceScore().equals(value)) {
|| element.getMechanicalConfidenceScore().toString().equals(value)) {
element.setMechanicalConfidenceScore(Integer.valueOf(value));
} else {
logger.warn(prefix + "MCS in db different: \"" + element.getMechanicalConfidenceScore() + "\", " + value);
logger.warn(prefix + "MCS in db different: \"" + element.getMechanicalConfidenceScore() + "\", \"" + value + "\"");
}
}
......
......@@ -281,7 +281,7 @@ public class HgncAnnotator extends ElementAnnotator implements IExternalService
"Only " + MiriamType.HGNC + " and " + MiriamType.HGNC_SYMBOL + " are accepted but " + miriamData.getDataType() + " found.");
}
try {
List<MiriamData> result = new ArrayList<MiriamData>();
List<MiriamData> result = new ArrayList<>();
String content = getWebPageContent(query);
Node xml = getXmlDocumentFromString(content);
Node response = getNode("response", xml.getChildNodes());
......
......@@ -17,11 +17,10 @@ import lcsb.mapviewer.annotation.services.ExternalServiceStatusType;
import lcsb.mapviewer.annotation.services.IExternalService;
import lcsb.mapviewer.common.EventStorageLoggerAppender;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerChemical;
import lcsb.mapviewer.model.map.AnnotatedObject;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Chemical;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.SimpleMolecule;
import lcsb.mapviewer.model.map.species.Species;
import lcsb.mapviewer.modelutils.map.ElementUtils;
......@@ -129,7 +128,7 @@ public class ReconAnnotator extends ElementAnnotator implements IExternalService
} else if (annotatedObject instanceof Reaction) {
url = REACTION_ANNOTATION_URL_PREFIX + id;
} else {
logger.warn(elementUtils.getElementTag(annotatedObject) + " Unknown class type: " + annotatedObject.getClass());
logger.warn("Unknown class type: " + annotatedObject.getClass());
return;
}
try {
......@@ -148,7 +147,7 @@ public class ReconAnnotator extends ElementAnnotator implements IExternalService
String value = null;
if (!entry.getValue().isJsonNull()) {
// if (entry.getValue().isJsonPrimitive()) {
value = entry.getValue().getAsString();
value = entry.getValue().getAsString().trim();
}
if (value == null || value.isEmpty()) {
continue;
......@@ -181,14 +180,8 @@ public class ReconAnnotator extends ElementAnnotator implements IExternalService
continue;
} else if (key.equals("lastModified")) {
continue;
} else if (key.equals("modifiedBy")) {
continue;
} else if (key.equals("addedBy")) {
continue;
} else if (key.equals("met_id")) {
continue;
} else if (key.equals("neutralFormula")) {
continue;
} else if (key.equals("csecoida")) {
continue;
} else if (key.equals("chargedFormula")) {
......@@ -205,22 +198,24 @@ public class ReconAnnotator extends ElementAnnotator implements IExternalService
} else {
unknown = true;
}
} else if (key.equals("smile")) {
if (annotatedObject instanceof CellDesignerChemical) {
setSmile((Chemical) annotatedObject, value, prefix);
} else {
unknown = true;
}
} else if (key.equals("cheBlId")) {
addChebiMiriam(annotatedObject, value);
} else if (key.equals("subsystem")) {
setSubsystem((Reaction) annotatedObject, value, prefix);
if (annotatedObject instanceof Reaction) {
setSubsystem((Reaction) annotatedObject, value, prefix);
} else {
unknown = true;
}
} else if (key.equals("keggId")) {
addKeggMiriam(annotatedObject, value, prefix);
} else if (key.equals("keggorthology")) {
addKeggMiriam(annotatedObject, value, prefix);
} else if (key.equals("pubChemId")) {
addPubchemMiriam(annotatedObject, value);
} else if (key.equals("hmdb")) {
addHmdbMiriam(annotatedObject, value);
} else if (key.equals("cog")) {
addCogMiriam(annotatedObject, value);
} else if (key.equals("pdmapName")) {
// for now we skip it...
continue;
......
package lcsb.mapviewer.annotation.services.annotators;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
......@@ -118,11 +117,6 @@ public class UniprotAnnotator extends ElementAnnotator implements IExternalServi
logger.warn(elementUtils.getElementTag(object) + " Invalid uniprot id: " + uniprotId);
}
}
} catch (FileNotFoundException exception) {
// this means that entry with a given uniprot id doesn't exist
if (uniprotFound) {
logger.warn(elementUtils.getElementTag(object) + " Invalid uniprot id: " + uniprotId);
}
} catch (IOException exception) {
throw new AnnotatorException(exception);
}
......
......@@ -8,17 +8,12 @@ package lcsb.mapviewer.annotation.services.genome;
*
*/
public class FileNotAvailableException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public FileNotAvailableException() {
}
/**
* Default constructor.
*
......
package lcsb.mapviewer.annotation.services;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
......@@ -18,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.annotation.AnnotationTestFunctions;
import lcsb.mapviewer.annotation.cache.GeneralCacheInterface;
import lcsb.mapviewer.annotation.cache.GeneralCacheWithExclusion;
import lcsb.mapviewer.annotation.cache.SourceNotAvailable;
import lcsb.mapviewer.annotation.cache.WebPageDownloader;
import lcsb.mapviewer.annotation.data.MeSH;
......@@ -32,20 +34,19 @@ public class MeSHParserTest extends AnnotationTestFunctions {
@Autowired
MeSHParser meshParser;
@Autowired
private GeneralCacheInterface cache;
@Before
public void setUp() throws Exception {
meshParser.setCache(new GeneralCacheWithExclusion(cache, 1));
}
@After
public void tearDown() throws Exception {
meshParser.setCache(cache);
}
/**
* @throws Exception
*/
/**
* @throws Exception
*/
@Test
public void testCreateChemicalListFromDB() throws Exception {
try {
......@@ -59,19 +60,96 @@ public class MeSHParserTest extends AnnotationTestFunctions {
assertTrue(mesh.getDescription() != null);
assertTrue(mesh.getSynonyms().size() > 0);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testIsValidMesh() throws Exception {
try {
MiriamData meshID = new MiriamData(MiriamType.MESH_2012, "D004298");
assertTrue(meshParser.isValidMeshId(meshID));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testIsValidMesh3() throws Exception {
try {
MiriamData meshID = new MiriamData(MiriamType.MESH_2012, "blablabla");
assertFalse(meshParser.isValidMeshId(meshID));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testIsValidMesh2() throws Exception {
try {
assertFalse(meshParser.isValidMeshId(null));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testGetInvalidMesh() throws Exception {
try {
meshParser.getMeSH(null);
fail("Exception expected");