JAL-3383 code and unit test tweaks
[jalview.git] / src / jalview / ext / ensembl / EnsemblInfo.java
1 package jalview.ext.ensembl;
2
3 import jalview.bin.ApplicationSingletonProvider;
4 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.DBRefSource;
7
8 import java.io.IOException;
9 import java.net.MalformedURLException;
10 import java.net.URL;
11 import java.util.HashMap;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 import org.json.simple.parser.ParseException;
18
19 public class EnsemblInfo extends EnsemblRestClient implements ApplicationSingletonI
20 {
21
22   /**
23    * On first request only, populate the lookup map by fetching the list of
24    * divisions known to EnsemblGenomes
25    * 
26    */
27   private static EnsemblInfo getInstance()
28   {
29     return (EnsemblInfo) ApplicationSingletonProvider.getInstance(EnsemblInfo.class);
30   }
31
32   private EnsemblInfo()
33   {
34     // use getInstance()
35
36     /*
37      * for convenience, pre-fill ensembl.org as the domain for "ENSEMBL"
38      */
39     divisions.put(DBRefSource.ENSEMBL.toUpperCase(), ensemblDomain);
40     try
41     {
42       @SuppressWarnings("unchecked")
43       Iterator<Object> rvals = (Iterator<Object>) getJSON(
44               getDivisionsUrl(ensemblGenomesDomain), null, -1,
45               MODE_ITERATOR, null);
46       if (rvals == null)
47       {
48         return;
49       }
50       while (rvals.hasNext())
51       {
52         String division = rvals.next().toString();
53         divisions.put(division.toUpperCase(), ensemblGenomesDomain);
54       }
55     } catch (IOException | ParseException | NumberFormatException e)
56     {
57       // ignore
58     }
59   }
60
61   /*
62    * cached results of REST /info/divisions service, currently
63    * <pre>
64    * { 
65    *  { "ENSEMBLFUNGI", "http://rest.ensemblgenomes.org"},
66    *    "ENSEMBLBACTERIA", "http://rest.ensemblgenomes.org"},
67    *    "ENSEMBLPROTISTS", "http://rest.ensemblgenomes.org"},
68    *    "ENSEMBLMETAZOA", "http://rest.ensemblgenomes.org"},
69    *    "ENSEMBLPLANTS",  "http://rest.ensemblgenomes.org"},
70    *    "ENSEMBL", "http://rest.ensembl.org" }
71    *  }
72    * </pre>
73    * The values for EnsemblGenomes are retrieved by a REST call, that for
74    * Ensembl is added programmatically for convenience of lookup
75    */
76   private Map<String, String> divisions = new HashMap<>();
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 }