This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Bam File: Extract Chromosome Number And Start Position Reads

Dear All,

I am working with BAM files for chip-seq analysis. For each read in the BAM file, I want to extractt the chromosome number, the start position and the stop position and replace that in a new file. Is that possible?

Many thanks!

bam samtools

3 answers

samtools view bamfile.bam|awk '{print $3 "\t" $4 "\t" $4+length($10)-1}' > newfile.tab

will do the job. The stop position here is the last matching position.

Thanks to Farhat, that works fine for me! :)

this approach has a minor issue that the length of the sequence does not necessarily agree with the span of the alignment, e.g. indels

bedtools' bamToBed inspects the CIGAR string when computing the end coordinate, so deletions are properly handled. the example here assumes that only substitutions can occur,

True. bamToBed would be the proper tool to handle something like this.

Yes it is pssible, using e.g. SAMtools samtools view or Rsamtools.

The reference sequence name is in column 3 of a SAM file, the (leftmost) start in column 4 and the end position needs to be calculated using the CIGAR string (e.g. start + alignment length).

In this case how is strand information treated? Is it always start + alignment length or does it depends on the strand where the read mapped?

You could also try BEDTools, which can convert your .bam to a .bed, and bed pretty much is a name, a chromosome, a start and a stop position.

Log in to answer this question.