#!/usr/bin/perl

$ltPink = "255,102,178";  # female E14.5
$dkPink = "255,0,127";    # female E16.5
$vdkPink = "204,0,102";   # female PND5
$ltBlue = "0,128,255";    # male E14.5
$dkBlue = "0,0,204";      # male E16.5
$ltGray = "102,102,102";  # input

open(OUT, ">trackDb.ChIP.txt");
$prio = 600;

### write composite track header
print OUT "track\tdataChIPseq\n";
print OUT "compositeTrack\ton\n";
print OUT "shortLabel\tChIPseq\n";
print OUT "longLabel\tChIPseq data\n";
print OUT "priority\t$prio\n";
print OUT "subGroup1\tview View COV=depth PK=peak\n";
print OUT "subGroup2\tSexAge SexAge M14=M_E14.5 F14=F_E14.5 M16=M_E16.5 F16=F_E16.5 I14=Input_E14.5 I16=Input_E16.5 FD5=F_PND5 I5=Input_PND5\n";
print OUT "subGroup3\tIP IP NR2F2=NR2F2 AR=AR ERa=ERa\n";
print OUT "subGroup4\tRep Rep 1=1 2=2 M=M\n";
print OUT "dimensions\tdimX=IP dimY=SexAge dimA=Rep\n";
print OUT "sortOrder\tSexAge=+ Rep=+ view=+\n";
print OUT "configurable\ton\n";
print OUT "type\tbed 3\n\n";

### write header for depth tracks
print OUT "   track\tdataChIPseq_ViewDepth\n";
print OUT "   parent\tdataChIPseq\n";
print OUT "   shortLabel\tChIPseq Depth\n";
print OUT "   longLabel\tChIP-seq normalized depth\n";
print OUT "   view\tCOV\n";
print OUT "   visibility\tfull\n";
print OUT "   type\tbigWig\n";
print OUT "   allButtonPair\ton\n";
print OUT "   centerLabelsDense\ton\n";
print OUT "   dragAndDrop\ton\n";
print OUT "   alwaysZero\ton\n";
print OUT "   graphTypeDefault\tbar\n";
print OUT "   maxHeightPixels\t150:30:11\n";
print OUT "   viewLimits\t0:40\n";
print OUT "   showSubtrackColorOnUi\ton\n";
print OUT "   viewUi\ton\n\n";

