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