
sample=$1
indir=$2

#reads dimer
reads_file=$indir"/"$sample"/1.QC/"$sample"_reads.txt"

total_count=`awk '{if(NR>1){print $0}}' $reads_file | wc -l`
dimer_count=`grep dimer $reads_file | wc -l`
dimer_rate=`awk -v x=$total_count -v y=$dimer_count 'BEGIN{printf "%.2f\n",y*100/x}'`

#fusion count
IPC_count_file=$indir"/"$sample"/2.mapping/"$sample".IPC.reads.txt"
nonspecific_file=$indir"/"$sample"/2.mapping/"$sample".nonspecific.reads.txt"
target_file=$indir"/"$sample"/2.mapping/"$sample".target.reads.txt"
fusion_file=$indir"/"$sample"/4.fusion/fusion_results.txt"

IPC_count=`cat $IPC_count_file | wc -l`
nonspecific_count=`cat $nonspecific_file | wc -l`
target_count=`cat $target_file | wc -l`
fusion_count=`awk '{sum+=$NF}END{print sum}' $fusion_file`

fusion_rate=`awk -v x=$total_count -v y=$fusion_count 'BEGIN{printf "%.2f\n",y*100/x}'`
IPC_rate=`awk -v x=$total_count -v y=$IPC_count 'BEGIN{printf "%.2f\n",y*100/x}'`
nonspecific_rate=`awk -v x=$total_count -v y=$nonspecific_count 'BEGIN{printf "%.2f\n",y*100/x}'`
target_no_fusion_count=`expr $target_count - $fusion_count`
target_no_fusion_count_rate=`awk -v x=$total_count -v y=$target_no_fusion_count 'BEGIN{printf "%.2f\n",y*100/x}'`
target_rate=`awk -v x=$total_count -v y=$target_count 'BEGIN{printf "%.2f\n",y*100/x}'`
Misc_count=`expr $total_count - $IPC_count - $nonspecific_count - $fusion_count - $dimer_count - $target_no_fusion_count`
Misc_rate=`awk -v x=$total_count -v y=$Misc_count 'BEGIN{printf "%.2f\n",y*100/x}'`

#outfile
QC_stat_file=$indir"/"$sample"/"$sample"_QC_stat.txt"

echo -e "Sample\tTotal\tDimer\tDimer_Rate(%)\tFusion\tFusion_Rate(%)\tIPC\tIPC_Rate(%)\tNonspecific\tNonspecific_Rate(%)\tMisc\tMisc_Rate\tTarget_no_fusion\tTarget_no_fusion_Rate(%)" > $QC_stat_file
echo -e "$sample\t$total_count\t$dimer_count\t$dimer_rate\t$fusion_count\t$fusion_rate\t$IPC_count\t$IPC_rate\t$nonspecific_count\t$nonspecific_rate\t$Misc_count\t$Misc_rate\t$target_no_fusion_count\t$target_no_fusion_count_rate" >> $QC_stat_file

#fusion counts
fusion_stat_file=$indir"/"$sample"/"$sample"_fusion_stat.txt"
awk '{if(NR==1){print "Sample\t"$0}}'  $fusion_file > $fusion_stat_file
awk '{if(NR>1){print "'$sample'\t"$0}}' $fusion_file >> $fusion_stat_file

echo "Done!"

#reads primer check
primer1=$indir"/"$sample"/1.QC/"$sample"*reads_primer_check*"
sam1=$indir"/"$sample"/2.mapping/"$sample"*.sam"
pe_bam=$indir"/"$sample"/2.mapping/"$sample".pe.bam"

rm $primer1 $sam1 $pe_bam

echo "Remove Done!"