This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extract Alignment By Read Id From A Sam File

Hi,

Is there a rapid way to extract alignment from a sam file using read ids (about~100 read ids in average). If the read ids are in a file (one per line), I could do :

cat in.sam | grep -f idFile.txt > out.sam

but with a big sam file (~40Gb) it takes a lot of time.... so is there maybe a method to extract these alignments faster ?

Thanks,

N.

sam read id

1 answer

faster ?

 LC_ALL=C grep -w -F -f idFile.txt  < in.sam > subset.sam

amazingly simple!!!! thanks so much

thanks so much for this! any suggestion on how to also keep the sam header in the output subset.sam file?

Capture the header (Lines starting with ^@). Add to the new subset.sam file.

Log in to answer this question.