JAL-2016 FeatureSettingsI now FeatureSettingsModelI
[jalview.git] / src / jalview / gui / SequenceFetcher.java
index 207d2bc..784474e 100755 (executable)
  */
 package jalview.gui;
 
+import jalview.api.FeatureSettingsModelI;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.DBRefEntry;
-import jalview.datamodel.DBRefSource;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
-import jalview.io.FormatAdapter;
-import jalview.io.IdentifyFile;
 import jalview.util.DBRefUtils;
 import jalview.util.MessageManager;
 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
@@ -54,8 +52,6 @@ import javax.swing.JTextArea;
 import javax.swing.SwingConstants;
 import javax.swing.tree.DefaultMutableTreeNode;
 
-import com.stevesoft.pat.Regex;
-
 public class SequenceFetcher extends JPanel implements Runnable
 {
   JLabel dbeg = new JLabel();
@@ -518,6 +514,7 @@ public class SequenceFetcher extends JPanel implements Runnable
       resetDialog();
       return;
     }
+    // TODO: Refactor to GUI independent code and write tests.
     // indicate if successive sources should be merged into one alignment.
     boolean addToLast = false;
     ArrayList<String> aresultq = new ArrayList<String>(), presultTitle = new ArrayList<String>();
@@ -529,6 +526,8 @@ public class SequenceFetcher extends JPanel implements Runnable
             .split(";"));
     Iterator<String> en = Arrays.asList(new String[0]).iterator();
     int nqueries = qries.length;
+
+    FeatureSettingsModelI preferredFeatureColours = null;
     while (proxies.hasNext() && (en.hasNext() || nextfetch.size() > 0))
     {
       if (!en.hasNext() && nextfetch.size() > 0)
@@ -552,7 +551,7 @@ public class SequenceFetcher extends JPanel implements Runnable
                             Integer.valueOf(nqueries).toString(),
                             proxy.getDbName() }), Thread.currentThread()
                         .hashCode());
-        isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
+        isAliSource = proxy.isAlignmentSource();
         if (proxy.getAccessionSeparator() == null)
         {
           while (en.hasNext())
@@ -639,34 +638,19 @@ public class SequenceFetcher extends JPanel implements Runnable
                 DBRefEntry dbr = new DBRefEntry(), found[] = null;
                 dbr.setSource(proxy.getDbSource());
                 dbr.setVersion(null);
-                if (proxy.getAccessionValidator() != null)
-                {
-                  Regex vgr = proxy.getAccessionValidator();
-                  vgr.search(q);
-                  if (vgr.numSubs() > 0)
-                  {
-                    dbr.setAccessionId(vgr.stringMatched(1));
-                  }
-                  else
-                  {
-                    dbr.setAccessionId(vgr.stringMatched());
-                  }
-                }
-                else
-                {
-                  dbr.setAccessionId(q);
-                }
+                String accId = proxy.getAccessionIdFromQuery(q);
+                dbr.setAccessionId(accId);
                 boolean rfound = false;
                 for (int r = 0; r < rs.length; r++)
                 {
-                  if (rs[r] != null
-                          && (found = DBRefUtils.searchRefs(
-                                  rs[r].getDBRef(), dbr)) != null
-                          && found.length > 0)
+                  if (rs[r] != null)
                   {
-                    rfound = true;
-                    rs[r] = null;
-                    continue;
+                    found = DBRefUtils.searchRefs(rs[r].getDBRefs(), accId);
+                    if (found != null && found.length > 0)
+                    {
+                      rfound = true;
+                      rs[r] = null;
+                    }
                   }
                 }
                 if (!rfound)
@@ -710,6 +694,13 @@ public class SequenceFetcher extends JPanel implements Runnable
       // Stack results ready for opening in alignment windows
       if (aresult != null && aresult.size() > 0)
       {
+        FeatureSettingsModelI proxyColourScheme = proxy
+                .getFeatureColourScheme();
+        if (proxyColourScheme != null)
+        {
+          preferredFeatureColours = proxyColourScheme;
+        }
+
         AlignmentI ar = null;
         if (isAliSource)
         {
@@ -760,7 +751,8 @@ public class SequenceFetcher extends JPanel implements Runnable
     // process results
     while (presult.size() > 0)
     {
-      parseResult(presult.remove(0), presultTitle.remove(0), null);
+      parseResult(presult.remove(0), presultTitle.remove(0), null,
+              preferredFeatureColours);
     }
     // only remove visual delay after we finished parsing.
     guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
@@ -788,35 +780,6 @@ public class SequenceFetcher extends JPanel implements Runnable
     resetDialog();
   }
 
-  AlignmentI parseResult(String result, String title)
-  {
-    String format = new IdentifyFile().identify(result, "Paste");
-    AlignmentI sequences = null;
-    if (FormatAdapter.isValidFormat(format))
-    {
-      sequences = null;
-      try
-      {
-        sequences = new FormatAdapter().readFile(result.toString(),
-                "Paste", format);
-      } catch (Exception ex)
-      {
-      }
-
-      if (sequences != null)
-      {
-        return parseResult(sequences, title, format);
-      }
-    }
-    else
-    {
-      showErrorMessage("Error retrieving " + textArea.getText() + " from "
-              + database.getSelectedItem());
-    }
-
-    return null;
-  }
-
   /**
    * 
    * @return a standard title for any results retrieved using the currently
@@ -828,7 +791,8 @@ public class SequenceFetcher extends JPanel implements Runnable
   }
 
   AlignmentI parseResult(AlignmentI al, String title,
-          String currentFileFormat)
+          String currentFileFormat,
+          FeatureSettingsModelI preferredFeatureColours)
   {
 
     if (al != null && al.getHeight() > 0)
@@ -866,6 +830,11 @@ public class SequenceFetcher extends JPanel implements Runnable
 
           }
         }
+
+        if (preferredFeatureColours != null)
+        {
+          af.viewport.applyFeaturesStyle(preferredFeatureColours);
+        }
         Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
                 AlignFrame.DEFAULT_HEIGHT);