pipeline_1.sh 4.88 KB
Newer Older
Gonzalo S Nido's avatar
Gonzalo S Nido committed
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

# exit when any command fails
set -e
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
trap 'echo "\"${last_command}\" command filed with exit code $?"' EXIT

if [ "$1" == "" ]
then
    echo "Provide ID!!"
    exit 1
fi

wd="/data/bs"
id=$1
ref_dir="/data/ref"
fastq_dir="/data/bs/trimmed_fq"

# Programs
bismark="/usr/local/bin/bismark-0.22.3"
deduplicate_bismark="/usr/local/bin/deduplicate_bismark-0.22.3"
bismark_meth_extract="/usr/local/bin/bismark_methylation_extractor-0.22.3"
bowtie2="/usr/local/bin/bowtie2-2.4.41"
bowtie_path="/opt/bowtie2-2.4.41/"
samtools="/usr/local/bin/samtools-1.10"
samtools_path="/opt/samtools-1.10"
fetch_mt_reads="/data/bin/fetch_mt_and_mate/fetch_mt_and_mate.py"
#samviewwithmate="/data/bin/jvarkit/dist/samviewwithmate.jar"
#methylKit_extract="~/scripts/extractMethylation.R"

# References
b37_ref=${ref_dir}/b37_decoy/
#nuc_ref=${ref_dir}/b37_decoy_no_mt/
mt_ref=${ref_dir}/b37_decoy_only_mt/

# Output folders
bamfiles_1=${wd}/bamfiles_1
mkdir -p ${bamfiles_1}
meth_1=${wd}/meth_1
mkdir -p ${meth_1}
tmp_dir=${wd}/tmp_1
mkdir -p ${tmp_dir}
log_dir=${wd}/logs_1
mkdir -p ${log_dir}


# Get fastq files
r1_files=$( find ${fastq_dir} -name *${id}*R1.fastq.gz | paste -sd "," - )
r2_files=$( find ${fastq_dir} -name *${id}*R2*fastq.gz | paste -sd "," - )

echo "R1 FILES: ${r1_files}"
echo "R2 FILES: ${r2_files}"


echo "#### STRATEGY 1: Align vs the whole genome (nc + mt), dedup, and extract \
methylation of MT"
 
echo "     1.1 Aligning..."
${bismark} ${b37_ref} -1 ${r1_files} -2 ${r2_files} --un --ambiguous --multicore 12 \
    -o ${bamfiles_1} --temp_dir ${tmp_dir} --path_to_bowtie ${bowtie_path} \
    --samtools_path ${samtools_path} > ${log_dir}/1_1_aln.stdout 2> ${log_dir}/1_1_aln.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 1.1"
    exit 1
fi

echo "     1.2 Merging unaligned and ambiguos reads..."
unaligned_list_r1=""
ambig_list_r1=""
unaligned_list_r2=""
ambig_list_r2=""
IFS=',' read -r -a array <<< "${r1_files}"
for element in "${array[@]}"; do
    uns=$( basename ${element} )
    uns="${bamfiles_1}/${uns}_unmapped_reads_1.fq.gz"
    unaligned_list_r1="${unaligned_list_r1} ${uns}"
    amb=$( basename ${element} )
    amb="${bamfiles_1}/${amb}_ambiguous_reads_1.fq.gz"
    ambig_list_r1="${ambig_list_r1} ${amb}"
done
IFS=',' read -r -a array <<< "${r2_files}"
for element in "${array[@]}"; do
    uns=$( basename ${element} )
    uns="${bamfiles_1}/${uns}_unmapped_reads_2.fq.gz"
    unaligned_list_r2="${unaligned_list_r2} ${uns}"
    amb=$( basename ${element} )
    amb="${bamfiles_1}/${amb}_ambiguous_reads_2.fq.gz"
    ambig_list_r2="${ambig_list_r2} ${amb}"
done

cat ${unaligned_list_r1} > ${bamfiles_1}/${id}_unmapped.R1.fastq.gz
cat ${ambig_list_r1} > ${bamfiles_1}/${id}_ambiguous.R1.fastq.gz
cat ${unaligned_list_r2} > ${bamfiles_1}/${id}_unmapped.R2.fastq.gz
cat ${ambig_list_r2} > ${bamfiles_1}/${id}_ambiguous.R2.fastq.gz

if [ -s "${bamfiles_1}/${id}_unmapped.R1.fastq.gz" ]; then
    rm ${unaligned_list_r1}
fi
if [ -s "${bamfiles_1}/${id}_unmapped.R2.fastq.gz" ]; then
    rm ${unaligned_list_r2}
fi
if [ -s "${bamfiles_1}/${id}_ambiguous.R1.fastq.gz" ]; then
    rm ${ambig_list_r1}
fi
if [ -s "${bamfiles_1}/${id}_ambiguous.R2.fastq.gz" ]; then
    rm ${ambig_list_r2}
fi


echo "     1.3 Merging bamfiles..."
bamfile_list=""
IFS=',' read -r -a array <<< "${r1_files}"
for element in "${array[@]}"; do
    bamfile=$( basename ${element} )
    bamfile="${bamfiles_1}/${bamfile%.fastq.gz}_bismark_bt2_pe.bam"
    bamfile_list="${bamfile_list} ${bamfile}"
done

${samtools} merge -n -r -@ 64 ${bamfiles_1}/${id}_b37.bam ${bamfile_list} > \
    ${log_dir}/1_3_merge.stdout 2> \
    ${log_dir}/1_3_merge.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 1.3"
    exit 1
fi

if [ -s "${bamfiles_1}/${id}_b37.bam" ]; then
    echo "         Removing lane bams..."
    rm ${bamfile_list}
fi


echo "     1.4 Dedupping alignment..."
${deduplicate_bismark} --paired --samtools_path ${samtools_path} \
    --output_dir ${bamfiles_1} ${bamfiles_1}/${id}_b37.bam > \
    ${log_dir}/1_4_dedup.stdout 2> \
    ${log_dir}/1_4_dedup.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 1.4"
    exit 1
fi

if [ -s "${bamfiles_1}/${id}_b37.deduplicated.bam" ]; then
    mv ${bamfiles_1}/${id}_b37.deduplicated.bam ${bamfiles_1}/${id}_b37.dedup.bam
fi


echo "     1.5 Extracting methylation in MT..."
${bismark_meth_extract} -p --comprehensive --output ${meth_1} \
    --gzip --multicore 16 --bedGraph --genome_folder ${b37_ref} \
    --CX --samtools_path ${samtools_path} --ample_memory \
    ${bamfiles_1}/${id}_b37.dedup.bam > \
    ${log_dir}/1_5_meth.stdout 2> \
    ${log_dir}/1_5_meth.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 1.5"
    exit 1
fi


# echo "     1.6 Fetching MT reads (and their pairs)..."
# python3 ${fetch_mt_reads} ${bamfiles_1}/${id}_b37.dedup.bam | \
#     ${samtools} view -@ 16 -b -o ${bamfiles_1}/${id}_mt_reads_and_mates.bam -