| 1 | package App::Sandy::Role::IO; | ||
| 2 | # ABSTRACT: Input and output custom wrappers. | ||
| 3 | |||
| 4 | 1 | ▶ | use App::Sandy::Base 'role'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | use App::Sandy::BGZF; | ||
| 6 | use IO::Compress::Gzip '$GzipError'; | ||
| 7 | use PerlIO::gzip; | ||
| 8 | |||
| 9 | our $VERSION = '0.25'; # VERSION | ||
| 10 | |||
| 11 | sub with_open_r { | ||
| 12 | my ($self, $file) = @_; | ||
| 13 | |||
| 14 | my $fh; | ||
| 15 | my $mode = $file =~ /\.gz$/ ? "<:gzip" : "<"; | ||
| 16 | |||
| 17 | open $fh, $mode => $file | ||
| 18 | or die "Not possible to read $file: $!\n"; | ||
| 19 | |||
| 20 | return $fh; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub with_open_w { | ||
| 24 | my ($self, $file, $level) = @_; | ||
| 25 | |||
| 26 | my $fh; | ||
| 27 | |||
| 28 | if ($level) { | ||
| 29 | $fh = IO::Compress::Gzip->new($file, -Level => $level) | ||
| 30 | or die "Not possible to create $file: $GzipError\n"; | ||
| 31 | } else { | ||
| 32 | open $fh, '>' => $file | ||
| 33 | or die "Not possible to create $file: $!\n"; | ||
| 34 | } | ||
| 35 | |||
| 36 | return $fh; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub with_open_a { | ||
| 40 | my ($self, $file) = @_; | ||
| 41 | |||
| 42 | open my $fh, '>>' => $file | ||
| 43 | or die "Not possible to append to $file: $!\n"; | ||
| 44 | |||
| 45 | return $fh; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub with_open_bam_w { | ||
| 49 | my ($self, $file, $level) = @_; | ||
| 50 | my $fh = App::Sandy::BGZF->new_filehandle($file, $level); | ||
| 51 | return $fh; | ||
| 52 | } | ||
| 53 | |||
| 54 | __END__ | ||