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
use crate::{
    gfa::{Line, Link, Path, Segment},
    parser::GFAParser,
};

use anyhow::{bail, Result};

use memmap::Mmap;

use std::fs::File;
use std::io::prelude::*;

use bstr::ByteSlice;

#[derive(Debug)]
pub struct MmapGFA {
    pub cursor: std::io::Cursor<Mmap>,
    pub line_buf: Vec<u8>,
    pub current_line_len: usize,
    pub last_buf_offset: usize,
    pub parser: GFAParser<usize, ()>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LineType {
    Segment,
    Link,
    Path,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LineIndices {
    pub segments: Vec<(usize, usize)>,
    pub links: Vec<usize>,
    pub paths: Vec<usize>,
}

#[derive(Debug)]
pub struct SegmentIter<'a> {
    mmap: &'a mut MmapGFA,
    parser: GFAParser<usize, ()>,
}

impl<'a> Iterator for SegmentIter<'a> {
    type Item = Segment<usize, ()>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        while let Ok(line) = self.mmap.next_line() {
            if let Some(b'S') = line.first() {
                if let Some(Line::Segment(s)) =
                    self.parser.parse_gfa_line(line).ok()
                {
                    return Some(s);
                }
            }
        }
        None
    }
}

#[derive(Debug)]
pub struct LinkIter<'a> {
    mmap: &'a mut MmapGFA,
    parser: GFAParser<usize, ()>,
}

impl<'a> Iterator for LinkIter<'a> {
    type Item = Link<usize, ()>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        while let Ok(line) = self.mmap.next_line() {
            if let Some(b'S') = line.first() {
                if let Some(Line::Link(s)) =
                    self.parser.parse_gfa_line(line).ok()
                {
                    return Some(s);
                }
            }
        }
        None
    }
}

#[derive(Debug)]
pub struct PathIter<'a> {
    mmap: &'a mut MmapGFA,
    parser: GFAParser<usize, ()>,
}

impl<'a> Iterator for PathIter<'a> {
    type Item = Path<usize, ()>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        while let Ok(line) = self.mmap.next_line() {
            if let Some(b'S') = line.first() {
                if let Some(Line::Path(s)) =
                    self.parser.parse_gfa_line(line).ok()
                {
                    return Some(s);
                }
            }
        }
        None
    }
}

impl MmapGFA {
    pub fn new(path: &str) -> Result<Self> {
        let file = File::open(path)?;
        let mmap = unsafe { Mmap::map(&file)? };

        let cursor = std::io::Cursor::new(mmap);
        let line_buf = Vec::with_capacity(1024);
        let current_line_len = 0;
        let last_buf_offset = 0;

        let parser = GFAParser::new();

        Ok(Self {
            cursor,
            line_buf,
            current_line_len,
            last_buf_offset,
            parser,
        })
    }

    pub fn reset_position(&mut self) -> u64 {
        let cur_pos = self.cursor.position();
        self.cursor.set_position(0);
        cur_pos
    }

    pub fn set_position(&mut self, new_pos: u64) -> u64 {
        let cur_pos = self.cursor.position();
        self.cursor.set_position(new_pos);
        cur_pos
    }
    pub fn get_ref(&self) -> &[u8] {
        self.cursor.get_ref().as_ref()
    }

    pub fn get_parser(&self) -> &GFAParser<usize, ()> {
        &self.parser
    }

    pub fn next_line(&mut self) -> Result<&[u8]> {
        self.line_buf.clear();

        self.last_buf_offset = self.cursor.position() as usize;

        let n_read = self.cursor.read_until(b'\n', &mut self.line_buf)?;

        self.current_line_len = n_read;

        Ok(&self.line_buf[..n_read])
    }

    pub fn read_line_at(&mut self, offset: usize) -> Result<&[u8]> {
        self.cursor.set_position(offset as u64);
        self.next_line()
    }

    pub fn build_index(&mut self) -> Result<LineIndices> {
        let start_position = self.cursor.position();
        let current_line_len = self.current_line_len;
        let last_buf_offset = self.last_buf_offset;

        let mut segments = Vec::new();
        let mut links = Vec::new();
        let mut paths = Vec::new();

        self.cursor.set_position(0);

        let mut line_start = 0;

        loop {
            let line = self.next_line()?;
            let length = line.len();

            if let Some(ref byte) = line.first() {
                match byte {
                    b'S' => {
                        segments.push((line_start, length));
                    }
                    b'L' => {
                        links.push(line_start);
                    }
                    b'P' => {
                        paths.push(line_start);
                    }
                    _ => (),
                };

                line_start += line.len();
            } else {
                break;
            }
        }

        self.cursor.set_position(start_position);
        self.current_line_len = current_line_len;
        self.last_buf_offset = last_buf_offset;

        let res = LineIndices {
            segments,
            links,
            paths,
        };

        Ok(res)
    }

    pub fn current_line(&self) -> &[u8] {
        &self.line_buf[..self.current_line_len]
    }

    pub fn current_line_name(&self) -> Option<&[u8]> {
        let mut iter = self.line_buf.split_str("\t");
        let _lt = iter.next()?;
        let name = iter.next()?;
        Some(name)
    }

    pub fn parse_current_line(&self) -> Result<Line<usize, ()>> {
        let line = self.current_line();
        if line.is_empty() {
            bail!("Line at offset {} is empty", self.last_buf_offset);
        }

        let gfa_line = self.parser.parse_gfa_line(line)?;
        Ok(gfa_line)
    }

    pub fn iter_segments(&mut self, from_start: bool) -> SegmentIter<'_> {
        if from_start {
            self.cursor.set_position(0);
        }
        let parser = self.parser.clone();
        SegmentIter { mmap: self, parser }
    }

    pub fn iter_links(&mut self, from_start: bool) -> LinkIter<'_> {
        if from_start {
            self.cursor.set_position(0);
        }
        let parser = self.parser.clone();
        LinkIter { mmap: self, parser }
    }

    pub fn iter_paths(&mut self, from_start: bool) -> PathIter<'_> {
        if from_start {
            self.cursor.set_position(0);
        }
        let parser = self.parser.clone();
        PathIter { mmap: self, parser }
    }
}