the problem still remains that the read name is not present
I can see the read name in column 4 of a file I just converted using bamtobed.
chrIV 462211 462361 M12345:768:000000000-JHFDF:1:2103:22666:7676 1:N:0:ACCGATTA+GCCGTGGC/1 45 +
chrIV 462211 462361 M12345:768:000000000-JHFDF:1:1109:21710:12608 1:N:0:ACCGATTA+GCCGTGGG/1 45 -
chrIV 462212 462362 M12345:768:000000000-JHFDF:1:1110:3933:9109 1:N:0:ACCGATTA+GCCGTGGC/1 45 +
chrIV 462212 462362 M12345:768:000000000-JHFDF:1:1113:17079:20757 1:N:0:ACCGATTA+GCCGTGGC/1 45 +
chrIV 462212 462362 M12345:768:000000000-JHFDF:1:2107:13009:28070 1:N:0:ACCGATTA+GCCGTGGC/2 45 +
chrIV 462215 462365 M12345:768:000000000-JHFDF:1:2101:9787:11405 1:N:0:ACCGATTA+GCCGTGGC/1 45 -
chrIV 462216 462366 M12345:768:000000000-JHFDF:1:1106:20844:11556 1:N:0:ACCGATTA+GCCGTGGC/1 45 +
So perhaps following would work.
$ bedtools bamtobed -i test_sorted.bam | awk -F "\t" '{OFS="\t"}{print $4,$1,$2,$3,$6}'
M12345:768:000000000-JHFDF:1:2112:13619:12220 1:N:0:ACCGATTA+GCCGTGGC/2 chrII 321102 321252 +
M12345:768:000000000-JHFDF:1:2110:13390:8727 1:N:0:ACCGATTA+GCCGTGGC/2 chrII 435977 436127 +
M12345:768:000000000-JHFDF:1:2114:21240:13267 1:N:0:ACCGATTA+GCCGTGGC/2 chrIV 132378 132528 -
Please read the SAM specifications. SAM/BAM is essentially a tab-delimited format, and the documentation will tell you in which columns you find the information. Convert BAM to SAM with
samtools viewand use any Unix tool (awk, sed, cut) to then select the relevant columns. Can be done via a pipe efficiently.the problem is the stop coordinate, it is not all that easy to get that information
it is one of those endemic bioinformatics things where a commonly needed information like start/stop of an alignment ought to be present ... but it isn't there
BTW I have asked the ChatGPT and lo behold it incorrectly claims that the following works:
but the answer is not correct,
the stop coordinate can only be computed by parsing the CIGAR string and moving the position for every
MandDoperator but not onS,HorIoperators