JAL-2295 don't copy 'Chimera' features back to Chimera
[jalview.git] / src / jalview / ext / rbvi / chimera / ChimeraCommands.java
index a5ee933..533b441 100644 (file)
@@ -49,12 +49,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 +292,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 +400,15 @@ 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)
+              || suppressFeature(type))
       {
         continue;
       }
@@ -403,12 +418,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);
         }
@@ -460,8 +475,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 +501,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;
+  }
+
 }