pysam-developers / pysam

Pysam is a Python package for reading, manipulating, and writing genomics data such as SAM/BAM/CRAM and VCF/BCF files. It's a lightweight wrapper of the HTSlib API, the same one that powers samtools, bcftools, and tabix.
https://pysam.readthedocs.io/en/latest/
MIT License
774 stars 274 forks source link

pysam.AlignmentFile: _open() takes at least 1 positional argument (0 given) #182

Closed karel-brinda closed 8 years ago

karel-brinda commented 8 years ago

After upgrade to the newest version of PySam, I am getting the following error in my code:

TypeError in line 52 of /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/rnftools/mishmash/mishmash.snake:
_open() takes at least 1 positional argument (0 given)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/rnftools/mishmash/ArtIllumina.py", line 153, in create_fq
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/rnftools/mishmash/Source.py", line 197, in recode_sam_reads

It corresponds to the following Python lines:

with pysam.AlignmentFile(
                    filename=sam_fn,
                    check_header=False,
                ) as samfile:

The code used to work before the last update of PySam. I tried to Google a little bit and a similar problem was reported here in the past: http://www.biopython.org/pipermail/biopython/2011-April/007208.html

It might be somehow related because my code is executed within Snakemake (https://bitbucket.org/johanneskoester/snakemake/), which also uses multiprocessing.

AndreasHeger commented 8 years ago

Thanks, that is interesting, I am not sure why the new version would cause this. I would be happy to have a look at this, do you have a complete example?

Best wishes, Andreas

karel-brinda commented 8 years ago

Here it is. Please, install rnftools using pip3 install --upgrade rnftools (Python 3.4+ is required for it). Then create some testing directory and create there a file called Snakefile with the following content:

import rnftools
import smbl

rnftools.mishmash.sample("non_working_example",reads_in_tuple=2)

rnftools.mishmash.ArtIllumina(
    fasta=smbl.fasta.EXAMPLE,
    read_length_1=100,
    read_length_2=100,
    coverage=0.5,
    distance=500,
    distance_deviation=50.0,
    rng_seed=12345,
)

include: rnftools.include()
rule: input: rnftools.input()

Now execute snakemake in this directory (Snakemake is installed together with RNFtools). You will get something like this:

On-y-go:tmp karel$ snakemake
Multiple include of /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/smbl/include_all.snake ignored
Provided cores: 1
Rules claiming more threads will be scaled down.
Job counts:
    count   jobs
    1   42
    1   43
    1   45
    3
rule 43:
    input: /Users/karel/.smbl/bin/art_illumina, /Users/karel/.smbl/bin/samtools, /Users/karel/.smbl/fa/Mycobacterium_tuberculosis.fa, /Users/karel/.smbl/fa/Mycobacterium_tuberculosis.fa.fai
    output: non_working_example/001/_final_reads.fq, non_working_example/001/tmp.1.sam, non_working_example/001/tmp.1.corrected.sam, non_working_example/001/tmp.11.fq, non_working_example/001/tmp.12.fq
Error in job 43 while creating output files non_working_example/001/_final_reads.fq, non_working_example/001/tmp.1.sam, non_working_example/001/tmp.1.corrected.sam, non_working_example/001/tmp.11.fq, non_working_example/001/tmp.12.fq.
RuleException:
TypeError in line 52 of /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/rnftools/mishmash/mishmash.snake:
_open() takes at least 1 positional argument (0 given)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/rnftools/mishmash/ArtIllumina.py", line 153, in create_fq
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/rnftools/mishmash/Source.py", line 197, in recode_sam_reads
Removing output files of failed job 43 since they might be corrupted:
non_working_example/001/_final_reads.fq, non_working_example/001/tmp.1.sam, non_working_example/001/tmp.1.corrected.sam, non_working_example/001/tmp.11.fq, non_working_example/001/tmp.12.fq
Will exit after finishing currently running jobs.
Exiting because a job execution failed. Look above for error message
AndreasHeger commented 8 years ago

Thanks, I can reproduce the problem. Will get back shortly.

Best wishes, Andreas

AndreasHeger commented 8 years ago

Hi Karel,

unfortunately you fell victim to another backwards incompatibility. I renamed the "filename" parameter to "filepath_or_object" to reflect that it is now possible to open a Samfile from an existing file object. This was really a change for the sake of the documentation as I had assumed that most users would simply provide the filename as the first argument.

I suggest to change:

                with pysam.AlignmentFile(
                                        filename=sam_fn,
                                        check_header=False,
                                ) as samfile:

to

                with pysam.AlignmentFile(
                                        sam_fn,
                                        check_header=False,
                                ) as samfile:

to

However, I will also add filename back for backwards compatibility. Best wishes, Andreas