JAL-1705 fetch all Ensembl xrefs (except GO terms)
[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 d
29    */
30   public EnsemblSymbol(String d)
31   {
32     super(d);
33   }
34
35   /**
36    * Returns the first "id" value in gene identifier format from the JSON
37    * response, or null if none found
38    * 
39    * @param br
40    * @return
41    * @throws IOException
42    */
43   protected String parseSymbolResponse(BufferedReader br)
44           throws IOException
45   {
46     JSONParser jp = new JSONParser();
47     String result = null;
48     try
49     {
50       JSONArray responses = (JSONArray) jp.parse(br);
51       Iterator rvals = responses.iterator();
52       while (rvals.hasNext())
53       {
54         JSONObject val = (JSONObject) rvals.next();
55         String id = val.get("id").toString();
56         if (id != null && isGeneIdentifier(id))
57         {
58           result = id;
59           break;
60         }
61       }
62     } catch (ParseException e)
63     {
64       // ignore
65     }
66     return result;
67   }
68
69   protected URL getUrl(String id, Species species)
70   {
71     String url = getDomain() + "/xrefs/symbol/" + species.toString() + "/"
72             + id
73             + "?content-type=application/json";
74     try
75     {
76       return new URL(url);
77     } catch (MalformedURLException e)
78     {
79       return null;
80     }
81   }
82
83   /**
84    * Calls the Ensembl xrefs REST 'symbol' endpoint and retrieves any gene ids
85    * for the given identifier, for any known model organisms
86    * 
87    * @param identifier
88    * @return
89    */
90   public List<String> getIds(String identifier)
91   {
92     List<String> result = new ArrayList<String>();
93     List<String> ids = new ArrayList<String>();
94     ids.add(identifier);
95   
96     String[] queries = identifier.split(getAccessionSeparator());
97     BufferedReader br = null;
98     try
99     {
100       for (String query : queries)
101       {
102         for (Species taxon : Species.values())
103         {
104           if (taxon.isModelOrganism())
105           {
106             URL url = getUrl(query, taxon);
107             if (url != null)
108             {
109               br = getHttpResponse(url, ids);
110             }
111             String geneId = parseSymbolResponse(br);
112             if (geneId != null)
113             {
114               result.add(geneId);
115             }
116           }
117         }
118       }
119     } catch (IOException e)
120     {
121       // ignore
122     } finally
123     {
124       if (br != null)
125       {
126         try
127         {
128           br.close();
129         } catch (IOException e)
130         {
131           // ignore
132         }
133       }
134     }
135     return result;
136   }
137
138 }