2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.ws.uniprot;
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.io.UnsupportedEncodingException;
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;
40 import org.forester.util.ForesterUtil;
42 public final class UniProtWsTools {
44 private static final boolean ALLOW_TAXONOMY_CODE_HACKS = true; //TODO turn off for final realease!
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;
61 private static String encode( final String str ) throws UnsupportedEncodingException {
62 return URLEncoder.encode( str.trim(), URL_ENC );
66 * Returns null if no match.
72 static public String parseUniProtAccessor( final String query ) {
73 final Matcher m = UNIPROT_AC_PATTERN.matcher( query );
74 if ( m.lookingAt() ) {
82 public static List<UniProtTaxonomy> getTaxonomiesFromCommonName( final String cn, final int max_taxonomies_return )
84 final List<String> result = getTaxonomyStringFromCommonName( cn, max_taxonomies_return );
85 if ( result.size() > 0 ) {
86 return parseUniProtTaxonomy( result );
91 public static List<UniProtTaxonomy> getTaxonomiesFromCommonNameStrict( final String cn,
92 final int max_taxonomies_return )
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 );
102 return filtered_taxonomies;
107 public static List<UniProtTaxonomy> getTaxonomiesFromId( final String id, final int max_taxonomies_return )
109 final List<String> result = getTaxonomyStringFromId( id, max_taxonomies_return );
110 if ( result.size() > 0 ) {
111 return parseUniProtTaxonomy( result );
116 public static List<UniProtTaxonomy> getTaxonomiesFromScientificName( final String sn,
117 final int max_taxonomies_return )
119 // Hack! Craniata? ..
120 if ( sn.equals( "Drosophila" ) ) {
121 return uniProtTaxonomyToList( UniProtTaxonomy.DROSOPHILA_GENUS );
123 else if ( sn.equals( "Xenopus" ) ) {
124 return uniProtTaxonomyToList( UniProtTaxonomy.XENOPUS_GENUS );
126 // else if ( sn.equals( "Nucleariidae and Fonticula group" ) ) {
127 // return hack( UniProtTaxonomy.NUCLEARIIDAE_AND_FONTICULA );
129 final List<String> result = getTaxonomyStringFromScientificName( sn, max_taxonomies_return );
130 if ( result.size() > 0 ) {
131 return parseUniProtTaxonomy( result );
137 * Does not return "sub-types".
138 * For example, for "Mus musculus" only returns "Mus musculus"
139 * and not "Mus musculus", "Mus musculus bactrianus", ...
142 public static List<UniProtTaxonomy> getTaxonomiesFromScientificNameStrict( final String sn,
143 final int max_taxonomies_return )
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 );
153 return filtered_taxonomies;
158 public static List<UniProtTaxonomy> getTaxonomiesFromTaxonomyCode( final String code,
159 final int max_taxonomies_return )
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 );
168 final List<String> result = getTaxonomyStringFromTaxonomyCode( my_code, max_taxonomies_return );
169 if ( result.size() > 0 ) {
170 return parseUniProtTaxonomy( result );
175 private static List<UniProtTaxonomy> resolveFakeTaxonomyCodes( final int max_taxonomies_return, final String code )
177 if ( code.equals( "CAP" ) ) {
178 return getTaxonomiesFromId( "283909", max_taxonomies_return );
180 else if ( code.equals( "FUGRU" ) ) {
181 return getTaxonomiesFromId( "31033", max_taxonomies_return );
183 else if ( code.equals( "GIALA" ) ) {
184 return getTaxonomiesFromId( "5741", max_taxonomies_return );
186 else if ( code.equals( "TRIVE" ) ) {
187 return getTaxonomiesFromId( "413071", max_taxonomies_return );
189 else if ( code.equals( "CAPOWC" ) ) {
190 return getTaxonomiesFromId( "192875", max_taxonomies_return );
192 else if ( code.equals( "SPHARC" ) ) {
193 return getTaxonomiesFromId( "667725", max_taxonomies_return );
195 else if ( code.equals( "THETRA" ) ) {
196 return getTaxonomiesFromId( "529818", max_taxonomies_return );
198 else if ( code.equals( "CHLVUL" ) ) {
199 return getTaxonomiesFromId( "574566", max_taxonomies_return );
201 else if ( code.equals( "CITCLE" ) ) {
202 return getTaxonomiesFromId( "85681", max_taxonomies_return );
204 else if ( code.equals( "MYCPOP" ) ) {
205 return getTaxonomiesFromId( "85929", max_taxonomies_return );
207 else if ( code.equals( "AGABB" ) ) {
208 return getTaxonomiesFromId( "597362", max_taxonomies_return );
210 else if ( code.equals( "BAUCOM" ) ) {
211 return getTaxonomiesFromId( "430998", max_taxonomies_return );
213 else if ( code.equals( "DICSQU" ) ) {
214 return getTaxonomiesFromId( "114155", max_taxonomies_return );
216 else if ( code.equals( "FOMPIN" ) ) {
217 return getTaxonomiesFromId( "40483", max_taxonomies_return );
219 else if ( code.equals( "HYDMA" ) ) {
220 return getTaxonomiesFromId( "6085", max_taxonomies_return );
222 else if ( code.equals( "MYCFI" ) ) {
223 return getTaxonomiesFromId( "83344", max_taxonomies_return );
225 else if ( code.equals( "OIDMAI" ) ) {
226 return getTaxonomiesFromId( "78148", max_taxonomies_return );
228 else if ( code.equals( "OSTRC" ) ) {
229 return getTaxonomiesFromId( "385169", max_taxonomies_return );
231 else if ( code.equals( "POSPL" ) ) {
232 return getTaxonomiesFromId( "104341", max_taxonomies_return );
234 else if ( code.equals( "SAICOM" ) ) {
235 return getTaxonomiesFromId( "5606", max_taxonomies_return );
237 else if ( code.equals( "SERLA" ) ) {
238 return getTaxonomiesFromId( "85982", max_taxonomies_return );
240 else if ( code.equals( "SPORO" ) ) {
241 return getTaxonomiesFromId( "40563", max_taxonomies_return );
243 else if ( code.equals( "ACRALC" ) ) {
244 return getTaxonomiesFromId( "398408", max_taxonomies_return );
246 else if ( code.equals( "THITER" ) ) {
247 return getTaxonomiesFromId( "35720", max_taxonomies_return );
249 else if ( code.equals( "MYCTHE" ) ) {
250 return getTaxonomiesFromId( "78579", max_taxonomies_return );
252 else if ( code.equals( "CONPUT" ) ) {
253 return getTaxonomiesFromId( "80637", max_taxonomies_return );
255 else if ( code.equals( "WOLCOC" ) ) {
256 return getTaxonomiesFromId( "81056", max_taxonomies_return );
263 private static List<String> getTaxonomyStringFromCommonName( final String cn, final int max_lines_to_return )
265 return queryUniprot( "taxonomy/?query=common%3a%22" + encode( cn ) + "%22&format=tab", max_lines_to_return );
268 private static List<String> getTaxonomyStringFromId( final String id, final int max_lines_to_return )
270 return queryUniprot( "taxonomy/?query=id%3a%22" + encode( id ) + "%22&format=tab", max_lines_to_return );
273 private static List<String> getTaxonomyStringFromScientificName( final String sn, final int max_lines_to_return )
275 return queryUniprot( "taxonomy/?query=scientific%3a%22" + encode( sn ) + "%22&format=tab", max_lines_to_return );
278 private static List<String> getTaxonomyStringFromTaxonomyCode( final String code, final int max_lines_to_return )
280 return queryUniprot( "taxonomy/?query=mnemonic%3a%22" + encode( code ) + "%22&format=tab", max_lines_to_return );
283 private static List<UniProtTaxonomy> uniProtTaxonomyToList( final UniProtTaxonomy tax ) {
284 final List<UniProtTaxonomy> l = new ArrayList<UniProtTaxonomy>();
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.
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 );
305 if ( line.split( "\t" ).length > 4 ) {
306 taxonomies.add( new UniProtTaxonomy( line ) );
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 );
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 );
321 public static List<String> queryDb( final String query, int max_lines_to_return, final String base_url )
323 if ( ForesterUtil.isEmpty( query ) ) {
324 throw new IllegalArgumentException( "illegal attempt to use empty query " );
326 if ( max_lines_to_return < 1 ) {
327 max_lines_to_return = 1;
329 final URL url = new URL( base_url + query );
331 System.out.println( "url: " + url.toString() );
333 final URLConnection urlc = url.openConnection();
334 final BufferedReader in = new BufferedReader( new InputStreamReader( urlc.getInputStream() ) );
336 final List<String> result = new ArrayList<String>();
337 while ( ( line = in.readLine() ) != null ) {
339 System.out.println( line );
342 if ( result.size() > max_lines_to_return ) {
350 public static SequenceDatabaseEntry obtainUniProtEntry( final String query, final int max_lines_to_return )
352 final List<String> lines = queryUniprot( "uniprot/" + query + ".txt", max_lines_to_return );
353 return UniProtEntry.createInstanceFromPlainText( lines );
356 public static SequenceDatabaseEntry obtainEmblEntry( final String query, final int max_lines_to_return )
358 final List<String> lines = queryEmblDb( query, max_lines_to_return );
359 return EbiDbEntry.createInstanceFromPlainText( lines );