/usr/include/d/bio/bam/readrange.d is in libbiod-dev 0.1.0-5build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
This file is part of BioD.
Copyright (C) 2012-2016 Artem Tarasov <lomereiter@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
module bio.bam.readrange;
import bio.bam.read;
import bio.bam.abstractreader;
import bio.bam.reader;
import bio.core.bgzf.inputstream;
import bio.core.bgzf.virtualoffset;
import undead.stream;
import std.algorithm;
import std.system;
import std.bitmanip;
/// Read + its start/end virtual offsets
struct BamReadBlock {
VirtualOffset start_virtual_offset; ///
VirtualOffset end_virtual_offset; ///
BamRead read; ///
alias read this; ///
///
BamReadBlock dup() @property const {
return BamReadBlock(start_virtual_offset, end_virtual_offset, read.dup);
}
}
///
mixin template withOffsets() {
/**
Returns: virtual offsets of beginning and end of the current read
plus the current read itself.
*/
BamReadBlock front() @property {
return BamReadBlock(_start_voffset,
_stream.virtualTell(),
_current_record);
}
private VirtualOffset _start_voffset;
private void beforeNextBamReadLoad() {
_start_voffset = _stream.virtualTell();
}
}
///
mixin template withoutOffsets() {
/**
Returns: current read
*/
ref BamRead front() @property {
return _current_record;
}
private void beforeNextBamReadLoad() {}
}
/// $(D front) return type is determined by $(I IteratePolicy)
struct BamReadRange(alias IteratePolicy)
{
/// Create new range from BgzfInputStream.
this(BgzfInputStream stream, BamReader reader=null) {
_stream = stream;
_reader = reader;
_endian_stream = new EndianStream(_stream, Endian.littleEndian);
readNext();
}
///
bool empty() @property const {
return _empty;
}
mixin IteratePolicy;
///
void popFront() {
readNext();
}
private:
BgzfInputStream _stream;
EndianStream _endian_stream;
BamReader _reader;
BamRead _current_record;
bool _empty = false;
ubyte[] _buffer;
/*
Reads next bamRead block from stream.
*/
void readNext() {
// In fact, on BAM files containing a special EOF BGZF block
// this condition will be always false!
//
// The reason is that we don't want to unpack next block just
// in order to see if it's an EOF one or not.
if (_stream.eof()) {
_empty = true;
return;
}
// In order to get the right virtual offset, we need to do it here.
version(extraVerbose) {
// import std.stdio; stderr.writeln("record v.o. = ", _stream.virtualTell());
}
beforeNextBamReadLoad();
// Here's where _empty is really set!
ubyte[int.sizeof] tmp = void;
auto _read = 0;
while (_read < int.sizeof) {
auto _actually_read = _endian_stream.readBlock(tmp.ptr + _read, int.sizeof - _read);
if (_actually_read == 0) {
version(development) {
import std.stdio;
stderr.writeln("[info][bamRead range] empty, read ", _read, " bytes, expected ", int.sizeof);
}
_empty = true;
return;
}
_read += _actually_read;
}
int block_size = littleEndianToNative!int(tmp);
version(extraVerbose) {
import std.stdio;
stderr.writeln("[uncompressed] record size: ", block_size);
}
ubyte[] data = void;
if (_reader !is null && _reader._seqprocmode) {
if (block_size > _buffer.length)
_buffer.length = block_size;
data = _buffer[0 .. block_size];
} else {
data = allocate(block_size);
}
_stream.readExact(data.ptr, block_size);
_current_record = BamRead(data);
_current_record.associateWithReader(_reader);
}
private {
ubyte[] allocate(size_t size) {
if (_alloc_buffer_used + size > _alloc_buffer.length) {
_alloc_buffer = uninitializedArray!(ubyte[])(max(size, 65536));
_alloc_buffer_used = 0;
}
auto result = _alloc_buffer[_alloc_buffer_used .. $][0 .. size];
_alloc_buffer_used += size;
return result;
}
ubyte[] _alloc_buffer;
size_t _alloc_buffer_used;
}
}
/// Returns: lazy range of BamRead/BamReadBlock structs constructed from a given stream.
auto bamReadRange(alias IteratePolicy=withoutOffsets)(BgzfInputStream stream, BamReader reader) {
return BamReadRange!IteratePolicy(stream, reader);
}
|