-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathminimizer_bucketing.rs
380 lines (330 loc) · 12.9 KB
/
minimizer_bucketing.rs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use crate::colors_manager::{ColorsManager, MinimizerBucketingSeqColorData};
use crate::hash::ExtendableHashTraitType;
use crate::hash::HashFunction;
use crate::hash::HashFunctionFactory;
use crate::intermediate_storage::{
IntermediateReadsWriter, IntermediateSequencesStorage, SequenceExtraData,
};
use crate::pipeline::kmers_merge::{KmersFlags, MapEntry};
use crate::pipeline::Pipeline;
use crate::rolling_kseq_iterator::{RollingKseqImpl, RollingKseqIterator};
use crate::rolling_minqueue::RollingMinQueue;
use crate::rolling_quality_check::{RollingQualityCheck, LOGPROB_MULTIPLIER, SCORES_INDEX};
use crate::sequences_reader::{FastaSequence, SequencesReader};
use crate::types::BucketIndexType;
use crate::KEEP_FILES;
use bstr::ByteSlice;
use crossbeam::channel::*;
use crossbeam::queue::{ArrayQueue, SegQueue};
use crossbeam::{scope, thread};
use hashbrown::HashMap;
use itertools::Itertools;
use nix::sys::ptrace::cont;
use object_pool::Pool;
use parallel_processor::multi_thread_buckets::MultiThreadBuckets;
use parallel_processor::phase_times_monitor::PHASES_TIMES_MONITOR;
use parallel_processor::threadpools_chain::{
ObjectsPoolManager, ThreadChainObject, ThreadPoolDefinition, ThreadPoolsChain,
};
use rayon::iter::ParallelIterator;
use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator};
use std::cmp::{max, min};
use std::intrinsics::unlikely;
use std::mem::swap;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::process::exit;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::{sleep, Thread};
use std::time::{Duration, Instant};
static SEQ_COUNT: AtomicU64 = AtomicU64::new(0);
static TOT_BASES_COUNT: AtomicU64 = AtomicU64::new(0);
static VALID_BASES_COUNT: AtomicU64 = AtomicU64::new(0);
struct QueueData {
data: Vec<u8>,
sequences: Vec<(usize, usize, usize, usize)>,
pub file_index: u64,
}
impl QueueData {
fn new(capacity: usize) -> Self {
Self {
data: Vec::with_capacity(capacity),
sequences: Vec::with_capacity(capacity / 512),
file_index: 0,
}
}
fn push_sequences(&mut self, seq: FastaSequence) -> bool {
let qual_len = seq.qual.map(|q| q.len()).unwrap_or(0);
let ident_len = seq.ident.len();
let seq_len = seq.seq.len();
let tot_len = qual_len + ident_len + seq_len;
if self.data.len() != 0 && (self.data.capacity() - self.data.len()) < tot_len {
return false;
}
let mut start = self.data.len();
self.data.extend_from_slice(seq.ident);
self.data.extend_from_slice(seq.seq);
if let Some(qual) = seq.qual {
self.data.extend_from_slice(qual);
}
self.sequences.push((start, ident_len, seq_len, qual_len));
true
}
fn iter_sequences(&self) -> impl Iterator<Item = FastaSequence> {
self.sequences
.iter()
.map(move |&(start, id_len, seq_len, qual_len)| {
let mut start = start;
let ident = &self.data[start..start + id_len];
start += id_len;
let seq = &self.data[start..start + seq_len];
start += seq_len;
let qual = match qual_len {
0 => None,
_ => Some(&self.data[start..start + qual_len]),
};
FastaSequence { ident, seq, qual }
})
}
}
impl ThreadChainObject for QueueData {
type InitData = usize;
fn initialize(params: &Self::InitData) -> Self {
Self::new(*params)
}
fn reset(&mut self) {
self.data.clear();
self.sequences.clear();
}
}
struct ExecutionContext<CX: ColorsManager> {
k: usize,
m: usize,
buckets_count: usize,
buckets: MultiThreadBuckets<
IntermediateReadsWriter<KmersFlags<CX::MinimizerBucketingSeqColorDataType>>,
>,
quality_threshold: Option<f64>,
}
struct CounterTracker<'a>(&'a AtomicUsize);
impl<'a> CounterTracker<'a> {
fn track(counter: &'a AtomicUsize) -> CounterTracker {
counter.fetch_add(1, Ordering::Relaxed);
CounterTracker(counter)
}
}
impl Drop for CounterTracker<'_> {
fn drop(&mut self) {
self.0.fetch_sub(1, Ordering::Relaxed);
}
}
const CHUNKS_SIZE: usize = 1024 * 1024 * 16;
const MAX_READING_THREADS: usize = 2;
const WATERMARK_HIGH: usize = 64;
fn worker<H: HashFunctionFactory, CX: ColorsManager>(
context: &ExecutionContext<CX>,
manager: ObjectsPoolManager<(), QueueData>,
) {
let mut minimizer_queue = RollingMinQueue::<H>::new(context.k - context.m);
let mut tmp_reads_buffer =
IntermediateSequencesStorage::new(context.buckets_count, &context.buckets);
let mut quality_check = RollingQualityCheck::new();
let quality_log_threshold: u64 = RollingQualityCheck::get_log_for_correct_probability(
context.quality_threshold.unwrap_or(0.0),
); //0.9967);
while let Some(data) = manager.recv_obj() {
let mut total_bases = 0;
let mut valid_bases = 0;
for x in data.iter_sequences() {
let mut start = 0;
let mut end = 0;
total_bases += x.seq.len() as u64;
let mut process_sequences = |process_fn: &mut dyn FnMut(&[u8])| {
while end < x.seq.len() {
start = end;
// Skip all not recognized characters
while start < x.seq.len() && x.seq[start] == b'N' {
start += 1;
}
end = start;
// Find the last valid character in this sequence
while end < x.seq.len() && x.seq[end] != b'N' {
end += 1;
}
// If the length of the read is long enough, return it
if end - start >= context.k {
if context.quality_threshold.is_some() && x.qual.is_some() {
let q = x.qual.unwrap();
for (valid, mut el) in &RollingKseqIterator::iter_seq(
&q[start..end],
context.k,
&mut quality_check,
)
.enumerate()
.group_by(|(_, x)| *x < quality_log_threshold)
{
if !valid {
continue;
}
let first_el = el.next().unwrap();
let last_el = el.last().unwrap_or(first_el);
let start_index = start + first_el.0;
let end_index = start + last_el.0 + context.k;
valid_bases += (end_index - start_index) as u64;
process_fn(&x.seq[start_index..end_index]);
}
} else {
valid_bases += (end - start) as u64;
process_fn(&x.seq[start..end]);
}
}
}
};
process_sequences(&mut |seq| {
let hashes = H::new(&seq[..], context.m);
let mut rolling_iter =
minimizer_queue.make_iter(hashes.iter().map(|x| x.to_unextendable()));
let mut last_index = 0;
let mut last_hash = rolling_iter.next().unwrap();
let mut include_first = true;
for (index, min_hash) in rolling_iter.enumerate() {
if min_hash != last_hash {
let bucket =
H::get_bucket(last_hash) % (context.buckets_count as BucketIndexType);
tmp_reads_buffer.add_read(
KmersFlags(
include_first as u8,
CX::MinimizerBucketingSeqColorDataType::create(data.file_index),
),
&seq[(max(1, last_index) - 1)..(index + context.k)],
bucket,
);
last_index = index + 1;
last_hash = min_hash;
include_first = false;
}
}
let start_index = max(1, last_index) - 1;
let include_last = true; // Always include the last element of the sequence in the last entry
tmp_reads_buffer.add_read(
KmersFlags(
include_first as u8 | ((include_last as u8) << 1),
CX::MinimizerBucketingSeqColorDataType::create(data.file_index),
),
&seq[start_index..seq.len()],
H::get_bucket(last_hash) % (context.buckets_count as BucketIndexType),
);
});
if SEQ_COUNT.fetch_add(1, Ordering::Relaxed) % 100000000 == 0 {
TOT_BASES_COUNT.fetch_add(total_bases, Ordering::Relaxed);
VALID_BASES_COUNT.fetch_add(valid_bases, Ordering::Relaxed);
total_bases = 0;
valid_bases = 0;
println!(
"Elaborated {} sequences! [{} | {:.2}%] quality bases {}",
SEQ_COUNT.load(Ordering::Relaxed),
VALID_BASES_COUNT.load(Ordering::Relaxed),
(VALID_BASES_COUNT.load(Ordering::Relaxed) as f64)
/ (max(1, TOT_BASES_COUNT.load(Ordering::Relaxed)) as f64)
* 100.0,
PHASES_TIMES_MONITOR
.read()
.get_formatted_counter_without_memory()
);
}
}
TOT_BASES_COUNT.fetch_add(total_bases, Ordering::Relaxed);
VALID_BASES_COUNT.fetch_add(valid_bases, Ordering::Relaxed);
manager.return_obj(data);
}
tmp_reads_buffer.finalize();
}
fn reader<CX: ColorsManager>(
context: &ExecutionContext<CX>,
manager: ObjectsPoolManager<QueueData, (PathBuf, u64)>,
) {
while let Some((input, file_index)) = manager.recv_obj() {
let mut data = manager.allocate();
data.file_index = file_index;
SequencesReader::process_file_extended(
input,
|x| {
if x.seq.len() < context.k {
return;
}
if unsafe { unlikely(!data.push_sequences(x)) } {
let mut tmp_data = manager.allocate();
data.file_index = file_index;
swap(&mut data, &mut tmp_data);
manager.send(tmp_data);
if !data.push_sequences(x) {
panic!("Out of memory!");
}
}
},
false,
);
if data.sequences.len() > 0 {
manager.send(data);
}
}
}
impl Pipeline {
pub fn minimizer_bucketing<H: HashFunctionFactory, CX: ColorsManager>(
input_files: Vec<PathBuf>,
output_path: &Path,
buckets_count: usize,
threads_count: usize,
k: usize,
m: usize,
quality_threshold: Option<f64>,
) -> Vec<PathBuf> {
PHASES_TIMES_MONITOR
.write()
.start_phase("phase: reads bucketing".to_string());
let mut buckets = MultiThreadBuckets::<
IntermediateReadsWriter<KmersFlags<CX::MinimizerBucketingSeqColorDataType>>,
>::new(buckets_count, &output_path.join("bucket"), None);
let mut input_files: Vec<_> = input_files
.into_iter()
.enumerate()
.map(|(i, f)| (f, i as u64))
.collect();
input_files.sort_by_cached_key(|(file, _)| {
std::fs::metadata(file)
.expect(&format!("Error while opening file {}", file.display()))
.len()
});
input_files.reverse();
let execution_context = ExecutionContext {
k,
m,
buckets_count,
buckets,
quality_threshold,
};
ThreadPoolsChain::run_double(
input_files,
ThreadPoolDefinition::new(
&execution_context,
CHUNKS_SIZE,
String::from("minimizer-bucketing-reader"),
threads_count,
&AtomicUsize::new(threads_count),
WATERMARK_HIGH,
reader,
),
ThreadPoolDefinition::new(
&execution_context,
(),
String::from("minimizer-bucketing-writer"),
threads_count,
&AtomicUsize::new(threads_count),
WATERMARK_HIGH,
worker::<H, CX>,
),
);
execution_context.buckets.finalize()
}
}