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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! Read 2bit files in Rust.
//!
//! This crate is inspired by <a href="https://github.com/deeptools/py2bit">py2bit</a> and tries to
//! offer the same functionality. It is written from the ground up with no C-dependency. It follows
//! the <a href="http://genome.ucsc.edu/FAQ/FAQformat.html#format7">2bit specification version
//! 0</a>.
//!
//! Note that most methods perform IO operations and return a `Result` for that reason.
//!
//! The main entry point for users of this crate is the [TwoBitFile](struct.TwoBitFile.html)
//! struct. Please see its documentation for details how to use this crate.
//!

pub mod block;
pub mod counts;
pub mod error;
pub mod types;
mod value_reader;

use std::collections::HashMap;
use std::default::Default;
use std::fs::metadata;
use std::io::SeekFrom;
use std::path::{Path, PathBuf};

use crate::block::Block;
use crate::counts::{BaseCounts, BasePercentages};
use crate::error::Error;
use crate::types::{Field, FileIndex};
use crate::value_reader::ValueReader;

/// 2bit signature magic number
const SIGNATURE: Field = 0x1A412743;

/// 2bit signature magic number reversed
const REV_SIGNATURE: Field = 0x4327411A;

/// Read data from a 2bit file
///
/// Usage:
///
/// ```
/// extern crate twobit;
/// use twobit::TwoBitFile;
///
/// let enable_softmask=true; //set to false to enforce upper case sequences
/// let tb = TwoBitFile::open("assets/foo.2bit", enable_softmask).unwrap();
/// let chromosome_lengths = tb.chroms();
/// assert_eq!(chromosome_lengths["chr1"], 150);
/// assert_eq!(chromosome_lengths["chr2"], 100);
/// ```
///
/// 2bit files offer two types of masks: N masks (aka hard masks) for unknown or arbitrary nucleotides
/// and soft masks for lower-case nucleotides (e.g. "t" instead of "T").
///
/// ```
/// # extern crate twobit;
/// # use twobit::TwoBitFile;
/// # let enable_softmask=true; //set to false to enforce upper case sequences
/// # let tb = TwoBitFile::open("assets/foo.2bit", enable_softmask).unwrap();
///
/// let expected_seq = "NNNNNNNNNNNNNNNNNNNNNNNNNNACGTACGTACGTagctagctGATC"; // some lower and some upper case
/// assert_eq!(tb.sequence("chr1",24,74).unwrap(), expected_seq);
/// ```
/// It is not possible to disable hard masks but you can disable soft masks:
/// ```
/// # extern crate twobit;
/// # use twobit::TwoBitFile;
///
/// let enable_softmask=false;
/// let tb_nosoft = TwoBitFile::open("assets/foo.2bit", enable_softmask).unwrap();
/// let expected_seq = "NNNNNNNNNNNNNNNNNNNNNNNNNNACGTACGTACGTAGCTAGCTGATC"; // all upper case
/// assert_eq!(tb_nosoft.sequence("chr1",24,74).unwrap(), expected_seq);
/// ```
///
/// There are 2 variants for every sequence-related method:
/// * Those that request information on parts of a chromosome
/// * Those that request information on a complete chromosome
///
/// The partial requests require you to provide a start (0-based, inclusive) and a stop (0-based,
/// exclusive) position as parameters. The full request methods all start with the prefix
/// `full_`.
pub struct TwoBitFile {
    path: PathBuf,
    softmask_enabled: bool,
    sequences: HashMap<String, FileIndex>,
}

struct SequenceRecord {
    dna_offset: FileIndex,
    dna_size: Field,
    n_blocks: Vec<Block>,
    soft_mask_blocks: Vec<Block>,
}

/// General information about a 2bit file
#[derive(Debug, PartialEq)]
pub struct TwoBitFileInfo {
    /// File size of the 2bit file
    pub file_size: u64,
    /// Number of chromosomes (or sequences) in the file
    pub chromosomes: usize,
    /// Total number of nucleotides in the file
    pub total_sequence_length: usize,
    /// Number of hard masks
    pub hard_masks_count: usize,
    /// Number of soft masks
    pub soft_masks_count: usize,
}

impl TwoBitFile {
    /// Constructor for a 2bit file.
    ///
    /// * `path` - A path to the 2bit file
    /// * `softmask_enabled` - return lower case nucleotides for soft blocks
    pub fn open<P: AsRef<Path>>(path: P, softmask_enabled: bool) -> Result<TwoBitFile, Error> {
        let mut reader = ValueReader::new(&path)?;

        let mut sequences = HashMap::new();

        let sequence_count = reader.field()?;
        let _reserved = reader.field(); // read the unused reserved field

        for _ in 0..sequence_count {
            let name_size = reader.byte()? as usize;
            let name = reader.string(name_size)?;
            let seq_offset = reader.field()?;

            sequences.insert(name, seq_offset);
        }

        let path = path.as_ref().to_path_buf();
        Ok(TwoBitFile {
            path,
            sequences,
            softmask_enabled,
        })
    }

    /// Get the sizes of chromosomes in a 2bit file as a `HashMap`
    pub fn chroms(&self) -> HashMap<String, Field> {
        let mut result = HashMap::new();
        for chr in self.sequences.keys() {
            let seq = self.sequence_record(chr).expect("Chromosome must be valid");
            result.insert((*chr).clone(), seq.dna_size);
        }
        result
    }

    /// Get the full sequence of a chromosome
    ///
    /// * `chr` Name of the chromosome from the 2bit file
    /// * returns the full sequence as a `String` on success
    pub fn full_sequence(&self, chr: &str) -> Result<String, Error> {
        if self.sequences.contains_key(chr) {
            let record = self.sequence_record(chr)?;
            record.sequence(&self.path, 0, record.dna_size as usize)
        } else {
            Err(Error::MissingName(chr.to_string()))
        }
    }

    /// Get a partial sequence of a chromosome
    ///
    /// * `chr` Name of the chromosome from the 2bit file
    /// * `start` Start position of the sequence
    /// * `end` Stop position of the sequence
    /// * returns the full sequence as a `String` on success
    pub fn sequence(&self, chr: &str, start: usize, end: usize) -> Result<String, Error> {
        if self.sequences.contains_key(chr) {
            let record = self.sequence_record(chr)?;
            record.sequence(&self.path, start, end)
        } else {
            Err(Error::MissingName(chr.to_string()))
        }
    }

    /// Count bases of a chromosome
    ///
    /// * `chr` Name of the chromosome from the 2bit file
    pub fn full_bases(&self, chr: &str) -> Result<BaseCounts, Error> {
        self.bases(chr, 0, self.chr_length(chr)?)
    }

    /// Calculates percentages of bases of a chromosome
    ///
    /// * `chr` Name of the chromosome from the 2bit file
    pub fn full_bases_percentages(&self, chr: &str) -> Result<BasePercentages, Error> {
        self.bases_percentages(chr, 0, self.chr_length(chr)?)
    }

    /// Count bases of a partial chromosome
    ///
    /// * `chr` Name of the chromosome from the 2bit file
    pub fn bases(&self, chr: &str, start: usize, end: usize) -> Result<BaseCounts, Error> {
        let nucs = self
            .sequence_record(chr)?
            .sequence(self.path.clone(), start, end)?;
        let mut result = BaseCounts::default();
        for nuc in nucs.chars() {
            match nuc {
                'A' | 'a' => result.a += 1,
                'C' | 'c' => result.c += 1,
                'G' | 'g' => result.g += 1,
                'T' | 't' => result.t += 1,
                'N' => result.n += 1,
                _ => return Err(Error::BadNucleotide(nuc)),
            }
        }
        Ok(result)
    }

    /// Calculates percentages of bases of a partial chromosome
    ///
    /// * `chr` Name of the chromosome from the 2bit file
    pub fn bases_percentages(
        &self,
        chr: &str,
        start: usize,
        end: usize,
    ) -> Result<BasePercentages, Error> {
        Ok(self.bases(chr, start, end)?.into())
    }

    /// Obtain general information on the 2bit file you are dealing with
    pub fn info(&self) -> Result<TwoBitFileInfo, Error> {
        let mut total_length = 0;
        let mut hard_masks_count = 0;
        let mut soft_masks_count = 0;
        for chr in self.sequences.keys() {
            total_length += self.chr_length(chr)?;
            let record = self.sequence_record(chr)?;
            hard_masks_count +=
                record.n_blocks.iter().fold(0, |acc, blk| acc + blk.length) as usize;
            soft_masks_count += record
                .soft_mask_blocks
                .iter()
                .fold(0, |acc, blk| acc + blk.length) as usize;
        }
        Ok(TwoBitFileInfo {
            file_size: metadata(&self.path)?.len(),
            chromosomes: self.sequences.len(),
            total_sequence_length: total_length,
            hard_masks_count,
            soft_masks_count,
        })
    }

    /// Get all hard blocks (N-blocks) of a chromosome
    ///
    /// * `chr` Name of the chromosome from the 2bit file
    pub fn full_hard_masked_blocks(&self, chr: &str) -> Result<Vec<Block>, Error> {
        match self.sequence_record(chr) {
            Ok(record) => Ok(record.n_blocks),
            Err(e) => Err(e),
        }
    }

    /// Get hard blocks (N-blocks) of a region on a chromosome
    ///
    /// * `chr` Name of the chromosome from the 2bit file
    pub fn hard_masked_blocks(
        &self,
        chr: &str,
        start: usize,
        end: usize,
    ) -> Result<Vec<Block>, Error> {
        match self.sequence_record(chr) {
            Ok(record) => {
                let mut result = Vec::new();
                for block in record.n_blocks {
                    let block_end = block.start + block.length;
                    if block_end as usize <= start {
                        continue;
                    } else if block.start as usize > end {
                        break;
                    }
                    result.push(block)
                }
                Ok(result)
            }
            Err(e) => Err(e),
        }
    }

    /// Get all soft blocks (lower case blocks) of a chromosome
    ///
    /// * `chr` Name of the chromosome from the 2bit file
    pub fn full_soft_masked_blocks(&self, chr: &str) -> Result<Vec<Block>, Error> {
        match self.sequence_record(chr) {
            Ok(record) => Ok(record.soft_mask_blocks),
            Err(e) => Err(e),
        }
    }

    /// Get soft blocks (lower case blocks) of a region on a chromosome
    ///
    /// * `chr` Name of the chromosome from the 2bit file
    pub fn soft_masked_blocks(
        &self,
        chr: &str,
        start: usize,
        end: usize,
    ) -> Result<Vec<Block>, Error> {
        match self.sequence_record(chr) {
            Ok(record) => {
                let mut result = Vec::new();
                for block in record.soft_mask_blocks {
                    let block_end = block.start + block.length;
                    if block_end as usize <= start {
                        continue;
                    } else if block.start as usize > end {
                        break;
                    }
                    result.push(block)
                }
                Ok(result)
            }
            Err(e) => Err(e),
        }
    }

    fn sequence_record(&self, chr: &str) -> Result<SequenceRecord, Error> {
        let mut reader = ValueReader::new(&self.path)?;
        let offset = self
            .sequences
            .get(chr)
            .ok_or_else(|| Error::MissingName(chr.to_string()))?;
        reader.seek(SeekFrom::Start(*offset as u64))?;
        let dna_size = reader.field()?;
        let n_blocks = reader.blocks()?;
        let soft_mask_blocks = {
            if self.softmask_enabled {
                reader.blocks()?
            } else {
                reader.skip_blocks()?;
                Vec::new()
            }
        };
        let _reserved = reader.field()?;
        let dna_offset = reader.tell()?;

        Ok(SequenceRecord {
            dna_offset,
            dna_size,
            n_blocks,
            soft_mask_blocks,
        })
    }

    fn chr_length(&self, chr: &str) -> Result<usize, Error> {
        match self.chroms().get(chr) {
            Some(v) => Ok(*v as usize),
            None => Err(Error::MissingName(chr.to_string())),
        }
    }
}

impl SequenceRecord {
    fn sequence<P: AsRef<Path>>(&self, path: P, start: usize, end: usize) -> Result<String, Error> {
        let mut reader = ValueReader::new(path)?;
        let mut skip = start % 4;
        reader
            .seek(SeekFrom::Start(self.dna_offset as u64))
            .unwrap(); // beginning of the DNA sequence
        reader.seek(SeekFrom::Current(start as i64 / 4))?; // position where we want to start reading

        let length = end - start;

        let mut result = String::with_capacity(length);
        while result.len() < length {
            let mut byte = reader.byte()?;
            for _ in 0..4 {
                if skip > 0 {
                    skip -= 1;
                    byte <<= 2;
                    continue;
                }
                let nuc = match byte & 192 {
                    0 => 'T',
                    64 => 'C',
                    128 => 'A',
                    192 => 'G',
                    _ => panic!("Bit flag magic is broken"),
                };
                byte <<= 2;
                result.push(nuc);
                if result.len() == length {
                    break;
                }
            }
        }

        let seq_block = Block::new(start as Field, length as Field);

        // closure over `seq_block`, `start` and `end`
        let replace_blocks =
            |mut seq: String, blocks: &Vec<Block>, replacement_mode: BlockReplacementMode| {
                for block in blocks {
                    let block_end = (block.start + block.length) as usize;
                    if block_end <= start {
                        continue;
                    } else if end <= block.start as usize {
                        break; // should be the last block assuming ordering is upheld
                    }
                    let mut range = block
                        .overlap(&seq_block)
                        .expect("Block ordering not upheld");

                    // adjust for partial sequence
                    range.start -= start;
                    range.end -= start;

                    let replacement = match replacement_mode {
                        BlockReplacementMode::Hard => "N".repeat(range.end - range.start),
                        BlockReplacementMode::Soft => seq[range.clone()].to_lowercase(),
                    };
                    seq.replace_range(range, &replacement);
                }
                seq
            };
        let result = replace_blocks(result, &self.n_blocks, BlockReplacementMode::Hard);
        let result = replace_blocks(result, &self.soft_mask_blocks, BlockReplacementMode::Soft);

        Ok(result)
    }
}

