Commit daa546d8 authored by Sander Bollen's avatar Sander Bollen

fix fetching of secondary records

parent 77816eac
......@@ -130,9 +130,9 @@ object VcfWithVcf extends ToolCommand {
require(vcfDict.getSequence(record.getContig) != null, s"Contig ${record.getContig} does not exist on reference")
val secondaryRecords = getSecondaryRecords(secondaryReader, record, commandArgs.matchAllele)
val fieldMap = createFieldMap(commandArgs.fields, secondaryRecords)
val fieldMap = createFieldMap(commandArgs.fields, record, secondaryRecords, secondHeader)
writer.add(createRecord(fieldMap, record, commandArgs.fields, header, secondaryRecords))
writer.add(createRecord(fieldMap, record, commandArgs.fields, header))
counter += 1
if (counter % 100000 == 0) {
......@@ -151,17 +151,19 @@ object VcfWithVcf extends ToolCommand {
/**
* Create Map of field -> List of attributes in secondary records
* @param fields List of Field
* @param record Original record
* @param secondaryRecords List of VariantContext with secondary records
* @param header: header of secondary reader
* @return Map of fields and their values in secondary records
*/
def createFieldMap(fields: List[Fields], secondaryRecords: List[VariantContext]): Map[String, List[Any]] = {
def createFieldMap(fields: List[Fields], record: VariantContext, secondaryRecords: List[VariantContext], header: VCFHeader): Map[String, List[Any]] = {
val fieldMap = (for (
f <- fields if secondaryRecords.exists(_.hasAttribute(f.inputField))
) yield {
f.outputField -> (for (
secondRecord <- secondaryRecords if secondRecord.hasAttribute(f.inputField)
) yield {
secondRecord.getAttribute(f.inputField) match {
getSecondaryField(record, secondRecord, f.inputField, header) match {
case l: List[_] => l
case y: util.ArrayList[_] => y.toList
case x => List(x)
......@@ -188,8 +190,9 @@ object VcfWithVcf extends ToolCommand {
}
}
def createRecord(fieldMap: Map[String, List[Any]], record: VariantContext,
fields: List[Fields], header: VCFHeader, secondaryRecords: List[VariantContext]): VariantContext = {
fields: List[Fields], header: VCFHeader): VariantContext = {
fieldMap.foldLeft(new VariantContextBuilder(record))((builder, attribute) => {
builder.attribute(attribute._1, fields.filter(_.outputField == attribute._1).head.fieldMethod match {
case FieldMethod.max =>
......@@ -206,16 +209,28 @@ object VcfWithVcf extends ToolCommand {
}
case FieldMethod.unique => scalaListToJavaObjectArrayList(attribute._2.distinct)
case _ => {
header.getInfoHeaderLine(attribute._1).getCountType match {
case VCFHeaderLineCount.A => scalaListToJavaObjectArrayList(secondaryRecords.flatMap(x => numberA(x, record, attribute._1)))
case VCFHeaderLineCount.R => scalaListToJavaObjectArrayList(secondaryRecords.flatMap(x => numberR(x, record, attribute._1)))
case _ => scalaListToJavaObjectArrayList(attribute._2)
}
scalaListToJavaObjectArrayList(attribute._2)
}
})
}).make()
}
/**
* Get the proper representation of a field from a secondary record given an original record
* @param record original record
* @param secondaryRecord secondary record
* @param field field
* @param header header of secondary record
* @return
*/
def getSecondaryField(record: VariantContext, secondaryRecord: VariantContext, field: String, header: VCFHeader): Any = {
header.getInfoHeaderLine(field).getCountType match {
case VCFHeaderLineCount.A => numberA(record, secondaryRecord, field)
case VCFHeaderLineCount.R => numberR(record, secondaryRecord, field)
case _ => secondaryRecord.getAttribute(field)
}
}
/**
* Get the correct values from a field that has number=A
* @param referenceRecord the reference record
......
##fileformat=VCFv4.1
##fileformat=VCFv4.2
##INFO=<ID=DN,Number=1,Type=Integer,Description="inDbSNP">
##INFO=<ID=DT,Number=0,Type=Flag,Description="in1000Genomes">
##INFO=<ID=DA,Number=1,Type=String,Description="allelesDBSNP">
......
##fileformat=VCFv4.1
##fileformat=VCFv4.2
##INFO=<ID=DN,Number=1,Type=Integer,Description="inDbSNP">
##INFO=<ID=DT,Number=0,Type=Flag,Description="in1000Genomes">
##INFO=<ID=DA,Number=1,Type=String,Description="allelesDBSNP">
......
......@@ -114,7 +114,9 @@ class VcfWithVcfTest extends TestNGSuite with MockitoSugar with Matchers {
@Test
def testFieldMap = {
val unvepRecord = new VCFFileReader(new File(unveppedPath)).iterator().next()
val unvepReader = new VCFFileReader(new File(unveppedPath))
val header = unvepReader.getFileHeader
val unvepRecord = unvepReader.iterator().next()
var fields = List(new Fields("FG", "FG"))
fields :::= List(new Fields("FD", "FD"))
......@@ -142,7 +144,7 @@ class VcfWithVcfTest extends TestNGSuite with MockitoSugar with Matchers {
fields :::= List(new Fields("VQSLOD", "VQSLOD"))
fields :::= List(new Fields("culprit", "culprit"))
val fieldMap = createFieldMap(fields, List(unvepRecord))
val fieldMap = createFieldMap(fields, unvepRecord, List(unvepRecord), header)
fieldMap("FG") shouldBe List("intron")
fieldMap("FD") shouldBe List("unknown")
......@@ -191,8 +193,8 @@ class VcfWithVcfTest extends TestNGSuite with MockitoSugar with Matchers {
val secRec = getSecondaryRecords(vepReader, unvepRecord, false)
val fieldMap = createFieldMap(List(new Fields("CSQ", "CSQ")), secRec)
val createdRecord = createRecord(fieldMap, unvepRecord, List(new Fields("CSQ", "CSQ")), header, secRec)
val fieldMap = createFieldMap(List(new Fields("CSQ", "CSQ")), vepRecord, secRec, header)
val createdRecord = createRecord(fieldMap, unvepRecord, List(new Fields("CSQ", "CSQ")), header)
identicalVariantContext(createdRecord, vepRecord) shouldBe true
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment