Search in sources :

Example 1 with Joining

use of io.repseq.core.GeneType.Joining in project mixcr by milaboratory.

the class FullSeqAssembler method buildClone.

/* ================================= Re-align and build final clone ====================================== */
private Clone buildClone(double count, BranchSequences targets) {
    Alignment<NucleotideSequence>[] vTopHitAlignments = new Alignment[targets.ranges.length], jTopHitAlignments = new Alignment[targets.ranges.length];
    VDJCHit hit = clone.getBestHit(Variable);
    if (hit == null)
        throw new UnsupportedOperationException("No V hit.");
    NucleotideSequence vTopReferenceSequence = hit.getGene().getFeature(hit.getAlignedFeature());
    hit = clone.getBestHit(Joining);
    if (hit == null)
        throw new UnsupportedOperationException("No J hit.");
    NucleotideSequence jTopReferenceSequence = hit.getGene().getFeature(hit.getAlignedFeature());
    // Excessive optimization
    CachedIntArray cachedIntArray = new CachedIntArray();
    int assemblingFeatureLength = targets.assemblingFeatureLength;
    for (int i = 0; i < targets.ranges.length; i++) {
        Range range = targets.ranges[i];
        NucleotideSequence sequence = targets.sequences[i].getSequence();
        // Asserts
        if (range.getFrom() < N_LEFT_DUMMIES + lengthV && range.getTo() >= N_LEFT_DUMMIES + lengthV && i != targets.assemblingFeatureTargetId)
            throw new RuntimeException();
        if (range.getTo() >= rightAssemblingFeatureBound && range.getFrom() < rightAssemblingFeatureBound && i != targets.assemblingFeatureTargetId)
            throw new RuntimeException();
        if (range.getTo() < N_LEFT_DUMMIES + lengthV) {
            boolean floatingLeftBound = !parameters.alignedRegionsOnly && i == 0 && alignerParameters.getVAlignerParameters().getParameters().isFloatingLeftBound();
            // Can be reduced to a single statement
            if (range.getFrom() < N_LEFT_DUMMIES)
                // This target contain extra non-V nucleotides on the left
                vTopHitAlignments[i] = alignLinearSeq1FromRight(((LinearGapAlignmentScoring<NucleotideSequence>) alignerParameters.getVAlignerParameters().getScoring()), vTopReferenceSequence, sequence.getSequence(), 0, range.getTo() - N_LEFT_DUMMIES, 0, sequence.size(), !floatingLeftBound, cachedIntArray);
            else if (floatingLeftBound)
                vTopHitAlignments[i] = alignLinearSeq1FromRight(((LinearGapAlignmentScoring<NucleotideSequence>) alignerParameters.getVAlignerParameters().getScoring()), vTopReferenceSequence, sequence.getSequence(), range.getFrom() - N_LEFT_DUMMIES, range.length(), 0, sequence.size(), false, cachedIntArray);
            else
                vTopHitAlignments[i] = Aligner.alignGlobal(alignerParameters.getVAlignerParameters().getScoring(), vTopReferenceSequence, sequence, range.getFrom() - N_LEFT_DUMMIES, range.length(), 0, sequence.size());
        } else if (i == targets.assemblingFeatureTargetId) {
            /*
                 *  V gene
                 */
            boolean vFloatingLeftBound = !parameters.alignedRegionsOnly && i == 0 && alignerParameters.getVAlignerParameters().getParameters().isFloatingLeftBound();
            // Can be reduced to a single statement
            if (range.getFrom() < N_LEFT_DUMMIES)
                // This target contain extra non-V nucleotides on the left
                vTopHitAlignments[i] = alignLinearSeq1FromRight(((LinearGapAlignmentScoring<NucleotideSequence>) alignerParameters.getVAlignerParameters().getScoring()), vTopReferenceSequence, sequence.getSequence(), 0, lengthV, 0, targets.assemblingFeatureOffset, !vFloatingLeftBound, cachedIntArray);
            else if (vFloatingLeftBound)
                vTopHitAlignments[i] = alignLinearSeq1FromRight(((LinearGapAlignmentScoring<NucleotideSequence>) alignerParameters.getVAlignerParameters().getScoring()), vTopReferenceSequence, sequence.getSequence(), range.getFrom() - N_LEFT_DUMMIES, lengthV - (range.getFrom() - N_LEFT_DUMMIES), 0, targets.assemblingFeatureOffset, false, cachedIntArray);
            else
                vTopHitAlignments[i] = Aligner.alignGlobal(alignerParameters.getVAlignerParameters().getScoring(), vTopReferenceSequence, sequence, range.getFrom() - N_LEFT_DUMMIES, lengthV - (range.getFrom() - N_LEFT_DUMMIES), 0, targets.assemblingFeatureOffset);
            /*
                 *  J gene
                 */
            boolean jFloatingRightBound = !parameters.alignedRegionsOnly && i == targets.ranges.length - 1 && alignerParameters.getJAlignerParameters().getParameters().isFloatingRightBound();
            if (range.getTo() >= rightAssemblingFeatureBound + jLength)
                // This target contain extra non-J nucleotides on the right
                jTopHitAlignments[i] = alignLinearSeq1FromLeft(((LinearGapAlignmentScoring<NucleotideSequence>) alignerParameters.getJAlignerParameters().getScoring()), jTopReferenceSequence, sequence.getSequence(), jOffset, jLength, targets.assemblingFeatureOffset + assemblingFeatureLength, sequence.size() - (targets.assemblingFeatureOffset + assemblingFeatureLength), !jFloatingRightBound, cachedIntArray);
            else if (jFloatingRightBound)
                jTopHitAlignments[i] = alignLinearSeq1FromLeft(((LinearGapAlignmentScoring<NucleotideSequence>) alignerParameters.getJAlignerParameters().getScoring()), jTopReferenceSequence, sequence.getSequence(), jOffset, range.getTo() - rightAssemblingFeatureBound, targets.assemblingFeatureOffset + assemblingFeatureLength, sequence.size() - (targets.assemblingFeatureOffset + assemblingFeatureLength), false, cachedIntArray);
            else
                jTopHitAlignments[i] = Aligner.alignGlobal(alignerParameters.getJAlignerParameters().getScoring(), jTopReferenceSequence, sequence, jOffset, range.getTo() - rightAssemblingFeatureBound, targets.assemblingFeatureOffset + assemblingFeatureLength, sequence.size() - (targets.assemblingFeatureOffset + assemblingFeatureLength));
        } else if (range.getFrom() > rightAssemblingFeatureBound) {
            boolean floatingRightBound = !parameters.alignedRegionsOnly && i == targets.ranges.length - 1 && alignerParameters.getJAlignerParameters().getParameters().isFloatingRightBound();
            if (range.getTo() >= rightAssemblingFeatureBound + jLength)
                // This target contain extra non-J nucleotides on the right
                jTopHitAlignments[i] = alignLinearSeq1FromLeft(((LinearGapAlignmentScoring<NucleotideSequence>) alignerParameters.getJAlignerParameters().getScoring()), jTopReferenceSequence, sequence.getSequence(), jOffset + (range.getFrom() - rightAssemblingFeatureBound), jLength - (range.getFrom() - rightAssemblingFeatureBound), 0, sequence.size(), !floatingRightBound, cachedIntArray);
            else if (floatingRightBound)
                jTopHitAlignments[i] = alignLinearSeq1FromLeft(((LinearGapAlignmentScoring<NucleotideSequence>) alignerParameters.getJAlignerParameters().getScoring()), jTopReferenceSequence, sequence.getSequence(), jOffset + (range.getFrom() - rightAssemblingFeatureBound), range.length(), 0, sequence.size(), false, cachedIntArray);
            else
                jTopHitAlignments[i] = Aligner.alignGlobal(alignerParameters.getJAlignerParameters().getScoring(), jTopReferenceSequence, sequence, jOffset + (range.getFrom() - rightAssemblingFeatureBound), range.length(), 0, sequence.size());
        } else
            throw new RuntimeException();
    }
    NSequenceWithQuality assemblingFeatureSeq = targets.sequences[targets.assemblingFeatureTargetId].getRange(targets.assemblingFeatureOffset, targets.assemblingFeatureOffset + targets.assemblingFeatureLength);
    Clone clone = cloneFactory.create(0, count, geneScores, new NSequenceWithQuality[] { assemblingFeatureSeq });
    vTopHitAlignments[targets.assemblingFeatureTargetId] = mergeTwoAlignments(vTopHitAlignments[targets.assemblingFeatureTargetId], clone.getBestHit(Variable).getAlignment(0).move(targets.assemblingFeatureOffset));
    jTopHitAlignments[targets.assemblingFeatureTargetId] = mergeTwoAlignments(clone.getBestHit(Joining).getAlignment(0).move(targets.assemblingFeatureOffset), jTopHitAlignments[targets.assemblingFeatureTargetId]);
    EnumMap<GeneType, VDJCHit[]> hits = new EnumMap<>(GeneType.class);
    for (GeneType gt : GeneType.VDJC_REFERENCE) hits.put(gt, Arrays.stream(clone.getHits(gt)).map(h -> moveHitTarget(h, targets.assemblingFeatureTargetId, targets.assemblingFeatureOffset, targets.ranges.length)).toArray(VDJCHit[]::new));
    VDJCHit[] tmp = hits.get(Variable);
    tmp[0] = substituteAlignments(tmp[0], vTopHitAlignments);
    tmp = hits.get(Joining);
    tmp[0] = substituteAlignments(tmp[0], jTopHitAlignments);
    return new Clone(targets.sequences, hits, count, 0);
}
Also used : IntStream(java.util.stream.IntStream) VDJCPartitionedSequence(com.milaboratory.mixcr.basictypes.VDJCPartitionedSequence) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) java.util(java.util) TObjectFloatHashMap(gnu.trove.map.hash.TObjectFloatHashMap) Clone(com.milaboratory.mixcr.basictypes.Clone) Supplier(java.util.function.Supplier) TIntIntIterator(gnu.trove.iterator.TIntIntIterator) com.milaboratory.core.alignment(com.milaboratory.core.alignment) VDJCAlignerParameters(com.milaboratory.mixcr.vdjaligners.VDJCAlignerParameters) CloneFactory(com.milaboratory.mixcr.assembler.CloneFactory) MutationsBuilder(com.milaboratory.core.mutations.MutationsBuilder) Constants(gnu.trove.impl.Constants) io.repseq.core(io.repseq.core) Range(com.milaboratory.core.Range) TObjectIntHashMap(gnu.trove.map.hash.TObjectIntHashMap) CUtils(cc.redberry.pipe.CUtils) TIntIntHashMap(gnu.trove.map.hash.TIntIntHashMap) OutputPort(cc.redberry.pipe.OutputPort) Collectors(java.util.stream.Collectors) com.milaboratory.core.sequence(com.milaboratory.core.sequence) TIntHashSet(gnu.trove.set.hash.TIntHashSet) Joining(io.repseq.core.GeneType.Joining) Stream(java.util.stream.Stream) VDJCHit(com.milaboratory.mixcr.basictypes.VDJCHit) VDJCGenes(io.repseq.gen.VDJCGenes) VDJCAlignments(com.milaboratory.mixcr.basictypes.VDJCAlignments) Variable(io.repseq.core.GeneType.Variable) Range(com.milaboratory.core.Range) VDJCHit(com.milaboratory.mixcr.basictypes.VDJCHit) Clone(com.milaboratory.mixcr.basictypes.Clone)

