-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsh_mergeFastQ.sh
executable file
·96 lines (89 loc) · 2.3 KB
/
sh_mergeFastQ.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
#!/bin/bash
#
# Usage: sh_mergeFastQ.sh <.fastq(.gz) folder>
#
##############################################################
## Description ##
##############################################################
#
# Simple script to consolidate fragmented .fastq files from different sequencing lanes.
# Original files will be backed up in a FastQbackup folder.
#
##
# Help!
if [ "${1}" == "--help" ] || [ "${2}" == "--help" ] || [ "${3}" == "--help" ]
then
echo "Usage: $(basename "$0") <.fastq(.gz) folder>"
echo ""
echo "Description"
echo ""
echo "Simple script to consolidate fragmented .fastq files from different sequencing lanes."
echo "Original files will be backed up in a FastQbackup folder."
echo ""
echo "Options:"
echo "$(basename "$0") --help : Display this help message."
echo ""
exit
fi
# Version
if [ "${1}" == "--version" ] || [ "${2}" == "--version" ] || [ "${3}" == "--version" ]
then
echo "$(basename "$0") version 1.0.1"
exit
fi
# Get fastq directory
dir="${1}"
# Check paths and trailing / in directories
if [ -z "${dir}" ]
then
${0} --help
exit
fi
if [ "${dir: -1}" = "/" ]
then
dir=${dir%?}
fi
# Test if sequence files are .fastq or .fastq.gz
fastqgz=$(find -L "${dir}" -maxdepth 1 -name '*.fastq.gz')
fastq=$(find -L "${dir}" -maxdepth 1 -name '*.fastq')
if [ -z "${fastqgz}" ] && [ -z "${fastq}" ]
then
echo ""
echo "No .fastq or .fastq.gz files are present in ${dir}/"
exit
fi
if [ -n "${fastqgz}" ] && [ -n "${fastq}" ]
then
echo ""
echo "Both .fastq and .fastq.gz files are present in ${dir}/"
echo "Existing .fastq.gz files will now be converted to .fastq files"
gunzip "${dir}/*.fastq.gz"
fi
# Concatenate files if split
echo ""
echo "-- Merging files --"
echo ""
mkdir -p "${dir}/FastQbackup/"
filestomergeR1=$(ls "${dir}"/*_L[0-9][0-9][0-9]_R1)
filestomergeR2=$(ls "${dir}"/*_L[0-9][0-9][0-9]_R2)
echo "Processing R1 files"
echo ""
for i in ${filestomergeR1}
do
sampleoutput="${i//_L[0-9][0-9][0-9]_R1/_R1}"
echo "Processing ${i}"
cat "${i}" >> "${sampleoutput}"
mv "${i}" "${dir}/FastQbackup/"
done
echo ""
echo "Processing R2 files"
echo ""
for i in ${filestomergeR2}
do
sampleoutput="${i//_L[0-9][0-9][0-9]_R2/_R2}"
echo "Processing ${i}"
cat "${i}" >> "${sampleoutput}"
mv "${i}" "${dir}/FastQbackup/"
done
echo ""
echo "-- Files merged --"