Merge branch 'develop' into merge/develop_bug/JAL-2154projectMappings
[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
75             + "?content-type=application/json";
76     try
77     {
78       return new URL(url);
79     } catch (MalformedURLException e)
80     {
81       return null;
82     }
83   }
84
85   /**
86    * Calls the Ensembl xrefs REST 'symbol' endpoint and retrieves any gene ids
87    * for the given identifier, for any known model organisms
88    * 
89    * @param identifier
90    * @return
91    */
92   public List<String> getIds(String identifier)
93   {
94     List<String> result = new ArrayList<String>();
95     List<String> ids = new ArrayList<String>();
96     ids.add(identifier);
97   
98     String[] queries = identifier.split(getAccessionSeparator());
99     BufferedReader br = null;
100     try
101     {
102       for (String query : queries)
103       {
104         for (Species taxon : Species.values())
105         {
106           if (taxon.isModelOrganism())
107           {
108             URL url = getUrl(query, taxon);
109             if (url != null)
110             {
111               br = getHttpResponse(url, ids);
112             }
113             String geneId = parseSymbolResponse(br);
114             if (geneId != null)
115             {
116               result.add(geneId);
117             }
118           }
119         }
120       }
121     } catch (IOException e)
122     {
123       // ignore
124     } finally
125     {
126       if (br != null)
127       {
128         try
129         {
130           br.close();
131         } catch (IOException e)
132         {
133           // ignore
134         }
135       }
136     }
137     return result;
138   }
139
140 }