2009-05-15 05:06:28 +08:00
|
|
|
// our package
|
|
|
|
|
package org.broadinstitute.sting.gatk.refdata;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the imports for unit testing.
|
|
|
|
|
|
|
|
|
|
import org.junit.*;
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
import org.broadinstitute.sting.BaseTest;
|
|
|
|
|
import org.broadinstitute.sting.utils.fasta.FastaSequenceFile2;
|
2009-06-22 22:39:41 +08:00
|
|
|
import org.broadinstitute.sting.utils.GenomeLocParser;
|
2009-05-15 05:06:28 +08:00
|
|
|
|
|
|
|
|
import java.io.File;
|
2009-05-15 07:20:11 +08:00
|
|
|
import java.io.PrintStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.FileNotFoundException;
|
2009-05-15 05:06:28 +08:00
|
|
|
import java.util.Arrays;
|
2009-05-15 07:20:11 +08:00
|
|
|
import java.util.ArrayList;
|
2009-05-15 05:06:28 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Basic unit test for TabularROD
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public class TabularRODTest extends BaseTest {
|
|
|
|
|
private static FastaSequenceFile2 seq;
|
|
|
|
|
private ReferenceOrderedData ROD;
|
2009-06-06 07:34:37 +08:00
|
|
|
private RODIterator iter;
|
2009-05-15 07:20:11 +08:00
|
|
|
|
2009-05-15 05:06:28 +08:00
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
public static void init() {
|
|
|
|
|
// sequence
|
|
|
|
|
seq = new FastaSequenceFile2(new File(seqLocation + "/references/Homo_sapiens_assembly18/v0/Homo_sapiens_assembly18.fasta"));
|
2009-06-22 22:39:41 +08:00
|
|
|
GenomeLocParser.setupRefContigOrdering(seq);
|
2009-05-15 05:06:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setupTabularROD() {
|
2009-05-15 07:20:11 +08:00
|
|
|
TabularROD.setDelimiter(TabularROD.DEFAULT_DELIMITER, TabularROD.DEFAULT_DELIMITER_REGEX);
|
2009-05-15 05:06:28 +08:00
|
|
|
File file = new File(testDir + "TabularDataTest.dat");
|
|
|
|
|
ROD = new ReferenceOrderedData("tableTest", file, TabularROD.class);
|
|
|
|
|
iter = ROD.iterator();
|
2009-05-15 07:20:11 +08:00
|
|
|
|
2009-05-15 05:06:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test1() {
|
|
|
|
|
logger.warn("Executing test1");
|
|
|
|
|
TabularROD one = (TabularROD)iter.next();
|
2009-06-16 23:49:26 +08:00
|
|
|
assertTrue(one.size() == 4);
|
2009-06-22 22:39:41 +08:00
|
|
|
assertTrue(one.getLocation().equals(GenomeLocParser.createGenomeLoc("chrM", 10)));
|
2009-05-15 05:06:28 +08:00
|
|
|
assertTrue(one.get("COL1").equals("A"));
|
|
|
|
|
assertTrue(one.get("COL2").equals("B"));
|
|
|
|
|
assertTrue(one.get("COL3").equals("C"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test2() {
|
|
|
|
|
logger.warn("Executing test2");
|
|
|
|
|
TabularROD one = (TabularROD)iter.next();
|
|
|
|
|
TabularROD two = (TabularROD)iter.next();
|
2009-06-16 23:49:26 +08:00
|
|
|
assertTrue(two.size() == 4);
|
2009-06-22 22:39:41 +08:00
|
|
|
assertTrue(two.getLocation().equals(GenomeLocParser.createGenomeLoc("chrM", 20)));
|
2009-05-15 05:06:28 +08:00
|
|
|
assertTrue(two.get("COL1").equals("C"));
|
|
|
|
|
assertTrue(two.get("COL2").equals("D"));
|
|
|
|
|
assertTrue(two.get("COL3").equals("E"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test3() {
|
|
|
|
|
logger.warn("Executing test3");
|
|
|
|
|
TabularROD one = (TabularROD)iter.next();
|
|
|
|
|
TabularROD two = (TabularROD)iter.next();
|
|
|
|
|
TabularROD three = (TabularROD)iter.next();
|
2009-06-16 23:49:26 +08:00
|
|
|
assertTrue(three.size() == 4);
|
2009-06-22 22:39:41 +08:00
|
|
|
assertTrue(three.getLocation().equals(GenomeLocParser.createGenomeLoc("chrM", 30)));
|
2009-05-15 05:06:28 +08:00
|
|
|
assertTrue(three.get("COL1").equals("F"));
|
|
|
|
|
assertTrue(three.get("COL2").equals("G"));
|
|
|
|
|
assertTrue(three.get("COL3").equals("H"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testDone() {
|
|
|
|
|
logger.warn("Executing testDone");
|
|
|
|
|
TabularROD one = (TabularROD)iter.next();
|
|
|
|
|
TabularROD two = (TabularROD)iter.next();
|
|
|
|
|
TabularROD three = (TabularROD)iter.next();
|
|
|
|
|
assertTrue(!iter.hasNext());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testSeek() {
|
|
|
|
|
logger.warn("Executing testSeek");
|
2009-06-22 22:39:41 +08:00
|
|
|
TabularROD two = (TabularROD)iter.seekForward(GenomeLocParser.createGenomeLoc("chrM", 20));
|
2009-06-16 23:49:26 +08:00
|
|
|
assertTrue(two.size() == 4);
|
2009-06-22 22:39:41 +08:00
|
|
|
assertTrue(two.getLocation().equals(GenomeLocParser.createGenomeLoc("chrM", 20)));
|
2009-05-15 05:06:28 +08:00
|
|
|
assertTrue(two.get("COL1").equals("C"));
|
|
|
|
|
assertTrue(two.get("COL2").equals("D"));
|
|
|
|
|
assertTrue(two.get("COL3").equals("E"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testToString() {
|
|
|
|
|
logger.warn("Executing testToString");
|
|
|
|
|
TabularROD one = (TabularROD)iter.next();
|
|
|
|
|
assertTrue(one.toString().equals("chrM:10\tA\tB\tC"));
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-15 07:20:11 +08:00
|
|
|
// Didn't change the delimiter
|
|
|
|
|
@Test (expected = RuntimeException.class)
|
|
|
|
|
public void testDelim1() {
|
|
|
|
|
File file2 = new File(testDir + "TabularDataTest2.dat");
|
|
|
|
|
ReferenceOrderedData ROD_commas = new ReferenceOrderedData("tableTest", file2, TabularROD.class);
|
2009-06-06 07:34:37 +08:00
|
|
|
RODIterator iter_commas = ROD_commas.iterator();
|
2009-05-15 07:20:11 +08:00
|
|
|
|
|
|
|
|
logger.warn("Executing testDelim1");
|
|
|
|
|
TabularROD one2 = (TabularROD)iter_commas.next();
|
2009-06-16 23:49:26 +08:00
|
|
|
assertTrue(one2.size() == 5);
|
2009-06-22 22:39:41 +08:00
|
|
|
assertTrue(one2.getLocation().equals(GenomeLocParser.createGenomeLoc("chrM", 10)));
|
2009-05-15 07:20:11 +08:00
|
|
|
assertTrue(one2.get("COL1").equals("A"));
|
|
|
|
|
assertTrue(one2.get("COL2").equals("B"));
|
|
|
|
|
assertTrue(one2.get("COL3").equals("C"));
|
|
|
|
|
assertTrue(one2.get("COL4").equals("1"));
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-15 05:06:28 +08:00
|
|
|
@Test
|
2009-05-15 07:20:11 +08:00
|
|
|
public void testDelim2() {
|
|
|
|
|
TabularROD.setDelimiter(",",",");
|
|
|
|
|
File file2 = new File(testDir + "TabularDataTest2.dat");
|
|
|
|
|
ReferenceOrderedData ROD_commas = new ReferenceOrderedData("tableTest", file2, TabularROD.class);
|
2009-06-06 07:34:37 +08:00
|
|
|
RODIterator iter_commas = ROD_commas.iterator();
|
2009-05-15 07:20:11 +08:00
|
|
|
|
|
|
|
|
logger.warn("Executing testDelim1");
|
|
|
|
|
TabularROD one2 = (TabularROD)iter_commas.next();
|
2009-06-16 23:49:26 +08:00
|
|
|
assertTrue(one2.size() == 5);
|
2009-06-22 22:39:41 +08:00
|
|
|
assertTrue(one2.getLocation().equals(GenomeLocParser.createGenomeLoc("chrM", 10)));
|
2009-05-15 05:06:28 +08:00
|
|
|
assertTrue(one2.get("COL1").equals("A"));
|
|
|
|
|
assertTrue(one2.get("COL2").equals("B"));
|
|
|
|
|
assertTrue(one2.get("COL3").equals("C"));
|
|
|
|
|
assertTrue(one2.get("COL4").equals("1"));
|
|
|
|
|
}
|
2009-05-15 07:20:11 +08:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testCreation() {
|
|
|
|
|
logger.warn("Executing testCreation");
|
|
|
|
|
ArrayList<String> header = new ArrayList<String>(Arrays.asList("HEADER", "col1", "col2", "col3"));
|
|
|
|
|
assertTrue(TabularROD.headerString(header).equals("HEADER\tcol1\tcol2\tcol3"));
|
|
|
|
|
String rowData = String.format("%d %d %d", 1, 2, 3);
|
2009-06-22 22:39:41 +08:00
|
|
|
TabularROD row = new TabularROD("myName", header, GenomeLocParser.createGenomeLoc("chrM", 1), rowData.split(" "));
|
2009-06-16 23:49:26 +08:00
|
|
|
System.out.println(">>>>> " + row.toString());
|
2009-05-15 07:20:11 +08:00
|
|
|
assertTrue(row.toString().equals("chrM:1\t1\t2\t3"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testCreationAndWriting() throws FileNotFoundException {
|
|
|
|
|
logger.warn("Executing testCreationAndWriting");
|
|
|
|
|
|
|
|
|
|
File outputFile = new File(testDir + "testTabularRodOutputTemp.dat");
|
|
|
|
|
PrintStream out = new PrintStream(new FileOutputStream(outputFile));
|
|
|
|
|
|
|
|
|
|
ArrayList<String> header = new ArrayList<String>(Arrays.asList("HEADER", "col1", "col2", "col3"));
|
|
|
|
|
out.println(TabularROD.commentString("Hello, created from test"));
|
|
|
|
|
out.println(TabularROD.commentString(""));
|
|
|
|
|
out.println(TabularROD.headerString(header));
|
|
|
|
|
|
|
|
|
|
String rowData = String.format("%d %d %d", 1, 2, 3);
|
2009-06-22 22:39:41 +08:00
|
|
|
TabularROD row = new TabularROD("myName", header, GenomeLocParser.createGenomeLoc("chrM", 1), rowData.split(" "));
|
2009-05-15 07:20:11 +08:00
|
|
|
out.println(row.toString());
|
|
|
|
|
|
|
|
|
|
rowData = String.format("%d %d %d", 3, 4, 5);
|
2009-06-22 22:39:41 +08:00
|
|
|
row = new TabularROD("myName", header, GenomeLocParser.createGenomeLoc("chrM", 2), rowData.split(" "));
|
2009-05-15 07:20:11 +08:00
|
|
|
out.println(row.toString());
|
|
|
|
|
|
|
|
|
|
ReferenceOrderedData ROD_commas = new ReferenceOrderedData("tableTest", outputFile, TabularROD.class);
|
2009-06-06 07:34:37 +08:00
|
|
|
RODIterator iter_commas = ROD_commas.iterator();
|
2009-05-15 07:20:11 +08:00
|
|
|
|
|
|
|
|
TabularROD one = (TabularROD)iter_commas.next();
|
2009-06-16 23:49:26 +08:00
|
|
|
assertTrue(one.size() == 4);
|
2009-06-22 22:39:41 +08:00
|
|
|
assertTrue(one.getLocation().equals(GenomeLocParser.createGenomeLoc("chrM", 1)));
|
2009-05-15 07:20:11 +08:00
|
|
|
assertTrue(one.get("col1").equals("1"));
|
|
|
|
|
assertTrue(one.get("col2").equals("2"));
|
|
|
|
|
assertTrue(one.get("col3").equals("3"));
|
|
|
|
|
|
|
|
|
|
TabularROD two = (TabularROD)iter_commas.next();
|
2009-06-16 23:49:26 +08:00
|
|
|
assertTrue(two.size() == 4);
|
2009-06-22 22:39:41 +08:00
|
|
|
assertTrue(two.getLocation().equals(GenomeLocParser.createGenomeLoc("chrM", 2)));
|
2009-05-15 07:20:11 +08:00
|
|
|
assertTrue(two.get("col1").equals("3"));
|
|
|
|
|
assertTrue(two.get("col2").equals("4"));
|
|
|
|
|
assertTrue(two.get("col3").equals("5"));
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-07 06:02:04 +08:00
|
|
|
/* @Test (expected=RuntimeException.class )
|
2009-05-15 07:20:11 +08:00
|
|
|
public void testBadHeader1() {
|
|
|
|
|
logger.warn("Executing testBadHeader1");
|
|
|
|
|
ArrayList<String> header = new ArrayList<String>();
|
2009-06-22 22:39:41 +08:00
|
|
|
TabularROD row = new TabularROD("myName", header, GenomeLocParser.createGenomeLoc("chrM", 1));
|
2009-07-07 06:02:04 +08:00
|
|
|
}*/
|
2009-05-15 07:20:11 +08:00
|
|
|
|
2009-07-07 06:02:04 +08:00
|
|
|
/* @Test (expected=RuntimeException.class )
|
2009-05-15 07:20:11 +08:00
|
|
|
public void testBadHeader2() {
|
|
|
|
|
logger.warn("Executing testBadHeader2");
|
|
|
|
|
ArrayList<String> header = new ArrayList<String>(Arrays.asList("col1", "col2", "col3"));
|
2009-06-22 22:39:41 +08:00
|
|
|
TabularROD row = new TabularROD("myName", header, GenomeLocParser.createGenomeLoc("chrM", 1));
|
2009-07-07 06:02:04 +08:00
|
|
|
}*/
|
2009-05-15 07:20:11 +08:00
|
|
|
|
|
|
|
|
@Test (expected=RuntimeException.class )
|
|
|
|
|
public void testBadData1() {
|
|
|
|
|
logger.warn("Executing testBadData1");
|
|
|
|
|
ArrayList<String> header = new ArrayList<String>(Arrays.asList("HEADER", "col1", "col2", "col3"));
|
|
|
|
|
assertTrue(TabularROD.headerString(header).equals("HEADER\tcol1\tcol2\tcol3"));
|
|
|
|
|
String rowData = String.format("%d %d %d %d", 1, 2, 3, 4);
|
2009-06-22 22:39:41 +08:00
|
|
|
TabularROD row = new TabularROD("myName", header, GenomeLocParser.createGenomeLoc("chrM", 1), rowData.split(" "));
|
2009-05-15 07:20:11 +08:00
|
|
|
}
|
2009-05-15 05:06:28 +08:00
|
|
|
}
|