refactored
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / UniProtSequenceObtainer.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.List;
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.UniProtEntry;
47 import org.forester.ws.uniprot.UniProtWsTools;
48
49 public class UniProtSequenceObtainer implements Runnable {
50
51     private final Phylogeny            _phy;
52     private final MainFrameApplication _mf;
53     private final TreePanel            _treepanel;
54
55     public UniProtSequenceObtainer( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
56         _phy = phy;
57         _mf = mf;
58         _treepanel = treepanel;
59     }
60
61     private String getBaseUrl() {
62         return UniProtWsTools.BASE_URL;
63     }
64
65     private void execute() {
66         _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
67         SortedSet<String> not_found = null;
68         try {
69             not_found = obtainSeqInformation( _phy );
70         }
71         catch ( final UnknownHostException e ) {
72             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
73             JOptionPane.showMessageDialog( _mf,
74                                            "Could not connect to \"" + getBaseUrl() + "\"",
75                                            "Network error during taxonomic information gathering",
76                                            JOptionPane.ERROR_MESSAGE );
77             return;
78         }
79         catch ( final IOException e ) {
80             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
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             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
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             sb.append( "Not all identifiers could be resolved.\n" );
103             if ( not_found.size() == 1 ) {
104                 sb.append( "The following identifier was not found:\n" );
105             }
106             else {
107                 sb.append( "The following identifiers were not found (total: " + not_found.size() + "):\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                                                "UniProt 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                                                "UniProt sequence tool successfully completed",
135                                                "UniProt 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     synchronized 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             if ( node.getNodeData().isHasSequence() ) {
149                 //TODO  Do something
150             }
151             //  else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
152             //     not_found.add( node.getName() );
153             //  }
154             else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
155                 String query = node.getName();
156                 if ( query.indexOf( '/' ) > 0 ) {
157                     query = query.substring( 0, query.indexOf( '/' ) );
158                 }
159                 if ( query.indexOf( '.' ) > 0 ) {
160                     query = query.substring( 0, query.indexOf( '.' ) );
161                 }
162                 if ( query.indexOf( '_' ) > 0 ) {
163                     query = query.substring( 0, query.indexOf( '_' ) );
164                 }
165                 final UniProtEntry upe = obtainUniProtEntry( query );
166                 if ( upe != null ) {
167                     final Sequence seq = new Sequence();
168                     final Taxonomy tax = new Taxonomy();
169                     if ( !ForesterUtil.isEmpty( upe.getAc() ) ) {
170                         seq.setAccession( new Accession( upe.getAc(), "uniprot" ) );
171                     }
172                     if ( !ForesterUtil.isEmpty( upe.getRecName() ) ) {
173                         seq.setName( upe.getRecName() );
174                     }
175                     if ( !ForesterUtil.isEmpty( upe.getSymbol() ) ) {
176                         seq.setSymbol( upe.getSymbol() );
177                     }
178                     if ( !ForesterUtil.isEmpty( upe.getOsScientificName() ) ) {
179                         tax.setScientificName( upe.getOsScientificName() );
180                     }
181                     if ( !ForesterUtil.isEmpty( upe.getTaxId() ) ) {
182                         tax.setIdentifier( new Identifier( upe.getTaxId(), "uniprot" ) );
183                     }
184                     node.getNodeData().setTaxonomy( tax );
185                     node.getNodeData().setSequence( seq );
186                 }
187                 else {
188                     not_found.add( node.getName() );
189                 }
190                 //}
191             }
192         }
193         return not_found;
194     }
195
196     static UniProtEntry obtainUniProtEntry( final String query ) throws IOException {
197         final List<String> lines = UniProtWsTools.queryUniprot( "uniprot/" + query + ".txt", 200 );
198         return UniProtEntry.createInstanceFromPlainText( lines );
199     }
200
201     @Override
202     public void run() {
203         execute();
204     }
205 }