From d275c18e5836393cc49633efc7c2f7b8cb718d0b Mon Sep 17 00:00:00 2001 From: aaron Date: Wed, 27 May 2009 22:32:25 +0000 Subject: [PATCH] adding some objects we need for the GLF format. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@846 348d0f76-0448-11de-a6fe-93d51630548a --- .../sting/utils/glf/LikelihoodObject.java | 101 +++++++++++++++++ .../sting/utils/glf/GLFRecordTest.java | 45 ++++++++ .../sting/utils/glf/LikelihoodObjectTest.java | 105 ++++++++++++++++++ 3 files changed, 251 insertions(+) create mode 100755 java/src/org/broadinstitute/sting/utils/glf/LikelihoodObject.java create mode 100755 java/test/org/broadinstitute/sting/utils/glf/GLFRecordTest.java create mode 100755 java/test/org/broadinstitute/sting/utils/glf/LikelihoodObjectTest.java diff --git a/java/src/org/broadinstitute/sting/utils/glf/LikelihoodObject.java b/java/src/org/broadinstitute/sting/utils/glf/LikelihoodObject.java new file mode 100755 index 000000000..865c778d6 --- /dev/null +++ b/java/src/org/broadinstitute/sting/utils/glf/LikelihoodObject.java @@ -0,0 +1,101 @@ +package org.broadinstitute.sting.utils.glf; + +import java.util.HashMap; + + +/* + * Copyright (c) 2009 The Broad Institute + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +/** + * @author aaron + * + *

+ * Class LikelyhoodObject + *

+ * An object used to store likelyhood information for genotypes. Genotype + * likelihoods are assumed to be zero, unless set. This allows the consumer + * to make an empty LikelihoodObject, and just set those values which have + * associated likelihood values. + */ +public class LikelihoodObject { + + // our possible genotypes, in order according to GLFv3 + public enum GENOTYPE { + AA, AT, AC, AG, CC, CT, CG, GG, GT, TT + } + + // the associated probabilities for each genotype + final protected HashMap likelihood = new HashMap(); + + /** create a blank likelihood object */ + public LikelihoodObject() { + for (GENOTYPE type : GENOTYPE.values()) { + likelihood.put(type, 0); + } + } + + /** + * create a likelyhood object, given an array of genotype scores in GLFv3 ordering + * @param values + */ + public LikelihoodObject(int[] values) { + if (values.length != GENOTYPE.values().length) { + throw new IllegalArgumentException("invalid array passed to LikelihoodObject, should be size " + GENOTYPE.values().length); + } + int index = 0; + for (GENOTYPE type : GENOTYPE.values()) { + if (values[index] < 0 || values[index] > 255) { + throw new IllegalArgumentException("likelihood values must be greater or equal 0, and less then 256, value given: " + values[index]); + } + likelihood.put(type, values[index]); + ++index; + } + } + + /** + * set the likelihood, given it's probability and the genotype + * @param type the genotype + * @param likelyhood the likelihood as a float between 0 and 1, which is converted to a byte + */ + public void setLikelihood(GENOTYPE type, float likelyhood) { + likelihood.put(type,(int)Math.round(likelyhood*255.0)); + } + + /** + * return a byte array representation of the likelihood object, in GLFv3 specified order + * @return a byte array of the genotype values + */ + public int[] toByteArray() { + int ret[] = new int[GENOTYPE.values().length]; + int index = 0; + for (GENOTYPE type : GENOTYPE.values()) { + ret[index] = likelihood.get(type); + ++index; + } + return ret; + } + + +} diff --git a/java/test/org/broadinstitute/sting/utils/glf/GLFRecordTest.java b/java/test/org/broadinstitute/sting/utils/glf/GLFRecordTest.java new file mode 100755 index 000000000..17f1ebda7 --- /dev/null +++ b/java/test/org/broadinstitute/sting/utils/glf/GLFRecordTest.java @@ -0,0 +1,45 @@ +package org.broadinstitute.sting.utils.glf; + +import org.junit.Test; + + +/* + * Copyright (c) 2009 The Broad Institute + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +/** + * @author aaron + *

+ * Class GLFRecordTest + *

+ * Tests for the GLFRecord class + */ +public class GLFRecordTest { + + @Test + public void basicWrite() { + + } + +} diff --git a/java/test/org/broadinstitute/sting/utils/glf/LikelihoodObjectTest.java b/java/test/org/broadinstitute/sting/utils/glf/LikelihoodObjectTest.java new file mode 100755 index 000000000..129ea56f0 --- /dev/null +++ b/java/test/org/broadinstitute/sting/utils/glf/LikelihoodObjectTest.java @@ -0,0 +1,105 @@ +package org.broadinstitute.sting.utils.glf; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import org.broadinstitute.sting.BaseTest; +import static junit.framework.Assert.assertTrue; + + +/* + * Copyright (c) 2009 The Broad Institute + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +/** + * @author aaron + *

+ * Class LikelihoodObjectTest + *

+ * Tests the Likelihood object. + */ +public class LikelihoodObjectTest extends BaseTest { + + private LikelihoodObject mLO = null; + + @Before + public void before() { + mLO = new LikelihoodObject(); + } + + @Test + public void testBlankConstruction() { + mLO = new LikelihoodObject(); + assertTrue(mLO.likelihood.size() == LikelihoodObject.GENOTYPE.values().length); + } + + @Test + public void testConstructionFromArray() { + int[] ray = new int[10]; + for (int x = 0; x < 10; x++) { + ray[x] = ( x * 25 ); + } + mLO = new LikelihoodObject(ray); + assertTrue(mLO.likelihood.size() == LikelihoodObject.GENOTYPE.values().length); + + int index = 0; + for (LikelihoodObject.GENOTYPE t : LikelihoodObject.GENOTYPE.values()) { + assertTrue(ray[index] == mLO.likelihood.get(t)); + ++index; + } + } + + @Test + public void testByteArrayReturn() { + int[] ray = new int[10]; + for (int x = 0; x < 10; x++) { + ray[x] = ( x * 25 ); + } + mLO = new LikelihoodObject(ray); + assertTrue(mLO.likelihood.size() == LikelihoodObject.GENOTYPE.values().length); + + int index = 0; + int[] ret = mLO.toByteArray(); + for (index = 0; index < ret.length; index++) { + assertTrue(ray[index] == ret[index]); + } + } + + @Test + public void testSetLikelihood() { + mLO = new LikelihoodObject(); + for (LikelihoodObject.GENOTYPE t : LikelihoodObject.GENOTYPE.values()) { + mLO.setLikelihood(t,0.5f); + } + assertTrue(mLO.likelihood.size() == LikelihoodObject.GENOTYPE.values().length); + + int index = 0; + int[] ret = mLO.toByteArray(); + for (index = 0; index < ret.length; index++) { + assertTrue(ret[index] == 128); + } + } + + +}