This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Converting bam to sam using htslib C++ api

Hi everyone, is it possible to convert between sam and bam file inside C++ code. I knowhtslib library offers implementation of file formats including sam and bam. However, I could not find a documentation describing this conversion. I know samtools view command can be used for this purpose but I need to deal with this inside C++ code.

bam htslib sam

Don't forget to follow up on your threads. If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one answer if they all work. If an answer was not really helpful or did not work, provide detailed feedback so others know not to use that answer.

Upvote|Bookmark|Accept

Extract bam file to sam inside code?

GAM file columns?

1 answer

read https://github.com/samtools/samtools/blob/develop/sam_view.c

  • allocate a new bam1_t* b = bam_init1
  • open the input file: sam_open_format
  • read the header sam_hdr_read
  • open the output file: sam_open_format
  • write the header: sam_hdr_str
  • while you can read a bam1_t : sam_read1
  • write the read sam_format1
  • dispose header sam_hdr_destroy
  • dispose read bam_destroy1
  • dispose files: sam_close

Log in to answer this question.