1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.ext.ensembl; |
22 |
|
|
23 |
|
import jalview.bin.Cache; |
24 |
|
import jalview.datamodel.AlignmentI; |
25 |
|
import jalview.datamodel.GeneLociI; |
26 |
|
import jalview.util.MapList; |
27 |
|
|
28 |
|
import java.io.BufferedReader; |
29 |
|
import java.io.IOException; |
30 |
|
import java.net.MalformedURLException; |
31 |
|
import java.net.URL; |
32 |
|
import java.util.Arrays; |
33 |
|
import java.util.Collections; |
34 |
|
import java.util.List; |
35 |
|
|
36 |
|
import org.json.simple.JSONObject; |
37 |
|
import org.json.simple.parser.JSONParser; |
38 |
|
import org.json.simple.parser.ParseException; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
@author |
46 |
|
|
|
|
| 0% |
Uncovered Elements: 121 (121) |
Complexity: 38 |
Complexity Density: 0.52 |
|
47 |
|
public class EnsemblLookup extends EnsemblRestClient |
48 |
|
{ |
49 |
|
private static final String SPECIES = "species"; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
54 |
0 |
public EnsemblLookup()... |
55 |
|
{ |
56 |
0 |
super(); |
57 |
|
} |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@param |
63 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
0 |
public EnsemblLookup(String d)... |
65 |
|
{ |
66 |
0 |
super(d); |
67 |
|
} |
68 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
69 |
0 |
@Override... |
70 |
|
public String getDbName() |
71 |
|
{ |
72 |
0 |
return "ENSEMBL"; |
73 |
|
} |
74 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
75 |
0 |
@Override... |
76 |
|
public AlignmentI getSequenceRecords(String queries) throws Exception |
77 |
|
{ |
78 |
0 |
return null; |
79 |
|
} |
80 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
81 |
0 |
@Override... |
82 |
|
protected URL getUrl(List<String> ids) throws MalformedURLException |
83 |
|
{ |
84 |
0 |
String identifier = ids.get(0); |
85 |
0 |
return getUrl(identifier, null); |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
@param |
93 |
|
@param |
94 |
|
@return |
95 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
96 |
0 |
protected URL getUrl(String identifier, String objectType)... |
97 |
|
{ |
98 |
0 |
String url = getDomain() + "/lookup/id/" + identifier |
99 |
|
+ CONTENT_TYPE_JSON; |
100 |
0 |
if (objectType != null) |
101 |
|
{ |
102 |
0 |
url += "&" + OBJECT_TYPE + "=" + objectType; |
103 |
|
} |
104 |
|
|
105 |
0 |
try |
106 |
|
{ |
107 |
0 |
return new URL(url); |
108 |
|
} catch (MalformedURLException e) |
109 |
|
{ |
110 |
0 |
return null; |
111 |
|
} |
112 |
|
} |
113 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
0 |
@Override... |
115 |
|
protected boolean useGetRequest() |
116 |
|
{ |
117 |
0 |
return true; |
118 |
|
} |
119 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
120 |
0 |
@Override... |
121 |
|
protected String getRequestMimeType(boolean multipleIds) |
122 |
|
{ |
123 |
0 |
return "application/json"; |
124 |
|
} |
125 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
126 |
0 |
@Override... |
127 |
|
protected String getResponseMimeType() |
128 |
|
{ |
129 |
0 |
return "application/json"; |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
@param |
137 |
|
@return |
138 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
139 |
0 |
public String getGeneId(String identifier)... |
140 |
|
{ |
141 |
0 |
return getGeneId(identifier, null); |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
@param |
149 |
|
@param |
150 |
|
@return |
151 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
152 |
0 |
public String getGeneId(String identifier, String objectType)... |
153 |
|
{ |
154 |
0 |
return parseGeneId(getResult(identifier, objectType)); |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
@param |
164 |
|
@return |
165 |
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 5 |
Complexity Density: 0.42 |
|
166 |
0 |
protected String parseGeneId(JSONObject val)... |
167 |
|
{ |
168 |
0 |
if (val == null) |
169 |
|
{ |
170 |
0 |
return null; |
171 |
|
} |
172 |
0 |
String geneId = null; |
173 |
0 |
String type = val.get(OBJECT_TYPE).toString(); |
174 |
0 |
if (OBJECT_TYPE_GENE.equalsIgnoreCase(type)) |
175 |
|
{ |
176 |
|
|
177 |
0 |
geneId = val.get(JSON_ID).toString(); |
178 |
|
} |
179 |
0 |
else if (OBJECT_TYPE_TRANSCRIPT.equalsIgnoreCase(type)) |
180 |
|
{ |
181 |
|
|
182 |
0 |
geneId = val.get(PARENT).toString(); |
183 |
|
} |
184 |
0 |
else if (OBJECT_TYPE_TRANSLATION.equalsIgnoreCase(type)) |
185 |
|
{ |
186 |
|
|
187 |
0 |
String transcriptId = val.get(PARENT).toString(); |
188 |
0 |
geneId = getGeneId(transcriptId, OBJECT_TYPE_TRANSCRIPT); |
189 |
|
} |
190 |
|
|
191 |
0 |
return geneId; |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
@param |
199 |
|
@return |
200 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
201 |
0 |
public String getSpecies(String identifier)... |
202 |
|
{ |
203 |
0 |
String species = null; |
204 |
0 |
JSONObject json = getResult(identifier, null); |
205 |
0 |
if (json != null) |
206 |
|
{ |
207 |
0 |
Object o = json.get(SPECIES); |
208 |
0 |
if (o != null) |
209 |
|
{ |
210 |
0 |
species = o.toString(); |
211 |
|
} |
212 |
|
} |
213 |
0 |
return species; |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
@param |
221 |
|
@param |
222 |
|
|
223 |
|
@return |
224 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 6 |
Complexity Density: 0.5 |
|
225 |
0 |
protected JSONObject getResult(String identifier, String objectType)... |
226 |
|
{ |
227 |
0 |
List<String> ids = Arrays.asList(new String[] { identifier }); |
228 |
|
|
229 |
0 |
BufferedReader br = null; |
230 |
0 |
try |
231 |
|
{ |
232 |
0 |
URL url = getUrl(identifier, objectType); |
233 |
|
|
234 |
0 |
if (url != null) |
235 |
|
{ |
236 |
0 |
br = getHttpResponse(url, ids); |
237 |
|
} |
238 |
0 |
return br == null ? null : (JSONObject) (new JSONParser().parse(br)); |
239 |
|
} catch (IOException | ParseException e) |
240 |
|
{ |
241 |
0 |
System.err.println("Error parsing " + identifier + " lookup response " |
242 |
|
+ e.getMessage()); |
243 |
0 |
return null; |
244 |
|
} finally |
245 |
|
{ |
246 |
0 |
if (br != null) |
247 |
|
{ |
248 |
0 |
try |
249 |
|
{ |
250 |
0 |
br.close(); |
251 |
|
} catch (IOException e) |
252 |
|
{ |
253 |
|
|
254 |
|
} |
255 |
|
} |
256 |
|
} |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
@param |
264 |
|
@return |
265 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
266 |
0 |
public GeneLociI getGeneLoci(String geneId)... |
267 |
|
{ |
268 |
0 |
return parseGeneLoci(getResult(geneId, OBJECT_TYPE_GENE)); |
269 |
|
} |
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
@param |
277 |
|
@return |
278 |
|
|
|
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 5 |
Complexity Density: 0.25 |
|
279 |
0 |
GeneLociI parseGeneLoci(JSONObject json)... |
280 |
|
{ |
281 |
0 |
if (json == null) |
282 |
|
{ |
283 |
0 |
return null; |
284 |
|
} |
285 |
|
|
286 |
0 |
try |
287 |
|
{ |
288 |
0 |
final String species = json.get("species").toString(); |
289 |
0 |
final String assembly = json.get("assembly_name").toString(); |
290 |
0 |
final String chromosome = json.get("seq_region_name").toString(); |
291 |
0 |
String strand = json.get("strand").toString(); |
292 |
0 |
int start = Integer.parseInt(json.get("start").toString()); |
293 |
0 |
int end = Integer.parseInt(json.get("end").toString()); |
294 |
0 |
int fromEnd = end - start + 1; |
295 |
0 |
boolean reverseStrand = "-1".equals(strand); |
296 |
0 |
int toStart = reverseStrand ? end : start; |
297 |
0 |
int toEnd = reverseStrand ? start : end; |
298 |
0 |
List<int[]> fromRange = Collections.singletonList(new int[] { 1, |
299 |
|
fromEnd }); |
300 |
0 |
List<int[]> toRange = Collections.singletonList(new int[] { toStart, |
301 |
|
toEnd }); |
302 |
0 |
final MapList map = new MapList(fromRange, toRange, 1, 1); |
303 |
0 |
return new GeneLociI() |
304 |
|
{ |
305 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
306 |
0 |
@Override... |
307 |
|
public String getSpeciesId() |
308 |
|
{ |
309 |
0 |
return species == null ? "" : species; |
310 |
|
} |
311 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
312 |
0 |
@Override... |
313 |
|
public String getAssemblyId() |
314 |
|
{ |
315 |
0 |
return assembly; |
316 |
|
} |
317 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
318 |
0 |
@Override... |
319 |
|
public String getChromosomeId() |
320 |
|
{ |
321 |
0 |
return chromosome; |
322 |
|
} |
323 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
324 |
0 |
@Override... |
325 |
|
public MapList getMap() |
326 |
|
{ |
327 |
0 |
return map; |
328 |
|
} |
329 |
|
}; |
330 |
|
} catch (NullPointerException | NumberFormatException e) |
331 |
|
{ |
332 |
0 |
Cache.log.error("Error looking up gene loci: " + e.getMessage()); |
333 |
0 |
e.printStackTrace(); |
334 |
|
} |
335 |
0 |
return null; |
336 |
|
} |
337 |
|
|
338 |
|
} |