make sure we close the error log stream in CommandLineProgram if it's opened; unit tests and clean-up for BasicVariation

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2241 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
aaron 2009-12-03 06:59:27 +00:00
parent d27d56ea0c
commit b3bdcd0e60
3 changed files with 135 additions and 14 deletions

View File

@ -261,6 +261,7 @@ public abstract class CommandLineProgram {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
clp.generateErrorLog(stream, e); clp.generateErrorLog(stream, e);
stream.close();
} }
/** /**

View File

@ -5,7 +5,9 @@ import org.broadinstitute.sting.utils.StingException;
import org.broadinstitute.sting.utils.Utils; import org.broadinstitute.sting.utils.Utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* User: aaron * User: aaron
@ -65,14 +67,14 @@ public class BasicVariation implements Variation {
*/ */
@Override @Override
public VARIANT_TYPE getType() { public VARIANT_TYPE getType() {
if (mLength != 0) return VARIANT_TYPE.INSERTION; if (mLength > 0) return VARIANT_TYPE.INSERTION;
if (mLength < 0) return VARIANT_TYPE.DELETION;
return (isSNP()) ? VARIANT_TYPE.SNP : VARIANT_TYPE.REFERENCE; return (isSNP()) ? VARIANT_TYPE.SNP : VARIANT_TYPE.REFERENCE;
} }
@Override @Override
public boolean isSNP() { public boolean isSNP() {
if (mLength == 0) return true; return ((mLength == 0) && (new HashSet(getAlternateAlleleList()).size() == 1));
return false;
} }
@Override @Override
@ -95,10 +97,15 @@ public class BasicVariation implements Variation {
return (mRef); return (mRef);
} }
/** are we bi-allelic? */ /**
* are we bi-allelic? In this case we always
* count the reference as an allele
*/
@Override @Override
public boolean isBiallelic() { public boolean isBiallelic() {
return (getAlternateAlleleList().size() == 1); // put the alternate alleles into a set, there may be duplicates (i.e. hom var)
Set<String> alleles = new HashSet(getAlternateAlleleList());
return (alleles.size() == 1); // if the alt list contained one unqiue non-ref base, we're biallelic
} }
@Override @Override
@ -133,20 +140,16 @@ public class BasicVariation implements Variation {
@Override @Override
public List<String> getAlleleList() { public List<String> getAlleleList() {
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
if (this.mBases.contains(mRef)) list.add(mRef);
for (char c : this.mBases.toCharArray()) for (char c : this.mBases.toCharArray())
if (c != Utils.stringToChar(mRef)) list.add(String.valueOf(c));
list.add(String.valueOf(c));
return list; return list;
} }
@Override @Override
public boolean isReference() { public boolean isReference() {
if (mLength != 0) return false; if (mLength != 0) return false;
int refIndex = 0; for (String str : getAlleleList())
for (char c : mBases.toCharArray()) { if (!str.equals(mRef)) return false;
if (mRef.charAt(refIndex) != c) return false;
}
return true; return true;
} }
@ -169,8 +172,8 @@ public class BasicVariation implements Variation {
@Override @Override
public char getAlternativeBaseForSNP() { public char getAlternativeBaseForSNP() {
if (!this.isSNP()) throw new IllegalStateException("we're not a SNP"); if (!this.isSNP()) throw new IllegalStateException("we're not a SNP");
if (!this.isBiallelic() || this.getAlternateAlleleList().size() != 1) throw new IllegalStateException("we're not biallelic"); if (!this.isBiallelic()) throw new IllegalStateException("we're not biallelic");
return Utils.stringToChar(this.getAlternateAlleleList().get(0)); return Utils.stringToChar((new HashSet<String>(getAlternateAlleleList())).iterator().next());
} }
/** /**

View File

@ -0,0 +1,117 @@
package org.broadinstitute.sting.utils.genotype;
import org.broadinstitute.sting.BaseTest;
import org.broadinstitute.sting.utils.GenomeLocParser;
import org.broadinstitute.sting.utils.StingException;
import org.broadinstitute.sting.utils.fasta.IndexedFastaSequenceFile;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
/**
* Created by IntelliJ IDEA.
* User: aaron
* Date: Dec 2, 2009
* Time: 1:05:58 AM
* <p/>
* some quick tests for the BasicVariation class
*/
public class BasicVariationTest extends BaseTest {
private static IndexedFastaSequenceFile seq;
@BeforeClass
public static void beforeTests() {
try {
seq = new IndexedFastaSequenceFile(new File(seqLocation + "/references/Homo_sapiens_assembly18/v0/Homo_sapiens_assembly18.fasta"));
} catch (FileNotFoundException e) {
throw new StingException("unable to load the sequence dictionary");
}
GenomeLocParser.setupRefContigOrdering(seq);
}
@Test
public void testIsBiallelic() {
BasicVariation var = new BasicVariation("CC", "C", 0, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertTrue(!var.isBiallelic());
BasicVariation var2 = new BasicVariation("CA", "C", 0, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertTrue(var2.isBiallelic());
BasicVariation var3 = new BasicVariation("CC", "A", 0, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertTrue(var3.isBiallelic());
}
@Test
public void testVariantType() {
// test reference
BasicVariation var = new BasicVariation("CC", "C", 0, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertTrue(var.getType() == Variation.VARIANT_TYPE.REFERENCE);
// test SNP's
BasicVariation var2 = new BasicVariation("CA", "C", 0, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertTrue(var2.getType() == Variation.VARIANT_TYPE.SNP);
BasicVariation var3 = new BasicVariation("AA", "C", 0, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertTrue(var3.getType() == Variation.VARIANT_TYPE.SNP);
// test deletions
BasicVariation var4 = new BasicVariation("", "C", -10, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertTrue(var4.getType() == Variation.VARIANT_TYPE.DELETION);
// test insertions
BasicVariation var5 = new BasicVariation("ACACACACACA", "C", "ACACACACACA".length(), GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertTrue(var5.getType() == Variation.VARIANT_TYPE.INSERTION);
}
@Test(expected = IllegalStateException.class)
public void testGetAlternativeBaseForSNPNotASNP() {
// test reference
BasicVariation var = new BasicVariation("CC", "C", 0, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
var.getAlternativeBaseForSNP();
}
@Test(expected = IllegalStateException.class)
public void testGetAlternativeBaseForSNPFromIndel() {
// test reference
BasicVariation var = new BasicVariation("ACACACACACA", "C", "ACACACACACA".length(), GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
var.getAlternativeBaseForSNP();
}
@Test(expected = IllegalStateException.class)
public void testGetAlternativeBaseForSNPFromDel() {
// test reference
BasicVariation var = new BasicVariation("", "C", -10, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
var.getAlternativeBaseForSNP();
}
@Test
public void testGetAlternativeBaseForSNP() {
// test SNP's
BasicVariation var = new BasicVariation("CA", "C", 0, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertEquals('A', var.getAlternativeBaseForSNP());
var = new BasicVariation("AC", "C", 0, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertEquals('A', var.getAlternativeBaseForSNP());
var = new BasicVariation("AA", "C", 0, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertEquals('A', var.getAlternativeBaseForSNP());
}
@Test
public void testGetAlleleList() {
BasicVariation var = new BasicVariation("CA", "C", 0, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertTrue(var.getAlleleList().size() == 2);
Assert.assertTrue(var.getAlleleList().contains("C"));
Assert.assertTrue(var.getAlleleList().contains("A"));
var = new BasicVariation("AC", "C", 0, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertTrue(var.getAlleleList().size() == 2);
Assert.assertTrue(var.getAlleleList().contains("C"));
Assert.assertTrue(var.getAlleleList().contains("A"));
var = new BasicVariation("AA", "C", 0, GenomeLocParser.createGenomeLoc(1, 1, 1), 1.22);
Assert.assertTrue(var.getAlleleList().size() == 2);
Assert.assertTrue(var.getAlleleList().get(0).equals("A"));
Assert.assertTrue(var.getAlleleList().get(1).equals("A"));
}
}