Skip to content

Commit d542468

Browse files
committed
Destroy the index *before* closing SAM/BAM/CRAM file
For CRAM files, indexing is done within HTSlib's cram_fd and the index's hts_idx_t is merely a shim pointing to the associated samFile. (See samtools/htslib@81b173e et al.) Hence for CRAM files in particular, it is vital to destroy an index *before* closing the associated samFile. Fixes #1169.
1 parent 3888168 commit d542468

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

pysam/libcalignmentfile.pyx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,13 +1665,13 @@ cdef class AlignmentFile(HTSFile):
16651665
if self.htsfile == NULL:
16661666
return
16671667

1668-
cdef int ret = hts_close(self.htsfile)
1669-
self.htsfile = NULL
1670-
16711668
if self.index != NULL:
16721669
hts_idx_destroy(self.index)
16731670
self.index = NULL
16741671

1672+
cdef int ret = hts_close(self.htsfile)
1673+
self.htsfile = NULL
1674+
16751675
self.header = None
16761676

16771677
if ret < 0:
@@ -1684,14 +1684,14 @@ cdef class AlignmentFile(HTSFile):
16841684
def __dealloc__(self):
16851685
cdef int ret = 0
16861686

1687-
if self.htsfile != NULL:
1688-
ret = hts_close(self.htsfile)
1689-
self.htsfile = NULL
1690-
16911687
if self.index != NULL:
16921688
hts_idx_destroy(self.index)
16931689
self.index = NULL
16941690

1691+
if self.htsfile != NULL:
1692+
ret = hts_close(self.htsfile)
1693+
self.htsfile = NULL
1694+
16951695
self.header = None
16961696

16971697
if self.b:
@@ -2046,8 +2046,8 @@ cdef class IteratorRow:
20462046
def __dealloc__(self):
20472047
bam_destroy1(self.b)
20482048
if self.owns_samfile:
2049-
hts_close(self.htsfile)
20502049
hts_idx_destroy(self.index)
2050+
hts_close(self.htsfile)
20512051

20522052

20532053
cdef class IteratorRowRegion(IteratorRow):

0 commit comments

Comments
 (0)