snp-calling pipeline

$cat sample.txt
/path/sample1_1.fq
/path/sample2_1.fq
#manul:./calling-snp-pipeline.sh -r /path/reference.fa -s sample.txt [-t threads] 

cat calling-snp-pipeline.sh
#!/bin/bash
set -e 
set -u
set -o pipefail

while getopts s:r:t:h opt
do
    case "$opt" in
    s) sample="$OPTARG";;
    r) reference="$OPTARG";;
    t) t="$OPTARG";;
    h) printf "Usage: %s: <-s sample_name.txt> <-r abspath/reference > <-t bwa_threads> \n" $0
       printf "\t -r: path/reference.fasta,\n"
       printf "\t -s: format of sample.txt: it contains the reads name and relative path. for exmaple: \n"
       printf "\t path/reads_1.fq.gz \n"
       printf "\t path/reads_2.fq.gz \n"
       exit 0;;
    *) printf "Usage: %s: [-a value] [-h] args \n" $0
        exit 2;;
    esac
done

shift $(($OPTIND - 1))
#########################################################################

sample_names=($(cut -f 1  "$sample" |  cut -d '_' -f 1 | uniq))

PICARD=/home/wangshaobo/biosoft/picard/picard.jar
GATK=~/biosoft/GATK/GenomeAnalysisTK.jar
dict=${reference%.*}.dict
#dict=$(echo $reference | sed s/fa/dict/)


###########
###index###
###########
if test -e ${reference}.sa
then
        echo bwa index already done.
else
        echo [bwa index start`date`]
        bwa index -a bwtsw $reference
        echo [bwa index end `date`]
fi


test -f $dict ||  java -jar $PICARD CreateSequenceDictionary  R=$reference O=$dict
test -f $reference.fai || samtools faidx $reference

##########################################################################################
####align####
#############
#sample_info=sample.txt
sample_names=($(cat 1  "$sample" |  cut -d '_' -f 1 | uniq))
tmp=($(cat "$sample" |  cut -d '_' -f 2 | uniq))
fq_suffix=${tmp#*.}


for samples in ${sample_names[@]}
do
{
 sample=$(basename $samples)
 work_dir=$(dirname $samples) 
 
 mkdir $sample && cd $sample

echo [${sample} bwa mem start-time     `date`]
bwa mem -t ${t:-8} -M -R "@RG\tID:<${sample}>\tLB:\tSM:<${sample}>\tPL:ILLUMINA" \
${reference} ${work_dir}/${sample}_1.${fq_suffix} ${work_dir}/${sample}_2.${fq_suffix} > ${sample}.sam 

perl -alne 'print if ($F[0]=~/^@/ || $F[1]==99 ||$F[1]==147 ||$F[1]==83 ||$F[1]==163)' ${sample}.sam >${sample}_filter.sam 
samtools view -bS ${sample}_filter.sam >${sample}.bam
echo [$sample bwa mem end-time     `date`]

####sortsam####
echo [${sample} sortsam start-time     `date`]
java -Xmx20g -jar $PICARD SortSam I=${sample}.bam O=${sample}.sorted.bam SORT_ORDER=coordinate
echo [${sample} sortsam end-time     `date`]

####Markduplicate####
echo [${sample} markdup start-time     `date`]
java -Xmx20g -jar ${PICARD} MarkDuplicates I=${sample}.sorted.bam O=${sample}.markdup.bam REMOVE_DUPLICATES=false  MAX_FILE_HANDLES_FOR_READ_ENDS_MAP=1000 METRICS_FILE=${sample}.sort.bam.metrics
echo [${sample} markdup end-time     `date`]

samtools index ${sample}.markdup.bam

##########realign#############

echo [${sample} realign start-time     `date`]
java -Xmx20g -jar $GATK -R $reference -T RealignerTargetCreator -I ${sample}.markdup.bam -o ${sample}.realign.intervals

java -Xmx20g -jar $GATK -R $reference -T IndelRealigner -targetIntervals ${sample}.realign.intervals -I ${sample}.markdup.bam -o ${sample}.realign.bam
echo [${sample} realign end-time     `date`]

############GATK first generate vcf file############
echo [${sample} HaplotypeCaller start-time     `date`]
java -Xmx20g -jar $GATK -R $reference -T HaplotypeCaller -I ${sample}.realign.bam -o ${sample}.gatk.raw.vcf -stand_call_conf 30 -nct 60
echo [${sample} HaplotypeCaller end-time     `date`]

samtools index ${sample}.realign.bam

#############samtools generate vcf file###################

echo [${sample} samtools-vcf start-time     `date`]
samtools mpileup  -t DP -t SP -uf $reference ${sample}.realign.bam |bcftools call -vm -Ov > ${sample}.samtools.raw.vcf

echo [${sample} samtools-vcf end-time     `date`]

#####################samtools and gatk concordance###########################

java -Xmx20g -jar $GATK -R $reference -T SelectVariants --variant ${sample}.gatk.raw.vcf --concordance ${sample}.samtools.raw.vcf -o ${sample}.concordance.raw.vcf

MEANQUAL=$(awk '{ if ($1 !~ /#/) { total += $6; count++  }  } END { print total/count  }' ${sample}.concordance.raw.vcf)

###############################VariantFiltration####################################

####=====set by user=======
java -Xmx20g -jar $GATK -R $reference -T VariantFiltration --filterExpression "QD < 20.0 || ReadPosRankSum < -8.0 || FS > 10.0 || QUAL < MEANQUAL" --filterName LowQualFilter  --missingValuesInExpressionsShouldEvaluateAsFailing  --variant ${sample}.concordance.raw.vcf --logging_level ERROR -o ${sample}.concordance.flt.vcf

grep -v "Filter" ${sample}.concordance.flt.vcf > ${sample}.concordance.flt.knowsites.vcf

##################################### BaseRecalibrator####################################

echo [${sample} BaseRecalibrator start-time     `date`]
java -Xmx20g -jar $GATK -R $reference -T  BaseRecalibrator -I ${sample}.realign.bam -knownSites ${sample}.concordance.flt.knowsites.vcf  -o ${sample}.recal_data.grp
echo [${sample} BaseRecalibrator end-time     `date`]


#########################################PrintReads############################################

echo [${sample} printReads start-time     `date`]

java -Xmx20g -jar $GATK -R $reference -T PrintReads -I ${sample}.realign.bam -o ${sample}.recal.bam -BQSR ${sample}.recal_data.grp


samtools index ${sample}.recal.bam

java -Xmx20g -jar $GATK -R $reference -T HaplotypeCaller -I ${sample}.recal.bam -o ${sample}.vcf --genotyping_mode DISCOVERY -stand_call_conf 30 -nct 66

echo [${sample} printReads-HapCall end-time     `date`]

java -Xmx24g -jar $GATK -R $reference -T SelectVariants -R /disk3/wangshaobo/MSU/ref/rice-MSU.fa -selectType SNP -V ${sample}.vcf -o ${id}.snp.vcf
java -Xmx24g -jar $GATK -R $reference -T SelectVariants -R /disk3/wangshaobo/MSU/ref/rice-MSU.fa -selectType INDEL -V ${sample}.vcf -o ${id}.indel.vcf


} &
done
wait 

你可能感兴趣的:(snp-calling pipeline)