d4tools 0.3.9

The CLI utils for D4 file format
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
use clap::{load_yaml, App, ArgMatches};
use d4::ptab::PTablePartitionWriter;
use d4::stab::SecondaryTablePartWriter;
use d4::{Chrom, D4FileBuilder, D4FileWriter, Dictionary};
use d4_hts::{BamFile, DepthIter, Alignment};
use d4tools::{make_dictionary, parse_bed_file, parse_genome_file, setup_thread_pool, InputType};
use log::{info, warn};
use rayon::prelude::*;
use regex::Regex;
use std::path::{Path, PathBuf};

type DynErr = Box<dyn std::error::Error>;

struct CreateAppCtx {
    input_path: PathBuf,
    input_type: InputType,
    min_mq: u8,
    bam_flags: Option<u16>,
    inclusive_flag: u16,
    exclusive_flag: u16,
    chr_filter: Regex,
    compression: bool,
    denominator: Option<f64>,
    compression_level: u32,
    builder: D4FileBuilder,
}

#[derive(Clone, Copy)]
struct BamFilter {
    min_mq: u8,
    bam_flags: Option<u16>,
    inclusive_flag: u16,
    exclusive_flag: u16,
}

impl BamFilter {
    fn filter_alignment(&self, read: &Alignment) -> bool {
        let quality = read.map_qual() >= self.min_mq;
        let flag = read.flag();
        let exact_match = self.bam_flags.map_or(true, |expected| expected == flag);
        let inclusive_match = (self.inclusive_flag & flag) == self.inclusive_flag;
        let exclusive_match = (self.exclusive_flag & flag) == 0;
        quality && exact_match && inclusive_match && exclusive_match
    }
}

impl CreateAppCtx {
    fn get_bam_filter(&self) -> BamFilter {
        BamFilter { min_mq: self.min_mq, bam_flags: self.bam_flags, inclusive_flag: self.inclusive_flag, exclusive_flag: self.exclusive_flag }
    }
    fn new(matches: &ArgMatches) -> Result<Self, DynErr> {
        let input_path: &Path = matches.value_of("input-file").unwrap().as_ref();
        let input_type = InputType::detect(input_path);

        let min_mq = matches.value_of("min-mqual").map_or(60, |v| {
            v.parse().expect("Invalid minimal mapping quality option")
        });

        let mut bam_flags = None;
        let mut inclusive_flag = 0;
        let mut exclusive_flag = 0;

        if let Some(bam_flag_expr) = matches.value_of("bam-flag") {
            for fragment in bam_flag_expr.split(',') {
                let (opcode, val_str) = match fragment.chars().next() {
                    Some('+') => ('+', &fragment[1..]),
                    Some('-') => ('-', &fragment[1..]),
                    Some('~') => ('~', &fragment[1..]),
                    _ => ('=', fragment),
                };
                let value:u16 = val_str.parse().expect("Invalid BAM flag");
                match opcode {
                    '+' => inclusive_flag |= value,
                    '-' | '~' => exclusive_flag |= value,
                    '=' => bam_flags = Some(value),
                    _ => unreachable!(),
                }
            }
        }
        
        let output_path = matches.value_of("output-file").map_or_else(
            || {
                let mut ret = input_path.to_owned();
                ret.set_extension("d4");
                ret
            },
            |path| path.into(),
        );
        let denominator: Option<f64> = matches
            .value_of("denominator")
            .map(|what| what.parse().unwrap());
        let mut builder = d4::D4FileBuilder::new(output_path);

        let chr_filter = Regex::new(matches.value_of("filter").unwrap_or(".*"))?;

        builder.set_filter(move |chr, _size| chr_filter.is_match(chr));

        let compression = matches.is_present("deflate") || matches.is_present("sparse");
        let compression_level: u32 = matches.value_of("deflate-level").unwrap_or("5").parse()?;

        Ok(Self {
            input_path: input_path.to_owned(),
            input_type,
            min_mq,
            bam_flags,
            inclusive_flag,
            exclusive_flag,
            chr_filter: Regex::new(matches.value_of("filter").unwrap_or(".*"))?,
            compression,
            compression_level,
            denominator,
            builder,
        })
    }
    fn auto_dict_for_bam(&mut self, matches: &ArgMatches) -> Result<(), DynErr> {
        let filter = self.get_bam_filter();
        let dict = Dictionary::from_sample_bam(
            self.input_path.as_path(),
            |chr, _size| self.chr_filter.is_match(chr),
            matches.value_of("ref"),
            move |r| filter.filter_alignment(r),
        )?;
        self.builder.set_dictionary(dict);
        Ok(())
    }
    fn auto_dict_for_bw(&mut self) -> Result<(), DynErr> {
        let fp = std::fs::metadata(self.input_path.as_path())?;
        let bw_file = d4_bigwig::BigWigFile::open(self.input_path.as_path())?;

        let genome_size: u64 = bw_file.chroms().into_iter().map(|(_, sz)| sz as u64).sum();

        let file_size = fp.len();

        if file_size < genome_size / 8 {
            self.builder
                .set_dictionary(Dictionary::new_simple_range_dict(0, 1)?);
            self.compression = true;
        }

        Ok(())
    }
    fn auto_dict_for_bedgraph(&mut self, matches: &ArgMatches) -> Result<(), DynErr> {
        let genomes = parse_genome_file(
            matches
                .value_of("genome")
                .expect("Genome file is required for text file format"),
        )?;
        let genome_size: u64 = genomes.into_iter().map(|chr| chr.size as u64).sum();

        let fp = std::fs::metadata(self.input_path.as_path())?;
        let file_size = fp.len();

        if file_size < genome_size {
            self.builder
                .set_dictionary(Dictionary::new_simple_range_dict(0, 1)?);
            self.compression = true;
        }

        Ok(())
    }
    fn configure_dict(&mut self, matches: &ArgMatches) -> Result<(), DynErr> {
        self.builder.set_dictionary(make_dictionary(
            matches.value_of("dict-range"),
            matches.value_of("dict-file"),
        )?);

        if matches.is_present("sparse") {
            self.builder
                .set_dictionary(d4::Dictionary::new_simple_range_dict(0, 1)?);
        }

        let auto_dict_detection = (!matches.is_present("dict_range")
            && !matches.is_present("dict-file"))
            || matches.is_present("dict-auto");

        if auto_dict_detection {
            match self.input_type {
                InputType::Alignment => self.auto_dict_for_bam(matches)?,
                InputType::BiwWig => self.auto_dict_for_bw()?,
                InputType::BedGraph => self.auto_dict_for_bedgraph(matches)?,
                _ => {
                    panic!("Unsupported input type")
                }
            }
        }

        Ok(())
    }

