JAL-3691 automatic insertion of Locale.ROOT to toUpperCase() and toLowerCase() and...
[jalview.git] / src / jalview / ws / DBRefFetcher.java
index b1c987e..9420465 100644 (file)
  */
 package jalview.ws;
 
+import java.util.Locale;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.Vector;
+
 import jalview.analysis.AlignSeq;
+import jalview.api.FeatureSettingsModelI;
 import jalview.bin.Cache;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.DBRefEntry;
@@ -35,15 +48,6 @@ import jalview.gui.OOMWarning;
 import jalview.util.DBRefUtils;
 import jalview.util.MessageManager;
 import jalview.ws.seqfetcher.DbSourceProxy;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.List;
-import java.util.StringTokenizer;
-import java.util.Vector;
-
 import uk.ac.ebi.picr.model.UPEntry;
 import uk.ac.ebi.www.picr.AccessionMappingService.AccessionMapperServiceLocator;
 
@@ -71,8 +75,6 @@ public class DBRefFetcher implements Runnable
 
   CutAndPasteTransfer output = new CutAndPasteTransfer();
 
-  boolean running = false;
-
   /**
    * picr client instance
    */
@@ -222,24 +224,13 @@ public class DBRefFetcher implements Runnable
    */
   public void fetchDBRefs(boolean waitTillFinished)
   {
-    // TODO can we not simply write
-    // if (waitTillFinished) { run(); } else { new Thread(this).start(); }
-
-    Thread thread = new Thread(this);
-    thread.start();
-    running = true;
-
     if (waitTillFinished)
     {
-      while (running)
-      {
-        try
-        {
-          Thread.sleep(500);
-        } catch (Exception ex)
-        {
-        }
-      }
+      run();
+    }
+    else
+    {
+      new Thread(this).start();
     }
   }
 
@@ -254,7 +245,7 @@ public class DBRefFetcher implements Runnable
    */
   void addSeqId(SequenceI seq, String key)
   {
-    key = key.toUpperCase();
+    key = key.toUpperCase(Locale.ROOT);
 
     Vector<SequenceI> seqs;
     if (seqRefs.containsKey(key))
@@ -292,7 +283,6 @@ public class DBRefFetcher implements Runnable
       throw new Error(MessageManager
               .getString("error.implementation_error_must_init_dbsources"));
     }
-    running = true;
     long startTime = System.currentTimeMillis();
     if (progressWindow != null)
     {
@@ -317,6 +307,9 @@ public class DBRefFetcher implements Runnable
             Arrays.asList(dataset));
     List<String> warningMessages = new ArrayList<>();
 
+    // clear any old feature display settings recorded from past sessions
+    featureDisplaySettings = null;
+
     int db = 0;
     while (sdataset.size() > 0 && db < dbSources.length)
     {
@@ -383,7 +376,7 @@ public class DBRefFetcher implements Runnable
           }
           if (retrieved != null)
           {
-            transferReferences(sdataset, dbsource.getDbSource(), retrieved,
+            transferReferences(sdataset, dbsource, retrieved,
                     trimDsSeqs, warningMessages);
           }
         }
@@ -406,7 +399,7 @@ public class DBRefFetcher implements Runnable
                DBRefEntry upref = uprefs.get(j);
                 addSeqId(sequence, upref.getAccessionId());
                 queries.addElement(
-                        upref.getAccessionId().toUpperCase());
+                        upref.getAccessionId().toUpperCase(Locale.ROOT));
               }
             }
             else
@@ -445,7 +438,7 @@ public class DBRefFetcher implements Runnable
                           "Validated ID against PICR... (for what its worth):"
                                   + token);
                   addSeqId(sequence, token);
-                  queries.addElement(token.toUpperCase());
+                  queries.addElement(token.toUpperCase(Locale.ROOT));
                 }
                 else
                 {
@@ -453,7 +446,7 @@ public class DBRefFetcher implements Runnable
                   // System.out.println("Not querying source with
                   // token="+token+"\n");
                   addSeqId(sequence, token);
-                  queries.addElement(token.toUpperCase());
+                  queries.addElement(token.toUpperCase(Locale.ROOT));
                 }
               }
             }
@@ -492,7 +485,6 @@ public class DBRefFetcher implements Runnable
     {
       listener.finished();
     }
