Thank you. Do you know of any strategies for non-coordinated-sorted bam files?
To get the QNAME field from the last read in a bam file, I do the following:
samtools view "${bam}" | tail -1 | cut -f1
However, this takes quite a long time for larger bam files (for example, I am working with bam files in the range of 40–50 GB). Does anyone know of a faster and perhaps less resource intensive way to do this?
1 answer
get the last chromosome of the bam using samtools idxstat in.bam and then use the index:
samtools view in.bam <NAME_OF_LAST_CHROM>| tail -1
If these are unsorted files, why would you need the last read? Couldn't you pick a random read or the first one?
Thanks, they are queryname-sorted files.
I wrote a small Python script that uses pysam to write a bam outfile from a bam infile (queryname-sorted) filtered to exclude all reads in a txt list of querynames (reverse queryname-sorted). That is, if a given queryname is in the list, then it is not written to the bam outfile.
how is it related to your original question ?
Quick way to get a field such as QNAME from the last read in a bam file?
furthermore you re-invented https://gatk.broadinstitute.org/hc/en-us/articles/360036882611-FilterSamReads-Picard- + READ_LIST_FILE
Thank you and apologies. To keep the discussion on point, I removed the non-relevant information.
Answering the question from Friederike: I use the queryname of the last read to break a while loop in a script that filters a bam file to exclude reads with querynames that match those in a user-supplied list of querynames.
Log in to answer this question.