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.util.SequenceIdParser;
47 import org.forester.ws.uniprot.SequenceDatabaseEntry;
48 import org.forester.ws.uniprot.UniProtWsTools;
50 public final class SequenceDataRetriver extends RunnableProcess {
52 private final Phylogeny _phy;
53 private final MainFrameApplication _mf;
54 private final TreePanel _treepanel;
55 private final static boolean DEBUG = true;
58 UNKNOWN, UNIPROT, EMBL, NCBI;
61 public SequenceDataRetriver( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
64 _treepanel = treepanel;
67 private void execute() {
68 start( _mf, "sequence data" );
69 SortedSet<String> not_found = null;
71 not_found = obtainSeqInformation( _phy );
73 catch ( final UnknownHostException e ) {
74 final String what = "_"; //TODO FIXME
75 JOptionPane.showMessageDialog( _mf,
76 "Could not connect to \"" + what + "\"",
77 "Network error during taxonomic information gathering",
78 JOptionPane.ERROR_MESSAGE );
81 catch ( final IOException e ) {
83 JOptionPane.showMessageDialog( _mf,
85 "Failed to obtain taxonomic information",
86 JOptionPane.ERROR_MESSAGE );
92 _treepanel.setTree( _phy );
94 _treepanel.setEdited( true );
95 if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
96 int max = not_found.size();
102 final StringBuffer sb = new StringBuffer();
103 if ( not_found.size() == 1 ) {
104 sb.append( "Data for the following sequence identifier was not found:\n" );
107 sb.append( "Data for the following sequence identifiers was not found (total: " + not_found.size()
111 for( final String string : not_found ) {
123 JOptionPane.showMessageDialog( _mf,
125 "Sequence Tool Completed",
126 JOptionPane.WARNING_MESSAGE );
128 catch ( final Exception e ) {
129 // Not important if this fails, do nothing.
134 JOptionPane.showMessageDialog( _mf,
135 "UniProt sequence tool successfully completed",
136 "UniProt Sequence Tool Completed",
137 JOptionPane.INFORMATION_MESSAGE );
139 catch ( final Exception e ) {
140 // Not important if this fails, do nothing.
145 public static SortedSet<String> obtainSeqInformation( final Phylogeny phy ) throws IOException {
146 final SortedSet<String> not_found = new TreeSet<String>();
147 for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
148 final PhylogenyNode node = iter.next();
151 if ( node.getNodeData().isHasSequence() ) {
152 seq = node.getNodeData().getSequence();
155 seq = new Sequence();
157 if ( node.getNodeData().isHasTaxonomy() ) {
158 tax = node.getNodeData().getTaxonomy();
161 tax = new Taxonomy();
164 Identifier id = null;
166 if ( node.getNodeData().isHasSequence() && ( node.getNodeData().getSequence().getAccession() != null )
167 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() )
168 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() )
169 && node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "uniprot" ) ) {
170 query = node.getNodeData().getSequence().getAccession().getValue();
173 else if ( node.getNodeData().isHasSequence()
174 && ( node.getNodeData().getSequence().getAccession() != null )
175 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() )
176 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() )
177 && ( node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "embl" ) || node
178 .getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "ebi" ) ) ) {
179 query = node.getNodeData().getSequence().getAccession().getValue();
182 else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
183 if ( ( query = UniProtWsTools.parseUniProtAccessor( node.getName() ) ) != null ) {
186 else if ( ( id = SequenceIdParser.parse( node.getName() ) ) != null ) {
190 SequenceDatabaseEntry db_entry = null;
191 if ( !ForesterUtil.isEmpty( query ) ) {
192 if ( db == Db.UNIPROT ) {
194 System.out.println( "uniprot: " + query );
197 db_entry = UniProtWsTools.obtainUniProtEntry( query, 200 );
199 catch ( final FileNotFoundException e ) {
203 if ( ( db == Db.EMBL ) || ( ( db == Db.UNIPROT ) && ( db_entry == null ) ) ) {
205 System.out.println( "embl: " + query );
208 db_entry = UniProtWsTools.obtainEmblEntry( query, 200 );
210 catch ( final FileNotFoundException e ) {
213 if ( ( db == Db.UNIPROT ) && ( db_entry != null ) ) {
218 else if ( ( db == Db.NCBI ) && ( id != null ) ) {
219 System.out.println( "db == Db.NCBI && id != null" );
220 db_entry = UniProtWsTools.obtainrefSeqentryFromEmbl( id, 200 );
222 if ( ( db_entry != null ) && !db_entry.isEmpty() ) {
223 if ( !ForesterUtil.isEmpty( db_entry.getAccession() ) ) {
225 if ( db == Db.EMBL ) {
228 else if ( db == Db.UNIPROT ) {
231 seq.setAccession( new Accession( db_entry.getAccession(), type ) );
233 if ( !ForesterUtil.isEmpty( db_entry.getSequenceName() ) ) {
234 seq.setName( db_entry.getSequenceName() );
236 if ( !ForesterUtil.isEmpty( db_entry.getSequenceSymbol() ) ) {
237 seq.setSymbol( db_entry.getSequenceSymbol() );
239 if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyScientificName() ) ) {
240 tax.setScientificName( db_entry.getTaxonomyScientificName() );
242 if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyIdentifier() ) ) {
243 tax.setIdentifier( new Identifier( db_entry.getTaxonomyIdentifier(), "uniprot" ) );
245 node.getNodeData().setTaxonomy( tax );
246 node.getNodeData().setSequence( seq );
249 not_found.add( node.getName() );