Debian Codegraph @

t/Bio/Tradis/RunTradisSmalt.t

raw
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_smalt_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/RunTradisSmalt/test.tagged.fastq";
31 $ref = "t/data/RunTradisSmalt/smallref.fa";
32 $tag = "TAAGAGTCAG";
33 $outfile = "test.plot";
34 $aligner = 1;
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/RunTradisSmalt/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/RunTradisSmalt/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/RunTradisSmalt/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/RunTradisSmalt/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/RunTradisSmalt/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 system(
176 "gunzip -c t/data/RunTradisSmalt/expected.1mm.plot.gz > $output_directory/expected.plot.unzipped");
177 compare_ok(
178 "$output_directory/test.plot.unzipped",
179 "$output_directory/expected.plot.unzipped",
180 'checking completed pipeline with mismatch file contents - Normal files one mismatch'
181 );
182
183 unlink("$output_directory/tmp1.sam");
184 unlink("$output_directory/tmp2.sam");
185 unlink("$output_directory/test.plot.AE004091.insert_site_plot.gz");
186 unlink("$output_directory/expected.plot.unzipped");
187 unlink("$output_directory/test.plot.unzipped");
188
189 # Test pipeline with gzipped input
190 $temp_directory_obj = File::Temp->newdir( CLEANUP => 0,
191 DIR => $output_directory );
192 $temp_directory = $temp_directory_obj->dirname();
193 $fastqfile = "t/data/RunTradisSmalt/test.tagged.fastq.gz";
194 ok(
195 $obj = Bio::Tradis::RunTradis->new(
196 fastqfile => $fastqfile,
197 reference => $ref,
198 tag => $tag,
199 smalt => $aligner,
200 outfile => $outfile,
201 output_directory => $output_directory,
202 _temp_directory => $temp_directory,
203 _stats_handle => $stats_handle
204 ),
205 'creating object with gzipped data - Normal files one mismatch'
206 );
207
208 ok( $obj->run_tradis, 'testing complete analysis with gzipped data' );
209 ok(
210 -e "$output_directory/test.plot.AE004091.insert_site_plot.gz",
211 'checking plot file existence (gzipped data) - Normal files one mismatch'
212 );
213 ok( -e "$output_directory/test.plot.mapped.bam", 'checking mapped bam existence - Normal files one mismatch');
214 ok( -e "$output_directory/test.plot.mapped.bam.bai", 'checking indexed bam file - Normal files one mismatch');
215
216 system("gunzip -c $output_directory/test.plot.AE004091.insert_site_plot.gz > $output_directory/test.plot.unzipped");
217 system("gunzip -c t/data/RunTradisSmalt/expected.plot.gz > $output_directory/expected.plot.unzipped");
218 compare_ok(
219 "$output_directory/test.plot.unzipped",
220 "$output_directory/expected.plot.unzipped",
221 'checking completed pipeline with gzipped data file contents - Normal files one mismatch'
222 );
223
224 # Test mapping stage with custom smalt parameters
225 $temp_directory_obj = File::Temp->newdir( CLEANUP => 0,
226 DIR => $output_directory );
227 $temp_directory = $temp_directory_obj->dirname();
228 ok(
229 $obj = Bio::Tradis::RunTradis->new(
230 fastqfile => $fastqfile,
231 reference => $ref,
232 tag => $tag,
233 smalt => $aligner,
234 outfile => $outfile,
235 output_directory => $output_directory,
236 _temp_directory => $temp_directory,
237 _stats_handle => $stats_handle,
238 smalt_k => 10,
239 smalt_s => 2
240 ),
241 'creating object with custom smalt parameters'
242 );
243 # Filtering step
244 $obj->_filter;
245 $obj->_remove;
246 ok( $obj->_map, 'mapping with custom parameters fine' );
247
248 # Check die if ref is not found
249 $temp_directory_obj = File::Temp->newdir( CLEANUP => 0,
250 DIR => $output_directory );
251 $temp_directory = $temp_directory_obj->dirname();
252 ok(
253 $obj = Bio::Tradis::RunTradis->new(
254 fastqfile => $fastqfile,
255 reference => "not_really_a_ref.fa",
256 tag => $tag,
257 outfile => $outfile,
258 output_directory => $output_directory,
259 _temp_directory => $temp_directory,
260 _stats_handle => $stats_handle,
261 smalt_k => 10,
262 smalt_s => 2
263 ),
264 'creating object with custom smalt parameters'
265 );
266 throws_ok {$obj->run_tradis} 'Bio::Tradis::Exception::RefNotFound', 'correct error thrown';
267
268 rmtree($output_directory);
269 done_testing();