-    running = false;
   }
 
   /**
@@ -512,7 +504,8 @@ public class DBRefFetcher implements Runnable
    * @param warningMessages
    *          a list of messages to add to
    */
-  boolean transferReferences(Vector<SequenceI> sdataset, String dbSource,
+  boolean transferReferences(Vector<SequenceI> sdataset,
+          DbSourceProxy dbSourceProxy,
           AlignmentI retrievedAl, boolean trimDatasetSeqs,
           List<String> warningMessages)
   {
@@ -522,6 +515,7 @@ public class DBRefFetcher implements Runnable
       return false;
     }
 
+    String dbSource = dbSourceProxy.getDbName();
     boolean modified = false;
     SequenceI[] retrieved = recoverDbSequences(
             retrievedAl.getSequencesArray());
@@ -548,7 +542,7 @@ public class DBRefFetcher implements Runnable
        DBRefEntry ref = entryRefs.get(j);
         String accessionId = ref.getAccessionId();
         // match up on accessionId
-        if (seqRefs.containsKey(accessionId.toUpperCase()))
+        if (seqRefs.containsKey(accessionId.toUpperCase(Locale.ROOT)))
         {
           Vector<SequenceI> seqs = seqRefs.get(accessionId);
           for (int jj = 0; jj < seqs.size(); jj++)
@@ -593,10 +587,14 @@ public class DBRefFetcher implements Runnable
        * seqs.elementAt(jj); if (!sequenceMatches.contains(sequence)) {
        * sequenceMatches.addElement(sequence); } } } }
        */
+      if (sequenceMatches.size() > 0)
+      {
+        addFeatureSettings(dbSourceProxy);
+      }
       // sequenceMatches now contains the set of all sequences associated with
       // the returned db record
       final String retrievedSeqString = retrievedSeq.getSequenceAsString();
-      String entrySeq = retrievedSeqString.toUpperCase();
+      String entrySeq = retrievedSeqString.toUpperCase(Locale.ROOT);
       for (int m = 0; m < sequenceMatches.size(); m++)
       {
         sequence = sequenceMatches.elementAt(m);
@@ -615,7 +613,7 @@ public class DBRefFetcher implements Runnable
         boolean remoteEnclosesLocal = false;
         String nonGapped = AlignSeq
                 .extractGaps("-. ", sequence.getSequenceAsString())
-                .toUpperCase();
+                .toUpperCase(Locale.ROOT);
         int absStart = entrySeq.indexOf(nonGapped);
         if (absStart == -1)
         {
@@ -735,10 +733,10 @@ public class DBRefFetcher implements Runnable
               String ngAlsq = AlignSeq
                       .extractGaps("-. ",
                               alseqs[alsq].getSequenceAsString())
-                      .toUpperCase();
+                      .toUpperCase(Locale.ROOT);
               int oldstrt = alseqs[alsq].getStart();
               alseqs[alsq].setStart(sequence.getSequenceAsString()
-                      .toUpperCase().indexOf(ngAlsq) + sequence.getStart());
+                      .toUpperCase(Locale.ROOT).indexOf(ngAlsq) + sequence.getStart());
               if (oldstrt != alseqs[alsq].getStart())
               {
                 alseqs[alsq].setEnd(
@@ -761,6 +759,33 @@ public class DBRefFetcher implements Runnable
     return modified;
   }
 
+  Map<String, FeatureSettingsModelI> featureDisplaySettings = null;
+
+  private void addFeatureSettings(DbSourceProxy dbSourceProxy)
+  {
+    FeatureSettingsModelI fsettings = dbSourceProxy
+            .getFeatureColourScheme();
+    if (fsettings != null)
+    {
+      if (featureDisplaySettings == null)
+      {
+        featureDisplaySettings = new HashMap<>();
+      }
+      featureDisplaySettings.put(dbSourceProxy.getDbName(), fsettings);
+    }
+  }
+
+  /**
+   * 
+   * @return any feature settings associated with sources that have provided sequences
+   */
+  public List<FeatureSettingsModelI>getFeatureSettingsModels()
+  {
+    return featureDisplaySettings == null
+            ? Arrays.asList(new FeatureSettingsModelI[0])
+            : Arrays.asList(featureDisplaySettings.values()
+                    .toArray(new FeatureSettingsModelI[1]));
+  }
   /**
    * Adds the message to the list unless it already contains it
    *