Search in sources :

Example 6 with VDJCAlignmentsReader

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

the class ActionSortAlignments method go.

@Override
public void go(ActionHelper helper) throws Exception {
    try (VDJCAlignmentsReader reader = new VDJCAlignmentsReader(parameters.getInputFile())) {
        SmartProgressReporter.startProgressReport("Reading vdjca", reader);
        try (OutputPortCloseable<VDJCAlignments> sorted = Sorter.sort(reader, idComparator, 1024 * 512, new VDJCAlignmentsSerializer(reader), TempFileManager.getTempFile());
            VDJCAlignmentsWriter writer = new VDJCAlignmentsWriter(parameters.getOutputFile())) {
            writer.header(reader.getParameters(), reader.getUsedGenes());
            final long nReads = reader.getNumberOfReads();
            final CountingOutputPort<VDJCAlignments> counter = new CountingOutputPort<>(sorted);
            SmartProgressReporter.startProgressReport("Writing sorted alignments", SmartProgressReporter.extractProgress(counter, nReads));
            for (VDJCAlignments res : CUtils.it(counter)) writer.write(res);
            writer.setNumberOfProcessedReads(nReads);
        }
    }
}
Also used : VDJCAlignmentsReader(com.milaboratory.mixcr.basictypes.VDJCAlignmentsReader) CountingOutputPort(cc.redberry.pipe.util.CountingOutputPort) VDJCAlignmentsWriter(com.milaboratory.mixcr.basictypes.VDJCAlignmentsWriter) VDJCAlignments(com.milaboratory.mixcr.basictypes.VDJCAlignments)

Example 7 with VDJCAlignmentsReader

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

the class VersionInfoAction method go.

@Override
public void go(ActionHelper helper) throws Exception {
    String inputFile = parameters.getInputFile();
    String i = inputFile.toLowerCase();
    if (i.endsWith(".vdjca.gz") || i.endsWith(".vdjca")) {
        try (VDJCAlignmentsReader reader = new VDJCAlignmentsReader(inputFile)) {
            reader.init();
            System.out.println("MagicBytes = " + reader.getMagic());
            System.out.println(reader.getVersionInfo());
        }
    } else if (i.endsWith(".clns.gz") || i.endsWith(".clns")) {
        CloneSet cs = CloneSetIO.read(inputFile);
        System.out.println(cs.getVersionInfo());
    } else if (i.endsWith(".clna")) {
        try (ClnAReader reader = new ClnAReader(inputFile, VDJCLibraryRegistry.createDefaultRegistry())) {
            System.out.println(reader.getVersionInfo());
        }
    } else
        throw new ParameterException("Wrong file type.");
}
Also used : CloneSet(com.milaboratory.mixcr.basictypes.CloneSet) VDJCAlignmentsReader(com.milaboratory.mixcr.basictypes.VDJCAlignmentsReader) ClnAReader(com.milaboratory.mixcr.basictypes.ClnAReader) ParameterException(com.beust.jcommander.ParameterException)

Example 8 with VDJCAlignmentsReader

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

the class ActionAlignmentsDiff method go.

