JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / ext / ensembl / EnsemblInfo.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ext.ensembl;
22
23 import java.util.Locale;
24
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.DBRefSource;
27 import jalview.util.JSONUtils;
28
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.net.MalformedURLException;
32 import java.net.URL;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38
39 import org.json.simple.parser.ParseException;
40
41 public class EnsemblInfo extends EnsemblRestClient
42 {
43
44   /*
45    * cached results of REST /info/divisions service, currently
46    * <pre>
47    * { 
48    *  { "ENSEMBLFUNGI", "http://rest.ensemblgenomes.org"},
49    *    "ENSEMBLBACTERIA", "http://rest.ensemblgenomes.org"},
50    *    "ENSEMBLPROTISTS", "http://rest.ensemblgenomes.org"},
51    *    "ENSEMBLMETAZOA", "http://rest.ensemblgenomes.org"},
52    *    "ENSEMBLPLANTS",  "http://rest.ensemblgenomes.org"},
53    *    "ENSEMBL", "http://rest.ensembl.org" }
54    *  }
55    * </pre>
56    * The values for EnsemblGenomes are retrieved by a REST call, that for
57    * Ensembl is added programmatically for convenience of lookup
58    */
59   private static Map<String, String> divisions;
60
61   @Override
62   public String getDbName()
63   {
64     return "ENSEMBL";
65   }
66
67   @Override
68   public AlignmentI getSequenceRecords(String queries) throws Exception
69   {
70     return null;
71   }
72
73   @Override
74   protected URL getUrl(List<String> ids) throws MalformedURLException
75   {
76     return null;
77   }
78
79   @Override
80   protected boolean useGetRequest()
81   {
82     return true;
83   }
84
85   /**
86    * Answers the domain (http://rest.ensembl.org or
87    * http://rest.ensemblgenomes.org) for the given division, or null if not
88    * recognised by Ensembl.
89    * 
90    * @param division
91    * @return
92    */
93   public String getDomain(String division)
94   {
95     if (divisions == null)
96     {
97       fetchDivisions();
98     }
99     return divisions.get(division.toUpperCase(Locale.ROOT));
100   }
101
102   /**
103    * On first request only, populate the lookup map by fetching the list of
104    * divisions known to EnsemblGenomes.
105    */
106   void fetchDivisions()
107   {
108     divisions = new HashMap<>();
109
110     /*
111      * for convenience, pre-fill ensembl.org as the domain for "ENSEMBL"
112      */
113     divisions.put(DBRefSource.ENSEMBL.toUpperCase(Locale.ROOT),
114             ensemblDomain);
115     try
116     {
117       @SuppressWarnings("unchecked")
118       Iterator<Object> rvals = (Iterator<Object>) getJSON(
119               getDivisionsUrl(ensemblGenomesDomain), null, -1,
120               MODE_ITERATOR, null);
121       if (rvals == null)
122         return;
123       while (rvals.hasNext())
124       {
125         String division = rvals.next().toString();
126         divisions.put(division.toUpperCase(Locale.ROOT),
127                 ensemblGenomesDomain);
128       }
129     } catch (IOException | ParseException | NumberFormatException e)
130     {
131       // ignore
132     }
133   }
134
135   /**
136    * Constructs the URL for the EnsemblGenomes /info/divisions REST service
137    * 
138    * @param domain
139    *          TODO
140    * 
141    * @return
142    * @throws MalformedURLException
143    */
144   URL getDivisionsUrl(String domain) throws MalformedURLException
145   {
146     return new URL(
147             domain + "/info/divisions?content-type=application/json");
148   }
149
150   /**
151    * Returns the set of 'divisions' recognised by Ensembl or EnsemblGenomes
152    * 
153    * @return
154    */
155   public Set<String> getDivisions()
156   {
157     if (divisions == null)
158     {
159       fetchDivisions();
160     }
161
162     return divisions.keySet();
163   }
164 }