Browse Source

fixes

remotes/georgeg/bam_output_redesign
lomereiter 10 years ago
parent
commit
9fdba56fd1
  1. 30
      bio/bam/bai/indexing.d
  2. 8
      bio/bam/baseinfo.d
  3. 6
      bio/bam/readrange.d
  4. 5
      bio/bam/serialization/json.d
  5. 8
      bio/bam/serialization/sam.d
  6. 29
      bio/core/tinymap.d
  7. 4
      bio/core/utils/format.d

30
bio/bam/bai/indexing.d

@ -1,13 +1,13 @@
/*
This file is part of Sambamba.
This file is part of BioD.
Copyright (C) 2012 Artem Tarasov <lomereiter@gmail.com>
Sambamba is free software; you can redistribute it and/or modify
BioD is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Sambamba is distributed in the hope that it will be useful,
BioD is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
@ -17,13 +17,13 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
module bai.indexing;
module bio.bam.bai.indexing;
import bamfile;
import constants;
import bio.bam.reader;
import bio.bam.constants;
import bai.bin;
import bai.chunk;
import bio.bam.bai.bin;
import bio.bam.bai.chunk;
import std.stream;
import std.array;
@ -51,7 +51,7 @@ void defaultProgressBarFunc(lazy float dummy) {}
/// Writes BAM index to the $(D stream)
///
/// Accepts optional $(D progressBarFunc)
void createIndex(ref BamFile bam, ref Stream stream, void delegate(lazy float p) progressBarFunc=null) {
void createIndex(BamReader bam, ref Stream stream, void delegate(lazy float p) progressBarFunc=null) {
auto endian_stream = new EndianStream(stream, Endian.littleEndian);
@ -67,8 +67,8 @@ void createIndex(ref BamFile bam, ref Stream stream, void delegate(lazy float p)
}
// BAM file contains no alignments at all or all reads are unmapped
if (bam.alignments!withOffsets.empty ||
bam.alignments!withOffsets.front.alignment.ref_id < 0) {
if (bam.reads!withOffsets.empty ||
bam.reads!withOffsets.front.read.ref_id < 0) {
foreach (i; 0 .. nrefs) {
writeEmptyReference();
}
@ -77,13 +77,13 @@ void createIndex(ref BamFile bam, ref Stream stream, void delegate(lazy float p)
// OK, now let's deal with non-degenerate case
auto alignment_blocks = bam.alignmentsWithProgress!withOffsets(progressBarFunc);
auto alignment_blocks = bam.readsWithProgress!withOffsets(progressBarFunc);
auto prev_block = alignment_blocks.front;
alignment_blocks.popFront();
// this is the main character hereafter
auto prev_read = prev_block.alignment;
auto prev_read = prev_block.read;
// array of linear offsets for the current reference entry
ulong[BAI_MAX_BIN_ID - BAI_MAX_NONLEAF_BIN_ID + 1] linear_index;
@ -93,7 +93,7 @@ void createIndex(ref BamFile bam, ref Stream stream, void delegate(lazy float p)
// map: bin ID -> array of chunks
Chunk[][uint] chunks;
auto first_ref_id = prev_block.alignment.ref_id;
auto first_ref_id = prev_block.read.ref_id;
auto current_chunk_beg = prev_block.start_virtual_offset;
assert(first_ref_id >= 0);
@ -212,7 +212,7 @@ void createIndex(ref BamFile bam, ref Stream stream, void delegate(lazy float p)
foreach (block; alignment_blocks) {
auto read = block.alignment;
auto read = block.read;
// new reference, so write data for previous one(s)
if (read.ref_id != prev_read.ref_id) {

8
bio/bam/baseinfo.d

@ -183,7 +183,7 @@ struct PerBaseInfo(R, Tags...) {
///
/// Example:
///
/// basesWith!"FZ"(arg!"FZ"(flow_order));
/// basesWith!"FZ"(arg!"flowOrder"(flow_order), arg!"keySequence"(key_sequence));
///
template basesWith(Tags...) {
auto basesWith(R, Args...)(R read, Args args) {
@ -196,7 +196,7 @@ template FZbaseInfo(R) {
mixin template resultProperties() {
/// Current flow call
ReadFlowCall flow_call() @property {
ReadFlowCall flow_call() @property const {
return _flow_call;
}
@ -281,14 +281,14 @@ template CIGARbaseInfo(R) {
mixin template resultProperties() {
/// Current CIGAR operation
CigarOperation cigar_operation() @property {
CigarOperation cigar_operation() @property const {
return _cigar_operation;
}
/// Position of the corresponding base on the reference.
/// If current CIGAR operation is not one of 'M', '=', 'X',
/// returns the position of the previous valid base.
ulong position() @property {
ulong position() @property const {
return _reference_position;
}

6
bio/bam/readrange.d

@ -32,11 +32,11 @@ import std.system;
struct BamReadBlock {
VirtualOffset start_virtual_offset;
VirtualOffset end_virtual_offset;
BamRead bamRead;
alias bamRead this;
BamRead read;
alias read this;
BamReadBlock dup() @property const {
return BamReadBlock(start_virtual_offset, end_virtual_offset, bamRead.dup);
return BamReadBlock(start_virtual_offset, end_virtual_offset, read.dup);
}
}

5
bio/bam/serialization/json.d

@ -19,7 +19,8 @@
*/
module bio.bam.serialization.json;
import bio.bam.file;
import bio.bam.read;
import bio.bam.reference;
import bio.bam.tagvalue;
import bio.core.utils.format;
@ -279,7 +280,7 @@ void jsonSerialize(S)(BamRead alignment, ReferenceSequenceInfo[] info, ref S str
putstring(stream, `],"tags":{`);
bool not_first = false;
foreach (k, v; alignment.tags) {
foreach (k, v; alignment) {
assert(k.length == 2);
if (not_first) {

8
bio/bam/serialization/sam.d

@ -44,7 +44,9 @@ import std.array;
assert(toSam(v) == "B:i,1,2,3");
----------
*/
string toSam(Value v) {
string toSam(V)(auto ref V v)
if(is(V == Value))
{
char[] buf;
buf.reserve(16);
serialize(v, buf);
@ -113,7 +115,7 @@ void serialize(S)(const ref Value v, ref S stream) {
/// -------------
/// toSam(alignment, bam.reference_sequences);
/// -------------
string toSam(BamRead alignment, ReferenceSequenceInfo[] info) {
string toSam(R)(auto ref R alignment, ReferenceSequenceInfo[] info) {
char[] buf;
buf.reserve(512);
serialize(alignment, info, buf);
@ -122,7 +124,7 @@ string toSam(BamRead alignment, ReferenceSequenceInfo[] info) {
/// Serialize $(D alignment) to FILE* or append it to char[]/char*
/// (in char* case it's your responsibility to allocate enough memory)
void serialize(S)(BamRead alignment, ReferenceSequenceInfo[] info, ref S stream)
void serialize(S, R)(auto ref R alignment, ReferenceSequenceInfo[] info, auto ref S stream)
if (is(Unqual!S == FILE*) || is(Unqual!S == char*) || is(Unqual!S == char[]))
{

29
bio/core/tinymap.d

@ -51,11 +51,22 @@ struct TinyMap(K, V, alias TinyMapPolicy=useBitArray) {
}
/// Indexed access
V opIndex(K key) const {
auto ref V opIndex(Key)(auto ref Key key)
if(is(Key == K))
{
assert(key in this);
return _dict[key.internal_code];
}
/// ditto
auto ref const(V) opIndex(Key)(auto ref Key key) const
if(is(Key == K))
{
assert(key in this);
return _dict[key.internal_code];
}
/// ditto
V opIndexAssign(V value, K key) {
if (key !in this) {
@ -197,11 +208,18 @@ mixin template useDefaultValue(K, V) {
/// Allows to set up a dictionary which is always full.
mixin template fillNoRemove(K, V) {
private void init(V value=V.init) {
_dict[] = value;
private void init() {
_size = K.ValueSetSize;
}
private void init(V value) {
_size = K.ValueSetSize;
for (size_t i = 0; i < _size; ++i)
_dict[i] = value;
}
private bool _hasKey(K key) const {
return true;
}
@ -254,6 +272,11 @@ unittest {
test(dict1);
test(dict2);
test(dict3);
auto dict4 = TinyMap!(Base, ulong[4])();
dict4[Base('A')] = [0, 1, 2, 3];
dict4[Base('A')][3] += 1;
assert(dict4[Base('A')] == [0, 1, 2, 4]);
}
/// Convenient mixin template for getting your struct working with TinyMap.

4
bio/core/utils/format.d

@ -154,9 +154,9 @@ private {
char* wstr=str;
static if (isSigned!T) {
uint uvalue = (value < 0) ? -value : value;
ulong uvalue = (value < 0) ? -value : value;
} else {
uint uvalue = value;
ulong uvalue = value;
}
do {

Loading…
Cancel
Save