simon-anders / htseq

HTSeq is a Python library to facilitate processing and analysis of data from high-throughput sequencing (HTS) experiments.
https://htseq.readthedocs.io/en/release_0.11.1/
GNU General Public License v3.0
122 stars 77 forks source link

Pysam compatibility issue #4

Closed kriemo closed 7 years ago

kriemo commented 8 years ago

I recently upgraded to Pysam v. 0.9.0 and now HTSeq fails to correctly search for alignments within a genomicInterval from a BAM_Reader object.

Here's the error code and an example.

inbam = HTSeq.BAM_Reader("test.bam")
window = HTSeq.GenomicInterval("chrM", 9904, 9926, "-")
>>> window
<GenomicInterval object 'chrM', [9904,9926), strand '-'>
>>> for almt in inbam[window]:
...     print almt
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/HTSeq-0.6.1-py2.7-macosx-10.10-intel.egg/HTSeq/__init__.py", line 976, in __getitem__
    if not self.sf._hasIndex():
AttributeError: 'pysam.csamfile.Samfile' object has no attribute '_hasIndex'

I tracked down the _hasIndex function in pysam and the problem is that the _hasIndex function was renamed to has_index in the calignmentfile.pyx. This change happened in pysam version 0.8.4 I believe.

I edited the HTSeq/__init__.py script line 977

if not self.sf._hasIndex():
              raise ValueError, "The .bam-file has no index, random-access is disabled!"

to

if not self.sf.has_index():
              raise ValueError, "The .bam-file has no index, random-access is disabled!"

reinstalled HTSeq and then the error went away and my scripts ran normally again. I issued a pull request which fixes this error.

yhoogstrate commented 8 years ago

I think if such a change would be included, it also requires an update to setup.py by including something like install_requires(['pysam>=0.9'], otherwise people with pysam 0.8.* that now have a working version of htseq suddenly become incompatible after upgrading htseq only. Maybe even better try to use try: and except: to make it compatible with both? Could you add the change and make a pull request? If @simon-anders accepts your change you will then get the credits for it as it will be linked to your account and you're pretty close already :)

iosonofabio commented 7 years ago

we are working on pysam 0.9+ compatibility. for now we have a version that works with pysam 0.9 and python3 but not python2: check out the python3 branch.

Not sure whether pysam 0.9+ compatibility will be backported to python2 anytime soon.

iosonofabio commented 7 years ago

@kriemo: Pull requests #14 and #15 make htseq compatible with pysam 0.9+ for both python2 and python3. Support for pysam<=0.8.x will probably drop because of their API changes. You can try the code in those PRs.