| 1 | package Bio::Tradis::Samtools; | ||
| 2 | $Bio::Tradis::Samtools::VERSION = '1.4.1'; | ||
| 3 | # ABSTRACT: Change samtools syntax depending on version found | ||
| 4 | |||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use File::Spec; | ||
| 8 | |||
| 9 | has 'exec' => ( is => 'ro', isa => 'Str', default => 'samtools' ); | ||
| 10 | has 'threads' => ( is => 'ro', isa => 'Int', default => 1 ); | ||
| 11 | has 'exec_version' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_exec_version' ); | ||
| 12 | |||
| 13 | sub _build_exec_version { | ||
| 14 | my ($self) = @_; | ||
| 15 | |||
| 16 | my $fp = $self->find_exe($self->exec); | ||
| 17 | if(!$fp) | ||
| 18 | { | ||
| 19 | exit("ERROR: Can't find required ".$self->exec." in your \$PATH"); | ||
| 20 | } | ||
| 21 | my $cmd_version = $self->exec." 2>&1 | grep Version"; | ||
| 22 | my ($version_string) = qx($cmd_version); | ||
| 23 | |||
| 24 | if(defined($version_string)) | ||
| 25 | { | ||
| 26 | #Version: 0.1.19-44428cd | ||
| 27 | #Version: 1.2 (using htslib 1.2) | ||
| 28 | # we dont use 3rd number in version so just look for 0.1, 1.2 | ||
| 29 | if($version_string =~ /Version:[\t\s]+(\d+)\.(\d+)/) | ||
| 30 | { | ||
| 31 | return $1.'.'.$2; | ||
| 32 | } | ||
| 33 | else | ||
| 34 | { | ||
| 35 | print STDERR "ERROR: Couldn't identify samtools version"; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | else | ||
| 39 | { | ||
| 40 | print STDERR "ERROR: Couldn't identify samtools version"; | ||
| 41 | } | ||
| 42 | # reasonable fallback | ||
| 43 | return '0.1'; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub find_exe { | ||
| 47 | my ( $self, $bin ) = @_; | ||
| 48 | for my $dir ( File::Spec->path ) { | ||
| 49 | my $exe = File::Spec->catfile( $dir, $bin ); | ||
| 50 | return $exe if -x $exe; | ||
| 51 | } | ||
| 52 | return; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _is_version_less_than_1 { | ||
| 56 | my ($self) = @_; | ||
| 57 | if($self->exec_version < 1.0) | ||
| 58 | { | ||
| 59 | return 1; | ||
| 60 | } | ||
| 61 | else | ||
| 62 | { | ||
| 63 | return 0; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | sub run_sort { | ||
| 68 | my ( $self, $input_file, $output_file ) = @_; | ||
| 69 | |||
| 70 | my $cmd; | ||
| 71 | if ( $self->_is_version_less_than_1 ) { | ||
| 72 | $output_file =~ s/\.bam//i; | ||
| 73 | $cmd = join( ' ', ( $self->exec, 'sort', $input_file, $output_file) ); | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | $cmd = join( ' ', ( $self->exec, 'sort', '-@', $self->threads, '-O', 'bam', '-T', $input_file.'.tmp', '-o', $output_file, $input_file ) ); | ||
| 77 | } | ||
| 78 | system($cmd); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub run_index { | ||
| 82 | my ( $self, $input_file ) = @_; | ||
| 83 | system( $self->exec . " index $input_file" ); | ||
| 84 | } | ||
| 85 | |||
| 86 | no Moose; | ||
| 87 | __PACKAGE__->meta->make_immutable; | ||
| 88 | |||
| 89 | 1; | ||
| 90 | |||
| 91 | __END__ | ||
| 92 | |||
| 93 | =pod | ||
| 94 | |||
| 95 | =encoding UTF-8 | ||
| 96 | |||
| 97 | =head1 NAME | ||
| 98 | |||
| 99 | Bio::Tradis::Samtools - Change samtools syntax depending on version found | ||
| 100 | |||
| 101 | =head1 VERSION | ||
| 102 | |||
| 103 | version 1.4.1 | ||
| 104 | |||
| 105 | =head1 SYNOPSIS | ||
| 106 | |||
| 107 | Change samtools syntax depending on version found | ||
| 108 | use Bio::Tradis::Samtools; | ||
| 109 | |||
| 110 | my $obj = Bio::Tradis::Samtools->new( | ||
| 111 | exec => 'samtools' | ||
| 112 | ); | ||
| 113 | |||
| 114 | $obj->run_sort(); | ||
| 115 | |||
| 116 | =head1 AUTHOR | ||
| 117 | |||
| 118 | Carla Cummins <path-help@sanger.ac.uk> | ||
| 119 | |||
| 120 | =head1 COPYRIGHT AND LICENSE | ||
| 121 | |||
| 122 | This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute. | ||
| 123 | |||
| 124 | This is free software, licensed under: | ||
| 125 | |||
| 126 | The GNU General Public License, Version 3, June 2007 | ||
| 127 | |||
| 128 | =cut |