#!/bin/bash -Ceuo pipefail
mkdir -p HCC1395_BL
# Determine whether to split based on params.split_fastq and file size
if [ "true" == "false" ]; then
split_args=""
else
file_size=$(stat -f%z HCC1395_BL_S2_L004_R1_001.fastq.gz 2>/dev/null || stat -c%s HCC1395_BL_S2_L004_R1_001.fastq.gz)
if [ 8880000000 -gt 0 ] && [ $file_size -lt 8880000000 ]; then
split_args=""
else
split_args="--split_by_lines 400000000"
fi
fi
fastp \
--in1 HCC1395_BL_S2_L004_R1_001.fastq.gz \
--in2 HCC1395_BL_S2_L004_R2_001.fastq.gz \
--out1 HCC1395_BL/HCC1395_BL_R1.fastq.gz \
--out2 HCC1395_BL/HCC1395_BL_R2.fastq.gz \
--detect_adapter_for_pe \
--length_required 15 \
--json HCC1395_BL.fastp.json \
--html HCC1395_BL.fastp.html \
--thread 4 \
$split_args \
2> >(tee HCC1395_BL.fastp.log >&2)
# Remove empty files created by fastp edge case (when reads/chunk_size == threads)
find HCC1395_BL -name "*.fastq.gz" -size 0 -delete
# Handle 0-read input: fastp exits 0 but creates no output files.
# Create empty gzip files so the output glob matches and downstream sees 0 reads.
if [ -z "$(ls -A HCC1395_BL/)" ]; then
echo "" | gzip > HCC1395_BL/HCC1395_BL_R1.fastq.gz
echo "" | gzip > HCC1395_BL/HCC1395_BL_R2.fastq.gz
fi
fastp_version=$(fastp --version 2>&1 | sed 's/fastp //')
cat <<-END_VERSIONS > versions.yml
"DAQ:FASTP":
fastp: $fastp_version
END_VERSIONS