| 1 |
#!/usr/bin/env perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Cwd; |
| 5 |
use File::Path 'rmtree'; |
| 6 |
use File::Temp; |
| 7 |
use Test::Files qw(compare_ok); |
| 8 |
|
| 9 |
BEGIN { unshift( @INC, './lib' ) } |
| 10 |
BEGIN { unshift( @INC, '../lib' ) } |
| 11 |
|
| 12 |
BEGIN { |
| 13 |
use Test::Most; |
| 14 |
use Test::Exception; |
| 15 |
use_ok('Bio::Tradis::RunTradis'); |
| 16 |
} |
| 17 |
|
| 18 |
my $output_directory_obj = File::Temp->newdir( 'tmp_run_tradis_tests_XXXXX', |
| 19 |
CLEANUP => 0, |
| 20 |
DIR => cwd() ); |
| 21 |
my $output_directory = $output_directory_obj->dirname; |
| 22 |
my $temp_directory_obj = File::Temp->newdir( CLEANUP => 0, |
| 23 |
DIR => $output_directory ); |
| 24 |
my $temp_directory = $temp_directory_obj->dirname(); |
| 25 |
|
| 26 |
my ( $obj, $fastqfile, $stats_handle, $ref, $tag, $outfile, $aligner); |
| 27 |
|
| 28 |
# First, test all parts and complete pipeline without mismatch |
| 29 |
|
| 30 |
$fastqfile = "t/data/RunTradisBWA/test.tagged.fastq"; |
| 31 |
$ref = "t/data/RunTradisBWA/smallref.fa"; |
| 32 |
$tag = "TAAGAGTCAG"; |
| 33 |
$outfile = "test.plot"; |
| 34 |
$aligner = 0; |
| 35 |
open( $stats_handle, '>', "$output_directory/test.stats" ); |
| 36 |
|
| 37 |
ok( |
| 38 |
$obj = Bio::Tradis::RunTradis->new( |
| 39 |
fastqfile => $fastqfile, |
| 40 |
reference => $ref, |
| 41 |
tag => $tag, |
| 42 |
smalt => $aligner, |
| 43 |
outfile => $outfile, |
| 44 |
output_directory => $output_directory, |
| 45 |
_temp_directory => $temp_directory, |
| 46 |
_stats_handle => $stats_handle |
| 47 |
), |
| 48 |
'creating object - Normal files, no mismatch' |
| 49 |
); |
| 50 |
|
| 51 |
# Filtering step |
| 52 |
ok( $obj->_filter, 'testing filtering step' ); |
| 53 |
ok( |
| 54 |
-e "$temp_directory/filter.fastq", |
| 55 |
'checking filtered file existence - Normal files, no mismatch' |
| 56 |
); |
| 57 |
compare_ok( |
| 58 |
"$temp_directory/filter.fastq", |
| 59 |
't/data/RunTradisBWA/filtered.fastq', |
| 60 |
'checking filtered file contents - Normal files, no mismatch' |
| 61 |
); |
| 62 |
|
| 63 |
# Check filtering step |
| 64 |
ok( $obj->_check_filter, 'testing check filtering step' ); |
| 65 |
system("mv $temp_directory/filter.fastq $temp_directory/filter.fastq.bak"); |
| 66 |
throws_ok {$obj->_check_filter} 'Bio::Tradis::Exception::TagFilterError', 'complain if no filtered reads'; |
| 67 |
system("touch $temp_directory/filter.fastq"); |
| 68 |
throws_ok {$obj->_check_filter} 'Bio::Tradis::Exception::TagFilterError', 'complain if filtered reads are empty'; |
| 69 |
system("echo foo > $temp_directory/filter.fastq"); |
| 70 |
throws_ok {$obj->_check_filter} 'Bio::Tradis::Exception::TagFilterError', 'complain if filtered reads has less than 4 lines'; |
| 71 |
system("echo 'foo\nbar\nbaz\nquux' > $temp_directory/filter.fastq"); |
| 72 |
throws_ok {$obj->_check_filter} 'Bio::Tradis::Exception::TagFilterError', 'complain if filtered reads do not look like a fastq'; |
| 73 |
system("echo 'foo\nbar\n+' > $temp_directory/filter.fastq"); |
| 74 |
throws_ok {$obj->_check_filter} 'Bio::Tradis::Exception::TagFilterError', 'complain if filtered reads are too short'; |
| 75 |
system("echo 'foo\nbar\n+\nquux' > $temp_directory/filter.fastq"); |
| 76 |
ok( $obj->_check_filter, 'check very basic filtered reads validation'); |
| 77 |
system("mv $temp_directory/filter.fastq.bak $temp_directory/filter.fastq"); |
| 78 |
|
| 79 |
# Tag removal |
| 80 |
ok( $obj->_remove, 'testing tag removal' ); |
| 81 |
ok( -e "$temp_directory/tags_removed.fastq", |
| 82 |
'checking de-tagged file existence - Normal files, no mismatch' ); |
| 83 |
compare_ok( |
| 84 |
"$temp_directory/tags_removed.fastq", |
| 85 |
't/data/RunTradisBWA/notags.fastq', |
| 86 |
'checking de-tagged file contents - Normal files, no mismatch' |
| 87 |
); |
| 88 |
|
| 89 |
# Mapping |
| 90 |
ok( $obj->_map, 'testing mapping' ); |
| 91 |
ok( -e "$temp_directory/mapped.sam", 'checking SAM existence' ); |
| 92 |
`grep -v "\@PG" $temp_directory/mapped.sam > $output_directory/tmp1.sam`; |
| 93 |
`grep -v "\@PG" t/data/RunTradisBWA/mapped.sam > $output_directory/tmp2.sam`; |
| 94 |
compare_ok( |
| 95 |
"$output_directory/tmp1.sam", |
| 96 |
"$output_directory/tmp2.sam", |
| 97 |
'checking mapped file contents' |
| 98 |
); |
| 99 |
|
| 100 |
# Conversion |
| 101 |
ok( $obj->_sam2bam, 'testing SAM/BAM conversion' ); |
| 102 |
ok( -e "$temp_directory/mapped.bam", 'checking BAM existence' ); |
| 103 |
|
| 104 |
# Sorting |
| 105 |
ok( $obj->_sort_bam, 'testing BAM sorting' ); |
| 106 |
ok( -e "$temp_directory/mapped.sort.bam", |
| 107 |
'checking sorted BAM existence - Normal files, no mismatch' ); |
| 108 |
ok( -e "$temp_directory/mapped.sort.bam.bai", |
| 109 |
'checking indexed BAM existence - Normal files, no mismatch' ); |
| 110 |
|
| 111 |
#Bamcheck |
| 112 |
ok( $obj->_bamcheck, 'testing bamcheck' ); |
| 113 |
ok( -e "$temp_directory/mapped.bamcheck", |
| 114 |
'checking bamcheck file existence - Normal files, no mismatch' ); |
| 115 |
|
| 116 |
# Plot |
| 117 |
ok( $obj->_make_plot, 'testing plotting' ); |
| 118 |
ok( -e "$temp_directory/test.plot.AE004091.insert_site_plot.gz", |
| 119 |
'checking plot file existence - Normal files, no mismatch' ); |
| 120 |
system( |
| 121 |
"gunzip -c $temp_directory/test.plot.AE004091.insert_site_plot.gz > $output_directory/test.plot.unzipped" |
| 122 |
); |
| 123 |
system("gunzip -c t/data/RunTradisBWA/expected.plot.gz > $output_directory/expected.plot.unzipped"); |
| 124 |
compare_ok( |
| 125 |
"$output_directory/test.plot.unzipped", |
| 126 |
"$output_directory/expected.plot.unzipped", |
| 127 |
'checking plot file contents - Normal files, no mismatch' |
| 128 |
); |
| 129 |
|
| 130 |
|
| 131 |
# Complete pipeline |
| 132 |
ok( $obj->run_tradis, 'testing complete analysis - Normal files, no mismatch' ); |
| 133 |
ok( -e "$output_directory/test.plot.AE004091.insert_site_plot.gz", |
| 134 |
'checking plot file existence - Normal files, no mismatch' ); |
| 135 |
system("gunzip -c $output_directory/test.plot.AE004091.insert_site_plot.gz > $output_directory/test.plot.unzipped"); |
| 136 |
system("gunzip -c t/data/RunTradisBWA/expected.plot.gz > $output_directory/expected.plot.unzipped"); |
| 137 |
compare_ok( |
| 138 |
"$output_directory/test.plot.unzipped", |
| 139 |
"$output_directory/expected.plot.unzipped", |
| 140 |
'checking completed pipeline file contents - Normal files, no mismatch' |
| 141 |
); |
| 142 |
|
| 143 |
unlink("$temp_directory/filter.fastq"); |
| 144 |
unlink("$temp_directory/tags_removed.fastq"); |
| 145 |
|
| 146 |
unlink("$output_directory/test.plot.AE004091.insert_site_plot.gz"); |
| 147 |
unlink("$output_directory/expected.plot.unzipped"); |
| 148 |
unlink("$output_directory/test.plot.unzipped"); |
| 149 |
|
| 150 |
# Test complete pipeline with 1 mismatch allowed |
| 151 |
|
| 152 |
$temp_directory_obj = File::Temp->newdir( CLEANUP => 0, |
| 153 |
DIR => $output_directory ); |
| 154 |
$temp_directory = $temp_directory_obj->dirname(); |
| 155 |
|
| 156 |
ok( |
| 157 |
$obj = Bio::Tradis::RunTradis->new( |
| 158 |
fastqfile => $fastqfile, |
| 159 |
reference => $ref, |
| 160 |
tag => $tag, |
| 161 |
smalt => $aligner, |
| 162 |
outfile => $outfile, |
| 163 |
mismatch => 1, |
| 164 |
output_directory => $output_directory, |
| 165 |
_temp_directory => $temp_directory, |
| 166 |
_stats_handle => $stats_handle |
| 167 |
), |
| 168 |
'creating object - Normal files one mismatch' |
| 169 |
); |
| 170 |
|
| 171 |
ok( $obj->run_tradis, 'testing complete analysis with mismatch' ); |
| 172 |
ok( -e "$output_directory/test.plot.AE004091.insert_site_plot.gz", |
| 173 |
'checking plot file existence - Normal files one mismatch' ); |
| 174 |
system("gunzip -c $output_directory/test.plot.AE004091.insert_site_plot.gz > $output_directory/test.plot.unzipped"); |
| 175 |
|
| 176 |
|
| 177 |
system("gunzip -c t/data/RunTradisBWA/expected.1mm.plot.gz > $output_directory/expected.plot.unzipped"); |
| 178 |
compare_ok( |
| 179 |
"$output_directory/test.plot.unzipped", |
| 180 |
"$output_directory/expected.plot.unzipped", |
| 181 |
'checking completed pipeline with mismatch file contents - Normal files one mismatch' |
| 182 |
); |
| 183 |
|
| 184 |
unlink("$output_directory/tmp1.sam"); |
| 185 |
unlink("$output_directory/tmp2.sam"); |
| 186 |
unlink("$output_directory/test.plot.AE004091.insert_site_plot.gz"); |
| 187 |
unlink("$output_directory/expected.plot.unzipped"); |
| 188 |
unlink("$output_directory/test.plot.unzipped"); |
| 189 |
|
| 190 |
|
| 191 |
# Test pipeline with gzipped input |
| 192 |
$temp_directory_obj = File::Temp->newdir( CLEANUP => 0, |
| 193 |
DIR => $output_directory ); |
| 194 |
$temp_directory = $temp_directory_obj->dirname(); |
| 195 |
$fastqfile = "t/data/RunTradisBWA/test.tagged.fastq.gz"; |
| 196 |
ok( |
| 197 |
$obj = Bio::Tradis::RunTradis->new( |
| 198 |
fastqfile => $fastqfile, |
| 199 |
reference => $ref, |
| 200 |
tag => $tag, |
| 201 |
smalt => $aligner, |
| 202 |
outfile => $outfile, |
| 203 |
output_directory => $output_directory, |
| 204 |
_temp_directory => $temp_directory, |
| 205 |
_stats_handle => $stats_handle |
| 206 |
), |
| 207 |
'creating object with gzipped data - Normal files one mismatch' |
| 208 |
); |
| 209 |
|
| 210 |
ok( $obj->run_tradis, 'testing complete analysis with gzipped data' ); |
| 211 |
ok( |
| 212 |
-e "$output_directory/test.plot.AE004091.insert_site_plot.gz", |
| 213 |
'checking plot file existence (gzipped data) - Normal files one mismatch' |
| 214 |
); |
| 215 |
ok( -e "$output_directory/test.plot.mapped.bam", 'checking mapped bam existence - Normal files one mismatch'); |
| 216 |
ok( -e "$output_directory/test.plot.mapped.bam.bai", 'checking indexed bam file - Normal files one mismatch'); |
| 217 |
|
| 218 |
system("gunzip -c $output_directory/test.plot.AE004091.insert_site_plot.gz > $output_directory/test.plot.unzipped"); |
| 219 |
system("gunzip -c t/data/RunTradisBWA/expected.plot.gz > $output_directory/expected.plot.unzipped"); |
| 220 |
compare_ok( |
| 221 |
"$output_directory/test.plot.unzipped", |
| 222 |
"$output_directory/expected.plot.unzipped", |
| 223 |
'checking completed pipeline with gzipped data file contents - Normal files one mismatch' |
| 224 |
); |
| 225 |
|
| 226 |
$temp_directory_obj = File::Temp->newdir( CLEANUP => 0, |
| 227 |
DIR => $output_directory ); |
| 228 |
$temp_directory = $temp_directory_obj->dirname(); |
| 229 |
ok( |
| 230 |
$obj = Bio::Tradis::RunTradis->new( |
| 231 |
fastqfile => $fastqfile, |
| 232 |
reference => $ref, |
| 233 |
tag => $tag, |
| 234 |
smalt => $aligner, |
| 235 |
outfile => $outfile, |
| 236 |
output_directory => $output_directory, |
| 237 |
_temp_directory => $temp_directory, |
| 238 |
_stats_handle => $stats_handle, |
| 239 |
k => 10, |
| 240 |
|
| 241 |
), |
| 242 |
'creating object with custom smalt parameters' |
| 243 |
); |
| 244 |
# Filtering step |
| 245 |
$obj->_filter; |
| 246 |
$obj->_remove; |
| 247 |
ok( $obj->_map, 'mapping with custom parameters fine' ); |
| 248 |
|
| 249 |
|
| 250 |
# Check die if ref is not found |
| 251 |
$temp_directory_obj = File::Temp->newdir( CLEANUP => 0, |
| 252 |
DIR => $output_directory ); |
| 253 |
$temp_directory = $temp_directory_obj->dirname(); |
| 254 |
ok( |
| 255 |
$obj = Bio::Tradis::RunTradis->new( |
| 256 |
fastqfile => $fastqfile, |
| 257 |
reference => "not_really_a_ref.fa", |
| 258 |
tag => $tag, |
| 259 |
outfile => $outfile, |
| 260 |
output_directory => $output_directory, |
| 261 |
_temp_directory => $temp_directory, |
| 262 |
_stats_handle => $stats_handle, |
| 263 |
|
| 264 |
), |
| 265 |
'creating object' |
| 266 |
); |
| 267 |
throws_ok {$obj->run_tradis} 'Bio::Tradis::Exception::RefNotFound', 'correct error thrown'; |
| 268 |
|
| 269 |
unlink('t/data/RunTradisBWA/smallref.fa.amb'); |
| 270 |
unlink('t/data/RunTradisBWA/smallref.fa.ann'); |
| 271 |
unlink('t/data/RunTradisBWA/smallref.fa.bwt'); |
| 272 |
unlink('t/data/RunTradisBWA/smallref.fa.pac'); |
| 273 |
unlink('t/data/RunTradisBWA/smallref.fa.sa'); |
| 274 |
rmtree($output_directory); |
| 275 |
done_testing(); |