This repository was archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrentStreamedEstimateLibraryComplexity.java
More file actions
130 lines (101 loc) · 4.93 KB
/
ConcurrentStreamedEstimateLibraryComplexity.java
File metadata and controls
130 lines (101 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package picard.sam.markduplicates;
/*
* Created by GoodforGod
* Date: 09.02.2017
* Time: 17:29
*/
import htsjdk.samtools.SAMReadGroupRecord;
import htsjdk.samtools.util.*;
import picard.cmdline.CommandLineProgramProperties;
import picard.cmdline.programgroups.Metrics;
import picard.sam.markduplicates.util.ConcurrentSortingCollection;
import picard.sam.markduplicates.util.QueueProducer;
import java.util.List;
import java.util.concurrent.*;
import java.util.function.Predicate;
import static java.lang.Math.decrementExact;
import static java.lang.Math.pow;
/*
* Streamed version of ELC, implements all major improvements, uses streams in most cases
*/
@CommandLineProgramProperties(
usage = EstimateLibraryComplexity.USAGE_SUMMARY + EstimateLibraryComplexity.USAGE_DETAILS,
usageShort = EstimateLibraryComplexity.USAGE_SUMMARY,
programGroup = Metrics.class
)
public class ConcurrentStreamedEstimateLibraryComplexity extends ConcurrentExecutorEstimateLibraryComplexity
{
protected final Log log = Log.getInstance(ConcurrentStreamedEstimateLibraryComplexity.class);
public ConcurrentStreamedEstimateLibraryComplexity() {
super();
}
protected int doWork() {
IOUtil.assertFilesAreReadable(INPUT);
final boolean useBarcodes = (null != BARCODE_TAG
|| null != READ_ONE_BARCODE_TAG
|| null != READ_TWO_BARCODE_TAG);
final ElcSmartSortResponse response = doSmartSort(useBarcodes);
final long doWorkStartTime = System.nanoTime();
final ConcurrentSortingCollection<PairedReadSequence> sorter = response.getSorter();
final ProgressLogger progress = response.getProgress();
final List<SAMReadGroupRecord> readGroups = response.getReadGroup();
final int meanGroupSize = (int) (Math.max(1, (progress.getCount() / 2) / (int) pow(4, MIN_IDENTICAL_BASES * 2)));
final Predicate<List<PairedReadSequence>> isGroupValid = (grp) -> grp.size() > meanGroupSize * MAX_GROUP_RATIO;
final ConcurrentHistoCollection histoCollection = new ConcurrentHistoCollection(useBarcodes);
final ConcurrentSupplier<List<PairedReadSequence>> groupSupplier
= new ConcurrentSupplier<>(GROUP_PROCESS_STACK_SIZE, USED_THREADS);
final QueueProducer<PairedReadSequence, List<PairedReadSequence>> pairProducer
= new QueueProducer<>(new PeekableIterator<>(sorter.iterator()), pairHandler);
final ForkJoinPool pool = new ForkJoinPool();
final long groupStartTime = System.nanoTime();
// Pool process sorted groups
pool.execute(() -> {
while (!Thread.interrupted()) {
final List<List<PairedReadSequence>> groupList = groupSupplier.getJob();
if (isPairsPoisonPill.test(groupList))
return;
pool.submit(() -> {
groupList.stream()
.parallel()
.unordered()
.flatMap(grp -> splitByLibrary(grp, readGroups).entrySet().stream())
.forEach(histoCollection::processGroup);
groupSupplier.confirm();
});
}
});
// Now go through the sorted reads and attempt to find duplicates
while (pairProducer.hasNext()) {
final List<PairedReadSequence> group = pairProducer.next();
if (isGroupValid.test(group))
logInvalidGroup(group, meanGroupSize);
else
groupSupplier.add(group);
}
groupSupplier.tryAddRest();
groupSupplier.awaitConfirmation();
groupSupplier.finish();
double groupEndTime = System.nanoTime();
pairProducer.finish();
sorter.cleanup();
final long metricStartTime = System.nanoTime();
final ConcurrentMetrics concurrentMetrics = new ConcurrentMetrics(histoCollection);
histoCollection.getLibraries().stream()
.parallel()
.unordered()
.forEach(concurrentMetrics::add);
concurrentMetrics.fillFile();
pool.shutdown();
try { pool.awaitTermination(1000, TimeUnit.SECONDS); }
catch (InterruptedException e) { e.printStackTrace(); }
double doWorkEndTime = System.nanoTime();
double doWorkTotal = (doWorkEndTime - doWorkStartTime) / 1000000;
log.info("GROUPS - STREAMED (ms) : " + ((groupEndTime - groupStartTime) / 1000000));
log.info("METRIC - STREAMED (ms) : " + ((doWorkEndTime - metricStartTime) / 1000000));
log.info("doWork - STREAMED (ms) : " + doWorkTotal);
log.info("----------------------------------------");
log.info("TOTAL - STREAMED (ms) : " + (sortTime + doWorkTotal));
concurrentMetrics.writeFile();
return 0;
}
}