pipeline_2.sh 4.85 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
167
168
169
170
#!/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
bam_dir=${wd}/bamfiles_2
mkdir -p ${bam_dir}
meth_dir=${wd}/meth_2
mkdir -p ${meth_dir}
tmp_dir=${wd}/tmp_2
mkdir -p ${tmp_dir}
log_dir=${wd}/logs_2
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 2: Align vs mitochondrial genome only (mt), dedup, and extract \
methylation"
 
echo "     2.1 Aligning..."
${bismark} ${mt_ref} -1 ${r1_files} -2 ${r2_files} --un --ambiguous --multicore 12 \
    -o ${bam_dir} --temp_dir ${tmp_dir} --path_to_bowtie ${bowtie_path} \
    --samtools_path ${samtools_path} > ${log_dir}/2_1_aln.stdout 2> ${log_dir}/2_1_aln.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 2.1"
    exit 1
fi

echo "     2.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="${bam_dir}/${uns}_unmapped_reads_1.fq.gz"
    unaligned_list_r1="${unaligned_list_r1} ${uns}"
    amb=$( basename ${element} )
    amb="${bam_dir}/${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="${bam_dir}/${uns}_unmapped_reads_2.fq.gz"
    unaligned_list_r2="${unaligned_list_r2} ${uns}"
    amb=$( basename ${element} )
    amb="${bam_dir}/${amb}_ambiguous_reads_2.fq.gz"
    ambig_list_r2="${ambig_list_r2} ${amb}"
done

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

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


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

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

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


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

if [ -s "${bam_dir}/${id}_mt.deduplicated.bam" ]; then
    mv ${bam_dir}/${id}_mt.deduplicated.bam ${bam_dir}/${id}_mt.dedup.bam
fi
 
 
echo "     2.5 Extracting methylation in MT..."
${bismark_meth_extract} -p --comprehensive --output ${meth_dir} \
    --gzip --multicore 16 --cytosine_report --genome_folder ${mt_ref} \
    --CX --samtools_path ${samtools_path} \
    ${bam_dir}/${id}_mt.dedup.bam > \
    ${log_dir}/2_5_meth.stdout 2> \
    ${log_dir}/2_5_meth.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 2.5"
    exit 1
fi


echo "     2.6 Fetching MT reads (and their pairs)..."
python3 ${fetch_mt_reads} ${bam_dir}/${id}_mt.dedup.bam | \
    ${samtools} view -@ 16 -b -o ${bam_dir}/${id}_mt_reads_and_mates.bam -
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 2.6"
    exit 1
fi