-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrenameFQGZ.bash
executable file
·83 lines (71 loc) · 3.2 KB
/
renameFQGZ.bash
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
#!/bin/bash
# bash script to rename raw fq.gz files from NovoGene with TAMUCC sample name decode file
enable_lmod
module load parallel
if [[ -z "$1" ]]; then
echo "please specify the name of the decode file"
echo "bash renameFQGZ.bash NameOfDecodeFile.tsv"
exit 1
else
echo "decode file read into memory"
fi
if [[ -z "$2" ]]; then
echo "rename not specified, original and new file names will be printed to screen"
echo "bash renameFQGZ.bash $1"
echo; echo "if you want to rename then bash renameFQGZ.bash $1 rename"; echo
MODE="test"
elif [[ "$2" == rename ]]; then
echo "rename specified, files will be renamed"
read -p "Are you sure? " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo
echo "bash renameFQGZ.bash $1 rename"
MODE="rename"
else
echo
echo "rename aborted, original and new file names will be printed to screen"
echo "bash renameFQGZ.bash $1"
MODE="test"
fi
else
echo "rename not specified, original and new file names will be printed to screen"
echo "bash renameFQGZ.bash $1 $2"
echo; echo "if you want to rename then bash renameFQGZ.bash $1 rename"; echo
MODE="test"
fi
echo "writing original file names to file, origFileNames.txt..."
ls *_1.fq.gz | \
sed 's/1\.fq\.gz//' | \
sort > \
origFileNames.txt
echo "writing newFileNames.txt..."
sed 's/_.*_L[1-9]_/./1' origFileNames.txt > newFileNames.txt
#sed $'s/\t/\//' $1 > decode_sedlist.txt # Eric commented this line and added the one below to accomodate when original names have an extra "_[12]" which makes sed fail in the sed -i -f command below
paste <(cut -f1 $1 | sed 's/_.*//') <(cut -f2 $1) | sed 's/\t/\//' > decode_sedlist.txt
sed -i 's/^/s\//' decode_sedlist.txt
sed -i 's/$/\//' decode_sedlist.txt
echo "editing newFileNames.txt..."
sed -i -f decode_sedlist.txt newFileNames.txt
reportOldNewFILENAMES(){
echo "preview of orig and new R1 file names..."
parallel --no-notice -k --link "echo {1}1.fq.gz {2}1.fq.gz" :::: origFileNames.txt :::: newFileNames.txt
echo "preview of orig and new R2 file names..."
parallel --no-notice -k --link "echo {1}2.fq.gz {2}2.fq.gz" :::: origFileNames.txt :::: newFileNames.txt
}
reportOldNewFILENAMES
if [[ $MODE == rename ]]; then
echo; echo "Last chance to back out. If the original and new file names look ok, then proceed."
read -p "Are you sure you want to rename the files? " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo; echo "saving relationship between old and new names..."
reportOldNewFILENAMES > old_new_filenames.log
echo; echo "renaming R1 files..."
parallel --no-notice -k --link "mv {1}1.fq.gz {2}1.fq.gz" :::: origFileNames.txt :::: newFileNames.txt
echo "renaming R2 files..."
parallel --no-notice -k --link "mv {1}2.fq.gz {2}2.fq.gz" :::: origFileNames.txt :::: newFileNames.txt
else
echo
echo "rename aborted, exiting..."
exit 1
fi
fi