fastafactory.t 7.24 KB
Newer Older
Roy Storey's avatar
Roy Storey committed
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
package Hive::Test::FastaFactory;

use strict;
use warnings;

use lib 't/lib/';

use Hive::Test qw{spurt};

use base qw{Hive::Config};

sub test_suite_init {
    my $fasta = shift;
    ## write some sequences
    spurt <<EOF, $fasta;
>sequence1
ATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTA
GCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGAT
CTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTG
AGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTT
ATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTA
GCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGAT
CTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTG
AGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTT
ATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTA
GCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGAT
CTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTT
>sequence2
AGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCG
ATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTA
GTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGA
CGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTC
AGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCG
ATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTA
GTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGA
CGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTC
AGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCG
ATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTA
GTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGA
CGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTC
AGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCG
ATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTA
GTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGA
CGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTC
AGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCG
ATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTA
GTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGA
CGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTC
AGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCG
ATCGTACTGACTACTGAGTC
>sequence3
ATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTA
GCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGAT
CTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTG
AGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTT
ATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTA
GCTAGTCGGTGTGTGTGTTTAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTA
GTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGA
CGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTC
AGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCG
ATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTA
GTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGA
CGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTC
AGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCG
ATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTA
GTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGA
CGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTC
AGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCG
ATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTA
GTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGA
CGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTC
AGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCG
ATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGACGATCTAGCAGCTGTAGCTA
GTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTCAGTCGTACGTAGCGATCTGA
CGATCTAGCAGCTGTAGCTAGTCGGTGTGTGTGTTTATCGATCGTACTGACTACTGAGTC
EOF

   return (
       '-inputfile'        => $fasta,
       '-max_chunk_length' => 1,
       '-chunks_dir'       => '.',
       '-output_suffix'    => '.fa',
   );
}

sub pipeline_analyses {
    my $self = shift;
    return [
	{
	    -logic_name => 'fasta_factory',
            -module     => 'Bio::EnsEMBL::Hive::RunnableDB::FastaFactory',
            -input_ids => [ 
		 {
		     'inputfile'         => $self->o('inputfile'),
Roy Storey's avatar
Roy Storey committed
95
		     'max_chunk_length'  => 20000, ## big enough for all sequences
Roy Storey's avatar
Roy Storey committed
96
		     'output_prefix'     => $self->o('chunks_dir').'/test1_',
Roy Storey's avatar
Roy Storey committed
97
98
99
100
		     'output_suffix'     => $self->o('output_suffix'),
		 },
		 {
		     'inputfile'         => $self->o('inputfile'),
Roy Storey's avatar
Roy Storey committed
101
102
		     'max_chunk_length'  => 200, ## smaller than all sequences
		     'output_prefix'     => $self->o('chunks_dir').'/test2_',
Roy Storey's avatar
Roy Storey committed
103
		     'output_suffix'     => $self->o('output_suffix'),
Roy Storey's avatar
Roy Storey committed
104
105
106
107
108
109
110
		 },
		 {
		     'inputfile'         => $self->o('inputfile'),
		     'max_chunk_length'  => 1000, ## smaller than two combined sequences
		     'output_prefix'     => $self->o('chunks_dir').'/test3_',
		     'output_suffix'     => $self->o('output_suffix'),
		 },
Roy Storey's avatar
Roy Storey committed
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
		],
	}
	];
}

package main;

use Test::More;
use Data::Dumper;
use File::Temp qw{tempdir};
use Hive::Apiarist;

BEGIN {
    use_ok( 'Bio::EnsEMBL::Hive::RunnableDB::FastaFactory' );
}
#########################

my $dir = tempdir CLEANUP => 1;
chdir $dir;

my @argv   = Hive::Test::FastaFactory::test_suite_init('input.fa');
my $config = Hive::Test::FastaFactory->init_pipeline_here( @argv );

my $apiarist = Hive::Apiarist->new();
my $job      = $apiarist->get_a_new_job( $config->pipeline_url, 1 );
ok( $job, 'got work to do' );

my $runnable = Bio::EnsEMBL::Hive::RunnableDB::FastaFactory->new();
ok($runnable, 'instantiation');

run_job( $runnable, $job );
##
## do some checks
##
Roy Storey's avatar
Roy Storey committed
145
my $expected_filename = 'test1_1.fa';
Roy Storey's avatar
Roy Storey committed
146
147
148
149
ok(-e $expected_filename, 'output file exists');

is((stat($expected_filename))[7], (stat('input.fa'))[7], 'file size of input == output');

Roy Storey's avatar
Roy Storey committed
150
151
my @all_files = glob('test1_*.fa');
is(@all_files, 1, 'exactly one output file - test 1');
Roy Storey's avatar
Roy Storey committed
152
153
154
155

## 
## next job
##
156
$job = $apiarist->get_a_new_job( $config->pipeline_url, 2 );
Roy Storey's avatar
Roy Storey committed
157
158
159
160
ok( $job, 'got more work to do' );

run_job( $runnable, $job );

Roy Storey's avatar
Roy Storey committed
161
162
$expected_filename = 'test2_1.fa';
ok(-e $expected_filename, 'output file exists');
Roy Storey's avatar
Roy Storey committed
163

Roy Storey's avatar
Roy Storey committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
@all_files = glob('test2_*.fa');
is(@all_files, 3, 'correct number of output files - test 2');
# diag "@all_files";

my $expected_properties = {
    'test2_1.fa' => [ 662 ],
    'test2_2.fa' => [ 1313 ],
    'test2_3.fa' => [ 1475 ],
    'test2_4.fa' => [ 0 ],

    'test3_1.fa' => [ 1975 ],
    'test3_2.fa' => [ 1475 ],
    'test3_3.fa' => [ 0 ],
    'test3_4.fa' => [ 0 ],
};

foreach my $file(@all_files) {
    my $exp_size = $expected_properties->{$file}[0];
    is((stat($file))[7], $exp_size, "file '$file' has expected file size ($exp_size)");
}
Roy Storey's avatar
Roy Storey committed
184

Roy Storey's avatar
Roy Storey committed
185
186
187
## 
## next job
##
188
$job = $apiarist->get_a_new_job( $config->pipeline_url, 3 );
Roy Storey's avatar
Roy Storey committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
ok( $job, 'got more work to do' );

run_job( $runnable, $job );

$expected_filename = 'test3_1.fa';
ok(-e $expected_filename, 'output file exists');

@all_files = glob('test3_*.fa');
is(@all_files, 2, 'correct number of output files - test 3');
# diag "@all_files";

foreach my $file(@all_files) {
    my $exp_size = $expected_properties->{$file}[0];
    is((stat($file))[7], $exp_size, "file '$file' has expected file size ($exp_size)");
}
Roy Storey's avatar
Roy Storey committed
204
205
206
207
208


sub run_job {
    my ($runnable, $job) = @_;
    $job->param_init( 
Roy Storey's avatar
Roy Storey committed
209
	$runnable->strict_hash_format(),
Roy Storey's avatar
Roy Storey committed
210
211
212
213
214
215
216
217
218
219
220
221
	$runnable->param_defaults(), 
	$job->input_id(),
	);
    $runnable->input_job( $job );    
    $runnable->fetch_input();
    $runnable->run();
    $runnable->write_output();
}

chdir $ENV{'EHIVE_ROOT_DIR'};

done_testing();