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