Aggregations

CUtils (cc.redberry.pipe.CUtils)1 OutputPort (cc.redberry.pipe.OutputPort)1 Range (com.milaboratory.core.Range)1 com.milaboratory.core.alignment (com.milaboratory.core.alignment)1 MutationsBuilder (com.milaboratory.core.mutations.MutationsBuilder)1 com.milaboratory.core.sequence (com.milaboratory.core.sequence)1 CloneFactory (com.milaboratory.mixcr.assembler.CloneFactory)1 Clone (com.milaboratory.mixcr.basictypes.Clone)1 VDJCAlignments (com.milaboratory.mixcr.basictypes.VDJCAlignments)1 VDJCHit (com.milaboratory.mixcr.basictypes.VDJCHit)1 VDJCPartitionedSequence (com.milaboratory.mixcr.basictypes.VDJCPartitionedSequence)1 VDJCAlignerParameters (com.milaboratory.mixcr.vdjaligners.VDJCAlignerParameters)1 Constants (gnu.trove.impl.Constants)1 TIntIntIterator (gnu.trove.iterator.TIntIntIterator)1 TIntIntHashMap (gnu.trove.map.hash.TIntIntHashMap)1 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)1 TObjectFloatHashMap (gnu.trove.map.hash.TObjectFloatHashMap)1 TObjectIntHashMap (gnu.trove.map.hash.TObjectIntHashMap)1 TIntHashSet (gnu.trove.set.hash.TIntHashSet)1 io.repseq.core (io.repseq.core)1