NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
output_bam.h
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
32 
39 #include <nvbio/io/bam_format.h>
40 #include <nvbio/basic/threads.h>
41 #include <stdio.h>
42 
43 namespace nvbio {
44 namespace io {
45 
46 struct BamOutput : public OutputFile
47 {
48 private:
49  // BAM alignment flags
50  // these are meant to be bitwised OR'ed together
51  typedef enum {
52  BAM_FLAGS_PAIRED = 1 << 16,
53  BAM_FLAGS_PROPER_PAIR = 2 << 16,
54  BAM_FLAGS_UNMAPPED = 4 << 16,
55  BAM_FLAGS_MATE_UNMAPPED = 8 << 16,
56  BAM_FLAGS_REVERSE = 16 << 16,
57  BAM_FLAGS_MATE_REVERSE = 32 << 16,
58  BAM_FLAGS_READ_1 = 64 << 16,
59  BAM_FLAGS_READ_2 = 128 << 16,
60  BAM_FLAGS_SECONDARY = 256 << 16,
61  BAM_FLAGS_QC_FAILED = 512 << 16,
62  BAM_FLAGS_DUPLICATE = 1024 << 16
63  } BamAlignmentFlags;
64 
65 public:
67  ~BamOutput();
68 
69  void header() { output_header(); }
70 
75  void process(struct HostOutputBatchSE& batch);
76 
81  void process(struct HostOutputBatchPE& batch);
82 
83  void close(void);
84 
85 private:
86  void output_header(void);
87  uint32 process_one_alignment(AlignmentData& alignment, AlignmentData& mate);
88 
89  void write_block();
90  void flush_blocks();
91 
92  uint32 generate_cigar(struct BAM_alignment& alnh,
93  struct BAM_alignment_data_block& alnd,
94  const AlignmentData& alignment);
95  uint32 generate_md_string(BAM_alignment& alnh, BAM_alignment_data_block& alnd,
96  const AlignmentData& alignment);
97 
98  void output_tag_uint32(DataBuffer& out, const char *tag, uint32 val);
99  void output_tag_uint8(DataBuffer& out, const char *tag, uint8 val);
100  void output_tag_string(DataBuffer& out, const char *tag, const char *val);
101 
102  void output_alignment(BAM_alignment& alnh, BAM_alignment_data_block& alnd);
103 
104  static uint8 encode_bp(uint8 bp);
105 
106  // our file pointer
107  FILE *fp;
108  // CPU copy of the current alignment batch
109  HostOutputBatchPE cpu_output;
110 
111  // text buffer that we're filling with data
112  static const uint32 BUFFERS = 64;
113  DataBuffer data_buffers[BUFFERS];
114  DataBuffer compressed_buffers[BUFFERS];
115  int32 buffer_id;
116 
117  // our BGZF compressors
118  BGZFCompressor bgzf[BUFFERS];
119 
120  Mutex mutex;
121 };
122 
123 } // namespace io
124 } // namespace nvbio