enum BlockReplacementMode {
    Soft, // convert to lower case
    Hard, // replace with Ns
}

#[cfg(test)]
mod tests {
    use super::*;
    const TESTFILE: &str = "assets/foo.2bit";

    #[test]
    fn test_chroms() {
        let bit = TwoBitFile::open(TESTFILE, true).unwrap();
        let mut chr_count = 0;
        for (chr, len) in bit.chroms() {
            match chr.as_ref() {
                "chr1" => assert_eq!(len, 150),
                "chr2" => assert_eq!(len, 100),
                _ => assert!(false), // unexpected chromosome
            }
            chr_count += 1;
        }
        assert_eq!(chr_count, 2)
    }

    #[test]
    fn test_full_sequence() {
        let seq = "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNACGTACGTACGTagctagctGATCGATCGTAGCTAGCTAGCTAGCTGATCNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN".to_string();
        let bit = TwoBitFile::open(TESTFILE, true).unwrap();
        assert_eq!(seq, bit.full_sequence("chr1").unwrap());
    }

    #[test]
    fn test_sequence() {
        let seq = "NNNNNNNNNNNNNNNNNNNNNNNNNNACGTACGTACGTagctagctGATC";
        let bit = TwoBitFile::open(TESTFILE, true).unwrap();
        assert_eq!(seq, bit.sequence("chr1", 24, 74).unwrap());
        // let's try different offsets to test positions not divisible by 4
        let seq = "ACGTACGTagctagctGATC";
        assert_eq!(seq, bit.sequence("chr1", 54, 74).unwrap());
        let seq = "CGTACGTagctagctGATC";
        assert_eq!(seq, bit.sequence("chr1", 55, 74).unwrap());
        let seq = "GTACGTagctagctGATC";
        assert_eq!(seq, bit.sequence("chr1", 56, 74).unwrap()); // divisible by 4
        let seq = "TACGTagctagctGATC";
        assert_eq!(seq, bit.sequence("chr1", 57, 74).unwrap());
    }

    #[test]
    fn test_full_bases() {
        let percentages = BasePercentages {
            a: 0.08,
            c: 0.08,
            t: 0.08666666666666667,
            g: 0.08666666666666667,
            n: 100.0 / 150.0,
        };
        let bit = TwoBitFile::open(TESTFILE, true).unwrap();
        assert_eq!(percentages, bit.full_bases_percentages("chr1").unwrap());
    }

    #[test]
    fn test_bases() {
        let counts = BaseCounts {
            a: 6,
            c: 6,
            t: 6,
            g: 6,
            n: 26,
        };
        let percentages = BasePercentages {
            a: 0.12,
            c: 0.12,
            t: 0.12,
            g: 0.12,
            n: 26.0 / 50.0,
        };
        let bit = TwoBitFile::open(TESTFILE, true).unwrap();
        assert_eq!(counts, bit.bases("chr1", 24, 74).unwrap());
        assert_eq!(percentages, bit.bases_percentages("chr1", 24, 74).unwrap());
    }

    #[test]
    fn test_info() {
        let info = TwoBitFileInfo {
            file_size: 161,
            chromosomes: 2,
            total_sequence_length: 250,
            hard_masks_count: 150,
            soft_masks_count: 8,
        };
        let bit = TwoBitFile::open(TESTFILE, true).unwrap();
        assert_eq!(bit.info().unwrap(), info);
    }

    #[test]
    fn test_hard_masked_blocks() {
        let bit = TwoBitFile::open(TESTFILE, true).unwrap();
        let mut i = 0;
        for block_ in bit.full_hard_masked_blocks("chr1").unwrap() {
            match i {
                0 => {
                    assert_eq!(block_.start, 0);
                    assert_eq!(block_.length, 50);
                }
                1 => {
                    assert_eq!(block_.start, 100);
                    assert_eq!(block_.length, 50);
                }
                _ => assert!(false),
            }
            i += 1
        }
        assert_eq!(2, i);
        //TODO hard_masked_blocks()
    }

    #[test]
    fn test_soft_masked_blocks() {
        let bit = TwoBitFile::open(TESTFILE, true).unwrap();
        let mut i = 0;
        for block_ in bit.full_soft_masked_blocks("chr1").unwrap() {
            match i {
                0 => {
                    assert_eq!(block_.start, 62);
                    assert_eq!(block_.length, 8);
                }
                _ => assert!(false),
            }
            i += 1
        }
        assert_eq!(1, i);
        //TODO soft_masked_blocks()
    }

    //TODO IO Errors
}