-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert_bam_to_fastq.sh
38 lines (27 loc) · 1.07 KB
/
convert_bam_to_fastq.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
#!/bin/bash
# this script will convert bam files into fastq files
echo Program to convert bam files into fastq files
echo by Lucas K Bobadilla
# get variables
echo "Do you have pair or single read fastq files mapped to the bam? (answer pair or single)"
read read_type
bam_input_dir=$1
fastq_out=$2
# run program
cd $bam_input_dir
for bam_file in *.bam; do
if samtools view -H $bam_file | head -n 1 | grep -q "queryname"
then
echo "${bam_file} already sorted by name. Converting to fastq: "
if [ $read_type == "pair" ]
then
bedtools bamtofastq -i $bam_file -fq ${fastq_out}/${bam_file::-4}_R1.fastq -fq2 ${fastq_out}/${bam_file::-4}_R2.fastq
pigz -p 25 ${fastq_out}/${bam_file::-4}_R1.fastq
pigz -p 25 ${fastq_out}/${bam_file::-4}_R2.fastq
else
bedtools bamtofastq -i $bam_file -fq ${fastq_out}/${bam_file::-4}.fastq
pigz -p 25 ${fastq_out}/${bam_file::-4}.fastq
fi
echo "${bam_file::-4} converted"
else
echo "${bam_file} not sorted by name. Sorting:"