in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / SequenceDataRetriver.java
index 7aed148..3f4ddda 100644 (file)
@@ -25,7 +25,6 @@
 
 package org.forester.archaeopteryx.tools;
 
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.net.UnknownHostException;
 import java.util.SortedSet;
@@ -43,19 +42,20 @@ import org.forester.phylogeny.data.Sequence;
 import org.forester.phylogeny.data.Taxonomy;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 import org.forester.util.ForesterUtil;
-import org.forester.ws.uniprot.DatabaseTools;
-import org.forester.ws.uniprot.SequenceDatabaseEntry;
-import org.forester.ws.uniprot.UniProtWsTools;
+import org.forester.util.SequenceIdParser;
+import org.forester.ws.seqdb.SequenceDatabaseEntry;
+import org.forester.ws.seqdb.SequenceDbWsTools;
 
-public final class SequenceDataRetriver implements Runnable {
+public final class SequenceDataRetriver extends RunnableProcess {
 
+    public final static int            DEFAULT_LINES_TO_RETURN = 50;
     private final Phylogeny            _phy;
     private final MainFrameApplication _mf;
     private final TreePanel            _treepanel;
-    private final static boolean       DEBUG = false;
+    private final static boolean       DEBUG                   = false;
 
     private enum Db {
-        UNKNOWN, UNIPROT, EMBL;
+        UNIPROT, EMBL, NCBI, NONE, REFSEQ;
     }
 
     public SequenceDataRetriver( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
@@ -64,26 +64,21 @@ public final class SequenceDataRetriver implements Runnable {
         _treepanel = treepanel;
     }
 
-    private String getBaseUrl() {
-        return UniProtWsTools.BASE_URL;
-    }
-
     private void execute() {
-        _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
+        start( _mf, "sequence data" );
         SortedSet<String> not_found = null;
         try {
-            not_found = obtainSeqInformation( _phy );
+            not_found = obtainSeqInformation( _phy, false, true );
         }
         catch ( final UnknownHostException e ) {
-            _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
+            final String what = "_"; //TODO FIXME 
             JOptionPane.showMessageDialog( _mf,
-                                           "Could not connect to \"" + getBaseUrl() + "\"",
+                                           "Could not connect to \"" + what + "\"",
                                            "Network error during taxonomic information gathering",
                                            JOptionPane.ERROR_MESSAGE );
             return;
         }
         catch ( final IOException e ) {
-            _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
             e.printStackTrace();
             JOptionPane.showMessageDialog( _mf,
                                            e.toString(),
@@ -92,7 +87,7 @@ public final class SequenceDataRetriver implements Runnable {
             return;
         }
         finally {
-            _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
+            end( _mf );
         }
         _treepanel.setTree( _phy );
         _mf.showWhole();
@@ -137,8 +132,8 @@ public final class SequenceDataRetriver implements Runnable {
         else {
             try {
                 JOptionPane.showMessageDialog( _mf,
-                                               "UniProt sequence tool successfully completed",
-                                               "UniProt Sequence Tool Completed",
+                                               "Sequence tool successfully completed",
+                                               "Sequence Tool Completed",
                                                JOptionPane.INFORMATION_MESSAGE );
             }
             catch ( final Exception e ) {
@@ -147,26 +142,21 @@ public final class SequenceDataRetriver implements Runnable {
         }
     }
 
-    public static SortedSet<String> obtainSeqInformation( final Phylogeny phy ) throws IOException {
+    public static SortedSet<String> obtainSeqInformation( final Phylogeny phy,
+                                                          final boolean ext_nodes_only,
+                                                          final boolean allow_to_set_taxonomic_data )
+            throws IOException {
         final SortedSet<String> not_found = new TreeSet<String>();
         for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
             final PhylogenyNode node = iter.next();
-            Sequence seq = null;
-            Taxonomy tax = null;
-            if ( node.getNodeData().isHasSequence() ) {
-                seq = node.getNodeData().getSequence();
-            }
-            else {
-                seq = new Sequence();
-            }
-            if ( node.getNodeData().isHasTaxonomy() ) {
-                tax = node.getNodeData().getTaxonomy();
-            }
-            else {
-                tax = new Taxonomy();
+            if ( ext_nodes_only && node.isInternal() ) {
+                continue;
             }
+            final Sequence seq = node.getNodeData().isHasSequence() ? node.getNodeData().getSequence() : new Sequence();
+            final Taxonomy tax = node.getNodeData().isHasTaxonomy() ? node.getNodeData().getTaxonomy() : new Taxonomy();
             String query = null;
-            Db db = Db.UNKNOWN;
+            Identifier id = null;
+            Db db = Db.NONE;
             if ( node.getNodeData().isHasSequence() && ( node.getNodeData().getSequence().getAccession() != null )
                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() )
                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() )
@@ -184,69 +174,84 @@ public final class SequenceDataRetriver implements Runnable {
                 db = Db.EMBL;
             }
             else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
-                if ( ( query = UniProtWsTools.parseUniProtAccessor( node.getName() ) ) != null ) {
+                if ( ( query = SequenceDbWsTools.parseUniProtAccessor( node.getName() ) ) != null ) {
                     db = Db.UNIPROT;
                 }
-                else if ( ( query = DatabaseTools.parseGenbankAccessor( node.getName() ) ) != null ) {
-                    db = Db.EMBL;
+                else if ( ( id = SequenceIdParser.parse( node.getName() ) ) != null ) {
+                    if ( id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) {
+                        db = Db.NCBI;
+                    }
+                    else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) {
+                        db = Db.REFSEQ;
+                    }
                 }
             }
+            if ( db == Db.NONE ) {
+                not_found.add( node.getName() );
+            }
+            SequenceDatabaseEntry db_entry = null;
             if ( !ForesterUtil.isEmpty( query ) ) {
-                SequenceDatabaseEntry db_entry = null;
                 if ( db == Db.UNIPROT ) {
                     if ( DEBUG ) {
                         System.out.println( "uniprot: " + query );
                     }
-                    try {
-                        db_entry = UniProtWsTools.obtainUniProtEntry( query, 200 );
-                    }
-                    catch ( final FileNotFoundException e ) {
-                        // Ignore.
-                    }
+                    db_entry = SequenceDbWsTools.obtainUniProtEntry( query, DEFAULT_LINES_TO_RETURN );
                 }
                 if ( ( db == Db.EMBL ) || ( ( db == Db.UNIPROT ) && ( db_entry == null ) ) ) {
                     if ( DEBUG ) {
                         System.out.println( "embl: " + query );
                     }
-                    try {
-                        db_entry = UniProtWsTools.obtainEmblEntry( query, 200 );
-                    }
-                    catch ( final FileNotFoundException e ) {
-                        // Ignore.
-                    }
+                    db_entry = SequenceDbWsTools.obtainEmblEntry( new Identifier( query ), DEFAULT_LINES_TO_RETURN );
                     if ( ( db == Db.UNIPROT ) && ( db_entry != null ) ) {
                         db = Db.EMBL;
                     }
                 }
-                if ( ( db_entry != null ) && !db_entry.isEmpty() ) {
-                    if ( !ForesterUtil.isEmpty( db_entry.getAccession() ) ) {
-                        String type = null;
-                        if ( db == Db.EMBL ) {
-                            type = "embl";
-                        }
-                        else if ( db == Db.UNIPROT ) {
-                            type = "uniprot";
-                        }
-                        seq.setAccession( new Accession( db_entry.getAccession(), type ) );
-                    }
-                    if ( !ForesterUtil.isEmpty( db_entry.getSequenceName() ) ) {
-                        seq.setName( db_entry.getSequenceName() );
+            }
+            else if ( ( db == Db.REFSEQ ) && ( id != null ) ) {
+                db_entry = SequenceDbWsTools.obtainRefSeqEntryFromEmbl( id, DEFAULT_LINES_TO_RETURN );
+            }
+            else if ( ( db == Db.NCBI ) && ( id != null ) ) {
+                db_entry = SequenceDbWsTools.obtainEmblEntry( id, DEFAULT_LINES_TO_RETURN );
+            }
+            if ( ( db_entry != null ) && !db_entry.isEmpty() ) {
+                if ( !ForesterUtil.isEmpty( db_entry.getAccession() ) ) {
+                    String type = null;
+                    if ( db == Db.EMBL ) {
+                        type = "embl";
                     }
-                    if ( !ForesterUtil.isEmpty( db_entry.getSequenceSymbol() ) ) {
-                        seq.setSymbol( db_entry.getSequenceSymbol() );
+                    else if ( db == Db.UNIPROT ) {
+                        type = "uniprot";
                     }
-                    if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyScientificName() ) ) {
-                        tax.setScientificName( db_entry.getTaxonomyScientificName() );
+                    else if ( db == Db.NCBI ) {
+                        type = "ncbi";
                     }
-                    if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyIdentifier() ) ) {
-                        tax.setIdentifier( new Identifier( db_entry.getTaxonomyIdentifier(), "uniprot" ) );
+                    else if ( db == Db.REFSEQ ) {
+                        type = "refseq";
                     }
-                    node.getNodeData().setTaxonomy( tax );
-                    node.getNodeData().setSequence( seq );
+                    seq.setAccession( new Accession( db_entry.getAccession(), type ) );
+                }
+                if ( !ForesterUtil.isEmpty( db_entry.getSequenceName() ) ) {
+                    seq.setName( db_entry.getSequenceName() );
                 }
-                else {
-                    not_found.add( node.getName() );
+                if ( !ForesterUtil.isEmpty( db_entry.getSequenceSymbol() ) ) {
+                    seq.setSymbol( db_entry.getSequenceSymbol() );
                 }
+                if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyScientificName() ) ) {
+                    tax.setScientificName( db_entry.getTaxonomyScientificName() );
+                }
+                if ( allow_to_set_taxonomic_data && !ForesterUtil.isEmpty( db_entry.getTaxonomyIdentifier() ) ) {
+                    tax.setIdentifier( new Identifier( db_entry.getTaxonomyIdentifier(), "uniprot" ) );
+                }
+                node.getNodeData().setTaxonomy( tax );
+                node.getNodeData().setSequence( seq );
+            }
+            else if ( db != Db.NONE ) {
+                not_found.add( node.getName() );
+            }
+            try {
+                Thread.sleep( 10 );// Sleep for 10 ms
+            }
+            catch ( final InterruptedException ie ) {
             }
         }
         return not_found;