bascet 0.4.0

Bascet is a tool to preprocess single-cell data, handling barcode detection, trimming, QC, and managing the execution of custom tools for each cell
Documentation
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
use fs2::FileExt;
use memmap2::MmapOptions;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::PathBuf;
use std::{
    cmp::min,
    fs::File,
    sync::Arc,
    usize,
};

use super::kmer_codec::KMERandCount;
use super::{BoundedHeap, BoundedMinHeap, KMERCodec};

use threadpool::ThreadPool;


// \t[0-9]{10}\n
// (4 294 967 296) is max value for kmer counts, thats 10 digits :)
pub const KMC_COUNTER_MAX_DIGITS: usize = 12;
pub const HUGE_PAGE_SIZE: usize = 2048 * 1024;






pub struct CountSketch {
    pub sketch: Vec<i64>,
    pub total: i64
}
impl CountSketch {
    pub fn new(size: usize) -> CountSketch {
        CountSketch {
            sketch: vec![0; size],
            total: 0
        }
    }

    // Return +1 for even numbers, and -1 for odd numbers. note that the type likely is important
    #[inline(always)]
    fn to_plusmin_one(kmer: u32) -> i32 {
        1 - ((kmer & 1) << 1) as i32
    }
    
    pub fn add(&mut self, kmer: KMERandCount) {

        // https://wangshusen.github.io/code/countsketch.html inspo
        // in R: https://www.rdocumentation.org/packages/aroma.light/versions/3.2.0/topics/wpca
        // https://docs.rs/streaming_algorithms/latest/streaming_algorithms/  mincounthash code
        // python: https://pdsa.readthedocs.io/en/latest/frequency/count_sketch.html
        // MurmurHash3

        let h = KMERCodec::hash_for_kmer(kmer.kmer);
        let g = KMERCodec::g_hash_for_kmer(kmer.kmer);

        let sgn = CountSketch::to_plusmin_one(g) as i64;  //using last bit of hash only. suspect this can be sped up
        //println!("{} {}", h, sgn);

        let pos = (h as usize) % self.sketch.len();
        self.sketch[pos] += sgn;

        self.total += 1;
    }

} 


pub struct KmerCounter {
    pub path_kmcdump: std::path::PathBuf,
    pub kmer_size: usize,
    pub features_nmin: usize,
}
impl KmerCounter {



    pub fn get_countsketch_fq(
        path_read_r1: std::path::PathBuf,
        path_read_r2: std::path::PathBuf,
        kmer_size: usize,
        sketch_size: usize,
        max_reads: usize
    ) -> anyhow::Result<CountSketch> {

        //Decide on KMER encoding
        let codec = KMERCodec::new(kmer_size);

        let mut sketch=CountSketch::new(sketch_size);

        //Process both R1 and R2
        KmerCounter::get_countsketch_fq_one(path_read_r1, &mut sketch, &codec, max_reads)?;
        KmerCounter::get_countsketch_fq_one(path_read_r2, &mut sketch, &codec, max_reads)?;

        //TODO We can speed up the process by directly getting the readpairs without writing to disk. this needs a new API

        Ok(sketch)
    }




    pub fn get_countsketch_fq_one(
        path_read: std::path::PathBuf,
        sketch: &mut CountSketch,
        codec: &KMERCodec,
        max_reads: usize
    ) -> anyhow::Result<()> {

        //Read FASTQ file
        let mut cur_reads = 0;
        let file = File::open(&path_read).unwrap();
        let reader = BufReader::new(file);
        let mut readit = reader.lines();
        while cur_reads < max_reads {
            if let Some(_line1) = readit.next() {
                let seq= readit.next().unwrap()?;
                let _line3= readit.next().unwrap()?;
                let _line4= readit.next().unwrap()?;

                for kmer in seq.as_bytes().windows(codec.kmer_size) {
                    let encoded = unsafe {
                        codec.encode(kmer, 1) ///////////////////////////////// TODO there is a big loop inside here to compress bytes using lookup. should get a kmer iterator instead
                    };    
                    sketch.add(encoded); //can reduce to one addition per read for speed
                }

            } else {
                break;
            }
            cur_reads += 1;
        }

        Ok(())
    }






