forked from vanrijlab/piRNAClusterAnnotation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtractpiRNAs.sh
166 lines (131 loc) · 3.23 KB
/
ExtractpiRNAs.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
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
#!/bin/bash
#$ -S /bin/bash
#$ -cwd
#$ -V
set -e
# Set working directory
date=$(date)
workdir=$(pwd)
# Function to display usage
usage()
{
cat << EOF
Summary: This script extracts the 5' ends of piRNA reads from a bam file.
Usage:
bash $(basename "$0") [options] -i bamfile
-i: bam file
Options:
-m: Minimal size of piRNA reads (default: 25)
-M: maximum size of piRNA reads (default: 30)
-b: store unfiltered bed file
-o: Folder in which file will be stored. If not specified, the current working directory will be used.
-x: Run script in debugging-mode. In that case the script will print the code that is executed.
Output:
bed file containing 5' positions of piRNA-sized reads
EOF
}
check_file()
{
local file="$1"
if [ ! -e "${file}" ]; then
printf "***Error: File ${file} does not exist.***\n\n"
usage
exit 1
fi
}
# Function to check if input is integer
check_input()
{
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
printf "\n***Error: Expected an integer as input.***\n"
usage
exit 1
fi
}
# Function to check if program is installed
check_program()
{
local program=$1
if ! command -v "${program}" &>/dev/null; then
printf "\n***Error: cannot find the program ${program}. Not installed or not found in PATH.**\n\n"
exit 1
fi
}
# Initialize required files:
input_bam=""
# Default variables (if not specified otherwise by user)
min_size=25
max_size=30
out_directory="${workdir}"
debug="no"
keep_bed="no"
# Parse options
while getopts ":i:m:M:o:bx" options; do
case $options in
i)
input_bam="$OPTARG"
;;
m)
min_size="$OPTARG"
check_input "$min_size"
;;
M)
max_size="$OPTARG"
check_input "$max_size"
;;
o)
out_directory="$OPTARG"
;;
b)
keep_bed="yes"
;;
x)
debug="yes"
printf "Running in debugging mode. \n"
;;
:)
printf "***Error: Option -${OPTARG} requires an argument.***\n\n"
usage
exit 1
;;
\?)
printf "Invalid option: -$OPTARG" >&2
usage
exit 1
;;
esac
done
# Check if the required bam file is provided
check_file "${input_bam}"
# Check: are required programs installed:
# List of required programs
required_programs=( "awk" "bedtools")
# Check each required program
for prog in "${required_programs[@]}"; do
check_program "${prog}"
done
# print executed code if script is run in debuagging mode
if [ "${debug}" = "yes" ]; then
set -x
fi
# Check if output directory exists, otherwise make
if [ ! -d "${out_directory}" ]; then
mkdir "${out_directory}"
fi
# Extract filename of input bam file
file="$(basename -- $input_bam)"
filename="$(echo ${file%.*})"
# Convert bam file to bed file
bedtools bamtobed -i "${input_bam}" > "${out_directory}"/"${filename}".bed
# Extract piRNA-sized reads and determine 5' ends
awk -v n="${min_size}" -v m="${max_size}" 'BEGIN { OFS = "\t" }
{ $7 = $3 - $2 }
{ if(( $7>=n ) && ( $7<=m )){ print }}' "${out_directory}"/"${filename}".bed |
awk 'BEGIN { OFS = "\t" }
{ if( $6=="+" ){ $3= $2 + 1 } else{ $2= $3 - 1 }} 1' |
sort -k1,1 -k2,2n |
awk 'NF > 0' > "${out_directory}"/"${filename}".piRNAs.bed
# delete bed file with all small RNA reads
if [ ! "${keep_bed}" = "yes" ]; then
rm "${out_directory}"/"${filename}".bed
fi