Search in sources :

Example 6 with SequenceRead

use of com.milaboratory.core.io.sequence.SequenceRead in project mixcr by milaboratory.

the class FullSeqAssemblerTest method testRandom1.

@Test
public void testRandom1() throws Exception {
    CloneFraction[] clones = { new CloneFraction(750, masterSeq1WT), // V: S346:G->T
    new CloneFraction(1000, masterSeq1VSub1), // J: D55:A
    new CloneFraction(1000, masterSeq1VDel1JDel1), // J: D62:C
    new CloneFraction(500, masterSeq1VDel1JDelVSub2) };
    Well19937c rand = new Well19937c();
    rand.setSeed(12345);
    RandomDataGenerator rdg = new RandomDataGenerator(rand);
    List<SequenceRead> readsOrig = new ArrayList<>();
    int readLength = 100;
    int id = -1;
    for (CloneFraction clone : clones) {
        for (int i = 0; i < clone.count; i++) {
            // Left read with CDR3
            ++id;
            readsOrig.add(new PairedRead(new SingleReadImpl(id, new NSequenceWithQuality(clone.seq.getRangeFromCDR3Begin(-rand.nextInt(readLength - clone.seq.cdr3Part), readLength)), "R1_" + id), new SingleReadImpl(id, new NSequenceWithQuality(clone.seq.getRangeFromCDR3End(rdg.nextInt(-clone.seq.cdr3Part / 2, clone.seq.jPart), readLength).getReverseComplement()), "R2_" + id)));
            ++id;
            readsOrig.add(new PairedRead(new SingleReadImpl(id, new NSequenceWithQuality(clone.seq.getRangeFromCDR3Begin(rdg.nextInt(-clone.seq.vPart, clone.seq.cdr3Part / 2 - readLength), readLength)), "R1_" + id), new SingleReadImpl(id, new NSequenceWithQuality(clone.seq.getRangeFromCDR3Begin(-rand.nextInt(readLength - clone.seq.cdr3Part), readLength)).getReverseComplement(), "R2_" + id)));
        }
    }
    // readsOrig = Arrays.asList(setReadId(0, readsOrig.get(12)), setReadId(1, readsOrig.get(13)));
    int[] perm = rdg.nextPermutation(readsOrig.size(), readsOrig.size());
    List<SequenceRead> reads = new ArrayList<>();
    for (int i = 0; i < readsOrig.size(); i++) reads.add(readsOrig.get(perm[i]));
    RunMiXCR.RunMiXCRAnalysis params = new RunMiXCR.RunMiXCRAnalysis(new SequenceReaderCloseable<SequenceRead>() {

        int counter = 0;

        @Override
        public void close() {
        }

        @Override
        public long getNumberOfReads() {
            return counter;
        }

        @Override
        public synchronized SequenceRead take() {
            if (counter == reads.size())
                return null;
            return reads.get(counter++);
        }
    }, true);
    params.alignerParameters = VDJCParametersPresets.getByName("rna-seq");
    params.alignerParameters.setSaveOriginalReads(true);
    params.alignerParameters.setVAlignmentParameters(params.alignerParameters.getVAlignerParameters().setGeneFeatureToAlign(GeneFeature.VTranscriptWithP));
    RunMiXCR.AlignResult align = RunMiXCR.align(params);
    // // TODO exception for translation
    // for (VDJCAlignments al : align.alignments) {
    // for (int i = 0; i < al.numberOfTargets(); i++) {
    // System.out.println(VDJCAlignmentsFormatter.getTargetAsMultiAlignment(al, i));
    // System.out.println();
    // }
    // System.out.println();
    // System.out.println(" ================================================ ");
    // System.out.println();
    // }
    RunMiXCR.AssembleResult assemble = RunMiXCR.assemble(align);
    Assert.assertEquals(1, assemble.cloneSet.size());
    CloneFactory cloneFactory = new CloneFactory(align.parameters.cloneAssemblerParameters.getCloneFactoryParameters(), align.parameters.cloneAssemblerParameters.getAssemblingFeatures(), align.usedGenes, align.parameters.alignerParameters.getFeaturesToAlignMap());
    FullSeqAssembler agg = new FullSeqAssembler(cloneFactory, DEFAULT_PARAMETERS, assemble.cloneSet.get(0), align.parameters.alignerParameters);
    FullSeqAssembler.RawVariantsData prep = agg.calculateRawData(() -> CUtils.asOutputPort(align.alignments.stream().filter(a -> a.getFeature(GeneFeature.CDR3) != null).collect(Collectors.toList())));
    List<Clone> clns = new ArrayList<>(new CloneSet(Arrays.asList(agg.callVariants(prep))).getClones());
    clns.sort(Comparator.comparingDouble(Clone::getCount).reversed());
    System.out.println("# Clones: " + clns.size());
    id = 0;
    for (Clone clone : clns) {
        clone = clone.setId(id++);
        System.out.println(clone.numberOfTargets());
        System.out.println(clone.getCount());
        System.out.println(clone.getFraction());
        System.out.println(clone.getBestHit(GeneType.Variable).getAlignment(0).getAbsoluteMutations());
        System.out.println(clone.getBestHit(GeneType.Joining).getAlignment(0).getAbsoluteMutations());
        System.out.println();
    // ActionExportClonesPretty.outputCompact(System.out, clone);
    }
}
Also used : java.util(java.util) SequenceRead(com.milaboratory.core.io.sequence.SequenceRead) Well44497b(org.apache.commons.math3.random.Well44497b) SequenceQuality(com.milaboratory.core.sequence.SequenceQuality) Clone(com.milaboratory.mixcr.basictypes.Clone) GeneFeature(io.repseq.core.GeneFeature) CloneFactory(com.milaboratory.mixcr.assembler.CloneFactory) Main(com.milaboratory.mixcr.cli.Main) StreamSupport(java.util.stream.StreamSupport) PairedRead(com.milaboratory.core.io.sequence.PairedRead) RunMiXCR(com.milaboratory.mixcr.util.RunMiXCR) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) VDJCAlignmentsFormatter(com.milaboratory.mixcr.basictypes.VDJCAlignmentsFormatter) CUtils(cc.redberry.pipe.CUtils) Test(org.junit.Test) Collectors(java.util.stream.Collectors) TIntHashSet(gnu.trove.set.hash.TIntHashSet) RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) Well19937c(org.apache.commons.math3.random.Well19937c) CloneSet(com.milaboratory.mixcr.basictypes.CloneSet) SingleReadImpl(com.milaboratory.core.io.sequence.SingleReadImpl) GeneType(io.repseq.core.GeneType) NSequenceWithQuality(com.milaboratory.core.sequence.NSequenceWithQuality) ActionExportClonesPretty(com.milaboratory.mixcr.cli.ActionExportClonesPretty) VDJCParametersPresets(com.milaboratory.mixcr.vdjaligners.VDJCParametersPresets) Assert(org.junit.Assert) SequenceReaderCloseable(com.milaboratory.core.io.sequence.SequenceReaderCloseable) VDJCAlignments(com.milaboratory.mixcr.basictypes.VDJCAlignments) SingleReadImpl(com.milaboratory.core.io.sequence.SingleReadImpl) PairedRead(com.milaboratory.core.io.sequence.PairedRead) Well19937c(org.apache.commons.math3.random.Well19937c) CloneSet(com.milaboratory.mixcr.basictypes.CloneSet) NSequenceWithQuality(com.milaboratory.core.sequence.NSequenceWithQuality) Clone(com.milaboratory.mixcr.basictypes.Clone) RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) SequenceRead(com.milaboratory.core.io.sequence.SequenceRead) RunMiXCR(com.milaboratory.mixcr.util.RunMiXCR) CloneFactory(com.milaboratory.mixcr.assembler.CloneFactory) Test(org.junit.Test)

