f731e9412129b31571d8afc5a72b199d93ee5c44
[jalview.git] / src / jalview / ext / ensembl / EnsemblSymbol.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ext.ensembl;
22
23 import java.io.IOException;
24 import java.net.MalformedURLException;
25 import java.net.URL;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.json.simple.parser.ParseException;
32
33 /**
34  * A client for the Ensembl xrefs/symbol REST service;
35  * 
36  * @see http://rest.ensembl.org/documentation/info/xref_external
37  * @author gmcarstairs
38  *
39  */
40 public class EnsemblSymbol extends EnsemblXref
41 {
42   private static final String GENE = "gene";
43   private static final String TYPE = "type";
44   /**
45    * Constructor given the target domain to fetch data from
46    * 
47    * @param domain
48    * @param dbName
49    * @param dbVersion
50    */
51   public EnsemblSymbol(String domain, String dbName, String dbVersion)
52   {
53     super(domain, dbName, dbVersion);
54   }
55
56 //  /**
57 //   * Returns the first "id" value in gene identifier format from the JSON
58 //   * response, or null if none found
59 //   * 
60 //   * @param br
61 //   * @return
62 //   * @throws IOException
63 //   */
64 //  @SuppressWarnings("unchecked")
65 //protected String parseSymbolResponse(BufferedReader br) throws IOException
66 //  {
67 //  }
68
69   /**
70    * Constructs the URL for the REST symbol endpoint
71    * 
72    * @param id
73    *          the accession id (Ensembl or external)
74    * @param species
75    *          a species name recognisable by Ensembl
76    * @param type
77    *          an optional type to filter the response (gene, transcript,
78    *          translation)
79    * @return
80    */
81   protected URL getUrl(String id, Species species, String... type)
82   {
83     StringBuilder sb = new StringBuilder();
84     sb.append(getDomain()).append("/xrefs/symbol/")
85             .append(species.toString()).append("/").append(id)
86             .append(CONTENT_TYPE_JSON);
87     for (String t : type)
88     {
89       sb.append("&object_type=").append(t);
90     }
91     try
92     {
93       String url = sb.toString();
94       return new URL(url);
95     } catch (MalformedURLException e)
96     {
97       return null;
98     }
99   }
100
101   /**
102    * Calls the Ensembl xrefs REST 'symbol' endpoint and retrieves any gene ids
103    * for the given identifier, for any known model organisms
104    * 
105    * @param identifier
106    * @return
107    */
108   @SuppressWarnings("unchecked")
109   public List<String> getGeneIds(String identifier)
110   {
111     List<String> result = new ArrayList<String>();
112     List<String> ids = new ArrayList<String>();
113     ids.add(identifier);
114
115     String[] queries = identifier.split(getAccessionSeparator());
116     try
117     {
118       for (String query : queries)
119       {
120         for (Species taxon : Species.getModelOrganisms())
121         {
122           String geneId = null;///parseSymbolResponse(br);          
123           try
124           {
125                         Iterator<Object> rvals = (Iterator<Object>) getJSON(getUrl(query, taxon, GENE), ids, -1, MODE_ITERATOR, null);
126             if (rvals == null)
127                 continue;
128             while (rvals.hasNext())
129             {
130               Map<String, Object> val = (Map<String, Object>) rvals.next();
131               String id = val.get(JSON_ID).toString();
132               String type = val.get(TYPE).toString();
133               if (id != null && GENE.equals(type))
134               {
135                 geneId = id;
136                 break;
137               }
138             }
139           } catch (ParseException e)
140           {
141             // ignore
142           }
143           
144           if (geneId != null && !result.contains(geneId))
145           {
146             result.add(geneId);
147           }
148         }
149       }
150     } catch (IOException e)
151     {
152       // ignore
153     }
154     return result;
155   }
156
157 }