-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclimportfastq.pl
executable file
·369 lines (338 loc) · 9.2 KB
/
climportfastq.pl
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use strict;
use Fcntl ':flock';
use File::Spec;
my $buildno = '0.9.x';
my $devnull = File::Spec->devnull();
# options
my $compress = 'gz';
my $append;
my $numthreads = 1;
# Input/Output
my $inputfile;
my $outputfolder;
# other variables
my @inputfiles;
my %in2out;
# file handles
my $filehandleinput1;
my $filehandleoutput1;
&main();
sub main {
# print startup messages
&printStartupMessage();
# get command line arguments
&getOptions();
# check variable consistency
&checkVariables();
# read input file
&readInputFile();
# process FASTQ files
&processFASTQ();
}
sub printStartupMessage {
print(STDERR <<"_END");
climportfastq $buildno
=======================================================================
Official web site of this script is
https://www.fifthdimension.jp/products/claident/ .
To know script details, see above URL.
Copyright (C) 2011-XXXX Akifumi S. Tanabe
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
_END
# display usage if command line options were not specified
unless (@ARGV) {
&helpMessage();
}
}
sub getOptions {
$inputfile = $ARGV[-2];
unless (-e $inputfile) {
&errorMessage(__LINE__, "\"$inputfile\" does not exist.");
}
$outputfolder = $ARGV[-1];
for (my $i = 0; $i < scalar(@ARGV) - 2; $i ++) {
if ($ARGV[$i] =~ /^-+compress=(.+)$/i) {
my $value = $1;
if ($value =~ /^(?:g|gz|gzip)$/i) {
$compress = 'gz';
}
elsif ($value =~ /^(?:b|bz|bz2|bzip|bzip2)$/i) {
$compress = 'bz2';
}
elsif ($value =~ /^(?:x|xz)$/i) {
$compress = 'xz';
}
elsif ($value =~ /^(?:disable|d|no|n|false|f)$/i) {
$compress = 0;
}
else {
&errorMessage(__LINE__, "\"$ARGV[$i]\" is invalid option.");
}
}
elsif ($ARGV[$i] =~ /^-+(?:a|append)$/i) {
$append = 1;
}
elsif ($ARGV[$i] =~ /^-+(?:n|n(?:um)?threads?)=(\d+)$/i) {
$numthreads = $1;
}
else {
&errorMessage(__LINE__, "\"$ARGV[$i]\" is unknown option.");
}
}
}
sub checkVariables {
if (!$append && -e $outputfolder) {
&errorMessage(__LINE__, "\"$outputfolder\" already exists.");
}
}
sub readInputFile {
print(STDERR "Reading input file...\n");
my %tempin;
my %tempout;
unless (open($filehandleinput1, "< $inputfile")) {
&errorMessage(__LINE__, "Cannot open \"$inputfile\".");
}
while (<$filehandleinput1>) {
if (/^(\S+)\s+(\S+)/) {
my $tempin = $1;
my $tempout = $2;
if ($tempout =~ /\//) {
&errorMessage(__LINE__, "The output file name \"$tempout\" is invalid.");
}
{
my @tempout = split(/__/, $tempout);
if (scalar(@tempout) != 3) {
&errorMessage(__LINE__, "The output file name \"$tempout\" is invalid.");
}
}
if ($tempin{$tempin}) {
&errorMessage(__LINE__, "The input file \"$tempin\" is doubly specified.");
}
else {
$tempin{$tempin} = 1;
}
if ($tempout{$tempout}) {
&errorMessage(__LINE__, "The output file \"$tempout\" is doubly specified.");
}
else {
$tempout{$tempout} = 1;
}
$in2out{$tempin} = $tempout;
push(@inputfiles, $tempin);
}
}
close($filehandleinput1);
print(STDERR "done.\n\n");
}
sub processFASTQ {
print(STDERR "Processing FASTQs...\n");
if (!-e $outputfolder && !mkdir($outputfolder)) {
&errorMessage(__LINE__, "Cannot make output folder.");
}
foreach my $tempin (@inputfiles) {
print(STDERR "Processing \"$tempin\"...");
{
$filehandleinput1 = &readFile($tempin);
my $tempnline = 1;
my $seqname;
my $nucseq;
my $qualseq;
my %pid;
my $child = 0;
my $nchild = 1;
$| = 1;
$? = 0;
# Processing FASTQ in parallel
while (<$filehandleinput1>) {
s/\r?\n?$//;
if ($tempnline % 4 == 1 && /^\@(.+)/) {
$seqname = $1;
}
elsif ($tempnline % 4 == 2) {
s/[^a-zA-Z]//g;
$nucseq = uc($_);
}
elsif ($tempnline % 4 == 3 && /^\+/) {
$tempnline ++;
next;
}
elsif ($tempnline % 4 == 0 && $seqname) {
s/\s//g;
$qualseq = $_;
if (my $pid = fork()) {
$pid{$pid} = $child;
if ($nchild == $numthreads * 2) {
my $endpid = wait();
while (!exists($pid{$endpid}) && $endpid != -1) {
$endpid = wait();
}
if (exists($pid{$endpid})) {
$child = $pid{$endpid};
delete($pid{$endpid});
}
elsif ($endpid == -1) {
$child = 0;
}
}
elsif ($nchild < $numthreads * 2) {
$child = $nchild;
$nchild ++;
}
if ($?) {
&errorMessage(__LINE__);
}
undef($seqname);
undef($nucseq);
undef($qualseq);
$tempnline ++;
next;
}
else {
#print(STDERR "Thread $child\n");
if (!-e "$outputfolder/$in2out{$tempin}") {
mkdir("$outputfolder/$in2out{$tempin}");
}
unless (open($filehandleoutput1, "+>> $outputfolder/$in2out{$tempin}/$child.fastq")) {
&errorMessage(__LINE__, "Cannot write \"$outputfolder/$in2out{$tempin}/$child.fastq\".");
}
unless (flock($filehandleoutput1, LOCK_EX)) {
&errorMessage(__LINE__, "Cannot lock \"$outputfolder/$in2out{$tempin}/$child.fastq\".");
}
unless (seek($filehandleoutput1, 0, 2)) {
&errorMessage(__LINE__, "Cannot seek \"$outputfolder/$in2out{$tempin}/$child.fastq\".");
}
print($filehandleoutput1 "\@$seqname SN:$in2out{$tempin}\n$nucseq\n+\n$qualseq\n");
close($filehandleoutput1);
exit;
}
}
else {
&errorMessage(__LINE__, "Invalid FASTQ.\nFile: $inputfiles[0]\nLine: $tempnline");
}
$tempnline ++;
}
close($filehandleinput1);
# join processes
while (wait != -1) {
if ($?) {
&errorMessage(__LINE__, 'Cannot split sequence file correctly.');
}
}
}
# join files
{
if ($compress) {
$filehandleoutput1 = writeFile("$outputfolder/$in2out{$tempin}.fastq.$compress");
}
else {
$filehandleoutput1 = writeFile("$outputfolder/$in2out{$tempin}.fastq");
}
foreach my $fastq (glob("$outputfolder/$in2out{$tempin}/*.fastq")) {
unless (open($filehandleinput1, "< $fastq")) {
&errorMessage(__LINE__, "Cannot open \"$fastq\".");
}
while (<$filehandleinput1>) {
print($filehandleoutput1 $_);
}
close($filehandleinput1);
unlink($fastq);
}
close($filehandleoutput1);
rmdir("$outputfolder/$in2out{$tempin}");
}
print(STDERR "done.\n");
}
print(STDERR "done.\n\n");
}
sub readFile {
my $filehandle;
my $filename = shift(@_);
if ($filename =~ /\.gz$/i) {
unless (open($filehandle, "pigz -p 8 -dc $filename 2> $devnull |")) {
&errorMessage(__LINE__, "Cannot open \"$filename\".");
}
}
elsif ($filename =~ /\.bz2$/i) {
unless (open($filehandle, "lbzip2 -n 8 -dc $filename 2> $devnull |")) {
&errorMessage(__LINE__, "Cannot open \"$filename\".");
}
}
elsif ($filename =~ /\.xz$/i) {
unless (open($filehandle, "xz -T 8 -dc $filename 2> $devnull |")) {
&errorMessage(__LINE__, "Cannot open \"$filename\".");
}
}
else {
unless (open($filehandle, "< $filename")) {
&errorMessage(__LINE__, "Cannot open \"$filename\".");
}
}
return($filehandle);
}
sub writeFile {
my $filehandle;
my $filename = shift(@_);
if ($filename =~ /\.gz$/i) {
unless (open($filehandle, "| pigz -p $numthreads -c >> $filename 2> $devnull")) {
&errorMessage(__LINE__, "Cannot open \"$filename\".");
}
}
elsif ($filename =~ /\.bz2$/i) {
unless (open($filehandle, "| lbzip2 -n $numthreads -c >> $filename 2> $devnull")) {
&errorMessage(__LINE__, "Cannot open \"$filename\".");
}
}
elsif ($filename =~ /\.xz$/i) {
unless (open($filehandle, "| xz -T $numthreads -c >> $filename 2> $devnull")) {
&errorMessage(__LINE__, "Cannot open \"$filename\".");
}
}
else {
unless (open($filehandle, ">> $filename")) {
&errorMessage(__LINE__, "Cannot open \"$filename\".");
}
}
return($filehandle);
}
sub errorMessage {
my $lineno = shift(@_);
my $message = shift(@_);
print(STDERR "ERROR!: line $lineno\n$message\n");
print(STDERR "If you want to read help message, run this script without options.\n");
exit(1);
}
sub helpMessage {
print(STDERR <<"_END");
Usage
=====
climportfastq options inputfile outputfolder
Command line options
====================
--compress=GZIP|BZIP2|XZ|DISABLE
Specify compress output files or not. (default: GZIP)
-a, --append
Specify outputfile append or not. (default: off)
-n, --numthreads=INTEGER
Specify the number of processes. (default: 1)
Acceptable input file formats
=============================
Tab-delimited text like below.
inputfilename samplename
Note that samplename must be compliant with the following style.
RunID__TagID__PrimerID
Acceptable sequence file formats
================================
FASTQ (uncompressed, gzip-compressed, bzip2-compressed, or xz-compressed)
(Quality values must be encoded in Sanger format.)
_END
exit;
}