How to generate a bam file with reads of only a certain length?
I have certain bam files which have reads of different size. I wish to create another bam file from it which has reads smaller than a certain length N. I should be able to run samtools stats and idxstats like commands on it. Is there a way to do it?
• 1,598 views
•
link
1 answer
samtools view -e 'length(seq)<100' -O BAM -o out.bam in.bam
• 0 views
•
link
Log in to answer this question.
You can use
samtools view -h myfile.bam | awk 'length($10) > 30 || $1 ~ /^@/' | samtools view -bS - > out.bamfrom Ashutosh Pandey answer available here Filtration Of Reads With Length Lower Than 30 From BamWhere 30 is the minimum length
Hey, can range be used in this case? Like if I want it to be 20 < 'length($10) < 30? Or must I perform these actions separately? Much thanks for your previous answer
Sure;
samtools view -h myfile.bam | awk '(length($10) > 20 && length($10) < 30) || $1 ~ /^@/' | samtools view -bS - > out.bam