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