This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Is there an R function that transforms a decimal SAM FLAG into a list of strings?

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 answers

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

library(Rsamtools)

bamFlagAsBitMatrix( as.integer (1024))

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

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

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, ...)

Log in to answer this question.