All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.monarchinitiative.phenol.io.utils.CurieUtilBuilder Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package org.monarchinitiative.phenol.io.utils;

import com.google.common.collect.ImmutableMap;
import org.prefixcommons.CurieUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.Map;

/**
 * Wrapper class to help build a {@link CurieUtil} using the default mappings and / or just a subset.
 *
 * @author Jules Jacobsen 
 */
public class CurieUtilBuilder {

  private static final Logger LOGGER = LoggerFactory.getLogger(CurieUtilBuilder.class);


  private static final Map DEFAULT_CURIE_MAP = ImmutableMap.copyOf(generate());
  private static final CurieUtil DEFAULT_CURIE_UTIL = new CurieUtil(DEFAULT_CURIE_MAP);


  private CurieUtilBuilder() {
  }

  public static Map defaultCurieMap() {
    return DEFAULT_CURIE_MAP;
  }

  public static CurieUtil defaultCurieUtil() {
    return DEFAULT_CURIE_UTIL;
  }

  public static CurieUtil withDefaultsAnd(Map additionalCuries) {
    ImmutableMap.Builder merged = new ImmutableMap.Builder<>();
    merged.putAll(DEFAULT_CURIE_MAP);
    merged.putAll(additionalCuries);
    return new CurieUtil(merged.build());
  }

  public static CurieUtil just(Map curies) {
    return new CurieUtil(ImmutableMap.copyOf(curies));
  }

  /**
   * Reads curie_map.yaml and put the k-v entries into curieMap, which will be used for initialization of
   * CurieUtil. The original curie_map.yaml is available at Dipper's Github:
   * https://github.com/monarch-initiative/dipper/blob/master/dipper/curie_map.yaml.
   */
  private static Map generate() {
    try {
      InputStream inputStream = CurieUtilBuilder.class.getClassLoader().getResourceAsStream("curie_map.yaml");
      Yaml yaml = new Yaml();
      return yaml.load(inputStream);
    } catch (Exception e) {
      LOGGER.error(e.getMessage(), e);
    }
    return ImmutableMap.of();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy