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