1
$\begingroup$

I would like to read iteratively the same BAM file twice using htslib.

My option is to use hts_open, sam_hdr_read, then sam_read1 as much as needed to read the file until EOF.

Is there a possibility to "rewind", and use sam_read1 again from the beginning of the file, without closing the file, destroying the object, and opening it again?


EDIT

My aim is group the reads by the barcode. The first pass would allocate the specific sizes first (the number of reads by barcode), then store the reads.

I know I could sort the reads first (using samtools sort -t BX), but I would like to skip this step using pre-allocation.

$\endgroup$
1
  • 1
    $\begingroup$ @M__ Thanks, I edited the question. I hope it helps! $\endgroup$ Jun 6 at 14:48

1 Answer 1

1
$\begingroup$

The best solution is to reopen the file. For a coordinate sorted file you have the option to jump back to the start of the first chromosome with index but that is more complex, less flexible and won't be faster.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.