Question: How To Interpret The Flag Field In The Sam Specification
1
6.1 years ago by

Hi all ,

I created a sam file and converted it to bam file using samtools.

One of the record in bam file is :

``````HWI-ST1162:72:D0PMMACXX:5:1104:10496:88597      16      gi|355477192|ref|NC_007299.5|   1       255     101M    *       0       0       TACCCCACTCACACTTATGGATAGATCAACTAAACAGAAAATTAACAAGGAAAAAAAAAACAAAAAAAACATGTATCTATAAAGCTCACTAAAGGAAAGCA        05DDDDCDDCDDDDDDDDDDDCDCCADDDCDDDEDDEDEEEEDDDDDDDDDDHJJJJJJIJJJJJJJJIIJJJJJIJGJJJJJJIIHGHHHHHFFFFFCCC        XA:i:1  MD:Z:60A33C6    RG:Z:D0PMMACXX:IT27.48-3        NM:i:2
``````

What does the second field means? What may be the possible values for second field of a bam file. In one paper I read it as a flag. But I could not find the explanation for value 0 and 16 .

Thanks, Deeps

bam • 19k views
modified 3.1 years ago by -_-680 • written 6.1 years ago by deepthithomaskannan230

Answer is fine but it is not working so sorry

What answer isn't working? Istvan's link to the "Explain SAM Flags" still works, as does that site.

15
6.1 years ago by
Istvan Albert ♦♦ 77k
University Park, USA
Istvan Albert ♦♦ 77k wrote:

In a nutshell any integer number may be entered although some of these number may have contradictory or overlapping effects.

The best way to visualize the flag fields is via Explain SAM Flags web interface.

To also answer your question each flag corresponds to a bit set in a binary representation of the number

``````1 -> 00001
2 -> 00010
4 -> 00100
8 -> 01000
``````

Then if you need to set flags 1 and 4 then the bits that will be set are `00101` and the integer flag would be `5`

great tool the "Explain SAM Flags"!, I expended A LOT of time understanding the combinations

4
4.9 years ago by
swbarnes24.1k
United States
swbarnes24.1k wrote:

I think some of the confusion happens because we call them binary flags, but they are displayed in decimal in the .sam file, but when they are explained in the SAM specification document, they are displayed in hexadecimal.

3
6.1 years ago by
Damian Kao15k
USA
Damian Kao15k wrote:

It might be more detail than you need to know about how bitwise flags work.

excellent summary - your blog was not listed in the planet feed - problem rectified!

3
3.1 years ago by
-_-680
-_-680 wrote:

For even faster and more handy interpretation of the flag, try http://www.samformat.info/#/flag

this is neat but IMO it should work both ways, selecting rows should produce numbers.

building a number out of the desired flags is just as often needed

Agreed, added that feature, too and improved the binary illustration.

nice, I'll add this to my lecture notes ;-)

2
6.1 years ago by
JC6.8k
Mexico
JC6.8k wrote:

In short: 0 is a single-end read mapped in the forward strand, 16 is in the reverse strand. Check all the details about the bitwise flag in the samtools manual: http://samtools.sourceforge.net/samtools.shtml and SAM format description: http://samtools.sourceforge.net/SAM1.pdf