-
Notifications
You must be signed in to change notification settings - Fork 3
/
fix_sra_fastq.sh
executable file
·186 lines (151 loc) · 7.08 KB
/
fix_sra_fastq.sh
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
#!/bin/bash
#
# Copyright (C) 2021-2024 - Gert Hulselmans
#
# Purpose:
# Fix read names of FASTQ files generated with SRA Toolkit.
# If an Illumina read name is specified as first comment in the FASTQ
# comment field, it will be used as new read name, else the SRR read
# name will be used (without comment fields).
set -e
set -o pipefail
decompress_fastq_cat_cmd='cat';
decompress_fastq_zcat_cmd='zcat';
decompress_fastq_igzip_cmd='igzip -c -d';
# Number of threads to use to compress each FASTQ output file.
compress_fastq_threads="${compress_fastq_threads:-4}";
# Gzip compression level for bgzip, pigz and gzip.
compress_fastq_level="${compress_fastq_level:-6}";
# Gzip compression level for igzip (3 is maximum).
compress_fastq_igzip_level="3";
compress_fastq_bgzip_cmd="bgzip -@ ${compress_fastq_threads} -l ${compress_fastq_level} -c";
compress_fastq_pigz_cmd="pigz -p ${compress_fastq_threads} -${compress_fastq_level} -c";
compress_fastq_igzip_cmd="igzip -${compress_fastq_igzip_level} -c";
compress_fastq_gzip_cmd="gzip -${compress_fastq_level} -c";
fix_sra_fastq () {
local fastq_input_filename="${1}";
local fastq_output_filename="${2}";
local compress_fastq_cmd="${3:-bgzip}";
if [ ${#@} -lt 2 ] ; then
printf '\nUsage:\n';
printf ' fix_sra_fastq \\\n';
printf ' fastq_input \\\n';
printf ' fastq_output \\\n';
printf ' <compress_fastq_cmd [bgzip|pigz|gzip|stdout|-|uncompressed]> \\\n\n';
printf 'Purpose: Fix read names of FASTQ files generated with SRA Toolkit.\n';
printf ' If an Illumina read name is specified as first comment in the FASTQ\n';
printf ' comment field, it will be used as new read name, else the SRR read\n';
printf ' name will be used (without comment fields).\n\n';
printf 'Parameters:\n';
printf ' - fastq_input: FASTQ input filename created with SRA toolkit (uncompressed or gzipped).\n';
printf ' - fastq_output: FASTQ output filename with fixed read name (uncompressed or gzipped).\n';
printf ' - compress_fastq_cmd:\n';
printf ' - Compression program to use for output FASTQ files:\n';
printf " - \"bgzip\": '%s' (default)\n" "${compress_fastq_bgzip_cmd}";
printf " - \"pigz\": '%s'\n" "${compress_fastq_pigz_cmd}";
printf " - \"igzip\": '%s' (very fast, low compression)\n" "${compress_fastq_igzip_cmd}";
printf " - \"gzip\": '%s'\n" "${compress_fastq_gzip_cmd}";
printf ' - "stdout": Write uncompressed output to stdout.\n';
printf ' - "-": Write uncompressed output to stdout.\n';
printf ' - "uncompressed": Write uncompressed FASTQ files.\n';
printf ' - full custom command\n\n';
printf ' To change number of compression threads:\n';
printf ' - export compress_fastq_threads="%s"\n\n' "${compress_fastq_threads}";
printf ' To change compression level:\n';
printf ' - export compress_fastq_level="%s"\n\n' "${compress_fastq_level}";
return 1;
fi
if type igzip > /dev/null 2>&1 ; then
# Decompress gzipped FASTQ files with igzip if installed (6x faster than gzip).
local decompress_fastq_gzipped_cmd="${decompress_fastq_igzip_cmd}";
else
# Decompress gzipped FASTQ files with gzip.
local decompress_fastq_gzipped_cmd="${decompress_fastq_zcat_cmd}";
fi
# Detect if input FASTQ files are gzip compressed or not.
if [ "${fastq_input_filename}" != "${fastq_input_filename%.gz}" ] ; then
local decompress_input_fastq_cmd="${decompress_fastq_gzipped_cmd}";
else
local decompress_input_fastq_cmd="${decompress_fastq_cat_cmd}";
fi
if [ "${fastq_output_filename}" == "${fastq_output_filename%.gz}" ] ; then
case "${fastq_output_filename}" in
-|stdout|/dev/stdout)
# Write uncompressed FASTQ file to stdout.
fastq_output_filename='/dev/stdout';
local compress_fastq_cmd="cat";;
esac
fi
case "${compress_fastq_cmd}" in
bgzip)
local compress_fastq_cmd="${compress_fastq_bgzip_cmd}";;
pigz)
local compress_fastq_cmd="${compress_fastq_pigz_cmd}";;
igzip)
local compress_fastq_cmd="${compress_fastq_igzip_cmd}";;
gzip)
local compress_fastq_cmd="${compress_fastq_gzip_cmd}";;
stdout|-)
# Write uncompressed FASTQ file to stdout.
fastq_output_filename='/dev/stdout';
local compress_fastq_cmd="cat";;
uncompressed)
# Write uncompressed FASTQ file.
fastq_output_filename="${fastq_R1_output_filename%.gz}";
local compress_fastq_cmd="cat";;
esac
if ! type mawk > /dev/null 2>&1 ; then
printf 'Error: "mawk" not found or executable.\n';
return 1;
fi
if ! type "${compress_fastq_cmd%% *}" > /dev/null 2>&1 ; then
printf 'Error: "%s" not found or executable.\n' "${compress_fastq_cmd%% *}";
return 1;
fi
mawk \
-v fastq_input_filename="${fastq_input_filename}" \
-v fastq_output_filename="${fastq_output_filename}" \
-v decompress_input_fastq_cmd="${decompress_input_fastq_cmd}" \
-v compress_fastq_cmd="${compress_fastq_cmd}" \
-F ' ' \
'
BEGIN {
read_fastq_input_cmd = decompress_input_fastq_cmd " " fastq_input_filename;
write_fastq_output_cmd = compress_fastq_cmd " > " fastq_output_filename;
fastq_line_number = 0;
# Read FASTQ input file.
while ( (read_fastq_input_cmd | getline) > 0 ) {
fastq_line_number += 1;
fastq_part = fastq_line_number % 4;
if ( fastq_part == 1 ) {
# Read name lines with comments.
if ( $1 ~ /^@SRR/ ) {
# FASTQ file was generated with SRA Toolkit (fastq-dump or fasterq-dump).
if (NF >= 3 && $2 ~ /^[A-Za-z0-9]+:[A-Za-z0-9]+:/ ) {
# Assume it is the original read name given by the Illumina sequencer.
read_name = $2;
} else {
# Use the read name given by SRA Toolkit.
read_name = substr($1, 2);
}
} else {
# Just keep the FASTQ name and trow away the comment.
read_name = substr($1, 2);
}
} else if ( fastq_part == 2 ) {
# Sequence lines.
# Store sequence line.
sequence = $0;
} else if ( fastq_part == 0 ) {
# Quality lines.
# Write the full FASTQ record to output FASTQ file.
print "@" read_name "\n" sequence "\n+\n" $0 | write_fastq_output_cmd;
}
}
# Close open file handles.
close(read_fastq_input_cmd);
close(write_fastq_output_cmd);
}'
return $?
}
fix_sra_fastq "${@}";