    fn detect_default_denominator_for_bigwig(
        &mut self,
        matches: &ArgMatches,
    ) -> Result<(), DynErr> {
        let auto_dict_detection = (!matches.is_present("dict_range")
            && !matches.is_present("dict-file"))
            || matches.is_present("dict-auto");

        let bw_file = d4_bigwig::BigWigFile::open(self.input_path.as_path())?;
        let mut purposed_denominator = 1.0f64;
        let mut num_of_intervals = 0;
        let mut genome_size = 0;
        for (chr_name, chr_size) in bw_file.chroms() {
            genome_size += chr_size;
            if let Some(result) = bw_file.query_range(&chr_name, 0, chr_size as u32) {
                for bw_interval in result {
                    let value = bw_interval.value as f64;
                    if value.abs() < 1e-10 {
                        continue;
                    }
                    num_of_intervals += 1;

                    let mut denominator = 1.0;

                    while ((value * denominator).round() - (value * denominator)).abs() > 1e-10 {
                        denominator *= 10.0;
                    }

                    purposed_denominator = purposed_denominator.max(denominator);
                }
            }
        }
        if auto_dict_detection && num_of_intervals * 10 < genome_size * 6 {
            self.builder
                .set_dictionary(Dictionary::new_simple_range_dict(0, 1)?);
            self.compression = true;
        }
        if purposed_denominator != 1.0 {
            self.builder.set_denominator(purposed_denominator);
            self.denominator = Some(purposed_denominator);
        }
        Ok(())
    }