### write depth tracks
# NR2F2
@ireps = ("E14.5_female_NR2F2-rep1", "E14.5_female_NR2F2-rep2", "E14.5_male_NR2F2-rep1", "E14.5_male_NR2F2-rep2", "pooled_input-rep1-NR2F2", "pooled_input-rep2-NR2F2");
foreach $id (@ireps) {
  if ($id =~ /rep1/) { $r = 1; } elsif ($id =~ /rep2/) { $r = 2; } else { print "ERROR: replicate for $id?\n"; exit; }
  if ($id =~ /input/) { $sexage = "I14"; $label = "input E14.5\-$r (NR2F2)"; $rgb = $ltGray; }
  elsif ($id =~ /female/) { $sexage = "F14"; $label = "Female E14.5\-$r NR2F2"; $rgb = $ltPink; }
  elsif ($id =~ /male/) { $sexage = "M14"; $label = "Male E14.5\-$r NR2F2"; $rgb = $ltBlue; }
  else { print "ERROR: unexpected age/sex for $id\n"; exit; }
  $bwfile = "data/ChIPseq/$id.bwt.mm10.rmdup.ext200.norm25M.bigWig";
  unless (-e $bwfile) { print "WARNING: Did not find $bwfile.\n"; exit; }
  $prio++;
  print OUT "      track\t$sexage$r.NR2F2\n";
  print OUT "      parent\tdataChIPseq_ViewDepth\n";
  print OUT "      bigDataUrl\t$bwfile\n";
  print OUT "      shortLabel\t$label\n";
  print OUT "      longLabel\tnormalized ChIP-seq depth: $label\n";
  print OUT "      type\tbigWig\n";
  print OUT "      color\t$rgb\n";
  print OUT "      priority\t$prio\n";
  print OUT "      visibility\thide\n";
  print OUT "      subGroups\tSexAge=$sexage IP=NR2F2 Rep=$r view=COV\n\n";
}
@mreps = ("E14.5_female_NR2F2", "E14.5_male_NR2F2", "pooled_input-NR2F2");
foreach $id (@mreps) {
  $r = "M";
  if ($id =~ /input/) { $sexage = "I14"; $label = "input E14.5 (NR2F2)"; $rgb = $ltGray; }
  elsif ($id =~ /female/) { $sexage = "F14"; $label = "Female E14.5 NR2F2"; $rgb = $ltPink; }
  elsif ($id =~ /male/) { $sexage = "M14"; $label = "Male E14.5 NR2F2"; $rgb = $ltBlue; }
  else { print "ERROR: unexpected age/sex for $id\n"; exit; }
  $bwfile = "data/ChIPseq/$id.bwt.mm10.rmdup.ext200.norm50M.bigWig";
  unless (-e $bwfile) { print "WARNING: Did not find $bwfile.\n"; exit; }
  $prio++;
  print OUT "      track\t$sexage$r.NR2F2\n";
  print OUT "      parent\tdataChIPseq_ViewDepth\n";
  print OUT "      bigDataUrl\t$bwfile\n";
  print OUT "      shortLabel\t$label\n";
  print OUT "      longLabel\tnormalized ChIP-seq depth: $label\n";
  print OUT "      type\tbigWig\n";
  print OUT "      color\t$rgb\n";
  print OUT "      priority\t$prio\n";
  print OUT "      visibility\thide\n";
  print OUT "      subGroups\tSexAge=$sexage IP=NR2F2 Rep=$r view=COV\n\n";
}
print OUT "\n\n";
# AR
$prio++;
print OUT "      track\tM161.AR\n";
print OUT "      parent\tdataChIPseq_ViewDepth\n";
print OUT "      bigDataUrl\tdata/ChIPseq/AR-rep1.bwt.mm10.rmdup.ext200.norm10M.bigWig\n";
print OUT "      shortLabel\tMale E16.5-1 AR\n";
print OUT "      longLabel\tnormalized ChIP-seq depth: Male E16.5-1 AR\n";
print OUT "      type\tbigWig\n";
print OUT "      color\t$dkBlue\n";
print OUT "      priority\t$prio\n";
print OUT "      visibility\thide\n";
print OUT "      subGroups\tSexAge=M16 IP=AR Rep=1 view=COV\n\n";
$prio++;
print OUT "      track\tM162.AR\n";
print OUT "      parent\tdataChIPseq_ViewDepth\n";
print OUT "      bigDataUrl\tdata/ChIPseq/AR-rep2.bwt.mm10.rmdup.ext200.norm10M.bigWig\n";
print OUT "      shortLabel\tMale E16.5-2 AR\n";
print OUT "      longLabel\tnormalized ChIP-seq depth: Male E16.5-2 AR\n";
print OUT "      type\tbigWig\n";
print OUT "      color\t$dkBlue\n";
print OUT "      priority\t$prio\n";
print OUT "      visibility\thide\n";
print OUT "      subGroups\tSexAge=M16 IP=AR Rep=2 view=COV\n\n";
$prio++;
print OUT "      track\tM16M.AR\n";
print OUT "      parent\tdataChIPseq_ViewDepth\n";
print OUT "      bigDataUrl\tdata/ChIPseq/AR.merged.bwt.mm10.rmdup.ext200.norm10M.bigWig\n";
print OUT "      shortLabel\tMale E16.5 AR\n";
print OUT "      longLabel\tnormalized ChIP-seq depth: Male E16.5 AR\n";
print OUT "      type\tbigWig\n";
print OUT "      color\t$dkBlue\n";
print OUT "      priority\t$prio\n";
print OUT "      visibility\thide\n";
print OUT "      subGroups\tSexAge=M16 IP=AR Rep=M view=COV\n\n";
$prio++;
print OUT "      track\tI161.AR\n";
print OUT "      parent\tdataChIPseq_ViewDepth\n";
print OUT "      bigDataUrl\tdata/ChIPseq/input-AR.bwt.mm10.rmdup.ext200.norm10M.bigWig\n";
print OUT "      shortLabel\tinput E16.5 (AR)\n";
print OUT "      longLabel\tnormalized ChIP-seq depth: input E16.5 (AR)\n";
print OUT "      type\tbigWig\n";
print OUT "      color\t$ltGray\n";
print OUT "      priority\t$prio\n";
print OUT "      visibility\thide\n";
print OUT "      subGroups\tSexAge=I16 IP=AR Rep=1 view=COV\n\n";
# ERa
$prio++;
print OUT "      track\tFD5Veh.ERa\n";
print OUT "      parent\tdataChIPseq_ViewDepth\n";
print OUT "      bigDataUrl\tdata/ChIPseq_WJ/PND5-F_Veh_ERa.bwt.mm10.rmdup.ext200.norm10M.bigWig\n";
print OUT "      shortLabel\tFemale PND5 ERa (Veh)\n";
print OUT "      longLabel\tnormalized ChIP-seq depth: Female PND5 ERa (Veh)\n";
print OUT "      type\tbigWig\n";
print OUT "      color\t$vdkPink\n";
print OUT "      priority\t$prio\n";
print OUT "      visibility\thide\n";
print OUT "      subGroups\tSexAge=FD5 IP=ERa Rep=1 view=COV\n\n";
$prio++;
print OUT "      track\tFD5DES.ERa\n";
print OUT "      parent\tdataChIPseq_ViewDepth\n";
print OUT "      bigDataUrl\tdata/ChIPseq_WJ/PND5-F_DES_ERa.bwt.mm10.rmdup.ext200.norm10M.bigWig\n";
print OUT "      shortLabel\tFemale PND5 ERa (DES)\n";
print OUT "      longLabel\tnormalized ChIP-seq depth: Female PND5 ERa (DES)\n";
print OUT "      type\tbigWig\n";
print OUT "      color\t$vdkPink\n";
print OUT "      priority\t$prio\n";
print OUT "      visibility\thide\n";
print OUT "      subGroups\tSexAge=FD5 IP=ERa Rep=1 view=COV\n\n";
$prio++;
print OUT "      track\tFD5I.ERa\n";
print OUT "      parent\tdataChIPseq_ViewDepth\n";
print OUT "      bigDataUrl\tdata/ChIPseq_WJ/PND5-F_input.bwt.mm10.rmdup.ext200.norm10M.bigWig\n";
print OUT "      shortLabel\tFemale PND5 input\n";
print OUT "      longLabel\tnormalized ChIP-seq depth: Female PND5 input\n";
print OUT "      type\tbigWig\n";
print OUT "      color\t$ltGray\n";
print OUT "      priority\t$prio\n";
print OUT "      visibility\thide\n";
print OUT "      subGroups\tSexAge=I5 IP=ERa Rep=1 view=COV\n\n";


