twitcher 0.6.9

Find template switch mutations in genomic data
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
use std::{
    fs::File,
    path::{Path, PathBuf},
    time::Duration,
};

use anyhow::{Context, bail};
use bstr::ByteSlice;
use itertools::Itertools;
use rust_htslib::{
    bam,
    bcf::{
        self, HeaderRecord, Read as _,
        header::{HeaderView, TagLength, TagType},
    },
};
use tracing::{debug, instrument, warn};
use url::Url;

use crate::{
    RunnableCommand,
    common::{
        aligner::RUNNING,
        csv::TwitcherCSVWriter,
        list_of_regions::{Regions, RegionsDefinition, Targets},
        reference::ReferenceReader,
    },
    vcf::{
        cli::{Command, OutputOptions},
        pipeline::{
            VCFPipeline,
            reader::VCFReader,
            writer::{OutputWriter, region_writer::RegionWriter},
        },
    },
};

pub mod cli;
pub mod pipeline;
pub mod strings;

impl RunnableCommand for Command {
    #[instrument(name = "vcf", skip_all)]
    async fn run(self) -> anyhow::Result<()> {
        tokio::spawn(async {
            let mut interval = tokio::time::interval(Duration::from_secs(1));
            loop {
                interval.tick().await;
                let r = RUNNING.load(std::sync::atomic::Ordering::Relaxed);
                debug!("Running tasks: {r}");
            }
        });
        self.validate()?;
        let regions = self.regions.read_regions().await?;
        let input = tokio::task::block_in_place(move || open_input_file(&self.input, regions))?;
        warn_on_unwritable_format_fields(input.header());
        let mut header = bcf::Header::from_template(input.header());
        augment_header(&mut header, self.no_version);
        let reference = tokio::task::block_in_place(|| ReferenceReader::try_from(&self.reference))?;
        let targets = self.targets.read_regions().await?.map(Targets::new);
        let region_output = RegionWriter::from_parameter(self.ts_regions.as_deref()).await?;
        let output = tokio::task::block_in_place(move || open_output_file(&self.output, &header))?;
        let csv_output = if let Some(f) = self.csv_output {
            Some(TwitcherCSVWriter::new(Box::new(File::create(f)?)))
        } else {
            None
        };
        let bam = open_phasing_bam_file(self.phasing_bam.as_deref(), &self.reference.file)?;
        VCFPipeline {
            input,
            output,
            reference,
            targets,
            region_output,
            csv_output,
            settings: self.pipeline_settings,
            bam,
            phasing: self.phasing,
        }
        .run()
        .await?;

        Ok(())
    }
}

impl Command {
    fn validate(&self) -> anyhow::Result<()> {
        if self.output.output_file.as_ref().is_none_or(|o| o == "-")
            && self.ts_regions.as_ref().is_some_and(|o| o == "-")
        {
            bail!("Cannot output both vcf and regions output on stdout.");
        }
        self.pipeline_settings.cluster.cluster_strategy.validate()?;
        Ok(())
    }
}

fn augment_header(header: &mut bcf::Header, no_version: bool) {
    #[allow(clippy::wildcard_imports)]
    use strings::*;
    // Eventually, what we add in this method will likely depend on the settings etc ...
    header.push_record(
        format!(
            "##INFO=<ID={VCF_TS_CIGARETS_KEY},Number=1,Type=String,Description=\"{VCF_TS_CIGARETS_DESC}\">"
        )
        .as_bytes(),
    );
    header.push_record(
        format!(
            "##INFO=<ID={VCF_TS_COST_KEY},Number=1,Type=Integer,Description=\"{VCF_TS_COST_DESC}\">"
        )
        .as_bytes(),
    );
    header.push_record(
        format!(
            "##INFO=<ID={VCF_TS_INNER_LEN_KEY},Number=.,Type=Integer,Description=\"{VCF_TS_INNER_LEN_DESC}\">"
        )
        .as_bytes(),
    );
    header.push_record(
        format!(
            "##INFO=<ID={VCF_TS_JUMP_KEY},Number=.,Type=Integer,Description=\"{VCF_TS_JUMP_DESC}\">"
        )
        .as_bytes(),
    );
    header.push_record(
        format!(
            "##INFO=<ID={VCF_TS_ID_KEY},Number=1,Type=String,Description=\"{VCF_TS_ID_DESC}\">"
        )
        .as_bytes(),
    );
    header.push_record(
        format!(
            "##INFO=<ID={VCF_CLUSTER_GRP_KEY},Number=1,Type=String,Description=\"{VCF_CLUSTER_GRP_DESC}\">"
        )
        .as_bytes(),
    );
    header.push_record(
        format!(
            "##INFO=<ID={VCF_TWITCHER_PHASE_KEY},Number=0,Type=Flag,Description=\"{VCF_TWITCHER_PHASE_DESC}\">"
        )
        .as_bytes(),
    );
    header.push_record(VCF_FORMAT_GT_LINE);
    header.push_record(VCF_FORMAT_PS_LINE);
    if !no_version {
        header.push_record(
            format!("##{VCF_HEADER_TWITCHER_VERSION_KEY}={TWITCHER_VERSION}").as_bytes(),
        );
        header.push_record(
            format!("##{VCF_HEADER_TWITCHER_CMD}={}", std::env::args().join(" ")).as_bytes(),
        );
    }
}

