diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/AnnotatorConfigParameter.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/AnnotatorConfigParameter.java index 01bce8df9860d4593806612cb6abfe3d3e76e4a3..6421efe5dadc54f6949d6021cf8253b6b57026a1 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/AnnotatorConfigParameter.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/AnnotatorConfigParameter.java @@ -5,7 +5,7 @@ import lcsb.mapviewer.model.user.AnnotatorParamDefinition; public class AnnotatorConfigParameter extends AnnotatorParameter { private String value; - + private AnnotatorParamDefinition type; public AnnotatorConfigParameter(AnnotatorParamDefinition type, String value) { diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/AnnotatorException.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/AnnotatorException.java index 34ebfe92eec012dfc78373fdc4186200114c49ca..bd2ce021eddfa5dfd7e22e9cb39b543046bc884c 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/AnnotatorException.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/AnnotatorException.java @@ -8,41 +8,41 @@ package lcsb.mapviewer.annotation.services.annotators; */ public class AnnotatorException extends Exception { - /** - * - */ - private static final long serialVersionUID = 1L; + /** + * + */ + private static final long serialVersionUID = 1L; - /** - * Default constructor. - * - * @param exception - * original exception - */ - public AnnotatorException(Exception exception) { - super(exception); - } + /** + * Default constructor. + * + * @param exception + * original exception + */ + public AnnotatorException(Exception exception) { + super(exception); + } - /** - * Default constructor. - * - * @param message - * message associated with exception - * @param exception - * original exception - */ - public AnnotatorException(String message, Exception exception) { - super(message, exception); - } + /** + * Default constructor. + * + * @param message + * message associated with exception + * @param exception + * original exception + */ + public AnnotatorException(String message, Exception exception) { + super(message, exception); + } - /** - * Default constructor. - * - * @param message - * message associated with exception - */ - public AnnotatorException(String message) { - super(message); - } + /** + * Default constructor. + * + * @param message + * message associated with exception + */ + public AnnotatorException(String message) { + super(message); + } } diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/BiocompendiumAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/BiocompendiumAnnotator.java index 9ea6a6296246dea43ae82d138a155ad13ba3243b..26c20f739fc3058b4ba9eaca2444d97033d61838 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/BiocompendiumAnnotator.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/BiocompendiumAnnotator.java @@ -59,249 +59,248 @@ import lcsb.mapviewer.model.map.species.Rna; @Service public class BiocompendiumAnnotator extends ElementAnnotator implements IExternalService { - /** - * Address of the restfull API for biocompendium. - */ - private static final String SERVICE_ADDRESS = "http://biocompendium.embl.de/map_annotator/REST/GetAnnotation/"; - - /** - * Standard class logger. - */ - private final Logger logger = Logger.getLogger(BiocompendiumAnnotator.class); - - /** - * Xml parser used for processing notes into structured data. - */ - private RestAnnotationParser rap = new RestAnnotationParser(); - - /** - * Default constructor. - */ - public BiocompendiumAnnotator() { - super(BiocompendiumAnnotator.class, new Class[] { Protein.class, Gene.class, Rna.class }, true); - } - - @Override - public String refreshCacheQuery(Object query) throws SourceNotAvailable { - String result = null; - try { - if (query instanceof String) { - String name = (String) query; - result = getAnnotation(createMiriamData(MiriamType.HGNC_SYMBOL, name)); - } else { - throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); - } - } catch (IOException e) { - throw new SourceNotAvailable(e); - } - return result; - } - - /** - * Returns String with annotations for species described by {@link MiriamData} - * with {@link MiriamType#HGNC_SYMBOL} entry. - * - * @param md - * description of the element by {@link MiriamData} with - * {@link MiriamType#HGNC_SYMBOL} entry - * @return annotation for species with given name and type from Venkata server - * @throws IOException - * thrown when there is a problem with connection to the server - */ - protected String getAnnotation(MiriamData md) throws IOException { - if (!MiriamType.HGNC_SYMBOL.equals(md.getDataType())) { - throw new InvalidArgumentException("Only " + MiriamType.HGNC_SYMBOL.getCommonName() + " miriam registry is supported."); - } - - MiriamData miriamData = createMiriamData(md); - miriamData.setResource(miriamData.getResource().replaceAll("[\n\r]+", " ")); - - String annotation = getCacheValue(miriamData.getResource()); - if (annotation != null) { - return annotation; - } - - CloseableHttpClient httpClient = HttpClients.createDefault(); - - HttpPost httppost = new HttpPost(SERVICE_ADDRESS); - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - - // prepare a query - String requestXml = dataToString(miriamData); - - // save query to a temporary file - File tmp = File.createTempFile("annotation-plugin", "xml"); - BufferedWriter out = new BufferedWriter(new FileWriter(tmp)); - out.write(requestXml); - out.close(); - - builder.addBinaryBody("File", tmp, ContentType.APPLICATION_OCTET_STREAM, tmp.getName()); - HttpEntity multipart = builder.build(); - httppost.setEntity(multipart); - CloseableHttpResponse response = httpClient.execute(httppost); - - HttpEntity entity2 = response.getEntity(); - if (entity2 != null) { - InputStream instream = entity2.getContent(); - Scanner scanner = new Scanner(instream); - try { - Scanner s = scanner.useDelimiter("\\A"); - // get results - String responseXml = ""; - if (s.hasNext()) { - responseXml = s.next(); - } - try { - annotation = getAnnotationsFromXml(responseXml).get(miriamData.getResource()); - } catch (InvalidXmlSchemaException e) { - throw new IOException(e); - } - - } finally { - scanner.close(); - instream.close(); - } - } - if (annotation != null) { - setCacheValue(miriamData.getResource(), annotation); - } - return annotation; - } - - /** - * This method convert a hgnc symbol in miriam data into xml request. - * - * @param miriamData - * - {@link MiriamData} with {@link MiriamType#HGNC_SYMBOL} - * @return xml string that represent a request to RESTful service - */ - String dataToString(MiriamData miriamData) { - if (!MiriamType.HGNC_SYMBOL.equals(miriamData.getDataType())) { - throw new InvalidArgumentException("Only " + MiriamType.HGNC_SYMBOL.getCommonName() + " miriam registry is supported."); - } - String requestXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; - requestXml += "<request>\n"; - requestXml += " <species name=\"" + miriamData.getResource() + "\" type=\"PROTEIN\"/>\n"; - requestXml += "</request>\n"; - return requestXml; - } - - /** - * This method parse a response xml from annotation service into a map. A key - * in the map is a name of species and value is a annotation received from the - * service. - * - * @param xml - * - xml string to be parsed - * @return a map with information about annotations. A key in the map is a - * name of species and value is a annotation received from the - * service. - * @throws InvalidXmlSchemaException - * thrown when there is a problem with xml - */ - Map<String, String> getAnnotationsFromXml(String xml) throws InvalidXmlSchemaException { - Map<String, String> result = new HashMap<String, String>(); - // if xml is null or empty string then return empty map - if (xml == null) { - return result; - } - if (xml.isEmpty()) { - return result; - } - // there is a bug in annotation service that create incorrect xml, therefore - // we need to alter the xml - xml = correctInvalidXml(xml); - - Document doc = getXmlDocumentFromString(xml); - result = getAnnotationsFromXmlNode(doc); - return result; - - } - - /** - * Transforms xml retrieved from Venkata server into map of annotations. A key - * in the map is a name of species and value is a annotation received from the - * service. - * - * @param doc - * - xml node - * @return a map with information about annotations. A key in the map is a - * name of species and value is a annotation received from the - * service. - */ - private Map<String, String> getAnnotationsFromXmlNode(Node doc) { - Map<String, String> result = new HashMap<String, String>(); - NodeList root = doc.getChildNodes(); - Node responseNode = getNode("response", root); - // root node is called "response" - if (responseNode == null) { - throw new InvalidArgumentException("Invalid xml returned by annotation service. No response root node"); - } - NodeList list = responseNode.getChildNodes(); - for (int i = 0; i < list.getLength(); i++) { - Node node = list.item(i); - // all nodes in response are species - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("species")) { - String name = getNodeAttr("name", node).replaceAll("%0A", "\n"); - String annotation = getNodeValue(node); - if (annotation.contains("Symbol: " + name.split(" ")[0].toUpperCase())) { - result.put(name, annotation); - } else { - logger.warn("Problem with annotation for: " + name); - result.put(name, ""); - } - } else { - throw new InvalidArgumentException("Unknown node in xml response: " + node.getNodeName()); - } - } - } - return result; - } - - /** - * Corrects invalid xml retrieved from Venkata server. - * - * @param xml - * xml to correct - * @return corrected xml - */ - private String correctInvalidXml(String xml) { - int size = 0; - int size2 = 0; - do { - size = xml.length(); - xml = xml.replaceAll("<species name=\"[^\"]*\" type=\"[^\"]*\">(\\s)*<species", "<species"); - xml = xml.replaceAll("<species name=\"[^\"]*\" type=\"[^\"]*\">(\\s)*</response", "</response"); - size2 = xml.length(); - } while (size != size2); - return xml; - } - - @Override - public ExternalServiceStatus getServiceStatus() { - ExternalServiceStatus status = new ExternalServiceStatus(getCommonName(), getUrl()); - - GeneralCacheInterface cacheCopy = getCache(); - this.setCache(null); - - try { - String annotations = getAnnotation(createMiriamData(MiriamType.HGNC_SYMBOL, "SNCA")); - - status.setStatus(ExternalServiceStatusType.OK); - if (annotations == null) { - status.setStatus(ExternalServiceStatusType.DOWN); - } else if (!annotations.contains("ymbol")) { - status.setStatus(ExternalServiceStatusType.CHANGED); - } - } catch (Exception e) { - logger.error(status.getName() + " is down", e); - status.setStatus(ExternalServiceStatusType.DOWN); - } - this.setCache(cacheCopy); - return status; - } + /** + * Address of the restfull API for biocompendium. + */ + private static final String SERVICE_ADDRESS = "http://biocompendium.embl.de/map_annotator/REST/GetAnnotation/"; + + /** + * Standard class logger. + */ + private final Logger logger = Logger.getLogger(BiocompendiumAnnotator.class); + + /** + * Xml parser used for processing notes into structured data. + */ + private RestAnnotationParser rap = new RestAnnotationParser(); + + /** + * Default constructor. + */ + public BiocompendiumAnnotator() { + super(BiocompendiumAnnotator.class, new Class[] { Protein.class, Gene.class, Rna.class }, true); + } + + @Override + public String refreshCacheQuery(Object query) throws SourceNotAvailable { + String result = null; + try { + if (query instanceof String) { + String name = (String) query; + result = getAnnotation(createMiriamData(MiriamType.HGNC_SYMBOL, name)); + } else { + throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); + } + } catch (IOException e) { + throw new SourceNotAvailable(e); + } + return result; + } + + /** + * Returns String with annotations for species described by {@link MiriamData} + * with {@link MiriamType#HGNC_SYMBOL} entry. + * + * @param md + * description of the element by {@link MiriamData} with + * {@link MiriamType#HGNC_SYMBOL} entry + * @return annotation for species with given name and type from Venkata server + * @throws IOException + * thrown when there is a problem with connection to the server + */ + protected String getAnnotation(MiriamData md) throws IOException { + if (!MiriamType.HGNC_SYMBOL.equals(md.getDataType())) { + throw new InvalidArgumentException( + "Only " + MiriamType.HGNC_SYMBOL.getCommonName() + " miriam registry is supported."); + } + + MiriamData miriamData = createMiriamData(md); + miriamData.setResource(miriamData.getResource().replaceAll("[\n\r]+", " ")); + + String annotation = getCacheValue(miriamData.getResource()); + if (annotation != null) { + return annotation; + } + CloseableHttpClient httpClient = HttpClients.createDefault(); + + HttpPost httppost = new HttpPost(SERVICE_ADDRESS); + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + + // prepare a query + String requestXml = dataToString(miriamData); + + // save query to a temporary file + File tmp = File.createTempFile("annotation-plugin", "xml"); + BufferedWriter out = new BufferedWriter(new FileWriter(tmp)); + out.write(requestXml); + out.close(); + + builder.addBinaryBody("File", tmp, ContentType.APPLICATION_OCTET_STREAM, tmp.getName()); + HttpEntity multipart = builder.build(); + httppost.setEntity(multipart); + CloseableHttpResponse response = httpClient.execute(httppost); + + HttpEntity entity2 = response.getEntity(); + if (entity2 != null) { + InputStream instream = entity2.getContent(); + Scanner scanner = new Scanner(instream); + try { + Scanner s = scanner.useDelimiter("\\A"); + // get results + String responseXml = ""; + if (s.hasNext()) { + responseXml = s.next(); + } + try { + annotation = getAnnotationsFromXml(responseXml).get(miriamData.getResource()); + } catch (InvalidXmlSchemaException e) { + throw new IOException(e); + } + + } finally { + scanner.close(); + instream.close(); + } + } + if (annotation != null) { + setCacheValue(miriamData.getResource(), annotation); + } + return annotation; + } + + /** + * This method convert a hgnc symbol in miriam data into xml request. + * + * @param miriamData + * - {@link MiriamData} with {@link MiriamType#HGNC_SYMBOL} + * @return xml string that represent a request to RESTful service + */ + String dataToString(MiriamData miriamData) { + if (!MiriamType.HGNC_SYMBOL.equals(miriamData.getDataType())) { + throw new InvalidArgumentException( + "Only " + MiriamType.HGNC_SYMBOL.getCommonName() + " miriam registry is supported."); + } + String requestXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; + requestXml += "<request>\n"; + requestXml += " <species name=\"" + miriamData.getResource() + "\" type=\"PROTEIN\"/>\n"; + requestXml += "</request>\n"; + return requestXml; + } + + /** + * This method parse a response xml from annotation service into a map. A key in + * the map is a name of species and value is a annotation received from the + * service. + * + * @param xml + * - xml string to be parsed + * @return a map with information about annotations. A key in the map is a name + * of species and value is a annotation received from the service. + * @throws InvalidXmlSchemaException + * thrown when there is a problem with xml + */ + Map<String, String> getAnnotationsFromXml(String xml) throws InvalidXmlSchemaException { + Map<String, String> result = new HashMap<String, String>(); + // if xml is null or empty string then return empty map + if (xml == null) { + return result; + } + if (xml.isEmpty()) { + return result; + } + // there is a bug in annotation service that create incorrect xml, therefore + // we need to alter the xml + xml = correctInvalidXml(xml); + + Document doc = getXmlDocumentFromString(xml); + result = getAnnotationsFromXmlNode(doc); + return result; + + } + + /** + * Transforms xml retrieved from Venkata server into map of annotations. A key + * in the map is a name of species and value is a annotation received from the + * service. + * + * @param doc + * - xml node + * @return a map with information about annotations. A key in the map is a name + * of species and value is a annotation received from the service. + */ + private Map<String, String> getAnnotationsFromXmlNode(Node doc) { + Map<String, String> result = new HashMap<String, String>(); + NodeList root = doc.getChildNodes(); + Node responseNode = getNode("response", root); + // root node is called "response" + if (responseNode == null) { + throw new InvalidArgumentException("Invalid xml returned by annotation service. No response root node"); + } + NodeList list = responseNode.getChildNodes(); + for (int i = 0; i < list.getLength(); i++) { + Node node = list.item(i); + // all nodes in response are species + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("species")) { + String name = getNodeAttr("name", node).replaceAll("%0A", "\n"); + String annotation = getNodeValue(node); + if (annotation.contains("Symbol: " + name.split(" ")[0].toUpperCase())) { + result.put(name, annotation); + } else { + logger.warn("Problem with annotation for: " + name); + result.put(name, ""); + } + } else { + throw new InvalidArgumentException("Unknown node in xml response: " + node.getNodeName()); + } + } + } + return result; + } + + /** + * Corrects invalid xml retrieved from Venkata server. + * + * @param xml + * xml to correct + * @return corrected xml + */ + private String correctInvalidXml(String xml) { + int size = 0; + int size2 = 0; + do { + size = xml.length(); + xml = xml.replaceAll("<species name=\"[^\"]*\" type=\"[^\"]*\">(\\s)*<species", "<species"); + xml = xml.replaceAll("<species name=\"[^\"]*\" type=\"[^\"]*\">(\\s)*</response", "</response"); + size2 = xml.length(); + } while (size != size2); + return xml; + } + + @Override + public ExternalServiceStatus getServiceStatus() { + ExternalServiceStatus status = new ExternalServiceStatus(getCommonName(), getUrl()); + + GeneralCacheInterface cacheCopy = getCache(); + this.setCache(null); + + try { + String annotations = getAnnotation(createMiriamData(MiriamType.HGNC_SYMBOL, "SNCA")); + + status.setStatus(ExternalServiceStatusType.OK); + if (annotations == null) { + status.setStatus(ExternalServiceStatusType.DOWN); + } else if (!annotations.contains("ymbol")) { + status.setStatus(ExternalServiceStatusType.CHANGED); + } + } catch (Exception e) { + logger.error(status.getName() + " is down", e); + status.setStatus(ExternalServiceStatusType.DOWN); + } + this.setCache(cacheCopy); + return status; + } @Override public boolean annotateElement(BioEntity element, MiriamData identifier, AnnotationParameters parameters) @@ -340,15 +339,15 @@ public class BiocompendiumAnnotator extends ElementAnnotator implements IExterna } - @Override - public String getCommonName() { - return "Biocompendium"; - } + @Override + public String getCommonName() { + return "Biocompendium"; + } - @Override - public String getUrl() { - return "http://biocompendium.embl.de/"; - } + @Override + public String getUrl() { + return "http://biocompendium.embl.de/"; + } @Override public List<AnnotatorInputParameter> getAvailableInputParameters() { diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/CazyAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/CazyAnnotator.java index 2a1bbd975343456d69a0eb511fb7507418aadce1..01156488e9a8c43a199da7af2b6980454cc70efe 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/CazyAnnotator.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/CazyAnnotator.java @@ -11,6 +11,7 @@ import java.util.regex.Pattern; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; import lcsb.mapviewer.annotation.cache.GeneralCacheInterface; import lcsb.mapviewer.annotation.cache.SourceNotAvailable; @@ -27,7 +28,6 @@ import lcsb.mapviewer.model.map.MiriamType; import lcsb.mapviewer.model.map.species.Gene; import lcsb.mapviewer.model.map.species.Protein; import lcsb.mapviewer.model.map.species.Rna; -import org.springframework.stereotype.Service; /** * This is a class that implements a backend to CAZy. @@ -38,49 +38,49 @@ import org.springframework.stereotype.Service; @Service public class CazyAnnotator extends ElementAnnotator implements IExternalService { - /** - * Default class logger. - */ - private static Logger logger = Logger.getLogger(CazyAnnotator.class); - - /** - * Service used for annotation of entities using {@link MiriamType#TAIR_LOCUS - * tair}. - */ - private TairAnnotator tairAnnotator; - - /** - * Pattern used for finding UniProt symbol from TAIR info page . - */ - private Pattern cazyIdMatcher = Pattern.compile("\\/((GT|GH|PL|CE|CBM)\\d+(\\_\\d+)?)\\.html"); - - @Autowired - public CazyAnnotator(TairAnnotator tairAnnotator) { - super(CazyAnnotator.class, new Class[] { Protein.class, Gene.class, Rna.class }, false); - this.tairAnnotator = tairAnnotator; - } - - @Override - public ExternalServiceStatus getServiceStatus() { - ExternalServiceStatus status = new ExternalServiceStatus(getCommonName(), getUrl()); - - GeneralCacheInterface cacheCopy = getCache(); - this.setCache(null); - - try { - MiriamData md = uniprotToCazy(createMiriamData(MiriamType.UNIPROT, "Q9SG95")); - - status.setStatus(ExternalServiceStatusType.OK); - if (md == null || !md.getResource().equalsIgnoreCase("GH5_7")) { - status.setStatus(ExternalServiceStatusType.CHANGED); - } - } catch (Exception e) { - logger.error(status.getName() + " is down", e); - status.setStatus(ExternalServiceStatusType.DOWN); - } - this.setCache(cacheCopy); - return status; - } + /** + * Default class logger. + */ + private static Logger logger = Logger.getLogger(CazyAnnotator.class); + + /** + * Service used for annotation of entities using {@link MiriamType#TAIR_LOCUS + * tair}. + */ + private TairAnnotator tairAnnotator; + + /** + * Pattern used for finding UniProt symbol from TAIR info page . + */ + private Pattern cazyIdMatcher = Pattern.compile("\\/((GT|GH|PL|CE|CBM)\\d+(\\_\\d+)?)\\.html"); + + @Autowired + public CazyAnnotator(TairAnnotator tairAnnotator) { + super(CazyAnnotator.class, new Class[] { Protein.class, Gene.class, Rna.class }, false); + this.tairAnnotator = tairAnnotator; + } + + @Override + public ExternalServiceStatus getServiceStatus() { + ExternalServiceStatus status = new ExternalServiceStatus(getCommonName(), getUrl()); + + GeneralCacheInterface cacheCopy = getCache(); + this.setCache(null); + + try { + MiriamData md = uniprotToCazy(createMiriamData(MiriamType.UNIPROT, "Q9SG95")); + + status.setStatus(ExternalServiceStatusType.OK); + if (md == null || !md.getResource().equalsIgnoreCase("GH5_7")) { + status.setStatus(ExternalServiceStatusType.CHANGED); + } + } catch (Exception e) { + logger.error(status.getName() + " is down", e); + status.setStatus(ExternalServiceStatusType.DOWN); + } + this.setCache(cacheCopy); + return status; + } @Override public boolean annotateElement(BioEntity object, MiriamData identifier, AnnotationParameters parameters) @@ -109,115 +109,115 @@ public class CazyAnnotator extends ElementAnnotator implements IExternalService return cazyIds.size() > 0; } - /** - * Returns URL to TAIR page about TAIR entry. - * - * @param uniProtId - * UniProt identifier - * @return URL to CAZY UniProt accession search result page - */ - private String getCazyUrl(String uniProtId) { - return "http://www.cazy.org/search?page=recherche&recherche=" + uniProtId + "&tag=10"; - } - - /** - * Parse CAZy webpage to find information about - * {@link MiriamType#CAZY} and returns them. - * - * @param pageContent - * CAZy info page - * @return CAZy family identifier found on the page - */ - private Collection<MiriamData> parseCazy(String pageContent) { - Collection<MiriamData> result = new HashSet<MiriamData>(); - Matcher m = cazyIdMatcher.matcher(pageContent); - if (m.find()) { - result.add(createMiriamData(MiriamType.CAZY, m.group(1))); - } - return result; - } - - @Override - public Object refreshCacheQuery(Object query) throws SourceNotAvailable { - String name; - String result = null; - if (query instanceof String) { - name = (String) query; - if (name.startsWith("http")) { - try { - result = getWebPageContent(name); - } catch (IOException e) { - throw new SourceNotAvailable(e); - } - } else { - throw new InvalidArgumentException("Don't know what to do with query: " + query); - } - } else { - throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); - } - return result; - } - - /** - * Transform UniProt identifier to CAZy identifier. - * - * @param UniProt - * {@link MiriamData} with UniProt identifier - * @return {@link MiriamData} with CAZy identifier - * @throws AnnotatorException - * thrown when there is a problem with accessing external database - */ - public MiriamData uniprotToCazy(MiriamData uniprot) throws AnnotatorException { - if (uniprot == null) { - return null; - } - - if (!MiriamType.UNIPROT.equals(uniprot.getDataType())) { - throw new InvalidArgumentException(MiriamType.UNIPROT + " expected."); - } - - String accessUrl = getCazyUrl(uniprot.getResource()); - try { - String pageContent = getWebPageContent(accessUrl); - Collection<MiriamData> collection = parseCazy(pageContent); - if (collection.size() > 0) { - return collection.iterator().next(); - } else { - logger.warn("Cannot find CAZy data for UniProt id: " + uniprot.getResource()); - return null; - } - } catch (WrongResponseCodeIOException exception) { - logger.warn("Wrong response code when retrieving CAZy data for UniProt id: " + uniprot.getResource()); - return null; - } catch (IOException exception) { - throw new AnnotatorException(exception); - } - } - - @Override - public String getCommonName() { - return MiriamType.CAZY.getCommonName(); - } - - @Override - public String getUrl() { - return MiriamType.CAZY.getDbHomepage(); - } - - @Override - protected WebPageDownloader getWebPageDownloader() { - return super.getWebPageDownloader(); - } - - @Override - protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { - super.setWebPageDownloader(webPageDownloader); - } - - @Override - public List<AnnotatorInputParameter> getAvailableInputParameters() { - return Arrays.asList(new AnnotatorInputParameter(MiriamType.TAIR_LOCUS), - new AnnotatorInputParameter(MiriamType.UNIPROT)); - } + /** + * Returns URL to TAIR page about TAIR entry. + * + * @param uniProtId + * UniProt identifier + * @return URL to CAZY UniProt accession search result page + */ + private String getCazyUrl(String uniProtId) { + return "http://www.cazy.org/search?page=recherche&recherche=" + uniProtId + "&tag=10"; + } + + /** + * Parse CAZy webpage to find information about {@link MiriamType#CAZY} and + * returns them. + * + * @param pageContent + * CAZy info page + * @return CAZy family identifier found on the page + */ + private Collection<MiriamData> parseCazy(String pageContent) { + Collection<MiriamData> result = new HashSet<MiriamData>(); + Matcher m = cazyIdMatcher.matcher(pageContent); + if (m.find()) { + result.add(createMiriamData(MiriamType.CAZY, m.group(1))); + } + return result; + } + + @Override + public Object refreshCacheQuery(Object query) throws SourceNotAvailable { + String name; + String result = null; + if (query instanceof String) { + name = (String) query; + if (name.startsWith("http")) { + try { + result = getWebPageContent(name); + } catch (IOException e) { + throw new SourceNotAvailable(e); + } + } else { + throw new InvalidArgumentException("Don't know what to do with query: " + query); + } + } else { + throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); + } + return result; + } + + /** + * Transform UniProt identifier to CAZy identifier. + * + * @param UniProt + * {@link MiriamData} with UniProt identifier + * @return {@link MiriamData} with CAZy identifier + * @throws AnnotatorException + * thrown when there is a problem with accessing external database + */ + public MiriamData uniprotToCazy(MiriamData uniprot) throws AnnotatorException { + if (uniprot == null) { + return null; + } + + if (!MiriamType.UNIPROT.equals(uniprot.getDataType())) { + throw new InvalidArgumentException(MiriamType.UNIPROT + " expected."); + } + + String accessUrl = getCazyUrl(uniprot.getResource()); + try { + String pageContent = getWebPageContent(accessUrl); + Collection<MiriamData> collection = parseCazy(pageContent); + if (collection.size() > 0) { + return collection.iterator().next(); + } else { + logger.warn("Cannot find CAZy data for UniProt id: " + uniprot.getResource()); + return null; + } + } catch (WrongResponseCodeIOException exception) { + logger.warn("Wrong response code when retrieving CAZy data for UniProt id: " + uniprot.getResource()); + return null; + } catch (IOException exception) { + throw new AnnotatorException(exception); + } + } + + @Override + public String getCommonName() { + return MiriamType.CAZY.getCommonName(); + } + + @Override + public String getUrl() { + return MiriamType.CAZY.getDbHomepage(); + } + + @Override + protected WebPageDownloader getWebPageDownloader() { + return super.getWebPageDownloader(); + } + + @Override + protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { + super.setWebPageDownloader(webPageDownloader); + } + + @Override + public List<AnnotatorInputParameter> getAvailableInputParameters() { + return Arrays.asList(new AnnotatorInputParameter(MiriamType.TAIR_LOCUS), + new AnnotatorInputParameter(MiriamType.UNIPROT)); + } } diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ChebiSearchException.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ChebiSearchException.java index 1ca1eb2705dcb808e6553ce4d39244b4b79bb5d7..727f0fdf20691feb785b43486cd18dc72ca0f229 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ChebiSearchException.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ChebiSearchException.java @@ -11,22 +11,22 @@ import lcsb.mapviewer.annotation.data.Chebi; */ public class ChebiSearchException extends Exception { - /** - * - */ - private static final long serialVersionUID = 1L; + /** + * + */ + private static final long serialVersionUID = 1L; - /** - * Constructs a new exception with the specified detail message and cause. - * - * @param message - * the detail message - * @param cause - * the cause (A <tt>null</tt> value is permitted, and indicates that - * the cause is nonexistent or unknown.) - */ - public ChebiSearchException(String message, Throwable cause) { - super(message, cause); - } + /** + * Constructs a new exception with the specified detail message and cause. + * + * @param message + * the detail message + * @param cause + * the cause (A <tt>null</tt> value is permitted, and indicates that + * the cause is nonexistent or unknown.) + */ + public ChebiSearchException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotator.java index be106f4ed157c834c62b62f0381adbb077a0817b..38fea73fe98bc16af4fbe4b230b1e20b7fa52058 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotator.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotator.java @@ -89,7 +89,7 @@ public abstract class ElementAnnotator extends CachableInterface { */ public void annotateElement(BioEntity element) throws AnnotatorException { annotateElement(element, new AnnotationParameters()); - }; + } /** * Annotate element using parameters. diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/EnsemblAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/EnsemblAnnotator.java index 96437e769ccb8364bf6d8ea3b60eef726c4b65e2..4d4d007003ac6f26c05caf222037d9db8aca59c0 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/EnsemblAnnotator.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/EnsemblAnnotator.java @@ -43,257 +43,260 @@ import lcsb.mapviewer.modelutils.map.ElementUtils; @Service public class EnsemblAnnotator extends ElementAnnotator implements IExternalService { - /** - * Version of the rest API that is supported by this annotator. - */ - static final String SUPPORTED_VERSION = "9.0"; - - /** - * Url address of ensembl restful service. - */ - private static final String REST_SERVICE_URL = "https://rest.ensembl.org/xrefs/id/"; - - /** - * Suffix that is needed for getting proper result using - * {@link #REST_SERVICE_URL}. - */ - private static final String URL_SUFFIX = "?content-type=text/xml"; - - /** - * Url used for retrieving version of the restful API. - */ - private static final String REST_SERVICE_VERSION_URL = "https://rest.ensembl.org/info/rest?content-type=text/xml"; - - /** - * Default constructor. - */ - public EnsemblAnnotator() { - super(EnsemblAnnotator.class, new Class[] { Protein.class, Rna.class, Gene.class }, false); - } - - @Override - public String refreshCacheQuery(Object query) throws SourceNotAvailable { - if (query instanceof String) { - String name = (String) query; - if (name.startsWith("http")) { - try { - String result = getWebPageContent(name); - return result; - } catch (IOException e) { - throw new SourceNotAvailable(e); - } - } else { - throw new InvalidArgumentException("Don't know what to do with input string: " + name); - } - } else { - throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); - } - } - - /** - * Standard class logger. - */ - private final Logger logger = Logger.getLogger(EnsemblAnnotator.class); - - @Override - public String getCommonName() { - return MiriamType.ENSEMBL.getCommonName(); - } - - @Override - public String getUrl() { - return MiriamType.ENSEMBL.getDbHomepage(); - } - - @Override - public ExternalServiceStatus getServiceStatus() { - ExternalServiceStatus status = new ExternalServiceStatus(getCommonName(), getUrl()); - - GeneralCacheInterface cacheCopy = getCache(); - this.setCache(null); - - try { - - GenericProtein proteinAlias = new GenericProtein("mock_id"); - proteinAlias.addMiriamData(createMiriamData(MiriamType.ENSEMBL, "ENSG00000157764")); - annotateElement(proteinAlias); - - if (proteinAlias.getFullName() == null || proteinAlias.getFullName().isEmpty()) { - status.setStatus(ExternalServiceStatusType.CHANGED); - } else { - status.setStatus(ExternalServiceStatusType.OK); - } - - String version = getRestfulApiVersion(); - if (!SUPPORTED_VERSION.equals(version)) { - logger.debug("Version of Ensembl API changed... (new version: " + version + ")"); - status.setStatus(ExternalServiceStatusType.CHANGED); - } - - } catch (Exception e) { - logger.error(getCommonName() + " is down", e); - status.setStatus(ExternalServiceStatusType.DOWN); - } - this.setCache(cacheCopy); - return status; - } - - /** - * Returns current version of restful API. - * - * @return version of Ensembl restful API - * @throws IOException - * thrown when there is a problem with accessing API - * @throws InvalidXmlSchemaException - * thrown when the result returned by API is invalid - */ - private String getRestfulApiVersion() throws IOException, InvalidXmlSchemaException { - String content = getWebPageContent(REST_SERVICE_VERSION_URL); - Node xml = getXmlDocumentFromString(content); - Node response = getNode("opt", xml.getChildNodes()); - Node data = getNode("data", response.getChildNodes()); - - String version = super.getNodeAttr("release", data); - return version; - } - - @Override - public boolean annotateElement(BioEntity element, MiriamData identifier, AnnotationParameters parameters) - throws AnnotatorException { - if (identifier.getDataType().equals(MiriamType.ENSEMBL)) { - ElementUtils eu = new ElementUtils(); - String prefix = eu.getElementTag(element); - return annotateElement(element, identifier, prefix); - } else { - throw new NotImplementedException(); - } - } - - /** - * Annotates element using identifier given in the parameter. - * - * @param annotatedObject - * element that we want to annotate - * @param entrezMiriamData - * identifier that should be used for annotation - * @param prefix - * prefix used in warnings - * @throws AnnotatorException - * thrown when there is a problem with annotating element - */ - private boolean annotateElement(BioEntity annotatedObject, MiriamData entrezMiriamData, String prefix) throws AnnotatorException { - String query = REST_SERVICE_URL + entrezMiriamData.getResource() + URL_SUFFIX; - try { - String content = getWebPageContent(query); - Node xml = getXmlDocumentFromString(content); - Node response = getNode("opt", xml.getChildNodes()); - - NodeList list = response.getChildNodes(); - Set<String> synonyms = new HashSet<String>(); - for (int i = 0; i < list.getLength(); i++) { - Node node = list.item(i); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equals("data")) { - String error = super.getNodeAttr("error", node); - if (error != null && !error.isEmpty()) { - logger.warn(prefix + error); - } - String dbname = super.getNodeAttr("dbname", node); - - if ("EntrezGene".equals(dbname)) { - String entrezId = super.getNodeAttr("primary_id", node); - if (entrezId != null && !entrezId.isEmpty()) { - annotatedObject.addMiriamData(createMiriamData(MiriamType.ENTREZ, entrezId)); - } - String symbol = super.getNodeAttr("display_id", node); - if (symbol != null) { - setSymbol(annotatedObject, symbol, prefix); - } - String fullName = super.getNodeAttr("description", node); - if (symbol != null) { - setFullName((Element) annotatedObject, fullName, prefix); - } - NodeList synonymNodeList = node.getChildNodes(); - - for (int j = 0; j < synonymNodeList.getLength(); j++) { - Node synonymNode = synonymNodeList.item(j); - if (synonymNode.getNodeType() == Node.ELEMENT_NODE && "synonyms".equalsIgnoreCase(synonymNode.getNodeName())) { - synonyms.add(synonymNode.getTextContent()); - } - } - } else if ("HGNC".equals(dbname)) { - String hgncId = super.getNodeAttr("primary_id", node); - if (hgncId != null && !hgncId.isEmpty()) { - hgncId = hgncId.replaceAll("HGNC:", ""); - annotatedObject.addMiriamData(createMiriamData(MiriamType.HGNC, hgncId)); - } - String hgncSymbol = super.getNodeAttr("display_id", node); - if (hgncSymbol != null && !hgncSymbol.isEmpty()) { - annotatedObject.addMiriamData(createMiriamData(MiriamType.HGNC_SYMBOL, hgncSymbol)); - } - NodeList synonymNodeList = node.getChildNodes(); - - for (int j = 0; j < synonymNodeList.getLength(); j++) { - Node synonymNode = synonymNodeList.item(j); - if (synonymNode.getNodeType() == Node.ELEMENT_NODE && "synonyms".equalsIgnoreCase(synonymNode.getNodeName())) { - synonyms.add(synonymNode.getTextContent()); - } - } - } - } - } - } - if (synonyms.size() > 0) { - setSynonyms(annotatedObject, synonyms, prefix); - } - return true; - } catch (WrongResponseCodeIOException e) { - logger.warn(prefix + "Cannot find information for ensembl: " + entrezMiriamData.getResource()); - return false; - } catch (Exception e) { - throw new AnnotatorException(e); - } - } - - /** - * Converts {@link MiriamType#ENSEMBL} identifier into - * {@link MiriamType#ENTREZ} identifier. - * - * @param miriamData - * {@link MiriamData} with {@link MiriamType#ENSEMBL} identifier - * @return {@link MiriamData} with {@link MiriamType#ENTREZ} or null if such - * identifier doesn't exists - * @throws AnnotatorException - * thrown when there was problem with accessing external database or - * any other unknown problem - */ - public MiriamData ensemblIdToEntrezId(MiriamData miriamData) throws AnnotatorException { - if (!MiriamType.ENSEMBL.equals(miriamData.getDataType())) { - throw new InvalidArgumentException("Only " + MiriamType.ENSEMBL + " identifier is accepted as an input"); - } - GenericProtein proteinAlias = new GenericProtein("id"); - annotateElement(proteinAlias, miriamData, ""); - for (MiriamData md : proteinAlias.getMiriamData()) { - if (MiriamType.ENTREZ.equals(md.getDataType())) { - return md; - } - } - return null; - } - - @Override - protected WebPageDownloader getWebPageDownloader() { - return super.getWebPageDownloader(); - } - - @Override - protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { - super.setWebPageDownloader(webPageDownloader); - } - - @Override - public List<AnnotatorInputParameter> getAvailableInputParameters() { - return Arrays.asList(new AnnotatorInputParameter(MiriamType.ENSEMBL)); - } + /** + * Version of the rest API that is supported by this annotator. + */ + static final String SUPPORTED_VERSION = "9.0"; + + /** + * Url address of ensembl restful service. + */ + private static final String REST_SERVICE_URL = "https://rest.ensembl.org/xrefs/id/"; + + /** + * Suffix that is needed for getting proper result using + * {@link #REST_SERVICE_URL}. + */ + private static final String URL_SUFFIX = "?content-type=text/xml"; + + /** + * Url used for retrieving version of the restful API. + */ + private static final String REST_SERVICE_VERSION_URL = "https://rest.ensembl.org/info/rest?content-type=text/xml"; + + /** + * Default constructor. + */ + public EnsemblAnnotator() { + super(EnsemblAnnotator.class, new Class[] { Protein.class, Rna.class, Gene.class }, false); + } + + @Override + public String refreshCacheQuery(Object query) throws SourceNotAvailable { + if (query instanceof String) { + String name = (String) query; + if (name.startsWith("http")) { + try { + String result = getWebPageContent(name); + return result; + } catch (IOException e) { + throw new SourceNotAvailable(e); + } + } else { + throw new InvalidArgumentException("Don't know what to do with input string: " + name); + } + } else { + throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); + } + } + + /** + * Standard class logger. + */ + private final Logger logger = Logger.getLogger(EnsemblAnnotator.class); + + @Override + public String getCommonName() { + return MiriamType.ENSEMBL.getCommonName(); + } + + @Override + public String getUrl() { + return MiriamType.ENSEMBL.getDbHomepage(); + } + + @Override + public ExternalServiceStatus getServiceStatus() { + ExternalServiceStatus status = new ExternalServiceStatus(getCommonName(), getUrl()); + + GeneralCacheInterface cacheCopy = getCache(); + this.setCache(null); + + try { + + GenericProtein proteinAlias = new GenericProtein("mock_id"); + proteinAlias.addMiriamData(createMiriamData(MiriamType.ENSEMBL, "ENSG00000157764")); + annotateElement(proteinAlias); + + if (proteinAlias.getFullName() == null || proteinAlias.getFullName().isEmpty()) { + status.setStatus(ExternalServiceStatusType.CHANGED); + } else { + status.setStatus(ExternalServiceStatusType.OK); + } + + String version = getRestfulApiVersion(); + if (!SUPPORTED_VERSION.equals(version)) { + logger.debug("Version of Ensembl API changed... (new version: " + version + ")"); + status.setStatus(ExternalServiceStatusType.CHANGED); + } + + } catch (Exception e) { + logger.error(getCommonName() + " is down", e); + status.setStatus(ExternalServiceStatusType.DOWN); + } + this.setCache(cacheCopy); + return status; + } + + /** + * Returns current version of restful API. + * + * @return version of Ensembl restful API + * @throws IOException + * thrown when there is a problem with accessing API + * @throws InvalidXmlSchemaException + * thrown when the result returned by API is invalid + */ + private String getRestfulApiVersion() throws IOException, InvalidXmlSchemaException { + String content = getWebPageContent(REST_SERVICE_VERSION_URL); + Node xml = getXmlDocumentFromString(content); + Node response = getNode("opt", xml.getChildNodes()); + Node data = getNode("data", response.getChildNodes()); + + String version = super.getNodeAttr("release", data); + return version; + } + + @Override + public boolean annotateElement(BioEntity element, MiriamData identifier, AnnotationParameters parameters) + throws AnnotatorException { + if (identifier.getDataType().equals(MiriamType.ENSEMBL)) { + ElementUtils eu = new ElementUtils(); + String prefix = eu.getElementTag(element); + return annotateElement(element, identifier, prefix); + } else { + throw new NotImplementedException(); + } + } + + /** + * Annotates element using identifier given in the parameter. + * + * @param annotatedObject + * element that we want to annotate + * @param entrezMiriamData + * identifier that should be used for annotation + * @param prefix + * prefix used in warnings + * @throws AnnotatorException + * thrown when there is a problem with annotating element + */ + private boolean annotateElement(BioEntity annotatedObject, MiriamData entrezMiriamData, String prefix) + throws AnnotatorException { + String query = REST_SERVICE_URL + entrezMiriamData.getResource() + URL_SUFFIX; + try { + String content = getWebPageContent(query); + Node xml = getXmlDocumentFromString(content); + Node response = getNode("opt", xml.getChildNodes()); + + NodeList list = response.getChildNodes(); + Set<String> synonyms = new HashSet<String>(); + for (int i = 0; i < list.getLength(); i++) { + Node node = list.item(i); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equals("data")) { + String error = super.getNodeAttr("error", node); + if (error != null && !error.isEmpty()) { + logger.warn(prefix + error); + } + String dbname = super.getNodeAttr("dbname", node); + + if ("EntrezGene".equals(dbname)) { + String entrezId = super.getNodeAttr("primary_id", node); + if (entrezId != null && !entrezId.isEmpty()) { + annotatedObject.addMiriamData(createMiriamData(MiriamType.ENTREZ, entrezId)); + } + String symbol = super.getNodeAttr("display_id", node); + if (symbol != null) { + setSymbol(annotatedObject, symbol, prefix); + } + String fullName = super.getNodeAttr("description", node); + if (symbol != null) { + setFullName((Element) annotatedObject, fullName, prefix); + } + NodeList synonymNodeList = node.getChildNodes(); + + for (int j = 0; j < synonymNodeList.getLength(); j++) { + Node synonymNode = synonymNodeList.item(j); + if (synonymNode.getNodeType() == Node.ELEMENT_NODE + && "synonyms".equalsIgnoreCase(synonymNode.getNodeName())) { + synonyms.add(synonymNode.getTextContent()); + } + } + } else if ("HGNC".equals(dbname)) { + String hgncId = super.getNodeAttr("primary_id", node); + if (hgncId != null && !hgncId.isEmpty()) { + hgncId = hgncId.replaceAll("HGNC:", ""); + annotatedObject.addMiriamData(createMiriamData(MiriamType.HGNC, hgncId)); + } + String hgncSymbol = super.getNodeAttr("display_id", node); + if (hgncSymbol != null && !hgncSymbol.isEmpty()) { + annotatedObject.addMiriamData(createMiriamData(MiriamType.HGNC_SYMBOL, hgncSymbol)); + } + NodeList synonymNodeList = node.getChildNodes(); + + for (int j = 0; j < synonymNodeList.getLength(); j++) { + Node synonymNode = synonymNodeList.item(j); + if (synonymNode.getNodeType() == Node.ELEMENT_NODE + && "synonyms".equalsIgnoreCase(synonymNode.getNodeName())) { + synonyms.add(synonymNode.getTextContent()); + } + } + } + } + } + } + if (synonyms.size() > 0) { + setSynonyms(annotatedObject, synonyms, prefix); + } + return true; + } catch (WrongResponseCodeIOException e) { + logger.warn(prefix + "Cannot find information for ensembl: " + entrezMiriamData.getResource()); + return false; + } catch (Exception e) { + throw new AnnotatorException(e); + } + } + + /** + * Converts {@link MiriamType#ENSEMBL} identifier into {@link MiriamType#ENTREZ} + * identifier. + * + * @param miriamData + * {@link MiriamData} with {@link MiriamType#ENSEMBL} identifier + * @return {@link MiriamData} with {@link MiriamType#ENTREZ} or null if such + * identifier doesn't exists + * @throws AnnotatorException + * thrown when there was problem with accessing external database or + * any other unknown problem + */ + public MiriamData ensemblIdToEntrezId(MiriamData miriamData) throws AnnotatorException { + if (!MiriamType.ENSEMBL.equals(miriamData.getDataType())) { + throw new InvalidArgumentException("Only " + MiriamType.ENSEMBL + " identifier is accepted as an input"); + } + GenericProtein proteinAlias = new GenericProtein("id"); + annotateElement(proteinAlias, miriamData, ""); + for (MiriamData md : proteinAlias.getMiriamData()) { + if (MiriamType.ENTREZ.equals(md.getDataType())) { + return md; + } + } + return null; + } + + @Override + protected WebPageDownloader getWebPageDownloader() { + return super.getWebPageDownloader(); + } + + @Override + protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { + super.setWebPageDownloader(webPageDownloader); + } + + @Override + public List<AnnotatorInputParameter> getAvailableInputParameters() { + return Arrays.asList(new AnnotatorInputParameter(MiriamType.ENSEMBL)); + } } diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/EntrezAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/EntrezAnnotator.java index 1e3df5ecb85e290c169b373ebd859c81534584a6..6fc9a2a0fa0c64cc3c134b498afb1cc1701a53b9 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/EntrezAnnotator.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/EntrezAnnotator.java @@ -1,7 +1,6 @@ package lcsb.mapviewer.annotation.services.annotators; import java.io.IOException; -import java.io.StringReader; import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.Arrays; @@ -10,15 +9,10 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - import org.apache.log4j.Logger; import org.springframework.stereotype.Service; -import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; import lcsb.mapviewer.annotation.cache.GeneralCacheInterface; import lcsb.mapviewer.annotation.cache.SourceNotAvailable; @@ -55,345 +49,351 @@ import lcsb.mapviewer.modelutils.map.ElementUtils; @Service public class EntrezAnnotator extends ElementAnnotator implements IExternalService { - /** - * Prefix used in cache key to indicate that cached value contains information - * about {@link MiriamType#ENTREZ} to {@link MiriamType#HGNC} conviersion. - */ - public static final String CACHE_HGNC_ID_PREFIX = "HGNC_ID:"; - - /** - * Address of Entrez API that should be used for retrieving data. - */ - private static final String REST_API_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=gene&rettype=xml&id="; - - /** - * Prefix used in cache key to indicate that cached value contains - * {@link EntrezData} object for a given entrez id. - */ - static final String ENTREZ_DATA_PREFIX = "ENTREZ_DATA:"; - - /** - * Object that allows to serialize {@link EntrezData} elements into xml string - * and deserialize xml into {@link EntrezData} objects. - */ - private XmlSerializer<EntrezData> entrezSerializer; - - /** - * Default constructor. - */ - public EntrezAnnotator() { - super(EntrezAnnotator.class, new Class[] { Protein.class, Rna.class, Gene.class }, false); - entrezSerializer = new XmlSerializer<>(EntrezData.class); - } - - @Override - public String refreshCacheQuery(Object query) throws SourceNotAvailable { - String result = null; - try { - if (query instanceof String) { - String name = (String) query; - if (name.startsWith("http")) { - result = getWebPageContent(name); - } else if (name.startsWith(ENTREZ_DATA_PREFIX)) { - String id = name.replace(ENTREZ_DATA_PREFIX, ""); - result = entrezSerializer.objectToString(getEntrezForMiriamData(createMiriamData(MiriamType.ENTREZ, id), null)); - } else if (name.startsWith(CACHE_HGNC_ID_PREFIX)) { - String id = name.replace(CACHE_HGNC_ID_PREFIX, ""); - MiriamData md = getHgncIdFromEntrez(createMiriamData(MiriamType.ENTREZ, id)); - if (md != null) { - result = md.getResource(); - } - } else { - throw new InvalidArgumentException("Don't know what to do with input string: " + name); - } - } else { - throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); - } - } catch (AnnotatorException e) { - throw new SourceNotAvailable(e); - } catch (IOException e) { - throw new SourceNotAvailable(e); - } - return result; - } - - /** - * Standard class logger. - */ - private final Logger logger = Logger.getLogger(EntrezAnnotator.class); - - @Override - public String getCommonName() { - return MiriamType.ENTREZ.getCommonName(); - } - - @Override - public String getUrl() { - return MiriamType.ENTREZ.getDbHomepage(); - } - - @Override - public ExternalServiceStatus getServiceStatus() { - ExternalServiceStatus status = new ExternalServiceStatus(getCommonName(), getUrl()); - - GeneralCacheInterface cacheCopy = getCache(); - this.setCache(null); - - try { - GenericProtein proteinAlias = new GenericProtein("id"); - proteinAlias.addMiriamData(createMiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, MiriamType.ENTREZ, "9999")); - annotateElement(proteinAlias); - - if (proteinAlias.getFullName() == null || proteinAlias.getFullName().equals("")) { - status.setStatus(ExternalServiceStatusType.CHANGED); - } else { - status.setStatus(ExternalServiceStatusType.OK); - } - } catch (Exception e) { - logger.error(getCommonName() + " is down", e); - status.setStatus(ExternalServiceStatusType.DOWN); - } - this.setCache(cacheCopy); - return status; - } - - @Override - public boolean annotateElement(BioEntity element, MiriamData identifier, AnnotationParameters parameters) - throws AnnotatorException { - if (identifier.getDataType().equals(MiriamType.ENTREZ)) { - ElementUtils eu = new ElementUtils(); - String prefix = eu.getElementTag(element); - return annotateElement(element, identifier, prefix); + /** + * Prefix used in cache key to indicate that cached value contains information + * about {@link MiriamType#ENTREZ} to {@link MiriamType#HGNC} conviersion. + */ + public static final String CACHE_HGNC_ID_PREFIX = "HGNC_ID:"; + + /** + * Address of Entrez API that should be used for retrieving data. + */ + private static final String REST_API_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=gene&rettype=xml&id="; + + /** + * Prefix used in cache key to indicate that cached value contains + * {@link EntrezData} object for a given entrez id. + */ + static final String ENTREZ_DATA_PREFIX = "ENTREZ_DATA:"; + + /** + * Object that allows to serialize {@link EntrezData} elements into xml string + * and deserialize xml into {@link EntrezData} objects. + */ + private XmlSerializer<EntrezData> entrezSerializer; + + /** + * Default constructor. + */ + public EntrezAnnotator() { + super(EntrezAnnotator.class, new Class[] { Protein.class, Rna.class, Gene.class }, false); + entrezSerializer = new XmlSerializer<>(EntrezData.class); + } + + @Override + public String refreshCacheQuery(Object query) throws SourceNotAvailable { + String result = null; + try { + if (query instanceof String) { + String name = (String) query; + if (name.startsWith("http")) { + result = getWebPageContent(name); + } else if (name.startsWith(ENTREZ_DATA_PREFIX)) { + String id = name.replace(ENTREZ_DATA_PREFIX, ""); + result = entrezSerializer + .objectToString(getEntrezForMiriamData(createMiriamData(MiriamType.ENTREZ, id), null)); + } else if (name.startsWith(CACHE_HGNC_ID_PREFIX)) { + String id = name.replace(CACHE_HGNC_ID_PREFIX, ""); + MiriamData md = getHgncIdFromEntrez(createMiriamData(MiriamType.ENTREZ, id)); + if (md != null) { + result = md.getResource(); + } + } else { + throw new InvalidArgumentException("Don't know what to do with input string: " + name); + } + } else { + throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); + } + } catch (AnnotatorException e) { + throw new SourceNotAvailable(e); + } catch (IOException e) { + throw new SourceNotAvailable(e); + } + return result; + } + + /** + * Standard class logger. + */ + private final Logger logger = Logger.getLogger(EntrezAnnotator.class); + + @Override + public String getCommonName() { + return MiriamType.ENTREZ.getCommonName(); + } + + @Override + public String getUrl() { + return MiriamType.ENTREZ.getDbHomepage(); + } + + @Override + public ExternalServiceStatus getServiceStatus() { + ExternalServiceStatus status = new ExternalServiceStatus(getCommonName(), getUrl()); + + GeneralCacheInterface cacheCopy = getCache(); + this.setCache(null); + + try { + GenericProtein proteinAlias = new GenericProtein("id"); + proteinAlias + .addMiriamData(createMiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, MiriamType.ENTREZ, "9999")); + annotateElement(proteinAlias); + + if (proteinAlias.getFullName() == null || proteinAlias.getFullName().equals("")) { + status.setStatus(ExternalServiceStatusType.CHANGED); } else { - throw new NotImplementedException(); + status.setStatus(ExternalServiceStatusType.OK); + } + } catch (Exception e) { + logger.error(getCommonName() + " is down", e); + status.setStatus(ExternalServiceStatusType.DOWN); + } + this.setCache(cacheCopy); + return status; + } + + @Override + public boolean annotateElement(BioEntity element, MiriamData identifier, AnnotationParameters parameters) + throws AnnotatorException { + if (identifier.getDataType().equals(MiriamType.ENTREZ)) { + ElementUtils eu = new ElementUtils(); + String prefix = eu.getElementTag(element); + return annotateElement(element, identifier, prefix); + } else { + throw new NotImplementedException(); + } + } + + /** + * Annotates element using identifier given in the parameter. + * + * @param element + * element that we want to annotate + * @param entrezMiriamData + * identifier that should be used for annotation + * @param prefix + * prefix used in warnings + * @throws AnnotatorException + * thrown when there is a problem with annotating element + */ + private boolean annotateElement(BioEntity element, MiriamData entrezMiriamData, String prefix) + throws AnnotatorException { + EntrezData data = getEntrezForMiriamData(entrezMiriamData, prefix); + if (data != null) { + setSymbol(element, data.getSymbol(), prefix); + + setFullName((Element) element, data.getFullName(), prefix); + + element.addMiriamData(data.getMiriamData()); + setSynonyms(element, data.getSynonyms(), prefix); + setDescription(element, data.getDescription()); + return true; + } + return false; + + } + + /** + * Returns preprocessed data for entrez identifier. + * + * @param entrezMiriamData + * {@link MiriamData} with {@link MiriamType#ENTREZ} id + * @param prefix + * prefix used for logs + * @return {@link EntrezData preprocessed data} for entrez identifier + * @throws AnnotatorException + * thrown when there is a problem with obtaining data from entrez + * database + */ + EntrezData getEntrezForMiriamData(MiriamData entrezMiriamData, String prefix) throws AnnotatorException { + EntrezData result = entrezSerializer + .xmlToObject(super.getCacheNode(ENTREZ_DATA_PREFIX + entrezMiriamData.getResource())); + if (result == null) { + String query = REST_API_URL + entrezMiriamData.getResource(); + try { + String content = getWebPageContent(query); + result = new EntrezData(); + Node xml = getXmlDocumentFromString(content, false); + + Node response = getNode("Entrezgene-Set", xml.getChildNodes()); + if (response != null) { + Node errorNode = getNode("Error", response.getChildNodes()); + if (errorNode != null) { + logger.warn( + prefix + " Invalid " + MiriamType.ENTREZ + " identifier: \"" + entrezMiriamData.getResource() + "\""); + return null; + } + } else { + logger + .warn(prefix + "Problematic entrez response for identifier: \"" + entrezMiriamData.getResource() + "\""); + return null; + } + + Node rootNode = getNode("Entrezgene", response.getChildNodes()); + + Node annotationNode = getNode("Entrezgene_gene", rootNode.getChildNodes()); + Node annotationGenRefNode = getNode("Gene-ref", annotationNode.getChildNodes()); + + Node symbolNode = getNode("Gene-ref_locus", annotationGenRefNode.getChildNodes()); + if (symbolNode != null) { + String symbol = symbolNode.getTextContent(); + result.setSymbol(symbol); + } + + Node fullNameNode = getNode("Gene-ref_desc", annotationGenRefNode.getChildNodes()); + if (fullNameNode != null) { + String fullname = fullNameNode.getTextContent(); + result.setFullName(fullname); + } + + Node listNode = getNode("Gene-ref_db", annotationGenRefNode.getChildNodes()); + Set<String> synonyms = new HashSet<String>(); + if (listNode != null) { + NodeList list = getNode("Gene-ref_db", annotationGenRefNode.getChildNodes()).getChildNodes(); + + for (int i = 0; i < list.getLength(); i++) { + Node node = list.item(i); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equals("Dbtag")) { + Node dbNameNode = getNode("Dbtag_db", node.getChildNodes()); + String dbName = dbNameNode.getTextContent(); + + Node dbIdNode1 = getNode("Dbtag_tag", node.getChildNodes()); + Node dbIdNode2 = getNode("Object-id", dbIdNode1.getChildNodes()); + Node dbIdNode3 = getNode("Object-id_str", dbIdNode2.getChildNodes()); + if (dbIdNode3 == null) { + dbIdNode3 = getNode("Object-id_id", dbIdNode2.getChildNodes()); + } + String dbId = null; + if (dbIdNode3 != null) { + dbId = dbIdNode3.getTextContent(); + } + if ("HGNC".equals(dbName)) { + if (dbId != null && !dbId.isEmpty()) { + dbId = dbId.replaceAll("HGNC:", ""); + result.addMiriamData(createMiriamData(MiriamType.HGNC, dbId)); + } + } else if ("Ensembl".equals(dbName)) { + if (dbId != null && !dbId.isEmpty()) { + result.addMiriamData(createMiriamData(MiriamType.ENSEMBL, dbId)); + } + } + + } + } + } + + } + + Node synListNode = getNode("Gene-ref_syn", annotationGenRefNode.getChildNodes()); + if (synListNode != null) { + NodeList list = getNode("Gene-ref_syn", annotationGenRefNode.getChildNodes()).getChildNodes(); + + for (int i = 0; i < list.getLength(); i++) { + Node node = list.item(i); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equals("Gene-ref_syn_E")) { + synonyms.add(node.getTextContent()); + } + } + } + } + if (synonyms.size() > 0) { + List<String> sortedSynonyms = new ArrayList<String>(); + sortedSynonyms.addAll(synonyms); + Collections.sort(sortedSynonyms); + result.setSynonyms(sortedSynonyms); + } + Node notesNode = getNode("Entrezgene_summary", rootNode.getChildNodes()); + if (notesNode != null) { + String notes = notesNode.getTextContent(); + if (notes != null) { + result.setDescription(notes); + } + } + setCacheValue(ENTREZ_DATA_PREFIX + entrezMiriamData.getResource(), entrezSerializer.objectToString(result)); + } catch (WrongResponseCodeIOException e) { + if (e.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) { + logger.warn(prefix + " Invalid " + MiriamType.ENTREZ.getCommonName() + " identifier found"); + } else { + throw new AnnotatorException(e); + } + } catch (IOException e) { + throw new AnnotatorException(e); + } catch (InvalidXmlSchemaException e) { + throw new AnnotatorException(e); + } + } + return result; + } + + /** + * Converts {@link MiriamType#ENTREZ} identifier into {@link MiriamType#HGNC} + * identifier. + * + * @param miriamData + * {@link MiriamData} with {@link MiriamType#ENTREZ} identifier + * @return {@link MiriamData} with {@link MiriamType#HGNC} or null if such + * identifier doesn't exists + * @throws AnnotatorException + * thrown when there was problem with accessing external database or + * any other unknown problem + */ + public MiriamData getHgncIdFromEntrez(MiriamData miriamData) throws AnnotatorException { + if (!MiriamType.ENTREZ.equals(miriamData.getDataType())) { + throw new InvalidArgumentException("Only " + MiriamType.ENTREZ + " miriam type is accepted."); + } + String key = CACHE_HGNC_ID_PREFIX + miriamData.getResource(); + String value = getCacheValue(key); + if (value == null) { + GenericProtein proteinAlias = new GenericProtein("some_id"); + annotateElement(proteinAlias, miriamData, ""); + for (MiriamData md : proteinAlias.getMiriamData()) { + if (MiriamType.HGNC.equals(md.getDataType())) { + setCacheValue(key, md.getResource()); + return md; + } } - } - - /** - * Annotates element using identifier given in the parameter. - * - * @param element - * element that we want to annotate - * @param entrezMiriamData - * identifier that should be used for annotation - * @param prefix - * prefix used in warnings - * @throws AnnotatorException - * thrown when there is a problem with annotating element - */ - private boolean annotateElement(BioEntity element, MiriamData entrezMiriamData, String prefix) throws AnnotatorException { - EntrezData data = getEntrezForMiriamData(entrezMiriamData, prefix); - if (data != null) { - setSymbol(element, data.getSymbol(), prefix); - - setFullName((Element) element, data.getFullName(), prefix); - - element.addMiriamData(data.getMiriamData()); - setSynonyms(element, data.getSynonyms(), prefix); - setDescription(element, data.getDescription()); - return true; - } - return false; - - } - - /** - * Returns preprocessed data for entrez identifier. - * - * @param entrezMiriamData - * {@link MiriamData} with {@link MiriamType#ENTREZ} id - * @param prefix - * prefix used for logs - * @return {@link EntrezData preprocessed data} for entrez identifier - * @throws AnnotatorException - * thrown when there is a problem with obtaining data from entrez - * database - */ - EntrezData getEntrezForMiriamData(MiriamData entrezMiriamData, String prefix) throws AnnotatorException { - EntrezData result = entrezSerializer.xmlToObject(super.getCacheNode(ENTREZ_DATA_PREFIX + entrezMiriamData.getResource())); - if (result == null) { - String query = REST_API_URL + entrezMiriamData.getResource(); - try { - String content = getWebPageContent(query); - result = new EntrezData(); - Node xml = getXmlDocumentFromString(content, false); - - Node response = getNode("Entrezgene-Set", xml.getChildNodes()); - if (response != null) { - Node errorNode = getNode("Error", response.getChildNodes()); - if (errorNode != null) { - logger.warn(prefix + " Invalid " + MiriamType.ENTREZ + " identifier: \"" + entrezMiriamData.getResource() + "\""); - return null; - } - } else { - logger.warn(prefix + "Problematic entrez response for identifier: \"" + entrezMiriamData.getResource() + "\""); - return null; - } - - Node rootNode = getNode("Entrezgene", response.getChildNodes()); - - Node annotationNode = getNode("Entrezgene_gene", rootNode.getChildNodes()); - Node annotationGenRefNode = getNode("Gene-ref", annotationNode.getChildNodes()); - - Node symbolNode = getNode("Gene-ref_locus", annotationGenRefNode.getChildNodes()); - if (symbolNode != null) { - String symbol = symbolNode.getTextContent(); - result.setSymbol(symbol); - } - - Node fullNameNode = getNode("Gene-ref_desc", annotationGenRefNode.getChildNodes()); - if (fullNameNode != null) { - String fullname = fullNameNode.getTextContent(); - result.setFullName(fullname); - } - - Node listNode = getNode("Gene-ref_db", annotationGenRefNode.getChildNodes()); - Set<String> synonyms = new HashSet<String>(); - if (listNode != null) { - NodeList list = getNode("Gene-ref_db", annotationGenRefNode.getChildNodes()).getChildNodes(); - - for (int i = 0; i < list.getLength(); i++) { - Node node = list.item(i); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equals("Dbtag")) { - Node dbNameNode = getNode("Dbtag_db", node.getChildNodes()); - String dbName = dbNameNode.getTextContent(); - - Node dbIdNode1 = getNode("Dbtag_tag", node.getChildNodes()); - Node dbIdNode2 = getNode("Object-id", dbIdNode1.getChildNodes()); - Node dbIdNode3 = getNode("Object-id_str", dbIdNode2.getChildNodes()); - if (dbIdNode3 == null) { - dbIdNode3 = getNode("Object-id_id", dbIdNode2.getChildNodes()); - } - String dbId = null; - if (dbIdNode3 != null) { - dbId = dbIdNode3.getTextContent(); - } - if ("HGNC".equals(dbName)) { - if (dbId != null && !dbId.isEmpty()) { - dbId = dbId.replaceAll("HGNC:", ""); - result.addMiriamData(createMiriamData(MiriamType.HGNC, dbId)); - } - } else if ("Ensembl".equals(dbName)) { - if (dbId != null && !dbId.isEmpty()) { - result.addMiriamData(createMiriamData(MiriamType.ENSEMBL, dbId)); - } - } - - } - } - } - - } - - Node synListNode = getNode("Gene-ref_syn", annotationGenRefNode.getChildNodes()); - if (synListNode != null) { - NodeList list = getNode("Gene-ref_syn", annotationGenRefNode.getChildNodes()).getChildNodes(); - - for (int i = 0; i < list.getLength(); i++) { - Node node = list.item(i); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equals("Gene-ref_syn_E")) { - synonyms.add(node.getTextContent()); - } - } - } - } - if (synonyms.size() > 0) { - List<String> sortedSynonyms = new ArrayList<String>(); - sortedSynonyms.addAll(synonyms); - Collections.sort(sortedSynonyms); - result.setSynonyms(sortedSynonyms); - } - Node notesNode = getNode("Entrezgene_summary", rootNode.getChildNodes()); - if (notesNode != null) { - String notes = notesNode.getTextContent(); - if (notes != null) { - result.setDescription(notes); - } - } - setCacheValue(ENTREZ_DATA_PREFIX + entrezMiriamData.getResource(), entrezSerializer.objectToString(result)); - } catch (WrongResponseCodeIOException e) { - if (e.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) { - logger.warn(prefix + " Invalid " + MiriamType.ENTREZ.getCommonName() + " identifier found"); - } else { - throw new AnnotatorException(e); - } - } catch (IOException e) { - throw new AnnotatorException(e); - } catch (InvalidXmlSchemaException e) { - throw new AnnotatorException(e); - } - } - return result; - } - - /** - * Converts {@link MiriamType#ENTREZ} identifier into {@link MiriamType#HGNC} - * identifier. - * - * @param miriamData - * {@link MiriamData} with {@link MiriamType#ENTREZ} identifier - * @return {@link MiriamData} with {@link MiriamType#HGNC} or null if such - * identifier doesn't exists - * @throws AnnotatorException - * thrown when there was problem with accessing external database or - * any other unknown problem - */ - public MiriamData getHgncIdFromEntrez(MiriamData miriamData) throws AnnotatorException { - if (!MiriamType.ENTREZ.equals(miriamData.getDataType())) { - throw new InvalidArgumentException("Only " + MiriamType.ENTREZ + " miriam type is accepted."); - } - String key = CACHE_HGNC_ID_PREFIX + miriamData.getResource(); - String value = getCacheValue(key); - if (value == null) { - GenericProtein proteinAlias = new GenericProtein("some_id"); - annotateElement(proteinAlias, miriamData, ""); - for (MiriamData md : proteinAlias.getMiriamData()) { - if (MiriamType.HGNC.equals(md.getDataType())) { - setCacheValue(key, md.getResource()); - return md; - } - } - setCacheValue(key, "null"); - return null; - } else if (value.equals("null")) { - return null; - } else { - return createMiriamData(MiriamType.HGNC, value); - } - } - - @Override - protected WebPageDownloader getWebPageDownloader() { - return super.getWebPageDownloader(); - } - - @Override - protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { - super.setWebPageDownloader(webPageDownloader); - } - - /** - * @return the entrezSerializer - * @see #entrezSerializer - */ - protected XmlSerializer<EntrezData> getEntrezSerializer() { - return entrezSerializer; - } - - /** - * @param entrezSerializer - * the entrezSerializer to set - * @see #entrezSerializer - */ - protected void setEntrezSerializer(XmlSerializer<EntrezData> entrezSerializer) { - this.entrezSerializer = entrezSerializer; - } - - @Override - public List<AnnotatorInputParameter> getAvailableInputParameters() { - return Arrays.asList(new AnnotatorInputParameter(MiriamType.ENTREZ)); - } + setCacheValue(key, "null"); + return null; + } else if (value.equals("null")) { + return null; + } else { + return createMiriamData(MiriamType.HGNC, value); + } + } + + @Override + protected WebPageDownloader getWebPageDownloader() { + return super.getWebPageDownloader(); + } + + @Override + protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { + super.setWebPageDownloader(webPageDownloader); + } + + /** + * @return the entrezSerializer + * @see #entrezSerializer + */ + protected XmlSerializer<EntrezData> getEntrezSerializer() { + return entrezSerializer; + } + + /** + * @param entrezSerializer + * the entrezSerializer to set + * @see #entrezSerializer + */ + protected void setEntrezSerializer(XmlSerializer<EntrezData> entrezSerializer) { + this.entrezSerializer = entrezSerializer; + } + + @Override + public List<AnnotatorInputParameter> getAvailableInputParameters() { + return Arrays.asList(new AnnotatorInputParameter(MiriamType.ENTREZ)); + } } diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/GoSearchException.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/GoSearchException.java index be32c487daca186084e8bd073314feaf608dad62..47fcc8077857c93ed9441aaf2f0ef725fe272f2c 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/GoSearchException.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/GoSearchException.java @@ -9,22 +9,22 @@ package lcsb.mapviewer.annotation.services.annotators; */ public class GoSearchException extends Exception { - /** - * - */ - private static final long serialVersionUID = 1L; + /** + * + */ + private static final long serialVersionUID = 1L; - /** - * Constructs a new exception with the specified detail message and cause. - * - * @param message - * the detail message - * @param cause - * the cause (A <tt>null</tt> value is permitted, and indicates that - * the cause is nonexistent or unknown.) - */ - public GoSearchException(String message, Throwable cause) { - super(message, cause); - } + /** + * Constructs a new exception with the specified detail message and cause. + * + * @param message + * the detail message + * @param cause + * the cause (A <tt>null</tt> value is permitted, and indicates that + * the cause is nonexistent or unknown.) + */ + public GoSearchException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/HgncAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/HgncAnnotator.java index 19eaaf98cb0368382e70fa4fa83fac4275902f6e..c610abc4fe920ae1ee0d46cfffc1f5c2f0a2db75 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/HgncAnnotator.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/HgncAnnotator.java @@ -6,7 +6,6 @@ import java.util.Arrays; import java.util.List; import org.apache.log4j.Logger; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.w3c.dom.Node; import org.w3c.dom.NodeList; diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/PdbAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/PdbAnnotator.java index ac23a6631cd3a1458fed8ad58bd54907e107ca10..ec2609140b5b01ce6e05383fe4e57ac8943bbad1 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/PdbAnnotator.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/PdbAnnotator.java @@ -5,12 +5,16 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; -import java.util.Set; -import java.util.Map; 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.stereotype.Service; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import lcsb.mapviewer.annotation.cache.GeneralCacheInterface; import lcsb.mapviewer.annotation.cache.SourceNotAvailable; @@ -28,13 +32,9 @@ 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.modelutils.map.ElementUtils; import lcsb.mapviewer.model.map.species.field.Structure; import lcsb.mapviewer.model.map.species.field.UniprotRecord; - -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import org.springframework.stereotype.Service; +import lcsb.mapviewer.modelutils.map.ElementUtils; /** * This is a class that implements a backend to the EBI's PDB SIFTS REST API diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/PdbBestMappingEntry.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/PdbBestMappingEntry.java index c3cf1c9b2c2568050b2a702af2ed6cd3a2db7abe..9f3b2eccdfc3b389f874261f27e468581f3000f5 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/PdbBestMappingEntry.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/PdbBestMappingEntry.java @@ -8,33 +8,33 @@ import lcsb.mapviewer.model.map.species.field.Structure; * @author David Hoksza * */ -public class PdbBestMappingEntry { - public String chain_id; - public String experimental_method; - public String pdb_id; - public int start; - public int end; - public int unp_end; - public double coverage; - public int unp_start; - public double resolution; - public int tax_id; - - public Structure convertToStructure() { - - Structure s = new Structure(); - - s.setPdbId(this.pdb_id); - s.setChainId(this.chain_id); - s.setCoverage(this.coverage); - s.setResolution(this.resolution); - s.setStructStart(this.start); - s.setStructEnd(this.end); - s.setUnpStart(this.unp_start); - s.setUnpEnd(this.unp_end); - s.setExperimentalMethod(this.experimental_method); - s.setTaxId(this.tax_id); - - return s; - } +public class PdbBestMappingEntry { + public String chain_id; + public String experimental_method; + public String pdb_id; + public int start; + public int end; + public int unp_end; + public double coverage; + public int unp_start; + public double resolution; + public int tax_id; + + public Structure convertToStructure() { + + Structure s = new Structure(); + + s.setPdbId(this.pdb_id); + s.setChainId(this.chain_id); + s.setCoverage(this.coverage); + s.setResolution(this.resolution); + s.setStructStart(this.start); + s.setStructEnd(this.end); + s.setUnpStart(this.unp_start); + s.setUnpEnd(this.unp_end); + s.setExperimentalMethod(this.experimental_method); + s.setTaxId(this.tax_id); + + return s; + } } diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotator.java index 594d30f155aad77a5e63ced55225c3c87b90bd58..7b377b71ce5bf6b8e8541c4db92689be69933925 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotator.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotator.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map.Entry; import org.apache.log4j.Logger; +import org.springframework.stereotype.Service; import com.google.gson.JsonArray; import com.google.gson.JsonElement; @@ -31,7 +32,6 @@ 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; -import org.springframework.stereotype.Service; /** * This is a class that implements a backend to RECON database. @@ -356,7 +356,7 @@ public class ReconAnnotator extends ElementAnnotator implements IExternalService throw new AnnotatorException(e); } } else { - throw new NotImplementedException(); + throw new NotImplementedException(); } } diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/StitchAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/StitchAnnotator.java index 6b87438e2661d96156e04fa56f4c2652cd4bbd4f..ab6b1ced8c336200359c465c9eb56a7e4613cce6 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/StitchAnnotator.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/StitchAnnotator.java @@ -1,11 +1,10 @@ package lcsb.mapviewer.annotation.services.annotators; - import java.util.Arrays; import java.util.List; import org.apache.log4j.Logger; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; import lcsb.mapviewer.annotation.cache.SourceNotAvailable; import lcsb.mapviewer.annotation.cache.WebPageDownloader; @@ -17,10 +16,10 @@ import lcsb.mapviewer.model.map.BioEntity; import lcsb.mapviewer.model.map.MiriamData; import lcsb.mapviewer.model.map.MiriamType; import lcsb.mapviewer.model.map.species.SimpleMolecule; -import org.springframework.stereotype.Service; /** - * This is a class that implements STITCH annotation which is derived from existing ChEBI annotation. + * This is a class that implements STITCH annotation which is derived from + * existing ChEBI annotation. * * @author David Hoksza * @@ -28,38 +27,41 @@ import org.springframework.stereotype.Service; @Service public class StitchAnnotator extends ElementAnnotator implements IExternalService { - /** - * Default class logger. - */ - private static Logger logger = Logger.getLogger(StitchAnnotator.class); - - /** - * Service used for annotation of entities using {@link MiriamType#STITCH STITCH}. - */ - private ChebiAnnotator chebiAnnotator; - - /** - * Default constructor. - */ - public StitchAnnotator(ChebiAnnotator chebiAnnotator) { - super(StitchAnnotator.class, new Class[] { SimpleMolecule.class }, false); - this.chebiAnnotator = chebiAnnotator; - } - - @Override - public ExternalServiceStatus getServiceStatus() { - return chebiAnnotator.getServiceStatus(); - } - - /** - * Returns main layer of the InchKey, * i.e. everything - * before first hyphen: WBYWAXJHAXSJNI-VOTSOKGWSA-N -> WBYWAXJHAXSJNI - * @param inchiKey Full inchiKey - * @return Main layer of the InchiKey - */ - private String inchiKeyExtractMainLayer(String inchiKey){ - return inchiKey.replaceFirst("-.*", ""); - } + /** + * Default class logger. + */ + private static Logger logger = Logger.getLogger(StitchAnnotator.class); + + /** + * Service used for annotation of entities using {@link MiriamType#STITCH + * STITCH}. + */ + private ChebiAnnotator chebiAnnotator; + + /** + * Default constructor. + */ + public StitchAnnotator(ChebiAnnotator chebiAnnotator) { + super(StitchAnnotator.class, new Class[] { SimpleMolecule.class }, false); + this.chebiAnnotator = chebiAnnotator; + } + + @Override + public ExternalServiceStatus getServiceStatus() { + return chebiAnnotator.getServiceStatus(); + } + + /** + * Returns main layer of the InchKey, * i.e. everything before first hyphen: + * WBYWAXJHAXSJNI-VOTSOKGWSA-N -> WBYWAXJHAXSJNI + * + * @param inchiKey + * Full inchiKey + * @return Main layer of the InchiKey + */ + private String inchiKeyExtractMainLayer(String inchiKey) { + return inchiKey.replaceFirst("-.*", ""); + } @Override public boolean annotateElement(BioEntity object, MiriamData identifier, AnnotationParameters parameters) @@ -87,35 +89,34 @@ public class StitchAnnotator extends ElementAnnotator implements IExternalServic } } - @Override - public Object refreshCacheQuery(Object query) throws SourceNotAvailable { - return chebiAnnotator.refreshCacheQuery(query); - } - - - @Override - public String getCommonName() { - return MiriamType.STITCH.getCommonName(); - } - - @Override - public String getUrl() { - return MiriamType.STITCH.getDbHomepage(); - } - - @Override - protected WebPageDownloader getWebPageDownloader() { - return super.getWebPageDownloader(); - } - - @Override - protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { - super.setWebPageDownloader(webPageDownloader); - } - - @Override - public List<AnnotatorInputParameter> getAvailableInputParameters() { - return Arrays.asList(new AnnotatorInputParameter(MiriamType.CHEBI)); - } + @Override + public Object refreshCacheQuery(Object query) throws SourceNotAvailable { + return chebiAnnotator.refreshCacheQuery(query); + } + + @Override + public String getCommonName() { + return MiriamType.STITCH.getCommonName(); + } + + @Override + public String getUrl() { + return MiriamType.STITCH.getDbHomepage(); + } + + @Override + protected WebPageDownloader getWebPageDownloader() { + return super.getWebPageDownloader(); + } + + @Override + protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { + super.setWebPageDownloader(webPageDownloader); + } + + @Override + public List<AnnotatorInputParameter> getAvailableInputParameters() { + return Arrays.asList(new AnnotatorInputParameter(MiriamType.CHEBI)); + } } diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/StringAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/StringAnnotator.java index 7d97a67f213974f0bdd82ff15d5509ae14d3d35e..becf2954bc10b14f49e15edf47bb779ed63c6974 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/StringAnnotator.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/StringAnnotator.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; import lcsb.mapviewer.annotation.services.ExternalServiceStatus; import lcsb.mapviewer.annotation.services.IExternalService; @@ -16,7 +17,6 @@ import lcsb.mapviewer.model.map.MiriamType; import lcsb.mapviewer.model.map.species.Gene; import lcsb.mapviewer.model.map.species.Protein; import lcsb.mapviewer.model.map.species.Rna; -import org.springframework.stereotype.Service; /** * This is a class that implements a mapping to STRING database. @@ -26,26 +26,26 @@ import org.springframework.stereotype.Service; */ @Service public class StringAnnotator extends ElementAnnotator implements IExternalService { - - /** - * Service used for annotation of entities using {@link MiriamType#TAIR_LOCUS - * TAIR}. - */ - private TairAnnotator tairAnnotator; - - /** - * Default constructor. - */ - @Autowired - public StringAnnotator(TairAnnotator tairAnnotator) { - super(StringAnnotator.class, new Class[] { Protein.class, Gene.class, Rna.class }, false); - this.tairAnnotator = tairAnnotator; - } - @Override - public ExternalServiceStatus getServiceStatus() { - return tairAnnotator.getServiceStatus(); - } + /** + * Service used for annotation of entities using {@link MiriamType#TAIR_LOCUS + * TAIR}. + */ + private TairAnnotator tairAnnotator; + + /** + * Default constructor. + */ + @Autowired + public StringAnnotator(TairAnnotator tairAnnotator) { + super(StringAnnotator.class, new Class[] { Protein.class, Gene.class, Rna.class }, false); + this.tairAnnotator = tairAnnotator; + } + + @Override + public ExternalServiceStatus getServiceStatus() { + return tairAnnotator.getServiceStatus(); + } @Override public boolean annotateElement(BioEntity object, MiriamData identifier, AnnotationParameters parameters) @@ -72,45 +72,43 @@ public class StringAnnotator extends ElementAnnotator implements IExternalServic object.addMiriamData(mdString); } } - return stringIds.size()>0; + return stringIds.size() > 0; } - /** - * Transform UniProt {@link MiriamData} data to STRING {@link MiriamData}. - * - * @param UniProt - * {@link MiriamData} with UniProt identifier - * @return {@link MiriamData} with STRING identifier - * @throws AnnotatorException - * thrown when there is a problem with accessing external database - */ - public MiriamData uniprotToString(MiriamData uniprot) throws AnnotatorException { - if (uniprot == null) { - return null; - } + /** + * Transform UniProt {@link MiriamData} data to STRING {@link MiriamData}. + * + * @param UniProt + * {@link MiriamData} with UniProt identifier + * @return {@link MiriamData} with STRING identifier + * @throws AnnotatorException + * thrown when there is a problem with accessing external database + */ + public MiriamData uniprotToString(MiriamData uniprot) throws AnnotatorException { + if (uniprot == null) { + return null; + } - if (!MiriamType.UNIPROT.equals(uniprot.getDataType())) { - throw new InvalidArgumentException(MiriamType.UNIPROT + " expected."); - } + if (!MiriamType.UNIPROT.equals(uniprot.getDataType())) { + throw new InvalidArgumentException(MiriamType.UNIPROT + " expected."); + } - return createMiriamData(MiriamType.STRING, uniprot.getResource()); - } + return createMiriamData(MiriamType.STRING, uniprot.getResource()); + } - @Override - public String getCommonName() { - return MiriamType.STRING.getCommonName(); - } + @Override + public String getCommonName() { + return MiriamType.STRING.getCommonName(); + } - @Override - public String getUrl() { - return MiriamType.STRING.getDbHomepage(); - } + @Override + public String getUrl() { + return MiriamType.STRING.getDbHomepage(); + } - @Override - public List<AnnotatorInputParameter> getAvailableInputParameters() { - return Arrays.asList(new AnnotatorInputParameter(MiriamType.TAIR_LOCUS), - new AnnotatorInputParameter(MiriamType.UNIPROT)); - } + @Override + public List<AnnotatorInputParameter> getAvailableInputParameters() { + return Arrays.asList(new AnnotatorInputParameter(MiriamType.TAIR_LOCUS), + new AnnotatorInputParameter(MiriamType.UNIPROT)); + } } - - diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/TairAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/TairAnnotator.java index 14198eae55db6c2515147b6e0ff295a60d99584c..eacf74d2892b8a79f1fe2a262ff56e55e6a0890f 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/TairAnnotator.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/TairAnnotator.java @@ -1,7 +1,6 @@ package lcsb.mapviewer.annotation.services.annotators; import java.io.IOException; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; @@ -10,6 +9,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; +import org.springframework.stereotype.Service; import lcsb.mapviewer.annotation.cache.GeneralCacheInterface; import lcsb.mapviewer.annotation.cache.SourceNotAvailable; @@ -26,7 +26,6 @@ import lcsb.mapviewer.model.map.MiriamType; import lcsb.mapviewer.model.map.species.Gene; import lcsb.mapviewer.model.map.species.Protein; import lcsb.mapviewer.model.map.species.Rna; -import org.springframework.stereotype.Service; /** * This is a class that implements a backend to TAIR. @@ -37,200 +36,201 @@ import org.springframework.stereotype.Service; @Service public class TairAnnotator extends ElementAnnotator implements IExternalService { - /** - * Default class logger. - */ - private static Logger logger = Logger.getLogger(TairAnnotator.class); - - /** - * Pattern used for finding UniProt symbol from TAIR info page . - */ - private Pattern tairToUniprot = Pattern.compile("UniProtKB=([^-\"]*)"); - - /** - * Default constructor. - */ - public TairAnnotator() { - super(TairAnnotator.class, new Class[] { Protein.class, Gene.class, Rna.class }, false); - } - - @Override - public ExternalServiceStatus getServiceStatus() { - ExternalServiceStatus status = new ExternalServiceStatus(getCommonName(), getUrl()); - - GeneralCacheInterface cacheCopy = getCache(); - this.setCache(null); - - try { - MiriamData md = tairToUniprot(createMiriamData(MiriamType.TAIR_LOCUS, "AT1G01030")); - - status.setStatus(ExternalServiceStatusType.OK); - if (md == null || !md.getResource().equalsIgnoreCase("Q9MAN1")) { - status.setStatus(ExternalServiceStatusType.CHANGED); - } - } catch (Exception e) { - logger.error(status.getName() + " is down", e); - status.setStatus(ExternalServiceStatusType.DOWN); - } - this.setCache(cacheCopy); - return status; - } - - @Override - public boolean annotateElement(BioEntity object, MiriamData identifier, AnnotationParameters parameters) - throws AnnotatorException { - if (identifier.getDataType().equals(MiriamType.TAIR_LOCUS)) { - MiriamData mdUniprot = tairToUniprot(identifier); - if (mdUniprot != null ) { - object.addMiriamData(mdUniprot); - return true; - } - return false; - } else { - throw new NotImplementedException(); - } - } - - /** - * Returns URL to TAIR page about TAIR entry. - * - * @param tairId - * TAIR identifier - * @return URL to TAIR page about the TAIR entry - */ - private String getTairUrl(String tairId) { - return "https://arabidopsis.org/servlets/TairObject?type=locus&name=" + tairId; - } - - /** - * Returns URL to UniProt result containing mapped UniProtIds for submitted TAIR entry. - * - * @param tairId - * TAIR identifier - * @return URL to UniProt result page with the TAIR mapping - */ - private String getUniProtUrl(String tairId) { - return "https://www.uniprot.org/uniprot/?query=database%3A%28type%3Atair%29+database%3A%28type%3Atair+"+tairId+"%29&format=list&columns=id"; - } - - /** - * Parse TAIR webpage to find information about - * {@link MiriamType#UNIPROT} and returns them. - * - * @param pageContent - * tair info page - * @return uniprot identifier found on the page - */ - private Collection<MiriamData> parseUniprot(String pageContent) { - Collection<MiriamData> result = new HashSet<MiriamData>(); - Matcher m = tairToUniprot.matcher(pageContent); - if (m.find()) { - result.add(createMiriamData(MiriamType.UNIPROT, m.group(1))); - } - return result; - } - - /** - * Parse UniProt result page which contains list of Uniprot ids - * mapped to submitted TAIR id. The procedure obtains first - * {@link MiriamType#UNIPROT} and returns it. - * - * @param pageContent - * uniprot REST API result page - * @return uniprot identifier found on the page - */ - private Collection<MiriamData> parseUniprotUniprot(String pageContent) { - Collection<MiriamData> result = new HashSet<MiriamData>(); - if (!pageContent.isEmpty()) { - String[] sPageContent= pageContent.split("\\R"); - result.add(createMiriamData(MiriamType.UNIPROT, sPageContent[0])); - } - return result; - } - - @Override - public Object refreshCacheQuery(Object query) throws SourceNotAvailable { - String name; - String result = null; - if (query instanceof String) { - name = (String) query; - if (name.startsWith("http")) { - try { - result = getWebPageContent(name); - } catch (IOException e) { - throw new SourceNotAvailable(e); - } - } else { - throw new InvalidArgumentException("Don't know what to do with query: " + query); - } - } else { - throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); - } - return result; - } - - /** - * Transform TAIR identifier into uniprot identifier. - * - * Used to use the TAIR record page, but that tends to change and - * moreover TAIR limits number of accesses from an address. So now - * the transformation queries directly UniProt from which the - * mapping can be obtained as well. - * - * @param tair - * {@link MiriamData} with TAIR identifier - * @return {@link MiriamData} with UniProt identifier - * @throws AnnotatorException - * thrown when there is a problem with accessing external database - */ - public MiriamData tairToUniprot(MiriamData tair) throws AnnotatorException { - if (tair == null) { - return null; - } - - if (!MiriamType.TAIR_LOCUS.equals(tair.getDataType())) { - throw new InvalidArgumentException(MiriamType.TAIR_LOCUS + " expected."); - } - - //String accessUrl = getTairUrl(tair.getResource()); - String accessUrl = getUniProtUrl(tair.getResource()); - try { - String pageContent = getWebPageContent(accessUrl); - //Collection<MiriamData> collection = parseUniprot(pageContent); - Collection<MiriamData> collection = parseUniprotUniprot(pageContent); - if (collection.size() > 0) { - return collection.iterator().next(); - } else { - logger.warn("Cannot find uniprot data for id: " + tair.getResource() + " in the tair page"); - return null; - } - } catch (WrongResponseCodeIOException exception) { - logger.warn("Wrong reponse code when accessing tair data with id: " + tair.getResource()); - return null; - } catch (IOException exception) { - throw new AnnotatorException(exception); - } - } - - @Override - public String getCommonName() { - return "TAIR"; - } - - @Override - public String getUrl() { - return MiriamType.TAIR_LOCUS.getDbHomepage(); - } - - @Override - protected WebPageDownloader getWebPageDownloader() { - return super.getWebPageDownloader(); - } - - @Override - protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { - super.setWebPageDownloader(webPageDownloader); - } + /** + * Default class logger. + */ + private static Logger logger = Logger.getLogger(TairAnnotator.class); + + /** + * Pattern used for finding UniProt symbol from TAIR info page . + */ + private Pattern tairToUniprot = Pattern.compile("UniProtKB=([^-\"]*)"); + + /** + * Default constructor. + */ + public TairAnnotator() { + super(TairAnnotator.class, new Class[] { Protein.class, Gene.class, Rna.class }, false); + } + + @Override + public ExternalServiceStatus getServiceStatus() { + ExternalServiceStatus status = new ExternalServiceStatus(getCommonName(), getUrl()); + + GeneralCacheInterface cacheCopy = getCache(); + this.setCache(null); + + try { + MiriamData md = tairToUniprot(createMiriamData(MiriamType.TAIR_LOCUS, "AT1G01030")); + + status.setStatus(ExternalServiceStatusType.OK); + if (md == null || !md.getResource().equalsIgnoreCase("Q9MAN1")) { + status.setStatus(ExternalServiceStatusType.CHANGED); + } + } catch (Exception e) { + logger.error(status.getName() + " is down", e); + status.setStatus(ExternalServiceStatusType.DOWN); + } + this.setCache(cacheCopy); + return status; + } + + @Override + public boolean annotateElement(BioEntity object, MiriamData identifier, AnnotationParameters parameters) + throws AnnotatorException { + if (identifier.getDataType().equals(MiriamType.TAIR_LOCUS)) { + MiriamData mdUniprot = tairToUniprot(identifier); + if (mdUniprot != null) { + object.addMiriamData(mdUniprot); + return true; + } + return false; + } else { + throw new NotImplementedException(); + } + } + + /** + * Returns URL to TAIR page about TAIR entry. + * + * @param tairId + * TAIR identifier + * @return URL to TAIR page about the TAIR entry + */ + private String getTairUrl(String tairId) { + return "https://arabidopsis.org/servlets/TairObject?type=locus&name=" + tairId; + } + + /** + * Returns URL to UniProt result containing mapped UniProtIds for submitted TAIR + * entry. + * + * @param tairId + * TAIR identifier + * @return URL to UniProt result page with the TAIR mapping + */ + private String getUniProtUrl(String tairId) { + return "https://www.uniprot.org/uniprot/?query=database%3A%28type%3Atair%29+database%3A%28type%3Atair+" + tairId + + "%29&format=list&columns=id"; + } + + /** + * Parse TAIR webpage to find information about {@link MiriamType#UNIPROT} and + * returns them. + * + * @param pageContent + * tair info page + * @return uniprot identifier found on the page + */ + private Collection<MiriamData> parseUniprot(String pageContent) { + Collection<MiriamData> result = new HashSet<MiriamData>(); + Matcher m = tairToUniprot.matcher(pageContent); + if (m.find()) { + result.add(createMiriamData(MiriamType.UNIPROT, m.group(1))); + } + return result; + } + + /** + * Parse UniProt result page which contains list of Uniprot ids mapped to + * submitted TAIR id. The procedure obtains first {@link MiriamType#UNIPROT} and + * returns it. + * + * @param pageContent + * uniprot REST API result page + * @return uniprot identifier found on the page + */ + private Collection<MiriamData> parseUniprotUniprot(String pageContent) { + Collection<MiriamData> result = new HashSet<MiriamData>(); + if (!pageContent.isEmpty()) { + String[] sPageContent = pageContent.split("\\R"); + result.add(createMiriamData(MiriamType.UNIPROT, sPageContent[0])); + } + return result; + } + + @Override + public Object refreshCacheQuery(Object query) throws SourceNotAvailable { + String name; + String result = null; + if (query instanceof String) { + name = (String) query; + if (name.startsWith("http")) { + try { + result = getWebPageContent(name); + } catch (IOException e) { + throw new SourceNotAvailable(e); + } + } else { + throw new InvalidArgumentException("Don't know what to do with query: " + query); + } + } else { + throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); + } + return result; + } + + /** + * Transform TAIR identifier into uniprot identifier. + * + * Used to use the TAIR record page, but that tends to change and moreover TAIR + * limits number of accesses from an address. So now the transformation queries + * directly UniProt from which the mapping can be obtained as well. + * + * @param tair + * {@link MiriamData} with TAIR identifier + * @return {@link MiriamData} with UniProt identifier + * @throws AnnotatorException + * thrown when there is a problem with accessing external database + */ + public MiriamData tairToUniprot(MiriamData tair) throws AnnotatorException { + if (tair == null) { + return null; + } + + if (!MiriamType.TAIR_LOCUS.equals(tair.getDataType())) { + throw new InvalidArgumentException(MiriamType.TAIR_LOCUS + " expected."); + } + + // String accessUrl = getTairUrl(tair.getResource()); + String accessUrl = getUniProtUrl(tair.getResource()); + try { + String pageContent = getWebPageContent(accessUrl); + // Collection<MiriamData> collection = parseUniprot(pageContent); + Collection<MiriamData> collection = parseUniprotUniprot(pageContent); + if (collection.size() > 0) { + return collection.iterator().next(); + } else { + logger.warn("Cannot find uniprot data for id: " + tair.getResource() + " in the tair page"); + return null; + } + } catch (WrongResponseCodeIOException exception) { + logger.warn("Wrong reponse code when accessing tair data with id: " + tair.getResource()); + return null; + } catch (IOException exception) { + throw new AnnotatorException(exception); + } + } + + @Override + public String getCommonName() { + return "TAIR"; + } + + @Override + public String getUrl() { + return MiriamType.TAIR_LOCUS.getDbHomepage(); + } + + @Override + protected WebPageDownloader getWebPageDownloader() { + return super.getWebPageDownloader(); + } + + @Override + protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { + super.setWebPageDownloader(webPageDownloader); + } @Override public List<AnnotatorInputParameter> getAvailableInputParameters() { diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/UniprotAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/UniprotAnnotator.java index 10bb726fad6d63f8ece1b622f28327ff349d9222..4c7a45896ef05d66e0eeec817c075d015320e8fd 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/UniprotAnnotator.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/UniprotAnnotator.java @@ -10,6 +10,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; +import org.springframework.stereotype.Service; import lcsb.mapviewer.annotation.cache.GeneralCacheInterface; import lcsb.mapviewer.annotation.cache.SourceNotAvailable; @@ -27,7 +28,6 @@ import lcsb.mapviewer.model.map.species.Gene; import lcsb.mapviewer.model.map.species.Protein; import lcsb.mapviewer.model.map.species.Rna; import lcsb.mapviewer.modelutils.map.ElementUtils; -import org.springframework.stereotype.Service; /** * This is a class that implements a backend to uniprot restfull API. @@ -38,273 +38,273 @@ import org.springframework.stereotype.Service; @Service public class UniprotAnnotator extends ElementAnnotator implements IExternalService { - /** - * Default class logger. - */ - private static Logger logger = Logger.getLogger(UniprotAnnotator.class); - - /** - * Pattern used for finding hgnc symbol from uniprot info page . - */ - private Pattern uniprotToHgnc = Pattern.compile("GN[\\ ]+Name=([^;\\ ]+)"); - - /** - * Pattern used for finding entrez identifier from uniprot info page . - */ - private Pattern uniprotToEntrez = Pattern.compile("DR[\\ ]+GeneID;\\ ([^;\\ ]+)"); - - /** - * Pattern used for finding EC symbol from UniProt info page . - */ - private Pattern uniprotToEC = Pattern.compile("EC=((\\d+\\.-\\.-\\.-)|(\\d+\\.\\d+\\.-\\.-)|(\\d+\\.\\d+\\.\\d+\\.-)|(\\d+\\.\\d+\\.\\d+\\.\\d+))"); - - - /** - * Class used for some simple operations on {@link BioEntity} elements. - */ - private ElementUtils elementUtils = new ElementUtils(); - - /** - * Default constructor. - */ - public UniprotAnnotator() { - super(UniprotAnnotator.class, new Class[] { Protein.class, Gene.class, Rna.class }, false); - } - - @Override - public ExternalServiceStatus getServiceStatus() { - ExternalServiceStatus status = new ExternalServiceStatus(getCommonName(), getUrl()); - - GeneralCacheInterface cacheCopy = getCache(); - this.setCache(null); - - try { - MiriamData md = uniProtToHgnc(createMiriamData(MiriamType.UNIPROT, "P37840")); - - status.setStatus(ExternalServiceStatusType.OK); - if (md == null) { - status.setStatus(ExternalServiceStatusType.DOWN); - } else if (!md.getResource().equalsIgnoreCase("SNCA")) { - status.setStatus(ExternalServiceStatusType.CHANGED); - } - } catch (Exception e) { - logger.error(status.getName() + " is down", e); - status.setStatus(ExternalServiceStatusType.DOWN); - } - this.setCache(cacheCopy); - return status; - } - - @Override - public boolean annotateElement(BioEntity object, MiriamData identifier, AnnotationParameters parameters) - throws AnnotatorException { - if (identifier.getDataType().equals(MiriamType.UNIPROT)) { - - String accessUrl = getUniprotUrl(identifier.getResource()); - try { - String pageContent = getWebPageContent(accessUrl); - // check if we have data over there - if (!pageContent.contains("<title>Error</title>")) { - Set<MiriamData> annotations = new HashSet<MiriamData>(); - annotations.addAll(parseHgnc(pageContent)); - annotations.addAll(parseEntrez(pageContent)); - annotations.addAll(parseEC(pageContent)); - if (!object.getMiriamData().contains(identifier)) { - annotations.add(createMiriamData(MiriamType.UNIPROT, identifier.getResource())); - } - object.addMiriamData(annotations); - return true; - } else { - // this means that entry with a given uniprot id doesn't exist - if (object.getMiriamData().contains(identifier)) { - logger.warn(elementUtils.getElementTag(object) + " Invalid uniprot id: " + identifier); - } - return false; - } - } catch (WrongResponseCodeIOException exception) { - logger.warn("Cannot find uniprot data for id: " + identifier); - return false; - } catch (IOException exception) { - throw new AnnotatorException(exception); - } + /** + * Default class logger. + */ + private static Logger logger = Logger.getLogger(UniprotAnnotator.class); + + /** + * Pattern used for finding hgnc symbol from uniprot info page . + */ + private Pattern uniprotToHgnc = Pattern.compile("GN[\\ ]+Name=([^;\\ ]+)"); + + /** + * Pattern used for finding entrez identifier from uniprot info page . + */ + private Pattern uniprotToEntrez = Pattern.compile("DR[\\ ]+GeneID;\\ ([^;\\ ]+)"); + + /** + * Pattern used for finding EC symbol from UniProt info page . + */ + private Pattern uniprotToEC = Pattern + .compile("EC=((\\d+\\.-\\.-\\.-)|(\\d+\\.\\d+\\.-\\.-)|(\\d+\\.\\d+\\.\\d+\\.-)|(\\d+\\.\\d+\\.\\d+\\.\\d+))"); + + /** + * Class used for some simple operations on {@link BioEntity} elements. + */ + private ElementUtils elementUtils = new ElementUtils(); + + /** + * Default constructor. + */ + public UniprotAnnotator() { + super(UniprotAnnotator.class, new Class[] { Protein.class, Gene.class, Rna.class }, false); + } + + @Override + public ExternalServiceStatus getServiceStatus() { + ExternalServiceStatus status = new ExternalServiceStatus(getCommonName(), getUrl()); + + GeneralCacheInterface cacheCopy = getCache(); + this.setCache(null); + + try { + MiriamData md = uniProtToHgnc(createMiriamData(MiriamType.UNIPROT, "P37840")); + + status.setStatus(ExternalServiceStatusType.OK); + if (md == null) { + status.setStatus(ExternalServiceStatusType.DOWN); + } else if (!md.getResource().equalsIgnoreCase("SNCA")) { + status.setStatus(ExternalServiceStatusType.CHANGED); + } + } catch (Exception e) { + logger.error(status.getName() + " is down", e); + status.setStatus(ExternalServiceStatusType.DOWN); + } + this.setCache(cacheCopy); + return status; + } + + @Override + public boolean annotateElement(BioEntity object, MiriamData identifier, AnnotationParameters parameters) + throws AnnotatorException { + if (identifier.getDataType().equals(MiriamType.UNIPROT)) { + + String accessUrl = getUniprotUrl(identifier.getResource()); + try { + String pageContent = getWebPageContent(accessUrl); + // check if we have data over there + if (!pageContent.contains("<title>Error</title>")) { + Set<MiriamData> annotations = new HashSet<MiriamData>(); + annotations.addAll(parseHgnc(pageContent)); + annotations.addAll(parseEntrez(pageContent)); + annotations.addAll(parseEC(pageContent)); + if (!object.getMiriamData().contains(identifier)) { + annotations.add(createMiriamData(MiriamType.UNIPROT, identifier.getResource())); + } + object.addMiriamData(annotations); + return true; + } else { + // this means that entry with a given uniprot id doesn't exist + if (object.getMiriamData().contains(identifier)) { + logger.warn(elementUtils.getElementTag(object) + " Invalid uniprot id: " + identifier); + } + return false; + } + } catch (WrongResponseCodeIOException exception) { + logger.warn("Cannot find uniprot data for id: " + identifier); + return false; + } catch (IOException exception) { + throw new AnnotatorException(exception); + } + } else { + throw new NotImplementedException(); + } + } + + /** + * Returns url to uniprot restfull API about uniprot entry. + * + * @param uniprotId + * uniprot identifier + * @return url to uniprot restfull API about uniprot entry + */ + private String getUniprotUrl(String uniprotId) { + return "https://www.uniprot.org/uniprot/" + uniprotId + ".txt"; + } + + /** + * Parse uniprot webpage to find information about {@link MiriamType#ENTREZ} and + * returns them. + * + * @param pageContent + * uniprot info page + * @return set of entrez identifiers found on the webpage + */ + private Collection<MiriamData> parseEntrez(String pageContent) { + Collection<MiriamData> result = new HashSet<MiriamData>(); + Matcher m = uniprotToEntrez.matcher(pageContent); + if (m.find()) { + result.add(createMiriamData(MiriamType.ENTREZ, m.group(1))); + } + return result; + } + + /** + * Parse uniprot webpage to find information about + * {@link MiriamType#HGNC_SYMBOL} and returns them. + * + * @param pageContent + * uniprot info page + * @return set of entrez identifiers found on the webpage + */ + private Collection<MiriamData> parseHgnc(String pageContent) { + Collection<MiriamData> result = new HashSet<MiriamData>(); + Matcher m = uniprotToHgnc.matcher(pageContent); + if (m.find()) { + result.add(createMiriamData(MiriamType.HGNC_SYMBOL, m.group(1))); + } + return result; + } + + /** + * Parse UniProt webpage to find information about {@link MiriamType#EC}s and + * returns them. + * + * @param pageContent + * UniProt info page + * @return EC found on the page + */ + private Collection<MiriamData> parseEC(String pageContent) { + Collection<MiriamData> result = new HashSet<MiriamData>(); + Matcher m = uniprotToEC.matcher(pageContent); + while (m.find()) { + result.add(createMiriamData(MiriamType.EC, m.group(1))); + } + return result; + } + + @Override + public Object refreshCacheQuery(Object query) throws SourceNotAvailable { + String name; + String result = null; + if (query instanceof String) { + name = (String) query; + if (name.startsWith("http")) { + try { + result = getWebPageContent(name); + } catch (IOException e) { + throw new SourceNotAvailable(e); + } + } else { + throw new InvalidArgumentException("Don't know what to do with query: " + query); + } + } else { + throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); + } + return result; + } + + /** + * Transform uniprot identifier into hgnc name. + * + * @param uniprot + * {@link MiriamData} with uniprot identifier + * @return {@link MiriamData} with hgnc name + * @throws UniprotSearchException + * thrown when there is aproblem with accessing external database + */ + public MiriamData uniProtToHgnc(MiriamData uniprot) throws UniprotSearchException { + if (uniprot == null) { + return null; + } + + if (!MiriamType.UNIPROT.equals(uniprot.getDataType())) { + throw new InvalidArgumentException(MiriamType.UNIPROT + " expected."); + } + + String accessUrl = getUniprotUrl(uniprot.getResource()); + try { + String pageContent = getWebPageContent(accessUrl); + Collection<MiriamData> collection = parseHgnc(pageContent); + if (collection.size() > 0) { + return collection.iterator().next(); + } else { + return null; + } + } catch (IOException e) { + throw new UniprotSearchException("Problem with accessing uniprot webpage", e); + } + + } + + /** + * Transform uniprot identifier into EC identifiers. + * + * @param uniprot + * {@link MiriamData} with uniprot identifier + * @return ArrayList of {@link MiriamData} with EC codes + * @throws UniprotSearchException + * thrown when there is a problem with accessing external database + */ + public Collection<MiriamData> uniProtToEC(MiriamData uniprot) throws UniprotSearchException { + if (uniprot == null) { + return null; + } + + if (!MiriamType.UNIPROT.equals(uniprot.getDataType())) { + throw new InvalidArgumentException(MiriamType.UNIPROT + " expected."); + } + + String accessUrl = getUniprotUrl(uniprot.getResource()); + try { + String pageContent = getWebPageContent(accessUrl); + Collection<MiriamData> collection = parseEC(pageContent); + if (collection.size() > 0) { + return collection; } else { - throw new NotImplementedException(); + return null; } - } - - /** - * Returns url to uniprot restfull API about uniprot entry. - * - * @param uniprotId - * uniprot identifier - * @return url to uniprot restfull API about uniprot entry - */ - private String getUniprotUrl(String uniprotId) { - return "https://www.uniprot.org/uniprot/" + uniprotId + ".txt"; - } - - /** - * Parse uniprot webpage to find information about {@link MiriamType#ENTREZ} - * and returns them. - * - * @param pageContent - * uniprot info page - * @return set of entrez identifiers found on the webpage - */ - private Collection<MiriamData> parseEntrez(String pageContent) { - Collection<MiriamData> result = new HashSet<MiriamData>(); - Matcher m = uniprotToEntrez.matcher(pageContent); - if (m.find()) { - result.add(createMiriamData(MiriamType.ENTREZ, m.group(1))); - } - return result; - } - - /** - * Parse uniprot webpage to find information about - * {@link MiriamType#HGNC_SYMBOL} and returns them. - * - * @param pageContent - * uniprot info page - * @return set of entrez identifiers found on the webpage - */ - private Collection<MiriamData> parseHgnc(String pageContent) { - Collection<MiriamData> result = new HashSet<MiriamData>(); - Matcher m = uniprotToHgnc.matcher(pageContent); - if (m.find()) { - result.add(createMiriamData(MiriamType.HGNC_SYMBOL, m.group(1))); - } - return result; - } - - /** - * Parse UniProt webpage to find information about - * {@link MiriamType#EC}s and returns them. - * - * @param pageContent - * UniProt info page - * @return EC found on the page - */ - private Collection<MiriamData> parseEC(String pageContent) { - Collection<MiriamData> result = new HashSet<MiriamData>(); - Matcher m = uniprotToEC.matcher(pageContent); - while (m.find()) { - result.add(createMiriamData(MiriamType.EC, m.group(1))); - } - return result; - } - - @Override - public Object refreshCacheQuery(Object query) throws SourceNotAvailable { - String name; - String result = null; - if (query instanceof String) { - name = (String) query; - if (name.startsWith("http")) { - try { - result = getWebPageContent(name); - } catch (IOException e) { - throw new SourceNotAvailable(e); - } - } else { - throw new InvalidArgumentException("Don't know what to do with query: " + query); - } - } else { - throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); - } - return result; - } - - /** - * Transform uniprot identifier into hgnc name. - * - * @param uniprot - * {@link MiriamData} with uniprot identifier - * @return {@link MiriamData} with hgnc name - * @throws UniprotSearchException - * thrown when there is aproblem with accessing external database - */ - public MiriamData uniProtToHgnc(MiriamData uniprot) throws UniprotSearchException { - if (uniprot == null) { - return null; - } - - if (!MiriamType.UNIPROT.equals(uniprot.getDataType())) { - throw new InvalidArgumentException(MiriamType.UNIPROT + " expected."); - } - - String accessUrl = getUniprotUrl(uniprot.getResource()); - try { - String pageContent = getWebPageContent(accessUrl); - Collection<MiriamData> collection = parseHgnc(pageContent); - if (collection.size() > 0) { - return collection.iterator().next(); - } else { - return null; - } - } catch (IOException e) { - throw new UniprotSearchException("Problem with accessing uniprot webpage", e); - } - - } - - /** - * Transform uniprot identifier into EC identifiers. - * - * @param uniprot - * {@link MiriamData} with uniprot identifier - * @return ArrayList of {@link MiriamData} with EC codes - * @throws UniprotSearchException - * thrown when there is a problem with accessing external database - */ - public Collection<MiriamData> uniProtToEC(MiriamData uniprot) throws UniprotSearchException { - if (uniprot == null) { - return null; - } - - if (!MiriamType.UNIPROT.equals(uniprot.getDataType())) { - throw new InvalidArgumentException(MiriamType.UNIPROT + " expected."); - } - - String accessUrl = getUniprotUrl(uniprot.getResource()); - try { - String pageContent = getWebPageContent(accessUrl); - Collection<MiriamData> collection = parseEC(pageContent); - if (collection.size() > 0) { - return collection; - } else { - return null; - } - } catch (IOException e) { - throw new UniprotSearchException("Problem with accessing uniprot webpage", e); - } - - } - - @Override - public String getCommonName() { - return MiriamType.UNIPROT.getCommonName(); - } - - @Override - public String getUrl() { - return MiriamType.UNIPROT.getDbHomepage(); - } - - @Override - protected WebPageDownloader getWebPageDownloader() { - return super.getWebPageDownloader(); - } - - @Override - protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { - super.setWebPageDownloader(webPageDownloader); - } - - @Override - public List<AnnotatorInputParameter> getAvailableInputParameters() { - return Arrays.asList(new AnnotatorInputParameter(MiriamType.UNIPROT), - new AnnotatorInputParameter(BioEntityField.NAME, MiriamType.UNIPROT)); - } + } catch (IOException e) { + throw new UniprotSearchException("Problem with accessing uniprot webpage", e); + } + + } + + @Override + public String getCommonName() { + return MiriamType.UNIPROT.getCommonName(); + } + + @Override + public String getUrl() { + return MiriamType.UNIPROT.getDbHomepage(); + } + + @Override + protected WebPageDownloader getWebPageDownloader() { + return super.getWebPageDownloader(); + } + + @Override + protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { + super.setWebPageDownloader(webPageDownloader); + } + + @Override + public List<AnnotatorInputParameter> getAvailableInputParameters() { + return Arrays.asList(new AnnotatorInputParameter(MiriamType.UNIPROT), + new AnnotatorInputParameter(BioEntityField.NAME, MiriamType.UNIPROT)); + } } diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/UniprotSearchException.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/UniprotSearchException.java index 6b12f60e3c74aa5d75f6acf86bbf6912a318f7de..5167e746694df311606aa30e3fcc1a0992cafbac 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/UniprotSearchException.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/UniprotSearchException.java @@ -9,22 +9,22 @@ package lcsb.mapviewer.annotation.services.annotators; */ public class UniprotSearchException extends Exception { - /** - * - */ - private static final long serialVersionUID = 1L; + /** + * + */ + private static final long serialVersionUID = 1L; - /** - * Constructs a new exception with the specified detail message and cause. - * - * @param message - * the detail message - * @param cause - * the cause (A <tt>null</tt> value is permitted, and indicates that - * the cause is nonexistent or unknown.) - */ - public UniprotSearchException(String message, Throwable cause) { - super(message, cause); - } + /** + * Constructs a new exception with the specified detail message and cause. + * + * @param message + * the detail message + * @param cause + * the cause (A <tt>null</tt> value is permitted, and indicates that + * the cause is nonexistent or unknown.) + */ + public UniprotSearchException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/AllAnnotatorTests.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/AllAnnotatorTests.java index d30e43ae11d147a1020494813b00cdf72a0a520b..c9b84d136fddf571e55df1e05233e297465cab91 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/AllAnnotatorTests.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/AllAnnotatorTests.java @@ -6,24 +6,24 @@ import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({ AnnotatorExceptionTest.class, - AnnotatorExceptionTest.class, - BrendaAnnotatorTest.class, - BiocompendiumAnnotatorTest.class, - CazyAnnotatorTest.class, - ChebiAnnotatorTest.class, - ElementAnnotatorTest.class, - EnsemblAnnotatorTest.class, - EntrezAnnotatorTest.class, - GoAnnotatorTest.class, - HgncAnnotatorTest.class, - KeggAnnotatorTest.class, - MultipleAnnotatorsTest.class, - PdbAnnotatorTest.class, - ReconAnnotatorTest.class, - StitchAnnotatorTest.class, - StringAnnotatorTest.class, - TairAnnotatorTest.class, - UniprotAnnotatorTest.class, + AnnotatorExceptionTest.class, + BrendaAnnotatorTest.class, + BiocompendiumAnnotatorTest.class, + CazyAnnotatorTest.class, + ChebiAnnotatorTest.class, + ElementAnnotatorTest.class, + EnsemblAnnotatorTest.class, + EntrezAnnotatorTest.class, + GoAnnotatorTest.class, + HgncAnnotatorTest.class, + KeggAnnotatorTest.class, + MultipleAnnotatorsTest.class, + PdbAnnotatorTest.class, + ReconAnnotatorTest.class, + StitchAnnotatorTest.class, + StringAnnotatorTest.class, + TairAnnotatorTest.class, + UniprotAnnotatorTest.class, }) public class AllAnnotatorTests { diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/AnnotatorExceptionTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/AnnotatorExceptionTest.java index 68f6a0176587ab7f5e5bdd4b25ba536c77c9b7ad..29befabb845bbfd0e8ae7c3b3c196f354caa3a26 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/AnnotatorExceptionTest.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/AnnotatorExceptionTest.java @@ -1,6 +1,6 @@ package lcsb.mapviewer.annotation.services.annotators; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import org.junit.After; import org.junit.AfterClass; @@ -9,22 +9,22 @@ import org.junit.Test; public class AnnotatorExceptionTest { - @AfterClass - public static void tearDownAfterClass() throws Exception { - } + @AfterClass + public static void tearDownAfterClass() throws Exception { + } - @Before - public void setUp() throws Exception { - } + @Before + public void setUp() throws Exception { + } - @After - public void tearDown() throws Exception { - } + @After + public void tearDown() throws Exception { + } - @Test - public void testConstructor() { - AnnotatorException e = new AnnotatorException("messss"); - assertEquals("messss",e.getMessage()); - } + @Test + public void testConstructor() { + AnnotatorException e = new AnnotatorException("messss"); + assertEquals("messss", e.getMessage()); + } } diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/BiocompendiumAnnotatorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/BiocompendiumAnnotatorTest.java index 5747fea0540177894351be9e9f92484c897f9a20..f4c2219a59ad132fc6afdf0c0cb34887531c6079 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/BiocompendiumAnnotatorTest.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/BiocompendiumAnnotatorTest.java @@ -301,7 +301,8 @@ public class BiocompendiumAnnotatorTest extends AnnotationTestFunctions { @Test public void testRefreshValueWhenProblemWithSource() throws Exception { try { - BiocompendiumAnnotator annotatorUnderTest = Mockito.mock(BiocompendiumAnnotator.class, Mockito.CALLS_REAL_METHODS); + BiocompendiumAnnotator annotatorUnderTest = Mockito.mock(BiocompendiumAnnotator.class, + Mockito.CALLS_REAL_METHODS); when(annotatorUnderTest.getAnnotationsFromXml(anyString())).thenThrow(InvalidXmlSchemaException.class); annotatorUnderTest.refreshCacheQuery(camk4.getResource()); fail("Exception expected"); diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/BrendaAnnotatorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/BrendaAnnotatorTest.java index d0b6b11890d7264ed9de8b08b88962fac2981584..8e0ab1c8a1a46b4294f3a2d0de5f2941bf01e2df 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/BrendaAnnotatorTest.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/BrendaAnnotatorTest.java @@ -4,8 +4,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.mockito.Matchers.nullable; -import static org.mockito.Matchers.anyString; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.when; import java.io.IOException; @@ -164,7 +164,8 @@ public class BrendaAnnotatorTest extends AnnotationTestFunctions { protein.addMiriamData(new MiriamData(MiriamType.UNIPROT, "bla")); WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); - when(mockDownloader.getFromNetwork(anyString(), anyString(), nullable(String.class))).thenThrow(new IOException()); + when(mockDownloader.getFromNetwork(anyString(), anyString(), nullable(String.class))) + .thenThrow(new IOException()); uniprotAnnotator.setWebPageDownloader(mockDownloader); brendaAnnotator.annotateElement(protein); diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/CazyAnnotatorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/CazyAnnotatorTest.java index 44ec236b94c4c21908a2365b5ca443e3fc1d8cc0..c7b943da75e2d4244579da82605667a7d914cd8c 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/CazyAnnotatorTest.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/CazyAnnotatorTest.java @@ -6,8 +6,8 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.when; import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.Mockito.when; import java.io.IOException; @@ -257,7 +257,8 @@ public class CazyAnnotatorTest extends AnnotationTestFunctions { WebPageDownloader downloader = cazyAnnotator.getWebPageDownloader(); try { WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); - when(mockDownloader.getFromNetwork(anyString(), anyString(), nullable(String.class))).thenThrow(new IOException()); + when(mockDownloader.getFromNetwork(anyString(), anyString(), nullable(String.class))) + .thenThrow(new IOException()); cazyAnnotator.setWebPageDownloader(mockDownloader); assertEquals(ExternalServiceStatusType.DOWN, cazyAnnotator.getServiceStatus().getStatus()); } catch (Exception e) { diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/EnsemblAnnotatorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/EnsemblAnnotatorTest.java index 166a23b64ceb772043d55a1c30c0c1d148349b82..e79f08c58eb2b46f0240391cfc171141d0a93985 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/EnsemblAnnotatorTest.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/EnsemblAnnotatorTest.java @@ -20,7 +20,6 @@ import org.springframework.beans.factory.annotation.Autowired; import lcsb.mapviewer.annotation.AnnotationTestFunctions; import lcsb.mapviewer.annotation.cache.GeneralCacheInterface; -import lcsb.mapviewer.annotation.cache.PermanentDatabaseLevelCache; import lcsb.mapviewer.annotation.cache.PermanentDatabaseLevelCacheInterface; import lcsb.mapviewer.annotation.cache.SourceNotAvailable; import lcsb.mapviewer.annotation.cache.WebPageDownloader; diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/HgncAnnotatorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/HgncAnnotatorTest.java index 3fa39070775be126efaa7736b7f3ade839345ea8..3f14d80fd8a0eec6e9968fe23e18e82777f9ceac 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/HgncAnnotatorTest.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/HgncAnnotatorTest.java @@ -22,7 +22,6 @@ import org.springframework.beans.factory.annotation.Autowired; import lcsb.mapviewer.annotation.AnnotationTestFunctions; import lcsb.mapviewer.annotation.cache.GeneralCacheInterface; -import lcsb.mapviewer.annotation.cache.PermanentDatabaseLevelCache; import lcsb.mapviewer.annotation.cache.PermanentDatabaseLevelCacheInterface; import lcsb.mapviewer.annotation.cache.SourceNotAvailable; import lcsb.mapviewer.annotation.cache.WebPageDownloader; diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/KeggAnnotatorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/KeggAnnotatorTest.java index 947b3cb56e406f617e1397d82708243808eb6146..f5a5bb90750a09b99bee0b1d559a877fdf94f6ac 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/KeggAnnotatorTest.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/KeggAnnotatorTest.java @@ -4,15 +4,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.nullable; -import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.when; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; -import java.util.List; import java.util.Set; import org.apache.http.HttpStatus; @@ -33,7 +32,6 @@ import lcsb.mapviewer.model.map.MiriamType; import lcsb.mapviewer.model.map.species.GenericProtein; import lcsb.mapviewer.model.map.species.Species; import lcsb.mapviewer.model.user.AnnotatorParamDefinition; -import lcsb.mapviewer.model.user.UserAnnotatorsParam; public class KeggAnnotatorTest extends AnnotationTestFunctions { diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/PdbAnnotatorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/PdbAnnotatorTest.java index b3d6d580eccc91c5c6b42a5764006e6db9d8d696..2eac724fa920edee5164cc112ee98bd0a0e85b59 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/PdbAnnotatorTest.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/PdbAnnotatorTest.java @@ -3,14 +3,15 @@ package lcsb.mapviewer.annotation.services.annotators; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.nullable; -import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.when; import java.util.Set; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; @@ -27,216 +28,221 @@ import lcsb.mapviewer.model.map.species.field.UniprotRecord; public class PdbAnnotatorTest extends AnnotationTestFunctions { - @Autowired - PdbAnnotator pdbAnnotator; - - @Autowired - UniprotAnnotator uniprotAnnotator; - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testAnnotate1() throws Exception { - try { - String uniprotId = "P29373"; - Species protein = new GenericProtein("id"); - protein.setName(uniprotId); - /* One needs to have the UniProt ID first. - * This tests simulates situation when the Uniprot annotator - * is called first. - */ - uniprotAnnotator.annotateElement(protein); - int cntAnnotations1 = protein.getMiriamData().size(); - pdbAnnotator.annotateElement(protein); - int cntAnnotations2 = protein.getMiriamData().size(); - - assertTrue(cntAnnotations2 > cntAnnotations1); - - boolean pdb = false; - - for (MiriamData md : protein.getMiriamData()) { - if (md.getDataType().equals(MiriamType.PDB)) { - pdb = true; - break; - } - } - assertTrue("No PDB annotation extracted from pdb annotator", pdb); - - Set<UniprotRecord> urs = protein.getUniprots(); - //Test whether there is a uniprot record - assertTrue(urs.size() > 0); - UniprotRecord ur = null; - for (UniprotRecord ur1 : urs) { - if (ur1.getUniprotId() == uniprotId) { - ur = ur1; - } - } - //Test whether there is a uniprot record with the uniprot ID which was stored - assertNotNull(ur); - //Test whether structures are there - Set<Structure> ss = ur.getStructures(); - assertNotNull(ss); - assertTrue(ss.size() > 0); - //Test whether uniprot is accessible from structure - assertTrue(ss.iterator().next().getUniprot() == ur); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - -// @Test -// public void testAnnotate2() throws Exception { -// try { -// Species protein = new GenericProtein("id"); -// protein.setName("P29373"); -// /* One needs to have the UniProt ID first, and Uniprot lookup -// * through HGNC is included in PDB annotator. -// * This tests simulates situation when the PDB annotator is called first. -// */ -// pdbAnnotator.annotateElement(protein); -// int cntAnnotations = protein.getMiriamData().size(); - -// assertTrue(cntAnnotations > 0); - -// boolean pdb = false; - -// for (MiriamData md : protein.getMiriamData()) { -// if (md.getDataType().equals(MiriamType.PDB)) { -// pdb = true; -// break; -// } -// } -// assertTrue("No PDB annotation extracted from pdb annotator", pdb); - -// } catch (Exception e) { -// e.printStackTrace(); -// throw e; -// } -// } - -// @Test -// public void testAnnotateNotUniprotAnnodated() throws Exception { -// try { -// Species protein = new GenericProtein("id"); -// protein.setName("P29373"); -// pdbAnnotator.annotateElement(protein); -// -// assertTrue("UniProt annotation in PDB annotator failed", protein.getMiriamData().size() > 0); - -// boolean pdb = false; - -// for (MiriamData md : protein.getMiriamData()) { -// if (md.getDataType().equals(MiriamType.PDB)) { -// pdb = true; -// break; -// } -// } -// assertTrue("No PDB annotation extracted from PDB annotator", pdb); - -// } catch (Exception e) { -// e.printStackTrace(); -// throw e; -// } -// } - - @Test - public void testAnnotateInvalidUniprot() throws Exception { - try { - Species protein = new GenericProtein("id"); - protein.setName("bla"); - pdbAnnotator.annotateElement(protein); - - assertEquals(0, protein.getMiriamData().size()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testAnnotateValidUniprotNonexistingPdb() throws Exception { - try { - Species protein = new GenericProtein("id"); - protein.setName("Q88VP8"); - pdbAnnotator.annotateElement(protein); - - boolean pdb = false; - for (MiriamData md : protein.getMiriamData()) { - if (md.getDataType().equals(MiriamType.PDB)) { - pdb = true; - break; - } - } - assertTrue("PDB mapping found for structure for which no should be available", !pdb); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testSimulateDownStatus() throws Exception { - WebPageDownloader downloader = pdbAnnotator.getWebPageDownloader(); - try { - WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); - when(mockDownloader.getFromNetwork(anyString(), anyString(), anyString())).thenReturn(""); - pdbAnnotator.setWebPageDownloader(mockDownloader); - assertEquals(ExternalServiceStatusType.DOWN, pdbAnnotator.getServiceStatus().getStatus()); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } finally { - pdbAnnotator.setWebPageDownloader(downloader); - } - } - - @Test - public void testSimulateChangedStatus() throws Exception { - WebPageDownloader downloader = pdbAnnotator.getWebPageDownloader(); - try { - WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); - when(mockDownloader.getFromNetwork(anyString(), anyString(), nullable(String.class))).thenReturn("{\"P29373\": [{\"xxx\": 140}]}"); - pdbAnnotator.setWebPageDownloader(mockDownloader); - assertEquals(ExternalServiceStatusType.CHANGED, pdbAnnotator.getServiceStatus().getStatus()); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } finally { - pdbAnnotator.setWebPageDownloader(downloader); - } - } - - @Test - public void testSimulateInvalidJson() throws Exception { - WebPageDownloader downloader = pdbAnnotator.getWebPageDownloader(); - try { - Species protein = new GenericProtein("id"); - protein.setName("Q88VP8"); - uniprotAnnotator.annotateElement(protein); - int cntAnnotations1 = protein.getMiriamData().size(); - - WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); - when(mockDownloader.getFromNetwork(anyString(), anyString(), anyString())).thenReturn("\"P29373\": [{\"xxx\": 140}]}"); - pdbAnnotator.setWebPageDownloader(mockDownloader); - - pdbAnnotator.annotateElement(protein); - int cntAnnotations2 = protein.getMiriamData().size(); - assertTrue(cntAnnotations1 == cntAnnotations2); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } finally { - pdbAnnotator.setWebPageDownloader(downloader); - } - } + @Autowired + PdbAnnotator pdbAnnotator; + + @Autowired + UniprotAnnotator uniprotAnnotator; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAnnotate1() throws Exception { + try { + String uniprotId = "P29373"; + Species protein = new GenericProtein("id"); + protein.setName(uniprotId); + /* + * One needs to have the UniProt ID first. This tests simulates situation when + * the Uniprot annotator is called first. + */ + uniprotAnnotator.annotateElement(protein); + int cntAnnotations1 = protein.getMiriamData().size(); + pdbAnnotator.annotateElement(protein); + int cntAnnotations2 = protein.getMiriamData().size(); + + assertTrue(cntAnnotations2 > cntAnnotations1); + + boolean pdb = false; + + for (MiriamData md : protein.getMiriamData()) { + if (md.getDataType().equals(MiriamType.PDB)) { + pdb = true; + break; + } + } + assertTrue("No PDB annotation extracted from pdb annotator", pdb); + + Set<UniprotRecord> urs = protein.getUniprots(); + // Test whether there is a uniprot record + assertTrue(urs.size() > 0); + UniprotRecord ur = null; + for (UniprotRecord ur1 : urs) { + if (ur1.getUniprotId() == uniprotId) { + ur = ur1; + } + } + // Test whether there is a uniprot record with the uniprot ID which was stored + assertNotNull(ur); + // Test whether structures are there + Set<Structure> ss = ur.getStructures(); + assertNotNull(ss); + assertTrue(ss.size() > 0); + // Test whether uniprot is accessible from structure + assertTrue(ss.iterator().next().getUniprot() == ur); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + @Ignore + public void testAnnotate2() throws Exception { + try { + Species protein = new GenericProtein("id"); + protein.setName("P29373"); + /* + * One needs to have the UniProt ID first, and Uniprot lookup through HGNC is + * included in PDB annotator. This tests simulates situation when the PDB + * annotator is called first. + */ + pdbAnnotator.annotateElement(protein); + int cntAnnotations = protein.getMiriamData().size(); + + assertTrue(cntAnnotations > 0); + + boolean pdb = false; + + for (MiriamData md : protein.getMiriamData()) { + if (md.getDataType().equals(MiriamType.PDB)) { + pdb = true; + break; + } + } + assertTrue("No PDB annotation extracted from pdb annotator", pdb); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + @Ignore + public void testAnnotateNotUniprotAnnodated() throws Exception { + try { + Species protein = new GenericProtein("id"); + protein.setName("P29373"); + pdbAnnotator.annotateElement(protein); + + assertTrue("UniProt annotation in PDB annotator failed", protein.getMiriamData().size() > 0); + + boolean pdb = false; + + for (MiriamData md : protein.getMiriamData()) { + if (md.getDataType().equals(MiriamType.PDB)) { + pdb = true; + break; + } + } + assertTrue("No PDB annotation extracted from PDB annotator", pdb); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testAnnotateInvalidUniprot() throws Exception { + try { + Species protein = new GenericProtein("id"); + protein.setName("bla"); + pdbAnnotator.annotateElement(protein); + + assertEquals(0, protein.getMiriamData().size()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testAnnotateValidUniprotNonexistingPdb() throws Exception { + try { + Species protein = new GenericProtein("id"); + protein.setName("Q88VP8"); + pdbAnnotator.annotateElement(protein); + + boolean pdb = false; + for (MiriamData md : protein.getMiriamData()) { + if (md.getDataType().equals(MiriamType.PDB)) { + pdb = true; + break; + } + } + assertTrue("PDB mapping found for structure for which no should be available", !pdb); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testSimulateDownStatus() throws Exception { + WebPageDownloader downloader = pdbAnnotator.getWebPageDownloader(); + try { + WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); + when(mockDownloader.getFromNetwork(anyString(), anyString(), anyString())).thenReturn(""); + pdbAnnotator.setWebPageDownloader(mockDownloader); + assertEquals(ExternalServiceStatusType.DOWN, pdbAnnotator.getServiceStatus().getStatus()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + pdbAnnotator.setWebPageDownloader(downloader); + } + } + + @Test + public void testSimulateChangedStatus() throws Exception { + WebPageDownloader downloader = pdbAnnotator.getWebPageDownloader(); + try { + WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); + when(mockDownloader.getFromNetwork(anyString(), anyString(), nullable(String.class))) + .thenReturn("{\"P29373\": [{\"xxx\": 140}]}"); + pdbAnnotator.setWebPageDownloader(mockDownloader); + assertEquals(ExternalServiceStatusType.CHANGED, pdbAnnotator.getServiceStatus().getStatus()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + pdbAnnotator.setWebPageDownloader(downloader); + } + } + + @Test + public void testSimulateInvalidJson() throws Exception { + WebPageDownloader downloader = pdbAnnotator.getWebPageDownloader(); + try { + Species protein = new GenericProtein("id"); + protein.setName("Q88VP8"); + uniprotAnnotator.annotateElement(protein); + int cntAnnotations1 = protein.getMiriamData().size(); + + WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); + when(mockDownloader.getFromNetwork(anyString(), anyString(), anyString())) + .thenReturn("\"P29373\": [{\"xxx\": 140}]}"); + pdbAnnotator.setWebPageDownloader(mockDownloader); + + pdbAnnotator.annotateElement(protein); + int cntAnnotations2 = protein.getMiriamData().size(); + assertTrue(cntAnnotations1 == cntAnnotations2); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + pdbAnnotator.setWebPageDownloader(downloader); + } + } } diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotatorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotatorTest.java index dc5a6896c06677f7fd3c6f8ff3a73b67c9c8f6dc..78e2ba5200e554e09935c4adc214bab89c9ce907 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotatorTest.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/ReconAnnotatorTest.java @@ -5,9 +5,9 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.nullable; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.when; diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/StitchAnnotatorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/StitchAnnotatorTest.java index b3d3e31e774356c1d25b8076f9aaee2414f0e680..eceab162f8fe8547547c2099fd669489ee85a476 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/StitchAnnotatorTest.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/StitchAnnotatorTest.java @@ -15,86 +15,85 @@ import lcsb.mapviewer.model.map.species.SimpleMolecule; import lcsb.mapviewer.model.map.species.Species; public class StitchAnnotatorTest extends AnnotationTestFunctions { - - @Autowired - StitchAnnotator testedAnnotator; - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testAnnotate() throws Exception { - try { - - Species bioEntity = new SimpleMolecule("id"); - bioEntity.addMiriamData(new MiriamData(MiriamType.CHEBI, "CHEBI:35697")); - - testedAnnotator.annotateElement(bioEntity); - - MiriamData mdStitch = null; - - for (MiriamData md : bioEntity.getMiriamData()) { - if (md.getDataType().equals(MiriamType.STITCH)) { - mdStitch = md; //there should be only one EC number for that TAIR<->UNIPROT record - } - } - - assertTrue("No STITCH annotation extracted from STITCH annotator", mdStitch != null); - assertTrue("Wrong number of annotations extract from STITCH annotator", bioEntity.getMiriamData().size() == 2); - assertTrue("Invalid STITCH annotation extracted from STITCH annotator based on CHEBI ID", mdStitch.getResource().equalsIgnoreCase("WBYWAXJHAXSJNI") ); - - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testExtractInchiMainLayer() throws Exception { - try { - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testAnnotateInvalidEmpty() throws Exception { - try { - Species bioEntity = new SimpleMolecule("id"); - testedAnnotator.annotateElement(bioEntity); - - assertEquals(0, bioEntity.getMiriamData().size()); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testAnnotateInvalidChebi() throws Exception { - try { - Species bioEntity = new SimpleMolecule("id"); - bioEntity.addMiriamData(new MiriamData(MiriamType.CHEBI, "bla")); - testedAnnotator.annotateElement(bioEntity); - - assertEquals(1, bioEntity.getMiriamData().size()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - - //All the service status tests are not necessary, since STTICH annotator - //internally calls ChEBI annotator which has it own set of tests + + @Autowired + StitchAnnotator testedAnnotator; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAnnotate() throws Exception { + try { + + Species bioEntity = new SimpleMolecule("id"); + bioEntity.addMiriamData(new MiriamData(MiriamType.CHEBI, "CHEBI:35697")); + + testedAnnotator.annotateElement(bioEntity); + + MiriamData mdStitch = null; + + for (MiriamData md : bioEntity.getMiriamData()) { + if (md.getDataType().equals(MiriamType.STITCH)) { + mdStitch = md; // there should be only one EC number for that TAIR<->UNIPROT record + } + } + + assertTrue("No STITCH annotation extracted from STITCH annotator", mdStitch != null); + assertTrue("Wrong number of annotations extract from STITCH annotator", bioEntity.getMiriamData().size() == 2); + assertTrue("Invalid STITCH annotation extracted from STITCH annotator based on CHEBI ID", + mdStitch.getResource().equalsIgnoreCase("WBYWAXJHAXSJNI")); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testExtractInchiMainLayer() throws Exception { + try { + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testAnnotateInvalidEmpty() throws Exception { + try { + Species bioEntity = new SimpleMolecule("id"); + testedAnnotator.annotateElement(bioEntity); + + assertEquals(0, bioEntity.getMiriamData().size()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testAnnotateInvalidChebi() throws Exception { + try { + Species bioEntity = new SimpleMolecule("id"); + bioEntity.addMiriamData(new MiriamData(MiriamType.CHEBI, "bla")); + testedAnnotator.annotateElement(bioEntity); + + assertEquals(1, bioEntity.getMiriamData().size()); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + // All the service status tests are not necessary, since STTICH annotator + // internally calls ChEBI annotator which has it own set of tests } diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/TairAnnotatorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/TairAnnotatorTest.java index 2f34409036627a9fde17554ff95291f0df9510ed..f977f8145c6096d3ccd4f2bd598396b68e1dca86 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/TairAnnotatorTest.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/TairAnnotatorTest.java @@ -5,15 +5,14 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.nullable; -import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.when; import java.io.IOException; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; @@ -41,7 +40,7 @@ public class TairAnnotatorTest extends AnnotationTestFunctions { } @Test - //@Ignore("TAIR DB restricts queries by IP") + // @Ignore("TAIR DB restricts queries by IP") public void testAnnotate1() throws Exception { try { @@ -70,7 +69,7 @@ public class TairAnnotatorTest extends AnnotationTestFunctions { } @Test - //@Ignore("TAIR DB restricts queries by IP") + // @Ignore("TAIR DB restricts queries by IP") public void testAnnotateExistingUniprot() throws Exception { try { @@ -155,7 +154,7 @@ public class TairAnnotatorTest extends AnnotationTestFunctions { } @Test - //@Ignore("TAIR DB restricts queries by IP") + // @Ignore("TAIR DB restricts queries by IP") public void testTairToUniprot() throws Exception { try { assertEquals(new MiriamData(MiriamType.UNIPROT, "Q9MAN1", TairAnnotator.class), @@ -167,7 +166,7 @@ public class TairAnnotatorTest extends AnnotationTestFunctions { } @Test - //@Ignore("TAIR DB restricts queries by IP") + // @Ignore("TAIR DB restricts queries by IP") public void testRefreshInvalidCacheQuery() throws Exception { try { tairAnnotator.refreshCacheQuery("invalid_query"); @@ -205,7 +204,7 @@ public class TairAnnotatorTest extends AnnotationTestFunctions { } @Test - //@Ignore("TAIR DB restricts queries by IP") + // @Ignore("TAIR DB restricts queries by IP") public void testStatus() throws Exception { try { assertEquals(ExternalServiceStatusType.OK, tairAnnotator.getServiceStatus().getStatus()); @@ -220,7 +219,8 @@ public class TairAnnotatorTest extends AnnotationTestFunctions { WebPageDownloader downloader = tairAnnotator.getWebPageDownloader(); try { WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); - when(mockDownloader.getFromNetwork(anyString(), anyString(), nullable(String.class))).thenThrow(new IOException()); + when(mockDownloader.getFromNetwork(anyString(), anyString(), nullable(String.class))) + .thenThrow(new IOException()); tairAnnotator.setWebPageDownloader(mockDownloader); assertEquals(ExternalServiceStatusType.DOWN, tairAnnotator.getServiceStatus().getStatus()); } catch (Exception e) { diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/UniprotAnnotatorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/UniprotAnnotatorTest.java index 6427c95760ee12aa7948247bc9e64a15510232dc..a08c898faad1118709d850e63b54ab6a7c32ab3e 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/UniprotAnnotatorTest.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/UniprotAnnotatorTest.java @@ -5,8 +5,8 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.nullable; -import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.when; import java.io.IOException; @@ -330,7 +330,8 @@ public class UniprotAnnotatorTest extends AnnotationTestFunctions { WebPageDownloader downloader = uniprotAnnotator.getWebPageDownloader(); try { WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); - when(mockDownloader.getFromNetwork(anyString(), anyString(), nullable(String.class))).thenThrow(new IOException()); + when(mockDownloader.getFromNetwork(anyString(), anyString(), nullable(String.class))) + .thenThrow(new IOException()); uniprotAnnotator.setWebPageDownloader(mockDownloader); assertEquals(ExternalServiceStatusType.DOWN, uniprotAnnotator.getServiceStatus().getStatus()); } catch (Exception e) { @@ -348,7 +349,8 @@ public class UniprotAnnotatorTest extends AnnotationTestFunctions { uniprotAnnotator.setCache(null); try { WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); - when(mockDownloader.getFromNetwork(anyString(), anyString(), nullable(String.class))).thenThrow(new IOException()); + when(mockDownloader.getFromNetwork(anyString(), anyString(), nullable(String.class))) + .thenThrow(new IOException()); uniprotAnnotator.setWebPageDownloader(mockDownloader); Species protein = new GenericProtein("id"); protein.addMiriamData(new MiriamData(MiriamType.UNIPROT, "P01308"));