Added some methods to generate random bases or random base indexes, optionally disallowing the generation of a specified base or base index.

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@943 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
kiran 2009-06-09 00:47:54 +00:00
parent 13eb868536
commit 681e67c72c
1 changed files with 47 additions and 3 deletions

View File

@ -1,9 +1,9 @@
package org.broadinstitute.sting.utils;
import java.util.Random;
/**
* BaseUtils contains some basic utilities for manipulating nucleotides.
*
* @author Kiran Garimella
*/
public class BaseUtils {
@ -216,6 +216,51 @@ public class BaseUtils {
return ((double) baseCounts[mostFrequentBaseIndex])/((double) sequence.length);
}
/**
* Return a random base index (A=0, C=1, G=2, T=3).
*
* @return a random base index (A=0, C=1, G=2, T=3)
*/
static public int getRandomBaseIndex() {
return getRandomBaseIndex(-1);
}
/**
* Return a random base index, excluding some base index.
*
* @param excludeBaseIndex the base index to exclude
* @return a random base index, excluding the one specified (A=0, C=1, G=2, T=3)
*/
static public int getRandomBaseIndex(int excludeBaseIndex) {
int randomBaseIndex = excludeBaseIndex;
Random generator = new Random();
while (randomBaseIndex == excludeBaseIndex) {
randomBaseIndex = generator.nextInt(4);
}
return randomBaseIndex;
}
/**
* Return a random base (A, C, G, T).
*
* @return a random base (A, C, G, T)
*/
static public char getRandomBase() {
return getRandomBase('.');
}
/**
* Return a random base, excluding some base.
*
* @param excludeBase the base to exclude
* @return a random base, excluding the one specified (A, C, G, T)
*/
static public char getRandomBase(char excludeBase) {
return BaseUtils.baseIndexToSimpleBase(getRandomBaseIndex(BaseUtils.simpleBaseToBaseIndex(excludeBase)));
}
/** Computes the smallest period >= minPeriod for the specified string. The period is defined as such p,
@ -261,7 +306,6 @@ public class BaseUtils {
}
return period;
}
}
/* code snippet for testing sequencePeriod():