@Override
public void go(ActionHelper actionHelper) throws Exception {
    try (VDJCAlignmentsReader reader1 = new VDJCAlignmentsReader(parameters.get1());
        VDJCAlignmentsReader reader2 = new VDJCAlignmentsReader(parameters.get2());
        VDJCAlignmentsWriterI only1 = parameters.onlyFirst == null ? VDJCAlignmentsWriterI.DummyWriter.INSTANCE : new VDJCAlignmentsWriter(parameters.onlyFirst);
        VDJCAlignmentsWriterI only2 = parameters.onlySecond == null ? VDJCAlignmentsWriterI.DummyWriter.INSTANCE : new VDJCAlignmentsWriter(parameters.onlySecond);
        VDJCAlignmentsWriterI diff1 = parameters.diff1 == null ? VDJCAlignmentsWriterI.DummyWriter.INSTANCE : new VDJCAlignmentsWriter(parameters.diff1);
        VDJCAlignmentsWriterI diff2 = parameters.diff1 == null ? VDJCAlignmentsWriterI.DummyWriter.INSTANCE : new VDJCAlignmentsWriter(parameters.diff2);
        PrintStream report = parameters.report().equals(".") ? System.out : new PrintStream(new FileOutputStream(parameters.report()))) {
        if (reader1.getNumberOfReads() > reader2.getNumberOfReads())
            SmartProgressReporter.startProgressReport("Analyzing diff", reader1);
        else
            SmartProgressReporter.startProgressReport("Analyzing diff", reader2);
        long same = 0, onlyIn1 = 0, onlyIn2 = 0, diffFeature = 0, justDiff = 0;
        long[] diffHits = new long[GeneType.NUMBER_OF_TYPES];
        only1.header(reader1.getParameters(), reader1.getUsedGenes());
        diff1.header(reader1.getParameters(), reader1.getUsedGenes());
        only2.header(reader2.getParameters(), reader2.getUsedGenes());
        diff2.header(reader2.getParameters(), reader2.getUsedGenes());
        VDJCAlignmentsDifferenceReader diffReader = new VDJCAlignmentsDifferenceReader(reader1, reader2, parameters.getFeature(), parameters.hitsCompareLevel);
        for (VDJCAlignmentsDifferenceReader.Diff diff : CUtils.it(diffReader)) {
            switch(diff.status) {
                case AlignmentsAreSame:
                    ++same;
                    break;
                case AlignmentPresentOnlyInFirst:
                    ++onlyIn1;
                    only1.write(diff.first);
                    break;
                case AlignmentPresentOnlyInSecond:
                    ++onlyIn2;
                    only2.write(diff.second);
                    break;
                case AlignmentsAreDifferent:
                    ++justDiff;
                    diff1.write(diff.first);
                    diff2.write(diff.second);
                    if (diff.reason.diffGeneFeature)
                        ++diffFeature;
                    for (Map.Entry<GeneType, Boolean> e : diff.reason.diffHits.entrySet()) if (e.getValue())
                        ++diffHits[e.getKey().ordinal()];
            }
        }
        only1.setNumberOfProcessedReads(onlyIn1);
        only2.setNumberOfProcessedReads(onlyIn2);
        diff1.setNumberOfProcessedReads(justDiff);
        diff2.setNumberOfProcessedReads(justDiff);
        report.println("First  file: " + parameters.get1());
        report.println("Second file: " + parameters.get2());
        report.println("Completely same reads: " + same);
        report.println("Aligned reads present only in the FIRST  file: " + onlyIn1 + " (" + Util.PERCENT_FORMAT.format(100. * onlyIn1 / reader1.getNumberOfReads()) + ")%");
        report.println("Aligned reads present only in the SECOND file: " + onlyIn2 + " (" + Util.PERCENT_FORMAT.format(100. * onlyIn2 / reader2.getNumberOfReads()) + ")%");
        report.println("Total number of different reads: " + justDiff);
        report.println("Reads with not same " + parameters.geneFeatureToMatch + ": " + diffFeature);
        for (GeneType geneType : GeneType.VDJC_REFERENCE) report.println("Reads with not same " + geneType.name() + " hits: " + diffHits[geneType.ordinal()]);
    }
}
Also used : PrintStream(java.io.PrintStream) VDJCAlignmentsReader(com.milaboratory.mixcr.basictypes.VDJCAlignmentsReader) VDJCAlignmentsWriterI(com.milaboratory.mixcr.basictypes.VDJCAlignmentsWriterI) VDJCAlignmentsWriter(com.milaboratory.mixcr.basictypes.VDJCAlignmentsWriter) VDJCAlignmentsDifferenceReader(com.milaboratory.mixcr.util.VDJCAlignmentsDifferenceReader) FileOutputStream(java.io.FileOutputStream) GeneType(io.repseq.core.GeneType) Map(java.util.Map)

Example 9 with VDJCAlignmentsReader

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

the class ActionAssemble method go.

