Search in sources :

Example 1 with FlatteningOutputPort

use of cc.redberry.pipe.util.FlatteningOutputPort in project mixcr by milaboratory.

the class ActionSlice method sliceClnA.

void sliceClnA(ActionHelper helper) throws Exception {
    try (ClnAReader reader = new ClnAReader(params.getInputFileName(), VDJCLibraryRegistry.getDefault());
        ClnAWriter writer = new ClnAWriter(params.getOutputFileName())) {
        // Getting full clone set
        CloneSet cloneSet = reader.readCloneSet();
        // old clone id -> new clone id
        TIntIntHashMap idMapping = new TIntIntHashMap();
        long newNumberOfAlignments = 0;
        // Creating new cloneset
        List<Clone> clones = new ArrayList<>();
        int i = 0;
        List<OutputPort<VDJCAlignments>> allAlignmentsList = new ArrayList<>();
        for (Long cloneId_ : params.ids) {
            int cloneId = (int) ((long) cloneId_);
            newNumberOfAlignments += reader.numberOfAlignmentsInClone(cloneId);
            Clone clone = cloneSet.get(cloneId);
            idMapping.put(clone.getId(), i);
            clones.add(clone.setId(i).resetParentCloneSet());
            OutputPort<VDJCAlignments> als = reader.readAlignmentsOfClone(cloneId);
            final int ii = i;
            allAlignmentsList.add(() -> {
                VDJCAlignments al = als.take();
                if (al == null)
                    return null;
                return al.updateCloneIndex(ii);
            });
            i++;
        }
        CloneSet newCloneSet = new CloneSet(clones, cloneSet.getUsedGenes(), cloneSet.getAlignedFeatures(), cloneSet.getAlignmentParameters(), cloneSet.getAssemblerParameters());
        OutputPort<VDJCAlignments> allAlignmentsPortRaw = new FlatteningOutputPort<>(CUtils.asOutputPort(allAlignmentsList));
        AtomicLong idGen = new AtomicLong();
        OutputPort<VDJCAlignments> allAlignmentsPort = () -> {
            VDJCAlignments al = allAlignmentsPortRaw.take();
            if (al == null)
                return null;
            return al.setAlignmentsIndex(idGen.getAndIncrement());
        };
        writer.writeClones(newCloneSet);
        writer.sortAlignments(allAlignmentsPort, newNumberOfAlignments);
        writer.writeAlignmentsAndIndex();
    }
}
Also used : OutputPort(cc.redberry.pipe.OutputPort) FlatteningOutputPort(cc.redberry.pipe.util.FlatteningOutputPort) ArrayList(java.util.ArrayList) FlatteningOutputPort(cc.redberry.pipe.util.FlatteningOutputPort) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) TIntIntHashMap(gnu.trove.map.hash.TIntIntHashMap)

Aggregations

OutputPort (cc.redberry.pipe.OutputPort)1 FlatteningOutputPort (cc.redberry.pipe.util.FlatteningOutputPort)1 TIntIntHashMap (gnu.trove.map.hash.TIntIntHashMap)1 ArrayList (java.util.ArrayList)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1