1 --------------------------------------------------------------------------------
3 --------------------------------------------------------------------------------
5 BamTools: a C++ API & toolkit for reading/writing/manipulating BAM files.
21 --------------------------------------------------------------------------------
23 --------------------------------------------------------------------------------
25 BamTools provides both a programmer's API and an end-user's toolkit for handling
28 ----------------------------------------
30 ----------------------------------------
32 The API consists of 2 main modules: BamReader and BamWriter. As you would
33 expect, BamReader provides read-access to BAM files, while BamWriter handles
34 writing data to BAM files. BamReader provides the interface for random-access
35 (jumping) in a BAM file, as well as generating BAM index files.
37 BamMultiReader is an extra module that allows you to manage multiple open BAM
38 files for reading. It provides some validation & bookkeeping under the hood to
39 keep all files sync'ed up for you.
41 Additional files used by the API:
43 - BamAlignment.* : implements the BamAlignment data structure
45 - BamAux.h : contains various constants, data structures and utility
46 methods used throught the API.
48 - BamIndex.* : implements both the standard BAM format index (".bai") as
49 well as a new BamTools-specific index (".bti").
51 - BGZF.* : contains our implementation of the Broad Institute's BGZF
54 ----------------------------------------
56 ----------------------------------------
58 If you've been using the BamTools since the early days, you'll notice that our
59 'toy' API examples (BamConversion, BamDump, BamTrim,...) are now gone. We have
60 dumped these in favor of a suite of small utilities that we hope both
61 developers and end-users find useful:
63 usage: bamtools [--help] COMMAND [ARGS]
65 Available bamtools commands:
67 convert Converts between BAM and a number of other formats
68 count Prints number of alignments in BAM file(s)
69 coverage Prints coverage statistics from the input BAM file
70 filter Filters BAM file(s) by user-specified criteria
71 header Prints BAM header information
72 index Generates index for BAM file
73 merge Merge multiple BAM files into single file
74 random Select random alignments from existing BAM file(s)
75 sort Sorts the BAM file according to some criteria
76 split Splits a BAM file on user-specifed property, creating a
77 new BAM output file for each value found
78 stats Prints some basic statistics from input BAM file(s)
80 See 'bamtools help COMMAND' for more information on a specific command.
82 --------------------------------------------------------------------------------
84 --------------------------------------------------------------------------------
86 ** General usage information - perhaps explain common terms, point to SAM/BAM
89 ----------------------------------------
91 ----------------------------------------
93 The API, as noted above, contains 2 main modules - BamReader & BamWriter - for
94 dealing with BAM files. Alignment data is made available through the
95 BamAlignment data structure.
97 A simple (read-only) scenario for accessing BAM data would look like the
100 // open our BamReader
102 reader.Open("someData.bam", "someData.bam.bai");
104 // define our region of interest
105 // in this example: bases 0-500 on the reference "chrX"
106 int id = reader.GetReferenceID("chrX");
107 BamRegion region(id, 0, id, 500);
108 reader.SetRegion(region);
110 // iterate through alignments in this region,
111 // ignoring alignments with a MQ below some cutoff
113 while ( reader.GetNextAlignment(al) ) {
114 if ( al.MapQuality >= 50 )
121 To use this API in your application, you simply need to do 3 things:
123 1 - Drop the BamTools API files somewhere the compiler can find them.
125 2 - Import BamTools API with the following lines of code
126 #include "BamReader.h" // (or "BamMultiReader.h") as needed
127 #include "BamWriter.h" // as needed
128 using namespace BamTools; // all of BamTools classes/methods live in
131 3 - Link with '-lz' ('l' as in Lima) to access ZLIB compression library
132 (For MSVC users, I can provide you modified zlib headers - just contact
135 See any included programs and Makefile for more specific compiling/usage
136 examples. See comments in the header files for more detailed API documentation.
138 ----------------------------------------
140 ----------------------------------------
142 BamTools provides a small, but powerful suite of command-line utility programs
143 for manipulating and querying BAM files for data.
149 All BamTools utilities handle I/O operations using a common set of arguments.
154 The input BAM files(s).
156 If a tool accepts multiple BAM files as input, each file gets its own "-in"
157 option on the command line. If no "-in" is provided, the tool will attempt
158 to read BAM data from stdin.
160 To read a single BAM file, use a single "-in" option:
161 > bamtools *tool* -in myData1.bam ...ARGS...
163 To read multiple BAM files, use multiple "-in" options:
164 > bamtools *tool* -in myData1.bam -in myData2.bam ...ARGS...
166 To read from stdin (if supported), omit the "-in" option:
167 > bamtools *tool* ...ARGS...
173 If a tool outputs a result BAM file, specify the filename using this option.
174 If none is provided, the tool will typically write to stdout.
176 *Note: Not all tools output BAM data (e.g. count, header, etc.)
180 A region of interest. See below for accepted 'REGION string' formats.
182 Many of the tools accept this option, which allows a user to only consider
183 alignments that overlap this region (whether counting, filtering, merging,
186 An alignment is considered to overlap a region if any part of the alignments
187 intersects the left/right boundaries. Thus, a 50bp alignment at position 70
188 will overlap a region beginning at position 100.
191 ----------------------
192 A proper REGION string can be formatted like any of the following examples:
193 where 'chr1' is the name of a reference (not its ID)and '' is any valid
194 integer position within that reference.
197 chr1 - only alignments on (entire) reference 'chr1'
198 chr1:500 - only alignments overlapping the region starting at
199 chr1:500 and continuing to the end of chr1
200 chr1:500..1000 - only alignments overlapping the region starting at
201 chr1:500 and continuing to chr1:1000
202 chr1:500..chr3:750 - only alignments overlapping the region starting at
203 chr1:500 and continuing to chr3:750. This 'spanning'
204 region assumes that the reference specified as the
205 right boundary will occur somewhere in the file after
206 the left boundary. On a sorted BAM, a REGION of
207 'chr4:500..chr2:1500' will produce undefined
208 (incorrect) results. So don't do it. :)
210 *Note: Most of the tools that accept a REGION string will perform without an
211 index file, but typically at great cost to performance (having to
212 plow through the entire file until the region of interest is found).
213 For optimum speed, be sure that index files are available for your
218 Force compression of BAM output.
220 When tools are piped together (see details below), the default behavior is
221 to turn off compression. This can greatly increase performance when the data
222 does not have to be constantly decompressed and recompressed. This is
223 ignored any time an output BAM file is specified using "-out".
229 Many of the tools in BamTools can be chained together by piping. Any tool that
230 accepts stdin can be piped into, and any that can output stdout can be piped
233 > bamtools filter -in data1.bam -in data2.bam -mapQuality ">50" | bamtools count
235 will give a count of all alignments in your 2 BAM files with a mapQuality of
236 greater than 50. And of course, any tool writing to stdout can be piped into
243 convert Converts between BAM and a number of other formats
244 count Prints number of alignments in BAM file(s)
245 coverage Prints coverage statistics from the input BAM file
246 filter Filters BAM file(s) by user-specified criteria
247 header Prints BAM header information
248 index Generates index for BAM file
249 merge Merge multiple BAM files into single file
250 random Select random alignments from existing BAM file(s)
251 sort Sorts the BAM file according to some criteria
252 split Splits a BAM file on user-specifed property, creating a new
253 BAM output file for each value found
254 stats Prints some basic statistics from input BAM file(s)
260 Description: converts BAM to a number of other formats
262 Usage: bamtools convert -format <FORMAT> [-in <filename> -in <filename> ...]
263 [-out <filename>] [other options]
266 -in <BAM filename> the input BAM file(s) [stdin]
267 -out <BAM filename> the output BAM file [stdout]
268 -format <FORMAT> the output file format - see below for
272 -region <REGION> genomic region. Index file is recommended for
273 better performance, and is read
274 automatically if it exists. See 'bamtools
275 help index' for more details on creating
279 -fasta <FASTA filename> FASTA reference file
280 -mapqual print the mapping qualities
283 -noheader omit the SAM header from output
286 --help, -h shows this help text
290 - Currently supported output formats ( BAM -> X )
292 Format type FORMAT (command-line argument)
293 ------------ -------------------------------
303 > bamtools convert -format json -in myData.bam -out myData.json
305 - Pileup Options have no effect on formats other than "pileup"
306 SAM Options have no effect on formats other than "sam"
312 Description: prints number of alignments in BAM file(s).
314 Usage: bamtools count [-in <filename> -in <filename> ...] [-region <REGION>]
317 -in <BAM filename> the input BAM file(s) [stdin]
320 -region <REGION> genomic region. Index file is required and
321 is read automatically if it exists. See
322 'bamtools help index' for more details
326 --help, -h shows this help text
374 --------------------------------------------------------------------------------
376 --------------------------------------------------------------------------------
378 Both the BamTools API and toolkit are released under the MIT License.
379 Copyright (c) 2009-2010 Derek Barnett, Erik Garrison, Gabor Marth,
382 See included file LICENSE for details.
384 --------------------------------------------------------------------------------
385 IV. Acknowledgements :
386 --------------------------------------------------------------------------------
388 * Aaron Quinlan for several key feature ideas and bug fix contributions
389 * Baptiste Lepilleur for the public-domain JSON parser (JsonCPP)
390 * Heng Li, author of SAMtools - the original C-language BAM API/toolkit.
392 --------------------------------------------------------------------------------
394 --------------------------------------------------------------------------------
396 Feel free to contact me with any questions, comments, suggestions, bug reports,
401 Biology Dept., Boston College
403 Email: barnetde@bc.edu
404 Project Websites: http://github.com/pezmaster31/bamtools (ACTIVE SUPPORT)
405 http://sourceforge.net/projects/bamtools (major updates only)