rasusa 4.0.0

Randomly subsample reads or alignments
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
use crate::alignment::make_program_id_unique;
use crate::fastx::{Fastx, FastxError};
use anyhow::Result;
use noodles::sam::header::record::value::map::{program::tag, Program};
use noodles::sam::header::record::value::Map;
use noodles_util::alignment::io::Format;
use std::collections::{HashMap, HashSet};
use std::io::Write;
use std::path::{Path, PathBuf};

pub trait RecordSource {
    fn read_lengths(&self) -> Result<Vec<u32>, FastxError>;
    fn filter_reads_into(
        &self,
        reads_to_keep: &[bool],
        nb_reads_keep: usize,
        write_to: &mut dyn Write,
        output_format: Option<Format>,
        is_fasta: bool,
    ) -> Result<usize, FastxError>;
}

pub struct AlignmentSource {
    path: PathBuf,
}

impl AlignmentSource {
    pub fn new(path: &Path) -> Self {
        Self {
            path: path.to_path_buf(),
        }
    }

    fn program_entry(&self, header: &noodles::sam::Header) -> (String, Map<Program>) {
        let (program_id, previous_pgid) = make_program_id_unique(header, "rasusa");

        let mut record = Map::<Program>::builder();
        record = record.insert(tag::NAME, "rasusa");
        record = record.insert(tag::VERSION, env!("CARGO_PKG_VERSION"));

        let cl = std::env::args().collect::<Vec<String>>().join(" ");
        record = record.insert(tag::COMMAND_LINE, cl);

        if let Some(pp) = previous_pgid {
            record = record.insert(tag::PREVIOUS_PROGRAM_ID, pp);
        };

        let program = record.build().expect("Failed to build program record");
        (program_id.into_owned(), program)
    }
}

impl RecordSource for AlignmentSource {
    fn read_lengths(&self) -> Result<Vec<u32>, FastxError> {
        let mut reader = noodles_util::alignment::io::reader::Builder::default()
            .build_from_path(&self.path)
            .map_err(|source| FastxError::AlignmentReadError { source })?;

        let header = reader
            .read_header()
            .map_err(|source| FastxError::AlignmentReadError { source })?;

        let mut read_lengths: Vec<u32> = vec![];
        let mut qname_to_idx: HashMap<Vec<u8>, usize> = HashMap::new();

        for result in reader.records(&header) {
            let record = result.map_err(|source| FastxError::AlignmentReadError { source })?;
            let flags = record
                .flags()
                .unwrap_or(noodles::sam::alignment::record::Flags::empty());

            if !flags.is_unmapped() {
                return Err(FastxError::MappedReadDetected);
            }

            let rlen = record.sequence().len() as u32;

            if flags.is_segmented() {
                let name = record
                    .name()
                    .map(|n| (n.as_ref() as &[u8]).to_vec())
                    .unwrap_or_default();
                if let Some(&idx) = qname_to_idx.get(&name) {
                    read_lengths[idx] += rlen;
                } else {
                    qname_to_idx.insert(name, read_lengths.len());
                    read_lengths.push(rlen);
                }
            } else {
                read_lengths.push(rlen);
            }
        }

        Ok(read_lengths)
    }

