phylotastic hackathon at NESCENT 120606
[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.util.SequenceIdParser;
47 import org.forester.ws.uniprot.DatabaseTools;
48 import org.forester.ws.uniprot.SequenceDatabaseEntry;
49 import org.forester.ws.uniprot.UniProtWsTools;
50
51 public final class SequenceDataRetriver extends RunnableProcess {
52
53     private final Phylogeny            _phy;
54     private final MainFrameApplication _mf;
55     private final TreePanel            _treepanel;
56     private final static boolean       DEBUG = false;
57
58     private enum Db {
59         UNKNOWN, UNIPROT, EMBL, NCBI;
60     }
61
62     public SequenceDataRetriver( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
63         _phy = phy;
64         _mf = mf;
65         _treepanel = treepanel;
66     }
67
68     private String getBaseUrl() {
69         return UniProtWsTools.BASE_URL;
70     }
71
72     private void execute() {
73         start( _mf, "sequence data" );
74         SortedSet<String> not_found = null;
75         try {
76             not_found = obtainSeqInformation( _phy );
77         }
78         catch ( final UnknownHostException e ) {
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             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             end( _mf );
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             if ( not_found.size() == 1 ) {
108                 sb.append( "Data for the following sequence identifier was not found:\n" );
109             }
110             else {
111                 sb.append( "Data for the following sequence identifiers was not found (total: " + not_found.size()
112                         + "):\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                                                "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 ( node.getNodeData().isHasSequence()
177                     && ( node.getNodeData().getSequence().getAccession() != null )
178                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() )
179                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() )
180                     && ( node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "embl" ) || node
181                             .getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "ebi" ) ) ) {
182                 query = node.getNodeData().getSequence().getAccession().getValue();
183                 db = Db.EMBL;
184             }
185             else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
186                 if ( ( query = UniProtWsTools.parseUniProtAccessor( node.getName() ) ) != null ) {
187                     db = Db.UNIPROT;
188                 }
189                 else if ( ( query = SequenceIdParser.parseGenbankAccessor( node.getName() ) ) != null ) {
190                     db = Db.NCBI;
191                 }
192             }
193             if ( !ForesterUtil.isEmpty( query ) ) {
194                 SequenceDatabaseEntry db_entry = null;
195                 if ( db == Db.UNIPROT ) {
196                     if ( DEBUG ) {
197                         System.out.println( "uniprot: " + query );
198                     }
199                     try {
200                         db_entry = UniProtWsTools.obtainUniProtEntry( query, 200 );
201                     }
202                     catch ( final FileNotFoundException e ) {
203                         // Ignore.
204                     }
205                 }
206                 if ( ( db == Db.EMBL ) || ( ( db == Db.UNIPROT ) && ( db_entry == null ) ) ) {
207                     if ( DEBUG ) {
208                         System.out.println( "embl: " + query );
209                     }
210                     try {
211                         db_entry = UniProtWsTools.obtainEmblEntry( query, 200 );
212                     }
213                     catch ( final FileNotFoundException e ) {
214                         // Ignore.
215                     }
216                     if ( ( db == Db.UNIPROT ) && ( db_entry != null ) ) {
217                         db = Db.EMBL;
218                     }
219                 }
220                 if ( ( db_entry != null ) && !db_entry.isEmpty() ) {
221                     if ( !ForesterUtil.isEmpty( db_entry.getAccession() ) ) {
222                         String type = null;
223                         if ( db == Db.EMBL ) {
224                             type = "embl";
225                         }
226                         else if ( db == Db.UNIPROT ) {
227                             type = "uniprot";
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 {
247                     not_found.add( node.getName() );
248                 }
249             }
250         }
251         return not_found;
252     }
253
254     @Override
255     public void run() {
256         execute();
257     }
258 }