From 887a8fc642b517955ff652bafa958c87e17aa0f4 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Fri, 15 Jun 2018 14:27:14 +0200
Subject: [PATCH] API providing genme information

---
 .../genomics/ReferenceGenomeController.java   | 36 +++++++++
 .../api/genomics/ReferenceGenomeRestImpl.java | 76 ++++++++++++++++++-
 .../ReferenceGenomeControllerTest.java        | 53 +++++++++++++
 .../interfaces/IReferenceGenomeService.java   | 18 ++---
 4 files changed, 173 insertions(+), 10 deletions(-)

diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/genomics/ReferenceGenomeController.java b/rest-api/src/main/java/lcsb/mapviewer/api/genomics/ReferenceGenomeController.java
index 67b4a370f0..51342dd986 100644
--- a/rest-api/src/main/java/lcsb/mapviewer/api/genomics/ReferenceGenomeController.java
+++ b/rest-api/src/main/java/lcsb/mapviewer/api/genomics/ReferenceGenomeController.java
@@ -1,5 +1,6 @@
 package lcsb.mapviewer.api.genomics;
 
+import java.util.List;
 import java.util.Map;
 
 import org.springframework.beans.factory.annotation.Autowired;
@@ -32,4 +33,39 @@ public class ReferenceGenomeController extends BaseController {
   ) throws SecurityException, QueryException, ObjectNotFoundException {
     return referenceGenomeController.getReferenceGenome(token, organism, type, version);
   }
