pattern match for up added....
[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.SequenceDatabaseEntry;
47 import org.forester.ws.uniprot.UniProtWsTools;
48
49 public final class SequenceDataRetriver implements Runnable {
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         UNKNOWN, UNIPROT;
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 String getBaseUrl() {
67         return UniProtWsTools.BASE_URL;
68     }
69
70     private void execute() {
71         _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
72         SortedSet<String> not_found = null;
73         try {
74             not_found = obtainSeqInformation( _phy );
75         }
76         catch ( final UnknownHostException e ) {
77             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
78             JOptionPane.showMessageDialog( _mf,
79                                            "Could not connect to \"" + getBaseUrl() + "\"",
80                                            "Network error during taxonomic information gathering",
81                                            JOptionPane.ERROR_MESSAGE );
82             return;
83         }
84         catch ( final IOException e ) {
85             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
86             e.printStackTrace();
87             JOptionPane.showMessageDialog( _mf,
88                                            e.toString(),
89                                            "Failed to obtain taxonomic information",
90                                            JOptionPane.ERROR_MESSAGE );
91             return;
92         }
93         finally {
94             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
95         }
96         _treepanel.setTree( _phy );
97         _mf.showWhole();
98         _treepanel.setEdited( true );
99         if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
100             int max = not_found.size();
101             boolean more = false;
102             if ( max > 20 ) {
103                 more = true;
104                 max = 20;
105             }
106             final StringBuffer sb = new StringBuffer();
107             sb.append( "Not all identifiers could be resolved.\n" );
108             if ( not_found.size() == 1 ) {
109                 sb.append( "The following identifier was not found:\n" );
110             }
111             else {
112                 sb.append( "The following identifiers were not found (total: " + not_found.size() + "):\n" );
113             }
114             int i = 0;
115             for( final String string : not_found ) {
116                 if ( i > 19 ) {
117                     break;
118                 }
119                 sb.append( string );
120                 sb.append( "\n" );
121                 ++i;
122             }
123             if ( more ) {
124                 sb.append( "..." );
125             }
126             try {
127                 JOptionPane.showMessageDialog( _mf,
128                                                sb.toString(),
129                                                "UniProt Sequence Tool Completed",
130                                                JOptionPane.WARNING_MESSAGE );
131             }
132             catch ( final Exception e ) {
133                 // Not important if this fails, do nothing. 
134             }
135         }
136         else {
137             try {
138                 JOptionPane.showMessageDialog( _mf,
139                                                "UniProt sequence tool successfully completed",
140                                                "UniProt Sequence Tool Completed",
141                                                JOptionPane.INFORMATION_MESSAGE );
142             }
143             catch ( final Exception e ) {
144                 // Not important if this fails, do nothing.
145             }
146         }
147     }
148
149     public static SortedSet<String> obtainSeqInformation( final Phylogeny phy ) throws IOException {
150         final SortedSet<String> not_found = new TreeSet<String>();
151         for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
152             final PhylogenyNode node = iter.next();
153             Sequence seq = null;
154             Taxonomy tax = null;
155             if ( node.getNodeData().isHasSequence() ) {
156                 seq = node.getNodeData().getSequence();
157             }
158             else {
159                 seq = new Sequence();
160             }
161             if ( node.getNodeData().isHasTaxonomy() ) {
162                 tax = node.getNodeData().getTaxonomy();
163             }
164             else {
165                 tax = new Taxonomy();
166             }
167             String query = null;
168             Db db = Db.UNKNOWN;
169             if ( node.getNodeData().isHasSequence() && ( node.getNodeData().getSequence().getAccession() != null )
170                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() )
171                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() )
172                     && node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "uniprot" ) ) {
173                 query = node.getNodeData().getSequence().getAccession().getValue();
174                 db = Db.UNIPROT;
175             }
176             else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
177                 query = UniProtWsTools.parseUniProtAccessor( node.getName() );
178                 if ( !ForesterUtil.isEmpty( query ) ) {
179                     db = Db.UNIPROT;
180                 }
181             }
182             if ( !ForesterUtil.isEmpty( query ) ) {
183                 SequenceDatabaseEntry db_entry = null;
184                 if ( db == Db.UNIPROT ) {
185                     if ( DEBUG ) {
186                         System.out.println( "uniprot: " + query );
187                     }
188                     try {
189                         db_entry = UniProtWsTools.obtainUniProtEntry( query, 200 );
190                     }
191                     catch ( final FileNotFoundException e ) {
192                         // Ignore.
193                     }
194                 }
195                 if ( db_entry != null ) {
196                     if ( !ForesterUtil.isEmpty( db_entry.getAccession() ) ) {
197                         seq.setAccession( new Accession( db_entry.getAccession(), "uniprot" ) );
198                     }
199                     if ( !ForesterUtil.isEmpty( db_entry.getSequenceName() ) ) {
200                         seq.setName( db_entry.getSequenceName() );
201                     }
202                     if ( !ForesterUtil.isEmpty( db_entry.getSequenceSymbol() ) ) {
203                         seq.setSymbol( db_entry.getSequenceSymbol() );
204                     }
205                     if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyScientificName() ) ) {
206                         tax.setScientificName( db_entry.getTaxonomyScientificName() );
207                     }
208                     if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyIdentifier() ) ) {
209                         tax.setIdentifier( new Identifier( db_entry.getTaxonomyIdentifier(), "uniprot" ) );
210                     }
211                     node.getNodeData().setTaxonomy( tax );
212                     node.getNodeData().setSequence( seq );
213                 }
214                 else {
215                     not_found.add( node.getName() );
216                 }
217             }
218         }
219         return not_found;
220     }
221
222     @Override
223     public void run() {
224         execute();
225     }
226 }