508d60febe908149973a6dd3c4608d43bc18bdcd
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / SequenceDataRetriver.java
1 // Exp $
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.archaeopteryx.tools;
27
28 import java.io.IOException;
29 import java.net.UnknownHostException;
30 import java.util.SortedSet;
31 import java.util.TreeSet;
32
33 import javax.swing.JOptionPane;
34
35 import org.forester.archaeopteryx.MainFrameApplication;
36 import org.forester.archaeopteryx.TreePanel;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.PhylogenyNode;
39 import org.forester.phylogeny.data.Accession;
40 import org.forester.phylogeny.data.Identifier;
41 import org.forester.phylogeny.data.Sequence;
42 import org.forester.phylogeny.data.Taxonomy;
43 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
44 import org.forester.util.ForesterUtil;
45 import org.forester.util.SequenceIdParser;
46 import org.forester.ws.seqdb.SequenceDatabaseEntry;
47 import org.forester.ws.seqdb.SequenceDbWsTools;
48
49 public final class SequenceDataRetriver extends RunnableProcess {
50
51     private final Phylogeny            _phy;
52     private final MainFrameApplication _mf;
53     private final TreePanel            _treepanel;
54     private final static boolean       DEBUG = true;
55
56     private enum Db {
57         UNIPROT, EMBL, NCBI, NONE, REFSEQ;
58     }
59
60     public SequenceDataRetriver( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
61         _phy = phy;
62         _mf = mf;
63         _treepanel = treepanel;
64     }
65
66     private void execute() {
67         start( _mf, "sequence data" );
68         SortedSet<String> not_found = null;
69         try {
70             not_found = obtainSeqInformation( _phy );
71         }
72         catch ( final UnknownHostException e ) {
73             final String what = "_"; //TODO FIXME 
74             JOptionPane.showMessageDialog( _mf,
75                                            "Could not connect to \"" + what + "\"",
76                                            "Network error during taxonomic information gathering",
77                                            JOptionPane.ERROR_MESSAGE );
78             return;
79         }
80         catch ( final IOException e ) {
81             e.printStackTrace();
82             JOptionPane.showMessageDialog( _mf,
83                                            e.toString(),
84                                            "Failed to obtain taxonomic information",
85                                            JOptionPane.ERROR_MESSAGE );
86             return;
87         }
88         finally {
89             end( _mf );
90         }
91         _treepanel.setTree( _phy );
92         _mf.showWhole();
93         _treepanel.setEdited( true );
94         if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
95             int max = not_found.size();
96             boolean more = false;
97             if ( max > 20 ) {
98                 more = true;
99                 max = 20;
100             }
101             final StringBuffer sb = new StringBuffer();
102             if ( not_found.size() == 1 ) {
103                 sb.append( "Data for the following sequence identifier was not found:\n" );
104             }
105             else {
106                 sb.append( "Data for the following sequence identifiers was not found (total: " + not_found.size()
107                         + "):\n" );
108             }
109             int i = 0;
110             for( final String string : not_found ) {
111                 if ( i > 19 ) {
112                     break;
113                 }
114                 sb.append( string );
115                 sb.append( "\n" );
116                 ++i;
117             }
118             if ( more ) {
119                 sb.append( "..." );
120             }
121             try {
122                 JOptionPane.showMessageDialog( _mf,
123                                                sb.toString(),
124                                                "Sequence Tool Completed",
125                                                JOptionPane.WARNING_MESSAGE );
126             }
127             catch ( final Exception e ) {
128                 // Not important if this fails, do nothing. 
129             }
130         }
131         else {
132             try {
133                 JOptionPane.showMessageDialog( _mf,
134                                                "Sequence tool successfully completed",
135                                                "Sequence Tool Completed",
136                                                JOptionPane.INFORMATION_MESSAGE );
137             }
138             catch ( final Exception e ) {
139                 // Not important if this fails, do nothing.
140             }
141         }
142     }
143
144     public static SortedSet<String> obtainSeqInformation( final Phylogeny phy ) throws IOException {
145         final SortedSet<String> not_found = new TreeSet<String>();
146         for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
147             final PhylogenyNode node = iter.next();
148             final Sequence seq = node.getNodeData().isHasSequence() ?  node.getNodeData().getSequence() : new Sequence() ;
149             final Taxonomy tax = node.getNodeData().isHasTaxonomy() ? node.getNodeData().getTaxonomy() : new Taxonomy() ;
150             
151            
152             String query = null;
153             Identifier id = null;
154             Db db = Db.NONE;
155             if ( node.getNodeData().isHasSequence() && ( node.getNodeData().getSequence().getAccession() != null )
156                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() )
157                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() )
158                     && node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "uniprot" ) ) {
159                 query = node.getNodeData().getSequence().getAccession().getValue();
160                 db = Db.UNIPROT;
161             }
162             else if ( node.getNodeData().isHasSequence()
163                     && ( node.getNodeData().getSequence().getAccession() != null )
164                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() )
165                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() )
166                     && ( node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "embl" ) || node
167                             .getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "ebi" ) ) ) {
168                 query = node.getNodeData().getSequence().getAccession().getValue();
169                 db = Db.EMBL;
170             }
171             else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
172                 if ( ( query = SequenceDbWsTools.parseUniProtAccessor( node.getName() ) ) != null ) {
173                     db = Db.UNIPROT;
174                 }
175                 else if ( ( id = SequenceIdParser.parse( node.getName() ) ) != null ) {
176                     
177                     if ( id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) {
178                         db = Db.NCBI;
179                     }
180                     else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) {
181                         db = Db.REFSEQ;
182                     }
183                    
184                 }
185             }
186             SequenceDatabaseEntry db_entry = null;
187             if ( !ForesterUtil.isEmpty( query ) ) {
188                 if ( db == Db.UNIPROT ) {
189                     if ( DEBUG ) {
190                         System.out.println( "uniprot: " + query );
191                     }
192                     db_entry = SequenceDbWsTools.obtainUniProtEntry( query, 200 );
193                 }
194                 if ( ( db == Db.EMBL ) || ( ( db == Db.UNIPROT ) && ( db_entry == null ) ) ) {
195                     if ( DEBUG ) {
196                         System.out.println( "embl: " + query );
197                     }
198                     db_entry = SequenceDbWsTools.obtainEmblEntry(  new Identifier( query ), 200 );
199                     if ( ( db == Db.UNIPROT ) && ( db_entry != null ) ) {
200                         db = Db.EMBL;
201                     }
202                 }
203             }
204             else if ( ( db == Db.REFSEQ ) && ( id != null ) ) {
205                 db_entry = SequenceDbWsTools.obtainRefSeqEntryFromEmbl( id, 200 );
206             }
207             else if ( ( db == Db.NCBI ) && ( id != null ) ) {
208                 db_entry = SequenceDbWsTools.obtainEmblEntry( id, 200 );
209             }
210             if ( ( db_entry != null ) && !db_entry.isEmpty() ) {
211                 if ( !ForesterUtil.isEmpty( db_entry.getAccession() ) ) {
212                     String type = null;
213                     if ( db == Db.EMBL ) {
214                         type = "embl";
215                     }
216                     else if ( db == Db.UNIPROT ) {
217                         type = "uniprot";
218                     }
219                     else if ( db == Db.NCBI ) {
220                         type = "ncbi";
221                     }
222                     else if ( db == Db.REFSEQ ) {
223                         type = "refseq";
224                     }
225                     seq.setAccession( new Accession( db_entry.getAccession(), type ) );
226                 }
227                 if ( !ForesterUtil.isEmpty( db_entry.getSequenceName() ) ) {
228                     seq.setName( db_entry.getSequenceName() );
229                 }
230                 if ( !ForesterUtil.isEmpty( db_entry.getSequenceSymbol() ) ) {
231                     seq.setSymbol( db_entry.getSequenceSymbol() );
232                 }
233                 if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyScientificName() ) ) {
234                     tax.setScientificName( db_entry.getTaxonomyScientificName() );
235                 }
236                 if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyIdentifier() ) ) {
237                     tax.setIdentifier( new Identifier( db_entry.getTaxonomyIdentifier(), "uniprot" ) );
238                 }
239                 node.getNodeData().setTaxonomy( tax );
240                 node.getNodeData().setSequence( seq );
241             }
242             else if ( db != Db.NONE ) {
243                 not_found.add( node.getName() );
244             }
245         }
246         return not_found;
247     }
248
249     @Override
250     public void run() {
251         execute();
252     }
253 }