bsxplorer2 0.2.3

A high-performance library for bisulfite sequencing data analysis and DNA methylation research
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
use std::collections::{
    BTreeMap,
    BTreeSet,
    VecDeque,
};
use std::sync::Arc;

use anyhow::{
    anyhow,
    bail,
};
use itertools::Itertools;
use polars::error::PolarsResult;
use polars::prelude::search_sorted::binary_search_ca;
use polars::prelude::SearchSortedSide;

use super::BsxFileReader;
use crate::data_structs::batch::{
    BsxBatch,
    BsxBatchBuilder,
};
use crate::data_structs::coords::Contig;
use crate::io::bsx::BatchIndex;

/// RegionReader is a reader for BSX files that operates on a specific region of
/// the genome.
type PreprocessFn =
    Arc<dyn Fn(BsxBatch) -> anyhow::Result<BsxBatch> + Send + Sync + 'static>;

#[derive(Clone)]
pub struct RegionReader {
    /// Cache of encoded BSX batches.
    cache:         BTreeMap<usize, BsxBatch>,
    /// Inner reader for the BSX file.
    inner:         BsxFileReader,
    /// Index of the BSX file.
    index:         BatchIndex,
    /// Preprocessing function to be applied to each batch before it is cached.
    preprocess_fn: Option<PreprocessFn>,
}

// PRIVATE METHODS
impl RegionReader {
    /// Finds the batches that overlap the given contig.
    fn find(
        &self,
        contig: &Contig,
    ) -> Option<Vec<usize>> {
        let required_batches = self.index().find(&contig.clone());

        let batches_found = required_batches
            .as_ref()
            .map(|v| !v.is_empty())
            .unwrap_or(false);

        if batches_found {
            required_batches
        }
        else {
            None
        }
    }

    /// Updates the cache with the given batches.
    fn update_cache(
        &mut self,
        batches: &[usize],
    ) -> anyhow::Result<()> {
        let required_batches = BTreeSet::from_iter(batches.iter().cloned());
        let existing_batches = BTreeSet::from_iter(self.cache.keys().cloned());

        let min_required = required_batches.first().unwrap();

        let to_discard = existing_batches
            .difference(&required_batches)
            .cloned()
            .filter(|idx| idx < min_required)
            .collect_vec();
        let to_read = required_batches
            .difference(&existing_batches)
            .cloned()
            .collect_vec();

        for idx in to_discard {
            self.cache.remove(&idx);
        }

        let batches = self
            .inner
            .get_batches(&to_read)
            .into_iter()
            .collect::<Option<PolarsResult<Vec<_>>>>();
        if batches.is_none() {
            return Err(anyhow!("Some batches not found in file"));
        }
        let batches = batches.unwrap()?;

        for (idx, batch) in to_read.into_iter().zip(batches.into_iter()) {
            self.cache.insert(idx, batch);
        }

        Ok(())
    }

    /// Gets the batches for the given contig from the cache.
    fn get_batches_for_contig(
        &self,
        contig: &Contig,
    ) -> anyhow::Result<Vec<&BsxBatch>> {
        self.index()
            .find(&contig.clone())
            .unwrap()
            .into_iter()
            .sorted()
            .map(|idx| self.cache.get(&idx))
            .collect::<Option<Vec<&BsxBatch>>>()
            .ok_or(anyhow!("Batch data missing"))
    }

    /// Assembles the region from the given batches.
    ///
    /// This method assumes that the cache is up-to-date and that the required
    /// batches are present.
    fn assemble_region(
        &self,
        contig: &Contig,
        batches: Vec<&BsxBatch>,
    ) -> anyhow::Result<Option<BsxBatch>> {
        let batches_total = batches.len();

        let mut res = vec![];
        for (read_batches, batch) in batches.into_iter().enumerate() {
            let is_first = read_batches == 0;
            let is_last = read_batches == batches_total - 1;
            let slice_start = if is_first {
                binary_search_ca(
                    batch.position(),
                    [Some(contig.start())].into_iter(),
                    SearchSortedSide::Left,
                    false,
                )[0]
            }
            else {
                0
            };

            let slice_end = if is_last {
                binary_search_ca(
                    batch.position(),
                    [Some(contig.end())].into_iter(),
                    SearchSortedSide::Left,
                    false,
                )[0]
            }
            else {
                batch.len() as u32
            };

            let slice =
                batch.slice(slice_start as i64, (slice_end - slice_start) as usize);
            res.push(slice);
        }
        res.sort_by_key(|b| b.first_pos());
        if res.is_empty() {
            Ok(None)
        }
        else if res.len() == 1 {
            Ok(Some(res.pop().unwrap()))
        }
        else {
            Some(BsxBatchBuilder::concat(res))
                .transpose()
                .map_err(|e| anyhow::anyhow!(e))
        }
    }

