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();
}
}
Aggregations