Class
BamStream
Class that provides an easy to use interface for reading and writing SAM and BAM files.
BamStream
Include Headers
seqan/bam_io.h
Data Members

bamIOContext | The BamIOContext object to use for reading and writing BamAlignmentRecords.
header | The BamHeader of the BamStream object.

Member Functions

BamStream | Constructor

Functions

atEnd | Check whether a BamStream object is at end when reading.
close | Close BamStream object's underlying file.
flush | Flush output when writing.
isGood | Check whether the BamStream object has is in the failure state.
open | Open a BamStream object for reading/writing.
readRecord | Read one BamAlignmentRecord from a BamStream.
reset | Reset BamStream object to status after construction.
writeRecord | Write one BamAlignmentRecord to a BamStream.

Examples
Read and write SAM or BAM files.
File "bam_stream.cpp"

1 | #include <iostream>
2 | #include <seqan/file.h>
3 | #include <seqan/bam_io.h>
4 |
5 | using namespace seqan;
6 |
7 | int main()
8 | {
9 | // Open input stream, BamStream can read SAM and BAM files.
10 | std::string pathSam = std::string(SEQAN_PATH_TO_ROOT()) + "/core/demos/bam_io/example.sam";
11 |
12 | BamStream bamStreamIn(pathSam.c_str());
13 | if (!isGood(bamStreamIn))
14 | {
15 | std::cerr << "Can't open the file." << std::endl;
16 | return 1;
17 | }
18 |
19 | // Open output stream. The value "-" means reading from stdin or writing to stdout.
20 | BamStream bamStreamOut("-", BamStream::WRITE);
21 | // Copy header. The header is automatically written out before the first record.
22 | bamStreamOut.header = bamStreamIn.header;
23 |
24 | // BamAlignmentRecord stores one record at a time.
25 | BamAlignmentRecord record;
26 | while (!atEnd(bamStreamIn))
27 | {
28 | if (readRecord(record, bamStreamIn) != 0)
29 | {
30 | std::cerr << "Can't read record!" << std::endl;
31 | return 1;
32 | }
33 | if (writeRecord(bamStreamOut, record) != 0)
34 | {
35 | std::cout << "Can't write record!" << std::endl;
36 | return 1;
37 | }
38 | }
39 | return 0;
40 | }
41 |

The output is as follows:
@HD VN:1.3 SO:coordinate
@SQ SN:ref LN:45
@SQ SN:ref2 LN:40
r001 163 ref 7 30 8M4I4M1D3M = 37 39 TTAGATAAAGAGGATACTG * XX:B:S,12561,2,20,112
r002 0 ref 9 30 1S2I6M1P1I1P1I4M2I * 0 0 AAAAGATAAGGGATAAA *
r003 0 ref 9 30 5H6M * 0 0 AGCTAA *
r004 0 ref 16 30 6M14N1I5M * 0 0 ATAGCTCTCAGC *
r003 16 ref 29 30 6H5M * 0 0 TAGGC *
r001 83 ref 37 30 9M = 7 -39 CAGCGCCAT *
SeqAn - Sequence Analysis Library - www.seqan.de

Page built @2014/10/13 16:12:56