#!/bin/sh
#$ -S /bin/sh

# BS-seq の解析終了後、bzou から chipatlas 配下に移動させ、summary.txt を作成する
# 実行方法
#   qsub chipatlas/sh/bzou2chipatlas.sh $genome $SRX bzou3

inG=$1
SRX=$2
bzou=$3

echo $inG $SRX $bzou
mkdir -p chipatlas/results/$inG/bmap/cover/
mkdir -p chipatlas/results/$inG/bmap/methyl/
for fn in Bed BigBed; do
  mkdir -p chipatlas/results/$inG/bmap/hmr/$fn/
  mkdir -p chipatlas/results/$inG/bmap/pmd/$fn/
  mkdir -p chipatlas/results/$inG/bmap/hypermr/$fn/
done
mkdir -p chipatlas/results/$inG/bmap/log/
mkdir -p chipatlas/results/$inG/bmap/summary/
mkdir -p chipatlas/results/$inG/bmap/tag/

if [ $inG = "rm6" ]; then
  outG="rn6"
else
  outG=$inG
fi

i_cover_bw="$bzou/results/$inG/$SRX/$SRX.cpg.cover.bw"
o_cover_bw="chipatlas/results/$outG/bmap/cover/$SRX.cover.bw"
i_methyl_bw="$bzou/results/$inG/$SRX/$SRX.cpg.methyl.bw "
o_methyl_bw="chipatlas/results/$outG/bmap/methyl/$SRX.methyl.bw"
i_log="$bzou/$inG.$SRX.log.txt"
o_log="chipatlas/results/$outG/bmap/log/$SRX.log.txt"
o_summary="chipatlas/results/$outG/bmap/summary/$SRX.txt"
gsize="chipatlas/lib/genome_size/$outG.chrom.sizes"

mv -f $i_cover_bw $o_cover_bw
mv -f $i_methyl_bw $o_methyl_bw
for BedType in hmr pmd hypermr; do
  i_Bed="$bzou/results/$inG/$SRX/$SRX.$BedType"
  o_Bed="chipatlas/results/$outG/bmap/$BedType/Bed/$SRX.$BedType.bed"
  o_BBd="chipatlas/results/$outG/bmap/$BedType/BigBed/$SRX.$BedType.bb"
  sort -k1,1 -k2,2n $i_Bed| chipatlas/bin/bedClip stdin $gsize $o_Bed
  rm $i_Bed
  chipatlas/bin/bedToBigBed $o_Bed $gsize $o_BBd
done

cat $i_log| awk '{
  if ($0 ~ /^SRX = /) a[1] = $3
  if ($0 ~ /^Read layout: /) a[2] = $3
  if ($3 == "okishinya") {
    if (a[2] == "SINGLE" && $NF ~ /fastq.gz$/) a[3] += $5
    if (a[2] == "PAIRED" && $NF ~ /_1.fastq.gz$/) a[3] += $5
  }
  if ($3 == "okishinya" && $NF ~ /bisulalign.gz$/) a[4] = $5
  if ($1 == "=-=-=num_reads") a[5] = $2
  if ($1 == "=-=-=mapping_rate_1") a[6] = $2 * 100
  if ($1 == "=-=-=methyl_rate_cpg_total") a[7] = $2 * 100
  if ($1 == "=-=-=coverage") a[8] = $2
  if ($1 == "=-=-=low_methyl_region") a[9] = $2
  if ($1 == "=-=-=partial_methyl_region") a[10] = $2
  if ($1 == "=-=-=high_methyl_region") a[11] = $2
  if ($1 == "=-=-=duration") a[12] = sprintf("%.2f", $2 * 60)
} END {
  printf a[1]
  for (i=2; i<=12; i++) printf "\t" a[i]
  printf "\n"
}' > $o_summary
mv -f $i_log $o_log
exit
