Merge branch 'develop' of https://source.jalview.org/git/jalview.git into develop
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 17 Aug 2015 15:26:20 +0000 (16:26 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 17 Aug 2015 15:26:20 +0000 (16:26 +0100)
src/jalview/analysis/AlignmentUtils.java
src/jalview/datamodel/SearchResults.java
test/jalview/analysis/AlignmentUtilsTests.java
test/jalview/datamodel/MatchTest.java
test/jalview/datamodel/SearchResultsTest.java
test/jalview/ws/gui/Jws2ParamView.java

index e6d3e83..f9d4c08 100644 (file)
  */
 package jalview.analysis;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-import java.util.TreeMap;
-
 import jalview.datamodel.AlignedCodon;
 import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.Alignment;
@@ -52,6 +38,20 @@ import jalview.util.DBRefUtils;
 import jalview.util.MapList;
 import jalview.util.MappingUtils;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.TreeMap;
+
 /**
  * grab bag of useful alignment manipulation operations Expect these to be
  * refactored elsewhere at some point.
@@ -448,6 +448,11 @@ public class AlignmentUtils
   protected static boolean translatesAs(char[] cdnaSeqChars, int cdnaStart,
           char[] aaSeqChars)
   {
+    if (cdnaSeqChars == null || aaSeqChars == null)
+    {
+      return false;
+    }
+
     int aaResidue = 0;
     for (int i = cdnaStart; i < cdnaSeqChars.length - 2
             && aaResidue < aaSeqChars.length; i += 3, aaResidue++)
@@ -862,7 +867,7 @@ public class AlignmentUtils
           {
             mapping.markMappedRegion(seq, pos, sr);
           }
-          newseq.append(sr.toString());
+          newseq.append(sr.getCharacters());
           if (first)
           {
             first = false;
@@ -1016,6 +1021,11 @@ public class AlignmentUtils
    */
   public static boolean isMappable(AlignmentI al1, AlignmentI al2)
   {
+    if (al1 == null || al2 == null)
+    {
+      return false;
+    }
+
     /*
      * Require one nucleotide and one protein
      */
@@ -1048,9 +1058,15 @@ public class AlignmentUtils
    * @param mappings
    * @return
    */
-  public static boolean isMappable(SequenceI dnaSeq, SequenceI proteinSeq,
+  protected static boolean isMappable(SequenceI dnaSeq,
+          SequenceI proteinSeq,
           Set<AlignedCodonFrame> mappings)
   {
+    if (dnaSeq == null || proteinSeq == null)
+    {
+      return false;
+    }
+
     SequenceI dnaDs = dnaSeq.getDatasetSequence() == null ? dnaSeq : dnaSeq.getDatasetSequence();
     SequenceI proteinDs = proteinSeq.getDatasetSequence() == null ? proteinSeq
             : proteinSeq.getDatasetSequence();
index 3948ba0..3329431 100755 (executable)
@@ -87,18 +87,27 @@ public class SearchResults
     }
 
     /**
-     * Returns the string of characters in the matched region.
+     * Returns the string of characters in the matched region, prefixed by the
+     * start position, e.g. "12CGT" or "208K"
      */
     @Override
     public String toString()
     {
+      final int from = Math.max(start - 1, 0);
+      String startPosition = String.valueOf(from);
+      return startPosition + getCharacters();
+    }
+
+    /**
+     * Returns the string of characters in the matched region.
+     */
+    public String getCharacters()
+    {
       char[] chars = sequence.getSequence();
       // convert start/end to base 0 (with bounds check)
       final int from = Math.max(start - 1, 0);
       final int to = Math.min(end, chars.length + 1);
-      // return String.valueOf(Arrays.copyOfRange(chars, from, to));
-      return String.valueOf(from)
-              + String.valueOf(Arrays.copyOfRange(chars, from, to));
+      return String.valueOf(Arrays.copyOfRange(chars, from, to));
     }
 
     public void setSequence(SequenceI seq)
@@ -300,9 +309,9 @@ public class SearchResults
   }
 
   /**
-   * Return the results as a string of characters. Meant for use when the
-   * context ensures that all matches are to regions of the same sequence
-   * (otherwise the result is meaningless).
+   * Return the results as a string of characters (bases) prefixed by start
+   * position(s). Meant for use when the context ensures that all matches are to
+   * regions of the same sequence (otherwise the result is meaningless).
    * 
    * @return
    */
@@ -318,6 +327,23 @@ public class SearchResults
   }
 
   /**
+   * Return the results as a string of characters (bases). Meant for use when
+   * the context ensures that all matches are to regions of the same sequence
+   * (otherwise the result is meaningless).
+   * 
+   * @return
+   */
+  public String getCharacters()
+  {
+    StringBuilder result = new StringBuilder(256);
+    for (Match m : matches)
+    {
+      result.append(m.getCharacters());
+    }
+    return result.toString();
+  }
+
+  /**
    * Hashcode is has derived from the list of matches. This ensures that when
    * two SearchResults objects satisfy the test for equals(), then they have the
    * same hashcode.
index 61b2487..6fef829 100644 (file)
@@ -1267,4 +1267,22 @@ public class AlignmentUtilsTests
     assertEquals(1, peptideMappings.size());
     assertSame(pep3.getDatasetSequence(), peptideMappings.get(0).getTo());
   }
+
+  @Test(groups = { "Functional" })
+  public void testIsMappable()
+  {
+    SequenceI dna1 = new Sequence("dna1", "cgCAGtgGT");
+    SequenceI aa1 = new Sequence("aa1", "RSG");
+    AlignmentI al1 = new Alignment(new SequenceI[] { dna1 });
+    AlignmentI al2 = new Alignment(new SequenceI[] { aa1 });
+
+    assertFalse(AlignmentUtils.isMappable(null, null));
+    assertFalse(AlignmentUtils.isMappable(al1, null));
+    assertFalse(AlignmentUtils.isMappable(null, al1));
+    assertFalse(AlignmentUtils.isMappable(al1, al1));
+    assertFalse(AlignmentUtils.isMappable(al2, al2));
+
+    assertTrue(AlignmentUtils.isMappable(al1, al2));
+    assertTrue(AlignmentUtils.isMappable(al2, al1));
+  }
 }
index 9b5e97b..f711a14 100644 (file)
@@ -19,6 +19,14 @@ public class MatchTest
   }
 
   @Test(groups = { "Functional" })
+  public void testGetCharacters()
+  {
+    SequenceI seq = new Sequence("", "abcdefghijklm");
+    Match m = new SearchResults().new Match(seq, 3, 5);
+    assertEquals("cde", m.getCharacters());
+  }
+
+  @Test(groups = { "Functional" })
   public void testEquals()
   {
     SequenceI seq1 = new Sequence("", "abcdefghijklm");
index 983bd34..ae9512f 100644 (file)
@@ -25,6 +25,21 @@ public class SearchResultsTest
   }
 
   @Test(groups = { "Functional" })
+  public void testGetCharacters()
+  {
+    SequenceI seq = new Sequence("", "abcdefghijklm");
+    SearchResults sr = new SearchResults();
+    sr.addResult(seq, 1, 1);
+    assertEquals("a", sr.getCharacters());
+    sr.addResult(seq, 3, 5);
+    assertEquals("acde", sr.getCharacters());
+
+    seq = new Sequence("", "pqrstuvwxy");
+    sr.addResult(seq, 6, 7);
+    assertEquals("acdeuv", sr.getCharacters());
+  }
+
+  @Test(groups = { "Functional" })
   public void testEquals()
   {
     SequenceI seq1 = new Sequence("", "abcdefghijklm");
index 41ead2f..294d426 100644 (file)
@@ -70,7 +70,7 @@ public class Jws2ParamView
   }
 
   /**
-   * This test marked Ignore as it appears to need user action to complete
+   * This test marked Interactive as it appears to need user action to complete
    * rather than hang
    */