    fn detect_default_denominator_for_bedgraph(&mut self) -> Result<(), DynErr> {
        let input = parse_bed_file(self.input_path.as_path())?;
        let mut purposed_denominator = 1.0f64;

        for (_, _, _, value) in input {
            if value.abs() < 1e-10 {
                continue;
            }
            let mut denominator = 1.0;

            while ((value * denominator).round() - (value * denominator)).abs() > 1e-10 {
                denominator *= 10.0;
            }

            purposed_denominator = purposed_denominator.max(denominator);
        }

        if purposed_denominator != 1.0 {
            self.builder.set_denominator(purposed_denominator);
            self.denominator = Some(purposed_denominator);
        }
        Ok(())
    }

    fn determine_default_denominator(&mut self, matches: &ArgMatches) -> Result<(), DynErr> {
        if self.denominator.is_some() {
            return Ok(());
        }

        match self.input_type {
            InputType::BiwWig => self.detect_default_denominator_for_bigwig(matches)?,
            InputType::BedGraph => self.detect_default_denominator_for_bedgraph()?,
            _ => (),
        }

        Ok(())
    }
    
    fn create_from_alignment(mut self, matches: &ArgMatches) -> Result<(), DynErr> {
        let reference = matches.value_of("ref");

        self.builder.load_chrom_info_from_bam(&self.input_path)?;
        let mut d4_writer: D4FileWriter = self.builder.create()?;

        if self.compression {
            d4_writer.enable_secondary_table_compression(self.compression_level);
        }

        let partitions = d4_writer.parallel_parts(Some(10_000_000))?;

        info!("Total number of parallel tasks: {}", partitions.len());
        
        let bam_filter = self.get_bam_filter();

        partitions
            .into_par_iter()
            .for_each(|(mut p_table, mut s_table)| {
                let (chr, from, to) = p_table.region();
                let chr = chr.to_owned();
                let mut alignment = BamFile::open(&self.input_path).unwrap();
                if let Some(reference) = reference {
                    alignment.reference_path(reference);
                }
                let al_from = from - from.min(5000);
                let time_begin = std::time::SystemTime::now();
                info!("Task begin: {}:{}-{}", chr, from, to);
                let range_iter = alignment
                    .range(&chr, al_from as usize, to as usize)
                    .unwrap();
                let mut p_encoder = p_table.make_encoder();
                let mut last_pos = 0;
                for (_, pos, depth) in DepthIter::with_filter(range_iter, |r| bam_filter.filter_alignment(r)) {
                    let depth = if let Some(denominator) = self.denominator {
                        (depth as f64 * denominator).round() as u32
                    } else {
                        depth
                    };

                    last_pos = pos;
                    if pos < from as usize {
                        continue;
                    }
                    if pos as u32 >= to {
                        break;
                    }
                    if !p_encoder.encode(pos, depth as i32) {
                        s_table.encode(pos as u32, depth as i32).unwrap();
                    }
                }
                for pos in last_pos.max(from as usize)..to as usize {
                    if !p_encoder.encode(pos, 0) {
                        s_table.encode(pos as u32, 0).unwrap();
                    }
                }
                s_table.flush().unwrap();
                s_table.finish().unwrap();
                let time_end = std::time::SystemTime::now();
                let duration = time_end.duration_since(time_begin).unwrap_or_default();
                info!(
                    "Task completed: {}:{}-{} Duration: {}ms",
                    chr,
                    from,
                    to,
                    duration.as_millis()
                );
            });
        Ok(())
    }

