PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/org/isatools/isacreator/ontologymanager/BioPortal4Client.java

http://github.com/ISA-tools/ISAcreator
Java | 159 lines | 101 code | 39 blank | 19 comment | 15 complexity | 1ace75644d53f880b7b14068287b99e1 MD5 | raw file
  1. package org.isatools.isacreator.ontologymanager;
  2. import org.apache.log4j.Logger;
  3. import org.isatools.isacreator.configuration.Ontology;
  4. import org.isatools.isacreator.configuration.RecommendedOntology;
  5. import org.isatools.isacreator.ontologymanager.bioportal.io.AcceptedOntologies;
  6. import org.isatools.isacreator.ontologymanager.bioportal.jsonresulthandlers.BioPortalSearchResultHandler;
  7. import org.isatools.isacreator.ontologymanager.common.OntologyTerm;
  8. import java.util.*;
  9. /**
  10. * Created by eamonnmaguire on 17/12/2013.
  11. */
  12. public class BioPortal4Client implements OntologyService {
  13. private static final Logger log = Logger.getLogger(BioPortal4Client.class);
  14. public static final String DIRECT_ONTOLOGY_URL = "http://bioportal.bioontology.org/ontologies/";
  15. public static final String REST_URL = "http://data.bioontology.org/";
  16. private BioPortalSearchResultHandler handler;
  17. private Map<String, Map<String, OntologyTerm>> cachedNodeChildrenQueries;
  18. public BioPortal4Client() {
  19. this.handler = new BioPortalSearchResultHandler();
  20. this.cachedNodeChildrenQueries = new HashMap<String, Map<String, OntologyTerm>>();
  21. }
  22. public Map<String, String> getOntologyNames() {
  23. return AcceptedOntologies.getOntologySourceToNames();
  24. }
  25. /**
  26. * Retrieves an OntologyTerm given the URI of a term and the ontology bioPortal URI
  27. * @param termId
  28. * @param ontology
  29. * @return
  30. */
  31. public OntologyTerm getTerm(String termId, String ontology) {
  32. return handler.getTermMetadata(termId, ontology);
  33. }
  34. public Map<String, List<OntologyTerm>> exactSearch(String term, String ontology) {
  35. return handler.getSearchResults(term, ontology, null, true);
  36. }
  37. public Map<String, String> getTermMetadata(String termId, String ontology) {
  38. return handler.getTermMetadata(termId, ontology).getComments();
  39. }
  40. public Map<OntologySourceRefObject, List<OntologyTerm>> getTermsByPartialNameFromSource(String term, String source, boolean reverseOrder) {
  41. term = correctTermForHTTPTransport(term);
  42. Map<String, List<OntologyTerm>> searchResult = handler.getSearchResults(term, source, null);
  43. return convertStringKeyMapToOntologySourceRefKeyMap(searchResult);
  44. }
  45. public Map<OntologySourceRefObject, List<OntologyTerm>> getTermsByPartialNameFromSource(String term, List<RecommendedOntology> recommendedOntology) {
  46. term = correctTermForHTTPTransport(term);
  47. // need to accommodate more complicated search strings in the case where the recommended source contains the branch
  48. // to search under as well!
  49. Map<OntologySourceRefObject, List<OntologyTerm>> result = new HashMap<OntologySourceRefObject, List<OntologyTerm>>();
  50. // need to do a loop over all branches and do a single query on those recommended ontologies only defined
  51. // by the source, not the branch.
  52. for (RecommendedOntology ro : recommendedOntology) {
  53. if (ro.getOntology() != null) {
  54. String subtree = null;
  55. if (ro.getBranchToSearchUnder() != null && !ro.getBranchToSearchUnder().getBranchIdentifier().equals("")) {
  56. subtree = ro.getBranchToSearchUnder().getBranchIdentifier();
  57. }
  58. Map<String, List<OntologyTerm>> searchResult = handler.getSearchResults(term, ro.getOntology().getOntologyAbbreviation(), subtree);
  59. if (searchResult != null) {
  60. result.putAll(convertStringKeyMapToOntologySourceRefKeyMap(searchResult));
  61. }
  62. }
  63. }
  64. return result;
  65. }
  66. public Map<String, String> getOntologyVersions() {
  67. return AcceptedOntologies.getOntologySourceToVersion();
  68. }
  69. public Map<String, OntologyTerm> getOntologyRoots(String ontologyAbbreviation) {
  70. // http://data.bioontology.org/ontologies/EFO/classes/roots
  71. if (!cachedNodeChildrenQueries.containsKey(ontologyAbbreviation)) {
  72. cachedNodeChildrenQueries.put(ontologyAbbreviation, handler.getOntologyRoots(ontologyAbbreviation));
  73. }
  74. return cachedNodeChildrenQueries.get(ontologyAbbreviation);
  75. }
  76. public Map<String, OntologyTerm> getTermParent(String termAccession, String ontology) {
  77. return null;
  78. }
  79. public Map<String, OntologyTerm> getTermChildren(String termAccession, String ontologyAbbreviation) {
  80. String uniqueReferenceId = ontologyAbbreviation + "-" + termAccession + "-children";
  81. if (!cachedNodeChildrenQueries.containsKey(uniqueReferenceId)) {
  82. cachedNodeChildrenQueries.put(uniqueReferenceId, handler.getTermChildren(termAccession, ontologyAbbreviation));
  83. }
  84. return cachedNodeChildrenQueries.get(uniqueReferenceId);
  85. }
  86. public Map<String, OntologyTerm> getAllTermParents(String termAccession, String ontologyAbbreviation) {
  87. String uniqueReferenceId = ontologyAbbreviation + "-" + termAccession + "-parents";
  88. if (!cachedNodeChildrenQueries.containsKey(uniqueReferenceId)) {
  89. cachedNodeChildrenQueries.put(uniqueReferenceId, handler.getTermParents(termAccession, ontologyAbbreviation));
  90. }
  91. return cachedNodeChildrenQueries.get(uniqueReferenceId);
  92. }
  93. public Collection<Ontology> getAllOntologies() {
  94. return handler.getAllOntologies().values();
  95. }
  96. private Map<OntologySourceRefObject, List<OntologyTerm>> convertStringKeyMapToOntologySourceRefKeyMap(Map<String, List<OntologyTerm>> toConvert) {
  97. Map<OntologySourceRefObject, List<OntologyTerm>> convertedMap = new HashMap<OntologySourceRefObject, List<OntologyTerm>>();
  98. for (String ontologyId : toConvert.keySet()) {
  99. Ontology ontology = AcceptedOntologies.getAcceptedOntologies().get(ontologyId);
  100. if (ontology != null) {
  101. OntologySourceRefObject osro = new OntologySourceRefObject(ontology.getOntologyAbbreviation(),
  102. ontologyId, ontology.getOntologyVersion(), ontology.getOntologyDisplayLabel());
  103. convertedMap.put(osro, new ArrayList<OntologyTerm>());
  104. for (OntologyTerm ontologyTerm : toConvert.get(ontologyId)) {
  105. ontologyTerm.setOntologySourceInformation(osro);
  106. convertedMap.get(osro).add(ontologyTerm);
  107. }
  108. }
  109. }
  110. return convertedMap;
  111. }
  112. private String correctTermForHTTPTransport(String term) {
  113. return term;
  114. // try {
  115. // return URLEncoder.encode(term, "UTF-8");
  116. // } catch (UnsupportedEncodingException e) {
  117. // return term;
  118. // }
  119. }
  120. }