phylotastic hackathon at NESCENT 120606
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / SequenceDataRetriver.java
index 168a163..21dcf1f 100644 (file)
@@ -43,19 +43,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.util.SequenceIdParser;
 import org.forester.ws.uniprot.DatabaseTools;
 import org.forester.ws.uniprot.SequenceDatabaseEntry;
 import org.forester.ws.uniprot.UniProtWsTools;
 
-public final class SequenceDataRetriver implements Runnable {
+public final class SequenceDataRetriver extends RunnableProcess {
 
     private final Phylogeny            _phy;
     private final MainFrameApplication _mf;
     private final TreePanel            _treepanel;
-    private final static boolean       DEBUG = true;
+    private final static boolean       DEBUG = false;
 
     private enum Db {
-        UNKNOWN, UNIPROT, EMBL;
+        UNKNOWN, UNIPROT, EMBL, NCBI;
     }
 
     public SequenceDataRetriver( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
@@ -69,13 +70,12 @@ public final class SequenceDataRetriver implements Runnable {
     }
 
     private void execute() {
-        _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
+        start( _mf, "sequence data" );
         SortedSet<String> not_found = null;
         try {
             not_found = obtainSeqInformation( _phy );
         }
         catch ( final UnknownHostException e ) {
-            _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
             JOptionPane.showMessageDialog( _mf,
                                            "Could not connect to \"" + getBaseUrl() + "\"",
                                            "Network error during taxonomic information gathering",
@@ -83,7 +83,6 @@ public final class SequenceDataRetriver implements Runnable {
             return;
         }
         catch ( final IOException e ) {
-            _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
             e.printStackTrace();
             JOptionPane.showMessageDialog( _mf,
                                            e.toString(),
@@ -92,7 +91,7 @@ public final class SequenceDataRetriver implements Runnable {
             return;
         }
         finally {
-            _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
+            end( _mf );
         }
         _treepanel.setTree( _phy );
         _mf.showWhole();
@@ -105,12 +104,12 @@ public final class SequenceDataRetriver implements Runnable {
                 max = 20;
             }
             final StringBuffer sb = new StringBuffer();
-            sb.append( "Not all identifiers could be resolved.\n" );
             if ( not_found.size() == 1 ) {
-                sb.append( "The following identifier was not found:\n" );
+                sb.append( "Data for the following sequence identifier was not found:\n" );
             }
             else {
-                sb.append( "The following identifiers were not found (total: " + not_found.size() + "):\n" );
+                sb.append( "Data for the following sequence identifiers was not found (total: " + not_found.size()
+                        + "):\n" );
             }
             int i = 0;
             for( final String string : not_found ) {
@@ -127,7 +126,7 @@ public final class SequenceDataRetriver implements Runnable {
             try {
                 JOptionPane.showMessageDialog( _mf,
                                                sb.toString(),
-                                               "UniProt Sequence Tool Completed",
+                                               "Sequence Tool Completed",
                                                JOptionPane.WARNING_MESSAGE );
             }
             catch ( final Exception e ) {
@@ -174,22 +173,21 @@ public final class SequenceDataRetriver implements Runnable {
                 query = node.getNodeData().getSequence().getAccession().getValue();
                 db = Db.UNIPROT;
             }
-            else if ( node.getNodeData().isHasSequence() && ( node.getNodeData().getSequence().getAccession() != null )
+            else if ( node.getNodeData().isHasSequence()
+                    && ( node.getNodeData().getSequence().getAccession() != null )
                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() )
                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() )
-                    && ( node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "embl" ) 
-                     || node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "ebi" )      
-                    ) ) {
+                    && ( node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "embl" ) || node
+                            .getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "ebi" ) ) ) {
                 query = node.getNodeData().getSequence().getAccession().getValue();
                 db = Db.EMBL;
             }
             else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
-               
-                if (  (query = UniProtWsTools.parseUniProtAccessor( node.getName() ))!=null  ) {
+                if ( ( query = UniProtWsTools.parseUniProtAccessor( node.getName() ) ) != null ) {
                     db = Db.UNIPROT;
                 }
-                else if (( query = DatabaseTools.parseGenbankAccessor( node.getName())) !=null ) {
-                    db = Db.EMBL;
+                else if ( ( query = SequenceIdParser.parseGenbankAccessor( node.getName() ) ) != null ) {
+                    db = Db.NCBI;
                 }
             }
             if ( !ForesterUtil.isEmpty( query ) ) {
@@ -205,7 +203,7 @@ public final class SequenceDataRetriver implements Runnable {
                         // Ignore.
                     }
                 }
-                else if ( db == Db.EMBL ) {
+                if ( ( db == Db.EMBL ) || ( ( db == Db.UNIPROT ) && ( db_entry == null ) ) ) {
                     if ( DEBUG ) {
                         System.out.println( "embl: " + query );
                     }
@@ -215,10 +213,20 @@ public final class SequenceDataRetriver implements Runnable {
                     catch ( final FileNotFoundException e ) {
                         // Ignore.
                     }
+                    if ( ( db == Db.UNIPROT ) && ( db_entry != null ) ) {
+                        db = Db.EMBL;
+                    }
                 }
-                if ( db_entry != null ) {
+                if ( ( db_entry != null ) && !db_entry.isEmpty() ) {
                     if ( !ForesterUtil.isEmpty( db_entry.getAccession() ) ) {
-                        seq.setAccession( new Accession( db_entry.getAccession(), "uniprot" ) );
+                        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() );