pysam alignment error
Hi,
I am trying to read bam file using a command: samfile = pysam.AlignmentFile(os.path.join(path,bam),'rb') but I keep on getting an error:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "pysam/libcalignmentfile.pyx", line 741, in pysam.libcalignmentfile.AlignmentFile.__cinit__
File "pysam/libcalignmentfile.pyx", line 990, in pysam.libcalignmentfile.AlignmentFile._open
ValueError: file has no sequences defined (mode='rb') - is it SAM/BAM format? Consider opening with check_sq=False
I checked my bam files and they look fine. All of my bam files have headers too. Could anyone provide any suggestion?
Thanks
• 8,062 views
•
link
2 answers
I have not used pysam before but have you tried the suggestion the code gives samfile = pysam.AlignmentFile(os.path.join(path,bam), check_sq=False, 'rb') ? also are you using Windows to process the file the rb call is for windows, linux/mac would be just r
• 0 views
•
link
A similar error on github: https://github.com/pysam-developers/pysam/issues/442
Maybe updating the version of your pysam can fix the problem?
• 0 views
•
link
Log in to answer this question.
How did you check and how did you conclude the files are fine?
I used samtools flagstat to see if the alignment is okay and I got the output. It didn't throw me any error,
Hello,
can you use
samtoolsto have a look into the the bam file?Or does this give you an error as well?
fin swimmer
When I used samtools view -h input.bam - I get the following output,
Ok,
just another guess: Are you sure you have a bam and not a sam file? What's the output of
head input.bam?fin swimmmer
The output of head .bam looks like this,
This is not a bam file. It is a sam file.
Just remove the
'rb'part. Because this opens the file in binary mode, which you don't have.I used samtools view -h input.sam > output.bam to convert the sam file to bam file. I do not know why it didn't convert to bam file. As you suggested I opened the file removing 'rb',
But still getting the following error for
The
fetch()method doesn't allow random access. Convert to bam and coordinate sort your sam file:The standard output by samtools is sam. So this command doesn't convert to bam. You have to use the parameter
-Obto get this. Or instead of redirecting to a file (> output.bam), use the-oparameter to define the output file.samtoolsrecognizes the file extension, and will convert.thanks a lot, it finally worked.
I am not sure if updating the newest version of pysam solved this issue. After updating the version, it is not throwing me the error. But I am getting other error while counting in the sam file, samfile.count(mychr, start, stop).