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