    fn filter_reads_into(
        &self,
        reads_to_keep: &[bool],
        nb_reads_keep: usize,
        write_to: &mut dyn Write,
        output_format: Option<Format>,
        is_fasta: bool,
    ) -> Result<usize, FastxError> {
        let mut reader = noodles_util::alignment::io::reader::Builder::default()
            .build_from_path(&self.path)
            .map_err(|source| FastxError::AlignmentReadError { source })?;

        let mut header = reader
            .read_header()
            .map_err(|source| FastxError::AlignmentReadError { source })?;

        let mut read_idx: usize = 0;
        let mut total_len = 0;
        let mut written_templates: HashSet<usize> = HashSet::new();
        let mut qname_to_idx: HashMap<Vec<u8>, usize> = HashMap::new();

        // If the output format is an alignment format (SAM/BAM/CRAM), we write alignment records
        if let Some(format) = output_format {
            {
                // add rasusa program command line to header
                let (pg_id, pg_map) = self.program_entry(&header);
                header.programs_mut().as_mut().insert(pg_id.into(), pg_map);

                let mut writer = noodles_util::alignment::io::writer::Builder::default()
                    .set_format(format)
                    .build_from_writer(&mut *write_to)
                    .map_err(|source| FastxError::AlignmentReadError { source })?;

                writer
                    .write_header(&header)
                    .map_err(|source| FastxError::AlignmentReadError { source })?;

                for result in reader.records(&header) {
                    let record =
                        result.map_err(|source| FastxError::AlignmentReadError { source })?;
                    let flags = record
                        .flags()
                        .unwrap_or(noodles::sam::alignment::record::Flags::empty());

                    let current_idx = if flags.is_segmented() {
                        let name = record
                            .name()
                            .map(|n| (n.as_ref() as &[u8]).to_vec())
                            .unwrap_or_default();
                        if let Some(&idx) = qname_to_idx.get(&name) {
                            idx
                        } else {
                            let idx = read_idx;
                            qname_to_idx.insert(name, idx);
                            read_idx += 1;
                            idx
                        }
                    } else {
                        let idx = read_idx;
                        read_idx += 1;
                        idx
                    };

                    if current_idx < reads_to_keep.len() && reads_to_keep[current_idx] {
                        total_len += record.sequence().len();
                        writer
                            .write_record(&header, &record)
                            .map_err(|source| FastxError::AlignmentReadError { source })?;
                        written_templates.insert(current_idx);
                    }
                }
                writer
                    .finish(&header)
                    .map_err(|source| FastxError::AlignmentReadError { source })?;
            }
            write_to.flush().map_err(|source| FastxError::WriteError {
                source: anyhow::Error::from(source),
            })?;
        } else {
            // Otherwise, we output as FASTQ (or FASTA)
            for result in reader.records(&header) {
                let record = result.map_err(|source| FastxError::AlignmentReadError { source })?;
                let flags = record
                    .flags()
                    .unwrap_or(noodles::sam::alignment::record::Flags::empty());

                let current_idx = if flags.is_segmented() {
                    let name = record
                        .name()
                        .map(|n| (n.as_ref() as &[u8]).to_vec())
                        .unwrap_or_default();
                    if let Some(&idx) = qname_to_idx.get(&name) {
                        idx
                    } else {
                        let idx = read_idx;
                        qname_to_idx.insert(name, idx);
                        read_idx += 1;
                        idx
                    }
                } else {
                    let idx = read_idx;
                    read_idx += 1;
                    idx
                };

                if current_idx < reads_to_keep.len() && reads_to_keep[current_idx] {
                    total_len += record.sequence().len();

                    let name = record.name().map(|n| n.as_ref()).unwrap_or(&b"*"[..]);
                    let seq: Vec<u8> = record.sequence().iter().collect();
                    let qual: Vec<u8> = record
                        .quality_scores()
                        .iter()
                        .map(|q| q.map(|score| score + 33))
                        .collect::<Result<Vec<u8>, _>>()
                        .map_err(|source| FastxError::AlignmentReadError { source })?;

                    if qual.is_empty() || is_fasta {
                        // FASTA
                        write_to
                            .write_all(b">")
                            .map_err(|source| FastxError::WriteError {
                                source: anyhow::Error::from(source),
                            })?;
                        write_to
                            .write_all(name)
                            .map_err(|source| FastxError::WriteError {
                                source: anyhow::Error::from(source),
                            })?;
                        write_to
                            .write_all(b"\n")
                            .map_err(|source| FastxError::WriteError {
                                source: anyhow::Error::from(source),
                            })?;
                        write_to
                            .write_all(&seq)
                            .map_err(|source| FastxError::WriteError {
                                source: anyhow::Error::from(source),
                            })?;
                        write_to
                            .write_all(b"\n")
                            .map_err(|source| FastxError::WriteError {
                                source: anyhow::Error::from(source),
                            })?;
                    } else {
                        // FASTQ
                        write_to
                            .write_all(b"@")
                            .map_err(|source| FastxError::WriteError {
                                source: anyhow::Error::from(source),
                            })?;
                        write_to
                            .write_all(name)
                            .map_err(|source| FastxError::WriteError {
                                source: anyhow::Error::from(source),
                            })?;
                        write_to
                            .write_all(b"\n")
                            .map_err(|source| FastxError::WriteError {
                                source: anyhow::Error::from(source),
                            })?;
                        write_to
                            .write_all(&seq)
                            .map_err(|source| FastxError::WriteError {
                                source: anyhow::Error::from(source),
                            })?;
                        write_to
                            .write_all(b"\n+\n")
                            .map_err(|source| FastxError::WriteError {
                                source: anyhow::Error::from(source),
                            })?;
                        write_to
                            .write_all(&qual)
                            .map_err(|source| FastxError::WriteError {
                                source: anyhow::Error::from(source),
                            })?;
                        write_to
                            .write_all(b"\n")
                            .map_err(|source| FastxError::WriteError {
                                source: anyhow::Error::from(source),
                            })?;
                    }

                    written_templates.insert(current_idx);
                }
            }
        }

        if written_templates.len() == nb_reads_keep {
            Ok(total_len)
        } else {
            Err(FastxError::IndicesNotFound)
        }
    }
}

