Merge branch 'develop' into JAL-1705_trialMerge
[jalview.git] / src / jalview / ws / DBRefFetcher.java
index f5789ea..dff1b98 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -28,9 +28,9 @@ import jalview.datamodel.DBRefSource;
 import jalview.datamodel.Mapping;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
-import jalview.gui.AlignFrame;
 import jalview.gui.CutAndPasteTransfer;
 import jalview.gui.Desktop;
+import jalview.gui.FeatureSettings;
 import jalview.gui.IProgressIndicator;
 import jalview.gui.OOMWarning;
 import jalview.util.MessageManager;
@@ -57,7 +57,7 @@ public class DBRefFetcher implements Runnable
 {
   SequenceI[] dataset;
 
-  IProgressIndicator af;
+  IProgressIndicator progressWindow;
 
   CutAndPasteTransfer output = new CutAndPasteTransfer();
 
@@ -91,47 +91,43 @@ public class DBRefFetcher implements Runnable
   }
 
   /**
-   * Creates a new SequenceFeatureFetcher object and fetches from the currently
-   * selected set of databases.
+   * Creates a new DBRefFetcher object and fetches from the currently selected
+   * set of databases, if this is null then it fetches based on feature settings
    * 
    * @param seqs
-   *          fetch references for these sequences
-   * @param af
-   *          the parent alignframe for progress bar monitoring.
-   */
-  public DBRefFetcher(SequenceI[] seqs, AlignFrame af)
-  {
-    this(seqs, af, null);
-  }
-
-  /**
-   * Creates a new SequenceFeatureFetcher object and fetches from the currently
-   * selected set of databases.
-   * 
-   * @param seqs
-   *          fetch references for these sequences
-   * @param af
-   *          the parent alignframe for progress bar monitoring.
+   *          fetch references for these SequenceI array
+   * @param progressIndicatorFrame
+   *          the frame for progress bar monitoring
    * @param sources
-   *          array of database source strings to query references from
+   *          array of DbSourceProxy to query references form
+   * @param featureSettings
+   *          FeatureSettings to get alternative DbSourceProxy from
+   * @param isNucleotide
+   *          indicates if the array of SequenceI are Nucleotides or not
    */
-  public DBRefFetcher(SequenceI[] seqs, AlignFrame af,
-          DbSourceProxy[] sources)
+  public DBRefFetcher(SequenceI[] seqs,
+          IProgressIndicator progressIndicatorFrame,
+          DbSourceProxy[] sources, FeatureSettings featureSettings, boolean isNucleotide)
   {
-    this.af = af;
+    this.progressWindow = progressIndicatorFrame;
     alseqs = new SequenceI[seqs.length];
     SequenceI[] ds = new SequenceI[seqs.length];
     for (int i = 0; i < seqs.length; i++)
     {
       alseqs[i] = seqs[i];
       if (seqs[i].getDatasetSequence() != null)
+      {
         ds[i] = seqs[i].getDatasetSequence();
+      }
       else
+      {
         ds[i] = seqs[i];
+      }
     }
     this.dataset = ds;
     // TODO Jalview 2.5 lots of this code should be in the gui package!
-    sfetcher = jalview.gui.SequenceFetcher.getSequenceFetcherSingleton(af);
+    sfetcher = jalview.gui.SequenceFetcher
+            .getSequenceFetcherSingleton(progressIndicatorFrame);
     // set default behaviour for transferring excess sequence data to the
     // dataset
     trimDsSeqs = Cache.getDefault("TRIM_FETCHED_DATASET_SEQS", true);
@@ -141,7 +137,7 @@ public class DBRefFetcher implements Runnable
       String[] defdb = null, otherdb = sfetcher
               .getDbInstances(jalview.ws.dbsources.das.datamodel.DasSequenceSource.class);
       List<DbSourceProxy> selsources = new ArrayList<DbSourceProxy>();
-      Vector dasselsrc = (af.featureSettings != null) ? af.featureSettings
+      Vector dasselsrc = (featureSettings != null) ? featureSettings
               .getSelectedSources() : new jalview.gui.DasSourceBrowser()
               .getSelectedSources();
       Enumeration<jalviewSourceI> en = dasselsrc.elements();
@@ -159,7 +155,7 @@ public class DBRefFetcher implements Runnable
         }
       }
       // select appropriate databases based on alignFrame context.
-      if (af.getViewport().getAlignment().isNucleotide())
+      if (isNucleotide)
       {
         defdb = DBRefSource.DNACODINGDBS;
       }
@@ -279,15 +275,23 @@ public class DBRefFetcher implements Runnable
   /**
    * DOCUMENT ME!
    */
