VCF reader now creates the correct type of header line for each header type
git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2423 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
parent
c0c9f6c8b6
commit
cf303810d3
|
|
@ -26,6 +26,24 @@ public class VCFFilterHeaderLine extends VCFHeaderLine {
|
||||||
mDescription = description;
|
mDescription = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create a VCF info header line
|
||||||
|
*
|
||||||
|
* @param line the header line
|
||||||
|
*/
|
||||||
|
protected VCFFilterHeaderLine(String line) {
|
||||||
|
super("FILTER", "");
|
||||||
|
String[] pieces = line.split(",");
|
||||||
|
if ( pieces.length < 2 )
|
||||||
|
throw new IllegalArgumentException("There are too few values in the VCF FILTER header line: " + line);
|
||||||
|
|
||||||
|
mName = pieces[0];
|
||||||
|
mDescription = pieces[1];
|
||||||
|
// just in case there were some commas in the description
|
||||||
|
for (int i = 1; i < pieces.length; i++)
|
||||||
|
mDescription += "," + pieces[i];
|
||||||
|
}
|
||||||
|
|
||||||
protected String makeStringRep() {
|
protected String makeStringRep() {
|
||||||
return String.format("FILTER=%s,\"%s\"", mName, mDescription);
|
return String.format("FILTER=%s,\"%s\"", mName, mDescription);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,26 @@ public class VCFFormatHeaderLine extends VCFHeaderLine {
|
||||||
mDescription = description;
|
mDescription = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create a VCF format header line
|
||||||
|
*
|
||||||
|
* @param line the header line
|
||||||
|
*/
|
||||||
|
protected VCFFormatHeaderLine(String line) {
|
||||||
|
super("FORMAT", "");
|
||||||
|
String[] pieces = line.split(",");
|
||||||
|
if ( pieces.length < 4 )
|
||||||
|
throw new IllegalArgumentException("There are too few values in the VCF FORMAT header line: " + line);
|
||||||
|
|
||||||
|
mName = pieces[0];
|
||||||
|
mCount = Integer.valueOf(pieces[1]);
|
||||||
|
mType = INFO_TYPE.valueOf(pieces[2]);
|
||||||
|
mDescription = pieces[3];
|
||||||
|
// just in case there were some commas in the description
|
||||||
|
for (int i = 4; i < pieces.length; i++)
|
||||||
|
mDescription += "," + pieces[i];
|
||||||
|
}
|
||||||
|
|
||||||
protected String makeStringRep() {
|
protected String makeStringRep() {
|
||||||
return String.format("FORMAT=%s,%d,%s,\"%s\"", mName, mCount, mType.toString(), mDescription);
|
return String.format("FORMAT=%s,%d,%s,\"%s\"", mName, mCount, mType.toString(), mDescription);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,26 @@ public class VCFInfoHeaderLine extends VCFHeaderLine {
|
||||||
mDescription = description;
|
mDescription = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create a VCF info header line
|
||||||
|
*
|
||||||
|
* @param line the header line
|
||||||
|
*/
|
||||||
|
protected VCFInfoHeaderLine(String line) {
|
||||||
|
super("INFO", "");
|
||||||
|
String[] pieces = line.split(",");
|
||||||
|
if ( pieces.length < 4 )
|
||||||
|
throw new IllegalArgumentException("There are too few values in the VCF INFO header line: " + line);
|
||||||
|
|
||||||
|
mName = pieces[0];
|
||||||
|
mCount = Integer.valueOf(pieces[1]);
|
||||||
|
mType = INFO_TYPE.valueOf(pieces[2]);
|
||||||
|
mDescription = pieces[3];
|
||||||
|
// just in case there were some commas in the description
|
||||||
|
for (int i = 4; i < pieces.length; i++)
|
||||||
|
mDescription += "," + pieces[i];
|
||||||
|
}
|
||||||
|
|
||||||
protected String makeStringRep() {
|
protected String makeStringRep() {
|
||||||
return String.format("INFO=%s,%d,%s,\"%s\"", mName, mCount, mType.toString(), mDescription);
|
return String.format("INFO=%s,%d,%s,\"%s\"", mName, mCount, mType.toString(), mDescription);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -169,11 +169,19 @@ public class VCFReader implements Iterator<VCFRecord>, Iterable<VCFRecord> {
|
||||||
arrayIndex++;
|
arrayIndex++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if ( str.startsWith("##INFO=") )
|
||||||
|
metaData.add(new VCFInfoHeaderLine(str.substring(7)));
|
||||||
|
else if ( str.startsWith("##FILTER=") )
|
||||||
|
metaData.add(new VCFFilterHeaderLine(str.substring(7)));
|
||||||
|
else if ( str.startsWith("##FORMAT=") )
|
||||||
|
metaData.add(new VCFFormatHeaderLine(str.substring(7)));
|
||||||
|
else {
|
||||||
int equals = str.indexOf("=");
|
int equals = str.indexOf("=");
|
||||||
if ( equals != -1 )
|
if ( equals != -1 )
|
||||||
metaData.add(new VCFHeaderLine(str.substring(2, equals), str.substring(equals+1)));
|
metaData.add(new VCFHeaderLine(str.substring(2, equals), str.substring(equals+1)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return new VCFHeader(metaData, auxTags);
|
return new VCFHeader(metaData, auxTags);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,10 +10,7 @@ import org.junit.Test;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/** test the VCFReader class test */
|
/** test the VCFReader class test */
|
||||||
public class VCFReaderTest extends BaseTest {
|
public class VCFReaderTest extends BaseTest {
|
||||||
|
|
@ -206,6 +203,23 @@ public class VCFReaderTest extends BaseTest {
|
||||||
public void testComplexExample() {
|
public void testComplexExample() {
|
||||||
VCFReader reader = new VCFReader(complexFile);
|
VCFReader reader = new VCFReader(complexFile);
|
||||||
|
|
||||||
|
// test header
|
||||||
|
Set<VCFHeaderLine> headerLines = reader.getHeader().getMetaData();
|
||||||
|
int formatCount = 0;
|
||||||
|
int infoCount = 0;
|
||||||
|
int filterCount = 0;
|
||||||
|
for ( VCFHeaderLine line : headerLines ) {
|
||||||
|
if ( line instanceof VCFFormatHeaderLine )
|
||||||
|
formatCount++;
|
||||||
|
else if ( line instanceof VCFInfoHeaderLine )
|
||||||
|
infoCount++;
|
||||||
|
else if ( line instanceof VCFFilterHeaderLine )
|
||||||
|
filterCount++;
|
||||||
|
}
|
||||||
|
Assert.assertEquals(3, formatCount);
|
||||||
|
Assert.assertEquals(4, infoCount);
|
||||||
|
Assert.assertEquals(0, filterCount);
|
||||||
|
|
||||||
// record #1: test dbsnp id
|
// record #1: test dbsnp id
|
||||||
if (!reader.hasNext()) Assert.fail("The reader should have a record");
|
if (!reader.hasNext()) Assert.fail("The reader should have a record");
|
||||||
VCFRecord rec = reader.next();
|
VCFRecord rec = reader.next();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue