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