    pub fn get_minhash_fq(
        path_read_r1: std::path::PathBuf,
        path_read_r2: std::path::PathBuf,
        kmer_size: usize,
        features_nmin: usize,
        max_reads: usize
    ) -> anyhow::Result<BoundedMinHeap<KMERandCount>> {

        let mut min_heap = BoundedMinHeap::with_capacity(features_nmin);

        //Decide on KMER encoding
        let codec = KMERCodec::new(kmer_size);

        //Process both R1 and R2
        KmerCounter::get_minhash_fq_one(path_read_r1, &mut min_heap, &codec, max_reads)?;
        KmerCounter::get_minhash_fq_one(path_read_r2, &mut min_heap, &codec, max_reads)?;

        //TODO We can speed up the process by directly getting the readpairs without writing to disk. this needs a new API

        Ok(min_heap)
    }

    pub fn get_minhash_fq_one(
        path_read: std::path::PathBuf,
        min_heap: &mut BoundedMinHeap<KMERandCount>,
        codec: &KMERCodec,
        max_reads: usize
    ) -> anyhow::Result<()> {

        //Read FASTQ file
        let mut cur_reads = 0;
        let file = File::open(&path_read).unwrap();
        let reader = BufReader::new(file);
        let mut readit = reader.lines();
        while cur_reads < max_reads {
            if let Some(_line1) = readit.next() {
                let seq= readit.next().unwrap()?;
                let _line3= readit.next().unwrap()?;
                let _line4= readit.next().unwrap()?;

                for kmer in seq.as_bytes().windows(codec.kmer_size) {
                    let encoded = unsafe {
                        codec.encode(kmer, 1) ///////////////////////////////// TODO there is a big loop inside here to compress bytes using lookup. should get a kmer iterator instead
                    };    
                    let _ = min_heap.push(encoded);
                }

            } else {
                break;
            }
            cur_reads += 1;
        }

        Ok(())
    }






    pub fn get_minhash_kmcdump_parallel(
        params: &KmerCounter,
        n_workers: usize
    ) -> anyhow::Result<BoundedMinHeap<KMERandCount>> {
        //Spinning up workers for every new file can be pricey... could put this in params or something, to hide it. future work

        let params= Arc::new(params);


        //Create all thread states
        let threads_buffer_size = (HUGE_PAGE_SIZE / n_workers) - (params.kmer_size + KMC_COUNTER_MAX_DIGITS);

        //Decide on KMER encoding
        let codec = KMERCodec::new(params.kmer_size);

        //Set up memory-mapped reading of file
        let file = File::open(&params.path_kmcdump).unwrap();
        let lock = file.lock_exclusive();
        let mmap = Arc::new(unsafe { MmapOptions::new().map(&file) }.unwrap());

        //Set up a channel to send regions for reading to worker threads
        let (tx, rx) = crossbeam::channel::bounded(n_workers*3);
        let (tx, rx) = (Arc::new(tx), Arc::new(rx));

        //Set up a channel to gather minheaps at end
        let (tx_minheap, rx_minheap) = crossbeam::channel::bounded(n_workers);
        let (tx_minheap, rx_minheap) = (Arc::new(tx_minheap), Arc::new(rx_minheap));
        
        //Start all workers
        let thread_pool = ThreadPool::new(n_workers);
        for _tidx in 0..n_workers {
            let rx = Arc::clone(&rx);
            let tx_minheap = Arc::clone(&tx_minheap);
            let mmap = Arc::clone(&mmap);
            let ovlp_size = params.kmer_size + KMC_COUNTER_MAX_DIGITS;
            let kmer_size = params.kmer_size;
            let features_nmin = params.features_nmin;
            thread_pool.execute(move || {
                let mut min_heap = BoundedMinHeap::with_capacity(features_nmin);
                while let Ok(Some((start, end))) = rx.recv() {
                    let chunk = &mmap[start..end];
                    process_chunk_to_minheap(
                        &chunk,
                        &mut min_heap,
                        codec,
                        kmer_size,
                        ovlp_size,
                    );
                }
                tx_minheap.send(Arc::new(min_heap)).unwrap();
            });
        }

        //In main thread, instruct workers where to read
        let overlap_window_size = params.kmer_size + KMC_COUNTER_MAX_DIGITS;
        let n_chunks = (mmap.len() + threads_buffer_size - 1) / threads_buffer_size;
        for i in 0..n_chunks {
            let raw_start = i * threads_buffer_size;
            let raw_end = min(
                raw_start + threads_buffer_size + overlap_window_size,
                mmap.len(),
            );
            let valid_start = find_chunk_start(&mmap[raw_start..], raw_start, overlap_window_size);
            let valid_end = find_chunk_end(&mmap[..raw_end], raw_end, overlap_window_size);
            tx.send(Some((valid_start, valid_end))).unwrap();
        }

        //Shut down all workers and wait for them to finish
        for _ in 0..n_workers {
            tx.send(None).unwrap();
        }
        thread_pool.join();

        //Merge all minheaps
        let mut final_min_heap = BoundedMinHeap::with_capacity(params.features_nmin);
        for _s in 0..n_workers {
            let mh = rx_minheap.recv().unwrap();
            for d in mh.iter() {
                _ = final_min_heap.push(d.clone());
            }
        }

        //Explicitly dropping file lock because i am paranoid it will not unlock otherwise
        drop(lock);

        Ok(final_min_heap)
    }






