universalmotif 0.1.0

Sequence motif types, conversions and analysis, following the universalmotif R package
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
//! k-mers, k-mer counts and background frequencies of sequences.

use rayon::prelude::*;

use crate::error::{Result, arg_err};
use crate::pvalue::with_threads;
use crate::scan::reverse_complement_seq;

/// All k-mers of `letters`, in lexicographic order of the letters given, so the
/// first position varies slowest.
pub fn kmers(letters: &[char], k: usize) -> Vec<String> {
    let mut out = vec![String::new()];
    for _ in 0..k {
        out = out
            .iter()
            .flat_map(|p| letters.iter().map(move |l| format!("{p}{l}")))
            .collect();
    }
    out
}

/// Sorted, unique letters of some sequences.
#[doc(hidden)]
pub fn sequence_letters<S: AsRef<str>>(seqs: &[S]) -> Vec<char> {
    let mut ascii = [false; 128];
    let mut other = std::collections::BTreeSet::new();
    for s in seqs {
        let s = s.as_ref();
        if s.is_ascii() {
            s.bytes().for_each(|b| ascii[b as usize] = true);
        } else {
            for c in s.chars() {
                if c.is_ascii() {
                    ascii[c as usize] = true;
                } else {
                    other.insert(c);
                }
            }
        }
    }
    (0u8..128)
        .filter(|&b| ascii[b as usize])
        .map(char::from)
        .chain(other)
        .collect()
}

/// Counts in a sequence of each k-mer of `alphabet`, in [`kmers`] order. k-mers
/// containing letters outside the alphabet are not counted.
pub fn count_kmers(seq: &str, k: usize, alphabet: &[char]) -> Vec<u64> {
    let n = alphabet.len();
    let mut counts = vec![0u64; n.pow(k as u32)];
    if k == 0 {
        return counts;
    }
    // A rolling code of the last k letters; `run` counts the letters since
    // the last one outside the alphabet.
    let keep = n.pow(k as u32 - 1);
    let (mut code, mut run) = (0usize, 0usize);
    let mut feed = |idx: Option<usize>| match idx {
        None => run = 0,
        Some(i) => {
            code = (code % keep) * n + i;
            run += 1;
            if run >= k {
                counts[code] += 1;
            }
        }
    };
    if seq.is_ascii() && alphabet.iter().all(char::is_ascii) {
        let mut table = [usize::MAX; 128];
        for (i, &a) in alphabet.iter().enumerate() {
            table[a as usize] = i;
        }
        for b in seq.bytes() {
            let i = table[b as usize];
            feed((i != usize::MAX).then_some(i));
        }
    } else {
        for c in seq.chars() {
            feed(alphabet.iter().position(|&a| a == c));
        }
    }
    counts
}

/// Letters `start..end` of `s`, counting from 0. For non-ASCII strings,
/// `bounds` holds the byte offset of every letter and of the end.
fn letters<'a>(s: &'a str, bounds: Option<&[usize]>, start: usize, end: usize) -> &'a str {
    match bounds {
        None => &s[start..end],
        Some(b) => &s[b[start]..b[end]],
    }
}

/// Sliding windows over a sequence of length `len`, as `(start, end)` with
/// 0-based, half-open positions. The last window may be shorter than
/// `window`, unless `incomplete` is false. Returns no windows if `window`
/// exceeds `len` or `overlap` is not below `window`.
pub fn windows(len: usize, window: usize, overlap: usize, incomplete: bool) -> Vec<(usize, usize)> {
    if window > len || overlap >= window || window == 0 {
        return Vec::new();
    }
    let mut out = vec![(1, window)];
    loop {
        let (start, stop) = *out.last().unwrap();
        if start == len {
            break;
        }
        let next_start = start + window - overlap;
        let next_stop = next_start + window - 1;
        if next_stop > len {
            let next_start = next_start.min(len);
            if len != stop {
                out.push((next_start, len));
            }
            break;
        }
        out.push((next_start, next_stop));
    }
    if !incomplete {
        // Only a last window shorter than `window` is dropped.
        let (start, stop) = *out.last().unwrap();
        if stop - start + 1 < window {
            out.pop();
        }
    }
    out.into_iter().map(|(s, e)| (s - 1, e)).collect()
}

/// Windowing for [`get_bkg`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WindowOptions {
    /// Window width; values below 1 are a fraction of each sequence's length.
    pub size: f64,
    /// Window overlap; values between 0 and 1 are a fraction of each
    /// sequence's length.
    pub overlap: f64,
}

/// Options for [`get_bkg`].
#[derive(Debug, Clone, PartialEq)]
pub struct BkgOptions {
    /// k-mer sizes to count.
    pub k: Vec<usize>,
    /// Added to every count.
    pub pseudocount: u64,
    /// Letters to count; by default the letters found in the sequences.
    pub alphabet: Option<Vec<char>>,
    /// Also count the reverse complements, for DNA and RNA only.
    pub rc: bool,
    /// Combine all sequences into one set of counts.
    pub merge: bool,
    /// Count in sliding windows.
    pub window: Option<WindowOptions>,
    /// Threads to use; 0 means all available.
    pub nthreads: usize,
}

