2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.archaeopteryx.tools;
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;
34 import javax.swing.JOptionPane;
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;
50 public final class SequenceDataRetriver implements Runnable {
52 private final Phylogeny _phy;
53 private final MainFrameApplication _mf;
54 private final TreePanel _treepanel;
55 private final static boolean DEBUG = false;
58 UNKNOWN, UNIPROT, EMBL;
61 public SequenceDataRetriver( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
64 _treepanel = treepanel;
67 private String getBaseUrl() {
68 return UniProtWsTools.BASE_URL;
71 private void execute() {
72 _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
73 SortedSet<String> not_found = null;
75 not_found = obtainSeqInformation( _phy );
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 );
85 catch ( final IOException e ) {
86 _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
88 JOptionPane.showMessageDialog( _mf,
90 "Failed to obtain taxonomic information",
91 JOptionPane.ERROR_MESSAGE );
95 _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
97 _treepanel.setTree( _phy );
99 _treepanel.setEdited( true );
100 if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
101 int max = not_found.size();
102 boolean more = false;
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" );
112 sb.append( "Data for the following sequence identifiers was not found (total: " + not_found.size()
116 for( final String string : not_found ) {
128 JOptionPane.showMessageDialog( _mf,
130 "Sequence Tool Completed",
131 JOptionPane.WARNING_MESSAGE );
133 catch ( final Exception e ) {
134 // Not important if this fails, do nothing.
139 JOptionPane.showMessageDialog( _mf,
140 "UniProt sequence tool successfully completed",
141 "UniProt Sequence Tool Completed",
142 JOptionPane.INFORMATION_MESSAGE );
144 catch ( final Exception e ) {
145 // Not important if this fails, do nothing.
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();
156 if ( node.getNodeData().isHasSequence() ) {
157 seq = node.getNodeData().getSequence();
160 seq = new Sequence();
162 if ( node.getNodeData().isHasTaxonomy() ) {
163 tax = node.getNodeData().getTaxonomy();
166 tax = new Taxonomy();
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();
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();
186 else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
187 if ( ( query = UniProtWsTools.parseUniProtAccessor( node.getName() ) ) != null ) {
190 else if ( ( query = DatabaseTools.parseGenbankAccessor( node.getName() ) ) != null ) {
194 if ( !ForesterUtil.isEmpty( query ) ) {
195 SequenceDatabaseEntry db_entry = null;
196 if ( db == Db.UNIPROT ) {
198 System.out.println( "uniprot: " + query );
201 db_entry = UniProtWsTools.obtainUniProtEntry( query, 200 );
203 catch ( final FileNotFoundException e ) {
207 if ( ( db == Db.EMBL ) || ( ( db == Db.UNIPROT ) && ( db_entry == null ) ) ) {
209 System.out.println( "embl: " + query );
212 db_entry = UniProtWsTools.obtainEmblEntry( query, 200 );
214 catch ( final FileNotFoundException e ) {
217 if ( ( db == Db.UNIPROT ) && ( db_entry != null ) ) {
221 if ( ( db_entry != null ) && !db_entry.isEmpty() ) {
222 if ( !ForesterUtil.isEmpty( db_entry.getAccession() ) ) {
224 if ( db == Db.EMBL ) {
227 else if ( db == Db.UNIPROT ) {
230 seq.setAccession( new Accession( db_entry.getAccession(), type ) );
232 if ( !ForesterUtil.isEmpty( db_entry.getSequenceName() ) ) {
233 seq.setName( db_entry.getSequenceName() );
235 if ( !ForesterUtil.isEmpty( db_entry.getSequenceSymbol() ) ) {
236 seq.setSymbol( db_entry.getSequenceSymbol() );
238 if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyScientificName() ) ) {
239 tax.setScientificName( db_entry.getTaxonomyScientificName() );
241 if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyIdentifier() ) ) {
242 tax.setIdentifier( new Identifier( db_entry.getTaxonomyIdentifier(), "uniprot" ) );
244 node.getNodeData().setTaxonomy( tax );
245 node.getNodeData().setSequence( seq );
248 not_found.add( node.getName() );