Skip to content

Commit

Permalink
fix postprocess of nodes and the same clones in the tree
Browse files Browse the repository at this point in the history
  • Loading branch information
gnefedev committed Jun 20, 2022
1 parent 01b3e58 commit 81ccc85
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import kotlin.math.abs

class BuildSHMTreeReport : AbstractCommandReport() {
@get:JsonProperty("stepResults")
val stepResults: MutableList<StepResult> = mutableListOf()
val stepResults = mutableListOf<StepResult>()
override fun getCommand() = CommandFindShmTrees.COMMAND_NAME

fun onStepEnd(step: BuildSHMTreeStep, clonesWasAdded: Int, treesCountDelta: Int) {
Expand Down Expand Up @@ -197,6 +197,8 @@ class BuildSHMTreeReport : AbstractCommandReport() {
helper.writeField("Trees combined", -stepResult.treesCountDelta)
}
}
helper.writeField("Total trees count", stepResults.sumOf { it.treesCountDelta })
helper.writeField("Total clones count in trees", stepResults.sumOf { it.clonesWasAdded })
}

private fun printPages(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import com.milaboratory.mixcr.trees.SHMTreesReader
import com.milaboratory.mixcr.trees.forPostanalysis
import io.repseq.core.VDJCLibraryRegistry
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters
import kotlin.io.path.Path
import kotlin.io.path.writeText
Expand All @@ -33,9 +32,6 @@ class CommandExportShmTreesNewick : ACommandWithOutputMiXCR() {
@Parameters(arity = "2", description = ["input_file.hsmt output_dir"])
var inOut: List<String> = ArrayList()

@Option(description = ["Output column headers with spaces."], names = ["-v", "--with-spaces"])
var humanReadable = false

override fun getInputFiles(): List<String> = listOf(inOut.first())

override fun getOutputFiles(): List<String> = listOf(inOut.last())
Expand Down
17 changes: 11 additions & 6 deletions src/main/kotlin/com/milaboratory/mixcr/trees/ClusterProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ internal class ClusterProcessor private constructor(
}
}
}
val notOverlappedCliques: MutableList<BitArrayInt> = ArrayList()
val notOverlappedCliques = mutableListOf<BitArrayInt>()
matrix.calculateMaximalCliques()
.filter { it.bitCount() > 1 }
.sortedByDescending { it.bitCount() }
Expand All @@ -358,11 +358,16 @@ internal class ClusterProcessor private constructor(
notOverlappedCliques.add(clique)
}
}
val clusters: MutableList<Cluster<CloneWithMutationsFromVJGermline>> = ArrayList()
for (clique in notOverlappedCliques) {
clusters.add(Cluster(clique.bits.map { index: Int -> clones[index] }))
}
return clusters
return notOverlappedCliques
.map { Cluster(it.bits.map { i -> clones[i] }) }
.filter {
//skip cluster if it formed by the same clones but with different C or from different samples
val toCompare = it.cluster.first()
it.cluster.subList(1, it.cluster.size)
.any { clone ->
!Arrays.equals(clone.cloneWrapper.clone.targets, toCompare.cloneWrapper.clone.targets)
}
}
}

private fun commonMutationsCount(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,8 @@ class TreeBuilderByAncestors<T, E, M> private constructor(
private val sumOfDistancesFromAncestor: BigDecimal,
private val distanceFromParentToReplaceWhat: BigDecimal
) : Action() {
override fun changeOfDistance(): BigDecimal {
return distanceFromParentToCommon.subtract(distanceFromParentToReplaceWhat).add(sumOfDistancesFromAncestor)
}
override fun changeOfDistance(): BigDecimal =
distanceFromParentToCommon - distanceFromParentToReplaceWhat + sumOfDistancesFromAncestor

override fun distanceFromObserved(): BigDecimal {
return (parent.content as Reconstructed<T, E>).minDistanceFromObserved
Expand All @@ -335,38 +334,45 @@ class TreeBuilderByAncestors<T, E, M> private constructor(
)
}

private fun postprocess(parentToPostprocess: Tree.Node<ObservedOrReconstructed<T, E>>): Tree.Node<ObservedOrReconstructed<T, E>> {
return Tree.Node(
private fun postprocess(parentToPostprocess: Tree.Node<ObservedOrReconstructed<T, E>>): Tree.Node<ObservedOrReconstructed<T, E>> =
Tree.Node(
parentToPostprocess.content,
postprocess(
(parentToPostprocess.content as Reconstructed<T, E>).content,
parentToPostprocess.links
)
)
}

private fun postprocess(
parentContent: E, links: List<NodeLink<ObservedOrReconstructed<T, E>>>
): List<NodeLink<ObservedOrReconstructed<T, E>>> = links
.map { link ->
.flatMap { link ->
val child = link.node
when (child.content) {
is Reconstructed<*, *> -> {
val childContent = child.content as Reconstructed<T, E>
val mapped = postprocessDescendants(parentContent, childContent.content)
NodeLink(
Tree.Node(
Reconstructed(
mapped, //TODO recalculate minDistanceFromObserved
childContent.minDistanceFromObserved,
++counter
val newDistance = distance(parentContent, mutationsBetween(parentContent, mapped))
if (newDistance.compareTo(BigDecimal.ZERO) == 0) {
//actually parent equals mapped result.
//in this case we should remove this node from tree by skipping it and inserting its children directly
postprocess(parentContent, child.links)
} else {
val result = NodeLink(
Tree.Node(
Reconstructed(
mapped, //TODO recalculate minDistanceFromObserved
childContent.minDistanceFromObserved,
++counter
),
postprocess(mapped, child.links)
),
postprocess(mapped, child.links)
),
distance(parentContent, mutationsBetween(parentContent, mapped))
)
newDistance
)
listOf(result)
}
}
else -> link
else -> listOf(link)
}
}
}
Expand Down

0 comments on commit 81ccc85

Please sign in to comment.