/// Warn about FORMAT fields that this program writes itself but that the input already
/// declares with an incompatible type.
///
/// The output header is duplicated from the input one, and htslib keeps the first declaration
/// of an ID, so the declaration [`augment_header`] appends is ignored in that case and the
/// field is silently left off the output records. Say so once, at startup.
fn warn_on_unwritable_format_fields(header: &HeaderView) {
    for (tag, expected) in [
        (&b"GT"[..], (TagType::String, TagLength::Fixed(1))),
        (&b"PS"[..], (TagType::Integer, TagLength::Fixed(1))),
    ] {
        if let Ok(found) = header.format_type(tag)
            && found != expected
        {
            warn!(
                "Input header declares FORMAT/{} as {found:?}, but this program writes {expected:?}. \
                 The field will not be written.",
                tag.as_bstr()
            );
        }
    }
}

fn open_input_file(input: &str, regions: Option<Regions>) -> anyhow::Result<VCFReader> {
    let reader = if let Some(regions) = regions {
        let inner = match input {
            "-" => bail!("Cannot create indexed reader from stdin"),
            path_or_url => {
                if let Ok(url) = Url::parse(path_or_url) {
                    bcf::IndexedReader::from_url(&url)
                } else {
                    bcf::IndexedReader::from_path(path_or_url)
                }
            }
        }?;

        let regions =
            regions.into_linear(|chr| inner.header().name2rid(chr.as_ref()).unwrap_or(0) as usize);
        VCFReader::with_regions(inner, regions)
    } else {
        let inner = match input {
            "-" => bcf::Reader::from_stdin(),
            path_or_url => {
                if let Ok(url) = Url::parse(path_or_url) {
                    bcf::Reader::from_url(&url)
                } else {
                    bcf::Reader::from_path(path_or_url)
                }
            }
        }?;
        VCFReader::entire_file(inner)
    };

    let sample_count = reader.header().sample_count();
    if sample_count > 1 {
        let first_sample = reader
            .header()
            .samples()
            .first()
            .context("could not get sample name")?
            .as_bstr();
        warn!(
            "The input file has {sample_count} samples, but twitcher only supports one sample per file. All but the first sample ({first_sample}) will be ignored."
        );
    }
    if sample_count == 0 {
        bail!(
            "There needs to be a sample present in the input VCF file. Sites-only VCFs are currently not supported."
        );
    }

    validate_version(get_vcf_version(reader.header())?)?;

    Ok(reader)
}

fn validate_version((major, minor): (u8, u8)) -> anyhow::Result<()> {
    if major != 4 || (!(2..5).contains(&minor)) {
        bail!("VCF version unsupported. Twitcher only handles v4.2 <= version <= v4.5");
    }
    Ok(())
}

fn get_vcf_version(header: &HeaderView) -> anyhow::Result<(u8, u8)> {
    let records = header.header_records();
    let first = records
        .first()
        .with_context(|| "Header does not have at least one record.")?;
    if let HeaderRecord::Generic { key, value } = first
        && key == "fileformat"
        && value.starts_with("VCFv")
    {
        let version = value
            .get("VCFv".len()..)
            .context("version string not long enough")?;
        let Some((major, minor)) = version.split_once('.') else {
            anyhow::bail!("no dot separating major and minor version");
        };
        return Ok((major.parse()?, minor.parse()?));
    }
    bail!("The first header record of the VCF input file must be the VCF version.");
}

fn open_phasing_bam_file<P: AsRef<Path>>(
    arg: Option<&str>,
    reference_path: P,
) -> anyhow::Result<Option<bam::IndexedReader>> {
    let Some(path) = arg else {
        return Ok(None);
    };

    let mut reader = if let Ok(url) = Url::parse(path) {
        bam::IndexedReader::from_url(&url)?
    } else {
        bam::IndexedReader::from_path(path)?
    };

    reader.set_reference(reference_path)?;

    Ok(Some(reader))
}