@Override
public void go(ActionHelper helper) throws Exception {
    // Saving initial timestamp
    long beginTimestamp = System.currentTimeMillis();
    // Checking consistency between actionParameters.doWriteClnA() value and file extension
    if ((actionParameters.getOutputFileName().toLowerCase().endsWith(".clna") && !actionParameters.doWriteClnA()) || (actionParameters.getOutputFileName().toLowerCase().endsWith(".clns") && actionParameters.doWriteClnA()))
        System.out.println("WARNING: Unexpected file extension, use .clns extension for clones-only (normal) output and\n" + ".clna if -a / --write-alignments options specified.");
    // Extracting V/D/J/C gene list from input vdjca file
    final List<VDJCGene> genes;
    final VDJCAlignerParameters alignerParameters;
    try (VDJCAlignmentsReader reader = new VDJCAlignmentsReader(actionParameters.getInputFileName(), VDJCLibraryRegistry.getDefault())) {
        genes = reader.getUsedGenes();
        // Saving aligner parameters to correct assembler parameters
        alignerParameters = reader.getParameters();
    }
    AlignmentsProvider alignmentsProvider = AlignmentsProvider.Util.createProvider(actionParameters.getInputFileName(), VDJCLibraryRegistry.getDefault());
    CloneAssemblerParameters assemblerParameters = actionParameters.getCloneAssemblerParameters();
    // set aligner parameters
    assemblerParameters.updateFrom(alignerParameters);
    // Overriding JSON parameters
    if (!actionParameters.overrides.isEmpty()) {
        assemblerParameters = JsonOverrider.override(assemblerParameters, CloneAssemblerParameters.class, actionParameters.overrides);
        if (assemblerParameters == null) {
            System.err.println("Failed to override some parameter.");
            System.exit(1);
        }
    }
    // Performing assembly
    try (CloneAssembler assembler = new CloneAssembler(assemblerParameters, actionParameters.doWriteClnA() || actionParameters.events != null, genes, alignerParameters.getFeaturesToAlignMap())) {
        // Creating event listener to collect run statistics
        CloneAssemblerReport report = new CloneAssemblerReport();
        report.setStartMillis(beginTimestamp);
        report.setInputFiles(new String[] { actionParameters.getInputFileName() });
        report.setOutputFiles(new String[] { actionParameters.getOutputFileName() });
        report.setCommandLine(helper.getCommandLineArguments());
        assembler.setListener(report);
        // Running assembler
        CloneAssemblerRunner assemblerRunner = new CloneAssemblerRunner(alignmentsProvider, assembler, actionParameters.threads);
        SmartProgressReporter.startProgressReport(assemblerRunner);
        assemblerRunner.run();
        // Getting results
        final CloneSet cloneSet = assemblerRunner.getCloneSet(alignerParameters);
        // Passing final cloneset to assemble last pieces of statistics for report
        report.onClonesetFinished(cloneSet);
        // Writing results
        if (actionParameters.doWriteClnA())
            try (ClnAWriter writer = new ClnAWriter(actionParameters.getOutputFileName())) {
                // writer will supply current stage and completion percent to the progress reporter
                SmartProgressReporter.startProgressReport(writer);
                // Writing clone block
                writer.writeClones(cloneSet);
                // Pre-soring alignments
                try (AlignmentsMappingMerger merged = new AlignmentsMappingMerger(alignmentsProvider.create(), assembler.getAssembledReadsPort())) {
                    writer.sortAlignments(merged, assembler.getAlignmentsCount());
                }
                writer.writeAlignmentsAndIndex();
            }
        else
            try (CloneSetIO.CloneSetWriter writer = new CloneSetIO.CloneSetWriter(cloneSet, actionParameters.getOutputFileName())) {
                SmartProgressReporter.startProgressReport(writer);
                writer.write();
            }
        // Writing report
        report.setFinishMillis(System.currentTimeMillis());
        assert cloneSet.getClones().size() == report.getCloneCount();
        report.setTotalReads(alignmentsProvider.getTotalNumberOfReads());
        // Writing report to stout
        System.out.println("============= Report ==============");
        Util.writeReportToStdout(report);
        if (actionParameters.report != null)
            Util.writeReport(actionParameters.report, report);
        if (actionParameters.jsonReport != null)
            Util.writeJsonReport(actionParameters.jsonReport, report);
        // Writing raw events (not documented feature)
        if (actionParameters.events != null)
            try (PipeWriter<ReadToCloneMapping> writer = new PipeWriter<>(actionParameters.events)) {
                CUtils.drain(assembler.getAssembledReadsPort(), writer);
            }
    }
}
Also used : VDJCAlignerParameters(com.milaboratory.mixcr.vdjaligners.VDJCAlignerParameters) VDJCAlignmentsReader(com.milaboratory.mixcr.basictypes.VDJCAlignmentsReader) CloneSetIO(com.milaboratory.mixcr.basictypes.CloneSetIO) PipeWriter(com.milaboratory.primitivio.PipeWriter) CloneSet(com.milaboratory.mixcr.basictypes.CloneSet) ClnAWriter(com.milaboratory.mixcr.basictypes.ClnAWriter) VDJCGene(io.repseq.core.VDJCGene)

