Skip to content

Fixing RAM issues with MAFToGVCF. This should reduce RAM requirement… #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/main/kotlin/biokotlin/genome/MAFToGVCF.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,19 @@ class MAFToGVCF {
val sampleName = variantsMap.keys.first()

// sort the variants by contig and position.
val variants = variantsMap.values.first().sortedWith(VariantContextComparator(contigList))
val variants = variantsMap.values.first().sortedBy { Position(it.chr,it.startPos) }

exportVariantContext(sampleName, variants, gvcfOutput, refSeqs)
exportVariantContext(sampleName, variants, gvcfOutput, refSeqs, outJustGT, delAsSymbolic, maxDeletionSize)
if (compressAndIndex) {
// compress and index the file with bgzip and tabix.
compressAndIndexFile(gvcfOutput)
}
} else if (variantsMap.size == 2) {
val outputNames = twoOutputFiles(gvcfOutput)
variantsMap.entries.forEachIndexed { index, (name, variants) ->
val sortedVariants = variants.sortedWith(VariantContextComparator(contigList))
val outputFile = exportVariantContext(name, sortedVariants, outputNames[index], refSeqs)
val sortedVariants = variants.sortedBy { Position(it.chr,it.startPos) }

val outputFile = exportVariantContext(name, sortedVariants, outputNames[index], refSeqs, outJustGT, delAsSymbolic, maxDeletionSize)
if (compressAndIndex) {
compressAndIndexFile(outputNames[index])
}
Expand All @@ -149,28 +150,28 @@ class MAFToGVCF {
delAsSymbolic: Boolean = false,
maxDeletionSize: Int = 0,
anchorwaveLegacy: Boolean = false
): Map<String, List<VariantContext>> {
): Map<String, List<AssemblyVariantInfo>> {

val mafRecords = loadMAFRecords(mafFile)
return if (twoGvcfs) {
val splitGenomes = splitMafRecordsIntoTwoGenomes(mafRecords)

if (splitGenomes.size == 1) {
println("getVariantContextsfromMAF:twoGvcfs is true but only 1 genome in the MAF file. Processing the single genome")
mapOf(sampleName to buildVariantsForAllAlignments(sampleName, mafRecords, refSeqs, fillGaps,outJustGT,outputType, delAsSymbolic, maxDeletionSize, anchorwaveLegacy))
mapOf(sampleName to buildVariantsForAllAlignments(mafRecords, refSeqs, fillGaps, outputType, anchorwaveLegacy))
} else {
splitGenomes.mapIndexed { index, mafrecs ->
splitGenomes.mapIndexed { index, splitMafRecords ->
//append _1 and _2 to the sampleName for the split genomes

val genomeName = "${sampleName}_${index + 1}"
println("MAFToGVCF:getVariantContextsfromMAF: Splitting ${sampleName} into ${genomeName}")
Pair(genomeName, buildVariantsForAllAlignments(genomeName, mafrecs, refSeqs, fillGaps, outJustGT, outputType, delAsSymbolic, maxDeletionSize, anchorwaveLegacy))
Pair(genomeName, buildVariantsForAllAlignments(splitMafRecords, refSeqs, fillGaps, outputType, anchorwaveLegacy))
}.toMap()
}

} else {
println("getVariantContextsfromMAF: Processing a single genome")
mapOf(sampleName to buildVariantsForAllAlignments(sampleName, mafRecords, refSeqs, fillGaps, outJustGT, outputType, delAsSymbolic, maxDeletionSize, anchorwaveLegacy))
mapOf(sampleName to buildVariantsForAllAlignments(mafRecords, refSeqs, fillGaps, outputType, anchorwaveLegacy))
}

}
Expand Down Expand Up @@ -237,16 +238,12 @@ class MAFToGVCF {
* Function to build the variants for all the alignments.
*/
fun buildVariantsForAllAlignments(
sampleName: String,
mafRecords: List<MAFRecord>,
refGenomeSequence: Map<String, NucSeq>,
fillGaps: Boolean,
outJustGT: Boolean,
outputType: OUTPUT_TYPE,
delAsSymbolic: Boolean,
maxDeletionSize: Int,
anchorwaveLegacy: Boolean = false
): List<VariantContext> {
): List<AssemblyVariantInfo> {
var variantInfos = mutableListOf<AssemblyVariantInfo>()

for (record in mafRecords) {
Expand All @@ -263,7 +260,7 @@ class MAFToGVCF {
variantInfos = fillInMissingVariantBlocks(variantInfos, refGenomeSequence, true)
}

return createVariantContextsFromInfo(sampleName, variantInfos, outJustGT, delAsSymbolic, maxDeletionSize)
return variantInfos
}

fun removeRefBlocks(variantInfos: MutableList<AssemblyVariantInfo>) : MutableList<AssemblyVariantInfo> {
Expand Down Expand Up @@ -1117,9 +1114,12 @@ class MAFToGVCF {
*/
fun exportVariantContext(
sampleName: String,
variantContexts: List<VariantContext>,
variantContexts: List<AssemblyVariantInfo>,
outputFileName: String,
refGenomeSequence: Map<String, NucSeq>
refGenomeSequence: Map<String, NucSeq>,
outputJustGT: Boolean,
delAsSymbolic: Boolean,
maxDeletionSize: Int
) {
val writer = VariantContextWriterBuilder()
.unsetOption(Options.INDEX_ON_THE_FLY)
Expand All @@ -1132,7 +1132,7 @@ class MAFToGVCF {
addSequenceDictionary(header, refGenomeSequence)
writer.writeHeader(header)
for (variant in variantContexts) {
writer.add(variant)
writer.add(convertVariantInfoToContext(sampleName, variant, outputJustGT, delAsSymbolic, maxDeletionSize))
}

writer.close()
Expand Down
18 changes: 18 additions & 0 deletions src/test/kotlin/biokotlin/genome/MAFToGVCFTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,24 @@ class MAFToGVCFTest : StringSpec({

}
}

"TestSort" {
val chr1 = "1"
val chr2 = "2"
val chr10 = "10"

val asmInfos = listOf(AssemblyVariantInfo(chr1,10,20,"0","A","T",true, intArrayOf(10,20), "chr1", 10, 20, "+"),
AssemblyVariantInfo(chr2,10,20,"0","A","T",true, intArrayOf(10,20), "chr2", 10, 20, "+"),
AssemblyVariantInfo(chr10,10,20,"0","A","T",true, intArrayOf(10,20), "chr10", 10, 20, "+"),
AssemblyVariantInfo(chr2,30,40,"0","A","T",true, intArrayOf(10,20), "chr2", 10, 20, "+"),
AssemblyVariantInfo(chr1,1,9,"0","A","T",true, intArrayOf(10,20), "chr1", 10, 20, "+"),
)

val expectedInfos = listOf(Pair(chr1,1), Pair(chr1,10), Pair(chr2,10), Pair(chr2,30), Pair(chr10,10))

val sorted = asmInfos.sortedBy { Position(it.chr,it.startPos) }
sorted.map { Pair(it.chr,it.startPos) } shouldBe expectedInfos
}

})

Expand Down