phylotastic hackathon at NESCENT 120606
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / SequenceDataRetriver.java
index 0960425..21dcf1f 100644 (file)
@@ -43,18 +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;
+        UNKNOWN, UNIPROT, EMBL, NCBI;
     }
 
     public SequenceDataRetriver( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
@@ -68,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",
@@ -82,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(),
@@ -91,7 +91,7 @@ public final class SequenceDataRetriver implements Runnable {
             return;
         }
         finally {
-            _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
+            end( _mf );
         }
         _treepanel.setTree( _phy );
         _mf.showWhole();
@@ -104,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 ) {
@@ -126,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 ) {
@@ -173,11 +173,22 @@ 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 )
+                    && !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" ) ) ) {
+                query = node.getNodeData().getSequence().getAccession().getValue();
+                db = Db.EMBL;
+            }
             else if ( !ForesterUtil.isEmpty( node.getName() ) ) {
-                query = UniProtWsTools.parseUniProtAccessor( node.getName() );
-                if ( !ForesterUtil.isEmpty( query ) ) {
+                if ( ( query = UniProtWsTools.parseUniProtAccessor( node.getName() ) ) != null ) {
                     db = Db.UNIPROT;
                 }
+                else if ( ( query = SequenceIdParser.parseGenbankAccessor( node.getName() ) ) != null ) {
+                    db = Db.NCBI;
+                }
             }
             if ( !ForesterUtil.isEmpty( query ) ) {
                 SequenceDatabaseEntry db_entry = null;
@@ -192,9 +203,30 @@ public final class SequenceDataRetriver implements Runnable {
                         // Ignore.
                     }
                 }
-                if ( db_entry != null ) {
+                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.
+                    }
+                    if ( ( db == Db.UNIPROT ) && ( db_entry != null ) ) {
+                        db = Db.EMBL;
+                    }
+                }
+                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() );