Search in sources :

Example 1 with MutationsBuilder

use of com.milaboratory.core.mutations.MutationsBuilder in project mixcr by milaboratory.

the class FullSeqAssembler method alignLinearSeq1FromRight.

// fixme write docs
static Alignment<NucleotideSequence> alignLinearSeq1FromRight(LinearGapAlignmentScoring<NucleotideSequence> scoring, NucleotideSequence seq1, NucleotideSequence seq2, int offset1, int length1, int offset2, int length2, boolean global, CachedIntArray cache) {
    MutationsBuilder<NucleotideSequence> mutations = new MutationsBuilder<>(NucleotideSequence.ALPHABET);
    BandedSemiLocalResult result;
    int width = 2 * length1;
    if (global) {
        int seq2added = width;
        if (length2 > length1) {
            length2 = Math.min(length2, length1 + width);
            seq2added = Math.min(length2, width + (length2 - length1));
        }
        result = BandedLinearAligner.alignLeftAdded0(scoring, seq1, seq2, offset1, length1, 0, offset2, length2, seq2added, width, mutations, cache);
    } else
        result = BandedLinearAligner.alignSemiLocalRight0(scoring, seq1, seq2, offset1, length1, offset2, length2, width, Integer.MIN_VALUE, mutations, cache);
    return new Alignment<>(seq1, mutations.createAndDestroy(), new Range(result.sequence1Stop, offset1 + length1), new Range(result.sequence2Stop, offset2 + length2), result.score);
}
Also used : MutationsBuilder(com.milaboratory.core.mutations.MutationsBuilder) Range(com.milaboratory.core.Range)

Example 2 with MutationsBuilder

use of com.milaboratory.core.mutations.MutationsBuilder in project mixcr by milaboratory.

the class FullSeqAssembler method alignLinearSeq1FromLeft.

// fixme write docs
static Alignment<NucleotideSequence> alignLinearSeq1FromLeft(LinearGapAlignmentScoring<NucleotideSequence> scoring, NucleotideSequence seq1, NucleotideSequence seq2, int offset1, int length1, int offset2, int length2, boolean global, CachedIntArray cache) {
    MutationsBuilder<NucleotideSequence> mutations = new MutationsBuilder<>(NucleotideSequence.ALPHABET);
    BandedSemiLocalResult result;
    int width = 2 * length1;
    if (global) {
        int seq2added = width;
        if (length2 > length1) {
            length2 = Math.min(length2, length1 + width);
            seq2added = Math.min(length2, width + (length2 - length1));
        }
        result = BandedLinearAligner.alignRightAdded0(scoring, seq1, seq2, offset1, length1, 0, offset2, length2, seq2added, width, mutations, cache);
    } else
        result = BandedLinearAligner.alignSemiLocalLeft0(scoring, seq1, seq2, offset1, length1, offset2, length2, width, Integer.MIN_VALUE, mutations, cache);
    return new Alignment<>(seq1, mutations.createAndDestroy(), new Range(offset1, result.sequence1Stop + 1), new Range(offset2, result.sequence2Stop + 1), result.score);
}
Also used : MutationsBuilder(com.milaboratory.core.mutations.MutationsBuilder) Range(com.milaboratory.core.Range)

Example 3 with MutationsBuilder

use of com.milaboratory.core.mutations.MutationsBuilder in project mixcr by milaboratory.

the class ClonalSequenceTest method createRandomTestDara.

private TestData createRandomTestDara(int size, RandomGenerator random) {
    MutationsBuilder<NucleotideSequence> builder = new MutationsBuilder<>(NucleotideSequence.ALPHABET);
    NSequenceWithQuality[] c1 = new NSequenceWithQuality[size];
    NSequenceWithQuality[] c2 = new NSequenceWithQuality[size];
    int offset = 0;
    for (int i = 0; i < size; ++i) {
        NucleotideSequence s1 = TestUtil.randomSequence(NucleotideSequence.ALPHABET, random, 5, 15);
        Mutations<NucleotideSequence> muts = MutationsGenerator.generateMutations(s1, MutationModels.getEmpiricalNucleotideMutationModel(), 1, s1.size() - 1);
        NucleotideSequence s2 = muts.mutate(s1);
        c1[i] = new NSequenceWithQuality(s1, SequenceQuality.getUniformQuality((byte) 0, s1.size()));
        c2[i] = new NSequenceWithQuality(s2, SequenceQuality.getUniformQuality((byte) 0, s2.size()));
        builder.append(muts.move(offset));
        offset += s1.size();
    }
    return new TestData(new ClonalSequence(c1), new ClonalSequence(c2), builder.createAndDestroy());
}
Also used : NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) NSequenceWithQuality(com.milaboratory.core.sequence.NSequenceWithQuality) MutationsBuilder(com.milaboratory.core.mutations.MutationsBuilder)

Example 4 with MutationsBuilder

use of com.milaboratory.core.mutations.MutationsBuilder in project mixcr by milaboratory.

