I would use
grep -F -w -m1
Hello.
I have a question about Bam files. How can I extract the alignment of a specific named read from a Bam file? I did a search for Biostar and and found the following answer
Question: (Closed) Efficiently Extracting Reads With Specific Names ('Queryname') From .Bam File Efficiently Extracting Reads With Specific Names ('Queryname') From .Bam File
samtools view file.bam | grep queryname - > subset.sam
Yes. This is a simple and great answer, but since it's seven years old, I thought there might be a better way today. Is there a more efficient way to extract the Bam files? Especially when they are sorted by name?
(I would like to know how to extract by name and not by location.)
FilterSamReads https://broadinstitute.github.io/picard/command-line-overview.html#FilterSamReads with READ_LIST_FILE option as I said in C: Extracting Subsets Of Reads From A Bam File
If you are looking for a particular read group, you can filter your bam file directly:
samtools view -br RGTAG file.bam > file.RGTAG.bam
If you're looking for a particular read name, parsing the bam file with grep is still a viable way to do it. You can try to optimize it though, if only 1 read is expected:
samtools view file.bam | grep -m1 queryname - > subset.sam
You can try to optimize it even more, if only 1 read is expected and you more or less know the region (say chr3:1000-2000 as an example) where the read could have been mapped:
samtools view file.bam chr3:1000-2000 | grep -m1 queryname - > subset.sam
If you are able to go for this last option, the results should be generated immediately.
Log in to answer this question.