*/
package jalview.ext.ensembl;
-import jalview.bin.Cache;
+import jalview.bin.Console;
import jalview.datamodel.AlignmentI;
import jalview.datamodel.GeneLociI;
+import jalview.datamodel.GeneLocus;
+import jalview.datamodel.Mapping;
import jalview.util.MapList;
-import java.io.BufferedReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import java.util.Map;
-import org.json.simple.JSONObject;
-import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
return true;
}
- @Override
- protected String getRequestMimeType(boolean multipleIds)
- {
- return "application/json";
- }
-
- @Override
- protected String getResponseMimeType()
- {
- return "application/json";
- }
-
/**
* Returns the gene id related to the given identifier (which may be for a
- * gene, transcript or protein)
+ * gene, transcript or protein), or null if none is found
*
* @param identifier
* @return
/**
* Returns the gene id related to the given identifier (which may be for a
- * gene, transcript or protein)
+ * gene, transcript or protein), or null if none is found
*
* @param identifier
* @param objectType
*/
public String getGeneId(String identifier, String objectType)
{
- List<String> ids = Arrays.asList(new String[] { identifier });
-
- BufferedReader br = null;
- try
- {
- URL url = getUrl(identifier, objectType);
- if (url != null)
- {
- br = getHttpResponse(url, ids);
- }
- return br == null ? null : parseResponse(br);
- } catch (IOException e)
- {
- // ignore
- return null;
- } finally
- {
- if (br != null)
- {
- try
- {
- br.close();
- } catch (IOException e)
- {
- // ignore
- }
- }
- }
+ return parseGeneId(getResult(identifier, objectType));
}
/**
*
* @param br
* @return
- * @throws IOException
*/
- protected String parseResponse(BufferedReader br) throws IOException
+ protected String parseGeneId(Map<String, Object> val)
{
+ if (val == null)
+ {
+ return null;
+ }
String geneId = null;
- JSONParser jp = new JSONParser();
- try
+ String type = val.get(OBJECT_TYPE).toString();
+ if (OBJECT_TYPE_GENE.equalsIgnoreCase(type))
{
- JSONObject val = (JSONObject) jp.parse(br);
- String type = val.get(OBJECT_TYPE).toString();
- if (OBJECT_TYPE_GENE.equalsIgnoreCase(type))
- {
- // got the gene - just returns its id
- geneId = val.get(JSON_ID).toString();
- }
- else if (OBJECT_TYPE_TRANSCRIPT.equalsIgnoreCase(type))
- {
- // got the transcript - return its (Gene) Parent
- geneId = val.get(PARENT).toString();
- }
- else if (OBJECT_TYPE_TRANSLATION.equalsIgnoreCase(type))
- {
- // got the protein - get its Parent, restricted to type Transcript
- String transcriptId = val.get(PARENT).toString();
- geneId = getGeneId(transcriptId, OBJECT_TYPE_TRANSCRIPT);
- }
- } catch (ParseException e)
+ // got the gene - just returns its id
+ geneId = val.get(JSON_ID).toString();
+ }
+ else if (OBJECT_TYPE_TRANSCRIPT.equalsIgnoreCase(type))
{
- // ignore
+ // got the transcript - return its (Gene) Parent
+ geneId = val.get(PARENT).toString();
}
+ else if (OBJECT_TYPE_TRANSLATION.equalsIgnoreCase(type))
+ {
+ // got the protein - get its Parent, restricted to type Transcript
+ String transcriptId = val.get(PARENT).toString();
+ geneId = getGeneId(transcriptId, OBJECT_TYPE_TRANSCRIPT);
+ }
+
return geneId;
}
public String getSpecies(String identifier)
{
String species = null;
- JSONObject json = getResult(identifier, null);
+ Map<String, Object> json = getResult(identifier, null);
if (json != null)
{
Object o = json.get(SPECIES);
}
/**
- * Calls the /lookup/id rest service and returns the response as a JSONObject,
- * or null if any error
+ * Calls the /lookup/id rest service and returns the response as a Map<String,
+ * Object>, or null if any error
*
* @param identifier
* @param objectType
* (optional)
* @return
*/
- protected JSONObject getResult(String identifier, String objectType)
+ @SuppressWarnings("unchecked")
+ protected Map<String, Object> getResult(String identifier,
+ String objectType)
{
List<String> ids = Arrays.asList(new String[] { identifier });
- BufferedReader br = null;
try
{
- URL url = getUrl(identifier, objectType);
-
- if (url != null)
- {
- br = getHttpResponse(url, ids);
- }
- return br == null ? null : (JSONObject) (new JSONParser().parse(br));
+ return (Map<String, Object>) getJSON(getUrl(identifier, objectType),
+ ids, -1, MODE_MAP, null);
} catch (IOException | ParseException e)
{
- System.err.println("Error parsing " + identifier + " lookup response "
+ jalview.bin.Console.errPrintln("Error parsing " + identifier + " lookup response "
+ e.getMessage());
return null;
- } finally
- {
- if (br != null)
- {
- try
- {
- br.close();
- } catch (IOException e)
- {
- // ignore
- }
- }
}
}
* @param json
* @return
*/
- GeneLociI parseGeneLoci(JSONObject json)
+ GeneLociI parseGeneLoci(Map<String, Object> json)
{
if (json == null)
{
boolean reverseStrand = "-1".equals(strand);
int toStart = reverseStrand ? end : start;
int toEnd = reverseStrand ? start : end;
- List<int[]> fromRange = Collections.singletonList(new int[] { 1,
- fromEnd });
- List<int[]> toRange = Collections.singletonList(new int[] { toStart,
- toEnd });
- final MapList map = new MapList(fromRange, toRange, 1, 1);
- return new GeneLociI()
- {
-
- @Override
- public String getSpeciesId()
- {
- return species == null ? "" : species;
- }
-
- @Override
- public String getAssemblyId()
- {
- return assembly;
- }
-
- @Override
- public String getChromosomeId()
- {
- return chromosome;
- }
-
- @Override
- public MapList getMap()
- {
- return map;
- }
- };
+ List<int[]> fromRange = Collections
+ .singletonList(new int[]
+ { 1, fromEnd });
+ List<int[]> toRange = Collections
+ .singletonList(new int[]
+ { toStart, toEnd });
+ final Mapping map = new Mapping(
+ new MapList(fromRange, toRange, 1, 1));
+ return new GeneLocus(species == null ? "" : species, assembly,
+ chromosome, map);
} catch (NullPointerException | NumberFormatException e)
{
- Cache.log.error("Error looking up gene loci: " + e.getMessage());
+ Console.error("Error looking up gene loci: " + e.getMessage());
e.printStackTrace();
}
return null;