80e6b615e1f3d8a082e888207f275f177208b77c
[jalview.git] / forester / java / src / org / forester / ws / uniprot / UniProtWsTools.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.ws.uniprot;
27
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.io.UnsupportedEncodingException;
32 import java.net.URL;
33 import java.net.URLConnection;
34 import java.net.URLEncoder;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39
40 import org.forester.util.ForesterUtil;
41
42 public final class UniProtWsTools {
43
44     private static final boolean ALLOW_TAXONOMY_CODE_HACKS = true; //TODO turn off for final realease!
45
46     public enum Db {
47         UNKNOWN, UNIPROT;
48     }
49     public final static String   BASE_URL           = "http://www.uniprot.org/";
50     public final static String   BASE_EMBL_DB_URL   = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/embl/";
51     private final static String  URL_ENC            = "UTF-8";
52     // uniprot/expasy accession number format (6 chars):
53     // letter digit letter-or-digit letter-or-digit letter-or-digit digit
54     // ?: => no back-reference
55     // \A => begin of String
56     // \Z => end of String
57     private final static Pattern UNIPROT_AC_PATTERN = Pattern
58                                                             .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]\\d[A-Z0-9]{3}\\d)(?:[^a-zA-Z0-9]|\\Z)" );
59     private final static boolean DEBUG              = false;
60
61     private static String encode( final String str ) throws UnsupportedEncodingException {
62         return URLEncoder.encode( str.trim(), URL_ENC );
63     }
64
65     /**
66      * Returns null if no match.
67      * 
68      * @param query
69      * @param db 
70      * @return
71      */
72     static public String parseUniProtAccessor( final String query ) {
73         final Matcher m = UNIPROT_AC_PATTERN.matcher( query );
74         if ( m.lookingAt() ) {
75             return m.group( 1 );
76         }
77         else {
78             return null;
79         }
80     }
81
82     public static List<UniProtTaxonomy> getTaxonomiesFromCommonName( final String cn, final int max_taxonomies_return )
83             throws IOException {
84         final List<String> result = getTaxonomyStringFromCommonName( cn, max_taxonomies_return );
85         if ( result.size() > 0 ) {
86             return parseUniProtTaxonomy( result );
87         }
88         return null;
89     }
90
91     public static List<UniProtTaxonomy> getTaxonomiesFromCommonNameStrict( final String cn,
92                                                                            final int max_taxonomies_return )
93             throws IOException {
94         final List<UniProtTaxonomy> taxonomies = getTaxonomiesFromCommonName( cn, max_taxonomies_return );
95         if ( ( taxonomies != null ) && ( taxonomies.size() > 0 ) ) {
96             final List<UniProtTaxonomy> filtered_taxonomies = new ArrayList<UniProtTaxonomy>();
97             for( final UniProtTaxonomy taxonomy : taxonomies ) {
98                 if ( taxonomy.getCommonName().equalsIgnoreCase( cn ) ) {
99                     filtered_taxonomies.add( taxonomy );
100                 }
101             }
102             return filtered_taxonomies;
103         }
104         return null;
105     }
106
107     public static List<UniProtTaxonomy> getTaxonomiesFromId( final String id, final int max_taxonomies_return )
108             throws IOException {
109         final List<String> result = getTaxonomyStringFromId( id, max_taxonomies_return );
110         if ( result.size() > 0 ) {
111             return parseUniProtTaxonomy( result );
112         }
113         return null;
114     }
115
116     public static List<UniProtTaxonomy> getTaxonomiesFromScientificName( final String sn,
117                                                                          final int max_taxonomies_return )
118             throws IOException {
119         // Hack!  Craniata? .. 
120         if ( sn.equals( "Drosophila" ) ) {
121             return uniProtTaxonomyToList( UniProtTaxonomy.DROSOPHILA_GENUS );
122         }
123         else if ( sn.equals( "Xenopus" ) ) {
124             return uniProtTaxonomyToList( UniProtTaxonomy.XENOPUS_GENUS );
125         }
126         // else if ( sn.equals( "Nucleariidae and Fonticula group" ) ) {
127         //     return hack( UniProtTaxonomy.NUCLEARIIDAE_AND_FONTICULA );
128         // }
129         final List<String> result = getTaxonomyStringFromScientificName( sn, max_taxonomies_return );
130         if ( result.size() > 0 ) {
131             return parseUniProtTaxonomy( result );
132         }
133         return null;
134     }
135
136     /**
137      * Does not return "sub-types".
138      * For example, for "Mus musculus" only returns "Mus musculus"
139      * and not "Mus musculus", "Mus musculus bactrianus", ...
140      * 
141      */
142     public static List<UniProtTaxonomy> getTaxonomiesFromScientificNameStrict( final String sn,
143                                                                                final int max_taxonomies_return )
144             throws IOException {
145         final List<UniProtTaxonomy> taxonomies = getTaxonomiesFromScientificName( sn, max_taxonomies_return );
146         if ( ( taxonomies != null ) && ( taxonomies.size() > 0 ) ) {
147             final List<UniProtTaxonomy> filtered_taxonomies = new ArrayList<UniProtTaxonomy>();
148             for( final UniProtTaxonomy taxonomy : taxonomies ) {
149                 if ( taxonomy.getScientificName().equalsIgnoreCase( sn ) ) {
150                     filtered_taxonomies.add( taxonomy );
151                 }
152             }
153             return filtered_taxonomies;
154         }
155         return null;
156     }
157
158     public static List<UniProtTaxonomy> getTaxonomiesFromTaxonomyCode( final String code,
159                                                                        final int max_taxonomies_return )
160             throws IOException {
161         final String my_code = new String( code );
162         if ( ALLOW_TAXONOMY_CODE_HACKS ) {
163             final List<UniProtTaxonomy> l = resolveFakeTaxonomyCodes( max_taxonomies_return, my_code );
164             if ( l != null ) {
165                 return l;
166             }
167         }
168         final List<String> result = getTaxonomyStringFromTaxonomyCode( my_code, max_taxonomies_return );
169         if ( result.size() > 0 ) {
170             return parseUniProtTaxonomy( result );
171         }
172         return null;
173     }
174
175     private static List<UniProtTaxonomy> resolveFakeTaxonomyCodes( final int max_taxonomies_return, final String code )
176             throws IOException {
177         if ( code.equals( "CAP" ) ) {
178             return getTaxonomiesFromId( "283909", max_taxonomies_return );
179         }
180         else if ( code.equals( "FUGRU" ) ) {
181             return getTaxonomiesFromId( "31033", max_taxonomies_return );
182         }
183         else if ( code.equals( "GIALA" ) ) {
184             return getTaxonomiesFromId( "5741", max_taxonomies_return );
185         }
186         else if ( code.equals( "TRIVE" ) ) {
187             return getTaxonomiesFromId( "413071", max_taxonomies_return );
188         }
189         else if ( code.equals( "CAPOWC" ) ) {
190             return getTaxonomiesFromId( "192875", max_taxonomies_return );
191         }
192         else if ( code.equals( "SPHARC" ) ) {
193             return getTaxonomiesFromId( "667725", max_taxonomies_return );
194         }
195         else if ( code.equals( "THETRA" ) ) {
196             return getTaxonomiesFromId( "529818", max_taxonomies_return );
197         }
198         else if ( code.equals( "CHLVUL" ) ) {
199             return getTaxonomiesFromId( "574566", max_taxonomies_return );
200         }
201         else if ( code.equals( "CITCLE" ) ) {
202             return getTaxonomiesFromId( "85681", max_taxonomies_return );
203         }
204         else if ( code.equals( "MYCPOP" ) ) {
205             return getTaxonomiesFromId( "85929", max_taxonomies_return );
206         }
207         else if ( code.equals( "AGABB" ) ) {
208             return getTaxonomiesFromId( "597362", max_taxonomies_return );
209         }
210         else if ( code.equals( "BAUCOM" ) ) {
211             return getTaxonomiesFromId( "430998", max_taxonomies_return );
212         }
213         else if ( code.equals( "DICSQU" ) ) {
214             return getTaxonomiesFromId( "114155", max_taxonomies_return );
215         }
216         else if ( code.equals( "FOMPIN" ) ) {
217             return getTaxonomiesFromId( "40483", max_taxonomies_return );
218         }
219         else if ( code.equals( "HYDMA" ) ) {
220             return getTaxonomiesFromId( "6085", max_taxonomies_return );
221         }
222         else if ( code.equals( "MYCFI" ) ) {
223             return getTaxonomiesFromId( "83344", max_taxonomies_return );
224         }
225         else if ( code.equals( "OIDMAI" ) ) {
226             return getTaxonomiesFromId( "78148", max_taxonomies_return );
227         }
228         else if ( code.equals( "OSTRC" ) ) {
229             return getTaxonomiesFromId( "385169", max_taxonomies_return );
230         }
231         else if ( code.equals( "POSPL" ) ) {
232             return getTaxonomiesFromId( "104341", max_taxonomies_return );
233         }
234         else if ( code.equals( "SAICOM" ) ) {
235             return getTaxonomiesFromId( "5606", max_taxonomies_return );
236         }
237         else if ( code.equals( "SERLA" ) ) {
238             return getTaxonomiesFromId( "85982", max_taxonomies_return );
239         }
240         else if ( code.equals( "SPORO" ) ) {
241             return getTaxonomiesFromId( "40563", max_taxonomies_return );
242         }
243         else if ( code.equals( "ACRALC" ) ) {
244             return getTaxonomiesFromId( "398408", max_taxonomies_return );
245         }
246         else if ( code.equals( "THITER" ) ) {
247             return getTaxonomiesFromId( "35720", max_taxonomies_return );
248         }
249         else if ( code.equals( "MYCTHE" ) ) {
250             return getTaxonomiesFromId( "78579", max_taxonomies_return );
251         }
252         else if ( code.equals( "CONPUT" ) ) {
253             return getTaxonomiesFromId( "80637", max_taxonomies_return );
254         }
255         else if ( code.equals( "WOLCOC" ) ) {
256             return getTaxonomiesFromId( "81056", max_taxonomies_return );
257         }
258         else {
259             return null;
260         }
261     }
262
263     private static List<String> getTaxonomyStringFromCommonName( final String cn, final int max_lines_to_return )
264             throws IOException {
265         return queryUniprot( "taxonomy/?query=common%3a%22" + encode( cn ) + "%22&format=tab", max_lines_to_return );
266     }
267
268     private static List<String> getTaxonomyStringFromId( final String id, final int max_lines_to_return )
269             throws IOException {
270         return queryUniprot( "taxonomy/?query=id%3a%22" + encode( id ) + "%22&format=tab", max_lines_to_return );
271     }
272
273     private static List<String> getTaxonomyStringFromScientificName( final String sn, final int max_lines_to_return )
274             throws IOException {
275         return queryUniprot( "taxonomy/?query=scientific%3a%22" + encode( sn ) + "%22&format=tab", max_lines_to_return );
276     }
277
278     private static List<String> getTaxonomyStringFromTaxonomyCode( final String code, final int max_lines_to_return )
279             throws IOException {
280         return queryUniprot( "taxonomy/?query=mnemonic%3a%22" + encode( code ) + "%22&format=tab", max_lines_to_return );
281     }
282
283     private static List<UniProtTaxonomy> uniProtTaxonomyToList( final UniProtTaxonomy tax ) {
284         final List<UniProtTaxonomy> l = new ArrayList<UniProtTaxonomy>();
285         l.add( tax );
286         return l;
287     }
288
289     private static List<UniProtTaxonomy> parseUniProtTaxonomy( final List<String> result ) throws IOException {
290         final List<UniProtTaxonomy> taxonomies = new ArrayList<UniProtTaxonomy>();
291         for( final String line : result ) {
292             if ( ForesterUtil.isEmpty( line ) ) {
293                 // Ignore empty lines.
294             }
295             else if ( line.startsWith( "Taxon" ) ) {
296                 final String[] items = line.split( "\t" );
297                 if ( !( items[ 1 ].equalsIgnoreCase( "Mnemonic" ) && items[ 2 ].equalsIgnoreCase( "Scientific name" )
298                         && items[ 3 ].equalsIgnoreCase( "Common name" ) && items[ 4 ].equalsIgnoreCase( "Synonym" )
299                         && items[ 5 ].equalsIgnoreCase( "Other Names" ) && items[ 6 ].equalsIgnoreCase( "Reviewed" )
300                         && items[ 7 ].equalsIgnoreCase( "Rank" ) && items[ 8 ].equalsIgnoreCase( "Lineage" ) ) ) {
301                     throw new IOException( "Unreconized UniProt Taxonomy format: " + line );
302                 }
303             }
304             else {
305                 if ( line.split( "\t" ).length > 4 ) {
306                     taxonomies.add( new UniProtTaxonomy( line ) );
307                 }
308             }
309         }
310         return taxonomies;
311     }
312
313     public static List<String> queryEmblDb( final String query, final int max_lines_to_return ) throws IOException {
314         return queryDb( query, max_lines_to_return, BASE_EMBL_DB_URL );
315     }
316
317     public static List<String> queryUniprot( final String query, final int max_lines_to_return ) throws IOException {
318         return queryDb( query, max_lines_to_return, BASE_URL );
319     }
320
321     public static List<String> queryDb( final String query, int max_lines_to_return, final String base_url )
322             throws IOException {
323         if ( ForesterUtil.isEmpty( query ) ) {
324             throw new IllegalArgumentException( "illegal attempt to use empty query " );
325         }
326         if ( max_lines_to_return < 1 ) {
327             max_lines_to_return = 1;
328         }
329         final URL url = new URL( base_url + query );
330         if ( DEBUG ) {
331             System.out.println( "url: " + url.toString() );
332         }
333         final URLConnection urlc = url.openConnection();
334         final BufferedReader in = new BufferedReader( new InputStreamReader( urlc.getInputStream() ) );
335         String line;
336         final List<String> result = new ArrayList<String>();
337         while ( ( line = in.readLine() ) != null ) {
338             if ( DEBUG ) {
339                 System.out.println( line );
340             }
341             result.add( line );
342             if ( result.size() > max_lines_to_return ) {
343                 break;
344             }
345         }
346         in.close();
347         return result;
348     }
349
350     public static SequenceDatabaseEntry obtainUniProtEntry( final String query, final int max_lines_to_return )
351             throws IOException {
352         final List<String> lines = queryUniprot( "uniprot/" + query + ".txt", max_lines_to_return );
353         return UniProtEntry.createInstanceFromPlainText( lines );
354     }
355
356     public static SequenceDatabaseEntry obtainEmblEntry( final String query, final int max_lines_to_return )
357             throws IOException {
358         final List<String> lines = queryEmblDb( query, max_lines_to_return );
359         return EbiDbEntry.createInstanceFromPlainText( lines );
360     }
361 }