JAL-3253 preliminary static fixes for JavaScript part 2
[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   private static EnsemblInfo instance;
21
22   /**
23    * Some question as to whether it is necessary to do this for each applet. IN
24    * PRINCIPLE, applets could set different properties for the origin of Ensembl
25    * data. But I suggest this is unlikely. If we DO care about that possibility,
26    * then we need to set doICare to Platform.isJS();
27    * 
28    */
29   private final static boolean doICare = false;// Platform.isJS();
30
31   /**
32    * On first request only, populate the lookup map by fetching the list of
33    * divisions known to EnsemblGenomes.
34    * 
35    */
36   private static EnsemblInfo getInstance()
37   {
38
39     // BH 2019.05.08 need to isolate static fields in JavaScript
40
41     EnsemblInfo i = instance;
42     @SuppressWarnings("unused")
43     ThreadGroup g = null;
44
45     if (doICare)
46     {
47       g = Thread.currentThread().getThreadGroup();
48       /**
49        * @j2sNative i = g._jalviewEnsemblInstance;
50        * 
51        */
52     }
53     if (i == null)
54     {
55       i = new EnsemblInfo();
56
57       if (doICare)
58       {
59         /**
60          * @j2sNative g._jalviewEnsemblInstance = i;
61          * 
62          */
63       }
64       else
65       {
66         instance = i;
67       }
68     }
69     return i;
70   }
71
72   /*
73    * cached results of REST /info/divisions service, currently
74    * <pre>
75    * { 
76    *  { "ENSEMBLFUNGI", "http://rest.ensemblgenomes.org"},
77    *    "ENSEMBLBACTERIA", "http://rest.ensemblgenomes.org"},
78    *    "ENSEMBLPROTISTS", "http://rest.ensemblgenomes.org"},
79    *    "ENSEMBLMETAZOA", "http://rest.ensemblgenomes.org"},
80    *    "ENSEMBLPLANTS",  "http://rest.ensemblgenomes.org"},
81    *    "ENSEMBL", "http://rest.ensembl.org" }
82    *  }
83    * </pre>
84    * The values for EnsemblGenomes are retrieved by a REST call, that for
85    * Ensembl is added programmatically for convenience of lookup
86    */
87   private Map<String, String> divisions = new HashMap<>();
88
89   private EnsemblInfo()
90   {
91
92     /*
93      * for convenience, pre-fill ensembl.org as the domain for "ENSEMBL"
94      */
95     divisions.put(DBRefSource.ENSEMBL.toUpperCase(), ensemblDomain);
96     try
97     {
98       @SuppressWarnings("unchecked")
99       Iterator<Object> rvals = (Iterator<Object>) getJSON(
100               getDivisionsUrl(ensemblGenomesDomain), null, -1,
101               MODE_ITERATOR, null);
102       if (rvals == null)
103       {
104         return;
105       }
106       while (rvals.hasNext())
107       {
108         String division = rvals.next().toString();
109         divisions.put(division.toUpperCase(), ensemblGenomesDomain);
110       }
111     } catch (IOException | ParseException | NumberFormatException e)
112     {
113       // ignore
114     }
115   }
116
117   @Override
118   public String getDbName()
119   {
120     return "ENSEMBL";
121   }
122
123   @Override
124   public AlignmentI getSequenceRecords(String queries) throws Exception
125   {
126     return null;
127   }
128
129   @Override
130   protected URL getUrl(List<String> ids) throws MalformedURLException
131   {
132     return null;
133   }
134
135   @Override
136   protected boolean useGetRequest()
137   {
138     return true;
139   }
140
141   /**
142    * Answers the domain (http://rest.ensembl.org or
143    * http://rest.ensemblgenomes.org) for the given division, or null if not
144    * recognised by Ensembl.
145    * 
146    * @param division
147    * @return
148    */
149   public static String getDomain(String division)
150   {
151     return getInstance().divisions.get(division.toUpperCase());
152   }
153
154   /**
155    * Constructs the URL for the EnsemblGenomes /info/divisions REST service
156    * @param domain TODO
157    * 
158    * @return
159    * @throws MalformedURLException
160    */
161   URL getDivisionsUrl(String domain) throws MalformedURLException
162   {
163     return new URL(domain
164             + "/info/divisions?content-type=application/json");
165   }
166
167   /**
168    * Returns the set of 'divisions' recognised by Ensembl or EnsemblGenomes
169    * 
170    * @return
171    */
172   public static Set<String> getDivisions()
173   {
174     return getInstance().divisions.keySet();
175   }
176 }