Search in sources :

Example 21 with NucleotideSequence

use of com.milaboratory.core.sequence.NucleotideSequence in project mixcr by milaboratory.

the class ClonalSequenceTest method testIsCompatible4.

@Test
public void testIsCompatible4() throws Exception {
    RandomGenerator generator = new Well1024a();
    for (int i = 0; i < 1000; i++) {
        TestData td = createRandomTestDara(1 + generator.nextInt(10), generator);
        assertTestData(td);
        int rp = generator.nextInt(td.c1.getConcatenated().size() / 2);
        byte rl = td.c1.getConcatenated().getSequence().codeAt(rp);
        if (rl == 0)
            rl = 1;
        else
            rl = (byte) (rl - 1);
        assert rp <= td.c1.getConcatenated().size();
        Mutations<NucleotideSequence> incompatible = td.mutations.combineWith(Mutations.decode("I" + rp + "" + NucleotideSequence.ALPHABET.codeToSymbol(rl), NucleotideSequence.ALPHABET));
        Assert.assertFalse(td.c1.isCompatible(td.c2, incompatible));
    }
}
Also used : NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) Well1024a(org.apache.commons.math3.random.Well1024a) Test(org.junit.Test)

Example 22 with NucleotideSequence

use of com.milaboratory.core.sequence.NucleotideSequence in project mixcr by milaboratory.

the class ClonalSequenceTest method testCompatiblePair.

private static void testCompatiblePair(ClonalSequence c1, ClonalSequence c2, RandomGenerator random) {
    int delta = c1.getConcatenated().size() - c2.getConcatenated().size();
    int c1size = c1.getConcatenated().size();
    for (int t = 0; t < 100; ++t) {
        int k = 1 + random.nextInt(2);
        int deletions, insertions;
        if (delta > 0) {
            // c1 > c2
            insertions = k;
            deletions = delta + k;
        } else {
            // c1 < c2
            deletions = k;
            insertions = -delta + k;
        }
        int[] muts = new int[Math.abs(deletions + insertions)];
        int c = 0;
        BitArray usedDels = new BitArray(c1size);
        for (int i = 0; i < deletions; ++i) {
            int p = random.nextInt(c1size);
            usedDels.set(p);
            muts[c++] = Mutation.createDeletion(p, 0);
        }
        for (int i = 0; i < insertions; ++i) {
            int p;
            do {
                p = random.nextInt(c1size);
            } while (usedDels.get(p));
            muts[c++] = Mutation.createInsertion(p, 0);
        }
        Arrays.sort(muts);
        Mutations<NucleotideSequence> mutations = new Mutations<>(NucleotideSequence.ALPHABET, muts);
        assert mutations.getLengthDelta() + c1.getConcatenated().size() == c2.getConcatenated().size();
        Assert.assertTrue(c1.isCompatible(c2, mutations));
    }
}
Also used : Mutations(com.milaboratory.core.mutations.Mutations) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) BitArray(com.milaboratory.util.BitArray)

Example 23 with NucleotideSequence

use of com.milaboratory.core.sequence.NucleotideSequence in project mixcr by milaboratory.

the class ClonalSequenceTest method createRandom.

private ClonalSequence createRandom(int size, RandomGenerator generator) {
    NSequenceWithQuality[] data = new NSequenceWithQuality[size];
    for (int i = 0; i < size; ++i) {
        NucleotideSequence s = TestUtil.randomSequence(NucleotideSequence.ALPHABET, generator, 2, 10);
        SequenceQuality q = SequenceQuality.getUniformQuality((byte) 0, s.size());
        data[i] = new NSequenceWithQuality(s, q);
    }
    return new ClonalSequence(data);
}
Also used : SequenceQuality(com.milaboratory.core.sequence.SequenceQuality) NSequenceWithQuality(com.milaboratory.core.sequence.NSequenceWithQuality) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence)

Example 24 with NucleotideSequence

use of com.milaboratory.core.sequence.NucleotideSequence in project mixcr by milaboratory.

the class FieldExtractorsTest method testAnchorPoints1.

