JAL-3210 Barebones gradle/buildship/eclipse. See README
[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 import jalview.util.JSONUtils;
6
7 import java.io.BufferedReader;
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
20 {
21
22   /*
23    * cached results of REST /info/divisions service, currently
24    * <pre>
25    * { 
26    *  { "ENSEMBLFUNGI", "http://rest.ensemblgenomes.org"},
27    *    "ENSEMBLBACTERIA", "http://rest.ensemblgenomes.org"},
28    *    "ENSEMBLPROTISTS", "http://rest.ensemblgenomes.org"},
29    *    "ENSEMBLMETAZOA", "http://rest.ensemblgenomes.org"},
30    *    "ENSEMBLPLANTS",  "http://rest.ensemblgenomes.org"},
31    *    "ENSEMBL", "http://rest.ensembl.org" }
32    *  }
33    * </pre>
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(getDivisionsUrl(ensemblGenomesDomain), null, -1, MODE_ITERATOR, null);
96       if (rvals == null)
97           return;
98       while (rvals.hasNext())
99       {
100         String division = rvals.next().toString();
101         divisions.put(division.toUpperCase(), ensemblGenomesDomain);
102       }
103     } catch (IOException | ParseException | NumberFormatException e)
104     {
105       // ignore
106     }
107   }
108
109   /**
110    * Constructs the URL for the EnsemblGenomes /info/divisions REST service
111    * @param domain TODO
112    * 
113    * @return
114    * @throws MalformedURLException
115    */
116   URL getDivisionsUrl(String domain) throws MalformedURLException
117   {
118     return new URL(domain
119             + "/info/divisions?content-type=application/json");
120   }
121
122   /**
123    * Returns the set of 'divisions' recognised by Ensembl or EnsemblGenomes
124    * 
125    * @return
126    */
127   public Set<String> getDivisions() {
128     if (divisions == null)
129     {
130       fetchDivisions();
131     }
132
133     return divisions.keySet();
134   }
135 }