pub fn determine_record_source(path: &Path) -> Box<dyn RecordSource> {
    match path.extension().and_then(|ext| ext.to_str()) {
        Some("sam") | Some("bam") | Some("cram") => Box::new(AlignmentSource::new(path)),
        _ => Box::new(Fastx::from_path(path)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use noodles_util::alignment::io::Format;
    use std::fs::File;
    use std::path::Path;

    fn create_test_sam(path: &Path) {
        let mut file = File::create(path).unwrap();
        let content = b"@HD\tVN:1.6\tSO:coordinate\n\
                        @SQ\tSN:ref\tLN:1000\n\
                        r01\t4\tref\t10\t255\t10M\t*\t0\t0\tACTGACTGAW\t*\n\
                        r02\t4\tref\t20\t255\t10M\t*\t0\t0\tGCTGACTGAC\t*\n";
        file.write_all(content).unwrap();
    }

    fn create_test_paired_sam(path: &Path) {
        let mut file = File::create(path).unwrap();
        let content = b"@HD\tVN:1.6\tSO:coordinate\n\
                        @SQ\tSN:ref\tLN:1000\n\
                        r01\t77\tref\t10\t255\t10M\t=\t20\t20\tACTGACTGAC\t*\n\
                        r01\t141\tref\t20\t255\t10M\t=\t10\t-20\tGCTGACTGAC\t*\n\
                        r02\t77\tref\t30\t255\t10M\t=\t40\t20\tACTGACTGAC\t*\n\
                        r02\t141\tref\t40\t255\t10M\t=\t30\t-20\tGCTGACTGAC\t*\n";
        file.write_all(content).unwrap();
    }

    fn create_test_sam_with_quality(path: &Path) {
        let mut file = File::create(path).unwrap();
        let content = b"@HD\tVN:1.6\tSO:coordinate\n\
                        @SQ\tSN:ref\tLN:1000\n\
                        r01\t4\tref\t10\t255\t10M\t*\t0\t0\tACTGACTGAC\tIIIIIIIIII\n\
                        r02\t4\tref\t20\t255\t10M\t*\t0\t0\tGCTGACTGAC\tJJJJJJJJJJ\n";
        file.write_all(content).unwrap();
    }

    #[test]
    fn test_determine_record_source_alignment() {
        let path = Path::new("test.bam");
        let _source = determine_record_source(path);
    }

    #[test]
    fn test_alignment_source_read_lengths() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("test.sam");
        create_test_sam(&path);

        let source = AlignmentSource::new(&path);
        let actual = source.read_lengths().unwrap();
        assert_eq!(actual.len(), 2);
        assert_eq!(actual[0], 10);
        assert_eq!(actual[1], 10);
    }

    #[test]
    fn test_alignment_source_read_lengths_paired() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("test_paired.sam");
        create_test_paired_sam(&path);

        let source = AlignmentSource::new(&path);
        let actual = source.read_lengths().unwrap();
        // 2 templates, each length 10 + 10 = 20.
        assert_eq!(actual.len(), 2);
        assert_eq!(actual[0], 20);
        assert_eq!(actual[1], 20);
    }

    #[test]
    fn test_alignment_source_filter_reads_into_sam_to_bam() {
        let temp_dir = tempfile::tempdir().unwrap();
        let input_path = temp_dir.path().join("test.sam");
        create_test_sam(&input_path);

        let source = AlignmentSource::new(&input_path);
        let reads_to_keep = vec![true, false];
        let nb_reads_keep = 1;
        let mut buffer = Vec::new();

        let result = source.filter_reads_into(
            &reads_to_keep,
            nb_reads_keep,
            &mut buffer,
            Some(Format::Bam),
            false,
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 10);

        // Verify the output is a valid BAM and has 1 record
        let mut reader = noodles_util::alignment::io::reader::Builder::default()
            .build_from_reader(&buffer[..])
            .unwrap();
        let header = reader.read_header().unwrap();
        let mut count = 0;
        for result in reader.records(&header) {
            let record = result.unwrap();
            assert_eq!(record.name().map(|n| n.as_ref()), Some(&b"r01"[..]));
            count += 1;
        }
        assert_eq!(count, 1);
    }

    #[test]
    fn test_alignment_source_filter_reads_into_sam_to_fasta() {
        let temp_dir = tempfile::tempdir().unwrap();
        let input_path = temp_dir.path().join("test.sam");
        create_test_sam(&input_path);

        let source = AlignmentSource::new(&input_path);
        let reads_to_keep = vec![false, true];
        let nb_reads_keep = 1;
        let mut buffer = Vec::new();

        let result =
            source.filter_reads_into(&reads_to_keep, nb_reads_keep, &mut buffer, None, true);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 10);

        let output = String::from_utf8(buffer).unwrap();
        assert_eq!(output.lines().next().unwrap(), ">r02");
        assert_eq!(output.lines().count(), 2);
        assert!(output.contains("GCTGACTGAC"));
    }

    #[test]
    fn test_alignment_source_filter_reads_into_sam_to_fastq_with_quality() {
        let temp_dir = tempfile::tempdir().unwrap();
        let input_path = temp_dir.path().join("test_qual.sam");
        create_test_sam_with_quality(&input_path);

        let source = AlignmentSource::new(&input_path);
        let reads_to_keep = vec![false, true];
        let nb_reads_keep = 1;
        let mut buffer = Vec::new();

        let result =
            source.filter_reads_into(&reads_to_keep, nb_reads_keep, &mut buffer, None, false);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 10);

        let output = String::from_utf8(buffer).unwrap();
        let lines: Vec<&str> = output.lines().collect();
        assert_eq!(lines[0], "@r02");
        assert_eq!(lines[1], "GCTGACTGAC");
        assert_eq!(lines[2], "+");
        assert_eq!(lines[3], "JJJJJJJJJJ");
    }

    #[test]
    fn test_alignment_source_filter_reads_into_paired_sam_to_sam() {
        let temp_dir = tempfile::tempdir().unwrap();
        let input_path = temp_dir.path().join("test_paired.sam");
        create_test_paired_sam(&input_path);

        let source = AlignmentSource::new(&input_path);
        // 2 templates. Keep first.
        let reads_to_keep = vec![true, false];
        let nb_reads_keep = 1;
        let mut buffer = Vec::new();

        let result = source.filter_reads_into(
            &reads_to_keep,
            nb_reads_keep,
            &mut buffer,
            Some(Format::Sam),
            false,
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 20);

        // Verify the output has 2 records (1 pair)
        let mut reader = noodles_util::alignment::io::reader::Builder::default()
            .build_from_reader(&buffer[..])
            .unwrap();
        let header = reader.read_header().unwrap();
        let mut count = 0;
        for result in reader.records(&header) {
            let record = result.unwrap();
            assert_eq!(record.name().map(|n| n.as_ref()), Some(&b"r01"[..]));
            count += 1;
        }
        assert_eq!(count, 2);
    }

    #[test]
    fn test_alignment_source_filter_reads_into_bam_to_sam() {
        let temp_dir = tempfile::tempdir().unwrap();
        let sam_path = temp_dir.path().join("input.sam");
        create_test_sam_with_quality(&sam_path);

        // Convert SAM to BAM first
        let bam_path = temp_dir.path().join("input.bam");
        {
            let source = AlignmentSource::new(&sam_path);
            let mut bam_file = File::create(&bam_path).unwrap();
            source
                .filter_reads_into(&[true, true], 2, &mut bam_file, Some(Format::Bam), false)
                .unwrap();
        }

        let source = AlignmentSource::new(&bam_path);
        let reads_to_keep = vec![false, true];
        let nb_reads_keep = 1;
        let mut buffer = Vec::new();

        let result = source.filter_reads_into(
            &reads_to_keep,
            nb_reads_keep,
            &mut buffer,
            Some(Format::Sam),
            false,
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 10);

        let output = String::from_utf8(buffer).unwrap();
        assert!(output.contains("r02"));
        assert!(output.contains("GCTGACTGAC"));
        assert!(output.contains("JJJJJJJJJJ"));
    }
}