+
+  @RequestMapping(value = "/genomics/taxonomies/", method = { RequestMethod.GET }, produces = {
+      MediaType.APPLICATION_JSON_VALUE })
+  public List<Map<String, Object>> getGenomeTaxonomies(//
+      @CookieValue(value = Configuration.AUTH_TOKEN) String token //
+  ) throws SecurityException, QueryException, ObjectNotFoundException {
+    return referenceGenomeController.getReferenceGenomeTaxonomies(token);
+  }
+
+  @RequestMapping(value = "/genomics/taxonomies/{organismId}/genomeTypes/", method = { RequestMethod.GET }, produces = {
+      MediaType.APPLICATION_JSON_VALUE })
+  public List<Map<String, Object>> getGenomeTaxonomyTypes(//
+      @CookieValue(value = Configuration.AUTH_TOKEN) String token, //
+      @PathVariable(value = "organismId") String organism //
+  ) throws SecurityException, QueryException, ObjectNotFoundException {
+    return referenceGenomeController.getReferenceGenomeTypes(token, organism);
+  }
+
+  @RequestMapping(value = "/genomics/taxonomies/{organismId}/genomeTypes/{type}/versions/", method = {
+      RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
+  public List<Map<String, Object>> getGenomeVersion(//
+      @CookieValue(value = Configuration.AUTH_TOKEN) String token, //
+      @PathVariable(value = "organismId") String organism, //
+      @PathVariable(value = "type") String type //
+  ) throws SecurityException, QueryException, ObjectNotFoundException {
+    return referenceGenomeController.getReferenceGenomeVersions(token, organism, type);
+  }
+
+  @RequestMapping(value = "/genomics/", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
+  public List<Map<String, Object>> getDownloaded(//
+      @CookieValue(value = Configuration.AUTH_TOKEN) String token //
+  ) throws SecurityException, QueryException, ObjectNotFoundException {
+    return referenceGenomeController.getReferenceGenomes(token);
+  }
+
 }
\ No newline at end of file
diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/genomics/ReferenceGenomeRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/genomics/ReferenceGenomeRestImpl.java
index 9c1f782af4..b8be215211 100644
--- a/rest-api/src/main/java/lcsb/mapviewer/api/genomics/ReferenceGenomeRestImpl.java
+++ b/rest-api/src/main/java/lcsb/mapviewer/api/genomics/ReferenceGenomeRestImpl.java
@@ -2,8 +2,11 @@ package lcsb.mapviewer.api.genomics;
 
 import java.io.FileNotFoundException;
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeMap;
 
 import org.apache.log4j.Logger;
@@ -11,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
 import lcsb.mapviewer.annotation.cache.BigFileCache;
+import lcsb.mapviewer.annotation.services.genome.ReferenceGenomeConnectorException;
 import lcsb.mapviewer.api.BaseRestImpl;
 import lcsb.mapviewer.api.ObjectNotFoundException;
 import lcsb.mapviewer.api.QueryException;
@@ -71,7 +75,7 @@ public class ReferenceGenomeRestImpl extends BaseRestImpl {
     result.put("localUrl", getLocalUrl(genome.getSourceUrl()));
     result.put("geneMapping", geneMappingToMaps(genome.getGeneMapping()));
 
-    return null;
+    return result;
   }
 
   private String getLocalUrl(String sourceUrl) {
@@ -102,4 +106,74 @@ public class ReferenceGenomeRestImpl extends BaseRestImpl {
     return result;
   }
 
+  public List<Map<String, Object>> getReferenceGenomeTaxonomies(String token) throws QueryException {
+    try {
+      Set<MiriamData> organisms = new HashSet<>();
+      for (ReferenceGenomeType type : ReferenceGenomeType.values()) {
+        organisms.addAll(referenceGenomeService.getOrganismsByReferenceGenomeType(type));
+      }
+      List<Map<String, Object>> result = new ArrayList<>();
+      for (MiriamData miriamData : organisms) {
+        result.add(createAnnotation(miriamData));
+      }
+      return result;
+    } catch (ReferenceGenomeConnectorException e) {
+      throw new QueryException("Problem with obtaining organism list", e);
+    }
+  }
+
+  public List<Map<String, Object>> getReferenceGenomeTypes(String token, String organismId) throws QueryException {
+    try {
+      Set<MiriamData> organisms = new HashSet<>();
+      for (ReferenceGenomeType type : ReferenceGenomeType.values()) {
+        organisms.addAll(referenceGenomeService.getOrganismsByReferenceGenomeType(type));
+      }
+      List<Map<String, Object>> result = new ArrayList<>();
+      for (MiriamData miriamData : organisms) {
+        if (miriamData.getResource().equals(organismId)) {
+          Map<String, Object> entry = new HashMap<>();
+          entry.put("type", ReferenceGenomeType.UCSC.toString());
+          result.add(entry);
+        }
+      }
+      return result;
+    } catch (ReferenceGenomeConnectorException e) {
+      throw new QueryException("Problem with obtaining organism list", e);
+    }
+  }
+
+  public List<Map<String, Object>> getReferenceGenomeVersions(String token, String organismId, String type)
+      throws QueryException {
+    ReferenceGenomeType genomeType = ReferenceGenomeType.valueOf(type);
+    MiriamData organism = null;
+    if (organismId != null && !organismId.isEmpty()) {
+      organism = new MiriamData(MiriamType.TAXONOMY, organismId);
+    } else {
+      throw new QueryException("Unknown taxonomy organism: " + organismId);
+    }
+    List<String> versions;
+    try {
+      versions = referenceGenomeService.getAvailableGenomeVersions(genomeType, organism);
+    } catch (ReferenceGenomeConnectorException e) {
+      throw new QueryException("Problem with obtaining version list", e);
+    }
+
+    List<Map<String, Object>> result = new ArrayList<>();
+    for (String string : versions) {
+      Map<String, Object> entry = new HashMap<>();
+      entry.put("version", string);
+      result.add(entry);
+    }
+    return result;
+  }
+
+  public List<Map<String, Object>> getReferenceGenomes(String token)
+      throws SecurityException, QueryException, ObjectNotFoundException {
+    List<Map<String, Object>> result = new ArrayList<>();
+    for (ReferenceGenome genome : referenceGenomeService.getDownloadedGenomes()) {
+      result.add(genomeToMap(genome));
+    }
+    return result;
+  }
+
 }
diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/genomics/ReferenceGenomeControllerTest.java b/rest-api/src/test/java/lcsb/mapviewer/api/genomics/ReferenceGenomeControllerTest.java
index 4d9929a940..d1bb8e0b1f 100644
--- a/rest-api/src/test/java/lcsb/mapviewer/api/genomics/ReferenceGenomeControllerTest.java
+++ b/rest-api/src/test/java/lcsb/mapviewer/api/genomics/ReferenceGenomeControllerTest.java
@@ -1,8 +1,12 @@
 package lcsb.mapviewer.api.genomics;
 
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.util.List;
+import java.util.Map;
+
 import org.apache.log4j.Logger;
 import org.junit.After;
 import org.junit.AfterClass;
@@ -58,4 +62,53 @@ public class ReferenceGenomeControllerTest extends RestTestFunctions {
     }
   }
 
+  @Test
+  public void testGetReferenceGenomeTaxonomies() throws Exception {
+    try {
+      List<Map<String, Object>> result = referenceGenomeRestImpl.getReferenceGenomeTaxonomies(token);
+      assertNotNull(result);
+      assertTrue(result.size() > 0);
+    } catch (Exception e) {
+      e.printStackTrace();
+      throw e;
+    }
+  }
+
+  @Test
+  public void testGetReferenceGenomeTypes() throws Exception {
+    try {
+      List<Map<String, Object>> result = referenceGenomeRestImpl.getReferenceGenomeTypes(token,"9606");
+      assertNotNull(result);
+      assertTrue(result.size() > 0);
+    } catch (Exception e) {
+      e.printStackTrace();
+      throw e;
+    }
+  }
+
+  @Test
+  public void testGetReferenceGenomeVersions() throws Exception {
+    try {
+      List<Map<String, Object>> result = referenceGenomeRestImpl.getReferenceGenomeVersions(token,"9606","UCSC");
+      assertNotNull(result);
+      assertTrue(result.size() > 0);
+    } catch (Exception e) {
+      e.printStackTrace();
+      throw e;
+    }
+  }
+
+  @Test
+  public void testGetAllDowloadedReferenceGenomes() throws Exception {
+    try {
+      List<?> result = referenceGenomeRestImpl.getReferenceGenomes(token);
+      assertNotNull(result);
+      assertTrue(result.size() > 0);
+    } catch (Exception e) {
+      e.printStackTrace();
+      throw e;
+    }
+  }
+
+
 }
