CvParameterLookupService.java

  1. /*
  2.  * Copyright 2018 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V..
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package de.isas.mztab2.cvmapping;

  17. import de.isas.mztab2.model.Parameter;
  18. import java.util.LinkedHashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.stream.Collectors;
  22. import lombok.extern.slf4j.Slf4j;
  23. import uk.ac.ebi.pride.utilities.ols.web.service.client.OLSClient;
  24. import uk.ac.ebi.pride.utilities.ols.web.service.config.OLSWsConfig;
  25. import uk.ac.ebi.pride.utilities.ols.web.service.model.Identifier;

  26. /**
  27.  * Abstraction over OLSClient to autoconvert Terms to Parameters and to allow
  28.  * easy matching of Parameters against parent terms and their children.
  29.  *
  30.  * @author nilshoffmann
  31.  */
  32. @Slf4j
  33. public class CvParameterLookupService {

  34.     private final OLSClient client;
  35.     private final Map<Parameter, List<Parameter>> childCache;
  36.     private final Map<Parameter, List<Parameter>> parentCache;

  37.     private static <K, V> Map<K, V> lruCache(final int maxSize) {
  38.         return new LinkedHashMap<K, V>(maxSize * 4 / 3, 0.75f, true) {
  39.             @Override
  40.             protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
  41.                 return size() > maxSize;
  42.             }
  43.         };
  44.     }

  45.     /**
  46.      * Create a new instance of the lookup service with default OLS configuration.
  47.      */
  48.     public CvParameterLookupService() {
  49.         this(new OLSClient(new OLSWsConfig()));
  50.     }

  51.     /**
  52.      * Create a new instance of the lookup service with a custom OLSClient.
  53.      * @param client the custom OLS client
  54.      */
  55.     public CvParameterLookupService(OLSClient client) {
  56.         this.client = client;
  57.         this.childCache = lruCache(4096);
  58.         this.parentCache = lruCache(4096);
  59.     }

  60.     /**
  61.      * Create a new instance of the lookup service with a custom OLSWsConfig configuration.
  62.      * @param config the custom configuration
  63.      */
  64.     public CvParameterLookupService(OLSWsConfig config) {
  65.         this(new OLSClient(config));
  66.     }
  67.    
  68.     /**
  69.      * Clears all query result caches (parent and child).
  70.      */
  71.     public void clearCaches() {
  72.         this.childCache.clear();
  73.         this.parentCache.clear();
  74.     }

  75.     /**
  76.      * Resolve all parents of parameter up to an arbitrary depth (actually height, since we go from bottom to top).
  77.      * Use at your own risk, the OLS service may terminate your connection if the response is too large or takes too long.
  78.      * @param parameter the parameter to start from
  79.      * @return a list of all parent parameters for the given parameter
  80.      * @throws org.springframework.web.client.HttpClientErrorException on http related errors
  81.      */
  82.     public List<Parameter> resolveParents(Parameter parameter) throws org.springframework.web.client.HttpClientErrorException {
  83.         return resolveParents(parameter, -1);
  84.     }

  85.     /**
  86.      * Resolve all parents of a parameter up to a given maximum depth (1 meaning the immediate parents, -1 meaning all).
  87.      * @param parameter the parameter to start from
  88.      * @param levels maximum levels to query
  89.      * @return a list of all parent parameters for the given parameter
  90.      * @throws org.springframework.web.client.HttpClientErrorException on http related errors
  91.      */
  92.     public List<Parameter> resolveParents(Parameter parameter, int levels) throws org.springframework.web.client.HttpClientErrorException {
  93.         if (parameter.getCvAccession() == null || parameter.getCvLabel() == null) {
  94.             throw new IllegalArgumentException(
  95.                 "Parameter must provide cvAccession and cvLabel!");
  96.         }
  97.         if(parentCache.containsKey(parameter)) {
  98.             log.debug("Cache hit for parameter "+parameter+" in parent cache!");
  99.             return parentCache.get(parameter);
  100.         }
  101.         Identifier ident = new Identifier(parameter.getCvAccession(),
  102.             Identifier.IdentifierType.OBO);
  103.         List<Parameter> parents = client.getTermParents(ident, parameter.getCvLabel(), levels).
  104.             stream().
  105.             map(CvMappingUtils::asParameter).
  106.             collect(Collectors.toList());
  107.         parentCache.put(parameter, parents);
  108.         return parents;
  109.     }

  110.     /**
  111.      * Resolve all children of a parameter up to a given maximum depth (1 meaning immediate children, -1 meaning all).
  112.      * @param parameter the parameter to start from
  113.      * @param levels maximum levels to query
  114.      * @return a list of all child parameters for the given parameter
  115.      * @throws org.springframework.web.client.HttpClientErrorException on http related errors
  116.      */
  117.     public List<Parameter> resolveChildren(Parameter parameter, int levels) throws org.springframework.web.client.HttpClientErrorException {
  118.         if (parameter.getCvAccession() == null || parameter.getCvLabel() == null) {
  119.             throw new IllegalArgumentException(
  120.                 "Parameter must provide cvAccession and cvLabel!");
  121.         }
  122.         if(childCache.containsKey(parameter)) {
  123.             log.debug("Cache hit for parameter "+parameter+" in child cache!");
  124.             return childCache.get(parameter);
  125.         }
  126.         Identifier ident = new Identifier(parameter.getCvAccession(),
  127.             Identifier.IdentifierType.OBO);
  128.         List<Parameter> children = client.getTermChildren(ident, parameter.getCvLabel(), levels).
  129.             stream().
  130.             map(CvMappingUtils::asParameter).
  131.             collect(Collectors.toList());
  132.         childCache.put(parameter, children);
  133.         return children;
  134.     }

  135.     /**
  136.      * Resolve all children of a parameter up to an arbitrary depth.
  137.      * Use at your own risk, the OLS service may terminate your connection if the response is too large or takes too long.
  138.      * @param parameter the parameter to start from
  139.      * @return a list of all child parameters for the given parameter
  140.      * @throws org.springframework.web.client.HttpClientErrorException on http related errors
  141.      */
  142.     public List<Parameter> resolveChildren(Parameter parameter) throws org.springframework.web.client.HttpClientErrorException {
  143.         return resolveChildren(parameter, -1);
  144.     }

  145.     /**
  146.      * Compares two parameters for their parent to child relationship. The result can be one of: IDENTICAL, if parent and potential child are the same node,
  147.      * CHILD_OF, if potentialChild is a child of parent (at least 1 level away), or NOT_RELATED, if there is no path from child to parent.
  148.      * @param parent the parent term to start from
  149.      * @param potentialChild the potential child term to check against parent
  150.      * @return the comparison result
  151.      * @throws org.springframework.web.client.HttpClientErrorException on http related errors
  152.      */
  153.     public ParameterComparisonResult isChildOfOrSame(Parameter parent,
  154.         Parameter potentialChild) throws org.springframework.web.client.HttpClientErrorException {
  155.         if (parent.getCvAccession().
  156.             toUpperCase().
  157.             equals(potentialChild.getCvAccession().
  158.                 toUpperCase())) {
  159.             return ParameterComparisonResult.IDENTICAL;
  160.         }
  161.         List<Parameter> parentsOf = resolveParents(potentialChild);
  162.         boolean result = parentsOf.stream().
  163.             anyMatch((potentialParent) ->
  164.             {
  165.                 return CvMappingUtils.isEqualTo(potentialParent, parent);
  166.             });
  167.         if (result) {
  168.             return ParameterComparisonResult.CHILD_OF;
  169.         }
  170.         return ParameterComparisonResult.NOT_RELATED;
  171.     }

  172. }