JAL-1705 include any unmapped protein start 'X' when aligning to dna
[jalview.git] / src / jalview / ext / ensembl / EnsemblLookup.java
1 package jalview.ext.ensembl;
2
3 import jalview.datamodel.AlignmentI;
4
5 import java.io.BufferedReader;
6 import java.io.IOException;
7 import java.net.MalformedURLException;
8 import java.net.URL;
9 import java.util.Arrays;
10 import java.util.List;
11
12 import org.json.simple.JSONObject;
13 import org.json.simple.parser.JSONParser;
14 import org.json.simple.parser.ParseException;
15
16 public class EnsemblLookup extends EnsemblRestClient
17 {
18
19   @Override
20   public String getDbName()
21   {
22     return "ENSEMBL";
23   }
24
25   @Override
26   public AlignmentI getSequenceRecords(String queries) throws Exception
27   {
28     return null;
29   }
30
31   @Override
32   protected URL getUrl(List<String> ids) throws MalformedURLException
33   {
34     String identifier = ids.get(0);
35     return getUrl(identifier);
36   }
37
38   /**
39    * @param identifier
40    * @return
41    */
42   protected URL getUrl(String identifier)
43   {
44     String url = ENSEMBL_REST + "/lookup/id/" + identifier
45             + "?content-type=application/json";
46     try
47     {
48       return new URL(url);
49     } catch (MalformedURLException e)
50     {
51       return null;
52     }
53   }
54
55   @Override
56   protected boolean useGetRequest()
57   {
58     return true;
59   }
60
61   @Override
62   protected String getRequestMimeType(boolean multipleIds)
63   {
64     return "application/json";
65   }
66
67   @Override
68   protected String getResponseMimeType()
69   {
70     return "application/json";
71   }
72
73   /**
74    * Calls the Ensembl lookup REST endpoint and retrieves the 'Parent' for the
75    * given identifier, or null if not found
76    * 
77    * @param identifier
78    * @return
79    */
80   public String getParent(String identifier)
81   {
82     List<String> ids = Arrays.asList(new String[] { identifier });
83   
84     BufferedReader br = null;
85     try
86     {
87       URL url = getUrl(identifier);
88       if (url != null)
89       {
90         br = getHttpResponse(url, ids);
91       }
92       return (parseResponse(br));
93     } catch (IOException e)
94     {
95       // ignore
96       return null;
97     } finally
98     {
99       if (br != null)
100       {
101         try
102         {
103           br.close();
104         } catch (IOException e)
105         {
106           // ignore
107         }
108       }
109     }
110   }
111
112   /**
113    * Parses "Parent" from the JSON response and returns the value, or null if
114    * not found
115    * 
116    * @param br
117    * @return
118    * @throws IOException
119    */
120   protected String parseResponse(BufferedReader br) throws IOException
121   {
122     String parent = null;
123     JSONParser jp = new JSONParser();
124     try
125     {
126       JSONObject val = (JSONObject) jp.parse(br);
127       parent = val.get("Parent").toString();
128     } catch (ParseException e)
129     {
130       // ignore
131     }
132     return parent;
133   }
134
135 }