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