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.SequenceDatabaseEntry;
47 import org.forester.ws.uniprot.UniProtWsTools;
49 public final class SequenceDataRetriver implements Runnable {
51 private final Phylogeny _phy;
52 private final MainFrameApplication _mf;
53 private final TreePanel _treepanel;
54 private final static boolean DEBUG = true;
60 public SequenceDataRetriver( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
63 _treepanel = treepanel;
66 private String getBaseUrl() {
67 return UniProtWsTools.BASE_URL;
70 private void execute() {
71 _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
72 SortedSet<String> not_found = null;
74 not_found = obtainSeqInformation( _phy );
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 );
84 catch ( final IOException e ) {
85 _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
87 JOptionPane.showMessageDialog( _mf,
89 "Failed to obtain taxonomic information",
90 JOptionPane.ERROR_MESSAGE );
94 _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
96 _treepanel.setTree( _phy );
98 _treepanel.setEdited( true );
99 if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
100 int max = not_found.size();
101 boolean more = false;
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" );
112 sb.append( "The following identifiers were not found (total: " + not_found.size() + "):\n" );
115 for( final String string : not_found ) {
127 JOptionPane.showMessageDialog( _mf,
129 "UniProt Sequence Tool Completed",
130 JOptionPane.WARNING_MESSAGE );
132 catch ( final Exception e ) {
133 // Not important if this fails, do nothing.
138 JOptionPane.showMessageDialog( _mf,
139 "UniProt sequence tool successfully completed",
140 "UniProt Sequence Tool Completed",
141 JOptionPane.INFORMATION_MESSAGE );
143 catch ( final Exception e ) {
144 // Not important if this fails, do nothing.
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();
155 if ( node.getNodeData().isHasSequence() ) {
156 seq = node.getNodeData().getSequence();
159 seq = new Sequence();
161 if ( node.getNodeData().isHasTaxonomy() ) {
162 tax = node.getNodeData().getTaxonomy();
165 tax = new Taxonomy();
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();
176 else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
177 query = UniProtWsTools.parseUniProtAccessor( node.getName() );
178 if ( !ForesterUtil.isEmpty( query ) ) {
182 if ( !ForesterUtil.isEmpty( query ) ) {
183 SequenceDatabaseEntry db_entry = null;
184 if ( db == Db.UNIPROT ) {
186 System.out.println( "uniprot: " + query );
189 db_entry = UniProtWsTools.obtainUniProtEntry( query, 200 );
191 catch ( final FileNotFoundException e ) {
195 if ( db_entry != null ) {
196 if ( !ForesterUtil.isEmpty( db_entry.getAccession() ) ) {
197 seq.setAccession( new Accession( db_entry.getAccession(), "uniprot" ) );
199 if ( !ForesterUtil.isEmpty( db_entry.getSequenceName() ) ) {
200 seq.setName( db_entry.getSequenceName() );
202 if ( !ForesterUtil.isEmpty( db_entry.getSequenceSymbol() ) ) {
203 seq.setSymbol( db_entry.getSequenceSymbol() );
205 if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyScientificName() ) ) {
206 tax.setScientificName( db_entry.getTaxonomyScientificName() );
208 if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyIdentifier() ) ) {
209 tax.setIdentifier( new Identifier( db_entry.getTaxonomyIdentifier(), "uniprot" ) );
211 node.getNodeData().setTaxonomy( tax );
212 node.getNodeData().setSequence( seq );
215 not_found.add( node.getName() );