Search in sources :

Example 1 with SequenceHistory

use of com.milaboratory.mixcr.basictypes.SequenceHistory in project mixcr by milaboratory.

the class VDJCAlignerPVFirst method process0.

@Override
protected VDJCAlignmentResult<PairedRead> process0(final PairedRead input) {
    Target[] targets = getTargets(input);
    // Creates helper classes for each PTarget
    PAlignmentHelper[] helpers = createInitialHelpers(targets);
    VDJCAlignmentResult<PairedRead> result = parameters.getAllowPartialAlignments() ? processPartial(input, helpers) : processStrict(input, helpers);
    // if sAligner == null (which means --no-merge option), no merge will be performed
    if (result.alignment != null && sAligner != null) {
        final VDJCAlignments alignment = result.alignment;
        final TargetMerger.TargetMergingResult mergeResult = alignmentsMerger.merge(new AlignedTarget(alignment, 0), new AlignedTarget(alignment, 1), false);
        if (mergeResult.failedDueInconsistentAlignments()) {
            GeneType geneType = mergeResult.getFailedMergedGeneType();
            int removeId = alignment.getBestHit(geneType).getAlignment(0).getScore() > alignment.getBestHit(geneType).getAlignment(1).getScore() ? 1 : 0;
            if (listener != null)
                listener.onTopHitSequenceConflict(input, alignment, geneType);
            return new VDJCAlignmentResult<>(input, alignment.removeBestHitAlignment(geneType, removeId));
        } else if (mergeResult.isSuccessful()) {
            assert mergeResult.isUsingAlignments();
            NSequenceWithQuality alignedTarget = mergeResult.getResult().getTarget();
            SingleRead sRead = new SingleReadImpl(input.getId(), alignedTarget, "");
            VDJCAlignmentResult<SingleRead> sResult = sAligner.process0(sRead);
            if (sResult.alignment == null)
                return result;
            VDJCAlignments sAlignment = sResult.alignment.setHistory(new SequenceHistory[] { new SequenceHistory.Merge(SequenceHistory.OverlapType.AlignmentOverlap, result.alignment.getHistory(0), result.alignment.getHistory(1), mergeResult.getOffset(), mergeResult.getMismatched()) }, new SequenceRead[] { input });
            if (listener != null)
                listener.onSuccessfulAlignmentOverlap(input, sAlignment);
            return new VDJCAlignmentResult<>(input, sAlignment);
        }
    }
    return result;
}
Also used : SingleReadImpl(com.milaboratory.core.io.sequence.SingleReadImpl) TargetMerger(com.milaboratory.mixcr.partialassembler.TargetMerger) SequenceHistory(com.milaboratory.mixcr.basictypes.SequenceHistory) PairedRead(com.milaboratory.core.io.sequence.PairedRead) AlignedTarget(com.milaboratory.mixcr.partialassembler.AlignedTarget) Target(com.milaboratory.core.Target) AlignedTarget(com.milaboratory.mixcr.partialassembler.AlignedTarget) NSequenceWithQuality(com.milaboratory.core.sequence.NSequenceWithQuality) SequenceRead(com.milaboratory.core.io.sequence.SequenceRead) SingleRead(com.milaboratory.core.io.sequence.SingleRead) VDJCAlignments(com.milaboratory.mixcr.basictypes.VDJCAlignments)

Example 2 with SequenceHistory

use of com.milaboratory.mixcr.basictypes.SequenceHistory in project mixcr by milaboratory.

the class TargetMerger method merge.