    pub fn detect_kmcdump_kmer_size(p: &PathBuf) -> anyhow::Result<usize> {
        let f = File::open(p).expect("Could not open file");
        let mut reader = BufReader::new(f);
        let mut buf = Vec::new();
        reader.read_until(b'\t', &mut buf).expect("Could not parse first KMER from KMC dump file"); 
        Ok(buf.len()-1)  // Subtract -1 because \t is included in the string
    }




    pub fn extract_kmcdump_single_thread (
        params: &KmerCounter
    ) -> anyhow::Result<BoundedMinHeap<KMERandCount>> {

        let mut min_heap = BoundedMinHeap::with_capacity(params.features_nmin);

        //Decide on KMER encoding
        let codec = KMERCodec::new(params.kmer_size);

        //Set up regular reading of file
        let file = File::open(&params.path_kmcdump).unwrap();
        let reader = BufReader::new(file);

        for line in reader.lines() {
            let line=line.unwrap();
            let mut splitter = line.split('\t');
            let kmer = splitter.next().unwrap();
            let count: u32 = splitter.next().unwrap().parse().unwrap();

            let encoded = unsafe {
                codec.encode(kmer.as_bytes(), count)
            };    
            let _ = min_heap.push(encoded);
        }

        Ok(min_heap)
    }





    
    pub fn store_countsketch_seq(
        _kmer_size: usize, 
        sketch: &CountSketch,
        p: &PathBuf
    ){
        //Open file for writing
        let f=File::create(p).expect("Could not open file for writing");
        let mut bw=BufWriter::new(f);

        //Write total number of counts
        writeln!(bw, "{}", sketch.total).unwrap();    

        //Write each sketch entry
        for e in &sketch.sketch {
            writeln!(bw, "{}", e).unwrap();    
        }
    }



    
    pub fn store_minhash_seq(
        kmer_size: usize, 
        minhash: &mut BoundedMinHeap<KMERandCount>,
        p: &PathBuf
    ){
        //Open file for writing
        let f=File::create(p).expect("Could not open file for writing");
        let mut bw=BufWriter::new(f);


        //Write just kmer sequences. First get them and presort them.
        //By sorting them, compression will be more efficient, and merging them across cells will be much faster
        let codec = KMERCodec::new(kmer_size);
        let mut list_string:Vec<String> = Vec::with_capacity(minhash.len());
        while let Some(h) = minhash.pop_min() {
            unsafe {
                let kmer_string = codec.decode(&h);
                list_string.push(kmer_string);
            }
        }

        //Sort and write them out
        list_string.sort();
        for kmer_string in list_string {
            writeln!(bw, "{}", &kmer_string).unwrap();    
        }

    }



