Search in sources :

Example 1 with PipeWriter

use of com.milaboratory.primitivio.PipeWriter 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)

Aggregations

ClnAWriter (com.milaboratory.mixcr.basictypes.ClnAWriter)1 CloneSet (com.milaboratory.mixcr.basictypes.CloneSet)1 CloneSetIO (com.milaboratory.mixcr.basictypes.CloneSetIO)1 VDJCAlignmentsReader (com.milaboratory.mixcr.basictypes.VDJCAlignmentsReader)1 VDJCAlignerParameters (com.milaboratory.mixcr.vdjaligners.VDJCAlignerParameters)1 PipeWriter (com.milaboratory.primitivio.PipeWriter)1 VDJCGene (io.repseq.core.VDJCGene)1