This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Adding annotations to the bam file from the txt file

Dear all,

I have bam file (bamfile.bam) where I need to add a flag and annotation from text file.

The bam file looks like this:

NS500688:631:HFLVJBGXF:1:11105:17585:2382       4       *       0       0       *       *       0       0       GTTGTGAAACATCTCAGTACCCACAGGAAGA //A///EEEEEAEEEE//EEA<EAEEEEEAE B0:Z:AGTTCCCGCATAGTGCC       B3:Z:ATCTTTA    RG:Z:0  B0:Z:AGTTCCCGCATAGTGCCG B1:Z:4  B2:Z:18

Then I have in a txt file (annotations.txt) all the annotations.

NS500688:631:HFLVJBGXF:1:11105:17585:2382       TRINITY_DN3616_c0_g2_i2: 121: 0: 31M`

I want to add the annotation to the bam file with the XF flag so that it will look like this:

NS500688:631:HFLVJBGXF:1:11105:17585:2382       4       *       0       0       *       *       0       0       GTTGTGAAACATCTCAGTACCCACAGGAAGA //A///EEEEEAEEEE//EEA<EAEEEEEAE B0:Z:AGTTCCCGCATAGTGCC       B3:Z:ATCTTTA    RG:Z:0  B0:Z:AGTTCCCGCATAGTGCCG B1:Z:4  B2:Z:18  XF:Z:TRINITY_DN3616_c0_g2_i2: 121: 0: 31M`

Basically each row in bam file should have the XF:Z: + annotation included after the correct read. What would be the best way to do this?

Thanks a lot!

rna-seq sequence bam samtools

I'm not sure those spaces in TRINITY_DN3616_c0_g2_i2: 121: 0: 31M are valid.

1 answer

"best" is subjective. In python I would read in the the annotations per name into a dict, then iterate through the bam file with pysam adding the annotation based on the read name writing each one out into a new bam file

Log in to answer this question.