f5f83e49343878a0f73bd18634cb512a9b4401b4
[jalview.git] / forester / java / src / org / forester / ws / seqdb / SequenceDbWsTools.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: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.ws.seqdb;
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.SortedSet;
38 import java.util.TreeSet;
39
40 import org.forester.go.GoTerm;
41 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
42 import org.forester.phylogeny.Phylogeny;
43 import org.forester.phylogeny.PhylogenyNode;
44 import org.forester.phylogeny.data.Accession;
45 import org.forester.phylogeny.data.Annotation;
46 import org.forester.phylogeny.data.Identifier;
47 import org.forester.phylogeny.data.Sequence;
48 import org.forester.phylogeny.data.Taxonomy;
49 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
50 import org.forester.util.ForesterUtil;
51 import org.forester.util.SequenceIdParser;
52
53 public final class SequenceDbWsTools {
54
55     public final static String   BASE_UNIPROT_URL  = "http://www.uniprot.org/";
56     public final static String   BASE_EMBL_DB_URL  = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/";
57     public final static String   EMBL_DBS_EMBL     = "embl";
58     public final static String   EMBL_DBS_REFSEQ_P = "refseqp";
59     public final static String   EMBL_DBS_REFSEQ_N = "refseqn";
60     private final static String  URL_ENC           = "UTF-8";
61     private final static boolean DEBUG             = false;
62
63     private static List<UniProtTaxonomy> getTaxonomiesFromCommonName( final String cn, final int max_taxonomies_return )
64             throws IOException {
65         final List<String> result = getTaxonomyStringFromCommonName( cn, max_taxonomies_return );
66         if ( result.size() > 0 ) {
67             return parseUniProtTaxonomy( result );
68         }
69         return null;
70     }
71
72     public static List<UniProtTaxonomy> getTaxonomiesFromCommonNameStrict( final String cn,
73                                                                            final int max_taxonomies_return )
74             throws IOException {
75         final List<UniProtTaxonomy> taxonomies = getTaxonomiesFromCommonName( cn, max_taxonomies_return );
76         if ( ( taxonomies != null ) && ( taxonomies.size() > 0 ) ) {
77             final List<UniProtTaxonomy> filtered_taxonomies = new ArrayList<UniProtTaxonomy>();
78             for( final UniProtTaxonomy taxonomy : taxonomies ) {
79                 if ( taxonomy.getCommonName().equalsIgnoreCase( cn ) ) {
80                     filtered_taxonomies.add( taxonomy );
81                 }
82             }
83             return filtered_taxonomies;
84         }
85         return null;
86     }
87
88     public static List<UniProtTaxonomy> getTaxonomiesFromId( final String id, final int max_taxonomies_return )
89             throws IOException {
90         final List<String> result = getTaxonomyStringFromId( id, max_taxonomies_return );
91         if ( result.size() > 0 ) {
92             return parseUniProtTaxonomy( result );
93         }
94         return null;
95     }
96
97     private static List<UniProtTaxonomy> getTaxonomiesFromScientificName( final String sn,
98                                                                           final int max_taxonomies_return )
99             throws IOException {
100         final List<String> result = getTaxonomyStringFromScientificName( sn, max_taxonomies_return );
101         if ( result.size() > 0 ) {
102             return parseUniProtTaxonomy( result );
103         }
104         return null;
105     }
106
107     /**
108      * Does not return "sub-types".
109      * For example, for "Mus musculus" only returns "Mus musculus"
110      * and not "Mus musculus", "Mus musculus bactrianus", ...
111      * 
112      */
113     public static List<UniProtTaxonomy> getTaxonomiesFromScientificNameStrict( final String sn,
114                                                                                final int max_taxonomies_return )
115             throws IOException {
116         final List<UniProtTaxonomy> taxonomies = getTaxonomiesFromScientificName( sn, max_taxonomies_return );
117         if ( ( taxonomies != null ) && ( taxonomies.size() > 0 ) ) {
118             final List<UniProtTaxonomy> filtered_taxonomies = new ArrayList<UniProtTaxonomy>();
119             for( final UniProtTaxonomy taxonomy : taxonomies ) {
120                 if ( taxonomy.getScientificName().equalsIgnoreCase( sn ) ) {
121                     filtered_taxonomies.add( taxonomy );
122                 }
123             }
124             return filtered_taxonomies;
125         }
126         return null;
127     }
128
129     public static List<UniProtTaxonomy> getTaxonomiesFromTaxonomyCode( final String code,
130                                                                        final int max_taxonomies_return )
131             throws IOException {
132         final String my_code = new String( code );
133         final List<String> result = getTaxonomyStringFromTaxonomyCode( my_code, max_taxonomies_return );
134         if ( result.size() > 0 ) {
135             return parseUniProtTaxonomy( result );
136         }
137         return null;
138     }
139
140     public static SequenceDatabaseEntry obtainEmblEntry( final Identifier id, final int max_lines_to_return )
141             throws IOException {
142         final List<String> lines = queryEmblDb( id, max_lines_to_return );
143         return EbiDbEntry.createInstanceFromPlainText( lines );
144     }
145
146     public static SequenceDatabaseEntry obtainRefSeqEntryFromEmbl( final Identifier id, final int max_lines_to_return )
147             throws IOException {
148         final List<String> lines = queryEmblDb( id, max_lines_to_return );
149         return EbiDbEntry.createInstanceFromPlainTextForRefSeq( lines );
150     }
151
152     public static SortedSet<String> obtainSeqInformation( final Phylogeny phy,
153                                                           final boolean ext_nodes_only,
154                                                           final boolean allow_to_set_taxonomic_data,
155                                                           final int lines_to_return ) throws IOException {
156         final SortedSet<String> not_found = new TreeSet<String>();
157         for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
158             final PhylogenyNode node = iter.next();
159             if ( ext_nodes_only && node.isInternal() ) {
160                 continue;
161             }
162             String query = null;
163             Identifier id = null;
164             Db db = Db.NONE;
165             if ( node.getNodeData().isHasSequence()
166                     && ( node.getNodeData().getSequence().getAccession() != null )
167                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() )
168                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() )
169                     && ( node.getNodeData().getSequence().getAccession().getValue().toLowerCase()
170                             .startsWith( "uniprot" )
171                             || node.getNodeData().getSequence().getAccession().getValue().toLowerCase()
172                                     .startsWith( "swissprot" )
173                             || node.getNodeData().getSequence().getAccession().getValue().toLowerCase()
174                                     .startsWith( "trembl" )
175                             || node.getNodeData().getSequence().getAccession().getValue().toLowerCase()
176                                     .startsWith( "sp" ) || node.getNodeData().getSequence().getAccession().getValue()
177                             .toLowerCase().startsWith( "uniprotkb" ) ) ) {
178                 query = node.getNodeData().getSequence().getAccession().getValue();
179                 db = Db.UNIPROT;
180             }
181             else if ( node.getNodeData().isHasSequence()
182                     && ( node.getNodeData().getSequence().getAccession() != null )
183                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() )
184                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() )
185                     && ( node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "embl" ) || node
186                             .getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "ebi" ) ) ) {
187                 query = node.getNodeData().getSequence().getAccession().getValue();
188                 db = Db.EMBL;
189             }
190             else if ( node.getNodeData().isHasSequence()
191                     && ( node.getNodeData().getSequence().getAccession() != null )
192                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() )
193                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() )
194                     && ( node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "ncbi" ) || node
195                             .getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "genbank" ) ) ) {
196                 query = node.getNodeData().getSequence().getAccession().getValue();
197                 // db = Db.NCBI;
198             }
199             else if ( node.getNodeData().isHasSequence() && ( node.getNodeData().getSequence().getAccession() != null )
200                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() )
201                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() )
202                     && node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "refseq" ) ) {
203                 query = node.getNodeData().getSequence().getAccession().getValue();
204                 db = Db.REFSEQ;
205             }
206             else {
207                 if ( ( query = ForesterUtil.extractUniProtKbProteinSeqIdentifier( node ) ) != null ) {
208                     db = Db.UNIPROT;
209                 }
210                 else if ( node.getNodeData().isHasSequence() ) {
211                     if ( ( id = SequenceIdParser.parse( node.getName() ) ) != null ) {
212                         if ( id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) {
213                             //  db = Db.NCBI;
214                         }
215                         else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) {
216                             db = Db.REFSEQ;
217                         }
218                     }
219                     else if ( ( id = SequenceIdParser.parse( node.getNodeData().getSequence().getName() ) ) != null ) {
220                         if ( id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) {
221                             // = Db.NCBI;
222                         }
223                         else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) {
224                             db = Db.REFSEQ;
225                         }
226                     }
227                     else if ( ( id = SequenceIdParser.parse( node.getNodeData().getSequence().getGeneName() ) ) != null ) {
228                         if ( id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) {
229                             // db = Db.NCBI;
230                         }
231                         else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) {
232                             db = Db.REFSEQ;
233                         }
234                     }
235                     else if ( ( id = SequenceIdParser.parse( node.getNodeData().getSequence().getSymbol() ) ) != null ) {
236                         if ( id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) {
237                             // db = Db.NCBI;
238                         }
239                         else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) {
240                             db = Db.REFSEQ;
241                         }
242                     }
243                 }
244             }
245             if ( db == Db.NONE ) {
246                 not_found.add( node.toString() );
247             }
248             SequenceDatabaseEntry db_entry = null;
249             if ( !ForesterUtil.isEmpty( query ) ) {
250                 if ( db == Db.UNIPROT ) {
251                     if ( DEBUG ) {
252                         System.out.println( "uniprot: " + query );
253                     }
254                     db_entry = obtainUniProtEntry( query, lines_to_return );
255                 }
256                 else if ( db == Db.EMBL ) {
257                     if ( DEBUG ) {
258                         System.out.println( "embl: " + query );
259                     }
260                     db_entry = obtainEmblEntry( new Identifier( query ), lines_to_return );
261                 }
262                 else if ( db == Db.REFSEQ ) {
263                     if ( DEBUG ) {
264                         System.out.println( "refseq: " + query );
265                     }
266                     db_entry = obtainRefSeqEntryFromEmbl( new Identifier( query ), lines_to_return );
267                 }
268                 //   else if ( db == Db.NCBI ) {
269                 //       if ( DEBUG ) {
270                 //           System.out.println( "ncbi: " + query );
271                 //       }
272                 //       db_entry = obtainNcbiEntry( new Identifier( query ), lines_to_return );
273                 //  }
274             }
275             else if ( ( db == Db.REFSEQ ) && ( id != null ) ) {
276                 db_entry = obtainRefSeqEntryFromEmbl( id, lines_to_return );
277             }
278             //else if ( ( db == Db.NCBI ) && ( id != null ) ) {
279             //    db_entry = obtainNcbiEntry( id, lines_to_return );
280             //}
281             if ( ( db_entry != null ) && !db_entry.isEmpty() ) {
282                 final Sequence seq = node.getNodeData().isHasSequence() ? node.getNodeData().getSequence()
283                         : new Sequence();
284                 if ( !ForesterUtil.isEmpty( db_entry.getAccession() ) ) {
285                     String type = null;
286                     if ( db == Db.EMBL ) {
287                         type = "embl";
288                     }
289                     else if ( db == Db.UNIPROT ) {
290                         type = "uniprot";
291                     }
292                     //   else if ( db == Db.NCBI ) {
293                     //       type = "ncbi";
294                     //   }
295                     else if ( db == Db.REFSEQ ) {
296                         type = "refseq";
297                     }
298                     seq.setAccession( new Accession( db_entry.getAccession(), type ) );
299                 }
300                 if ( !ForesterUtil.isEmpty( db_entry.getSequenceName() ) ) {
301                     seq.setName( db_entry.getSequenceName() );
302                 }
303                 if ( !ForesterUtil.isEmpty( db_entry.getGeneName() ) ) {
304                     seq.setGeneName( db_entry.getGeneName() );
305                 }
306                 if ( !ForesterUtil.isEmpty( db_entry.getSequenceSymbol() ) ) {
307                     try {
308                         seq.setSymbol( db_entry.getSequenceSymbol() );
309                     }
310                     catch ( final PhyloXmlDataFormatException e ) {
311                         // Eat this exception.
312                     }
313                 }
314                 if ( ( db_entry.getGoTerms() != null ) && !db_entry.getGoTerms().isEmpty() ) {
315                     for( final GoTerm go : db_entry.getGoTerms() ) {
316                         final Annotation ann = new Annotation( go.getGoId().getId() );
317                         ann.setDesc( go.getName() );
318                         seq.addAnnotation( ann );
319                     }
320                 }
321                 if ( ( db_entry.getCrossReferences() != null ) && !db_entry.getCrossReferences().isEmpty() ) {
322                     for( final Accession x : db_entry.getCrossReferences() ) {
323                         seq.addCrossReference( x );
324                     }
325                 }
326                 final Taxonomy tax = node.getNodeData().isHasTaxonomy() ? node.getNodeData().getTaxonomy()
327                         : new Taxonomy();
328                 if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyScientificName() ) ) {
329                     tax.setScientificName( db_entry.getTaxonomyScientificName() );
330                 }
331                 if ( allow_to_set_taxonomic_data && !ForesterUtil.isEmpty( db_entry.getTaxonomyIdentifier() ) ) {
332                     tax.setIdentifier( new Identifier( db_entry.getTaxonomyIdentifier(), "uniprot" ) );
333                 }
334                 node.getNodeData().setTaxonomy( tax );
335                 node.getNodeData().setSequence( seq );
336             }
337             else if ( db != Db.NONE ) {
338                 not_found.add( node.getName() );
339             }
340             try {
341                 Thread.sleep( 10 );// Sleep for 10 ms
342             }
343             catch ( final InterruptedException ie ) {
344             }
345         }
346         return not_found;
347     }
348
349     public static SequenceDatabaseEntry obtainUniProtEntry( final String query, final int max_lines_to_return )
350             throws IOException {
351         final List<String> lines = queryUniprot( "uniprot/" + query + ".txt", max_lines_to_return );
352         return UniProtEntry.createInstanceFromPlainText( lines );
353     }
354
355     public static List<String> queryDb( final String query, int max_lines_to_return, final String base_url )
356             throws IOException {
357         if ( ForesterUtil.isEmpty( query ) ) {
358             throw new IllegalArgumentException( "illegal attempt to use empty query " );
359         }
360         if ( max_lines_to_return < 1 ) {
361             max_lines_to_return = 1;
362         }
363         final URL url = new URL( base_url + query );
364         if ( DEBUG ) {
365             System.out.println( "url: " + url.toString() );
366         }
367         final URLConnection urlc = url.openConnection();
368         final BufferedReader in = new BufferedReader( new InputStreamReader( urlc.getInputStream() ) );
369         String line;
370         final List<String> result = new ArrayList<String>();
371         while ( ( line = in.readLine() ) != null ) {
372             if ( DEBUG ) {
373                 System.out.println( line );
374             }
375             result.add( line );
376             if ( result.size() > max_lines_to_return ) {
377                 break;
378             }
379         }
380         in.close();
381         try {
382             // To prevent accessing online dbs in too quick succession. 
383             Thread.sleep( 20 );
384         }
385         catch ( final InterruptedException e ) {
386             e.printStackTrace();
387         }
388         return result;
389     }
390
391     public static List<String> queryEmblDb( final Identifier id, final int max_lines_to_return ) throws IOException {
392         final StringBuilder url_sb = new StringBuilder();
393         url_sb.append( BASE_EMBL_DB_URL );
394         if ( ForesterUtil.isEmpty( id.getProvider() ) || id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) {
395             url_sb.append( SequenceDbWsTools.EMBL_DBS_EMBL );
396             url_sb.append( '/' );
397         }
398         else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) {
399             if ( id.getValue().toUpperCase().indexOf( 'P' ) == 1 ) {
400                 url_sb.append( SequenceDbWsTools.EMBL_DBS_REFSEQ_P );
401                 url_sb.append( '/' );
402             }
403             else {
404                 url_sb.append( SequenceDbWsTools.EMBL_DBS_REFSEQ_N );
405                 url_sb.append( '/' );
406             }
407         }
408         return queryDb( id.getValue(), max_lines_to_return, url_sb.toString() );
409     }
410
411     public static List<String> queryUniprot( final String query, final int max_lines_to_return ) throws IOException {
412         return queryDb( query, max_lines_to_return, BASE_UNIPROT_URL );
413     }
414
415     private static String encode( final String str ) throws UnsupportedEncodingException {
416         return URLEncoder.encode( str.trim(), URL_ENC );
417     }
418
419     private static List<String> getTaxonomyStringFromCommonName( final String cn, final int max_lines_to_return )
420             throws IOException {
421         return queryUniprot( "taxonomy/?query=common%3a%22" + encode( cn ) + "%22&format=tab", max_lines_to_return );
422     }
423
424     private static List<String> getTaxonomyStringFromId( final String id, final int max_lines_to_return )
425             throws IOException {
426         return queryUniprot( "taxonomy/?query=id%3a%22" + encode( id ) + "%22&format=tab", max_lines_to_return );
427     }
428
429     private static List<String> getTaxonomyStringFromScientificName( final String sn, final int max_lines_to_return )
430             throws IOException {
431         return queryUniprot( "taxonomy/?query=scientific%3a%22" + encode( sn ) + "%22&format=tab", max_lines_to_return );
432     }
433
434     private static List<String> getTaxonomyStringFromTaxonomyCode( final String code, final int max_lines_to_return )
435             throws IOException {
436         return queryUniprot( "taxonomy/?query=mnemonic%3a%22" + encode( code ) + "%22&format=tab", max_lines_to_return );
437     }
438
439     private static List<UniProtTaxonomy> parseUniProtTaxonomy( final List<String> result ) throws IOException {
440         final List<UniProtTaxonomy> taxonomies = new ArrayList<UniProtTaxonomy>();
441         for( final String line : result ) {
442             if ( ForesterUtil.isEmpty( line ) ) {
443                 // Ignore empty lines.
444             }
445             else if ( line.startsWith( "Taxon" ) ) {
446                 final String[] items = line.split( "\t" );
447                 if ( !( items[ 1 ].equalsIgnoreCase( "Mnemonic" ) && items[ 2 ].equalsIgnoreCase( "Scientific name" )
448                         && items[ 3 ].equalsIgnoreCase( "Common name" ) && items[ 4 ].equalsIgnoreCase( "Synonym" )
449                         && items[ 5 ].equalsIgnoreCase( "Other Names" ) && items[ 6 ].equalsIgnoreCase( "Reviewed" )
450                         && items[ 7 ].equalsIgnoreCase( "Rank" ) && items[ 8 ].equalsIgnoreCase( "Lineage" ) ) ) {
451                     throw new IOException( "Unreconized UniProt Taxonomy format: " + line );
452                 }
453             }
454             else {
455                 if ( line.split( "\t" ).length > 4 ) {
456                     taxonomies.add( new UniProtTaxonomy( line ) );
457                 }
458             }
459         }
460         return taxonomies;
461     }
462
463     public enum Db {
464         UNIPROT, EMBL, NCBI, NONE, REFSEQ;
465     }
466 }