Example 7 with SequenceRead

use of com.milaboratory.core.io.sequence.SequenceRead 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)

Example 8 with SequenceRead

use of com.milaboratory.core.io.sequence.SequenceRead in project mixcr by milaboratory.

the class VDJCAlignments method getOriginalSequence.

public NSequenceWithQuality getOriginalSequence(SequenceHistory.FullReadIndex index) {
    if (originalReads == null)
        throw new IllegalStateException("Original reads are not saved for the alignment object.");
    SequenceRead read = null;
    for (SequenceRead originalRead : originalReads) if (originalRead.getId() == index.readId) {
        read = originalRead;
        break;
    }
    if (read == null)
        throw new IllegalArgumentException("No such read index: " + index);
    NSequenceWithQuality seq = read.getRead(index.mateIndex).getData();
    return index.isReverseComplement ? seq.getReverseComplement() : seq;
}
Also used : NSequenceWithQuality(com.milaboratory.core.sequence.NSequenceWithQuality) SequenceRead(com.milaboratory.core.io.sequence.SequenceRead)

Aggregations

SequenceRead (com.milaboratory.core.io.sequence.SequenceRead)8 VDJCAlignments (com.milaboratory.mixcr.basictypes.VDJCAlignments)6 NSequenceWithQuality (com.milaboratory.core.sequence.NSequenceWithQuality)4 NucleotideSequence (com.milaboratory.core.sequence.NucleotideSequence)4 SingleReadImpl (com.milaboratory.core.io.sequence.SingleReadImpl)3 Target (com.milaboratory.core.Target)2 PairedRead (com.milaboratory.core.io.sequence.PairedRead)2 SequenceWriter (com.milaboratory.core.io.sequence.SequenceWriter)2 Clone (com.milaboratory.mixcr.basictypes.Clone)2 CloneSet (com.milaboratory.mixcr.basictypes.CloneSet)2 SequenceHistory (com.milaboratory.mixcr.basictypes.SequenceHistory)2 VDJCAlignmentsWriter (com.milaboratory.mixcr.basictypes.VDJCAlignmentsWriter)2 CanReportProgress (com.milaboratory.util.CanReportProgress)2 GeneFeature (io.repseq.core.GeneFeature)2 GeneType (io.repseq.core.GeneType)2 CUtils (cc.redberry.pipe.CUtils)1 ParallelProcessor (cc.redberry.pipe.blocks.ParallelProcessor)1 Chunk (cc.redberry.pipe.util.Chunk)1 CountLimitingOutputPort (cc.redberry.pipe.util.CountLimitingOutputPort)1 Indexer (cc.redberry.pipe.util.Indexer)1