-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmv_fixed_bams_to_soft_link.sh
97 lines (79 loc) · 3.89 KB
/
mv_fixed_bams_to_soft_link.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
#!/usr/bin/env bash
##Bash script to move BAM to their soft linked location
#should be ls <command> where command would select the files of choice
#e.g. searching for all files in current directory, would pass *rg_fixed* IN QUOTES
#https://linuxconfig.org/bash-script-display-usage-and-check-user - nice guide
display_help() {
#why does BASH make me do this...
dqt='"'
echo
echo "Usage: ./mv_fixed_bams_to_soft_link.sh ${dqt}<dir/>*rg_fixed*${dqt}"
echo "Author: Sam Bryce-Smith"
echo
echo "Intended to follow on from my hacky rg_fixing script."
echo "If you ran fixing script on soft links to broken BAMs, use this script to mv the fixed files to the original location of corresponding broken BAM"
echo "Requires 1 positional argument that tells ls how to find the find the fixed BAMs, enclosed in DOUBLE QUOTES."
echo
echo "Example call: bash mv_fixed_bams_to_soft_link.sh ${dqt}*rg_fixed*${dqt}"
echo "Defines fixed files in current directory as files containing rg_fixed string"
echo
echo "Note: This script will prompt you to confirm the moves before executing"
echo
echo "-h/--help Print this help message and exit"
echo
}
if [[ ( $@ == "--help") || $@ == "-h" ]]
then
display_help
exit 0
fi
rg_ls_command=$1
#echo $rg_ls_command
#if file has content it empties it - if not creates empty file
#don't want to add commands over and over
truncate -s 0 fixed_bams_to_move_list.txt
ls ${rg_ls_command} | for i in $(cat -); do
fixed_file_name=$(echo $i | cut -d. -f1) #get rid of exrention
original_name=${fixed_file_name%"_rg_fixed"} # strip _rg_fixed suffix from bam name - gets name of original file
ext=$(echo $i | awk -F "." '{for (i=2; i<=NF; i++) {if ($i !="") {printf "."$i};}}') #gets extension of file in question (up to two e.g. .bam.bai)
#https://stackoverflow.com/questions/8984720/awk-joining-n-fields-with-delimiter
#echo 'fixed file name is' ${fixed_file_name}${ext}
#original name should be a soft link to the original location - want to move fixed file to this location
#if ll <original_name> , the last field returned provides path to which soft link points
#.bam.bai for original files are not linked - need to add .bai to path to corresponding .bam file
if [[ "$ext" == ".bam" ]]
then
original_location=$(ls -l ${original_name}${ext} | awk '{print $NF}')
#echo 'original location is' ${original_location}
echo ${i} ${original_location} >> fixed_bams_to_move_list.txt
elif [[ $ext == ".bam.bai" ]]
then
original_bam_location=$(ls -l ${original_name}.bam | awk '{print $NF}')
original_index_location="${original_bam_location}.bai"
#echo 'original index location is' $original_index_location
echo ${i} ${original_index_location} >> fixed_bams_to_move_list.txt
fi
done
echo 'the following commands will be performed'
echo
#https://stackoverflow.com/questions/7551991/add-a-new-column-to-the-file
awk 'BEGIN{FS=OFS=" "}{print "mv" OFS $0}' fixed_bams_to_move_list.txt
#function to loop through fixed_bams_to_move_list - mv element in first column to element in second
function mv_files {
echo 'file containing files to move is' $1
while IFS=" " read -r line; do
#printf "line in text from file is:\n${line}\n"
mv ${line}
printf "\nFollowing command has been completed:\nmv ${line}\n"
done < $1
}
#user prompt to confirm whether to perform all moves
#https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script/27875395#27875395
echo 'Do you wish to proceed with all the above commands (put 1 or 2 to select yes or no)?'
select yn in "yes" "no"; do
case $yn in
yes ) echo 'you selected to perform these commands'; mv_files fixed_bams_to_move_list.txt;
break;;
no ) echo 'You have opted to not perform the above commands. Aborting...'; exit;;
esac
done