package jalview.ext.ensembl; import jalview.bin.ApplicationSingletonProvider; import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI; import jalview.datamodel.AlignmentI; import jalview.datamodel.DBRefSource; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.json.simple.parser.ParseException; public class EnsemblInfo extends EnsemblRestClient implements ApplicationSingletonI { /** * On first request only, populate the lookup map by fetching the list of * divisions known to EnsemblGenomes * */ private static EnsemblInfo getInstance() { return (EnsemblInfo) ApplicationSingletonProvider.getInstance(EnsemblInfo.class); } private EnsemblInfo() { // use getInstance() /* * for convenience, pre-fill ensembl.org as the domain for "ENSEMBL" */ divisions.put(DBRefSource.ENSEMBL.toUpperCase(), ensemblDomain); try { @SuppressWarnings("unchecked") Iterator rvals = (Iterator) getJSON( getDivisionsUrl(ensemblGenomesDomain), null, -1, MODE_ITERATOR, null); if (rvals == null) { return; } while (rvals.hasNext()) { String division = rvals.next().toString(); divisions.put(division.toUpperCase(), ensemblGenomesDomain); } } catch (IOException | ParseException | NumberFormatException e) { // ignore } } /* * cached results of REST /info/divisions service, currently *
   * { 
   *  { "ENSEMBLFUNGI", "http://rest.ensemblgenomes.org"},
   *    "ENSEMBLBACTERIA", "http://rest.ensemblgenomes.org"},
   *    "ENSEMBLPROTISTS", "http://rest.ensemblgenomes.org"},
   *    "ENSEMBLMETAZOA", "http://rest.ensemblgenomes.org"},
   *    "ENSEMBLPLANTS",  "http://rest.ensemblgenomes.org"},
   *    "ENSEMBL", "http://rest.ensembl.org" }
   *  }
   * 
* The values for EnsemblGenomes are retrieved by a REST call, that for * Ensembl is added programmatically for convenience of lookup */ private Map divisions = new HashMap<>(); @Override public String getDbName() { return "ENSEMBL"; } @Override public AlignmentI getSequenceRecords(String queries) throws Exception { return null; } @Override protected URL getUrl(List ids) throws MalformedURLException { return null; } @Override protected boolean useGetRequest() { return true; } /** * Answers the domain (http://rest.ensembl.org or * http://rest.ensemblgenomes.org) for the given division, or null if not * recognised by Ensembl. * * @param division * @return */ public static String getDomain(String division) { return getInstance().divisions.get(division.toUpperCase()); } /** * Constructs the URL for the EnsemblGenomes /info/divisions REST service * @param domain TODO * * @return * @throws MalformedURLException */ URL getDivisionsUrl(String domain) throws MalformedURLException { return new URL(domain + "/info/divisions?content-type=application/json"); } /** * Returns the set of 'divisions' recognised by Ensembl or EnsemblGenomes * * @return */ public static Set getDivisions() { return getInstance().divisions.keySet(); } }