Merge branch 'develop' into features/JAL-2295setChimeraAttributes
[jalview.git] / src / jalview / ext / rbvi / chimera / ChimeraCommands.java
index a5ee933..07c0015 100644 (file)
@@ -38,8 +38,6 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
-import MCview.PDBChain;
-
 /**
  * Routines for generating Chimera commands for Jalview/Chimera binding
  * 
@@ -49,12 +47,18 @@ import MCview.PDBChain;
 public class ChimeraCommands
 {
 
+  public static final String NAMESPACE_PREFIX = "jv_";
+
   /**
-   * utility to construct the commands to colour chains by the given alignment
-   * for passing to Chimera
-   * 
-   * @returns Object[] { Object[] { <model being coloured>,
+   * Constructs Chimera commands to colour residues as per the Jalview alignment
    * 
+   * @param ssm
+   * @param files
+   * @param sequence
+   * @param sr
+   * @param fr
+   * @param alignment
+   * @return
    */
   public static StructureMappingcommandSet getColourBySequenceCommand(
           StructureSelectionManager ssm, String[] files,
@@ -286,8 +290,9 @@ public class ChimeraCommands
   }
 
   /**
-   * Constructs and returns a set of Chimera commands to set attributes on
-   * residues corresponding to features in Jalview
+   * Constructs and returns Chimera commands to set attributes on residues
+   * corresponding to features in Jalview. Attribute names are the Jalview
+   * feature type, with a "jv_" prefix.
    * 
    * @param ssm
    * @param files
@@ -393,7 +398,14 @@ public class ChimeraCommands
     for (SequenceFeature sf : sfs)
     {
       String type = sf.getType();
-      if (!visibleFeatures.contains(type) || suppressFeature(type))
+
+      /*
+       * Only copy visible features, don't copy any which originated
+       * from Chimera, and suppress uninteresting ones (e.g. RESNUM)
+       */
+      boolean isFromViewer = JalviewChimeraBinding.CHIMERA_FEATURE_GROUP
+              .equals(sf.getFeatureGroup());
+      if (isFromViewer || !visibleFeatures.contains(type))
       {
         continue;
       }
@@ -403,12 +415,12 @@ public class ChimeraCommands
       if (!mappedRanges.isEmpty())
       {
         String value = sf.getDescription();
-        if (value == null)
+        if (value == null || value.length() == 0)
         {
           value = type;
         }
         float score = sf.getScore();
-        if (score != 0f && score != Float.NaN)
+        if (score != 0f && !Float.isNaN(score))
         {
           value = Float.toString(score);
         }
@@ -428,18 +440,6 @@ public class ChimeraCommands
   }
 
   /**
-   * Answers true if the feature type is one we don't wish to propagate to
-   * Chimera - for now, RESNUM
-   * 
-   * @param type
-   * @return
-   */
-  static boolean suppressFeature(String type)
-  {
-    return PDBChain.RESNUM_FEATURE.equals(type);
-  }
-
-  /**
    * Traverse the map of features/values/models/chains/positions to construct a
    * list of 'setattr' commands (one per distinct feature type and value).
    * <p>
@@ -460,8 +460,7 @@ public class ChimeraCommands
     List<String> commands = new ArrayList<String>();
     for (String featureType : featureMap.keySet())
     {
-      String attributeName = "jv_"
-              + featureType.replace(" ", "_").replace("-", "_");
+      String attributeName = makeAttributeName(featureType);
 
       /*
        * clear down existing attributes for this feature
@@ -487,4 +486,38 @@ public class ChimeraCommands
     return commands;
   }
 
+  /**
+   * Makes a prefixed and valid Chimera attribute name. A jv_ prefix is applied
+   * for a 'Jalview' namespace, and any non-alphanumeric character is converted
+   * to an underscore.
+   * 
+   * @param featureType
+   * @return <pre>
+   * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/setattr.html
+   * </pre>
+   */
+  protected static String makeAttributeName(String featureType)
+  {
+    StringBuilder sb = new StringBuilder();
+    if (featureType != null)
+    {
+      for (char c : featureType.toCharArray())
+      {
+        sb.append(Character.isLetterOrDigit(c) ? c : '_');
+      }
+    }
+    String attName = NAMESPACE_PREFIX + sb.toString();
+
+    /*
+     * Chimera treats an attribute name ending in 'color' as colour-valued;
+     * Jalview doesn't, so prevent this by appending an underscore
+     */
+    if (attName.toUpperCase().endsWith("COLOR"))
+    {
+      attName += "_";
+    }
+
+    return attName;
+  }
+
 }