97ad242e0795ee66d0a5d65ae6f287d36d7817f8
[jalview.git] / src / jalview / ext / ensembl / EnsemblInfo.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ext.ensembl;
22
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.DBRefSource;
25 import jalview.util.JSONUtils;
26
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.net.MalformedURLException;
30 import java.net.URL;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36
37 import org.json.simple.parser.ParseException;
38
39 public class EnsemblInfo extends EnsemblRestClient
40 {
41
42   /*
43    * cached results of REST /info/divisions service, currently
44    * <pre>
45    * { 
46    *  { "ENSEMBLFUNGI", "http://rest.ensemblgenomes.org"},
47    *    "ENSEMBLBACTERIA", "http://rest.ensemblgenomes.org"},
48    *    "ENSEMBLPROTISTS", "http://rest.ensemblgenomes.org"},
49    *    "ENSEMBLMETAZOA", "http://rest.ensemblgenomes.org"},
50    *    "ENSEMBLPLANTS",  "http://rest.ensemblgenomes.org"},
51    *    "ENSEMBL", "http://rest.ensembl.org" }
52    *  }
53    * </pre>
54    * The values for EnsemblGenomes are retrieved by a REST call, that for
55    * Ensembl is added programmatically for convenience of lookup
56    */
57   private static Map<String, String> divisions;
58
59   @Override
60   public String getDbName()
61   {
62     return "ENSEMBL";
63   }
64
65   @Override
66   public AlignmentI getSequenceRecords(String queries) throws Exception
67   {
68     return null;
69   }
70
71   @Override
72   protected URL getUrl(List<String> ids) throws MalformedURLException
73   {
74     return null;
75   }
76
77   @Override
78   protected boolean useGetRequest()
79   {
80     return true;
81   }
82
83   /**
84    * Answers the domain (http://rest.ensembl.org or
85    * http://rest.ensemblgenomes.org) for the given division, or null if not
86    * recognised by Ensembl.
87    * 
88    * @param division
89    * @return
90    */
91   public String getDomain(String division)
92   {
93     if (divisions == null)
94     {
95       fetchDivisions();
96     }
97     return divisions.get(division.toUpperCase());
98   }
99
100   /**
101    * On first request only, populate the lookup map by fetching the list of
102    * divisions known to EnsemblGenomes.
103    */
104   void fetchDivisions()
105   {
106     divisions = new HashMap<>();
107
108     /*
109      * for convenience, pre-fill ensembl.org as the domain for "ENSEMBL"
110      */
111     divisions.put(DBRefSource.ENSEMBL.toUpperCase(), ensemblDomain);
112     try
113     {
114       @SuppressWarnings("unchecked")
115           Iterator<Object> rvals = (Iterator<Object>) getJSON(getDivisionsUrl(ensemblGenomesDomain), null, -1, MODE_ITERATOR, null);
116       if (rvals == null)
117           return;
118       while (rvals.hasNext())
119       {
120         String division = rvals.next().toString();
121         divisions.put(division.toUpperCase(), ensemblGenomesDomain);
122       }
123     } catch (IOException | ParseException | NumberFormatException e)
124     {
125       // ignore
126     }
127   }
128
129   /**
130    * Constructs the URL for the EnsemblGenomes /info/divisions REST service
131    * @param domain TODO
132    * 
133    * @return
134    * @throws MalformedURLException
135    */
136   URL getDivisionsUrl(String domain) throws MalformedURLException
137   {
138     return new URL(domain
139             + "/info/divisions?content-type=application/json");
140   }
141
142   /**
143    * Returns the set of 'divisions' recognised by Ensembl or EnsemblGenomes
144    * 
145    * @return
146    */
147   public Set<String> getDivisions() {
148     if (divisions == null)
149     {
150       fetchDivisions();
151     }
152
153     return divisions.keySet();
154   }
155 }