JAL-1725 first version of Jetty server / chimera selection listener
[jalview.git] / src / jalview / util / MappingUtils.java
index ece1bac..f2213ad 100644 (file)
@@ -250,8 +250,23 @@ public final class MappingUtils
   public static SearchResults buildSearchResults(SequenceI seq, int index,
           Set<AlignedCodonFrame> seqmappings)
   {
-    SearchResults results;
-    results = new SearchResults();
+    SearchResults results = new SearchResults();
+    addSearchResults(results, seq, index, seqmappings);
+    return results;
+  }
+
+  /**
+   * Adds entries to a SearchResults object describing the mapped region
+   * corresponding to the specified sequence position.
+   * 
+   * @param results
+   * @param seq
+   * @param index
+   * @param seqmappings
+   */
+  public static void addSearchResults(SearchResults results, SequenceI seq,
+          int index, Set<AlignedCodonFrame> seqmappings)
+  {
     if (index >= seq.getStart() && index <= seq.getEnd())
     {
       for (AlignedCodonFrame acf : seqmappings)
@@ -259,7 +274,6 @@ public final class MappingUtils
         acf.markMappedRegion(seq, index, results);
       }
     }
-    return results;
   }
 
   /**
@@ -399,18 +413,18 @@ public final class MappingUtils
     SequenceI[] sortOrder = command.getSequenceOrder(undo);
     List<SequenceI> mappedOrder = new ArrayList<SequenceI>();
     int j = 0;
+
+    /*
+     * Assumption: we are only interested in a cDNA/protein mapping; refactor in
+     * future if we want to support sorting (c)dna as (c)dna or protein as
+     * protein
+     */
+    boolean mappingToNucleotide = mapTo.isNucleotide();
     for (SequenceI seq : sortOrder)
     {
       for (AlignedCodonFrame acf : mappings)
       {
-        /*
-         * Try protein-to-Dna, failing that try dna-to-protein
-         */
-        SequenceI mappedSeq = acf.getDnaForAaSeq(seq);
-        if (mappedSeq == null)
-        {
-          mappedSeq = acf.getAaForDnaSeq(seq);
-        }
+        SequenceI mappedSeq = mappingToNucleotide ? acf.getDnaForAaSeq(seq) : acf.getAaForDnaSeq(seq);
         if (mappedSeq != null)
         {
           for (SequenceI seq2 : mapTo.getSequences())
@@ -480,6 +494,12 @@ public final class MappingUtils
     Set<AlignedCodonFrame> codonFrames = protein.getAlignment()
             .getCodonFrames();
     ColumnSelection mappedColumns = new ColumnSelection();
+
+    if (colsel == null)
+    {
+      return mappedColumns;
+    }
+
     char fromGapChar = mapFrom.getAlignment().getGapCharacter();
 
     // FIXME allow for hidden columns
@@ -608,4 +628,28 @@ public final class MappingUtils
     }
     return result;
   }
+
+  /**
+   * Returns a list of any mappings that are from or to the given (aligned or
+   * dataset) sequence.
+   * 
+   * @param sequence
+   * @param mappings
+   * @return
+   */
+  public static List<AlignedCodonFrame> findMappingsForSequence(
+          SequenceI sequence, Set<AlignedCodonFrame> mappings)
+  {
+    List<AlignedCodonFrame> result = new ArrayList<AlignedCodonFrame>();
+    if (sequence == null || mappings == null)
+    {
+      return result;
+    }
+    for (AlignedCodonFrame mapping : mappings) {
+      if (mapping.involvesSequence(sequence)) {
+        result.add(mapping);
+      }
+    }
+    return result;
+  }
 }