Example 10 with VDJCAlignmentsReader

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

the class ActionAssemblePartialAlignments method go.

@Override
public void go(ActionHelper helper) throws Exception {
    if (parameters.writePartial != null)
        System.err.println("'-p' option is deprecated and will be removed in 2.2. " + "Use '-d' option to drop not-overlapped partial reads.");
    // Saving initial timestamp
    long beginTimestamp = System.currentTimeMillis();
    PartialAlignmentsAssemblerParameters assemblerParameters = PartialAlignmentsAssemblerParameters.getDefault();
    if (!parameters.overrides.isEmpty()) {
        assemblerParameters = JsonOverrider.override(assemblerParameters, PartialAlignmentsAssemblerParameters.class, parameters.overrides);
        if (assemblerParameters == null) {
            System.err.println("Failed to override some parameter.");
            return;
        }
    }
    try (PartialAlignmentsAssembler assembler = new PartialAlignmentsAssembler(assemblerParameters, parameters.getOutputFileName(), !parameters.doDropPartial(), parameters.getOverlappedOnly())) {
        ReportWrapper report = new ReportWrapper(command(), assembler);
        report.setStartMillis(beginTimestamp);
        report.setInputFiles(parameters.getInputFileName());
        report.setOutputFiles(parameters.getOutputFileName());
        report.setCommandLine(helper.getCommandLineArguments());
        try (VDJCAlignmentsReader reader = new VDJCAlignmentsReader(parameters.getInputFileName())) {
            SmartProgressReporter.startProgressReport("Building index", reader);
            assembler.buildLeftPartsIndex(reader);
        }
        try (VDJCAlignmentsReader reader = new VDJCAlignmentsReader(parameters.getInputFileName())) {
            SmartProgressReporter.startProgressReport("Searching for overlaps", reader);
            assembler.searchOverlaps(reader);
        }
        report.setFinishMillis(System.currentTimeMillis());
        // Writing report to stout
        System.out.println("============= Report ==============");
        Util.writeReportToStdout(report);
        if (parameters.report != null)
            Util.writeReport(parameters.report, report);
        if (parameters.jsonReport != null)
            Util.writeJsonReport(parameters.jsonReport, report);
    }
}
Also used : VDJCAlignmentsReader(com.milaboratory.mixcr.basictypes.VDJCAlignmentsReader) PartialAlignmentsAssembler(com.milaboratory.mixcr.partialassembler.PartialAlignmentsAssembler) PartialAlignmentsAssemblerParameters(com.milaboratory.mixcr.partialassembler.PartialAlignmentsAssemblerParameters)

Aggregations

VDJCAlignmentsReader (com.milaboratory.mixcr.basictypes.VDJCAlignmentsReader)10 VDJCAlignments (com.milaboratory.mixcr.basictypes.VDJCAlignments)5 VDJCAlignmentsWriter (com.milaboratory.mixcr.basictypes.VDJCAlignmentsWriter)4 CloneSet (com.milaboratory.mixcr.basictypes.CloneSet)2 VDJCGene (io.repseq.core.VDJCGene)2 FileOutputStream (java.io.FileOutputStream)2 PrintStream (java.io.PrintStream)2 FilteringPort (cc.redberry.pipe.blocks.FilteringPort)1 CountLimitingOutputPort (cc.redberry.pipe.util.CountLimitingOutputPort)1 CountingOutputPort (cc.redberry.pipe.util.CountingOutputPort)1 ParameterException (com.beust.jcommander.ParameterException)1 SingleRead (com.milaboratory.core.io.sequence.SingleRead)1 SingleFastqReader (com.milaboratory.core.io.sequence.fastq.SingleFastqReader)1 ClnAReader (com.milaboratory.mixcr.basictypes.ClnAReader)1 ClnAWriter (com.milaboratory.mixcr.basictypes.ClnAWriter)1 CloneSetIO (com.milaboratory.mixcr.basictypes.CloneSetIO)1 VDJCAlignmentsWriterI (com.milaboratory.mixcr.basictypes.VDJCAlignmentsWriterI)1 InfoWriter (com.milaboratory.mixcr.export.InfoWriter)1 AlignmentInfoCollector (com.milaboratory.mixcr.info.AlignmentInfoCollector)1 GeneFeatureCoverageCollector (com.milaboratory.mixcr.info.GeneFeatureCoverageCollector)1