JAL-3390 new View menu option to hide hidden regions in structure
[jalview.git] / src / jalview / ext / jmol / JmolCommands.java
index 6bf7010..c981165 100644 (file)
@@ -31,6 +31,7 @@ import jalview.renderer.seqfeatures.FeatureColourFinder;
 import jalview.structure.StructureMapping;
 import jalview.structure.StructureMappingcommandSet;
 import jalview.structure.StructureSelectionManager;
+import jalview.structures.models.AAStructureBindingModel;
 
 import java.awt.Color;
 import java.util.ArrayList;
@@ -55,6 +56,15 @@ public class JmolCommands
    */
   public static StructureMappingcommandSet[] getColourBySequenceCommand(
           StructureSelectionManager ssm, String[] files,
+          AAStructureBindingModel binding, AlignmentViewPanel viewPanel)
+  {
+    SequenceRenderer sr = binding.getSequenceRenderer(viewPanel);
+    SequenceI[][] sequence = binding.getSequence();
+    return getColourBySequenceCommand(ssm, files, sequence, sr, viewPanel);
+  }
+
+  public static StructureMappingcommandSet[] getColourBySequenceCommand(
+          StructureSelectionManager ssm, String[] files,
           SequenceI[][] sequence, SequenceRenderer sr,
           AlignmentViewPanel viewPanel)
   {
@@ -63,21 +73,20 @@ public class JmolCommands
     AlignViewportI viewport = viewPanel.getAlignViewport();
     HiddenColumns cs = viewport.getAlignment().getHiddenColumns();
     AlignmentI al = viewport.getAlignment();
-    List<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
+    List<StructureMappingcommandSet> cset = new ArrayList<>();
 
     for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
     {
       StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
       StringBuffer command = new StringBuffer();
       StructureMappingcommandSet smc;
-      ArrayList<String> str = new ArrayList<String>();
+      ArrayList<String> str = new ArrayList<>();
 
       if (mapping == null || mapping.length < 1)
       {
         continue;
       }
 
-      int lastPos = -1;
       for (int s = 0; s < sequence[pdbfnum].length; s++)
       {
         for (int sp, m = 0; m < mapping.length; m++)
@@ -85,6 +94,7 @@ public class JmolCommands
           if (mapping[m].getSequence() == sequence[pdbfnum][s]
                   && (sp = al.findIndex(sequence[pdbfnum][s])) > -1)
           {
+            int lastPos = StructureMapping.UNASSIGNED_VALUE;
             SequenceI asp = al.getSequenceAt(sp);
             for (int r = 0; r < asp.getLength(); r++)
             {
@@ -95,10 +105,22 @@ public class JmolCommands
               }
               int pos = mapping[m].getPDBResNum(asp.findPosition(r));
 
-              if (pos < 1 || pos == lastPos)
+              if (pos == lastPos)
               {
                 continue;
               }
+              if (pos == StructureMapping.UNASSIGNED_VALUE)
+              {
+                // terminate current colour op
+                if (command.length() > 0
+                        && command.charAt(command.length() - 1) != ';')
+                {
+                  command.append(";");
+                }
+                // reset lastPos
+                lastPos = StructureMapping.UNASSIGNED_VALUE;
+                continue;
+              }
 
               lastPos = pos;
 
@@ -128,7 +150,12 @@ public class JmolCommands
               // TODO: deal with case when buffer is too large for Jmol to parse
               // - execute command and flush
 
-              command.append(";");
+              if (command.length() > 0
+                      && command.charAt(command.length() - 1) != ';')
+              {
+                command.append(";");
+              }
+
               if (command.length() > 51200)
               {
                 // add another chunk