@@ -202,7 +202,11 @@ from cpython.bytes cimport PyBytes_FromStringAndSize
202202from cpython.unicode cimport PyUnicode_DecodeASCII
203203from cpython.version cimport PY_MAJOR_VERSION
204204
205- from pysam.chtslib cimport hisremote
205+ from pysam.chtslib cimport hisremote
206+
207+
208+ from warnings import warn
209+
206210
207211__all__ = [' VariantFile' ,
208212 ' VariantHeader' ,
@@ -3412,7 +3416,7 @@ cdef class VariantFile(object):
34123416
34133417 return vars
34143418
3415- def open (self , filename , mode = None ,
3419+ def open (self , filename , mode = ' rb ' ,
34163420 index_filename = None ,
34173421 VariantHeader header = None ,
34183422 drop_samples = False ):
@@ -3422,6 +3426,7 @@ cdef class VariantFile(object):
34223426 closed and a new file will be opened.
34233427 """
34243428 cdef bcf_hdr_t * hdr
3429+ cdef BGZF * bgzfp
34253430 cdef hts_idx_t * idx
34263431 cdef tbx_t * tidx
34273432 cdef char * cfilename
@@ -3432,17 +3437,6 @@ cdef class VariantFile(object):
34323437 if self .is_open:
34333438 self .close()
34343439
3435- # read mode autodetection
3436- if mode is None :
3437- try :
3438- self .open(filename, ' rb' , header = header)
3439- return
3440- except ValueError , msg:
3441- pass
3442-
3443- self .open(filename, ' r' , header = header)
3444- return
3445-
34463440 if mode not in (' r' ,' w' ,' rb' ,' wb' , ' wh' , ' wbu' , ' rU' , ' wb0' ):
34473441 raise ValueError (' invalid file opening mode `{}`' .format(mode))
34483442
@@ -3481,42 +3475,38 @@ cdef class VariantFile(object):
34813475 self .htsfile = hts_open(cfilename, cmode)
34823476
34833477 if not self .htsfile:
3484- raise ValueError (
3485- " could not open file `{}` (mode='{}')" .format(
3486- (filename, mode)))
3478+ raise ValueError (" could not open file `{}` (mode='{}')" .format((filename, mode)))
34873479
34883480 with nogil:
34893481 bcf_hdr_write(self .htsfile, self .header.ptr)
34903482
34913483 elif mode.startswith(b' r' ):
34923484 # open file for reading
3493- if filename != b' -' and not self .is_remote \
3494- and not os.path.exists(filename):
3485+ if filename != b' -' and not self .is_remote and not os.path.exists(filename):
34953486 raise IOError (' file `{}` not found' .format(filename))
34963487
34973488 cfilename, cmode = filename, mode
34983489 with nogil:
34993490 self .htsfile = hts_open(cfilename, cmode)
35003491
35013492 if not self .htsfile:
3502- raise ValueError (
3503- " could not open file `{}` (mode='{}') - "
3504- " is it VCF/BCF format?" .format(filename, mode))
3493+ raise ValueError (" could not open file `{}` (mode='{}') - is it VCF/BCF format?" .format(filename, mode))
35053494
35063495 if self .htsfile.format.format not in (bcf, vcf):
3507- raise ValueError (
3508- " invalid file `{}` (mode='{}') - "
3509- " is it VCF/BCF format?" .format(filename, mode))
3496+ raise ValueError (" invalid file `{}` (mode='{}') - is it VCF/BCF format?" .format(filename, mode))
3497+
3498+ if self .htsfile.format.compression == bgzf:
3499+ bgzfp = hts_get_bgzfp(self .htsfile)
3500+ if bgzfp and bgzf_check_EOF(bgzfp) == 0 :
3501+ warn(' [%s ] Warning: no BGZF EOF marker; file may be truncated' .format(filename))
35103502
35113503 with nogil:
35123504 hdr = bcf_hdr_read(self .htsfile)
35133505
35143506 try :
35153507 self .header = makeVariantHeader(hdr)
35163508 except ValueError :
3517- raise ValueError (
3518- " file `{}` does not have valid header (mode='{}') - "
3519- " is it VCF/BCF format?" .format(filename, mode))
3509+ raise ValueError (" file `{}` does not have valid header (mode='{}') - is it VCF/BCF format?" .format(filename, mode))
35203510
35213511 # check for index and open if present
35223512 if self .htsfile.format.format == bcf:
0 commit comments