JAL-3982 highlight any 3D structure mappings for positions mapped via CDS/transcript...
authorJim Procter <j.procter@dundee.ac.uk>
Fri, 25 Mar 2022 16:16:04 +0000 (16:16 +0000)
committerJim Procter <j.procter@dundee.ac.uk>
Fri, 25 Mar 2022 16:16:04 +0000 (16:16 +0000)
src/jalview/structure/StructureSelectionManager.java

index c8a846c..5cf6385 100644 (file)
@@ -44,6 +44,7 @@ import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SearchResultMatchI;
 import jalview.datamodel.SearchResults;
 import jalview.datamodel.SearchResultsI;
 import jalview.datamodel.SequenceI;
@@ -991,6 +992,19 @@ public class StructureSelectionManager
     {
       seqPos = seq.findPosition(indexpos);
     }
+    
+    // precompute so we can also relay structure highlights
+    if (results == null)
+    {
+      results = MappingUtils.buildSearchResults(seq, seqPos,
+              seqmappings);
+    }
+    if (handlingVamsasMo)
+    {
+      results.addResult(seq, seqPos, seqPos);
+
+    }
+
     for (int i = 0; i < listeners.size(); i++)
     {
       Object listener = listeners.elementAt(i);
@@ -1002,7 +1016,14 @@ public class StructureSelectionManager
       }
       if (listener instanceof StructureListener)
       {
-        highlightStructure((StructureListener) listener, seq, seqPos);
+        StructureListener sl = (StructureListener) listener;
+        // TODO: consider merging highlightStructure variants second call
+        // functionally same as first if seq/seqPos is part of the searchResults
+        if (highlightStructure(sl, seq, seqPos)==0 && relaySeqMappings)
+        {
+          // structure highlights for mapped sequences
+          highlightStructure(sl,results);
+        }
       }
       else
       {
@@ -1014,16 +1035,6 @@ public class StructureSelectionManager
           {
             if (relaySeqMappings)
             {
-              if (results == null)
-              {
-                results = MappingUtils.buildSearchResults(seq, seqPos,
-                        seqmappings);
-              }
-              if (handlingVamsasMo)
-              {
-                results.addResult(seq, seqPos, seqPos);
-
-              }
               if (!results.isEmpty())
               {
                 seqListener.highlightSequence(results);
@@ -1046,19 +1057,58 @@ public class StructureSelectionManager
   }
 
   /**
+   * highlights positions in a structure viewer corresponding to one or more positions on sequences
+   * @param sl
+   * @param searchResults
+   * @return 0 or number of structure regions highlighted
+   */
+  public int highlightStructure(StructureListener sl, SearchResultsI searchResults)
+  {
+    int atomNo;
+    List<AtomSpec> atoms = new ArrayList<>();
+
+    for (SearchResultMatchI sr: searchResults.getResults())
+    {
+      SequenceI seq = sr.getSequence();
+      for (StructureMapping sm : mappings)
+      {
+        if (sm.sequence == seq || sm.sequence == seq.getDatasetSequence()
+                || (sm.sequence.getDatasetSequence() != null
+                        && (sm.sequence.getDatasetSequence() == seq
+                                || sm.sequence.getDatasetSequence() == seq
+                                        .getDatasetSequence())))
+        {
+          for (int index=sr.getStart();index<=sr.getEnd();index++)
+          {
+            atomNo = sm.getAtomNum(index);
+
+            if (atomNo > 0)
+            {
+              atoms.add(new AtomSpec(sm.pdbfile, sm.pdbchain,
+                      sm.getPDBResNum(index), atomNo));
+            }
+          }
+        }
+      }
+    }
+    sl.highlightAtoms(atoms);
+    return atoms.size();
+  }
+  /**
    * Send suitable messages to a StructureListener to highlight atoms
    * corresponding to the given sequence position(s)
    * 
    * @param sl
    * @param seq
    * @param positions
+   * @return 0 if no atoms identified for position(s)
    */
-  public void highlightStructure(StructureListener sl, SequenceI seq,
+  public int highlightStructure(StructureListener sl, SequenceI seq,
           int... positions)
   {
     if (!sl.isListeningFor(seq))
     {
-      return;
+      return 0;
     }
     int atomNo;
     List<AtomSpec> atoms = new ArrayList<>();
@@ -1081,6 +1131,7 @@ public class StructureSelectionManager
       }
     }
     sl.highlightAtoms(atoms);
+    return atoms.size();
   }
 
   /**