# Change record sequence in a BAM file using XAM

**URL:** https://discourse.julialang.org/t/change-record-sequence-in-a-bam-file-using-xam/98141
**Category:** Biology, Health, and Medicine
**Created:** [April 30, 2023, 7:35pm UTC](https://discourse.julialang.org/t/change-record-sequence-in-a-bam-file-using-xam/98141 "2023-04-30T19:35:03Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ForestRaven](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@ForestRaven](https://discourse.julialang.org/u/ForestRaven)
#### Post date: [April 30, 2023, 7:35pm UTC](https://discourse.julialang.org/t/change-record-sequence-in-a-bam-file-using-xam/98141/1 "2023-04-30T19:35:03Z")

</div>

Hi,

I would like to update the sequence of a list of records in a BAM file. Looking at the code of record.jl, data is stored as Vector{UInt64} and some dark magic is used to convert to a DNA sequence.

As it looks very low level, how can I change the sequence ?

Thanks,

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [May 9, 2023, 9:47am UTC](https://discourse.julialang.org/t/change-record-sequence-in-a-bam-file-using-xam/98141/2 "2023-05-09T09:47:28Z")

</div>

I don’t think it’s possible at the moment, you would have to write a function that does the inverse of BAM.sequence basically :

> <https://github.com/BioJulia/XAM.jl/blob/develop/src/bam/record.jl#L456>

Maybe a workaround would be to write back to a SAM file and then convert to BAM with samtools. Looks like there’s string constructor for SAM :

> <https://github.com/BioJulia/XAM.jl/blob/develop/test/test_sam.jl#L53>

---

<div class="post-metadata">

### Author: ![ForestRaven](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@ForestRaven](https://discourse.julialang.org/u/ForestRaven)
#### Post date: [June 4, 2023, 8:19am UTC](https://discourse.julialang.org/t/change-record-sequence-in-a-bam-file-using-xam/98141/3 "2023-06-04T08:19:05Z")

</div>

Following your suggestion, I managed to make it work with:

```julia
function updateRecord(seq::BioSequences.LongDNA{4}, record::BAM.Record)
    seqlen = BAM.seqlength(record)
    src::Ptr{UInt64} = pointer(record.data, BAM.seqname_length(record) + BAM.n_cigar_op(record, false) * 4 + 1)
    for i in 1:length(seq.data)
        # Flip high and low nibble
        x = (seq.data[i] & 0x0f0f0f0f0f0f0f0f) << 4 | (seq.data[i] & 0xf0f0f0f0f0f0f0f0) >> 4
        unsafe_store!(src, x, i)
    end
end

```

Performances are good ! However, this only works for sequence substitution and not deletion/insertion.
