Search code examples
bioinformaticssamtools

How to use SAMtool htslib library to read optional info fields


I have a BAM file:

ERR174327.487900        99      chr9    80320323        60      101M    =       80320752        530     AGGGACATTGGTCCAAAAGGTTTTAATTAACCATACACCCTGCTCTACAAATCTAAAAAACTGTAGGACAGTATTTTGAGTCTCCAAGTATCCAGTGATAA   CCCFFFFFHHHHFIJJJJJJGIJJJJJJJJJJJJJJJJIJIJJJJJIIJJJIJIJJJJJJJGICEHIHFHH=BFFADFFDCCCDCDDDCCCDADC@>ADC@   NM:i:0  AS:i:101        XS:i:0  RG:Z:group1

I need to read the optional fields NM:i:0 AS:i:101 XS:i:0 RG:Z:group1 in my C++ code. I am able to use the htslib library to read everything but those optional fields.

The source file for the htsib library is here on Github. Unfortunately, I am not able to find a function that can read those fields.

Q: How to read the optional fields with htslib?


Solution

  • in bam.h

    /*!
      @abstract       Retrieve data of a tag
      @param  b       pointer to an alignment struct
      @param  tag     two-character tag to be retrieved
    
      @return  pointer to the type and data. The first character is the
      type that can be 'iIsScCdfAZH'.
     */
    
     static inline uint8_t *bam_aux_get_core(bam1_t *b, const char tag[2]);