Generates the plot data for a given region of a bam file
(
bam_path: P,
ref_path: P,
region: &Region,
max_read_depth: usize,
aux_tags: Option<Vec<String>>,
mismatch_display_min_percent: f64,
)
| 23 | |
| 24 | /// Generates the plot data for a given region of a bam file |
| 25 | pub(crate) fn create_plot_data<P: AsRef<Path> + std::fmt::Debug>( |
| 26 | bam_path: P, |
| 27 | ref_path: P, |
| 28 | region: &Region, |
| 29 | max_read_depth: usize, |
| 30 | aux_tags: Option<Vec<String>>, |
| 31 | mismatch_display_min_percent: f64, |
| 32 | ) -> Result<(Vec<EncodedRead>, Reference, usize, Coverage, usize)> { |
| 33 | let mut bam = bam::IndexedReader::from_path(&bam_path)?; |
| 34 | let tid = bam |
| 35 | .header() |
| 36 | .tid(region.target.as_bytes()) |
| 37 | .context(format!( |
| 38 | "bam header does not contain given region target {}", |
| 39 | ®ion.target |
| 40 | )) |
| 41 | .unwrap() as i32; |
| 42 | bam.fetch(FetchRegion(tid, region.start, region.end))?; |
| 43 | let mut data = bam |
| 44 | .records() |
| 45 | .filter_map(|r| r.ok()) |
| 46 | .filter_map(|r| { |
| 47 | Read::from_record(r, &ref_path, ®ion.target, &aux_tags) |
| 48 | .context(format!( |
| 49 | "bam file does not contain given region target {}", |
| 50 | ®ion.target |
| 51 | )) |
| 52 | .unwrap() |
| 53 | }) |
| 54 | .collect_vec(); |
| 55 | let coverage = Coverage::from_reads(&data, region, mismatch_display_min_percent); |
| 56 | let total_read_count = data.len(); |
| 57 | data.order(max_read_depth)?; |
| 58 | let retained_reads = data.len(); |
| 59 | let reference_data = Reference { |
| 60 | start: region.start, |
| 61 | reference: read_fasta(ref_path, region)?.iter().collect(), |
| 62 | }; |
| 63 | Ok(( |
| 64 | vec![EncodedRead::from_reads(data)], |
| 65 | reference_data, |
| 66 | total_read_count, |
| 67 | coverage, |
| 68 | retained_reads, |
| 69 | )) |
| 70 | } |
| 71 | |
| 72 | /// Reads the given region from the given fasta file and returns it as a vec of the bases as chars |
| 73 | fn read_fasta<P: AsRef<Path> + std::fmt::Debug>(path: P, region: &Region) -> Result<Vec<char>> { |