-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpfastq-dump
executable file
·200 lines (171 loc) · 4.29 KB
/
pfastq-dump
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
#!/bin/bash
# pfastq-dump: parallelize fastq-dump
# Original python implementation: https://github.com/rvalieris/parallel-fastq-dump
set -e
# pfastq-dump version
VERSION="0.1.6"
# default arguments
TMPDIR="$( pwd -P )"
OUTDIR="$( pwd -P )"
NTHREADS=1
# functions for version and help
print_version(){
echo "pfastq-dump version $VERSION using fastq-dump " `fastq-dump --version | awk '$1 ~ /^fastq/ { print $3 }'`
}
print_help(){
cat <<EOS
Usage:
pfastq-dump --threads <number of threads> [options] <path> [<path>...]
pfastq-dump option:
-t|--threads number of threads
-s|--sra-id SRA id
-O|--outdir output directory
--tmpdir temporary directory
Extra arguments will be passed through
Other:
-v|--version show version info
-h|--help program usage
EOS
}
# parse arguments
while [[ $# -gt 0 ]]; do
key=${1}
case ${key} in
-v|--version)
print_version
exit 0
;;
-h|--help)
print_help
exit 0
;;
-t|--threads)
NTHREADS=${2}
shift
;;
-s|--sra-id)
SRAID=${2}
shift
;;
-O|--outdir)
OUTDIR="${2}"
shift
;;
--tmpdir)
TMPDIR="${2}"
shift
;;
-Z|--stdout)
STDOUT="true"
;;
-*|--*)
OPTIONS="${OPTIONS} ${1}"
;;
*sra)
INPUT_FILES="${INPUT_FILES} ${1}"
;;
esac
shift
done
# function to exec multiple fastq dump
parallel_fastq_dump(){
local sra="${1}"
local count="${2}"
local sraid=$(basename ${sra} | sed -e 's:.sra$::')
local td="${TMPDIR}/pfd.tmp/${sraid}"
local avg=$(echo $((${count} / ${NTHREADS})))
local remain=$(echo $((${count} % ${NTHREADS})))
local out=()
local last=1
# Calculate blocks per thread
for i in $(seq ${NTHREADS}); do
local spots=$((${last} + ${avg} -1))
if [[ ${i} == ${NTHREADS} ]]; then
plus_remain=$((${spots} + ${remain}))
out+=("${last},${plus_remain}")
else
out+=("${last},${spots}")
fi
last=$((${last} + ${avg}))
done
echo "blocks: ${out[@]}" >&2
# Run fastq-dump and send it to background
for min_max in "${out[@]}"; do
local min=$(echo ${min_max} | cut -d ',' -f 1)
local max=$(echo ${min_max} | cut -d ',' -f 2)
local idx=$(echo $(($min / $avg + 1)))
local d="${td}/${idx}"
mkdir -p ${d}
fastq-dump -N ${min} -X ${max} -O ${d} ${OPTIONS} ${sra} 1>&2 &
done
# Wait subprocesses and collect exit status
local failure=0
for job in $(jobs -p); do
wait ${job} || let "failure+=1"
done
# Marge splitted fastq files
if [[ "${failure}" != "0" ]]; then
echo "ERROR during decompressing" >&2
exit 1
else
files=($(ls "${td}/1"))
# Create empty file to collect splitted fastq reads (if not --stdout)
if [[ "${STDOUT}" != "true" ]]; then
for fo in "${files[@]}"; do
touch "${OUTDIR}/${fo}"
done
fi
for min_max in "${out[@]}"; do
local min=$(echo ${min_max} | cut -d ',' -f 1)
local idx=$(echo $(($min / $avg + 1)))
for fo in "${files[@]}"; do
if [[ "${STDOUT}" == "true" ]]; then
cat "${td}/${idx}/${fo}"
else
cat "${td}/${idx}/${fo}" >> "${OUTDIR}/${fo}"
fi
done
done
fi
}
# function to calculate spot count using sra-stat
calc_spot_count(){
local sra="${1}"
local txt=$(sra-stat --meta --quick "${sra}")
local total=0
for line in ${txt}; do
local n=$(echo ${line} | cut -d '|' -f 3 | cut -d ':' -f 1)
local total=$(( ${total} + ${n} ))
done
echo ${total}
}
# function to check prerequisites
check_binary_location(){
local cmd="${1}"
local cmd_path=$(which ${cmd} 2>/dev/null ||:)
if [[ ! -e "${cmd_path}" ]]; then
echo "ERROR: ${cmd} not found." >&2
exit 1
else
echo "Using $(${cmd} --version | tr -d '\n')" >&2
fi
}
# main function
check_binary_location "sra-stat"
check_binary_location "fastq-dump"
echo "tmpdir: ${TMPDIR}" >&2
echo "outdir: ${OUTDIR}" >&2
if [[ "${SRAID}" ]]; then
spot_count=`calc_spot_count "${SRAID}"`
parallel_fastq_dump "${SRAID}" "${spot_count}"
elif [[ "${INPUT_FILES}" ]]; then
for srafile in ${INPUT_FILES}; do
spot_count=`calc_spot_count "${srafile}"`
parallel_fastq_dump "${srafile}" "${spot_count}"
done
else
print_help
exit 0
fi
# cleaning tmpfiles
rm -fr "${TMPDIR}/pfd.tmp"