Failed when reading bam file
1
0
Entering edit mode
9.4 years ago
tianlq • 0

Hi all,

I need to read bam file in my project. I met a problem. According to sam file format, the first information of bam file is: char[4] which is BAM\1. But my output is ?. Can you tell my what is wrong of my codes? Thank you!

My codes (C++ or C) is here:

ifstream is(argv[1],ifstream::binary);
char magic[4];
is.read((char *)magic,sizeof(char)*4);
cout<<magic<<endl;

or

FILE *stream;
stream=fopen(argv[1],"r");
char magic[4];
fread(magic,sizeof(char),4,stream);
cout<<magic<<endl;
sequencing • 1.7k views
ADD COMMENT
1
Entering edit mode
gzFile fp = gzopen(argv[1], "r"); gzread(fp, magic, 4);
ADD REPLY
0
Entering edit mode

It works! Thank you so much!

ADD REPLY
0
Entering edit mode
9.4 years ago

Search google "parsing a bam file c++"

Here is a random post I found. It will help you to understand how to parse a bam file.

Get Bases And Quality From A Bam File [C++]

ADD COMMENT
0
Entering edit mode

Thank you! I know many people just includes codes from samtools or picard. But that needs includes many files. I just want to take position, sequence and error information from bamfile. I try to write very simple codes by myself.

I have read the bam format from http://samtools.github.io/hts-specs/SAMv1.pdf

I am confused why I can not read bam file correctly.

ADD REPLY

Login before adding your answer.

Traffic: 2015 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6