Merge branch 'develop' into update/JAL-3949_abstracted_logging_for_jalviewjs_and_sanity
[jalview.git] / src / jalview / util / MappingUtils.java
index 298c006..76967de 100644 (file)
@@ -41,6 +41,7 @@ import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentOrder;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.HiddenColumns;
+import jalview.datamodel.Mapping;
 import jalview.datamodel.SearchResultMatchI;
 import jalview.datamodel.SearchResults;
 import jalview.datamodel.SearchResultsI;
@@ -363,49 +364,55 @@ public final class MappingUtils
        */
       int startResiduePos = selected.findPosition(firstUngappedPos);
       int endResiduePos = selected.findPosition(lastUngappedPos);
-
-      for (AlignedCodonFrame acf : codonFrames)
+      for (SequenceI seq : mapTo.getAlignment().getSequences())
       {
-        for (SequenceI seq : mapTo.getAlignment().getSequences())
+        int mappedStartResidue = 0;
+        int mappedEndResidue = 0;
+        for (AlignedCodonFrame acf : codonFrames)
         {
-          SequenceI peptide = targetIsNucleotide ? selected : seq;
-          SequenceI cds = targetIsNucleotide ? seq : selected;
-          SequenceToSequenceMapping s2s = acf.getCoveringMapping(cds,
-                  peptide);
-          if (s2s == null)
+          // rather than use acf.getCoveringMapping() we iterate through all
+          // mappings to make sure all CDS are selected for a protein
+          for (SequenceToSequenceMapping map: acf.getMappings())
           {
-            continue;
-          }
-          int mappedStartResidue = 0;
-          int mappedEndResidue = 0;
-          List<AlignedCodonFrame> mapping = Arrays.asList(acf);
-          SearchResultsI sr = buildSearchResults(selected, startResiduePos,
-                  mapping);
-          for (SearchResultMatchI m : sr.getResults())
-          {
-            mappedStartResidue = m.getStart();
-            mappedEndResidue = m.getEnd();
-          }
-          sr = buildSearchResults(selected, endResiduePos, mapping);
-          for (SearchResultMatchI m : sr.getResults())
+          if (map.covers(selected) && map.covers(seq))
           {
-            mappedStartResidue = Math.min(mappedStartResidue, m.getStart());
-            mappedEndResidue = Math.max(mappedEndResidue, m.getEnd());
-          }
+            /*
+             * Found a sequence mapping. Locate the start/end mapped residues.
+             */
+            List<AlignedCodonFrame> mapping = Arrays
+                    .asList(new AlignedCodonFrame[]
+                    { acf });
+            // locate start 
+            SearchResultsI sr = buildSearchResults(selected,
+                    startResiduePos, mapping);
+            for (SearchResultMatchI m : sr.getResults())
+            {
+              mappedStartResidue = m.getStart();
+              mappedEndResidue = m.getEnd();
+            }
+            // locate end - allowing for adjustment of start range
+            sr = buildSearchResults(selected, endResiduePos, mapping);
+            for (SearchResultMatchI m : sr.getResults())
+            {
+              mappedStartResidue = Math.min(mappedStartResidue,
+                      m.getStart());
+              mappedEndResidue = Math.max(mappedEndResidue, m.getEnd());
+            }
 
-          /*
-           * Find the mapped aligned columns, save the range. Note findIndex
-           * returns a base 1 position, SequenceGroup uses base 0
-           */
-          int mappedStartCol = seq.findIndex(mappedStartResidue) - 1;
-          minStartCol = minStartCol == -1 ? mappedStartCol
-                  : Math.min(minStartCol, mappedStartCol);
-          int mappedEndCol = seq.findIndex(mappedEndResidue) - 1;
-          maxEndCol = maxEndCol == -1 ? mappedEndCol
-                  : Math.max(maxEndCol, mappedEndCol);
-          mappedGroup.addSequence(seq, false);
-          break;
-        }
+            /*
+             * Find the mapped aligned columns, save the range. Note findIndex
+             * returns a base 1 position, SequenceGroup uses base 0
+             */
+            int mappedStartCol = seq.findIndex(mappedStartResidue) - 1;
+            minStartCol = minStartCol == -1 ? mappedStartCol
+                    : Math.min(minStartCol, mappedStartCol);
+            int mappedEndCol = seq.findIndex(mappedEndResidue) - 1;
+            maxEndCol = maxEndCol == -1 ? mappedEndCol
+                    : Math.max(maxEndCol, mappedEndCol);
+            mappedGroup.addSequence(seq, false);
+            break;
+          }
+        }}
       }
     }
     mappedGroup.setStartRes(minStartCol < 0 ? 0 : minStartCol);