
What is causing the error?
This error is caused when bedtools can’t handle the order that your reads are in. If you’ve just downloaded the bam from the tcga and haven’t checked, it is most likely that your bam file is sorted by chromosomal location. You can check the order by using the command:
samtools view -H your.bam | grep "@HD"
#the -H flag only reads the header so this is not #computationally intensive
This will output something like
@HD VN:1.6 SO:coordinate
Or
@HD VN:1.6 SO:queryname
If you get SO:coordinate, you will need to sort your file using the code below, if it reads SO:queryname, skip to the next section
#to sort your bamfile, you can use the following code
samtools sort -n -T "${your_unique_id}" your.bam > your_namesort.bam"
# -n specifies to sort by name, and -T allows you to specify a string to add to
# the start which is useful for parallel working
# the unique id can be anything you like, but the original filename is common
With a sorted bamfile your errors should hopefully go away
But Dr Prufstein, I already sorted my bam by readname!
The error is likely being caused by duplicate or orphaned reads in your bam file. You can search for these reads using the following commands;
What to check | How to check it |
If your BAM file contains only paired-end reads. | samtools flagstat |
If any reads are missing mates. | samtools view -f 1 -F 2 |
If your BAM file has duplicate read names. | samtools view -n 1000 > outputcheck.txt |
If you find erroneous reads, you can filter these out using
samtools view -bf 1 your_namesort.bam > filtered_paired.bam
#
I’m still having issues!
I’ve found issues with this when using bedtools bamtofastq
. I’m not sure why, but I’ve had much better luck using the samtools equivalent samtools fastq
. Please see below for my equivalencies.
Bedtools bamtofastq
bedtools bamtofastq -i
\your_namesort.bam
-fq your_1.fastq
\-fq2 your_2.fastq
samtools fastq
samtools fastq -n \
-1
\your_1.fastq
-2 ./
\your_2.fastq
your_namesort.bam
I hope that these tips help to sort your issues!
–Prufstein