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