fn open_output_file(output: &OutputOptions, header: &bcf::Header) -> anyhow::Result<OutputWriter> {
    let inner = match output.output_file.as_deref() {
        None | Some("-") => bcf::Writer::from_stdout(header, true, bcf::Format::Vcf),
        Some(path_or_url) => {
            let url = Url::parse(path_or_url);
            let path = PathBuf::from(match &url {
                Ok(url) => url.path(),
                _ => path_or_url,
            });
            let compressed = matches!(
                path.extension().and_then(|e| e.to_str()),
                Some("gz" | "bgz")
            );
            let format = if path
                .file_name()
                .is_none_or(|n| n.to_str().is_some_and(|s| s.contains(".vcf")))
            {
                bcf::Format::Vcf
            } else {
                bcf::Format::Bcf
            };
            match url {
                Ok(url) => bcf::Writer::from_url(&url, header, !compressed, format),
                Err(_) => bcf::Writer::from_path(path, header, !compressed, format),
            }
        }
    }?;

    let writer = if let Some(plus_minus) = output.output_filter.realigned_and_context {
        OutputWriter::new_buffered(inner, i64::try_from(plus_minus)?)
    } else {
        OutputWriter::new_native(inner, output.output_filter.only_realigned)
    };
    Ok(writer)
}

#[cfg(test)]
mod tests {
    use std::io::Read as _;

    use rust_htslib::{bcf::Header, bgzf};

    use crate::common::reference::CliReferenceArg;

    use super::*;

    #[test]
    fn validate_rejects_both_outputs_on_stdout() {
        let cmd = Command {
            output: None::<&str>.into(), // stdout (default)
            ts_regions: Some("-".into()),
            ..Default::default()
        };
        assert!(cmd.validate().is_err());
    }

    #[test]
    fn validate_allows_vcf_on_file_regions_on_stdout() {
        let cmd = Command {
            output: Some("/tmp/out.vcf").into(),
            ts_regions: Some("-".into()),
            ..Default::default()
        };
        assert!(cmd.validate().is_ok());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn test_pipeline() {
        let dir = tempfile::tempdir().unwrap();
        let outpath = format!("{}/out.vcf", dir.path().to_str().unwrap());
        let cmd = Command {
            input: "./test_files/test.vcf".to_string(),
            reference: CliReferenceArg::from("./test_files/test.fa"),
            output: Some(outpath.clone()).into(),
            ..Default::default()
        };
        cmd.run().await.unwrap();
        let output = std::fs::read_to_string(outpath).unwrap();
        let out_records = output.lines().filter(|l| !l.starts_with('#'));
        assert_eq!(out_records.count(), 2);
    }

    #[test]
    fn test_auto_detect_output_format_vcf() {
        let reader = open_input_file("./test_files/test.vcf", None).unwrap();
        let header = Header::from_template(reader.header());
        let dir = tempfile::tempdir().unwrap();
        let outpath_vcf = format!("{}/out.vcf", dir.path().to_str().unwrap());
        let writer = open_output_file(&Some(&outpath_vcf).into(), &header).unwrap();
        drop(writer);

        let written_file = std::fs::read_to_string(outpath_vcf).unwrap();
        assert!(written_file.starts_with("##fileformat=VCFv4"));
    }

    #[test]
    fn test_auto_detect_output_format_vcf_gz() {
        let reader = open_input_file("./test_files/test.vcf", None).unwrap();
        let header = Header::from_template(reader.header());
        let dir = tempfile::tempdir().unwrap();
        let outpath_vcf_gz = format!("{}/out.vcf.gz", dir.path().to_str().unwrap());
        let writer = open_output_file(&Some(&outpath_vcf_gz).into(), &header).unwrap();
        drop(writer);

        let mut written_file = Vec::new();
        bgzf::Reader::from_path(outpath_vcf_gz)
            .unwrap()
            .read_to_end(&mut written_file)
            .unwrap();
        assert!(written_file.starts_with(b"##fileformat=VCFv4"));
    }

    #[test]
    fn test_auto_detect_output_format_bcf() {
        let reader = open_input_file("./test_files/test.vcf", None).unwrap();
        let header = Header::from_template(reader.header());
        let dir = tempfile::tempdir().unwrap();
        let outpath_bcf = format!("{}/out.bcf", dir.path().to_str().unwrap());
        let writer = open_output_file(&Some(&outpath_bcf).into(), &header).unwrap();
        drop(writer);

        let mut written_file = Vec::new();
        bgzf::Reader::from_path(outpath_bcf)
            .unwrap()
            .read_to_end(&mut written_file)
            .unwrap();
        assert!(written_file.starts_with(b"BCF"));
    }

    #[test]
    fn test_auto_detect_output_format_bcf_gz() {
        // .bcf.gz should be the same as bcf
        let reader = open_input_file("./test_files/test.vcf", None).unwrap();
        let header = Header::from_template(reader.header());
        let dir = tempfile::tempdir().unwrap();
        let outpath_bcf_gz = format!("{}/out.bcf.gz", dir.path().to_str().unwrap());
        let writer = open_output_file(&Some(&outpath_bcf_gz).into(), &header).unwrap();
        drop(writer);

        let mut written_file = Vec::new();
        bgzf::Reader::from_path(outpath_bcf_gz)
            .unwrap()
            .read_to_end(&mut written_file)
            .unwrap();
        assert!(written_file.starts_with(b"BCF"));
    }
}