Is there an R function that transforms a decimal SAM FLAG into a list of strings?
3
1
Entering edit mode
7.2 years ago
biomagician ▴ 410

Hi,

Does anybody know of an R function that takes a decimal number as input, the FLAG number from a SAM file, and outputs a list of text stating whether the read has multiple alignments, is properly aligned, is mapped, etc.?

It should not be too hard to code, but if it is already present in some Bioconductor package, I would rather use that.

Thanks.

Best, C.

R SAM FLAG alignment format bam • 3.0k views
ADD COMMENT
2
Entering edit mode
7.2 years ago

https://rdrr.io/github/jefferys/SamSeq/

> library(SamSeq)
> samFlags(1024)
SUPPLEMENTARY_ALIGNMENT          DUPLICATE_READ    READ_FAILS_VENDOR_QC   NOT_PRIMARY_ALIGNMENT 
                  FALSE                    TRUE                   FALSE                   FALSE 
         SECOND_OF_PAIR           FIRST_OF_PAIR     MATE_REVERSE_STRAND     READ_REVERSE_STRAND 
                  FALSE                   FALSE                   FALSE                   FALSE 
          MATE_UNMAPPED           READ_UNMAPPED             PROPER_PAIR             READ_PAIRED 
                  FALSE                   FALSE                   FALSE                   FALSE 

> samFlags(228)[samFlags(228)==1]
     SECOND_OF_PAIR       FIRST_OF_PAIR MATE_REVERSE_STRAND       READ_UNMAPPED 
               TRUE                TRUE                TRUE                TRUE 
> samFlags(2056)[samFlags(2056)==1]
SUPPLEMENTARY_ALIGNMENT           MATE_UNMAPPED 
                   TRUE                    TRUE 
> samFlags(128)[samFlags(128)==1]
SECOND_OF_PAIR 
          TRUE

Reverse flag search:

> samFlags("SUPPLEMENTARY_ALIGNMENT")
[1] 2048
> samFlags(c("SUPPLEMENTARY_ALIGNMENT","MATE_UNMAPPED"))
[1] 2056
ADD COMMENT
1
Entering edit mode
6.5 years ago

library(Rsamtools)

bamFlagAsBitMatrix( as.integer (1024))

FLAG_BITNAMES[ as.logical( bamFlagAsBitMatrix( as.integer(1024)))]

ADD COMMENT
0
Entering edit mode

Tip of the day: to save typing, declare integer type with the L suffix as in 1024L, instead of as.integer(1024).

ADD REPLY
0
Entering edit mode
7.2 years ago
VHahaut ★ 1.2k

https://www.rdocumentation.org/packages/Rsamtools/versions/1.24.0/topics/ScanBamParam

This doesn't answer 100% your question but ScanBamParam() / ScanBamFlag can parse a SAM file and extract reads with certain features (isProperPair, isPaired, ...)

ADD COMMENT

Login before adding your answer.

Traffic: 1594 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