sort_sam_to_bam.sh 2.03 KB
Newer Older
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
#!/bin/ksh

### GAD PIPELINE ###
## clean_bam.sh
## Version : 2.4.0
## Description : This script allows user to launch convert a sam file to a bam file while sorting it. 
## Usage : qsub -pe smp 1 -v INPUTFILE=</path/to/the/input/file>,OUTPUTFILE=</path/to/the/output/file>,[LOGFILE=/path/to/the/log/file],[CONFIGFILE=/path/to/the/config/file] sort_sam_to_bam.sh
## Output : Sorted BAM file
## Requirements : Samtools 1.2

## Author : yannis.duffourd@u-bourgogne.fr
## Creation Date : 20170323
## last revision date : 20170323
## Known bugs : None

#$ -q batch
#$ -V

# Log file path option
if [ -z ${LOGFILE} ]
then
    LOGFILE=sort_sam_to_bam.$(date +"%F_%H-%M-%S").log
fi

# Config file path option
if [ -z ${CONFIGFILE} ]
then
    CONFIGFILE=analysis_config.tsv
fi

TEMPORARY_DIR=$(grep "temporary_dir" $CONFIGFILE | awk '{print $2}')
SAMTOOLSDIR=$(grep "samtools" $CONFIGFILE | awk '{print $2}')

# Logging
exec 1>> $LOGFILE 2>&1
echo "$(date +"%F_%H-%M-%S"): START"

# Check if config file exist
if [ ! -f $CONFIGFILE ]
then
    echo "Config file does not exist"
    echo "$(date +"%F_%H-%M-%S"): END"
    touch sort_sam_to_bam.failed
    exit 1
fi

# Check if input file exist
if [ ! -f $INPUTFILE ]
then
    echo "Input file does not exist"
    echo "$(date +"%F_%H-%M-%S"): END"
    touch sort_sam_to_bam.failed
    exit 1
fi

# Check if output file is specified
if [ -z $OUTPUTFILE ]
then
    echo "Output file is not specified"
    echo "$(date +"%F_%H-%M-%S"): END"
    touch sort_sam_to_bam.failed
    exit 1
fi

sample=`basename ${INPUTFILE%%.*}`

# Launch samtools sort in a multithreaded way
echo "Samtools command: $SAMTOOLSDIR/samtools sort -@ $NSLOTS -o $OUTPUTFILE -T $TEMPORARY_DIR/$sample.tmp.sort -O bam $INPUTFILE"
$SAMTOOLSDIR/samtools sort -@ $NSLOTS -o $OUTPUTFILE -T $TEMPORARY_DIR/$sample.tmp.sort -O bam $INPUTFILE


sortsamtobam_exitcode=$?
echo "CleanSam exit code : $cleansam_exitcode"
if [ $sortsamtobam_exitcode != 0 ]
then
    echo "$(date +"%F_%H-%M-%S"): END"
    touch sort_sam_to_bam.failed
    exit 1
fi

echo "$(date +"%F_%H-%M-%S"): END"