### write header for peak tracks
print OUT "   track\tdataChIPseq_ViewDepthPeaks\n";
print OUT "   parent\tdataChIPseq\n";
print OUT "   shortLabel\tChIP-seq Peaks\n";
print OUT "   longLabel\tChIP-seq Peaks\n";
print OUT "   view\tPK\n";
print OUT "   visibility\tdense\n";
print OUT "   type\tbigBed 3\n";
print OUT "   allButtonPair\ton\n";
print OUT "   centerLabelsDense\ton\n";
print OUT "   dragAndDrop\ton\n";
print OUT "   showSubtrackColorOnUi\ton\n";
print OUT "   viewUi\ton\n\n";

### write peak tracks
$prio++;
print OUT "       track\tE14F.NR2F2.pk\n";
print OUT "       parent\tdataChIPseq_ViewDepthPeaks\n";
print OUT "       bigDataUrl\tdata/ChIPseq/E14.5_female_NR2F2.HOMER_e-5.bigBed\n";
print OUT "       shortLabel\tFemale E14.5 NR2F2 peaks\n";
print OUT "       longLabel\tHOMER peak calls: Female E14.5 NR2F2 peaks\n";
print OUT "       type\tbigBed 3\n";
print OUT "       color\t$ltPink\n";
print OUT "       priority\t$prio\n";
print OUT "       visibility\tdense\n";
print OUT "       subGroups\tSexAge=F14 IP=NR2F2 Rep=M view=PK\n\n";
$prio++;
print OUT "       track\tE14M.NR2F2.pk\n";
print OUT "       parent\tdataChIPseq_ViewDepthPeaks\n";
print OUT "       bigDataUrl\tdata/ChIPseq/E14.5_male_NR2F2.HOMER_e-5.bigBed\n";
print OUT "       shortLabel\tMale E14.5 NR2F2 peaks\n";
print OUT "       longLabel\tHOMER peak calls: Male E14.5 NR2F2 peaks\n";
print OUT "       type\tbigBed 3\n";
print OUT "       color\t$ltBlue\n";
print OUT "       priority\t$prio\n";
print OUT "       visibility\tdense\n";
print OUT "       subGroups\tSexAge=M14 IP=NR2F2 Rep=M view=PK\n\n";
$prio++;
print OUT "       track\tE16M.AR.pk\n";
print OUT "       parent\tdataChIPseq_ViewDepthPeaks\n";
print OUT "	  bigDataUrl\tdata/ChIPseq/AR.homer-peakcalls.fdr_e-5.300bp.bigBed\n";
print OUT "       shortLabel\tMale E16.5 AR peaks\n";
print OUT "	  longLabel\tHOMER peak calls: Male E16.5 AR peaks\n";
print OUT "       type\tbigBed 3\n";
print OUT "       color\t$dkBlue\n";
print OUT "	  priority\t$prio\n";
print OUT "       visibility\tdense\n";
print OUT "       subGroups\tSexAge=M16 IP=AR Rep=M view=PK\n\n";

