-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathIndexNames.pm
720 lines (623 loc) · 24.1 KB
/
IndexNames.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
package Bio::JBrowse::Cmd::IndexNames;
=head1 NAME
Bio::JBrowse::Cmd::IndexNames - script module to create or update a
JBrowse names index from source files. Main script POD is in generate-names.pl.
=cut
use strict;
use warnings;
use base 'Bio::JBrowse::Cmd';
use File::Spec ();
use POSIX ();
use Storable ();
use File::Path ();
use File::Temp ();
use List::Util ();
use Encode qw(decode encode);
use GenomeDB ();
use Bio::GFF3::LowLevel qw/gff3_parse_feature/;
use Bio::JBrowse::HashStore ();
sub option_defaults {(
dir => 'data',
completionLimit => 20,
locationLimit => 100,
mem => 256 * 2**20,
tracks => [],
)}
sub option_definitions {(
"dir|out=s",
"completionLimit=i",
"locationLimit=i",
"verbose|v+",
"noSort",
"thresh=i",
"sortMem=i",
"mem=i",
"workdir=s",
'tracks=s@',
'hashBits=i',
'incremental|i',
"help|h|?",
'safeMode',
'compress'
)}
sub initialize {
my ( $self ) = @_;
# these are used in perf-critical tight loops, make them accessible faster
$self->{max_completions} = $self->opt('completionLimit');
$self->{max_locations} = $self->opt('locationLimit');
$self->{stats} = {
total_namerec_bytes => 0,
namerecs_buffered => 0,
tracksWithNames => [],
record_stream_estimated_count => 0,
operation_stream_estimated_count => 0,
};
}
sub run {
my ( $self ) = @_;
my $outDir = $self->opt('dir');
-d $outDir or die "Output directory '$outDir' does not exist.\n";
-w $outDir or die "Output directory '$outDir' is not writable.\n";
my $gdb = GenomeDB->new( $outDir );
my $refSeqs = $gdb->refSeqs;
unless( @$refSeqs ) {
die "No reference sequences defined in configuration, nothing to do.\n";
}
my @tracks = grep $self->track_is_included( $_->{label} ),
@{ $gdb->trackList || [] };
unless( @tracks ) {
die "No tracks. Nothing to do.\n";
}
$self->vprint( "Tracks:\n".join('', map " $_->{label}\n", @tracks ) );
# find the names files we will be working with
my $names_files = $self->find_names_files( \@tracks, $refSeqs );
unless( @$names_files ) {
warn "WARNING: No feature names found for indexing,"
." only reference sequence names will be indexed.\n";
}
$self->load( $refSeqs, $names_files );
# store the list of tracks that have names
$self->name_store->meta->{track_names} = [
$self->_uniq(
@{$self->name_store->meta->{track_names}||[]},
@{$self->{stats}{tracksWithNames}}
)
];
# record the fact that all the keys are lowercased
$self->name_store->meta->{lowercase_keys} = 1;
# set up the name store in the trackList.json
$gdb->modifyTrackList( sub {
my ( $data ) = @_;
$data->{names}{type} ||= 'Hash';
$data->{names}{url} ||= 'names/';
return $data;
});
if ( $self->opt('compress') ) {
# if we are compressing the sequence files, drop a .htaccess file
# in the seq/ dir that will automatically configure users with
# Apache (and AllowOverride on) to serve the .txt.gz files
# correctly
require GenomeDB;
my $hta = File::Spec->catfile( $self->opt('dir'), 'names', '.htaccess' );
open my $hta_fh, '>', $hta or die "$! writing $hta";
$hta_fh->print( GenomeDB->precompression_htaccess('.txtz','.jsonz') );
}
return;
}
sub load {
my ( $self, $ref_seqs, $names_files ) = @_;
# convert the stream of name records into a stream of operations to do
# on the data in the hash store
my $operation_stream = $self->make_operation_stream( $self->make_name_record_stream( $ref_seqs, $names_files ), $names_files );
# hash each operation and write it to a log file
$self->name_store->stream_do(
$operation_stream,
sub {
my ( $operation, $data ) = @_;
my %fake_store = ( $operation->[0] => $data );
$self->do_hash_operation( \%fake_store, $operation );
return $fake_store{ $operation->[0] } ;
},
$self->{stats}{operation_stream_estimated_count},
);
}
sub _hash_operation_freeze { $_[1] }
sub _hash_operation_thaw { $_[1] }
sub _uniq {
my $self = shift;
my %seen;
return grep !($seen{$_}++), @_;
}
sub _mergeIndexEntries {
my ( $self, $a, $b ) = @_;
# merge exact
{
my $aExact = $a->{exact} ||= [];
my $bExact = $b->{exact} || [];
no warnings 'uninitialized';
my %exacts = map { join( '|', @$_ ) => 1 } @$aExact;
while ( @$bExact && @$aExact < $self->{max_locations} ) {
my $e = shift @$bExact;
if( ! $exacts{ join('|',@$e) }++ ) {
push @{$aExact}, $e;
}
}
}
# merge prefixes
{
my $aPrefix = $a->{prefix} ||= [];
# only merge if the target prefix is not already full
if( ref $aPrefix->[-1] ne 'HASH' ) {
my $bPrefix = $b->{prefix} || [];
my %prefixes = map { $_ => 1 } @$aPrefix; #< keep the prefixes unique
while ( @$bPrefix && @$aPrefix < $self->{max_completions} ) {
my $p = shift @$bPrefix;
if ( ! $prefixes{ $p }++ ) {
push @{$aPrefix}, $p;
}
}
}
}
return $a;
}
sub make_file_record {
my ( $self, $track, $file ) = @_;
-f decode('UTF-8',$file) or die "$file not found\n";
-r decode('UTF-8',$file) or die "$file not readable\n";
my $gzipped = $file =~ /\.(txt|json|g)z(\.\d+)?$/;
my $type = $file =~ /\.txtz?$/ ? 'txt' :
$file =~ /\.jsonz?$/ ? 'json' :
$file =~ /\.vcf(\.gz)?$/ ? 'vcf' :
$file =~ /\.bed(\.gz)?$/ ? 'bed' :
$file =~ /\.gff3?(\.gz)?(\.\d+)?$/ ? 'gff' :
undef;
if( $type ) {
return {
gzipped => $gzipped,
nameAttributes => $track->{nameAttributes},
indexedFeatures => $track->{indexedFeatures},
fullpath => $file,
type => $type,
trackName => $track->{label}
};
}
return;
}
sub track_is_included {
my ( $self, $trackname ) = @_;
my $included = $self->{included_track_names} ||= do {
my @tracks = @{ $self->opt('tracks') };
my $inc = { map { $_ => 1 } map { split ',', $_ } @tracks };
@tracks ? sub { $inc->{ shift() } } : sub { 1 };
};
return $included->( $trackname );
}
sub name_store {
my ( $self ) = @_;
unless( $self->{name_store} ) {
$self->{name_store} = tie my %tied_hash, 'Bio::JBrowse::HashStore', (
dir => File::Spec->catdir( $self->opt('dir'), "names" ),
work_dir => $self->opt('workdir'),
mem => $self->opt('mem'),
empty => ! $self->opt('incremental'),
compress => $self->opt('compress'),
hash_bits => $self->requested_hash_bits,
verbose => $self->opt('verbose')
);
$self->{name_store_tied_hash} = \%tied_hash;
}
return $self->{name_store};
}
sub name_store_tied_hash {
my ( $self ) = @_;
$self->name_store;
return $self->{name_store_tied_hash};
}
sub close_name_store {
my ( $self ) = @_;
delete $self->{name_store};
delete $self->{name_store_tied_hash};
}
sub requested_hash_bits {
my $self = shift;
# set the hash size to try to get about 5-10KB per file, at an
# average of about 500 bytes per name record, for about 10 records
# per file (uncompressed). if the store has existing data in it,
# this will be ignored.
return $self->{hash_bits} ||= $self->opt('hashBits')
|| do {
if( $self->{stats}{record_stream_estimated_count} ) {
my $records_per_bucket = $self->opt('compress') ? 40 : 10;
my $bits = 4*int( log( $self->{stats}{record_stream_estimated_count} / $records_per_bucket )/ 4 / log(2));
# clamp bits between 4 and 32
sprintf( '%0.0f', List::Util::max( 4, List::Util::min( 32, $bits ) ));
}
else {
12
}
};
}
sub make_name_record_stream {
my ( $self, $refseqs, $names_files ) = @_;
my @names_files = @$names_files;
my $name_records_iterator = sub {};
my @namerecord_buffer;
# insert a name record for all of the reference sequences
for my $ref ( @$refseqs ) {
$self->{stats}{name_input_records}++;
$self->{stats}{namerecs_buffered}++;
my $rec = [ @{$ref}{ qw/ name length name seqDir start end seqChunkSize/ }];
$self->{stats}{total_namerec_bytes} += length join(",",$rec);
push @namerecord_buffer, $rec;
}
my %trackHash;
my $trackNum = 0;
my $names_dir = File::Spec->catdir( $self->opt('dir'), "names" );
if( -e File::Spec->catfile( $names_dir,'meta.json' ) ) {
# read meta.json data into a temp HashStore
my $temp_store = tie my %temp_hash, 'Bio::JBrowse::HashStore', (
dir => $names_dir,
empty => 0,
compress => 0,
verbose => 0);
# initialize the track hash with an index
foreach (@{$temp_store->meta->{track_names}}) {
$trackHash{$_}=$trackNum++;
}
untie $temp_store;
}
return sub {
while( ! @namerecord_buffer ) {
my $nameinfo = $name_records_iterator->() || do {
my $file = shift @names_files;
return unless $file;
$name_records_iterator = $self->make_names_iterator( $file );
$name_records_iterator->();
} or return;
# expand each name record into its aliases
my @aliases = map { ref($_) ? @$_ : $_ } @{$nameinfo->[0]};
foreach my $alias ( @aliases ) {
my $track = $nameinfo->[1];
unless ( defined $trackHash{$track} ) {
$trackHash{$track} = $trackNum++;
push @{$self->{stats}{tracksWithNames}}, $track;
}
$self->{stats}{namerecs_buffered}++;
push @namerecord_buffer, [
$alias,
$trackHash{$track},
@{$nameinfo}[2..$#{$nameinfo}]
];
}
}
return shift @namerecord_buffer;
};
}
sub find_names_files {
my ( $self, $tracks, $refseqs ) = @_;
my @files;
for my $track (@$tracks) {
for my $ref (@$refseqs) {
my $dir = File::Spec->catdir(
$self->opt('dir'),
"tracks",
$track->{label},
$ref->{name}
);
# read either names.txt or names.json files
my $name_records_iterator;
my $names_txt = File::Spec->catfile( $dir, 'names.txt' );
if( -e decode('UTF-8',$names_txt) ) {
push @files, $self->make_file_record( $track, $names_txt );
}
else {
my $names_json = File::Spec->catfile( $dir, 'names.json' );
if( -f $names_json ) {
push @files, $self->make_file_record( $track, $names_json );
}
}
}
# try to detect VCF tracks and index their VCF files
if( $track->{urlTemplate} && $track->{urlTemplate} =~ /\.vcf\.gz/
|| ($track->{storeClass}||'') =~ /VCFTabix$/
) {
my $path = File::Spec->catfile( $self->opt('dir'), $track->{urlTemplate} );
if( -r $path ) {
push @files, $self->make_file_record( $track, $path );
}
else {
warn "VCF file '$path' not found, or not readable. Skipping.\n";
}
}
# try to detect BED tracks and index their BED files
if( $track->{urlTemplate} && $track->{urlTemplate} =~ /\.bed\.gz/
|| ($track->{storeClass}||'') =~ /BEDTabix$/
) {
my $path = File::Spec->catfile( $self->opt('dir'), $track->{urlTemplate} );
if( -r $path ) {
push @files, $self->make_file_record( $track, $path );
}
else {
warn "BED file '$path' not found, or not readable. Skipping.\n";
}
}
# try to detect GFF3 tracks and index their GFF3 files
if( $track->{urlTemplate} && $track->{urlTemplate} =~ /\.gff3?\.gz(\.\d+)?/
|| ($track->{storeClass}||'') =~ /GFF3Tabix$/
) {
my $path = File::Spec->catfile( $self->opt('dir'), $track->{urlTemplate} );
if( -r $path ) {
push @files, $self->make_file_record( $track, $path );
}
else {
warn "GFF file '$path' not found, or not readable. Skipping.\n";
}
}
}
return \@files;
}
sub make_operation_stream {
my ( $self, $record_stream, $names_files ) = @_;
$self->{stats}{namerecs_converted_to_operations} = 0;
my @operation_buffer;
# try to fill the operation buffer a bit to estimate the number of operations per name record
{
while( @operation_buffer < 50000 && ( my $name_record = $record_stream->()) ) {
$self->{stats}{namerecs_converted_to_operations}++;
push @operation_buffer, $self->make_operations( $name_record );
}
}
# estimate the total number of name records we probably have based on the input file sizes
#print "sizes: $self->{stats}{total_namerec_bytes}, buffered: $namerecs_buffered, b/rec: ".$total_namerec_sizes/$namerecs_buffered."\n";
$self->{stats}{avg_record_text_bytes} = $self->{stats}{total_namerec_bytes}/($self->{stats}{namerecs_buffered}||1);
$self->{stats}{total_input_bytes} = List::Util::sum(
map { my $s = -s decode('UTF-8',$_->{fullpath});
$s *= 8 if decode('UTF-8',$_->{fullpath}) =~ /\.(g|txt|json)z$/;
$s;
} @$names_files ) || 0;
$self->{stats}{record_stream_estimated_count} = int( $self->{stats}{total_input_bytes} / ($self->{stats}{avg_record_text_bytes}||1));;
$self->{stats}{operation_stream_estimated_count} = $self->{stats}{record_stream_estimated_count} * int( @operation_buffer / ($self->{stats}{namerecs_converted_to_operations}||1) );
if( $self->opt('verbose') ) {
print "Sampled input stats:\n";
while( my ($k,$v) = each %{$self->{stats}} ) {
next if ref $v;
$k =~ s/_/ /g;
printf( '%40s'." $v\n", $k );
}
}
return sub {
unless( @operation_buffer ) {
while( (my $name_record = $record_stream->()) && @operation_buffer < 5000) {
push @operation_buffer, $self->make_operations($name_record);
}
}
return shift @operation_buffer;
};
}
my $OP_ADD_EXACT = 1;
my $OP_ADD_PREFIX = 2;
# given a name record, return zero or more operations to perform on the hash store
# to load it into the store
sub make_operations {
my ( $self, $record ) = @_;
my $lc_name = lc $record->[0];
unless( defined $lc_name ) {
unless( $self->{already_warned_about_blank_name_records} ) {
warn "WARNING: some blank name records found, skipping.\n";
$self->{already_warned_about_blank_name_records} = 1;
}
return;
}
# if the name of the thing is the same as its reference
# sequence (i.e. this is a reference sequence),
# then skip it, because we treat ref seqs separately.
{
no warnings 'uninitialized';
return if $record->[0] eq $record->[3];
}
my @ops = ( [ $lc_name, $OP_ADD_EXACT, $record ] );
if( $self->{max_completions} > 0 ) {
# generate all the prefixes
my $l = $lc_name;
chop $l;
while ( $l ) {
push @ops, [ $l, $OP_ADD_PREFIX, $record->[0] ];
chop $l;
}
}
$self->{stats}{operations_made} += scalar @ops;
return @ops;
}
my %full_entries;
sub do_hash_operation {
my ( $self, $store, $op ) = @_;
my ( $lc_name, $op_name, $record ) = @$op;
if( $op_name == $OP_ADD_EXACT ) {
my $r = $store->{$lc_name};
$r = $r ? $self->_hash_operation_thaw($r) : { exact => [], prefix => [] };
my $exact = $r->{exact};
if( @$exact < $self->{max_locations} ) {
# don't insert duplicate locations
no warnings 'uninitialized';
if( ! grep {
$record->[1] == $_->[1] && $record->[3] eq $_->[3] && $record->[4] == $_->[4] && $record->[5] == $_->[5]
} @$exact
) {
push @$exact, $record;
$store->{$lc_name} = $self->_hash_operation_freeze( $r );
}
}
# elsif( $verbose ) {
# print STDERR "Warning: $name has more than --locationLimit ($self->{max_locations}) distinct locations, not all of them will be indexed.\n";
# }
}
elsif( $op_name == $OP_ADD_PREFIX && ! exists $full_entries{$lc_name} ) {
my $r = $store->{$lc_name};
$r = $r ? $self->_hash_operation_thaw($r) : { exact => [], prefix => [] };
my $name = $record;
my $p = $r->{prefix};
if( @$p < $self->{max_completions} ) {
if( ! grep $name eq $_, @$p ) { #< don't insert duplicate prefixes
push @$p, $name;
$store->{$lc_name} = $self->_hash_operation_freeze( $r );
}
}
elsif( @$p == $self->{max_completions} ) {
push @$p, { name => 'too many matches', hitLimit => 1 };
$store->{$lc_name} = $self->_hash_operation_freeze( $r );
$full_entries{$lc_name} = 1;
}
}
}
# each of these takes an input filename containing names to be indexed,
# and returns a subroutine that, when called repeatedly, returns name
# records until there are no more (returning undef to signal the end)
sub make_names_iterator {
my ( $self, $file_record ) = @_;
if( $file_record->{type} eq 'txt' ) {
my $input_fh = $self->open_names_file( $file_record );
# read the input json partly with low-level parsing so that we
# can parse incrementally from the filehandle. names list
# files can be very big.
return sub {
my $t = <$input_fh>;
if( $t ) {
$self->{stats}{name_input_records}++;
$self->{stats}{total_namerec_bytes} += length $t;
return eval { JSON::from_json( $t ) };
}
return undef;
};
}
elsif( $file_record->{type} eq 'json' ) {
# read old-style names.json files all from memory
my $input_fh = $self->open_names_file( $file_record );
my $data = JSON::from_json(do {
local $/;
my $text = scalar <$input_fh>;
$self->{stats}{total_namerec_bytes} += length $text;
$text;
});
$self->{stats}{name_input_records} += scalar @$data;
return sub { shift @$data };
}
elsif( $file_record->{type} eq 'vcf' ) {
my $input_fh = $self->open_names_file( $file_record );
no warnings 'uninitialized';
return sub {
my $line;
while( ($line = <$input_fh>) =~ /^#/ ) {}
return unless $line;
$self->{stats}{name_input_records}++;
$self->{stats}{total_namerec_bytes} += length $line;
my ( $ref, $start, $name, $basevar ) = split "\t", $line, 5;
$start--;
my @names = split /\s*;\s*/, $name;
return [\@names,$file_record->{trackName},$name,$ref, $start, $start+length($basevar)];
};
}
elsif( $file_record->{type} eq 'bed' ) {
my $input_fh = $self->open_names_file( $file_record );
no warnings 'uninitialized';
return sub {
my $line;
while( ($line = <$input_fh>) =~ /^#/ ) {}
return unless $line;
$self->{stats}{name_input_records}++;
$self->{stats}{total_namerec_bytes} += length $line;
chomp($line);
my ( $ref, $start, $end, $name) = split "\t", $line;
my @names = split /\s*;\s*/, $name;
return [\@names,$file_record->{trackName},$name,$ref, $start, $end];
};
}
elsif( $file_record->{type} eq 'gff' ) {
my $input_fh = $self->open_names_file( $file_record );
no warnings 'uninitialized';
return sub {
# find the next feature in the file that has a name
my $line;
my $feature;
my @names;
while(my $line = <$input_fh>) {
if( $line !~ /^#/ ) {
chomp $line;
$feature = gff3_parse_feature($line);
my $type = $feature->{type} || [];
my @featureTypes;
if(ref(\$file_record->{indexedFeatures}) eq 'ARRAY') {
@featureTypes = $file_record->{indexedFeatures}
} elsif(ref(\$file_record->{indexedFeatures}) eq 'SCALAR') {
@featureTypes = split /\s*,\s*/, $file_record->{indexedFeatures};
}
if( (!@featureTypes) || (@featureTypes && (grep $_ eq $type, @featureTypes)) ) {
my $Name = $feature->{attributes}{Name} || [];
my $ID = $feature->{attributes}{ID} || [];
my $Alias = $feature->{attributes}{Alias} || [];
my @fields;
my @computedFields;
if(ref(\$file_record->{nameAttributes}) eq 'ARRAY') {
@fields = $file_record->{nameAttributes}
} elsif(ref(\$file_record->{nameAttributes}) eq 'SCALAR') {
@fields = split /\s*,\s*/, $file_record->{nameAttributes};
}
if(@fields) {
@computedFields = map { $feature->{attributes}{$_} || [] } @fields;
}
@names = @fields ? @computedFields : $Name->[0] ? (@$Name, @$ID) : @$ID;
}
last if scalar @names;
}
}
# end the stream by returning undef if there are no more features
return unless $feature;
# keep stats
$self->{stats}{name_input_records}++;
$self->{stats}{total_namerec_bytes} += length $line + 1;
my $names = $feature->{attributes}{Name} || $feature->{attributes}{ID};
return [
\@names,
$file_record->{trackName},
$names->[0],
$feature->{seq_id},
$feature->{start}-1,
$feature->{end}+0
];
};
}
else {
warn "ignoring names file $file_record->{fullpath}. unknown type $file_record->{type}.\n";
return sub {};
}
}
sub open_names_file {
my ( $self, $filerec ) = @_;
my $infile = $filerec->{fullpath};
if( $filerec->{gzipped} ) {
# can't use PerlIO::gzip, it truncates bgzipped files
my $z;
eval {
require IO::Uncompress::Gunzip;
$z = IO::Uncompress::Gunzip->new( $filerec->{fullpath }, -MultiStream => 1 )
or die "IO::Uncompress::Gunzip failed: $IO::Uncompress::Gunzip::GunzipError\n";
};
if( $@ ) {
# fall back to use gzip command if available
if( `which gunzip` ) {
open my $fh, '-|', 'gzip', '-dc', $filerec->{fullpath}
or die "$! running gunzip";
return $fh;
} else {
die "cannot uncompress $filerec->{fullpath}, could not use either IO::Uncompress::Gunzip or gzip";
}
}
else {
return $z;
}
}
else {
open my $fh, '<', decode('UTF-8',$infile) or die "$! reading $infile";
return $fh;
}
}
1;