brentp / hts-nim

nim wrapper for htslib for parsing genomics data files

Home Page:https://brentp.github.io/hts-nim/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: changing BAM alignment records in place

andreas-wilm opened this issue · comments

Hi Brent,

Could you please confirm that there is no way to modify BAM alignment records in place? As far as I can see there is no way to change base qualities or sequences, correct? I am aware of set_qname (which looks complicated enough to implement!) and variations of hts_set_opt, but that's it right?

I recently had to write a tool that adjusts base qualities. For that I converted the existing record to a string, changed the base qualities and convert the resulting string back to a new record. Is there a better way to achieve this?

Many thanks,
Andreas

PS: Feel free to add the BQ mapping tool the to your list of example uses of hts-nim

Hi Andreas,
I added your tool ot the list. Nice work!
Since changing the base-qualities doesn't change the lenght of the record, I think you could do this using this code as example:
https://github.com/brentp/hts-nim/blob/master/src/hts/bam.nim#L131

so maybe (writing in github so this will have errors):

proc set_base_quality(r: Record, c: uint8, idx: int) {.inline.} =
    var bqual = bam_get_qual(r.b)
    bqual[idx] = c

# or all base-qualities:
proc set_base_qualities(r: Record, bqs: seq[uint8]) {.inline.} =
    var bqual = bam_get_qual(r.b)
    for i in 0..<len(bqs):
        bqual[i] = bqs[i]

Oh I didn't realize bam_get_qual() returns a reference. Very nice! Thanks a lot!

And if I read the code correctly, then I could also mess with the sequence itself (as long as I keep the same length) via the reference returned by bam_get_seq(), right?

Great! Thanks a lot

And if I read the code correctly, then I could also mess with the sequence itself (as long as I keep the same length) via the reference returned by bam_get_seq(), right?

Yes, but note that the sequence is encoded. So you'll have to encode before setting.

Thanks a million Brent!