JAL-3390 pull up of getShownResidues() to AAStructureBindingModel
[jalview.git] / src / jalview / ext / rbvi / chimera / JalviewChimeraBinding.java
index 216320f..44bcbe4 100644 (file)
@@ -36,7 +36,6 @@ import jalview.io.DataSourceType;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ResidueProperties;
 import jalview.structure.AtomSpec;
-import jalview.structure.StructureMapping;
 import jalview.structure.StructureMappingcommandSet;
 import jalview.structure.StructureSelectionManager;
 import jalview.structures.models.AAStructureBindingModel;
@@ -52,7 +51,6 @@ import java.util.ArrayList;
 import java.util.BitSet;
 import java.util.Collections;
 import java.util.Hashtable;
-import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -464,7 +462,8 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
          * @see
          * https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
          */
-        command.append("match ").append(getModelSpec(pdbfnum)).append(":");
+        command.append("match ").append(getModelSpec(pdbfnum))
+                .append(":");
         command.append(selcom[pdbfnum]);
         command.append("@").append(
                 structures[pdbfnum].isRna ? PHOSPHORUS : ALPHACARBON);
@@ -541,7 +540,8 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
    * @param pdbfnum
    * @return
    */
-  protected String getModelSpec(int pdbfnum)
+  @Override
+  public String getModelSpec(int pdbfnum)
   {
     if (pdbfnum < 0 || pdbfnum >= getPdbCount())
     {
@@ -556,7 +556,8 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
      */
     List<ChimeraModel> maps = chimeraMaps.get(getStructureFiles()[pdbfnum]);
     boolean hasSubModels = maps != null && maps.size() > 1;
-    return "#" + String.valueOf(pdbfnum) + (hasSubModels ? ".1" : "");
+    String spec = "#" + String.valueOf(pdbfnum);
+    return hasSubModels ? spec + ".1" : spec;
   }
 
   /**
@@ -673,8 +674,8 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
   protected StructureMappingcommandSet[] getColourBySequenceCommands(
           String[] files, AlignmentViewPanel viewPanel)
   {
-    return ChimeraCommands.getColourBySequenceCommand(getSsm(), files,
-            this, viewPanel);
+    return ChimeraCommands.getColourBySequenceCommand(files, viewPanel,
+            this);
   }
 
   /**
@@ -1086,8 +1087,7 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
     }
 
     StructureMappingcommandSet commandSet = ChimeraCommands
-            .getSetAttributeCommandsForFeatures(getSsm(), files,
-                    getSequence(), avp);
+            .getSetAttributeCommandsForFeatures(avp, this);
     String[] commands = commandSet.commands;
     if (commands.length > 10)
     {
@@ -1291,7 +1291,10 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
   {
     StringBuilder cmd = new StringBuilder(128);
     cmd.append("~display; ~ribbon;");
-    String atomSpec = getMappedResidues(av);
+
+    AtomSpecModel model = getShownResidues(av);
+    String atomSpec = ChimeraCommands.getAtomSpec(model, this);
+    
     cmd.append("ribbon ").append(atomSpec);
     if (!isShowAlignmentOnly())
     {
@@ -1303,95 +1306,4 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
     }
     sendChimeraCommand(cmd.toString(), false);
   }
-
-  /**
-   * Builds a Chimera atomSpec of residues mapped from sequences, of the format
-   * (#model:residues.chain)
-   * 
-   * <pre>
-   * #0:2-94.A | #1:1-93.C | #2:1-93.A
-   * </pre>
-   * 
-   * Only residues visible in the alignment are included, that is, hidden columns
-   * and sequences are excluded.
-   * 
-   * @param av
-   * @return
-   */
-  private String getMappedResidues(AlignViewportI av)
-  {
-    AlignmentI alignment = av.getAlignment();
-    final int width = alignment.getWidth();
-  
-    String[] files = getStructureFiles();
-
-    StringBuilder atomSpec = new StringBuilder(256);
-
-    for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
-    {
-      StructureMapping[] mappings = getSsm().getMapping(files[pdbfnum]);
-
-      /*
-       * Find the first mapped sequence (if any) for this PDB entry which is in
-       * the alignment
-       */
-      final int seqCountForPdbFile = getSequence()[pdbfnum].length;
-      for (int s = 0; s < seqCountForPdbFile; s++)
-      {
-        for (StructureMapping mapping : mappings)
-        {
-          final SequenceI theSequence = getSequence()[pdbfnum][s];
-          if (mapping.getSequence() == theSequence
-                  && alignment.findIndex(theSequence) > -1)
-          {
-            String chainCd = mapping.getChain();
-            if (!isShowChain(mapping.getPdbId(), chainCd))
-            {
-              continue;
-            }
-            Iterator<int[]> visible;
-            if (isShowAlignmentOnly() && isHideHiddenRegions())
-            {
-              visible = alignment.getHiddenColumns()
-                    .getVisContigsIterator(0, width, true);
-            }
-            else
-            {
-              visible = Collections.singletonList(new int[] { 0, width })
-                      .iterator();
-            }
-            while (visible.hasNext())
-            {
-              int[] visibleRegion = visible.next();
-              int seqStartPos = theSequence.findPosition(visibleRegion[0]);
-              int seqEndPos = theSequence.findPosition(visibleRegion[1]);
-              List<int[]> residueRanges = mapping
-                      .getPDBResNumRanges(seqStartPos, seqEndPos);
-              if (!residueRanges.isEmpty())
-              {
-                if (atomSpec.length() > 0)
-                {
-                  atomSpec.append("| ");
-                }
-                atomSpec.append(getModelSpec(pdbfnum)).append(":");
-                boolean first = true;
-                for (int[] range : residueRanges)
-                {
-                  if (!first)
-                  {
-                    atomSpec.append(",");
-                  }
-                  first = false;
-                  atomSpec.append(range[0]).append("-").append(range[1]);
-                  atomSpec.append(".").append(chainCd);
-                }
-              }
-            }
-          }
-        }
-      }
-    }
-
-    return atomSpec.toString();
-  }
 }