@SuppressWarnings("unchecked")
public AlignedTarget merge(AlignedTarget targetLeft, AlignedTarget targetRight, int offset, OverlapType overlapType, int nMismatches) {
    if (offset < 0)
        return merge(targetRight, targetLeft, -offset, overlapType, nMismatches);
    final NSequenceWithQuality mergedTarget = merger.overlap(targetLeft.getTarget(), targetRight.getTarget(), offset);
    EnumMap<GeneType, VDJCHit[]> result = new EnumMap<>(GeneType.class);
    for (GeneType geneType : GeneType.VJC_REFERENCE) {
        final BatchAlignerWithBaseParameters bp = ((KGeneAlignmentParameters) alignerParameters.getGeneAlignerParameters(geneType)).getParameters();
        final VDJCHit[] leftHits = targetLeft.getAlignments().getHits(geneType);
        final VDJCHit[] rightHits = targetRight.getAlignments().getHits(geneType);
        GeneFeature alignedFeature = leftHits.length == 0 ? rightHits.length == 0 ? null : rightHits[0].getAlignedFeature() : leftHits[0].getAlignedFeature();
        Map<VDJCGeneId, HitMappingRecord> map = extractHitsMapping(targetLeft, targetRight, geneType);
        ArrayList<VDJCHit> resultingHits = new ArrayList<>();
        for (Map.Entry<VDJCGeneId, HitMappingRecord> mE : map.entrySet()) {
            final VDJCGene gene = mE.getValue().gene;
            Alignment<NucleotideSequence> mergedAl = merge(bp.getScoring(), extractBandedWidth(bp), mergedTarget.getSequence(), offset, mE.getValue().alignments[0], mE.getValue().alignments[1]);
            resultingHits.add(new VDJCHit(gene, mergedAl, alignedFeature));
        }
        Collections.sort(resultingHits);
        // final float relativeMinScore = extractRelativeMinScore(bp);
        // int threshold = (int) (resultingHits.size() > 0 ? resultingHits.get(0).getScore() * relativeMinScore : 0);
        // for (int i = resultingHits.size() - 1; i > 0; --i)
        // if (resultingHits.get(i).getScore() < threshold)
        // resultingHits.remove(i);
        result.put(geneType, resultingHits.toArray(new VDJCHit[resultingHits.size()]));
    }
    VDJCAlignments alignments = new VDJCAlignments(result, new NSequenceWithQuality[] { mergedTarget }, new SequenceHistory[] { new SequenceHistory.Merge(overlapType, targetLeft.getHistory(), targetRight.getHistory(), offset, nMismatches) }, VDJCAlignments.mergeOriginalReads(targetLeft.getAlignments(), targetRight.getAlignments()));
    AlignedTarget resultTarget = new AlignedTarget(alignments, 0);
    for (BPoint bPoint : BPoint.values()) {
        int leftPoint = targetLeft.getBPoint(bPoint);
        int rightPoint = targetRight.getBPoint(bPoint);
        if (leftPoint != -1 && rightPoint != -1)
            throw new IllegalArgumentException("Same bPoint defined in both input targets.");
        else if (leftPoint != -1)
            resultTarget = resultTarget.setBPoint(bPoint, leftPoint);
        else if (rightPoint != -1)
            resultTarget = resultTarget.setBPoint(bPoint, offset + rightPoint);
    }
    return resultTarget;
}
Also used : GeneFeature(io.repseq.core.GeneFeature) SequenceHistory(com.milaboratory.mixcr.basictypes.SequenceHistory) VDJCGeneId(io.repseq.core.VDJCGeneId) NSequenceWithQuality(com.milaboratory.core.sequence.NSequenceWithQuality) VDJCAlignments(com.milaboratory.mixcr.basictypes.VDJCAlignments) BatchAlignerWithBaseParameters(com.milaboratory.core.alignment.batch.BatchAlignerWithBaseParameters) KGeneAlignmentParameters(com.milaboratory.mixcr.vdjaligners.KGeneAlignmentParameters) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) VDJCGene(io.repseq.core.VDJCGene) GeneType(io.repseq.core.GeneType) VDJCHit(com.milaboratory.mixcr.basictypes.VDJCHit)

Example 3 with SequenceHistory

use of com.milaboratory.mixcr.basictypes.SequenceHistory in project mixcr by milaboratory.

the class VDJCAlignerWithMerge method process0.

@Override
protected VDJCAlignmentResult<PairedRead> process0(final PairedRead read) {
    PairedReadMergingResult merged = merger.process(read);
    if (merged.isSuccessful()) {
        VDJCAlignments alignment = singleAligner.process(new SingleReadImpl(read.getId(), merged.getOverlappedSequence(), "")).alignment;
        if (listener != null)
            listener.onSuccessfulSequenceOverlap(read, alignment);
        if (alignment != null) {
            boolean isRC = ((SequenceHistory.RawSequence) alignment.getHistory(0)).index.isReverseComplement;
            alignment = alignment.setHistory(new SequenceHistory[] { new SequenceHistory.Merge(SequenceHistory.OverlapType.SequenceOverlap, new SequenceHistory.RawSequence(read.getId(), (byte) (isRC ? 1 : 0), false, read.getR1().getData().size()), new SequenceHistory.RawSequence(read.getId(), (byte) (isRC ? 0 : 1), merged.isReversed(), read.getR2().getData().size()), merged.getOffset(), merged.getErrors()) }, new SequenceRead[] { read });
        }
        return new VDJCAlignmentResult<>(read, alignment);
    } else
        return pairedAligner.process(read);
}
Also used : SingleReadImpl(com.milaboratory.core.io.sequence.SingleReadImpl) PairedReadMergingResult(com.milaboratory.core.merger.PairedReadMergingResult) SequenceRead(com.milaboratory.core.io.sequence.SequenceRead) SequenceHistory(com.milaboratory.mixcr.basictypes.SequenceHistory) VDJCAlignments(com.milaboratory.mixcr.basictypes.VDJCAlignments)

Aggregations

SequenceHistory (com.milaboratory.mixcr.basictypes.SequenceHistory)3 VDJCAlignments (com.milaboratory.mixcr.basictypes.VDJCAlignments)3 SequenceRead (com.milaboratory.core.io.sequence.SequenceRead)2 SingleReadImpl (com.milaboratory.core.io.sequence.SingleReadImpl)2 NSequenceWithQuality (com.milaboratory.core.sequence.NSequenceWithQuality)2 Target (com.milaboratory.core.Target)1 BatchAlignerWithBaseParameters (com.milaboratory.core.alignment.batch.BatchAlignerWithBaseParameters)1 PairedRead (com.milaboratory.core.io.sequence.PairedRead)1 SingleRead (com.milaboratory.core.io.sequence.SingleRead)1 PairedReadMergingResult (com.milaboratory.core.merger.PairedReadMergingResult)1 NucleotideSequence (com.milaboratory.core.sequence.NucleotideSequence)1 VDJCHit (com.milaboratory.mixcr.basictypes.VDJCHit)1 AlignedTarget (com.milaboratory.mixcr.partialassembler.AlignedTarget)1 TargetMerger (com.milaboratory.mixcr.partialassembler.TargetMerger)1 KGeneAlignmentParameters (com.milaboratory.mixcr.vdjaligners.KGeneAlignmentParameters)1 GeneFeature (io.repseq.core.GeneFeature)1 GeneType (io.repseq.core.GeneType)1 VDJCGene (io.repseq.core.VDJCGene)1 VDJCGeneId (io.repseq.core.VDJCGeneId)1