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