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.IOException;
29 import java.net.UnknownHostException;
30 import java.util.List;
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.UniProtEntry;
47 import org.forester.ws.uniprot.UniProtWsTools;
49 public class UniProtSequenceObtainer implements Runnable {
51 private final Phylogeny _phy;
52 private final MainFrameApplication _mf;
53 private final TreePanel _treepanel;
55 public UniProtSequenceObtainer( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
58 _treepanel = treepanel;
61 private String getBaseUrl() {
62 return UniProtWsTools.BASE_URL;
65 private void execute() {
66 _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
67 SortedSet<String> not_found = null;
69 not_found = obtainSeqInformation( _phy );
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 );
79 catch ( final IOException e ) {
80 _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
82 JOptionPane.showMessageDialog( _mf,
84 "Failed to obtain taxonomic information",
85 JOptionPane.ERROR_MESSAGE );
89 _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
91 _treepanel.setTree( _phy );
93 _treepanel.setEdited( true );
94 if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
95 int max = not_found.size();
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" );
107 sb.append( "The following identifiers were not found (total: " + not_found.size() + "):\n" );
110 for( final String string : not_found ) {
122 JOptionPane.showMessageDialog( _mf,
124 "UniProt Sequence Tool Completed",
125 JOptionPane.WARNING_MESSAGE );
127 catch ( final Exception e ) {
128 // Not important if this fails, do nothing.
133 JOptionPane.showMessageDialog( _mf,
134 "UniProt sequence tool successfully completed",
135 "UniProt Sequence Tool Completed",
136 JOptionPane.INFORMATION_MESSAGE );
138 catch ( final Exception e ) {
139 // Not important if this fails, do nothing.
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() ) {
151 // else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
152 // not_found.add( node.getName() );
154 else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
155 String query = node.getName();
156 if ( query.indexOf( '/' ) > 0 ) {
157 query = query.substring( 0, query.indexOf( '/' ) );
159 if ( query.indexOf( '.' ) > 0 ) {
160 query = query.substring( 0, query.indexOf( '.' ) );
162 if ( query.indexOf( '_' ) > 0 ) {
163 query = query.substring( 0, query.indexOf( '_' ) );
165 final UniProtEntry upe = obtainUniProtEntry( query );
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" ) );
172 if ( !ForesterUtil.isEmpty( upe.getRecName() ) ) {
173 seq.setName( upe.getRecName() );
175 if ( !ForesterUtil.isEmpty( upe.getSymbol() ) ) {
176 seq.setSymbol( upe.getSymbol() );
178 if ( !ForesterUtil.isEmpty( upe.getOsScientificName() ) ) {
179 tax.setScientificName( upe.getOsScientificName() );
181 if ( !ForesterUtil.isEmpty( upe.getTaxId() ) ) {
182 tax.setIdentifier( new Identifier( upe.getTaxId(), "uniprot" ) );
184 node.getNodeData().setTaxonomy( tax );
185 node.getNodeData().setSequence( seq );
188 not_found.add( node.getName() );
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 );