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
use std::slice;
use std::iter;
use htslib;
use bam::record;
pub type Alignments<'a> = iter::Map<
slice::Iter<'a, htslib::bam_pileup1_t>,
fn(&'a htslib::bam_pileup1_t) -> Alignment<'a>
>;
pub struct Pileup {
inner: *const htslib::bam_pileup1_t,
depth: u32,
tid: u32,
pos: u32,
}
impl Pileup {
pub fn tid(&self) -> u32 {
self.tid
}
pub fn pos(&self) -> u32 {
self.pos
}
pub fn depth(&self) -> u32 {
self.depth
}
pub fn alignments(&self) -> Alignments {
self.inner().iter().map(Alignment::new)
}
fn inner(&self) -> &[htslib::bam_pileup1_t] {
unsafe { slice::from_raw_parts(self.inner as *mut htslib::bam_pileup1_t, self.depth as usize) }
}
}
pub struct Alignment<'a> {
inner: &'a htslib::bam_pileup1_t,
}
impl<'a> Alignment<'a> {
pub fn new(inner: &'a htslib::bam_pileup1_t) -> Self {
Alignment { inner: inner }
}
pub fn qpos(&self) -> usize {
self.inner.qpos as usize
}
pub fn indel(&self) -> Indel {
match self.inner.indel {
len if len < 0 => Indel::Del(-len as u32),
len if len > 0 => Indel::Ins(len as u32),
_ => Indel::None
}
}
pub fn record(&self) -> record::Record {
record::Record::from_inner(self.inner.b)
}
}
#[derive(PartialEq)]
#[derive(Debug)]
pub enum Indel {
Ins(u32),
Del(u32),
None
}
pub struct Pileups {
itr: htslib::bam_plp_t,
}
impl Pileups {
pub fn new(itr: htslib::bam_plp_t) -> Self {
Pileups { itr: itr }
}
pub fn set_max_depth(&mut self, depth: u32) {
unsafe { htslib::bam_plp_set_maxcnt(self.itr, depth as i32); }
}
}
impl Iterator for Pileups {
type Item = Result<Pileup, PileupError>;
fn next(&mut self) -> Option<Result<Pileup, PileupError>> {
let (mut tid, mut pos, mut depth) = (0i32, 0i32, 0i32);
let inner = unsafe {
htslib::bam_plp_auto(self.itr, &mut tid, &mut pos, &mut depth)
};
match inner.is_null() {
true if depth == -1 => Some(Err(PileupError::Some)),
true => None,
false => Some(Ok(
Pileup {
inner: inner,
depth: depth as u32,
tid: tid as u32,
pos: pos as u32,
}
))
}
}
}
impl Drop for Pileups {
fn drop(&mut self) {
unsafe {
htslib::bam_plp_reset(self.itr);
htslib::bam_plp_destroy(self.itr);
}
}
}
quick_error! {
#[derive(Debug)]
pub enum PileupError {
Some {
description("error generating pileup")
}
}
}