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.SortedSet;
31 import java.util.TreeSet;
33 import javax.swing.JOptionPane;
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;
49 public final class SequenceDataRetriver extends RunnableProcess {
51 public final static int DEFAULT_LINES_TO_RETURN = 50;
53 private final Phylogeny _phy;
54 private final MainFrameApplication _mf;
55 private final TreePanel _treepanel;
56 private final static boolean DEBUG = true;
59 UNIPROT, EMBL, NCBI, NONE, REFSEQ;
62 public SequenceDataRetriver( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
65 _treepanel = treepanel;
68 private void execute() {
69 start( _mf, "sequence data" );
70 SortedSet<String> not_found = null;
72 not_found = obtainSeqInformation( _phy );
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 );
82 catch ( final IOException e ) {
84 JOptionPane.showMessageDialog( _mf,
86 "Failed to obtain taxonomic information",
87 JOptionPane.ERROR_MESSAGE );
93 _treepanel.setTree( _phy );
95 _treepanel.setEdited( true );
96 if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
97 int max = not_found.size();
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" );
108 sb.append( "Data for the following sequence identifiers was not found (total: " + not_found.size()
112 for( final String string : not_found ) {
124 JOptionPane.showMessageDialog( _mf,
126 "Sequence Tool Completed",
127 JOptionPane.WARNING_MESSAGE );
129 catch ( final Exception e ) {
130 // Not important if this fails, do nothing.
135 JOptionPane.showMessageDialog( _mf,
136 "Sequence tool successfully completed",
137 "Sequence Tool Completed",
138 JOptionPane.INFORMATION_MESSAGE );
140 catch ( final Exception e ) {
141 // Not important if this fails, do nothing.
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() ;
155 Identifier id = null;
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();
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();
173 else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
174 if ( ( query = SequenceDbWsTools.parseUniProtAccessor( node.getName() ) ) != null ) {
177 else if ( ( id = SequenceIdParser.parse( node.getName() ) ) != null ) {
179 if ( id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) {
182 else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) {
188 SequenceDatabaseEntry db_entry = null;
189 if ( !ForesterUtil.isEmpty( query ) ) {
190 if ( db == Db.UNIPROT ) {
192 System.out.println( "uniprot: " + query );
194 db_entry = SequenceDbWsTools.obtainUniProtEntry( query, DEFAULT_LINES_TO_RETURN );
196 if ( ( db == Db.EMBL ) || ( ( db == Db.UNIPROT ) && ( db_entry == null ) ) ) {
198 System.out.println( "embl: " + query );
200 db_entry = SequenceDbWsTools.obtainEmblEntry( new Identifier( query ), DEFAULT_LINES_TO_RETURN );
201 if ( ( db == Db.UNIPROT ) && ( db_entry != null ) ) {
206 else if ( ( db == Db.REFSEQ ) && ( id != null ) ) {
207 db_entry = SequenceDbWsTools.obtainRefSeqEntryFromEmbl( id, DEFAULT_LINES_TO_RETURN );
209 else if ( ( db == Db.NCBI ) && ( id != null ) ) {
210 db_entry = SequenceDbWsTools.obtainEmblEntry( id, DEFAULT_LINES_TO_RETURN );
212 if ( ( db_entry != null ) && !db_entry.isEmpty() ) {
213 if ( !ForesterUtil.isEmpty( db_entry.getAccession() ) ) {
215 if ( db == Db.EMBL ) {
218 else if ( db == Db.UNIPROT ) {
221 else if ( db == Db.NCBI ) {
224 else if ( db == Db.REFSEQ ) {
227 seq.setAccession( new Accession( db_entry.getAccession(), type ) );
229 if ( !ForesterUtil.isEmpty( db_entry.getSequenceName() ) ) {
230 seq.setName( db_entry.getSequenceName() );
232 if ( !ForesterUtil.isEmpty( db_entry.getSequenceSymbol() ) ) {
233 seq.setSymbol( db_entry.getSequenceSymbol() );
235 if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyScientificName() ) ) {
236 tax.setScientificName( db_entry.getTaxonomyScientificName() );
238 if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyIdentifier() ) ) {
239 tax.setIdentifier( new Identifier( db_entry.getTaxonomyIdentifier(), "uniprot" ) );
241 node.getNodeData().setTaxonomy( tax );
242 node.getNodeData().setSequence( seq );
244 else if ( db != Db.NONE ) {
245 not_found.add( node.getName() );