$prio++;
print OUT "       track\tFD5Veh.ERa.pk\n";
print OUT "       parent\tdataChIPseq_ViewDepthPeaks\n";
print OUT "       bigDataUrl\tdata/ChIPseq_WJ/PND5-F_Veh_ERa.homer-peakcalls.fdr_e-5.300bp.bigBed\n";
print OUT "       shortLabel\tFemale PND5 ERa (Veh) peaks\n";
print OUT "       longLabel\tHOMER peak calls: Female PND5 ERa (Veh) peaks\n";
print OUT "       type\tbigBed 3\n";
print OUT "       color\t$vdkPink\n";
print OUT "       priority\t$prio\n";
print OUT "       visibility\tdense\n";
print OUT "       subGroups\tSexAge=FD5 IP=ERa Rep=1 view=PK\n\n";
$prio++;
print OUT "       track\tFD5DES.ERa.pk\n";
print OUT "       parent\tdataChIPseq_ViewDepthPeaks\n";
print OUT "       bigDataUrl\tdata/ChIPseq_WJ/PND5-F_DES_ERa.homer-peakcalls.fdr_e-5.300bp.bigBed\n";
print OUT "       shortLabel\tFemale PND5 ERa (DES) peaks\n";
print OUT "       longLabel\tHOMER peak calls: Female PND5 ERa (DES) peaks\n";
print OUT "       type\tbigBed 3\n";
print OUT "       color\t$vdkPink\n";
print OUT "       priority\t$prio\n";
print OUT "       visibility\tdense\n";
print OUT "       subGroups\tSexAge=FD5 IP=ERa Rep=1 view=PK\n\n";

print OUT "\n\n";
close(OUT);
