Merge branch 'documentation/JAL-3407_2.11.1_release' into releases/Release_2_11_1_Branch
[jalview.git] / src / jalview / ext / ensembl / EnsemblInfo.java
1 package jalview.ext.ensembl;
2
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.DBRefSource;
5
6 import java.io.BufferedReader;
7 import java.io.IOException;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Set;
15
16 import org.json.simple.JSONArray;
17 import org.json.simple.parser.JSONParser;
18 import org.json.simple.parser.ParseException;
19
20 public class EnsemblInfo extends EnsemblRestClient
21 {
22
23   /*
24    * cached results of REST /info/divisions service, currently
25    * <pre>
26    * { 
27    *  { "ENSEMBLFUNGI", "http://rest.ensemblgenomes.org"},
28    *    "ENSEMBLBACTERIA", "http://rest.ensemblgenomes.org"},
29    *    "ENSEMBLPROTISTS", "http://rest.ensemblgenomes.org"},
30    *    "ENSEMBLMETAZOA", "http://rest.ensemblgenomes.org"},
31    *    "ENSEMBLPLANTS",  "http://rest.ensemblgenomes.org"},
32    *    "ENSEMBL", "http://rest.ensembl.org" }
33    *  }
34    * </pre>
35    * The values for EnsemblGenomes are retrieved by a REST call, that for
36    * Ensembl is added programmatically for convenience of lookup
37    */
38   private static Map<String, String> divisions;
39
40   @Override
41   public String getDbName()
42   {
43     return "ENSEMBL";
44   }
45
46   @Override
47   public AlignmentI getSequenceRecords(String queries) throws Exception
48   {
49     return null;
50   }
51
52   @Override
53   protected URL getUrl(List<String> ids) throws MalformedURLException
54   {
55     return null;
56   }
57
58   @Override
59   protected boolean useGetRequest()
60   {
61     return true;
62   }
63
64   /**
65    * Answers the domain (http://rest.ensembl.org or
66    * http://rest.ensemblgenomes.org) for the given division, or null if not
67    * recognised by Ensembl.
68    * 
69    * @param division
70    * @return
71    */
72   public String getDomain(String division)
73   {
74     if (divisions == null)
75     {
76       fetchDivisions();
77     }
78     return divisions.get(division.toUpperCase());
79   }
80
81   /**
82    * On first request only, populate the lookup map by fetching the list of
83    * divisions known to EnsemblGenomes.
84    */
85   void fetchDivisions()
86   {
87     divisions = new HashMap<>();
88
89     /*
90      * for convenience, pre-fill ensembl.org as the domain for "ENSEMBL"
91      */
92     divisions.put(DBRefSource.ENSEMBL.toUpperCase(), ensemblDomain);
93
94     BufferedReader br = null;
95     try
96     {
97       URL url = getDivisionsUrl(ensemblGenomesDomain);
98       if (url != null)
99       {
100         br = getHttpResponse(url, null);
101       }
102       parseResponse(br, ensemblGenomesDomain);
103     } catch (IOException e)
104     {
105       // ignore
106     } finally
107     {
108       if (br != null)
109       {
110         try
111         {
112           br.close();
113         } catch (IOException e)
114         {
115           // ignore
116         }
117       }
118     }
119   }
120
121   /**
122    * Parses the JSON response to /info/divisions, and add each to the lookup map
123    * 
124    * @param br
125    * @param domain
126    */
127   void parseResponse(BufferedReader br, String domain)
128   {
129     JSONParser jp = new JSONParser();
130
131     try
132     {
133       JSONArray parsed = (JSONArray) jp.parse(br);
134
135       Iterator rvals = parsed.iterator();
136       while (rvals.hasNext())
137       {
138         String division = rvals.next().toString();
139         divisions.put(division.toUpperCase(), domain);
140       }
141     } catch (IOException | ParseException | NumberFormatException e)
142     {
143       // ignore
144     }
145   }
146
147   /**
148    * Constructs the URL for the EnsemblGenomes /info/divisions REST service
149    * @param domain TODO
150    * 
151    * @return
152    * @throws MalformedURLException
153    */
154   URL getDivisionsUrl(String domain) throws MalformedURLException
155   {
156     return new URL(domain
157             + "/info/divisions?content-type=application/json");
158   }
159
160   /**
161    * Returns the set of 'divisions' recognised by Ensembl or EnsemblGenomes
162    * 
163    * @return
164    */
165   public Set<String> getDivisions() {
166     if (divisions == null)
167     {
168       fetchDivisions();
169     }
170
171     return divisions.keySet();
172   }
173 }