9f86731d27b4e070e4cb787d520df4b103e8e496
[jalview.git] / src / jalview / ext / ensembl / EnsemblSymbol.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.io.BufferedReader;
24 import java.io.IOException;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30
31 import org.json.simple.JSONArray;
32 import org.json.simple.JSONObject;
33 import org.json.simple.parser.JSONParser;
34 import org.json.simple.parser.ParseException;
35
36 /**
37  * A client for the Ensembl xrefs/symbol REST service;
38  * 
39  * @see http://rest.ensembl.org/documentation/info/xref_external
40  * @author gmcarstairs
41  *
42  */
43 public class EnsemblSymbol extends EnsemblXref
44 {
45   /**
46    * Constructor given the target domain to fetch data from
47    * 
48    * @param domain
49    * @param dbName
50    * @param dbVersion
51    */
52   public EnsemblSymbol(String domain, String dbName, String dbVersion)
53   {
54     super(domain, dbName, dbVersion);
55   }
56
57   /**
58    * Returns the first "id" value in gene identifier format from the JSON
59    * response, or null if none found
60    * 
61    * @param br
62    * @return
63    * @throws IOException
64    */
65   protected String parseSymbolResponse(BufferedReader br) throws IOException
66   {
67     JSONParser jp = new JSONParser();
68     String result = null;
69     try
70     {
71       JSONArray responses = (JSONArray) jp.parse(br);
72       Iterator rvals = responses.iterator();
73       while (rvals.hasNext())
74       {
75         JSONObject val = (JSONObject) rvals.next();
76         String id = val.get("id").toString();
77         if (id != null && isGeneIdentifier(id))
78         {
79           result = id;
80           break;
81         }
82       }
83     } catch (ParseException e)
84     {
85       // ignore
86     }
87     return result;
88   }
89
90   protected URL getUrl(String id, Species species)
91   {
92     String url = getDomain() + "/xrefs/symbol/" + species.toString() + "/"
93             + id + "?content-type=application/json";
94     try
95     {
96       return new URL(url);
97     } catch (MalformedURLException e)
98     {
99       return null;
100     }
101   }
102
103   /**
104    * Calls the Ensembl xrefs REST 'symbol' endpoint and retrieves any gene ids
105    * for the given identifier, for any known model organisms
106    * 
107    * @param identifier
108    * @return
109    */
110   public List<String> getIds(String identifier)
111   {
112     List<String> result = new ArrayList<String>();
113     List<String> ids = new ArrayList<String>();
114     ids.add(identifier);
115
116     String[] queries = identifier.split(getAccessionSeparator());
117     BufferedReader br = null;
118     try
119     {
120       for (String query : queries)
121       {
122         for (Species taxon : Species.values())
123         {
124           if (taxon.isModelOrganism())
125           {
126             URL url = getUrl(query, taxon);
127             if (url != null)
128             {
129               br = getHttpResponse(url, ids);
130             }
131             String geneId = parseSymbolResponse(br);
132             if (geneId != null)
133             {
134               result.add(geneId);
135             }
136           }
137         }
138       }
139     } catch (IOException e)
140     {
141       // ignore
142     } finally
143     {
144       if (br != null)
145       {
146         try
147         {
148           br.close();
149         } catch (IOException e)
150         {
151           // ignore
152         }
153       }
154     }
155     return result;
156   }
157
158 }