JAL-3253 one global singleton instance of EnsemblInfo suffices
[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.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.parser.ParseException;
16
17 public class EnsemblInfo extends EnsemblRestClient
18 {
19
20   /**
21    * cached results of REST /info/divisions service, currently
22    * 
23    * <pre>
24    * { 
25    *  "ENSEMBLFUNGI", "http://rest.ensemblgenomes.org"},
26    *  "ENSEMBLBACTERIA", "http://rest.ensemblgenomes.org"},
27    *  "ENSEMBLPROTISTS", "http://rest.ensemblgenomes.org"},
28    *  "ENSEMBLMETAZOA", "http://rest.ensemblgenomes.org"},
29    *  "ENSEMBLPLANTS",  "http://rest.ensemblgenomes.org"},
30    *  "ENSEMBL", "http://rest.ensembl.org"
31    *  }
32    * </pre>
33    * 
34    * The values for EnsemblGenomes are retrieved by a REST call, that for
35    * Ensembl is added programmatically for convenience of lookup
36    */
37   private static Map<String, String> divisions;
38
39   @Override
40   public String getDbName()
41   {
42     return "ENSEMBL";
43   }
44
45   @Override
46   public AlignmentI getSequenceRecords(String queries) throws Exception
47   {
48     return null;
49   }
50
51   @Override
52   protected URL getUrl(List<String> ids) throws MalformedURLException
53   {
54     return null;
55   }
56
57   @Override
58   protected boolean useGetRequest()
59   {
60     return true;
61   }
62
63   /**
64    * Answers the domain (http://rest.ensembl.org or
65    * http://rest.ensemblgenomes.org) for the given division, or null if not
66    * recognised by Ensembl.
67    * 
68    * @param division
69    * @return
70    */
71   public String getDomain(String division)
72   {
73     if (divisions == null)
74     {
75       fetchDivisions();
76     }
77     return divisions.get(division.toUpperCase());
78   }
79
80   /**
81    * On first request only, populate the lookup map by fetching the list of
82    * divisions known to EnsemblGenomes.
83    */
84   void fetchDivisions()
85   {
86     divisions = new HashMap<>();
87
88     /*
89      * for convenience, pre-fill ensembl.org as the domain for "ENSEMBL"
90      */
91     divisions.put(DBRefSource.ENSEMBL.toUpperCase(), ensemblDomain);
92     try
93     {
94       @SuppressWarnings("unchecked")
95       Iterator<Object> rvals = (Iterator<Object>) getJSON(
96               getDivisionsUrl(ensemblGenomesDomain), null, -1,
97               MODE_ITERATOR, null);
98       if (rvals == null)
99       {
100         return;
101       }
102       while (rvals.hasNext())
103       {
104         String division = rvals.next().toString();
105         divisions.put(division.toUpperCase(), ensemblGenomesDomain);
106       }
107     } catch (IOException | ParseException | NumberFormatException e)
108     {
109       // ignore
110     }
111   }
112
113   /**
114    * Constructs the URL for the EnsemblGenomes /info/divisions REST service
115    * @param domain TODO
116    * 
117    * @return
118    * @throws MalformedURLException
119    */
120   URL getDivisionsUrl(String domain) throws MalformedURLException
121   {
122     return new URL(domain
123             + "/info/divisions?content-type=application/json");
124   }
125
126   /**
127    * Returns the set of 'divisions' recognised by Ensembl or EnsemblGenomes
128    * 
129    * @return
130    */
131   public Set<String> getDivisions()
132   {
133     if (divisions == null)
134     {
135       fetchDivisions();
136     }
137
138     return divisions.keySet();
139   }
140 }