mapfile -t listarr<./list.txt
for line in ${listarr[@]}
do
  sarr=(${line//,/ })
  DIR=(${sarr[0]})
  SAMPLE=(${sarr[1]})
  SUFFIX=(${sarr[2]})
  REF=(${sarr[3]})
  WGS=(${sarr[4]})

  if [ $WGS == 'wgs' ]
  then
    BAIFILE=(${DIR}/${SAMPLE}${SUFFIX}.bai)
    if [ -e BAIFILE ]
    then
      echo "exists"
    else
      echo "index"
      samtools index ${DIR}/${SAMPLE}${SUFFIX}
    fi

    if [ $REF == 'hg19' ]
    then
      samtools view -bh ${DIR}/${SAMPLE}${SUFFIX} MT NC_012920.1 chrM> ${DIR}/${SAMPLE}.MT.bam
      samtools view -bh ${DIR}/${SAMPLE}${SUFFIX} Y chrY> ${DIR}/${SAMPLE}.Y.bam

      sh proc/auto${REF}.sh ${DIR} ${SAMPLE} ${SUFFIX}
    elif [ $REF == 'hg38' ]
    then
      samtools view -bh ${DIR}/${SAMPLE}${SUFFIX} chrM> ${DIR}/${SAMPLE}.chrM.bam
      samtools view -bh ${DIR}/${SAMPLE}${SUFFIX} chrY> ${DIR}/${SAMPLE}.chrY.bam

      sh proc/auto${REF}.sh ${DIR} ${SAMPLE} ${SUFFIX}
    fi
  fi
done

