@@ -69,6 +69,7 @@ typedef struct
69
69
{
70
70
sr_sort_t sort ;
71
71
int regions_overlap , targets_overlap ;
72
+ int * closefile ; // close htsfile with sync reader close or not
72
73
}
73
74
aux_t ;
74
75
@@ -251,13 +252,28 @@ void bcf_sr_destroy_threads(bcf_srs_t *files) {
251
252
int bcf_sr_add_reader (bcf_srs_t * files , const char * fname )
252
253
{
253
254
char fmode [5 ];
255
+ int ret = 0 ;
256
+
254
257
strcpy (fmode , "r" );
255
258
vcf_open_mode (fmode + 1 , fname , NULL );
256
259
htsFile * file_ptr = hts_open (fname , fmode );
257
260
if ( ! file_ptr ) {
258
261
files -> errnum = open_failed ;
259
262
return 0 ;
260
263
}
264
+ if (!(ret = bcf_sr_add_hreader (files , file_ptr , 1 , fname ))) {
265
+ hts_close (file_ptr ); //failed, close the file
266
+ }
267
+ return ret ;
268
+ }
269
+
270
+ int bcf_sr_add_hreader (bcf_srs_t * files , htsFile * file_ptr , int autoclose , const char * fname )
271
+ {
272
+ aux_t * auxdata = NULL ;
273
+ if ( ! file_ptr ) {
274
+ files -> errnum = open_failed ;
275
+ return 0 ;
276
+ }
261
277
262
278
files -> has_line = (int * ) realloc (files -> has_line , sizeof (int )* (files -> nreaders + 1 ));
263
279
files -> has_line [files -> nreaders ] = 0 ;
@@ -274,7 +290,7 @@ int bcf_sr_add_reader(bcf_srs_t *files, const char *fname)
274
290
BGZF * bgzf = hts_get_bgzfp (reader -> file );
275
291
if ( bgzf && bgzf_check_EOF (bgzf ) == 0 ) {
276
292
files -> errnum = no_eof ;
277
- hts_log_warning ("No BGZF EOF marker; file '%s' may be truncated" , fname );
293
+ hts_log_warning ("No BGZF EOF marker; file '%s' may be truncated" , file_ptr -> fn );
278
294
}
279
295
if (files -> p )
280
296
bgzf_thread_pool (bgzf , files -> p -> pool , files -> p -> qsize );
@@ -290,7 +306,7 @@ int bcf_sr_add_reader(bcf_srs_t *files, const char *fname)
290
306
return 0 ;
291
307
}
292
308
293
- reader -> tbx_idx = tbx_index_load (fname );
309
+ reader -> tbx_idx = tbx_index_load (fname ? fname : file_ptr -> fn );
294
310
if ( !reader -> tbx_idx )
295
311
{
296
312
files -> errnum = idx_load_failed ;
@@ -309,7 +325,7 @@ int bcf_sr_add_reader(bcf_srs_t *files, const char *fname)
309
325
310
326
reader -> header = bcf_hdr_read (reader -> file );
311
327
312
- reader -> bcf_idx = bcf_index_load (fname );
328
+ reader -> bcf_idx = bcf_index_load (fname ? fname : file_ptr -> fn );
313
329
if ( !reader -> bcf_idx )
314
330
{
315
331
files -> errnum = idx_load_failed ;
@@ -362,7 +378,7 @@ int bcf_sr_add_reader(bcf_srs_t *files, const char *fname)
362
378
return 0 ;
363
379
}
364
380
365
- reader -> fname = strdup (fname );
381
+ reader -> fname = strdup (fname ? fname : file_ptr -> fn );
366
382
if ( files -> apply_filters )
367
383
reader -> filter_ids = init_filters (reader -> header , files -> apply_filters , & reader -> nfilter_ids );
368
384
@@ -413,6 +429,18 @@ int bcf_sr_add_reader(bcf_srs_t *files, const char *fname)
413
429
}
414
430
}
415
431
432
+ if ((auxdata = BCF_SR_AUX (files ))) {
433
+ //store closure status for htsfile
434
+ int * tmp = realloc (auxdata -> closefile , sizeof (int ) * files -> nreaders );
435
+ if (!tmp ) {
436
+ hts_log_error ("Failed to allocate memory" );
437
+ return 0 ;
438
+ }
439
+ tmp [files -> nreaders - 1 ] = autoclose ;
440
+ auxdata -> closefile = tmp ;
441
+ }
442
+
443
+
416
444
return 1 ;
417
445
}
418
446
@@ -426,13 +454,15 @@ bcf_srs_t *bcf_sr_init(void)
426
454
return files ;
427
455
}
428
456
429
- static void bcf_sr_destroy1 (bcf_sr_t * reader )
457
+ static void bcf_sr_destroy1 (bcf_sr_t * reader , int closefile )
430
458
{
431
459
free (reader -> fname );
432
460
if ( reader -> tbx_idx ) tbx_destroy (reader -> tbx_idx );
433
461
if ( reader -> bcf_idx ) hts_idx_destroy (reader -> bcf_idx );
434
462
bcf_hdr_destroy (reader -> header );
435
- hts_close (reader -> file );
463
+ if (closefile ) {
464
+ hts_close (reader -> file );
465
+ }
436
466
if ( reader -> itr ) tbx_itr_destroy (reader -> itr );
437
467
int j ;
438
468
for (j = 0 ; j < reader -> mbuffer ; j ++ )
@@ -445,8 +475,10 @@ static void bcf_sr_destroy1(bcf_sr_t *reader)
445
475
void bcf_sr_destroy (bcf_srs_t * files )
446
476
{
447
477
int i ;
478
+ int * autoclose = BCF_SR_AUX (files )-> closefile ;
479
+
448
480
for (i = 0 ; i < files -> nreaders ; i ++ )
449
- bcf_sr_destroy1 (& files -> readers [i ]);
481
+ bcf_sr_destroy1 (& files -> readers [i ], autoclose [ i ] );
450
482
free (files -> has_line );
451
483
free (files -> readers );
452
484
for (i = 0 ; i < files -> n_smpl ; i ++ ) free (files -> samples [i ]);
@@ -456,19 +488,23 @@ void bcf_sr_destroy(bcf_srs_t *files)
456
488
if (files -> tmps .m ) free (files -> tmps .s );
457
489
if (files -> n_threads ) bcf_sr_destroy_threads (files );
458
490
bcf_sr_sort_destroy (& BCF_SR_AUX (files )-> sort );
491
+ free (autoclose );
459
492
free (files -> aux );
460
493
free (files );
461
494
}
462
495
463
496
void bcf_sr_remove_reader (bcf_srs_t * files , int i )
464
497
{
465
498
assert ( !files -> samples ); // not ready for this yet
499
+ int * autoclose = BCF_SR_AUX (files )-> closefile ;
500
+
466
501
bcf_sr_sort_remove_reader (files , & BCF_SR_AUX (files )-> sort , i );
467
- bcf_sr_destroy1 (& files -> readers [i ]);
502
+ bcf_sr_destroy1 (& files -> readers [i ], autoclose [ i ] );
468
503
if ( i + 1 < files -> nreaders )
469
504
{
470
505
memmove (& files -> readers [i ], & files -> readers [i + 1 ], (files -> nreaders - i - 1 )* sizeof (bcf_sr_t ));
471
506
memmove (& files -> has_line [i ], & files -> has_line [i + 1 ], (files -> nreaders - i - 1 )* sizeof (int ));
507
+ memmove (& autoclose [i ], & autoclose [i + 1 ], (files -> nreaders - i - 1 )* sizeof (int ));
472
508
}
473
509
files -> nreaders -- ;
474
510
}
0 commit comments