/usr/include/d/bio/sam/reader.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 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 | /*
This file is part of BioD.
Copyright (C) 2012-2013 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.sam.reader;
import bio.bam.abstractreader;
import bio.sam.header;
import bio.bam.read;
import bio.bam.reference;
import bio.bam.referenceinfo;
import bio.core.utils.outbuffer;
import bio.core.utils.range;
import bio.core.utils.bylinefast;
alias ByLineFast _LineRange;
version(DigitalMars) {
import bio.sam.utils.recordparser;
} else {
import bio.sam.utils.fastrecordparser;
}
import std.stdio;
import std.array;
import std.string;
import std.range;
import std.algorithm;
import std.typecons;
import std.parallelism;
import std.process;
import std.exception;
import core.stdc.string;
BamRead _parseSamRecord(Tuple!(char[], SamReader, OutBuffer) t) {
auto r = parseAlignmentLine(cast(string)t[0], t[1]._header, t[2]);
BamRead result;
if (t[1]._seqprocmode) {
result = r;
} else {
auto storage = uninitializedArray!(ubyte[])(r.raw_data.length);
storage[] = r.raw_data[];
result.raw_data = storage;
}
result.associateWithReader(t[1]);
return result;
}
private {
extern(C) size_t lseek(int, size_t, int);
bool isSeekable(ref File file) {
return lseek(file.fileno(), 0, 0) != ~0;
}
}
///
class SamReader : IBamSamReader {
private {
version(gzippedSamSupport) {
void checkGunzip() {
auto gunzip = executeShell("gunzip -V");
if (gunzip.status != 0)
throw new Exception("gunzip is not installed on this system, can't read gzipped SAM");
}
File openSamFile(string filename) {
if (filename.length < 4)
throw new Exception("invalid name for SAM file: " ~ filename);
if (filename[$ - 3 .. $] == ".gz") {
checkGunzip();
auto pipe = pipeShell("gunzip -c " ~ filename);
return pipe.stdout;
} else if (filename[$ - 4 .. $] == ".bam") {
throw new Exception("SAM reader can't read BAM file " ~ filename);
} else {
return File(filename);
}
}
} else {
File openSamFile(string filename) {
if (filename[$ - 4 .. $] == ".bam") {
throw new Exception("SAM reader can't read BAM file " ~ filename);
} else {
return File(filename);
}
}
}
}
///
this(string filename) {
_file = openSamFile(filename);
_filename = filename;
_seekable = _file.isSeekable();
_initializeStream();
}
///
bio.sam.header.SamHeader header() @property {
return _header;
}
///
const(bio.bam.referenceinfo.ReferenceSequenceInfo)[] reference_sequences() @property const {
return _reference_sequences;
}
///
bool hasReference(string reference) {
return null != (reference in _reference_sequence_dict);
}
///
bio.bam.reference.ReferenceSequence opIndex(string ref_name) {
enforce(hasReference(ref_name), "Reference with name " ~ ref_name ~ " is not present in the header");
auto ref_id = _reference_sequence_dict[ref_name];
return ReferenceSequence(null, ref_id, _reference_sequences[ref_id]);
}
/// Reads in SAM file.
auto reads() @property {
_LineRange lines = _lines;
if (_seekable) {
if (_filename !is null) {
auto file = openSamFile(_filename);
lines = ByLineFast(file);
} else {
_file.seek(0);
lines = ByLineFast(_file);
}
auto dummy = lines.front;
for (int i = 0; i < _lines_to_skip; i++)
lines.popFront();
}
auto b = new OutBuffer(262144);
return lines.zip(repeat(this), repeat(b)).map!_parseSamRecord();
}
///
void assumeSequentialProcessing() {
_seqprocmode = true;
}
///
std.range.InputRange!(bio.bam.read.BamRead) allReads() @property {
return inputRangeObject(reads);
}
/// Filename
string filename() @property const {
return _filename;
}
private:
File _file;
bool _seekable;
string _filename;
_LineRange _lines;
ulong _lines_to_skip;
bool _seqprocmode;
SamHeader _header;
ReferenceSequenceInfo[] _reference_sequences;
int[string] _reference_sequence_dict;
void _initializeStream() {
auto header = Appender!(char[])();
_lines = ByLineFast(_file);
while (!_lines.empty) {
auto line = _lines.front;
if (line.length > 0 && line[0] == '@') {
header.put(line);
header.put('\n');
_lines_to_skip += 1;
_lines.popFront();
} else {
break;
}
}
import core.memory;
GC.disable();
_header = new SamHeader(cast(string)(header.data));
GC.enable();
_reference_sequences = new ReferenceSequenceInfo[_header.sequences.length];
foreach (sq; _header.sequences) {
auto seq = ReferenceSequenceInfo(sq.name, sq.length);
auto n = cast(int)_reference_sequences.length;
_reference_sequence_dict[sq.name] = n;
_reference_sequences[_header.getSequenceIndex(seq.name)] = seq;
}
}
}
|