Cosmetic change to list sampling functions: the typical usage of n and k were reversed. No change in functionality of the classes has been made and unit tests still pass.

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@1736 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
andrewk 2009-09-28 18:12:32 +00:00
parent 39598f1f0a
commit 5662a88ee1
3 changed files with 15 additions and 17 deletions

View File

@ -75,7 +75,7 @@ public class CoverageEvalWalker extends LocusWalker<List<String>, String> {
for (int coverage : coverage_levels) { for (int coverage : coverage_levels) {
coverage = Math.min(coverage_available, coverage); // don't exceed max available coverage coverage = Math.min(coverage_available, coverage); // don't exceed max available coverage
for (int r=0; r<downsampling_repeats; r++) { for (int r=0; r<downsampling_repeats; r++) {
List<Integer> subset_indices = ListUtils.sampleIndicesWithReplacement(coverage, coverage_available); List<Integer> subset_indices = ListUtils.sampleIndicesWithReplacement(coverage_available, coverage);
List<SAMRecord> sub_reads = ListUtils.sliceListByIndices(subset_indices, reads); List<SAMRecord> sub_reads = ListUtils.sliceListByIndices(subset_indices, reads);
List<Integer> sub_offsets = ListUtils.sliceListByIndices(subset_indices, offsets); List<Integer> sub_offsets = ListUtils.sliceListByIndices(subset_indices, offsets);

View File

@ -18,16 +18,16 @@ public class ListUtils {
/** /**
* Returns n random indices drawn with replacement from the range 0..(k-1) * Returns n random indices drawn with replacement from the range 0..(k-1)
* *
* @param n the number of random indices to draw (with replacement) * @param n the total number of indices sampled from
* @param k the total number of indices allowed * @param k the number of random indices to draw (with replacement)
* @return a list of random indices ranging from 0 to (k-1) with possible duplicates * @return a list of k random indices ranging from 0 to (n-1) with possible duplicates
*/ */
static public ArrayList<Integer> sampleIndicesWithReplacement(int n, int k) { static public ArrayList<Integer> sampleIndicesWithReplacement(int n, int k) {
ArrayList<Integer> chosen_balls = new ArrayList <Integer>(); ArrayList<Integer> chosen_balls = new ArrayList <Integer>(k);
for (int i=0; i<n; i++) { for (int i=0; i< k; i++) {
//Integer chosen_ball = balls[rand.nextInt(k)]; //Integer chosen_ball = balls[rand.nextInt(k)];
chosen_balls.add(rand.nextInt(k)); chosen_balls.add(rand.nextInt(n));
//balls.remove(chosen_ball); //balls.remove(chosen_ball);
} }
@ -37,9 +37,9 @@ public class ListUtils {
/** /**
* Returns n random indices drawn without replacement from the range 0..(k-1) * Returns n random indices drawn without replacement from the range 0..(k-1)
* *
* @param n the number of random indices to draw (without replacement) * @param n the total number of indices sampled from
* @param k the total number of indices allowed * @param k the number of random indices to draw (without replacement)
* @return a list of random indices ranging from 0 to (k-1) without duplicates * @return a list of k random indices ranging from 0 to (n-1) without duplicates
*/ */
static public ArrayList<Integer> sampleIndicesWithoutReplacement(int n, int k) { static public ArrayList<Integer> sampleIndicesWithoutReplacement(int n, int k) {
ArrayList<Integer> chosen_balls = new ArrayList<Integer>(k); ArrayList<Integer> chosen_balls = new ArrayList<Integer>(k);

View File

@ -4,10 +4,8 @@ import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.broadinstitute.sting.BaseTest; import org.broadinstitute.sting.BaseTest;
import org.broadinstitute.sting.utils.MathUtils;
import java.io.File;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -27,15 +25,15 @@ public class ListUtilsTest extends BaseTest {
logger.warn("Executing testRandomIndicesWithReplacement"); logger.warn("Executing testRandomIndicesWithReplacement");
// Check that the size of the list returned is correct // Check that the size of the list returned is correct
Assert.assertTrue(ListUtils.sampleIndicesWithReplacement(0, 5).size() == 0); Assert.assertTrue(ListUtils.sampleIndicesWithReplacement(5, 0).size() == 0);
Assert.assertTrue(ListUtils.sampleIndicesWithReplacement(1, 5).size() == 1); Assert.assertTrue(ListUtils.sampleIndicesWithReplacement(5, 1).size() == 1);
Assert.assertTrue(ListUtils.sampleIndicesWithReplacement(5, 5).size() == 5); Assert.assertTrue(ListUtils.sampleIndicesWithReplacement(5, 5).size() == 5);
Assert.assertTrue(ListUtils.sampleIndicesWithReplacement(1000, 5).size() == 1000); Assert.assertTrue(ListUtils.sampleIndicesWithReplacement(5, 1000).size() == 1000);
// Check that the list contains only the k element range that as asked for - no more, no less // Check that the list contains only the k element range that as asked for - no more, no less
List<Integer> Five = new ArrayList<Integer>(); List<Integer> Five = new ArrayList<Integer>();
Collections.addAll(Five, 0, 1, 2, 3, 4); Collections.addAll(Five, 0, 1, 2, 3, 4);
List<Integer> BigFive = ListUtils.sampleIndicesWithReplacement(10000, 5); List<Integer> BigFive = ListUtils.sampleIndicesWithReplacement(5, 10000);
Assert.assertTrue(BigFive.containsAll(Five)); Assert.assertTrue(BigFive.containsAll(Five));
Assert.assertTrue(Five.containsAll(BigFive)); Assert.assertTrue(Five.containsAll(BigFive));
} }
@ -53,7 +51,7 @@ public class ListUtilsTest extends BaseTest {
Collections.addAll(Five, 0, 1, 2, 3, 4); Collections.addAll(Five, 0, 1, 2, 3, 4);
List<Character> FiveAlpha = new ArrayList<Character>(); List<Character> FiveAlpha = new ArrayList<Character>();
Collections.addAll(FiveAlpha, 'a', 'b', 'c', 'd', 'e'); Collections.addAll(FiveAlpha, 'a', 'b', 'c', 'd', 'e');
List<Integer> BigFive = ListUtils.sampleIndicesWithReplacement(10000, 5); List<Integer> BigFive = ListUtils.sampleIndicesWithReplacement(5, 10000);
List<Character> BigFiveAlpha = ListUtils.sliceListByIndices(BigFive, FiveAlpha); List<Character> BigFiveAlpha = ListUtils.sliceListByIndices(BigFive, FiveAlpha);
Assert.assertTrue(BigFiveAlpha.containsAll(FiveAlpha)); Assert.assertTrue(BigFiveAlpha.containsAll(FiveAlpha));
Assert.assertTrue(FiveAlpha.containsAll(BigFiveAlpha)); Assert.assertTrue(FiveAlpha.containsAll(BigFiveAlpha));