impl Default for BkgOptions {
    fn default() -> Self {
        BkgOptions {
            k: vec![1, 2, 3],
            pseudocount: 0,
            alphabet: None,
            rc: false,
            merge: true,
            window: None,
            nthreads: 1,
        }
    }
}

/// One row of [`get_bkg`] output. `sequence`, `start` and `end` are set only
/// for per-sequence or windowed counts.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct BkgRow {
    /// Index of the sequence, for per-sequence counts.
    pub sequence: Option<usize>,
    /// First position of the window, counting from 0, for windowed counts.
    pub start: Option<usize>,
    /// Position just past the window, for windowed counts.
    pub end: Option<usize>,
    /// The k-mer.
    pub kmer: String,
    /// The number of times it occurs.
    pub count: u64,
    /// Its frequency among the k-mers of the same size counted with it.
    pub probability: f64,
}

fn probabilities(counts: &[u64]) -> Vec<f64> {
    let total: u64 = counts.iter().sum();
    counts
        .iter()
        .map(|&c| {
            if total == 0 {
                0.0
            } else {
                c as f64 / total as f64
            }
        })
        .collect()
}

/// k-mer counts and frequencies of sequences.
///
/// Rows are ordered by k, k-mer and sequence for per-sequence counts; by k-mer
/// across all k and then window start for merged windows; and by sequence name,
/// k-mer and window start for per-sequence windows. Names and k-mers are
/// compared bytewise, and `names` gives the sequence names to order by.
pub fn get_bkg<S: AsRef<str> + Sync>(
    seqs: &[S],
    names: &[String],
    opts: &BkgOptions,
) -> Result<Vec<BkgRow>> {
    if seqs.is_empty() {
        return arg_err("no sequences given");
    }
    if opts.k.is_empty() || opts.k.contains(&0) {
        return arg_err("k must be at least 1");
    }
    let mut ks = opts.k.clone();
    ks.sort_unstable();
    ks.dedup();
    // Borrowed unless reverse complements are added below.
    let mut seqs: Vec<std::borrow::Cow<str>> = seqs.iter().map(|s| s.as_ref().into()).collect();
    let alphabet = match &opts.alphabet {
        Some(a) => {
            let mut a = a.clone();
            a.sort_unstable();
            a.dedup();
            a
        }
        None => sequence_letters(&seqs),
    };
    if opts.rc {
        let rna = alphabet.contains(&'U');
        let rcs: Vec<String> = seqs
            .iter()
            .map(|s| reverse_complement_seq(s, rna))
            .collect();
        seqs.extend(rcs.into_iter().map(std::borrow::Cow::Owned));
    }
    let names: Vec<String> = if names.len() == seqs.len() {
        names.to_vec()
    } else {
        let base: Vec<String> = if names.is_empty() {
            (1..=seqs.len()).map(|i| i.to_string()).collect()
        } else {
            names.to_vec()
        };
        (0..seqs.len())
            .map(|i| base[i % base.len()].clone())
            .collect()
    };
    let kmax = *ks.last().unwrap();
    let min_len = seqs.iter().map(|s| s.chars().count()).min().unwrap();
    if min_len < kmax {
        return arg_err("k must not be greater than the length of the shortest sequence");
    }

    let Some(win) = opts.window else {
        let mut rows = Vec::new();
        for &k in &ks {
            let kl = kmers(&alphabet, k);
            let per_seq: Vec<Vec<u64>> = with_threads(opts.nthreads, || {
                seqs.par_iter()
                    .map(|s| count_kmers(s, k, &alphabet))
                    .collect()
            })?;
            if opts.merge {
                let counts: Vec<u64> = (0..kl.len())
                    .map(|j| per_seq.iter().map(|c| c[j]).sum::<u64>() + opts.pseudocount)
                    .collect();
                let probs = probabilities(&counts);
                for (j, kmer) in kl.into_iter().enumerate() {
                    rows.push(BkgRow {
                        sequence: None,
                        start: None,
                        end: None,
                        kmer,
                        count: counts[j],
                        probability: probs[j],
                    });
                }
            } else {
                let counts: Vec<Vec<u64>> = per_seq
                    .iter()
                    .map(|c| c.iter().map(|x| x + opts.pseudocount).collect())
                    .collect();
                let probs: Vec<Vec<f64>> = counts.iter().map(|c| probabilities(c)).collect();
                for (j, kmer) in kl.iter().enumerate() {
                    for (si, c) in counts.iter().enumerate() {
                        rows.push(BkgRow {
                            sequence: Some(si),
                            start: None,
                            end: None,
                            kmer: kmer.clone(),
                            count: c[j],
                            probability: probs[si][j],
                        });
                    }
                }
            }
        }
        return Ok(rows);
    };

    // Windowed counts.
    let mut wins: Vec<Vec<(usize, usize)>> = Vec::with_capacity(seqs.len());
    for s in &seqs {
        let len = s.chars().count();
        let size = if win.size < 1.0 {
            (win.size * len as f64) as usize
        } else {
            win.size as usize
        };
        let overlap = if win.overlap > 0.0 && win.overlap < 1.0 {
            (win.overlap * len as f64) as usize
        } else {
            win.overlap as usize
        };
        if size == 0 {
            return arg_err("the window size is too small for these sequences (resolved to 0)");
        }
        if size <= overlap {
            return arg_err("the window overlap must be smaller than the window size");
        }
        if size < kmax {
            return arg_err("the window size must be at least k");
        }
        let w = windows(len, size, overlap, true);
        if w.is_empty() {
            return arg_err("incorrect window and/or overlap size");
        }
        wins.push(w);
    }
    if opts.merge && seqs.iter().any(|s| s.chars().count() != min_len) {
        return arg_err("merged windowed counts need all sequences to be of equal length");
    }
    let bounds: Vec<Option<Vec<usize>>> = seqs
        .iter()
        .map(|s| {
            (!s.is_ascii()).then(|| {
                s.char_indices()
                    .map(|(i, _)| i)
                    .chain(std::iter::once(s.len()))
                    .collect()
            })
        })
        .collect();
    if wins
        .iter()
        .flatten()
        .any(|&(start, end)| end - start < kmax)
    {
        return arg_err("k must not be greater than the smallest window");
    }

    let mut rows = Vec::new();
    for &k in &ks {
        let kl = kmers(&alphabet, k);
        // counts[seq][window][kmer]
        let counts: Vec<Vec<Vec<u64>>> = with_threads(opts.nthreads, || {
            seqs.par_iter()
                .zip(&bounds)
                .zip(&wins)
                .map(|((s, b), ws)| {
                    ws.iter()
                        .map(|&(start, end)| {
                            count_kmers(letters(s, b.as_deref(), start, end), k, &alphabet)
                        })
                        .collect()
                })
                .collect()
        })?;
        if opts.merge {
            let ws = &wins[0];
            let summed: Vec<Vec<u64>> = (0..ws.len())
                .map(|w| {
                    (0..kl.len())
                        .map(|j| counts.iter().map(|c| c[w][j]).sum::<u64>() + opts.pseudocount)
                        .collect()
                })
                .collect();
            let probs: Vec<Vec<f64>> = summed.iter().map(|c| probabilities(c)).collect();
            for (j, kmer) in kl.iter().enumerate() {
                for (w, &(start, end)) in ws.iter().enumerate() {
                    rows.push(BkgRow {
                        sequence: None,
                        start: Some(start),
                        end: Some(end),
                        kmer: kmer.clone(),
                        count: summed[w][j],
                        probability: probs[w][j],
                    });
                }
            }
        } else {
            let mut order: Vec<usize> = (0..seqs.len()).collect();
            order.sort_by(|&a, &b| names[a].as_bytes().cmp(names[b].as_bytes()));
            for &si in &order {
                let c: Vec<Vec<u64>> = counts[si]
                    .iter()
                    .map(|w| w.iter().map(|x| x + opts.pseudocount).collect())
                    .collect();
                let probs: Vec<Vec<f64>> = c.iter().map(|w| probabilities(w)).collect();
                for (j, kmer) in kl.iter().enumerate() {
                    for (w, &(start, end)) in wins[si].iter().enumerate() {
                        rows.push(BkgRow {
                            sequence: Some(si),
                            start: Some(start),
                            end: Some(end),
                            kmer: kmer.clone(),
                            count: c[w][j],
                            probability: probs[w][j],
                        });
                    }
                }
            }
        }
    }
    if opts.merge {
        // Merged windows are ordered by k-mer only, across all k.
        rows.sort_by(|a, b| a.kmer.as_bytes().cmp(b.kmer.as_bytes()));
    } else {
        rows.sort_by(|a, b| {
            names[a.sequence.unwrap()]
                .as_bytes()
                .cmp(names[b.sequence.unwrap()].as_bytes())
                .then(a.kmer.as_bytes().cmp(b.kmer.as_bytes()))
        });
    }
    Ok(rows)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn kmers_counted_for_any_letters() {
        let ascii = count_kmers("AACGNAAC", 2, &['A', 'C', 'G']);
        // AA, AC, CG, then N breaks the run, then AA, AC.
        assert_eq!(ascii, [2, 2, 0, 0, 0, 1, 0, 0, 0]);
        let greek = count_kmers("αβαNββ", 2, &['α', 'β']);
        assert_eq!(greek, [0, 1, 1, 1]);
        let bounds: Vec<usize> = "αβγ".char_indices().map(|(i, _)| i).chain([6]).collect();
        assert_eq!(letters("αβγ", Some(&bounds), 1, 3), "βγ");
    }
}