From 681e67c72cfcae94141d7d967496fad036a4bc03 Mon Sep 17 00:00:00 2001 From: kiran Date: Tue, 9 Jun 2009 00:47:54 +0000 Subject: [PATCH] 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 --- .../broadinstitute/sting/utils/BaseUtils.java | 50 +++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/java/src/org/broadinstitute/sting/utils/BaseUtils.java b/java/src/org/broadinstitute/sting/utils/BaseUtils.java index f5ed8dcfa..0bf5aecbb 100644 --- a/java/src/org/broadinstitute/sting/utils/BaseUtils.java +++ b/java/src/org/broadinstitute/sting/utils/BaseUtils.java @@ -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():