#$ -S /bin/bash
#$ -cwd
EXPERIMENT=$1
REVISION=$2
SPECIES=$3
PNUM=$4

#summarize the mapping result
~/chipatlas/bin/MapSum -sum ${EXPERIMENT}.sum.gz -outprefix ${EXPERIMENT} > temp 2>&1
cat temp | grep -v "reads were processed"
# .mapsum.html の check
if [ ! -f ./${EXPERIMENT}.mapsum.html ]; then
  echo -e "=-=-=MapSum\tBad"
  cd ../
  rm -rf ${EXPERIMENT}
  exit
else
  echo -e "=-=-=MapSum\tOK"
fi
rm *.png
rm *.sum.gz
ls -lt | grep "mapsum"
# .graph の作成
~/chipatlas/bin/MPTC -species ${SPECIES} -revision ${REVISION} -graph ${EXPERIMENT}.graph -track ${EXPERIMENT} -thread ${PNUM} -bisulalign ${EXPERIMENT}.bisulalign.gz > temp 2>&1
cat temp | grep -v "processed"
# .graph の check
if [ ! -f ./${EXPERIMENT}.graph ]; then
  echo -e "=-=-=MPTC\tBad"
  cd ../
  rm -rf ${EXPERIMENT}
  exit
else
  echo -e "=-=-=MPTC\tOK"
fi
ls -lt | grep ".graph"
rm *.bisulalign.gz
# Coverage の作成
~/chipatlas/bin/CalcCoverage -graph ${EXPERIMENT}.graph -outprefix ${EXPERIMENT} > temp 2>&1
cat temp | grep -v "Processing" | grep -v "Step"
# .graph の check
# .stat.html の check
if [ ! -f ./${EXPERIMENT}.stat.html ]; then
  echo -e "=-=-=CalcCoverage\tBad"
  cd ../
  rm -rf ${EXPERIMENT}
  exit
else
  echo -e "=-=-=CalcCoverage\tOK"
fi
rm -r *.image
ls -lt | grep "stat"
# .graph から BedGraph を作成
~/chipatlas/bin/MethExport -graph ${EXPERIMENT}.graph -cpg true -methyl ${EXPERIMENT}.cpg.methyl.bedGraph.gz -cover ${EXPERIMENT}.cpg.cover.bedGraph.gz -tab /dev/null -methpipe ${EXPERIMENT}.cpg.methpipe.tab.gz > temp 2>&1
cat temp | grep -v "Start analysis" | grep -v "chr" | grep -v "LAMBDA"
# .cpg.cover.bedGraph.gz .cpg.methpipe.tab.gz .cpg.methyl.bedGraph.gz の check
if [ ! -f ./${EXPERIMENT}.cpg.cover.bedGraph.gz ] || [ ! -f ./${EXPERIMENT}.cpg.methpipe.tab.gz ] || [ ! -f ./${EXPERIMENT}.cpg.methyl.bedGraph.gz ]; then
  echo -e "=-=-=MethExport\tBad"
  cd ../
  rm -rf ${EXPERIMENT}
  exit
else
  echo -e "=-=-=MethExport\tOK"
fi
ls -lt | grep "cpg"
rm *.graph
rm temp
