From 6d9816f1a57df6db3c81c90bb704c07d7068c95d Mon Sep 17 00:00:00 2001 From: Mark DePristo Date: Thu, 31 Jan 2013 17:46:18 -0500 Subject: [PATCH] Cleanup unused utils functions, and add unit test for one (append) --- .../org/broadinstitute/sting/utils/Utils.java | 212 +----------------- .../sting/utils/UtilsUnitTest.java | 22 +- 2 files changed, 27 insertions(+), 207 deletions(-) diff --git a/public/java/src/org/broadinstitute/sting/utils/Utils.java b/public/java/src/org/broadinstitute/sting/utils/Utils.java index eb287abd8..39fcbb3d2 100644 --- a/public/java/src/org/broadinstitute/sting/utils/Utils.java +++ b/public/java/src/org/broadinstitute/sting/utils/Utils.java @@ -240,6 +240,13 @@ public class Utils { } } + /** + * Create a new list that contains the elements of left along with elements elts + * @param left a non-null list of elements + * @param elts a varargs vector for elts to append in order to left + * @param + * @return A newly allocated linked list containing left followed by elts + */ public static List append(final List left, T ... elts) { final List l = new LinkedList(left); l.addAll(Arrays.asList(elts)); @@ -329,15 +336,6 @@ public class Utils { return str.substring(start, end+1); } - public static byte listMaxByte(List quals) { - if (quals.size() == 0) return 0; - byte m = quals.get(0); - for (byte b : quals) { - m = b > m ? b : m; - } - return m; - } - /** * Splits expressions in command args by spaces and returns the array of expressions. * Expressions may use single or double quotes to group any individual expression, but not both. @@ -390,182 +388,6 @@ public class Utils { return C; } - /** - * Appends String(s) B to array A. - * @param A First array. - * @param B Strings to append. - * @return A with B(s) appended. - */ - public static String[] appendArray(String[] A, String... B) { - return concatArrays(A, B); - } - - /** - * Returns indices of all occurrences of the specified symbol in the string - * @param s Search string - * @param ch Character to search for - * @return Indices of all occurrences of the specified symbol - */ - public static int[] indexOfAll(String s, int ch) { - int[] pos = new int[64]; - int z = 0; - - for (int i = 0; i < s.length(); i++) { - if (s.charAt(i) == ch) pos[z++] = i; - } - return reallocate(pos, z); - } - - public static int countSetBits(boolean[] array) { - int counter = 0; - for ( int i = 0; i < array.length; i++ ) { - if ( array[i] ) - counter++; - } - return counter; - } - - /** - * Returns new (reallocated) integer array of the specified size, with content - * of the original array orig copied into it. If newSize is - * less than the size of the original array, only first newSize elements will be copied. - * If new size is greater than the size of the original array, the content of the original array will be padded - * with zeros up to the new size. Finally, if new size is the same as original size, no memory reallocation - * will be performed and the original array will be returned instead. - * - * @param orig Original size. - * @param newSize New Size. - * - * @return New array with length equal to newSize. - */ - public static int[] reallocate(int[] orig, int newSize) { - if (orig.length == newSize) return orig; - int[] new_array = new int[newSize]; - int L = (newSize > orig.length ? orig.length : newSize); - for (int i = 0; i < L; i++) new_array[i] = orig[i]; - return new_array; - } - - - /** - * Returns a copy of array a, extended with additional n elements to the right (if n > 0 ) or -n elements to the - * left (if n<0), copying the values form the original array. Newly added elements are filled with value v. Note that - * if array a is being padded to the left, first (-n) elements of the returned array are v's, followed by the content of - * array a. - * @param a original array - * @param n number of (v-filled) elements to append to a on the right (n>0) or on the left (n<0) - * @param v element value - * @return the extended copy of array a with additional n elements - */ - public static byte [] extend(final byte[] a, int n, byte v) { - - byte [] newA; - - if ( n > 0 ) { - newA = Arrays.copyOf(a, a.length+n); - if ( v != 0) { // java pads with 0's for us, so there is nothing to do if v==0 - for ( int i = a.length; i < newA.length ; i++ ) newA[i] = v; - } - return newA; - } - - // we are here only if n < 0: - n = (-n); - newA = new byte[ a.length + n ]; - int i; - if ( v!= 0 ) { - i = 0; - for( ; i < n; i++ ) newA[i] = v; - } else { - i = n; - } - for ( int j = 0 ; j < a.length ; i++, j++) newA[i]=a[j]; - return newA; - } - - - /** - * Returns a copy of array a, extended with additional n elements to the right (if n > 0 ) or -n elements to the - * left (if n<0), copying the values form the original array. Newly added elements are filled with value v. Note that - * if array a is padded to the left, first (-n) elements of the returned array are v's, followed by the content of - * array a. - * @param a original array - * @param n number of (v-filled) elements to append to a on the right (n>0) or on the left (n<0) - * @param v element value - * @return the extended copy of array a with additional n elements - */ - public static short [] extend(final short[] a, int n, short v) { - - short [] newA; - - if ( n > 0 ) { - newA = Arrays.copyOf(a, a.length+n); - if ( v != 0) { // java pads with 0's for us, so there is nothing to do if v==0 - for ( int i = a.length; i < newA.length ; i++ ) newA[i] = v; - } - return newA; - } - - // we are here only if n < 0: - n = (-n); - newA = new short[ a.length + n ]; - int i; - if ( v!= 0 ) { - i = 0; - for( ; i < n; i++ ) newA[i] = v; - } else { - i = n; - } - for ( int j = 0 ; j < a.length ; i++, j++) newA[i]=a[j]; - return newA; - } - - /* TEST ME - public static void main(String[] argv) { - List l1 = new LinkedList(); - List l2 = new ArrayList(); - - l1.add(1); - l1.add(5); - l1.add(3); - l1.add(10); - l1.add(4); - l1.add(2); - l2.add(1); - l2.add(5); - l2.add(3); - l2.add(10); - l2.add(4); - l2.add(2); - - Predicate p = new Predicate() { - public boolean apply(Integer i) { - return i > 2; - } - }; - filterInPlace(p, l1); - filterInPlace(p, l2); - - for ( int i = 0 ; i < l1.size(); i++ ) System.out.print(" "+l1.get(i)); - System.out.println(); - for ( int i = 0 ; i < l2.size(); i++ ) System.out.print(" " + l2.get(i)); - System.out.println(); - - } - - */ - - /** - * a helper method. Turns a single character string into a char. - * - * @param str the string - * - * @return a char - */ - public static char stringToChar(String str) { - if (str.length() != 1) throw new IllegalArgumentException("String length must be one"); - return str.charAt(0); - } public static > List sorted(Collection c) { return sorted(c, false); @@ -594,18 +416,6 @@ public class Utils { return l; } - public static , V> String sortedString(Map c) { - List t = new ArrayList(c.keySet()); - Collections.sort(t); - - List pairs = new ArrayList(); - for ( T k : t ) { - pairs.add(k + "=" + c.get(k)); - } - - return "{" + join(", ", pairs) + "}"; - } - /** * Reverse a byte array of bases * @@ -654,14 +464,6 @@ public class Utils { return new String( reverse( bases.getBytes() )) ; } - public static byte[] charSeq2byteSeq(char[] seqIn) { - byte[] seqOut = new byte[seqIn.length]; - for ( int i = 0; i < seqIn.length; i++ ) { - seqOut[i] = (byte)seqIn[i]; - } - return seqOut; - } - public static boolean isFlagSet(int value, int flag) { return ((value & flag) == flag); } diff --git a/public/java/test/org/broadinstitute/sting/utils/UtilsUnitTest.java b/public/java/test/org/broadinstitute/sting/utils/UtilsUnitTest.java index 5d6ecd0f9..29c643153 100644 --- a/public/java/test/org/broadinstitute/sting/utils/UtilsUnitTest.java +++ b/public/java/test/org/broadinstitute/sting/utils/UtilsUnitTest.java @@ -32,8 +32,7 @@ import org.broadinstitute.sting.BaseTest; import org.testng.annotations.Test; import java.io.File; -import java.util.LinkedHashMap; -import java.util.Map; +import java.util.*; /** * Testing framework for general purpose utilities class. @@ -43,6 +42,25 @@ import java.util.Map; */ public class UtilsUnitTest extends BaseTest { + @Test + public void testAppend() { + for ( int leftSize : Arrays.asList(0, 1, 2, 3) ) { + for ( final int rightSize : Arrays.asList(0, 1, 2) ) { + final List left = new LinkedList(); + for ( int i = 0; i < leftSize; i++ ) left.add(i); + final List total = new LinkedList(); + for ( int i = 0; i < leftSize + rightSize; i++ ) total.add(i); + + if ( rightSize == 0 ) + Assert.assertEquals(Utils.append(left), total); + if ( rightSize == 1 ) + Assert.assertEquals(Utils.append(left, leftSize), total); + if ( rightSize == 2 ) + Assert.assertEquals(Utils.append(left, leftSize, leftSize + 1), total); + } + } + + } @Test public void testDupStringNoChars() {