Supply TEMPLATE and SUFFIX for temporary query sequence files.
[bioperl-run.git] / t / Samtools.t
blob45c19d06ecc8ad32c19104a1f6f0ccb227380d86
1 #-*-perl-*-
2 #$Id$
4 use strict;
5 use warnings;
6 no warnings qw(once);
7 our $home;
8 BEGIN {
9     $home = '.'; # set to '.' for Build use, 
10                       # '..' for debugging from .t file
11     unshift @INC, $home;
12     use Bio::Root::Test;
13     test_begin(-tests => 41,
14                -requires_modules => [qw(IPC::Run Bio::Tools::Run::Samtools)]);
17 use File::Copy;
18 use Bio::Tools::Run::WrapperBase;
20 ok my $samt = Bio::Tools::Run::Samtools->new(
21     -command            => 'pileup',
22     -refseq             => 'my.fas',
23     -theta              => 0.05
24     ), "make a factory using command 'pileup'";
25 # ParameterBaseI compliance : really AssemblerBase tests...
26 ok $samt->parameters_changed, "parameters changed on construction";
27 ok $samt->refseq, "access parameter";
28 ok !$samt->parameters_changed, "parameters_changed cleared on read";
29 ok $samt->set_parameters( -n_haplos => 4 ), "set a param not set in constructor";
30 ok $samt->parameters_changed, "parameters_changed set";
31 is ($samt->n_haplos, 4, "parameter really set");
32 is ($samt->refseq, 'my.fas', "original parameter unchanged");
33 ok !$samt->parameters_changed, "parameters_changed cleared on read";
34 ok $samt->set_parameters( -refseq => 'their.fas' ), "change an original parameter";
35 is ($samt->refseq, 'their.fas', "parameter really changed");
36 ok $samt->reset_parameters( -refseq => 'our.fas' ), "reset parameters with arg";
37 ok !$samt->theta, "original parameters undefined";
38 is ($samt->refseq, 'our.fas', "parameter really reset via arg");
39 #back to beginning
40 $samt->set_parameters(
41     -command            => 'pileup',
42     -refseq             => 'my.fas',
43     -theta              => 0.05
44     );
45 ok $samt->parameters_changed, "parameters changed";
47 is( scalar $samt->available_parameters, 14, "all available options");
48 is( scalar $samt->available_parameters('params'), 9, "available parameters" );
49 is( scalar $samt->available_parameters('switches'), 5, "available switches" );
50 my %pms = $samt->get_parameters;
51 is_deeply( \%pms, 
52            {   command      => 'pileup',
53                refseq       => 'my.fas',
54              theta        => 0.05 }, "get_parameters correct");
55 is( $samt->command, 'pileup', "command attribute set");
57 is_deeply( $samt->{_options}->{_commands}, 
58            [@Bio::Tools::Run::Samtools::program_commands], 
59            "internal command array set" );
61 is_deeply( $samt->{_options}->{_prefixes},
62            {%Bio::Tools::Run::Samtools::command_prefixes}, 
63            "internal prefix hash set");
65 is_deeply( $samt->{_options}->{_params}, 
66            [qw( command refseq map_qcap ref_list site_list theta n_haplos exp_hap_diff indel_prob )],
67            "commands filtered by prefix");
70 my @a = @{$samt->_translate_params};
71 is shift @a, 'pileup', 'translate_params: command correct';
72 my ($k, %h);
73 for (@a) {
74     (/^-/) ? ( $h{$k = $_} = undef ) : ( $h{$k} = $_ );
76 is_deeply( \%h, { '-T' => 0.05, '-f' => 'my.fas' }, 'translate_params: options correct');
79 SKIP : {
80     test_skip( -requires_executable => $samt,
81                -tests => 16 );
83     my $new_bam = Bio::Tools::Run::Samtools->new(
84                            -command => 'merge',
85                            )->run(
86                            -obm => 'output_file.bam',
87                            -ibm => ['t/data/Ft.bam', 't/data/Ft.bam'],
88                            );
89     # test run
90     ok($new_bam, 'merge bam factory instantiated');
91     ok(-f 'output_file.bam', 'merged bam file created');
92     unlink('output_file.bam');
93                
94     my %tmpfiles;
95     for (qw(refseq bamfile samfile rtbamfile sorted_bamfile fai bai)) {
96         $tmpfiles{$_} = test_output_file();
97     }
98     copy(test_input_file('Ft.bam'), $tmpfiles{bamfile}) or die "copy failed (1)";
99     copy(test_input_file('Ft.frag.fas'), $tmpfiles{refseq}) or die "copy failed (2)";
100     ok $samt = Bio::Tools::Run::Samtools->new( -command => 'faidx' ), "fasta index factory";
101     ok $samt->run( -fas => $tmpfiles{refseq}, -out => $tmpfiles{fai}), "make fasta index";
102     ok -e $tmpfiles{fai}, "fai file present";
103     ok $samt = Bio::Tools::Run::Samtools->new( -command => 'view' ), "bam -> sam cvt factory";
105     ok $samt->run( -bam => $tmpfiles{bamfile}, -out => $tmpfiles{samfile} ), "convert bam -> sam";
106     ok -T $tmpfiles{samfile}, "sam file present and text";
107     ok $samt->set_parameters( -sam_input => 1, -bam_output => 1, -refseq => $tmpfiles{refseq} ), "sam -> bam cvt factory";
108     ok $samt->run( -bam => $tmpfiles{samfile}, -out => $tmpfiles{rtbamfile} ), "convert sam -> bam";
109     ok -B $tmpfiles{rtbamfile}, "bam file present and binary";
110     
111     ok $samt = Bio::Tools::Run::Samtools->new( -command => 'sort' ), 'bam sort factory';
112     ok $samt->run( -bam => $tmpfiles{rtbamfile}, -pfx => 'sorted_bam'), 'sort bam file';
114     ok $samt = Bio::Tools::Run::Samtools->new( -command => 'index' ), 'bam index factory';
115     ok $samt->run( -bam => 'sorted_bam', -out => $tmpfiles{bai}), 'make bam index';
116     ok -B $tmpfiles{bai}, 'bai file present and binary';
117     
118     unlink('sorted_bam');