1
1
# !/usr/bin/perl
2
2
# #########LICENCE ##########
3
- # Copyright (c) 2015 Genome Research Ltd.
3
+ # Copyright (c) 2015-2017 Genome Research Ltd.
4
4
# ##
5
5
# Author: Cancer Genome Project <[email protected] >
6
6
# ##
@@ -86,19 +86,22 @@ sub normals_filter {
86
86
my $fusions_file = File::Spec-> catfile($tmp ," $sample .fusions" );
87
87
my $fusions_sorted = File::Spec-> catfile($tmp ," $sample .fusions.sorted" );
88
88
PCAP::Cli::file_for_reading(' fusions.reformat' , $fusions_file .' .reformat' );
89
-
89
+
90
90
# Sort the fusions file prior to joining with the normals file
91
91
system (" sort -k1,1 $fusions_file .reformat > $fusions_file .sorted" );
92
-
92
+
93
93
my $normal_file = $options -> {' normals' };
94
- system (" join -v 2 $normal_file $fusions_file .sorted > $fusions_file .filtered" );
95
-
94
+ system (" join -v 2 $normal_file $fusions_file .sorted > $fusions_file .filtered" ) && die " An error occurred: $! " ;
95
+ if (-s " $fusions_file .filtered" == 0) {
96
+ system (" echo '##EOF##' > $fusions_file .filtered" ) && die " An error occurred: $! " ;
97
+ }
98
+
96
99
return 1;
97
100
}
98
101
99
102
sub process_program_params {
100
103
my $options = shift ;
101
-
104
+
102
105
my $program = lc ($options -> {' program' });
103
106
if ($program eq ' tophat' ){
104
107
$options -> {' split-char' } = $TOPHAT_SPLIT_CHAR ;
@@ -129,7 +132,7 @@ sub process_program_params {
129
132
else {
130
133
die " An invalid program name has been entered, parameter -p should be tophat, defuse or star.\n "
131
134
}
132
-
135
+
133
136
return 1;
134
137
}
135
138
@@ -144,29 +147,29 @@ sub reformat {
144
147
145
148
open (my $ifh , $input ) or die " Could not open file '$input ' $! " ;
146
149
open (my $ofh , ' >' , $output ) or die " Could not open file '$output ' $! " ;
147
-
150
+
148
151
while (<$ifh >) {
149
152
chomp ;
150
153
my $line = $_ ;
151
-
154
+
152
155
$header_pattern = $options -> {' header-pattern' } if (exists $options -> {' header-pattern' });
153
156
if ($line =~ m /$header_pattern / ){
154
-
157
+
155
158
if ($program eq ' star' ){
156
159
$line =~ s / ([Left|Right]+)Gene/ $1Gene\t $1GeneId/ g ;
157
160
$line =~ s / ([Left|Right]+)Breakpoint/ $1Chr\t $1Pos\t $1Strand/ g ;
158
161
$line =~ s / #// ;
159
162
}
160
-
163
+
161
164
$options -> {' header' } = " breakpoint_ref\t " .$line ;
162
165
}
163
166
else {
164
167
$line =~ s /\t chr/ \t / g ;
165
168
my @fields = split $options -> {' split-char' }, $line ;
166
169
my $fusion ;
167
-
170
+
168
171
if ($program eq ' star' ){
169
-
172
+
170
173
my @break1 = split $options -> {' split-char2' }, $fields [$options -> {' chr1' } - 1];
171
174
my @break2 = split $options -> {' split-char2' }, $fields [$options -> {' chr2' } - 1];
172
175
$fields [$options -> {' chr1' } - 1] = join (" \t " ,@break1 );
@@ -177,10 +180,10 @@ sub reformat {
177
180
$fields [$options -> {' gene2' } - 1] = join (" \t " ,@gene2 );
178
181
$fusion = $break1 [0]." :" .$break1 [1]." -" .$break2 [0]." :" .$break2 [1];
179
182
$line = join (" \t " ,@fields );
180
-
183
+
181
184
}
182
185
elsif ($program eq ' tophat' ){
183
-
186
+
184
187
# Fusion positions for TopHat need adjusting by +1 so that they are directly comparable to Star and deFuse
185
188
my $orig_pos1 = $fields [$options -> {' pos1' }-1];
186
189
my $orig_pos2 = $fields [$options -> {' pos2' }-1];
@@ -190,47 +193,47 @@ sub reformat {
190
193
$fields [$options -> {' pos1' }-1] = $adj_pos1 ;
191
194
$fields [$options -> {' pos2' }-1] = $adj_pos2 ;
192
195
$line = join (" \t " ,@fields );
193
-
196
+
194
197
}
195
198
else {
196
199
$fusion = $fields [$options -> {' chr1' }-1]." :" .$fields [$options -> {' pos1' }-1]." -" .$fields [$options -> {' chr2' }-1]." :" .$fields [$options -> {' pos2' }-1];
197
200
}
198
-
201
+
199
202
print $ofh " $fusion \t $line \n " ;
200
203
}
201
- }
204
+ }
202
205
close ($ifh );
203
206
close ($ofh );
204
-
207
+
205
208
return 1;
206
209
}
207
210
208
211
sub setup {
209
212
my %opts ;
210
213
pod2usage(-msg => " \n ERROR: Options must be defined.\n " , -verbose => 1, -output => \*STDERR ) if (scalar @ARGV == 0);
211
214
$opts {' cmd' } = join " " , $0 , @ARGV ;
212
-
215
+
213
216
GetOptions( ' h|help' => \$opts {' h' },
214
217
' m|man' => \$opts {' m' },
215
218
' i|input=s' => \$opts {' input' },
216
219
' o|outdir=s' => \$opts {' outdir' },
217
220
' n|normals=s' => \$opts {' normals' },
218
221
' s|sample=s' => \$opts {' sample' },
219
- ' p|program=s' => \$opts {' program' },
222
+ ' p|program=s' => \$opts {' program' },
220
223
) or pod2usage(2);
221
224
222
225
pod2usage(-verbose => 1) if (defined $opts {' h' });
223
226
pod2usage(-verbose => 2) if (defined $opts {' m' });
224
-
227
+
225
228
PCAP::Cli::file_for_reading(' input' , $opts {' input' });
226
229
PCAP::Cli::file_for_reading(' normals' , $opts {' normals' });
227
-
230
+
228
231
# Setup details specific to each fusion detection program output file
229
232
process_program_params(\%opts );
230
-
233
+
231
234
# Check the output directory exists and is writeable, create if not
232
235
PCAP::Cli::out_dir_check(' outdir' , $opts {' outdir' });
233
-
236
+
234
237
# Create working directory for storing intermediate files
235
238
my $tmpdir = File::Spec-> catdir($opts {' outdir' }, " tmp_$opts {'program'}Filter" );
236
239
make_path($tmpdir ) unless (-d $tmpdir );
@@ -241,7 +244,7 @@ sub setup {
241
244
242
245
sub write_output {
243
246
my $options = shift ;
244
-
247
+
245
248
my $tmp = $options -> {' tmp' };
246
249
my $sample = $options -> {' sample' };
247
250
my $program = $options -> {' program' };
@@ -250,7 +253,7 @@ sub write_output {
250
253
my $fusions_file = File::Spec-> catfile($tmp ," $sample .fusions.filtered" );
251
254
my $output_file = File::Spec-> catfile($outdir ," $sample .$program -fusion.normals.filtered.txt" );
252
255
PCAP::Cli::file_for_reading(' filtered.fusions' , $fusions_file );
253
-
256
+
254
257
open (my $ifh , $fusions_file ) or die " Could not open file $fusions_file $! " ;
255
258
open (my $ofh , ' >' , $output_file ) or die " Could not open file $output_file $! " ;
256
259
print $ofh $header ." \n " ;
@@ -261,8 +264,12 @@ sub write_output {
261
264
print $ofh $line ." \n " ;
262
265
}
263
266
close ($ifh );
264
- close ($ofh );
265
-
267
+ close ($ofh );
268
+
269
+ if (-s $output_file == 0) {
270
+ system (" echo '##EOF##' > $output_file " ) && die " An error occurred: $! " ;
271
+ }
272
+
266
273
return 1;
267
274
}
268
275
0 commit comments