+  @Override
   public void run()
   {
     if (dbSources == null)
     {
-      throw new Error(MessageManager.getString("error.implementation_error_must_init_dbsources"));
+      throw new Error(
+              MessageManager
+                      .getString("error.implementation_error_must_init_dbsources"));
     }
     running = true;
     long startTime = System.currentTimeMillis();
-    af.setProgressBar(MessageManager.getString("status.fetching_db_refs"), startTime);
+    if (progressWindow != null)
+    {
+      progressWindow.setProgressBar(
+              MessageManager.getString("status.fetching_db_refs"),
+            startTime);
+    }
     try
     {
       if (Cache.getDefault("DBREFFETCH_USEPICR", false))
@@ -328,16 +332,8 @@ public class DBRefFetcher implements Runnable
         // TODO: introduce multithread multisource queries and logic to remove a
         // query from other sources if any source for a database returns a
         // record
-        if (dbsource.getDbSourceProperties().containsKey(
-                DBRefSource.MULTIACC))
-        {
-          maxqlen = ((Integer) dbsource.getDbSourceProperties().get(
-                  DBRefSource.MULTIACC)).intValue();
-        }
-        else
-        {
-          maxqlen = 1;
-        }
+        maxqlen = dbsource.getMaximumQueryCount();
+
         while (queries.size() > 0 || seqIndex < currSeqs.length)
         {
           if (queries.size() > 0)
@@ -393,8 +389,8 @@ public class DBRefFetcher implements Runnable
             {
               SequenceI sequence = dataset[seqIndex];
               DBRefEntry[] uprefs = jalview.util.DBRefUtils.selectRefs(
-                      sequence.getDBRef(), new String[]
-                      { dbsource.getDbSource() }); // jalview.datamodel.DBRefSource.UNIPROT
+                      sequence.getDBRefs(),
+                      new String[] { dbsource.getDbSource() }); // jalview.datamodel.DBRefSource.UNIPROT
               // });
               // check for existing dbrefs to use
               if (uprefs != null && uprefs.length > 0)
@@ -472,11 +468,14 @@ public class DBRefFetcher implements Runnable
       // of the viewed sequence
 
     }
+    if (progressWindow != null)
+    {
+      progressWindow.setProgressBar(
+              MessageManager.getString("label.dbref_search_completed"),
+              startTime);
+      // promptBeforeBlast();
 
-    af.setProgressBar(
-            MessageManager.getString("label.dbref_search_completed"),
-            startTime);
-    // promptBeforeBlast();
+    }
 
     running = false;
 
@@ -517,8 +516,7 @@ public class DBRefFetcher implements Runnable
       Vector sequenceMatches = new Vector();
       // look for corresponding accession ids
       DBRefEntry[] entryRefs = jalview.util.DBRefUtils.selectRefs(
-              entry.getDBRef(), new String[]
-              { dbSource });
+              entry.getDBRefs(), new String[] { dbSource });
       if (entryRefs == null)
       {
         System.err
@@ -585,8 +583,8 @@ public class DBRefFetcher implements Runnable
         // no existing references
         // TODO: test for legacy where uniprot or EMBL refs exist but no
         // mappings are made (but content matches retrieved set)
-        boolean updateRefFrame = sequence.getDBRef() == null
-                || sequence.getDBRef().length == 0;
+        boolean updateRefFrame = sequence.getDBRefs() == null
+                || sequence.getDBRefs().length == 0;
         // TODO:
         // verify sequence against the entry sequence
 
@@ -594,9 +592,9 @@ public class DBRefFetcher implements Runnable
                 sequence.getSequenceAsString()).toUpperCase();
 
         int absStart = entrySeq.indexOf(nonGapped);
-        int mapStart = entry.getStart();
-        jalview.datamodel.Mapping mp;
+        Mapping mp;
 
+        final int sequenceStart = sequence.getStart();
         if (absStart == -1)
         {
           // Is local sequence contained in dataset sequence?
@@ -616,12 +614,10 @@ public class DBRefFetcher implements Runnable
           // absStart = 0;
           // create valid mapping between matching region of local sequence and
           // the mapped sequence
-          mp = new Mapping(null, new int[]
-          { sequence.getStart() + absStart,
-              sequence.getStart() + absStart + entrySeq.length() - 1 },
-                  new int[]
-                  { entry.getStart(),
-                      entry.getStart() + entrySeq.length() - 1 }, 1, 1);
+          mp = new Mapping(null, new int[] { sequenceStart + absStart,
+              sequenceStart + absStart + entrySeq.length() - 1 }, new int[]
+          { entry.getStart(), entry.getStart() + entrySeq.length() - 1 },
+                  1, 1);
           updateRefFrame = false; // mapping is based on current start/end so
           // don't modify start and end
         }
@@ -644,7 +640,7 @@ public class DBRefFetcher implements Runnable
             if (sequence.getSequenceFeatures() != null)
             {
               SequenceFeature[] sf = sequence.getSequenceFeatures();
-              int start = sequence.getStart();
+              int start = sequenceStart;
               int end = sequence.getEnd();
               int startShift = 1 - absStart - start; // how much the features
                                                      // are
@@ -666,8 +662,8 @@ public class DBRefFetcher implements Runnable
                 + " from " + dbSource + " sequence : " + entry.getName());
         sequence.transferAnnotation(entry, mp);
         // unknownSequences.remove(sequence);
-        int absEnd = absStart + nonGapped.length();
-        absStart += 1;
+        absStart += entry.getStart();
+        int absEnd = absStart + nonGapped.length() - 1;
         if (!trimDatasetSeqs)
         {
           // insert full length sequence from record
@@ -733,7 +729,7 @@ public class DBRefFetcher implements Runnable
     for (int i = 0; sequencesArray != null && i < sequencesArray.length; i++)
     {
       nseq.addElement(sequencesArray[i]);
-      DBRefEntry dbr[] = sequencesArray[i].getDBRef();
+      DBRefEntry dbr[] = sequencesArray[i].getDBRefs();
       jalview.datamodel.Mapping map = null;
       for (int r = 0; (dbr != null) && r < dbr.length; r++)
       {