    /// Determines the intersection kind between the cached region and the query
    /// contig.
    fn determine_intersection(
        &mut self,
        contig: &Contig,
    ) -> anyhow::Result<IntersectionKind> {
        let min_cached_pos = self
            .cache
            .first_key_value()
            .map(|(_k, v)| v.first_genomic_pos());
        let max_cached_pos = self
            .cache
            .last_key_value()
            .map(|(_k, v)| v.last_genomic_pos());

        if min_cached_pos.is_none() || max_cached_pos.is_none() {
            return Ok(IntersectionKind::None);
        }
        let min_cached_pos = min_cached_pos.unwrap();
        let max_cached_pos = max_cached_pos.unwrap();

        let intersection_kind =
            if let Some((min_pos, max_pos)) = min_cached_pos.zip(max_cached_pos) {
                assert_eq!(min_pos.seqname(), max_pos.seqname());
                let seqname = min_pos.seqname();
                let min_pos_val = min_pos.position();
                let max_pos_val = max_pos.position();

                // |-------------<cache>------------|
                //        |------<contig>----|
                if seqname == contig.seqname()
                    && contig.start() >= min_pos_val
                    && contig.end() <= max_pos_val
                {
                    IntersectionKind::Full
                }
                //        |-------------<cache>------------|
                // |--------<contig>----|
                // But this will qualify as PartialRight too, even though it
                // is not a partial intersection
                //        |-------------<cache>------------|
                // tig>-|
                else if min_pos.seqname() == contig.seqname()
                    && contig.start() <= min_pos_val
                    && contig.end() <= max_pos_val
                {
                    IntersectionKind::PartialLeft
                }
                // |-------------<cache>------------|
                //                  |----<region>------|
                else if min_pos.seqname() == contig.seqname()
                    && contig.start() > min_pos_val
                    && contig.start() < max_pos_val
                    && contig.end() >= max_pos_val
                // |----<cache>----|
                //                   |----<contig>-----|
                {
                    IntersectionKind::PartialRight
                }
                else {
                    IntersectionKind::None
                }
            }
            else {
                IntersectionKind::None
            };

        Ok(intersection_kind)
    }
}

// PUBLIC METHODS
impl RegionReader {
    /// Creates a new RegionReader from an existing BsxFileReader.
    ///
    /// Reads the index from the reader during initialization.
    pub fn from_reader(reader: BsxFileReader) -> anyhow::Result<Self> {
        let mut reader = reader;
        let index = BatchIndex::from_reader(&mut reader)?;
        Ok(Self::new(reader, index, None))
    }

    /// Sets the preprocessing function.
    pub fn set_preprocess_fn(
        &mut self,
        preprocess_fn: Option<PreprocessFn>,
    ) {
        self.preprocess_fn = preprocess_fn;
    }

    /// Creates a new RegionReader instance.
    ///
    /// # Arguments
    ///
    /// * `inner`: The inner BsxFileReader.
    /// * `index`: The BatchIndex for the BSX file.
    /// * `preprocess_fn`: An optional function to apply to each batch after
    ///   reading but before caching.
    pub fn new(
        inner: BsxFileReader,
        index: BatchIndex,
        preprocess_fn: Option<fn(BsxBatch) -> anyhow::Result<BsxBatch>>,
    ) -> Self {
        Self {
            cache: BTreeMap::new(),
            inner,
            index,
            preprocess_fn: preprocess_fn.map(|c| Arc::new(c) as PreprocessFn),
        }
    }

    /// Returns the index of the BSX file.
    pub fn index(&self) -> &BatchIndex {
        &self.index
    }

    /// Resets the cache.
    pub fn reset(&mut self) {
        self.cache.clear();
    }