diff --git a/service/src/main/java/lcsb/mapviewer/services/interfaces/IReferenceGenomeService.java b/service/src/main/java/lcsb/mapviewer/services/interfaces/IReferenceGenomeService.java
index 419116d1d7..c87597d001 100644
--- a/service/src/main/java/lcsb/mapviewer/services/interfaces/IReferenceGenomeService.java
+++ b/service/src/main/java/lcsb/mapviewer/services/interfaces/IReferenceGenomeService.java
@@ -11,7 +11,7 @@ import lcsb.mapviewer.model.map.layout.ReferenceGenomeGeneMapping;
 import lcsb.mapviewer.model.map.layout.ReferenceGenomeType;
 
 /**
- * Service used to maintain referemce genome data.
+ * Service used to maintain reference genome data.
  * 
  * @author Piotr Gawron
  *
@@ -24,7 +24,7 @@ public interface IReferenceGenomeService {
    * @param type
    *          {@link ReferenceGenomeType type} of reference genome
    * @param organism
-   *          organism for which renference genome is added
+   *          organism for which reference genome is added
    * @param version
    *          version of the reference genome
    * @param customUrl
@@ -35,7 +35,7 @@ public interface IReferenceGenomeService {
    *           thrown when url is invalid
    * @throws ReferenceGenomeConnectorException
    *           thrown when reference genome already exists or there is problem
-   *           wirh adding genome
+   *           with adding genome
    */
   void addReferenceGenome(ReferenceGenomeType type, MiriamData organism, String version, String customUrl)
       throws IOException, URISyntaxException, ReferenceGenomeConnectorException;
@@ -49,7 +49,7 @@ public interface IReferenceGenomeService {
    * @return list of available organisms
    * @throws ReferenceGenomeConnectorException
    *           thrown when there is a problem with accessing information about
-   *           reference genoems
+   *           reference genomes
    */
   List<MiriamData> getOrganismsByReferenceGenomeType(ReferenceGenomeType type) throws ReferenceGenomeConnectorException;
 
@@ -64,7 +64,7 @@ public interface IReferenceGenomeService {
    * @return list of available organisms
    * @throws ReferenceGenomeConnectorException
    *           thrown when there is a problem with accessing information about
-   *           reference genoems
+   *           reference genomes
    */
   List<String> getAvailableGenomeVersions(ReferenceGenomeType type, MiriamData organism)
       throws ReferenceGenomeConnectorException;
@@ -83,9 +83,9 @@ public interface IReferenceGenomeService {
   String getUrlForGenomeVersion(ReferenceGenomeType type, MiriamData organism, String version);
 
   /**
-   * Returns list of all downladed reference genomes.
+   * Returns list of all downloaded reference genomes.
    * 
-   * @return list of all downladed reference genomes
+   * @return list of all downloaded reference genomes
    */
   List<ReferenceGenome> getDownloadedGenomes();
 
@@ -100,7 +100,7 @@ public interface IReferenceGenomeService {
   void removeGenome(ReferenceGenome genome) throws IOException;
 
   /**
-   * Adds gene mapping to refernce genome.
+   * Adds gene mapping to reference genome.
    * 
    * @param referenceGenome
    *          reference genome where we add mapping
@@ -113,7 +113,7 @@ public interface IReferenceGenomeService {
    * @throws URISyntaxException
    *           thrown when url is invalid
    * @throws ReferenceGenomeConnectorException
-   *           thrown when there is a problem with manipulatinf information about
+   *           thrown when there is a problem with manipulating information about
    *           reference genome
    */
   void addReferenceGenomeGeneMapping(ReferenceGenome referenceGenome, String name, String url)
-- 
GitLab