*/
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;
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.
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++)
{
mapping.markMappedRegion(seq, pos, sr);
}
- newseq.append(sr.toString());
+ newseq.append(sr.getCharacters());
if (first)
{
first = false;
*/
public static boolean isMappable(AlignmentI al1, AlignmentI al2)
{
+ if (al1 == null || al2 == null)
+ {
+ return false;
+ }
+
/*
* Require one nucleotide and one protein
*/
* @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();
}
/**
- * 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)
}
/**
- * 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
*/
}
/**
+ * 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.
}
@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");
}
@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");