    /// Creates an iterator over a list of contigs.
    ///
    /// # Arguments
    ///
    /// * `contigs`: A slice of Contig regions to iterate over.
    pub fn iter_contigs(
        &mut self,
        contigs: &[Contig],
    ) -> RegionReaderIterator {
        RegionReaderIterator {
            reader:          self,
            pending_contigs: VecDeque::from(contigs.to_vec()),
            cached_contigs:  VecDeque::new(),
        }
    }

    /// Queries the BSX file for the given contig region.
    ///
    /// Attempts to retrieve the data for the specified contig, potentially
    /// reading batches from the file and caching them.
    ///
    /// # Arguments
    ///
    /// * `contig`: The Contig region to query.
    /// * `postprocess_fn`: An optional function to apply to the assembled batch
    ///   after querying.
    pub fn query(
        &mut self,
        contig: Contig,
        postprocess_fn: Option<PreprocessFn>,
    ) -> anyhow::Result<Option<BsxBatch>> {
        if !self.index().get_chr_order().contains(contig.seqname()) {
            bail!("Contig seqname not found in index")
        }

        match self.determine_intersection(&contig)? {
            IntersectionKind::PartialLeft => {
                bail!(
                    "Required batch has already been processed. Make sure the regions \
                     are sorted with BatchIndex.sort"
                )
            },
            IntersectionKind::Full => {
                self.prepare_region(&contig, postprocess_fn.clone())
            },
            IntersectionKind::PartialRight | IntersectionKind::None => {
                if let Some(required_batches) = self.find(&contig) {
                    self.update_cache(&required_batches)?;
                    self.prepare_region(&contig, postprocess_fn.clone())
                }
                else {
                    Ok(None)
                }
            },
        }
    }

    fn prepare_region(
        &self,
        contig: &Contig,
        postprocess_fn: Option<PreprocessFn>,
    ) -> anyhow::Result<Option<BsxBatch>> {
        let batches = self.get_batches_for_contig(contig)?;
        let res = self.assemble_region(contig, batches)?;
        if let Some(data) = res {
            if let Some(postprocess_fn) = postprocess_fn {
                Some(postprocess_fn(data)).transpose()
            }
            else {
                Some(Ok(data)).transpose()
            }
        }
        else {
            Ok(None)
        }
    }
}

/// Iterator for reading BSX data for multiple contigs using a RegionReader.
pub struct RegionReaderIterator<'a> {
    reader:          &'a mut RegionReader,
    pending_contigs: VecDeque<Contig>,
    cached_contigs:  VecDeque<Contig>,
}

impl Iterator for RegionReaderIterator<'_> {
    type Item = anyhow::Result<BsxBatch>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(contig) = self.cached_contigs.pop_front() {
            self.reader.query(contig, None::<PreprocessFn>).transpose()
        }
        else {
            let fill_cache_res = self.fill_cache();
            if let Some(Ok(_)) = fill_cache_res {
                self.next()
            }
            else if let Some(Err(e)) = fill_cache_res {
                Some(Err(e))
            }
            else {
                None
            }
        }
    }
}

impl RegionReaderIterator<'_> {
    fn fill_cache(&mut self) -> Option<anyhow::Result<()>> {
        let mut required_batches = BTreeSet::new();
        let n_threads = self.reader.inner.n_threads();
        let mut cur_chr = None;

        while required_batches.len() < n_threads && !self.pending_contigs.is_empty() {
            let contig = self.pending_contigs.pop_front().unwrap();
            if cur_chr.is_some() && cur_chr.as_ref() != Some(contig.seqname()) {
                self.pending_contigs.push_front(contig);
                break;
            }
            else {
                cur_chr = Some(contig.seqname().clone());
            }

            if let Some(batches) = self.reader.find(&contig) {
                required_batches.append(&mut BTreeSet::from_iter(batches));
                self.cached_contigs.push_back(contig);
            }
            else {
                continue;
            };
        }

        if required_batches.is_empty() {
            None
        }
        else {
            let res = self
                .reader
                .update_cache(&required_batches.into_iter().collect_vec());
            Some(res)
        }
    }
}

/// Enum representing the intersection kind between the cached region and the
/// query contig.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum IntersectionKind {
    Full,
    PartialRight,
    PartialLeft,
    None,
}