Browse Source

renames: writeRead -> writeRecord; phred_base_quality -> base_qualities

remotes/georgeg/bam_output_redesign
lomereiter 10 years ago
parent
commit
f4984db147
  1. 18
      bio/bam/read.d
  2. 32
      bio/bam/referenceinfo.d
  3. 2
      bio/bam/serialization/json.d
  4. 6
      bio/bam/serialization/sam.d
  5. 2
      bio/bam/writer.d
  6. 4
      test/unittests.d

18
bio/bam/read.d

@ -215,6 +215,12 @@ struct BamRead {
return is_reverse_strand ? '-' : '+';
}
/// ditto
@property void strand(char c) {
enforce(c == '-' || c == '+', "Strand must be '-' or '+'");
is_reverse_strand = c == '-';
}
/// Read name
@property string name() const nothrow {
// notice -1: the string is zero-terminated, so we should strip that '\0'
@ -412,12 +418,12 @@ struct BamRead {
}
/// Quality data
@property const(ubyte)[] phred_base_quality() const nothrow {
@property const(ubyte)[] base_qualities() const nothrow {
return _chunk[_qual_offset .. _qual_offset + _l_seq * char.sizeof];
}
/// Set quality data - array length must be of the same length as the sequence.
@property void phred_base_quality(const(ubyte)[] quality) {
@property void base_qualities(const(ubyte)[] quality) {
enforce(quality.length == _l_seq, "Quality data must be of the same length as sequence");
_dup();
_chunk[_qual_offset .. _qual_offset + _l_seq] = quality;
@ -616,7 +622,7 @@ struct BamRead {
packer.pack(next_pos);
packer.pack(template_length);
packer.pack(to!string(sequence));
packer.pack(phred_base_quality);
packer.pack(base_qualities);
packer.beginMap(tagCount());
foreach (key, value; this) {
@ -1052,9 +1058,9 @@ unittest {
assert(read.name == "anothername");
assert(read.cigarString() == "22M");
read.phred_base_quality = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22];
assert(reduce!"a+b"(0, read.phred_base_quality) == 253);
read.base_qualities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22];
assert(reduce!"a+b"(0, read.base_qualities) == 253);
read.cigar = [CigarOperation(20, 'M'), CigarOperation(2, 'X')];
assert(read.cigarString() == "20M2X");

32
bio/bam/referenceinfo.d

@ -24,19 +24,35 @@ import std.exception;
import std.array;
/**
Stores reference sequence name and length
Stores basic information about reference sequence.
*/
struct ReferenceSequenceInfo {
string name;
int length;
private {
string _name;
int _length;
}
/// Reference sequence name
string name() @property const {
return _name;
}
/// Reference sequence length
int length() @property const {
return _length;
}
///
this(string name, int length) {
_name = name;
_length = length;
}
/**
Constructs the structure from input stream
*/
/// Constructs the structure from input stream
this(ref Stream stream) {
int l_name; // length of the reference name plus one
stream.read(l_name);
name = stream.readString(l_name)[0..$-1].idup; // strip '\0' at the end
stream.read(length);
_name = stream.readString(l_name)[0..$-1].idup; // strip '\0' at the end
stream.read(_length);
}
}

2
bio/bam/serialization/json.d

@ -269,7 +269,7 @@ void jsonSerialize(S)(BamRead alignment, ReferenceSequenceInfo[] info, ref S str
putcharacter(stream, '[');
bool first = true;
foreach(ubyte c; alignment.phred_base_quality) {
foreach(ubyte c; alignment.base_qualities) {
if (!first) {
putcharacter(stream, ',');
} else {

6
bio/bam/serialization/sam.d

@ -213,12 +213,12 @@ void serialize(S, R)(auto ref R alignment, ReferenceSequenceInfo[] info, auto re
}
putcharacter(stream, '\t');
}
if (alignment.phred_base_quality.length == 0 ||
alignment.phred_base_quality[0] == '\xFF')
if (alignment.base_qualities.length == 0 ||
alignment.base_qualities[0] == '\xFF')
{
putcharacter(stream, '*');
} else {
foreach(char c; alignment.phred_base_quality) {
foreach(char c; alignment.base_qualities) {
putcharacter(stream, cast(char)(c + 33));
}
}

2
bio/bam/writer.d

@ -106,7 +106,7 @@ final class BamWriter {
}
/// Write BAM read. Throws exception if read reference ID is out of range.
void writeRead(R)(R read) {
void writeRecord(R)(R read) {
enforce(read.ref_id < _reference_sequences.length,
"Read reference ID is out of range");

4
test/unittests.d

@ -73,7 +73,7 @@ unittest {
auto reads = bf.reads;
auto read = reads.front;
assert(equal(read.sequence, "CTCAAGGTTGTTGCAAGGGGGTCTATGTGAACAAA"));
assert(equal(map!"cast(char)(a + 33)"(read.phred_base_quality),
assert(equal(map!"cast(char)(a + 33)"(read.base_qualities),
"<<<7<<<;<<<<<<<<8;;<7;4<;<;;;;;94<;"));
assert(bf.reference(read.ref_id).name == "chr1");
assert(read.name == "EAS56_57:6:190:289:82");
@ -267,7 +267,7 @@ unittest {
writer.writeReferenceSequenceInfo(bf.reference_sequences);
foreach (read; bf.reads)
writer.writeRead(read);
writer.writeRecord(read);
writer.flush();

Loading…
Cancel
Save