From: gmungoc Date: Mon, 17 Aug 2015 15:26:05 +0000 (+0100) Subject: JAL-1800 separated SearchResults.getCharacters() from toString() X-Git-Tag: Release_2_10_0~535^2~1 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=07e95212532922e9a7966b81724b5a54e26b51a8;p=jalview.git JAL-1800 separated SearchResults.getCharacters() from toString() --- diff --git a/src/jalview/analysis/AlignmentUtils.java b/src/jalview/analysis/AlignmentUtils.java index e6d3e83..f9d4c08 100644 --- a/src/jalview/analysis/AlignmentUtils.java +++ b/src/jalview/analysis/AlignmentUtils.java @@ -20,20 +20,6 @@ */ 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 mappings) { + if (dnaSeq == null || proteinSeq == null) + { + return false; + } + SequenceI dnaDs = dnaSeq.getDatasetSequence() == null ? dnaSeq : dnaSeq.getDatasetSequence(); SequenceI proteinDs = proteinSeq.getDatasetSequence() == null ? proteinSeq : proteinSeq.getDatasetSequence(); diff --git a/src/jalview/datamodel/SearchResults.java b/src/jalview/datamodel/SearchResults.java index 3948ba0..3329431 100755 --- a/src/jalview/datamodel/SearchResults.java +++ b/src/jalview/datamodel/SearchResults.java @@ -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. diff --git a/test/jalview/datamodel/MatchTest.java b/test/jalview/datamodel/MatchTest.java index 9b5e97b..f711a14 100644 --- a/test/jalview/datamodel/MatchTest.java +++ b/test/jalview/datamodel/MatchTest.java @@ -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"); diff --git a/test/jalview/datamodel/SearchResultsTest.java b/test/jalview/datamodel/SearchResultsTest.java index 983bd34..ae9512f 100644 --- a/test/jalview/datamodel/SearchResultsTest.java +++ b/test/jalview/datamodel/SearchResultsTest.java @@ -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");