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