    fn create_from_bigwig(mut self) -> Result<(), DynErr> {
        let bw_file = d4_bigwig::BigWigFile::open(&self.input_path)?;
        self.builder.append_chrom(
            bw_file
                .chroms()
                .into_iter()
                .map(|(name, size)| Chrom { name, size }),
        );
        let mut d4_writer: D4FileWriter = self.builder.create()?;
        if self.compression {
            d4_writer.enable_secondary_table_compression(self.compression_level);
        }
        let partition = d4_writer.parallel_parts(None)?;
        for (mut pt, mut st) in partition {
            let (chrom, left, right) = pt.region();
            let chrom = chrom.to_string();
            let mut last = left;
            let mut p_encoder = pt.make_encoder();

            let mut write_value = |pos: u32, value: i32| {
                if !p_encoder.encode(pos as usize, value) {
                    st.encode(pos as u32, value).unwrap();
                }
            };
            if let Some(iter) = bw_file.query_range(&chrom, left, right) {
                for d4_bigwig::BigWigInterval {
                    begin: left,
                    end: right,
                    value,
                } in iter
                {
                    let value = if let Some(denominator) = self.denominator {
                        ((value as f64) * denominator).round() as f32
                    } else {
                        value
                    };

                    for pos in last..left {
                        write_value(pos, 0);
                    }
                    for pos in left..right {
                        write_value(pos, value as i32);
                    }
                    last = right;
                }
            }
            for pos in last..right {
                write_value(pos, 0);
            }
        }
        Ok(())
    }
    fn create_from_bedgraph(mut self, matches: &ArgMatches) -> Result<(), DynErr> {
        self.builder.append_chrom(
            parse_genome_file(
                matches
                    .value_of("genome")
                    .expect("Genome file is required for text file format"),
            )?
            .into_iter(),
        );
        let default_pt_value = if self.builder.dictionary().bit_width() == 0 {
            Some(self.builder.dictionary().first_value())
        } else {
            None
        };
        let mut d4_writer: D4FileWriter = self.builder.create()?;
        if self.compression {
            d4_writer.enable_secondary_table_compression(self.compression_level);
        }
        let mut partition = d4_writer.parallel_parts(None)?;
        let input = parse_bed_file(&self.input_path)?;
        let mut current = 0;
        for (chr, from, to, depth) in input {
            let depth = if let Some(denominator) = self.denominator {
                ((depth as f64) * denominator).round() as i32
            } else {
                if depth - depth.floor() > 1e-10 {
                    warn!("Encoding a decimal valued input to a integer D4, fix-point mode is recommended");
                }
                depth as i32
            };

            if let Some(default) = default_pt_value {
                if default == depth {
                    // In this case, we have a zero sized primary table and the vlaue we need to encode is just that value
                    continue;
                } else {
                    let mut from = from;
                    while from < to {
                        let mut region = partition[current].0.region();
                        if region.0 != chr || region.1 < from || region.2 >= from {
                            if let Some((idx, _)) = (0..).zip(partition.iter()).find(|(_, part)| {
                                let reg = part.0.region();
                                reg.0 == chr && reg.1 <= from && from < reg.2
                            }) {
                                current = idx;
                                region = partition[current].0.region();
                            } else {
                                continue;
                            }
                        }
                        let record_from = from;
                        let record_to = region.2.min(to);
                        partition[current]
                            .1
                            .encode_record(record_from, record_to, depth)?;
                        from = record_to;
                    }
                }
            } else {
                for pos in from..to {
                    let region = partition[current].0.region();
                    if region.0 != chr || region.1 < pos || region.2 >= pos {
                        if let Some((idx, _)) = (0..).zip(partition.iter()).find(|(_, part)| {
                            let reg = part.0.region();
                            reg.0 == chr && reg.1 <= pos && pos < reg.2
                        }) {
                            current = idx;
                        } else {
                            continue;
                        }
                    }
                    let mut encoder = partition[current].0.make_encoder();
                    if !encoder.encode(pos as usize, depth) {
                        partition[current].1.encode(pos, depth)?;
                    }
                }
            }
            partition[current].1.flush()?;
        }
        for (_, mut stab) in partition {
            stab.finish()?;
        }
        Ok(())
    }
}

fn main_impl(matches: ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
    setup_thread_pool(&matches)?;

    let mut ctx = CreateAppCtx::new(&matches)?;

    ctx.configure_dict(&matches)?;

    if matches.values_of("dump-dict").is_some() {
        println!("{}", ctx.builder.dictionary().pretty_print()?);
        std::process::exit(0);
    }

    ctx.determine_default_denominator(&matches)?;

    match ctx.input_type {
        InputType::Alignment => ctx.create_from_alignment(&matches)?,
        InputType::BiwWig => ctx.create_from_bigwig()?,
        InputType::BedGraph => ctx.create_from_bedgraph(&matches)?,
        _ => panic!("Unsupported input file format"),
    }
    Ok(())
}

pub fn entry_point(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
    let yaml = load_yaml!("cli.yml");
    let matches = App::from_yaml(yaml)
        .version(d4tools::VERSION)
        .get_matches_from(args);

    main_impl(matches)
}