Search in sources :

Example 1 with PipeDataInputReader

use of com.milaboratory.primitivio.PipeDataInputReader in project mixcr by milaboratory.

the class ActionAssembleContigs method go.

@Override
public void go(ActionHelper helper) throws Exception {
    // TODO FIX!!!!!!!!!!!!!
    if (parameters.threads > 1)
        throw new ParameterException("Multithreaded processing is not supported yet.");
    long beginTimestamp = System.currentTimeMillis();
    FullSeqAssemblerParameters p = FullSeqAssemblerParameters.getByName("default");
    if (!parameters.overrides.isEmpty()) {
        // Perform parameters overriding
        p = JsonOverrider.override(p, FullSeqAssemblerParameters.class, parameters.overrides);
        if (p == null)
            throw new ProcessException("Failed to override some parameter.");
    }
    final FullSeqAssemblerReport report = new FullSeqAssemblerReport();
    FullSeqAssemblerParameters assemblerParameters = p;
    int totalClonesCount = 0;
    List<VDJCGene> genes;
    VDJCAlignerParameters alignerParameters;
    CloneAssemblerParameters cloneAssemblerParameters;
    try (ClnAReader reader = new ClnAReader(parameters.getInputFileName(), VDJCLibraryRegistry.getDefault());
        PrimitivO tmpOut = new PrimitivO(new BufferedOutputStream(new FileOutputStream(parameters.getOutputFileName())))) {
        final CloneFactory cloneFactory = new CloneFactory(reader.getAssemblerParameters().getCloneFactoryParameters(), reader.getAssemblingFeatures(), reader.getGenes(), reader.getAlignerParameters().getFeaturesToAlignMap());
        alignerParameters = reader.getAlignerParameters();
        cloneAssemblerParameters = reader.getAssemblerParameters();
        genes = reader.getGenes();
        IOUtil.registerGeneReferences(tmpOut, genes, alignerParameters);
        ClnAReader.CloneAlignmentsPort cloneAlignmentsPort = reader.clonesAndAlignments();
        SmartProgressReporter.startProgressReport("Assembling", cloneAlignmentsPort);
        OutputPort<Clone[]> parallelProcessor = new ParallelProcessor<>(cloneAlignmentsPort, cloneAlignments -> {
            FullSeqAssembler fullSeqAssembler = new FullSeqAssembler(cloneFactory, assemblerParameters, cloneAlignments.clone, alignerParameters);
            fullSeqAssembler.setReport(report);
            FullSeqAssembler.RawVariantsData rawVariantsData = fullSeqAssembler.calculateRawData(() -> {
                try {
                    return cloneAlignments.alignments();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
            return fullSeqAssembler.callVariants(rawVariantsData);
        }, parameters.threads);
        for (Clone[] clones : CUtils.it(parallelProcessor)) {
            totalClonesCount += clones.length;
            for (Clone cl : clones) tmpOut.writeObject(cl);
        }
        assert report.getInitialCloneCount() == reader.numberOfClones();
    }
    assert report.getFinalCloneCount() == totalClonesCount;
    assert report.getFinalCloneCount() >= report.getInitialCloneCount();
    Clone[] clones = new Clone[totalClonesCount];
    try (PrimitivI tmpIn = new PrimitivI(new BufferedInputStream(new FileInputStream(parameters.getOutputFileName())))) {
        IOUtil.registerGeneReferences(tmpIn, genes, alignerParameters);
        int i = 0;
        for (Clone clone : CUtils.it(new PipeDataInputReader<>(Clone.class, tmpIn, totalClonesCount))) clones[i++] = clone;
    }
    Arrays.sort(clones, Comparator.comparingDouble(c -> -c.getCount()));
    for (int i = 0; i < clones.length; i++) clones[i] = clones[i].setId(i);
    CloneSet cloneSet = new CloneSet(Arrays.asList(clones), genes, alignerParameters.getFeaturesToAlignMap(), alignerParameters, cloneAssemblerParameters);
    try (CloneSetIO.CloneSetWriter writer = new CloneSetIO.CloneSetWriter(cloneSet, parameters.getOutputFileName())) {
        SmartProgressReporter.startProgressReport(writer);
        writer.write();
    }
    ReportWrapper reportWrapper = new ReportWrapper(command(), report);
    reportWrapper.setStartMillis(beginTimestamp);
    reportWrapper.setInputFiles(parameters.getInputFileName());
    reportWrapper.setOutputFiles(parameters.getOutputFileName());
    reportWrapper.setCommandLine(helper.getCommandLineArguments());
    reportWrapper.setFinishMillis(System.currentTimeMillis());
    // Writing report to stout
    System.out.println("============= Report ==============");
    Util.writeReportToStdout(report);
    if (parameters.report != null)
        Util.writeReport(parameters.report, reportWrapper);
    if (parameters.jsonReport != null)
        Util.writeJsonReport(parameters.jsonReport, reportWrapper);
}
Also used : Parameters(com.beust.jcommander.Parameters) java.util(java.util) ParameterException(com.beust.jcommander.ParameterException) Action(com.milaboratory.cli.Action) Parameter(com.beust.jcommander.Parameter) CloneAssemblerParameters(com.milaboratory.mixcr.assembler.CloneAssemblerParameters) com.milaboratory.mixcr.basictypes(com.milaboratory.mixcr.basictypes) FullSeqAssemblerParameters(com.milaboratory.mixcr.assembler.fullseq.FullSeqAssemblerParameters) ParallelProcessor(cc.redberry.pipe.blocks.ParallelProcessor) VDJCAlignerParameters(com.milaboratory.mixcr.vdjaligners.VDJCAlignerParameters) PositiveInteger(com.beust.jcommander.validators.PositiveInteger) CloneFactory(com.milaboratory.mixcr.assembler.CloneFactory) FullSeqAssembler(com.milaboratory.mixcr.assembler.fullseq.FullSeqAssembler) ActionHelper(com.milaboratory.cli.ActionHelper) PrimitivO(com.milaboratory.primitivio.PrimitivO) CUtils(cc.redberry.pipe.CUtils) FullSeqAssemblerReport(com.milaboratory.mixcr.assembler.fullseq.FullSeqAssemblerReport) PrimitivI(com.milaboratory.primitivio.PrimitivI) OutputPort(cc.redberry.pipe.OutputPort) PipeDataInputReader(com.milaboratory.primitivio.PipeDataInputReader) java.io(java.io) VDJCGene(io.repseq.core.VDJCGene) SmartProgressReporter(com.milaboratory.util.SmartProgressReporter) DynamicParameter(com.beust.jcommander.DynamicParameter) VDJCLibraryRegistry(io.repseq.core.VDJCLibraryRegistry) ProcessException(com.milaboratory.cli.ProcessException) ActionParametersWithOutput(com.milaboratory.cli.ActionParametersWithOutput) VDJCAlignerParameters(com.milaboratory.mixcr.vdjaligners.VDJCAlignerParameters) FullSeqAssemblerParameters(com.milaboratory.mixcr.assembler.fullseq.FullSeqAssemblerParameters) CloneAssemblerParameters(com.milaboratory.mixcr.assembler.CloneAssemblerParameters) ParallelProcessor(cc.redberry.pipe.blocks.ParallelProcessor) ParameterException(com.beust.jcommander.ParameterException) PrimitivO(com.milaboratory.primitivio.PrimitivO) FullSeqAssembler(com.milaboratory.mixcr.assembler.fullseq.FullSeqAssembler) FullSeqAssemblerReport(com.milaboratory.mixcr.assembler.fullseq.FullSeqAssemblerReport) ProcessException(com.milaboratory.cli.ProcessException) VDJCGene(io.repseq.core.VDJCGene) CloneFactory(com.milaboratory.mixcr.assembler.CloneFactory) PrimitivI(com.milaboratory.primitivio.PrimitivI)

Example 2 with PipeDataInputReader

use of com.milaboratory.primitivio.PipeDataInputReader in project mixcr by milaboratory.

the class ClnAReader method readClones.

/**
 * Constructs output port to read clones one by one as a stream
 */
public OutputPort<Clone> readClones() throws IOException {
    PrimitivI input = new PrimitivI(new InputDataStream(firstClonePosition, index[0]));
    IOUtil.registerGeneReferences(input, genes, alignedFeatures);
    return new PipeDataInputReader<>(Clone.class, input, numberOfClones());
}
Also used : PipeDataInputReader(com.milaboratory.primitivio.PipeDataInputReader) PrimitivI(com.milaboratory.primitivio.PrimitivI)

Example 3 with PipeDataInputReader

use of com.milaboratory.primitivio.PipeDataInputReader in project mixcr by milaboratory.

the class ClnAReader method readAllAlignments.

/**
 * Constructs output port to read all alignments form the file. Alignments are sorted by cloneIndex.
 */
public OutputPort<VDJCAlignments> readAllAlignments() throws IOException {
    PrimitivI input = new PrimitivI(new InputDataStream(index[0], index[index.length - 1]));
    IOUtil.registerGeneReferences(input, genes, alignedFeatures);
    return new PipeDataInputReader<>(VDJCAlignments.class, input, totalAlignmentsCount);
}
Also used : PipeDataInputReader(com.milaboratory.primitivio.PipeDataInputReader) PrimitivI(com.milaboratory.primitivio.PrimitivI)

Example 4 with PipeDataInputReader

use of com.milaboratory.primitivio.PipeDataInputReader in project mixcr by milaboratory.

the class ClnAReader method readAlignmentsOfClone.

/**
 * Constructs output port to read alignments for a specific clone, or read unassembled alignments block
 *
 * @param cloneIndex index of clone; -1 to read unassembled alignments
 */
public OutputPort<VDJCAlignments> readAlignmentsOfClone(int cloneIndex) throws IOException {
    PrimitivI input = new PrimitivI(new InputDataStream(index[cloneIndex + 1], index[cloneIndex + 2]));
    IOUtil.registerGeneReferences(input, genes, alignedFeatures);
    return new PipeDataInputReader<>(VDJCAlignments.class, input, counts[cloneIndex + 1]);
}
Also used : PipeDataInputReader(com.milaboratory.primitivio.PipeDataInputReader) PrimitivI(com.milaboratory.primitivio.PrimitivI)

Example 5 with PipeDataInputReader

use of com.milaboratory.primitivio.PipeDataInputReader in project mixcr by milaboratory.

the class ClnAReader method readAssembledAlignments.

/**
 * Constructs output port to read all alignments that are attached to a clone. Alignments are sorted by cloneIndex.
 */
public OutputPort<VDJCAlignments> readAssembledAlignments() throws IOException {
    PrimitivI input = new PrimitivI(new InputDataStream(index[1], index[index.length - 1]));
    IOUtil.registerGeneReferences(input, genes, alignedFeatures);
    return new PipeDataInputReader<>(VDJCAlignments.class, input, totalAlignmentsCount - counts[0]);
}
Also used : PipeDataInputReader(com.milaboratory.primitivio.PipeDataInputReader) PrimitivI(com.milaboratory.primitivio.PrimitivI)

Aggregations

PipeDataInputReader (com.milaboratory.primitivio.PipeDataInputReader)5 PrimitivI (com.milaboratory.primitivio.PrimitivI)5 CUtils (cc.redberry.pipe.CUtils)1 OutputPort (cc.redberry.pipe.OutputPort)1 ParallelProcessor (cc.redberry.pipe.blocks.ParallelProcessor)1 DynamicParameter (com.beust.jcommander.DynamicParameter)1 Parameter (com.beust.jcommander.Parameter)1 ParameterException (com.beust.jcommander.ParameterException)1 Parameters (com.beust.jcommander.Parameters)1 PositiveInteger (com.beust.jcommander.validators.PositiveInteger)1 Action (com.milaboratory.cli.Action)1 ActionHelper (com.milaboratory.cli.ActionHelper)1 ActionParametersWithOutput (com.milaboratory.cli.ActionParametersWithOutput)1 ProcessException (com.milaboratory.cli.ProcessException)1 CloneAssemblerParameters (com.milaboratory.mixcr.assembler.CloneAssemblerParameters)1 CloneFactory (com.milaboratory.mixcr.assembler.CloneFactory)1 FullSeqAssembler (com.milaboratory.mixcr.assembler.fullseq.FullSeqAssembler)1 FullSeqAssemblerParameters (com.milaboratory.mixcr.assembler.fullseq.FullSeqAssemblerParameters)1 FullSeqAssemblerReport (com.milaboratory.mixcr.assembler.fullseq.FullSeqAssemblerReport)1 com.milaboratory.mixcr.basictypes (com.milaboratory.mixcr.basictypes)1