@Test
public void testAnchorPoints1() throws Exception {
    final boolean print = false;
    final Well44497b rg = new Well44497b(12312);
    final VDJCAlignerParameters rnaSeqParams = VDJCParametersPresets.getByName("rna-seq");
    final PartialAlignmentsAssemblerAligner aligner = new PartialAlignmentsAssemblerAligner(rnaSeqParams);
    final VDJCLibrary lib = VDJCLibraryRegistry.getDefault().getLibrary("default", "hs");
    for (VDJCGene gene : VDJCLibraryRegistry.getDefault().getLibrary("default", "hs").getGenes()) if (gene.isFunctional())
        aligner.addGene(gene);
    final TargetBuilder.VDJCGenes genes = new TargetBuilder.VDJCGenes(lib, "TRBV12-3*00", "TRBD1*00", "TRBJ1-3*00", "TRBC2*00");
    // | 310  | 338   | 438
    // 250V + 60CDR3 (20V 7N 10D 3N 20J) + 28J + 100C + 100N
    // "{CDR3Begin(-250)}V*270 NNNNNNN {DBegin(0)}D*10 NNN {CDR3End(-20):FR4End} {CBegin}C*100 N*100"
    final FieldExtractors.ExtractDefaultReferencePointsPositions extractor = new FieldExtractors.ExtractDefaultReferencePointsPositions();
    F6 goAssert = new F6() {

        @Override
        public Integer[][] go(String seq, int len, int offset1, int offset2, int offset3, String expected) {
            final NucleotideSequence baseSeq = TargetBuilder.generateSequence(genes, seq, rg);
            NucleotideSequence seq1 = baseSeq.getRange(offset1, Math.min(baseSeq.size(), offset1 + len));
            NucleotideSequence seq2 = offset2 == -1 ? null : baseSeq.getRange(offset2, Math.min(baseSeq.size(), offset2 + len));
            NucleotideSequence seq3 = offset3 == -1 ? null : baseSeq.getRange(offset3, Math.min(baseSeq.size(), offset3 + len));
            VDJCAlignmentResult<VDJCMultiRead> alignment = offset3 == -1 ? offset2 == -1 ? aligner.process(MiXCRTestUtils.createMultiRead(seq1)) : aligner.process(MiXCRTestUtils.createMultiRead(seq1, seq2)) : aligner.process(MiXCRTestUtils.createMultiRead(seq1, seq2, seq3));
            VDJCAlignments al = alignment.alignment;
            Assert.assertNotNull(al);
            if (print) {
                MiXCRTestUtils.printAlignment(al);
                System.out.println();
                System.out.println("-------------------------------------------");
                System.out.println();
            }
            String val = extractor.extract(al);
            if (print)
                System.out.println(val);
            String[] spl = val.split(",");
            Integer[][] result = new Integer[spl.length][ReferencePoint.DefaultReferencePoints.length];
            for (int i = 0; i < spl.length; i++) {
                String[] spl1 = spl[i].split(":");
                for (int j = 0; j < spl1.length; j++) {
                    try {
                        result[i][j] = Integer.decode(spl1[j]);
                    } catch (NumberFormatException e) {
                    }
                }
            }
            return result;
        }
    };
    // No PSegments, just deletions
    Integer[][] r = goAssert.go("{CDR3Begin(-250):VEnd(-3)} 'CCAAA' {DBegin(0):DEnd(0)} 'AAA' {JBegin(2):FR4End} " + "{CBegin}C*100 N*100", 100, 240, 307, 450, "");
    assertExportPoint(r[0], ReferencePoint.VEnd, -3);
    assertExportPoint(r[0], ReferencePoint.DBegin, 0);
    assertExportPoint(r[0], ReferencePoint.DEnd, 0);
    assertExportPoint(r[0], ReferencePoint.JBegin, -2);
    r = goAssert.go("{CDR3Begin(-250):VEnd(0)} 'CCAAA' {DBegin(0):DEnd(-2)} 'AAA' {JBegin:FR4End} {CBegin}C*100 N*100", 100, 240, 307, 450, "");
    assertExportPoint(r[0], ReferencePoint.VEnd, 0);
    assertExportPoint(r[0], ReferencePoint.DBegin, 0);
    assertExportPoint(r[0], ReferencePoint.DEnd, -2);
    assertExportPoint(r[0], ReferencePoint.JBegin, 0);
    // With PSegments
    r = goAssert.go("{CDR3Begin(-250):VEnd(0)} {VEnd:VEnd(-3)} 'CCAAA' {DBegin(3):DBegin} {DBegin:DEnd(-2)} 'AAA' " + "{JBegin(2):JBegin} {JBegin:FR4End} {CBegin}C*100 N*100", 100, 240, 307, 450, "");
    assertExportPoint(r[0], ReferencePoint.VEnd, 3);
    assertExportPoint(r[0], ReferencePoint.DBegin, 3);
    assertExportPoint(r[0], ReferencePoint.DEnd, -2);
    assertExportPoint(r[0], ReferencePoint.JBegin, 2);
}
Also used : VDJCMultiRead(com.milaboratory.mixcr.partialassembler.VDJCMultiRead) VDJCAlignerParameters(com.milaboratory.mixcr.vdjaligners.VDJCAlignerParameters) PartialAlignmentsAssemblerAligner(com.milaboratory.mixcr.partialassembler.PartialAlignmentsAssemblerAligner) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) Well44497b(org.apache.commons.math3.random.Well44497b) TargetBuilder(com.milaboratory.mixcr.tests.TargetBuilder) VDJCAlignments(com.milaboratory.mixcr.basictypes.VDJCAlignments) Test(org.junit.Test)

