This is a test version of Biostars. For the public version, visit https://www.biostars.org.
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?

samtools

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

1 answer

samtools view -e 'length(seq)<100'  -O BAM -o out.bam in.bam

Log in to answer this question.