#!/bin/bash
# by YH
# Linux system, based on Cutadapt version 2.10
# 2022-07-08
#version 1, N4 is for the samll sets of barcoding cells. Index1/2/3 will be used when Hundreds or more cells barcoding.
usage() {
    NAME=$(basename $0)
    cat <<EOF
Usage:
  ${NAME}  <input.fastq.gz> 
  record all the fastq.gz file location in a absolute formate 
EOF
}

OUTDIR=$(pwd -P)
if [[ "$1" ]]; then
 if [[ "$1" == "-h" || "$1" == "--help" ]]; then
  usage
  exit 1
else
OUTDIR=$1
fi
fi

WRKDIR=$(pwd -P)
errprog=""

SCRIPTARGS="$@"
# main script block
pipeline() {
while read line;
do
  echo "sample $line is being treated"
  if [ ! -f $line ];then
  continue
  fi
  dir=${line%/*}
  filename1=${line%%.*}
  filename=${filename1##*/}
  filePair=${filename/_R1/_R2};
  cd ${dir}
 	cutadapt -g TCCGATCT -O8 -n5 -j0  -e 0 --action trim  -o ${dir}/${filename}_qua.fastq.gz  -p  ${dir}/${filePair}_qua.fastq.gz ${dir}/${filename}.fastq.gz ${dir}/${filePair}.fastq.gz
    #remove the muti-TSO sequence
      	cutadapt -g CACGTCTC -O8 -n5 -j0 -e 0 --action trim  -o ${dir}/${filePair}_good.fastq.gz  -p  ${dir}/${filename}_good.fastq.gz  ${dir}/${filePair}_qua.fastq.gz ${dir}/${filename}_qua.fastq.gz
        #remove the muti-adaptor sequence
	cutadapt -a NNNNNNNAGATCGGA -a TCGGAAGAGCACAC  -A NNNNNNNAGATCGGA -A  TCGGAAGAGCACAC  -e0 -m 28 -O8 -o ${dir}/${filename}_trim.fastq -p ${dir}/${filePair}_trim.fastq ${dir}/${filename}_good.fastq.gz $dir/${filePair}_good.fastq.gz

  #gunzip ${dir}/${filename}.fastq.gz
  #gunzip ${dir}/${filePair}.fastq.gz
awk '{getline a; getline b; getline c;if(a ~ /^[ATGCN]{4,4}[GN]{3,3}/){print NR-3}}' ${dir}/${filename}_trim.fastq >${dir}/${filename}.record_FR_N4.txt
awk '{getline a; getline b; getline c;if(a ~ /^ATCACG[ATGCN]{4,4}[GN]{3,3}/){print NR-3}}' ${dir}/${filename}_trim.fastq >${dir}/${filename}.record_FR_index1.txt
awk '{getline a; getline b; getline c;if(a ~ /^CGATGT[ATGCN]{4,4}[GN]{3,3}/){print NR-3}}' ${dir}/${filename}_trim.fastq >${dir}/${filename}.record_FR_index2.txt
awk '{getline a; getline b; getline c;if(a ~ /^[ATGCN]{4,4}TTAGGC[ATGCN]{1,1}[GN]{3,3}/){print NR-3}}' ${dir}/${filename}_trim.fastq >${dir}/${filename}.record_FR_index3.txt
#get the righ reads according to the library structure
awk 'NR==FNR{x[$1]=1} NR>FNR && x[FNR]==1{getline a; getline b; getline c;print $0;print a;print b;print c}'  ${dir}/${filename}.record_FR_N4.txt ${dir}/${filename}_trim.fastq >${dir}/${filename}_strand_N4.fastq
awk 'NR==FNR{x[$1]=1} NR>FNR && x[FNR]==1{getline a; getline b; getline c;print $0;print substr(a,7,length(a));print b;print substr(c,7,length(c))}'  ${dir}/${filename}.record_FR_index1.txt ${dir}/${filename}_trim.fastq >${dir}/${filename}_strand_index1.fastq
awk 'NR==FNR{x[$1]=1} NR>FNR && x[FNR]==1{getline a; getline b; getline c;print $0;print substr(a,7,length(a));print b;print substr(c,7,length(c))}'  ${dir}/${filename}.record_FR_index2.txt ${dir}/${filename}_trim.fastq >${dir}/${filename}_strand_index2.fastq
awk 'NR==FNR{x[$1]=1} NR>FNR && x[FNR]==1{getline a; getline b; getline c;print $0;print substr(a,1,4)substr(a,12,length(a));print b;print  substr(c,1,4)substr(c,12,length(a))}'  ${dir}/${filename}.record_FR_index3.txt ${dir}/${filename}_trim.fastq >${dir}/${filename}_strand_index3.fastq
#awk 'NR==FNR{x[$1]=1} NR>FNR {if (x[FNR]==1){getline a;getline b;getline c;print $0;print a;print b;print c}}'  ${dir}/${filename}.record_FR.txt  ${dir}/${filePair}_trim.fastq > ${dir}/${filePair}_strand.fastq 
awk 'NR==FNR{x[$1]=1} NR>FNR && x[FNR]==1{getline a; getline b; getline c;print $0;print a;print b;print c}'  ${dir}/${filename}.record_FR_N4.txt ${dir}/${filePair}_trim.fastq >${dir}/${filePair}_strand_N4.fastq
awk 'NR==FNR{x[$1]=1} NR>FNR && x[FNR]==1{getline a; getline b; getline c;print $0;print a;print b;print c}'  ${dir}/${filename}.record_FR_index1.txt ${dir}/${filePair}_trim.fastq >${dir}/${filePair}_strand_index1.fastq
awk 'NR==FNR{x[$1]=1} NR>FNR && x[FNR]==1{getline a; getline b; getline c;print $0;print a;print b;print c}'  ${dir}/${filename}.record_FR_index2.txt ${dir}/${filePair}_trim.fastq >${dir}/${filePair}_strand_index2.fastq
awk 'NR==FNR{x[$1]=1} NR>FNR && x[FNR]==1{getline a; getline b; getline c;print $0;print a;print b;print c}'  ${dir}/${filename}.record_FR_index3.txt ${dir}/${filePair}_trim.fastq >${dir}/${filePair}_strand_index3.fastq
#remove the anchor sequence before the tso sequence
awk '{getline a;getline b;getline c;print $0,substr(a,1,7),substr(a,8,14);print a;print b;print c}'  ${dir}/${filename}_strand_N4.fastq >${dir}/${filename}_N4PreV2.fastq
awk '{getline a;getline b;getline c;print $0,substr(a,1,7),substr(a,8,14);print a;print b;print c}'  ${dir}/${filePair}_strand_N4.fastq >${dir}/${filePair}_N4PreV2.fastq

awk '{getline a;getline b;getline c;print $0,substr(a,1,7),substr(a,8,14);print a;print b;print c}'  ${dir}/${filename}_strand_index1.fastq >${dir}/${filename}_index1PreV2.fastq
awk '{getline a;getline b;getline c;print $0,substr(a,1,7),substr(a,8,14);print a;print b;print c}'  ${dir}/${filePair}_strand_index1.fastq >${dir}/${filePair}_index1PreV2.fastq
awk '{getline a;getline b;getline c;print $0,substr(a,1,7),substr(a,8,14);print a;print b;print c}'  ${dir}/${filename}_strand_index2.fastq >${dir}/${filename}_index2PreV2.fastq
awk '{getline a;getline b;getline c;print $0,substr(a,1,7),substr(a,8,14);print a;print b;print c}'  ${dir}/${filePair}_strand_index2.fastq >${dir}/${filePair}_index2PreV2.fastq
awk '{getline a;getline b;getline c;print $0,substr(a,1,7),substr(a,8,14);print a;print b;print c}'  ${dir}/${filename}_strand_index3.fastq >${dir}/${filename}_index3PreV2.fastq
awk '{getline a;getline b;getline c;print $0,substr(a,1,7),substr(a,8,14);print a;print b;print c}'  ${dir}/${filePair}_strand_index3.fastq >${dir}/${filePair}_index3PreV2.fastq
#wrire the tso-N4 and adaptor sequence(if N7 adaptor is used) sequences on the first line of a fastq record
echo "awk '{nowLine=\$1\"-\"\$3\"-\"\$4;getline a;getline b;getline c;getline <\"${filePair}_N4PreV2.fastq\";print nowLine\"-\"\$3\"-\"\$4;print nowLine\"-\"\$3\"-\"\$4 > \"${filePair}_N4V2.fastq\";print a;print b;print c;getline d <\"${filePair}_N4PreV2.fastq\";getline e <\"${filePair}_N4PreV2.fastq\";getline f <\"${filePair}_N4PreV2.fastq\";print d > \"${filePair}_N4V2.fastq\";print e > \"${filePair}_N4V2.fastq\";print f >\"${filePair}_N4V2.fastq\"}' ${dir}/${filename}_N4PreV2.fastq > ${filename}_N4V2.fastq " > pre-temp.sh
sh pre-temp.sh

echo "awk '{nowLine=\$1\"-\"\$3\"-\"\$4;getline a;getline b;getline c;getline <\"${filePair}_index1PreV2.fastq\";print nowLine\"-\"\$3\"-\"\$4;print nowLine\"-\"\$3\"-\"\$4 > \"${filePair}_index1V2.fastq\";print a;print b;print c;getline d <\"${filePair}_index1PreV2.fastq\";getline e <\"${filePair}_index1PreV2.fastq\";getline f <\"${filePair}_index1PreV2.fastq\";print d > \"${filePair}_index1V2.fastq\";print e > \"${filePair}_index1V2.fastq\";print f >\"${filePair}_index1V2.fastq\"}' ${dir}/${filename}_index1PreV2.fastq > ${filename}_index1V2.fastq " > pre-temp.sh
sh pre-temp.sh

echo "awk '{nowLine=\$1\"-\"\$3\"-\"\$4;getline a;getline b;getline c;getline <\"${filePair}_index2PreV2.fastq\";print nowLine\"-\"\$3\"-\"\$4;print nowLine\"-\"\$3\"-\"\$4 > \"${filePair}_index2V2.fastq\";print a;print b;print c;getline d <\"${filePair}_index2PreV2.fastq\";getline e <\"${filePair}_index2PreV2.fastq\";getline f <\"${filePair}_index2PreV2.fastq\";print d > \"${filePair}_index2V2.fastq\";print e > \"${filePair}_index2V2.fastq\";print f >\"${filePair}_index2V2.fastq\"}' ${dir}/${filename}_index2PreV2.fastq > ${filename}_index2V2.fastq " > pre-temp.sh
sh pre-temp.sh

echo "awk '{nowLine=\$1\"-\"\$3\"-\"\$4;getline a;getline b;getline c;getline <\"${filePair}_index3PreV2.fastq\";print nowLine\"-\"\$3\"-\"\$4;print nowLine\"-\"\$3\"-\"\$4 > \"${filePair}_index3V2.fastq\";print a;print b;print c;getline d <\"${filePair}_index3PreV2.fastq\";getline e <\"${filePair}_index3PreV2.fastq\";getline f <\"${filePair}_index3PreV2.fastq\";print d > \"${filePair}_index3V2.fastq\";print e > \"${filePair}_index3V2.fastq\";print f >\"${filePair}_index3V2.fastq\"}' ${dir}/${filename}_index3PreV2.fastq > ${filename}_index3V2.fastq " > pre-temp.sh
sh pre-temp.sh
#write the correct file structure.
wait
awk 'BEGIN{FS="-";OFS="\t"}{print $2$3$4$5,NR;getline;getline;getline}' ${filename}_N4V2.fastq |sort -k1 >${filename}_N4.flag
awk 'BEGIN{FS="-";OFS="\t"}{print $2$3$4$5,NR;getline;getline;getline}' ${filename}_index1V2.fastq |sort -k1 >${filename}_index1.flag
awk 'BEGIN{FS="-";OFS="\t"}{print $2$3$4$5,NR;getline;getline;getline}' ${filename}_index2V2.fastq |sort -k1 >${filename}_index2.flag
awk 'BEGIN{FS="-";OFS="\t"}{print $2$3$4$5,NR;getline;getline;getline}' ${filename}_index3V2.fastq |sort -k1 >${filename}_index3.flag
awk '{if (x[$1] !=1){print $2;x[$1]=1}}' ${filename}_N4.flag|sort -n >${filename}_N4.uniq.record
awk '{if (x[$1] !=1){print $2;x[$1]=1}}' ${filename}_index1.flag|sort -n >${filename}_index1.uniq.record
awk '{if (x[$1] !=1){print $2;x[$1]=1}}' ${filename}_index2.flag|sort -n >${filename}_index2.uniq.record
awk '{if (x[$1] !=1){print $2;x[$1]=1}}' ${filename}_index3.flag|sort -n >${filename}_index3.uniq.record
awk 'NR==FNR{x[$1]=1} NR>FNR {if (x[FNR]==1){getline a;getline b;getline c;print $0;print a;print b;print c}}'  ${filename}_N4.uniq.record  ${filename}_N4V2.fastq > ${dir}/${filename}_N4uniq.fastq 
awk 'NR==FNR{x[$1]=1} NR>FNR {if (x[FNR]==1){getline a;getline b;getline c;print $0;print a;print b;print c}}'  ${filename}_N4.uniq.record  ${filePair}_N4V2.fastq > ${dir}/${filePair}_N4uniq.fastq 
awk 'NR==FNR{x[$1]=1} NR>FNR {if (x[FNR]==1){getline a;getline b;getline c;print $0;print a;print b;print c}}'  ${filename}_index1.uniq.record  ${filename}_index1V2.fastq > ${dir}/${filename}_index1uniq.fastq 
awk 'NR==FNR{x[$1]=1} NR>FNR {if (x[FNR]==1){getline a;getline b;getline c;print $0;print a;print b;print c}}'  ${filename}_index1.uniq.record  ${filePair}_index1V2.fastq > ${dir}/${filePair}_index1uniq.fastq 
awk 'NR==FNR{x[$1]=1} NR>FNR {if (x[FNR]==1){getline a;getline b;getline c;print $0;print a;print b;print c}}'  ${filename}_index2.uniq.record  ${filename}_index2V2.fastq > ${dir}/${filename}_index2uniq.fastq 
awk 'NR==FNR{x[$1]=1} NR>FNR {if (x[FNR]==1){getline a;getline b;getline c;print $0;print a;print b;print c}}'  ${filename}_index2.uniq.record  ${filePair}_index2V2.fastq > ${dir}/${filePair}_index2uniq.fastq 
awk 'NR==FNR{x[$1]=1} NR>FNR {if (x[FNR]==1){getline a;getline b;getline c;print $0;print a;print b;print c}}'  ${filename}_index3.uniq.record  ${filename}_index3V2.fastq > ${dir}/${filename}_index3uniq.fastq 
awk 'NR==FNR{x[$1]=1} NR>FNR {if (x[FNR]==1){getline a;getline b;getline c;print $0;print a;print b;print c}}'  ${filename}_index3.uniq.record  ${filePair}_index3V2.fastq > ${dir}/${filePair}_index3uniq.fastq 
  done
} 
pipeline<$1 2>&1

