fastafactory.t 5.82 KB
Newer Older
1
# Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
nwillhoft's avatar
nwillhoft committed
2
# Copyright [2016-2021] EMBL-European Bioinformatics Institute
3 4 5 6 7 8 9 10 11 12 13 14 15
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#      http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Roy Storey's avatar
Roy Storey committed
16 17 18 19

use strict;
use warnings;

20 21
use Cwd;
use File::Basename;
Roy Storey's avatar
Roy Storey committed
22 23 24
use Test::More;
use Data::Dumper;
use File::Temp qw{tempdir};
25 26

use Bio::EnsEMBL::Hive::Utils::Test qw(standaloneJob);
Roy Storey's avatar
Roy Storey committed
27

28 29
# Where the Fasta file should be
my $inputfile = File::Basename::dirname( File::Basename::dirname( Cwd::realpath($0) ) ).'/input_fasta.fa';
30

Roy Storey's avatar
Roy Storey committed
31
my $dir = tempdir CLEANUP => 1;
32
my $orig = Cwd::getcwd;
Roy Storey's avatar
Roy Storey committed
33 34
chdir $dir;

35 36 37
standaloneJob(
    'Bio::EnsEMBL::Hive::RunnableDB::FastaFactory',
    {
38 39 40 41
        'inputfile'         => $inputfile,
        'max_chunk_length'  => 20000, ## big enough for all sequences
        'output_prefix'     => './test1_',
        'output_suffix'     => '.fa',
42 43 44 45 46 47 48 49 50 51 52 53 54 55
    },
    [
        [
            'DATAFLOW',
            {
                'chunk_number' => 1,
                'chunk_length' => 3360,
                'chunk_size' => 3,
                'chunk_name' => './test1_1.fa'
            },
            2
        ]
    ],
);
Roy Storey's avatar
Roy Storey committed
56 57 58 59 60


##
## do some checks
##
Roy Storey's avatar
Roy Storey committed
61
my $expected_filename = 'test1_1.fa';
Roy Storey's avatar
Roy Storey committed
62 63
ok(-e $expected_filename, 'output file exists');

64
is((stat($expected_filename))[7], (stat($inputfile))[7], 'file size of input == output');
Roy Storey's avatar
Roy Storey committed
65

Roy Storey's avatar
Roy Storey committed
66 67
my @all_files = glob('test1_*.fa');
is(@all_files, 1, 'exactly one output file - test 1');
Roy Storey's avatar
Roy Storey committed
68

69

Roy Storey's avatar
Roy Storey committed
70 71 72
## 
## next job
##
73 74
standaloneJob('Bio::EnsEMBL::Hive::RunnableDB::FastaFactory',
    {
75 76 77 78
        'inputfile'         => $inputfile,
        'max_chunk_length'  => 200, ## smaller than all sequences
        'output_prefix'     => './test2_',
        'output_suffix'     => '.fa',
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
    },
    [
        [
            'DATAFLOW',
            {
                'chunk_number' => 1,
                'chunk_length' => 640,
                'chunk_size' => 1,
                'chunk_name' => './test2_1.fa'
            },
            2
        ],
        [
            'DATAFLOW',
            {
                'chunk_number' => 2,
                'chunk_length' => 1280,
                'chunk_size' => 1,
                'chunk_name' => './test2_2.fa'
            },
            2
        ],
        [
            'DATAFLOW',
            {
                'chunk_number' => 3,
                'chunk_length' => 1440,
                'chunk_size' => 1,
                'chunk_name' => './test2_3.fa'
            },
            2
        ],
    ],
);
Roy Storey's avatar
Roy Storey committed
113 114


Roy Storey's avatar
Roy Storey committed
115 116
$expected_filename = 'test2_1.fa';
ok(-e $expected_filename, 'output file exists');
Roy Storey's avatar
Roy Storey committed
117

Roy Storey's avatar
Roy Storey committed
118 119 120 121 122 123 124 125 126 127 128
@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 ],

    'test3_1.fa' => [ 1975 ],
    'test3_2.fa' => [ 1475 ],
129 130 131 132 133 134 135 136 137 138 139

    'test4_1.fa' => [ 402 ],
    'test4_2.fa' => [ 386 ],
    'test4_3.fa' => [ 408 ],
    'test4_4.fa' => [ 408 ],
    'test4_5.fa' => [ 408 ],
    'test4_6.fa' => [ 368 ],
    'test4_7.fa' => [ 408 ],
    'test4_8.fa' => [ 408 ],
    'test4_9.fa' => [ 408 ],
    '0/test4_10.fa' => [ 204 ],
Roy Storey's avatar
Roy Storey committed
140 141 142 143 144 145
};

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
146

Roy Storey's avatar
Roy Storey committed
147 148 149
## 
## next job
##
150 151
standaloneJob('Bio::EnsEMBL::Hive::RunnableDB::FastaFactory',
    {
152 153 154 155
        'inputfile'         => $inputfile,
        'max_chunk_length'  => 1000, ## smaller than two combined sequences
        'output_prefix'     => './test3_',
        'output_suffix'     => '.fa',
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    },
    [
        [
            'DATAFLOW',
            {
                'chunk_number' => 1,
                'chunk_length' => 1920,
                'chunk_size' => 2,
                'chunk_name' => './test3_1.fa'
            },
            2
        ],
        [
            'DATAFLOW',
            {
                'chunk_number' => 2,
                'chunk_length' => 1440,
                'chunk_size' => 1,
                'chunk_name' => './test3_2.fa'
            },
            2
        ],
    ],
);
Roy Storey's avatar
Roy Storey committed
180 181 182 183 184 185 186 187 188 189 190 191

$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
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
##
## next job
##

# New input file that has shorter sequences
my $new_inputfile = 'test_input.fa';
system(q{awk '!($0 ~ /^>/) {print ">seq"NR; print $0}' }.qq{ $inputfile > $new_inputfile});

standaloneJob('Bio::EnsEMBL::Hive::RunnableDB::FastaFactory', {
        'inputfile'         => $new_inputfile,
        'max_chunk_length'  => 300, ## such that some files will be in sub-directories
        'output_prefix'     => './test4_',
        'output_suffix'     => '.fa',
        'hash_directories'  => 1,
});

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

@all_files = (glob('test4_*.fa'), glob('0/test4_*.fa'));
is(@all_files, 10, 'correct number of output files - test 4');
# 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)");
}

221
chdir $orig;
222

Roy Storey's avatar
Roy Storey committed
223
done_testing();