JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / ext / ensembl / EnsemblMap.java
index add71b3..f2ab195 100644 (file)
@@ -1,12 +1,32 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.ext.ensembl;
 
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.DBRefSource;
 import jalview.datamodel.GeneLociI;
-import jalview.util.JSONUtils;
+import jalview.datamodel.GeneLocus;
+import jalview.datamodel.Mapping;
 import jalview.util.MapList;
 
-import java.io.BufferedReader;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -18,6 +38,18 @@ import java.util.Map;
 
 import org.json.simple.parser.ParseException;
 
+/**
+ * A client for the Ensembl REST service /map endpoint, to convert from
+ * coordinates of one genome assembly to another.
+ * <p>
+ * Note that species and assembly identifiers passed to this class must be valid
+ * in Ensembl. They are not case sensitive.
+ * 
+ * @author gmcarstairs
+ * @see https://rest.ensembl.org/documentation/info/assembly_map
+ * @see https://rest.ensembl.org/info/assembly/human?content-type=text/xml
+ * @see https://rest.ensembl.org/info/species?content-type=text/xml
+ */
 public class EnsemblMap extends EnsemblRestClient
 {
   private static final String MAPPED = "mapped";
@@ -72,8 +104,8 @@ public class EnsemblMap extends EnsemblRestClient
    * @return
    * @throws MalformedURLException
    */
-  protected URL getAssemblyMapUrl(String species, String chromosome, String fromRef,
-          String toRef, int startPos, int endPos)
+  protected URL getAssemblyMapUrl(String species, String chromosome,
+          String fromRef, String toRef, int startPos, int endPos)
           throws MalformedURLException
   {
     /*
@@ -120,8 +152,8 @@ public class EnsemblMap extends EnsemblRestClient
     URL url = null;
     try
     {
-      url = getAssemblyMapUrl(species, chromosome, fromRef, toRef, queryRange[0],
-              queryRange[1]);
+      url = getAssemblyMapUrl(species, chromosome, fromRef, toRef,
+              queryRange[0], queryRange[1]);
       return (parseAssemblyMappingResponse(url));
     } catch (Throwable t)
     {
@@ -152,9 +184,12 @@ public class EnsemblMap extends EnsemblRestClient
 
     try
     {
-      Iterator<Object> rvals = (Iterator<Object>) getJSON(url, null, -1, MODE_ITERATOR, MAPPINGS);
+      Iterator<Object> rvals = (Iterator<Object>) getJSON(url, null, -1,
+              MODE_ITERATOR, MAPPINGS);
       if (rvals == null)
-         return null;
+      {
+        return null;
+      }
       while (rvals.hasNext())
       {
         // todo check for "mapped"
@@ -251,9 +286,9 @@ public class EnsemblMap extends EnsemblRestClient
   URL getIdMapUrl(String domain, String accession, int start, int end,
           String cdsOrCdna) throws MalformedURLException
   {
-    String url = String
-            .format("%s/map/%s/%s/%d..%d?include_original_region=1&content-type=application/json",
-                    domain, cdsOrCdna, accession, start, end);
+    String url = String.format(
+            "%s/map/%s/%s/%d..%d?include_original_region=1&content-type=application/json",
+            domain, cdsOrCdna, accession, start, end);
     return new URL(url);
   }
 
@@ -278,15 +313,17 @@ public class EnsemblMap extends EnsemblRestClient
    * @return
    */
   @SuppressWarnings("unchecked")
-GeneLociI parseIdMappingResponse(URL url, String accession,
-          String domain)
+  GeneLociI parseIdMappingResponse(URL url, String accession, String domain)
   {
 
     try
     {
-      Iterator<Object> rvals = (Iterator<Object>) getJSON(url, null, -1, MODE_ITERATOR, MAPPINGS);
+      Iterator<Object> rvals = (Iterator<Object>) getJSON(url, null, -1,
+              MODE_ITERATOR, MAPPINGS);
       if (rvals == null)
-         return null;
+      {
+        return null;
+      }
       String assembly = null;
       String chromosome = null;
       int fromEnd = 0;
@@ -295,7 +332,8 @@ GeneLociI parseIdMappingResponse(URL url, String accession,
       while (rvals.hasNext())
       {
         Map<String, Object> val = (Map<String, Object>) rvals.next();
-        Map<String, Object> original = (Map<String, Object>) val.get("original");
+        Map<String, Object> original = (Map<String, Object>) val
+                .get("original");
         fromEnd = Integer.parseInt(original.get("end").toString());
 
         Map<String, Object> mapped = (Map<String, Object>) val.get(MAPPED);
@@ -304,16 +342,16 @@ GeneLociI parseIdMappingResponse(URL url, String accession,
         String ass = mapped.get("assembly_name").toString();
         if (assembly != null && !assembly.equals(ass))
         {
-          System.err
-                  .println("EnsemblMap found multiple assemblies - can't resolve");
+          System.err.println(
+                  "EnsemblMap found multiple assemblies - can't resolve");
           return null;
         }
         assembly = ass;
         String chr = mapped.get("seq_region_name").toString();
         if (chromosome != null && !chromosome.equals(chr))
         {
-          System.err
-                  .println("EnsemblMap found multiple chromosomes - can't resolve");
+          System.err.println(
+                  "EnsemblMap found multiple chromosomes - can't resolve");
           return null;
         }
         chromosome = chr;
@@ -336,36 +374,12 @@ GeneLociI parseIdMappingResponse(URL url, String accession,
               .getSpecies(accession);
       final String as = assembly;
       final String chr = chromosome;
-      List<int[]> fromRange = Collections.singletonList(new int[] { 1,
-          fromEnd });
-      final MapList map = new MapList(fromRange, regions, 1, 1);
-      return new GeneLociI()
-      {
-
-        @Override
-        public String getSpeciesId()
-        {
-          return species == null ? "" : species;
-        }
-
-        @Override
-        public String getAssemblyId()
-        {
-          return as;
-        }
-
-        @Override
-        public String getChromosomeId()
-        {
-          return chr;
-        }
-
-        @Override
-        public MapList getMap()
-        {
-          return map;
-        }
-      };
+      List<int[]> fromRange = Collections
+              .singletonList(new int[]
+              { 1, fromEnd });
+      Mapping mapping = new Mapping(new MapList(fromRange, regions, 1, 1));
+      return new GeneLocus(species == null ? "" : species, as, chr,
+              mapping);
     } catch (IOException | ParseException | NumberFormatException e)
     {
       // ignore