File: writer.d

package info (click to toggle)
sambamba 1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,528 kB
  • sloc: sh: 220; python: 166; ruby: 147; makefile: 103
file content (87 lines) | stat: -rw-r--r-- 2,652 bytes parent folder | download | duplicates (4)
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
/*
    New style BAM writer. This file is part of Sambamba.
    Copyright (C) 2017 Pjotr Prins <pjotr.prins@thebird.nl>

    Sambamba 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, 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.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA

*/

module bio.std.experimental.hts.bam.writer;

import std.conv;
import core.stdc.stdio: fopen, fread, fclose;
import std.exception;
import std.file;
import std.stdio;
import std.string;
import std.typecons;
import std.bitmanip;


import bio.std.hts.bam.cigar; //depends on undead
import bio.std.hts.bam.constants;

import bio.std.experimental.hts.bgzf;
import bio.std.experimental.hts.bgzf_writer;
import bio.std.experimental.hts.constants;

import bio.std.experimental.hts.bam.header;
import bio.std.experimental.hts.bam.reader : ProcessReadBlob, Offset;

struct ModifyProcessReadBlob { // make this generic later
  ProcessReadBlob _read2;

  @property ubyte[] toBlob() {
    return _read2.toBlob();
  }

  @property void set_qc_fail() {
    auto data = _read2.toBlob;
    // writeln(_read2._flag);
    // data[Offset.flag_nc] = data[Offset.flag_nc] & 0x200;
    // writeln(data[Offset.flag_nc]);
    // buf.write!(T,Endian.littleEndian)(value,0);
    //  ushort _flag()          { return fetch!ushort(Offset.flag); }

    ushort flag = _read2._flag | 0x200;
    // writeln("flag=",flag);
    data[Offset.flag..Offset.flag+4].write!(ushort,Endian.littleEndian)(flag,0);
  }
}

struct BamWriter {
  BgzfWriter bgzf_writer;

  this(string fn, ref BamHeader header, int compression_level = -1) {
    bgzf_writer = BgzfWriter(fn,compression_level);
    write_bam_header(bgzf_writer,header);
  }

  void push(ModifyProcessReadBlob read) {
    auto mod = read;
    auto blob = mod.toBlob;
    // another hack for now:
    bgzf_writer.write!int(cast(int)(blob.length+2*int.sizeof));
    bgzf_writer.write!int(cast(int)mod._read2.raw_ref_id);
    bgzf_writer.write!int(cast(int)mod._read2.raw_start_pos);
    bgzf_writer.write(blob);
  }

  void push(ProcessReadBlob read) {
    push(ModifyProcessReadBlob(read));
  }

}