the class Merger method merge.

public static NSequenceWithQuality merge(Range range, Alignment<NucleotideSequence>[] alignments, NSequenceWithQuality[] targets) {
    // Checking arguments
    if (alignments.length != targets.length)
        throw new IllegalArgumentException();
    // Extracting reference sequence
    NucleotideSequence sequence = alignments[0].getSequence1();
    for (int i = 1; i < alignments.length; i++) if (sequence != alignments[i].getSequence1())
        throw new IllegalArgumentException("Different reference sequences.");
    int position = range.getFrom();
    int localPosition = 0;
    int[] mPointers = new int[alignments.length];
    for (int i = 0; i < alignments.length; i++) mPointers[i] = firstIndexAfter(alignments[i].getAbsoluteMutations(), position);
    // Aggregator of quality information
    SequenceQualityBuilder qualityBuilder = new SequenceQualityBuilder().ensureCapacity(range.length());
    // Aggregator of mutations
    MutationsBuilder<NucleotideSequence> mutationsBuilder = new MutationsBuilder<>(NucleotideSequence.ALPHABET);
    do {
        int winnerIndex = -1;
        byte bestQuality = -1;
        int winnerMPointer = -1, winnerMLength = -1;
        for (int i = 0; i < alignments.length; i++) {
            Alignment<NucleotideSequence> al = alignments[i];
            if (!al.getSequence1Range().contains(position))
                continue;
            // Current mutation index
            int mPointer = mPointers[i];
            Mutations<NucleotideSequence> mutations = al.getAbsoluteMutations();
            // Number of mutations with the same position
            int length;
            if (mPointer < mutations.size() && mutations.getPositionByIndex(mPointer) == position) {
                length = 1;
                while (mPointer + length < mutations.size() && mutations.getPositionByIndex(mPointer + length) == position) ++length;
            } else
                length = 0;
            byte pointQuality = getQuality(position, mPointer, length, al, targets[i].getQuality());
            if (bestQuality < pointQuality) {
                winnerIndex = i;
                winnerMPointer = mPointer;
                winnerMLength = length;
                bestQuality = pointQuality;
            }
        }
        Mutations<NucleotideSequence> winnerMutations = alignments[winnerIndex].getAbsoluteMutations();
        byte sumQuality = 0;
        // Second pass to calculate score
        OUT: for (int i = 0; i < alignments.length; i++) {
            Alignment<NucleotideSequence> al = alignments[i];
            if (!al.getSequence1Range().contains(position))
                continue;
            // Current mutation index
            int mPointer = mPointers[i];
            Mutations<NucleotideSequence> mutations = al.getAbsoluteMutations();
            // Number of mutations with the same position
            int length;
            if (mPointer < mutations.size() && mutations.getPositionByIndex(mPointer) == position) {
                length = 1;
                while (mPointer + length < mutations.size() && mutations.getPositionByIndex(mPointer + length) == position) ++length;
            } else
                length = 0;
            // Advancing pointer
            mPointers[i] += length;
            if (length != winnerMLength)
                continue;
            for (int k = 0; k < length; ++k) if (mutations.getMutation(mPointer + k) != winnerMutations.getMutation(winnerMPointer + k))
                continue OUT;
            byte pointQuality = getQuality(position, mPointer, length, al, targets[i].getQuality());
            sumQuality += pointQuality;
        }
        // todo ??
        if (winnerIndex == -1)
            return null;
        sumQuality = (byte) Math.min(sumQuality, MAX_QUALITY);
        qualityBuilder.append(sumQuality);
        if (winnerMLength != 0)
            mutationsBuilder.append(winnerMutations, winnerMPointer, winnerMLength);
        ++localPosition;
    } while (++position < range.getTo());
    Mutations<NucleotideSequence> mutations = mutationsBuilder.createAndDestroy();
    NSequenceWithQuality toMutate = new NSequenceWithQuality(sequence.getRange(range), qualityBuilder.createAndDestroy());
    return MutationsUtil.mutate(toMutate, mutations.move(-range.getFrom()));
}
Also used : Mutations(com.milaboratory.core.mutations.Mutations) MutationsBuilder(com.milaboratory.core.mutations.MutationsBuilder) Alignment(com.milaboratory.core.alignment.Alignment) SequenceQualityBuilder(com.milaboratory.core.sequence.SequenceQualityBuilder) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) NSequenceWithQuality(com.milaboratory.core.sequence.NSequenceWithQuality)

Aggregations

MutationsBuilder (com.milaboratory.core.mutations.MutationsBuilder)4 Range (com.milaboratory.core.Range)2 NSequenceWithQuality (com.milaboratory.core.sequence.NSequenceWithQuality)2 NucleotideSequence (com.milaboratory.core.sequence.NucleotideSequence)2 Alignment (com.milaboratory.core.alignment.Alignment)1 Mutations (com.milaboratory.core.mutations.Mutations)1 SequenceQualityBuilder (com.milaboratory.core.sequence.SequenceQualityBuilder)1