    pub fn store_minhash_all(
        kmer_size: usize, 
        minhash: &mut BoundedMinHeap<KMERandCount>,
        p: &PathBuf
    ){
        //Open file for writing
        let f=File::create(p).expect("Could not open file for writing");
        let mut bw=BufWriter::new(f);


        //Write kmer & count. Hopefully this iterates from min to max
        let codec = KMERCodec::new(kmer_size);

        while let Some(h) = minhash.pop_min() {
            unsafe {
                let kmer_string = codec.decode(&h);
                writeln!(bw, "{}\t{}\t{}\t{}", &kmer_string, &h.count,   &h.kmer, &h.hash).unwrap();    
            }
        }
    }

}





#[inline(always)]
fn find_chunk_start(chunk: &[u8], raw_start: usize, ovlp_size: usize) -> usize {
    for i in 0..min(ovlp_size, chunk.len()) {
        if chunk[i] == b'\n' {
            return raw_start + i + 1;
        }
    }
    raw_start
}

#[inline(always)]
fn find_chunk_end(chunk: &[u8], raw_end: usize, ovlp_size: usize) -> usize {
    let window_size = min(ovlp_size, chunk.len());
    for i in (chunk.len() - window_size..chunk.len()).rev() {
        if chunk[i] == b'\n' {
            return min(i + 1, raw_end);
        }
    }
    raw_end
}



///////// Parse the count of kmers from KMC database. Counting has already been done
#[inline(always)]
unsafe fn parse_count_u32(bytes: &[u8]) -> u32 {
    // Fast path for single digit, most common case
    if bytes.len() == 1 {
        return (bytes[0] - b'0') as u32;
    }

    // Fast path for two digits, second most common case
    if bytes.len() == 2 {
        return ((bytes[0] - b'0') * 10 + (bytes[1] - b'0')) as u32;
    }

    // LUT for two-digit numbers
    const LOOKUP: [u32; 100] = {
        let mut table = [0u32; 100];
        let mut i = 0;
        while i < 100 {
            table[i] = (i / 10 * 10 + i % 10) as u32;
            i += 1;
        }
        table
    };

    let chunks = bytes.chunks_exact(2);
    let remainder = chunks.remainder();

    let mut result = 0u32;
    for chunk in chunks {
        let idx = ((chunk[0] - b'0') * 10 + (chunk[1] - b'0')) as usize;
        result = result.wrapping_mul(100) + LOOKUP[idx];
    }

    // Handle last digit if present
    if let Some(&d) = remainder.first() {
        result = result.wrapping_mul(10) + (d - b'0') as u32;
    }

    result
}


////////// For a chunk of data, extract KMERs
#[inline(always)]
fn process_chunk_to_minheap(
    chunk: &[u8],
    min_heap: &mut BoundedMinHeap<KMERandCount>,
    codec: KMERCodec,
    kmer_size: usize,
    ovlp_size: usize,
) {
    let chunk_length = chunk.len();
    let min_read_size = kmer_size + 2; // K + 2 is minimum size for a kmer + count (\t\d)
    let n_max_panes = chunk_length / min_read_size;
    let mut cursor = 0;

    for _ in 0..n_max_panes {
        if cursor >= chunk_length {
            break;
        }

        let pane_start = cursor;
        let remaining = chunk_length - pane_start;

        if remaining < min_read_size {
            break;
        }

        // Find the length of the current pane (up to next newline)
        let mut pane_length = min_read_size;
        for i in pane_length..min(ovlp_size, remaining) {
            if chunk[pane_start + i] == b'\n' {
                pane_length = i;
                break;
            }
        }

        // Extract and encode kmer with its count
        let kmer_end = pane_start + kmer_size;
        let count = unsafe { parse_count_u32(&chunk[kmer_end + 1..pane_start + pane_length]) };

        let encoded = unsafe {
            codec.encode(&chunk[pane_start..kmer_end], count)
        };

        let _ = min_heap.push(encoded);

        cursor += pane_length + 1; // +1 for newline
    }
}