Example 25 with NucleotideSequence

use of com.milaboratory.core.sequence.NucleotideSequence in project mixcr by milaboratory.

the class TargetMerger method extractHitsMapping.

@SuppressWarnings("unchecked")
static Map<VDJCGeneId, HitMappingRecord> extractHitsMapping(AlignedTarget targetLeft, AlignedTarget targetRight, GeneType geneType) {
    Map<VDJCGeneId, HitMappingRecord> map = new HashMap<>();
    for (VDJCHit l : targetLeft.getAlignments().getHits(geneType)) {
        final VDJCGene gene = l.getGene();
        final Alignment<NucleotideSequence> al = l.getAlignment(targetLeft.getTargetId());
        if (al != null)
            map.put(gene.getId(), new HitMappingRecord(gene, new Alignment[] { al, null }));
    }
    for (VDJCHit r : targetRight.getAlignments().getHits(geneType)) {
        final VDJCGene gene = r.getGene();
        final Alignment<NucleotideSequence> alignment = r.getAlignment(targetRight.getTargetId());
        if (alignment == null)
            continue;
        final HitMappingRecord als = map.get(gene.getId());
        if (als == null)
            map.put(gene.getId(), new HitMappingRecord(gene, new Alignment[] { null, alignment }));
        else {
            assert als.alignments[1] == null;
            als.alignments[1] = alignment;
        }
    }
    return map;
}
Also used : NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) VDJCGene(io.repseq.core.VDJCGene) VDJCGeneId(io.repseq.core.VDJCGeneId) VDJCHit(com.milaboratory.mixcr.basictypes.VDJCHit)

Aggregations

NucleotideSequence (com.milaboratory.core.sequence.NucleotideSequence)81 Test (org.junit.Test)32 Range (com.milaboratory.core.Range)19 VDJCGene (io.repseq.core.VDJCGene)15 GeneType (io.repseq.core.GeneType)14 NSequenceWithQuality (com.milaboratory.core.sequence.NSequenceWithQuality)13 GeneFeature (io.repseq.core.GeneFeature)9 VDJCAlignments (com.milaboratory.mixcr.basictypes.VDJCAlignments)8 VDJCHit (com.milaboratory.mixcr.basictypes.VDJCHit)8 ReferencePoint (io.repseq.core.ReferencePoint)7 VDJCLibrary (io.repseq.core.VDJCLibrary)7 ArrayList (java.util.ArrayList)7 VDJCAlignerParameters (com.milaboratory.mixcr.vdjaligners.VDJCAlignerParameters)6 AlignmentHit (com.milaboratory.core.alignment.batch.AlignmentHit)5 PairedRead (com.milaboratory.core.io.sequence.PairedRead)5 AminoAcidSequence (com.milaboratory.core.sequence.AminoAcidSequence)5 Path (java.nio.file.Path)5 Alignment (com.milaboratory.core.alignment.Alignment)4 Pattern (java.util.regex.Pattern)4 AlignmentHelper (com.milaboratory.core.alignment.AlignmentHelper)3