This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extract read names from BAM files

Hello everyone, I need to extract the names of the reads in a BAM-file. The result should be a text-file with all the read names. I did not find any command in samtools or picard which would do this task. I am working with R, so it would be best to have something which is implementable in an R pipeline.

Can anyone help me?

rna-seq r

4 answers

Not a solution in R but this should do it:

samtools view your.bam | cut -f1 | sort | uniq > read_names

Thank you! Looks like it worked.

samtools view file.bam |cut -f 1 ?

I got this off of stack exchange; it's faster, because it doesn't sort

samtools view mine.bam | cut -f 1 | awk '!x[$0]++' > read.names.txt

Please add the link to Stack Exchange post.

If your bam file is very light, in R you can do :

library(Rsamtools)
bam <- scanBam("input.bam")
bam[[1]]$qname

Otherwise, command line answers are preferred

Log in to answer this question.