#!/bin/sh
#$ -S /bin/sh
# sh chipatlas/sh/makePeakBrowser/makeBed9GFF3.sh tmpDirForPeakBrowser/sortedBed/rn6/rn6.20.bed rn6 His Brs 05 xxx xxx
# qsub -l short -l s_vmem=8G,mem_req=8G -e a.log.txt -o a.log.txt chipatlas/sh/makePeakBrowser/makeBed9GFF3.sh tmpDirForPeakBrowser/sortedBed/ce10/ce10.20.bed ce10 His Brs 05 xxx xxx


inBed=$1
Genome=$2
agL=$3
ctL=$4
Qval=$5
agS=$6
ctS=$7

eval `echo $inBed@$Genome@$agL@$ctL@$Qval@$agS@$ctS| tr '@' '\t'| awk -F '\t' '
function subStr(str) {
  gsub("\\\(", "BRACKETL", str)
  gsub("\\\)", "BRACKETR", str)
  gsub("+", "PULUS", str)
  gsub(",", "KOMMA", str)
  gsub("\\\.", "PERIOD", str)
  gsub("/", "SLASH", str)
  gsub(" ", "_", str)
  return str
} BEGIN {
  while ((getline < "chipatlas/sh/abbreviationList_AG.tab") > 0) a[$1] = $2
  while ((getline < "chipatlas/sh/abbreviationList_CT.tab") > 0) c[$1] = $2
} {
  Lc = ($4 == "xxx") ? "ALL" : $4
  ag = ($6 == "xxx") ? "AllAg" : subStr($6)
  ct = ($7 == "xxx") ? "AllCell" : subStr($7)
  AG = ($6 == "xxx") ? a[$3] : $6
  if (AG == "hmr") AG = "Hypo MR"
  if (AG == "pmd") AG = "Partial MR"
  if (AG == "hypermr") AG = "Hyper MR"
  CT = ($7 == "xxx") ? c[$4] : $7
  CT = ($4 == "xxx") ? "All cell types" : CT
  if ($4 == "xxx") $4 = "ALL"
  print "outFn=\047"$3"."Lc"."$5"."ag"."ct".bed\047"
  printf "header=\047track name=\"%s (@ %s)%s\" ", AG, CT, ($5+0 >0) ? " "$5*10 : ""
  print "url=\"http://chip-atlas.org/view?id=$$\" gffTags=\"on\"\047"
  print "meta=\047"a[$3]"@"$6"@"c[$4]"@"$7"@"$5"\047"
}'`



cat $inBed| awk -F '\t' -v OFS='\t' -v header="$header" -v Genome=$Genome -v JOB_ID=$JOB_ID -v outFn="$outFn" -v meta="$meta" '
function colorCode(x) {
  MAX=1000
  N=MAX/4
  b=255
  if(x<N) {
    R=0
    G=b/N*x
    B=b
  } else if (x>=N && x<2*N) {
    R=0
    G=b
    B=-b/N*x+2*b
  } else if (x>=2*N && x<3*N) {
    R=b/N*x-2*b
    G=b
    B=0
  } else if (x>=3*N && x<4*N) {
    R=255
    G=-b/N*x+4*b
    B=0
  } else if (x>=4*N) {
    R=255
    G=0
    B=0
  }
  return int(R)","int(G)","int(B)
}
function maxVal(y) {
  return (y > 1000) ? 1000 : y
}
BEGIN {
  print header
  while ((getline < "chipatlas/lib/metadata/metadataForPeakBrowser.tsv") > 0) x[$1] = $7
  for (i=0; i<=10000; i++) {
    c[i] = colorCode(i)
    v[i] = maxVal(i)
  }
  c["hypermr"] = "70,70,70"
  c["pmd"] = "229,196,148"
  c["hmr"] = "231,138,195"
  v["hypermr"] = 0
  v["pmd"] = 0
  v["hmr"] = 0
} {
  str = x[$4]
  if (!s[$4]++) SRXs = SRXs "," $4
  if ($5 == "hypermr") sub("Name=Bisulfite-Seq%20", "Name=Hyper%20MR%20", str)
  if ($5 == "pmd") sub("Name=Bisulfite-Seq%20", "Name=Partial%20MR%20", str)
  if ($5 == "hmr") sub("Name=Bisulfite-Seq%20", "Name=Hypo%20MR%20", str)
  print $1, $2, $3, str, v[$5], ".", $2, $3, c[$5]
} END {
  split(meta, m, "@")
  if (m[2] == "xxx") m[2] = "-"
  if (m[2] == "hypermr") m[2] = "Hyper MR"
  if (m[2] == "pmd") m[2] = "Partial MR"
  if (m[2] == "hmr") m[2] = "Hypo MR"
  if (m[4] == "xxx") m[4] = "-"
  sub(",", "", SRXs)
  fn = "tmpDirForPeakBrowser/bedList/"Genome"/"JOB_ID".bedList.txt"
  print outFn, Genome, m[1], m[2], m[3], m[4], m[5], SRXs >> fn
}' > tmpDirForPeakBrowser/public/$Genome/$outFn

# Index 作成
java -Xmx2500m -Djava.awt.headless=true -jar chipatlas/bin/IGVTools/igvtools.jar index tmpDirForPeakBrowser/public/$Genome/$outFn

exit

