#!/usr/bin/perl

$stroot = "COVA";
$shortLab = "DepthAggr";
$longLab = "Normalized Depth Tracks (Aggregate)";

$prio = 140;

##############################################################

$datadir = "data/depthAggr";
$trackname = "trackDb.$stroot.txt";

@abs = ("K9-WV","K9-HC","K27-WV","K27-HC","input");
@trts = ("V", "P");
@grps = ("V-K27-WV","P-K27-WV","V-K9-WV","P-K9-WV","V-K27-HC","V-K9-HC","V-input","P-input");

%useColors = ("P-K9-WV" => "0,153,0",       # green [WV]
              "V-K9-WV" => "0,0,204",       # blue  [WV]
              "P-K27-WV" => "255,128,0",    # orange [WV]
              "V-K27-WV" => "255,215,0",    # gold [WV]
              "V-K9-HC" => "153,51,255",    # purple [HC]
              "V-K27-HC" => "153,0,0",      # dark red [HC]
              "V-input" => "102,102,102",   # gray
              "P-input" => "102,102,102");  # gray

##############################################################

open(OUT, ">$trackname");

##### normalized depth #####
# write header supertrack header
print OUT "track\tdata$stroot\n";
print OUT "compositeTrack\ton\n";
print OUT "shortLabel\t$shortLab\n";
print OUT "longLabel\t$longLab\n";
print OUT "priority\t$prio\n";
print OUT "subGroup1\tview View NORM=normDepth\n";
print OUT "subGroup2\tAntibody Antibody"; foreach $ab (@abs) { print OUT " $ab=$ab"; } print OUT "\n";
print OUT "subGroup3\tTreatment Treatment"; foreach $trt (@trts) { print OUT " $trt=$trt"; } print OUT "\n";
print OUT "dimensions\tdimX=Antibody dimY=Treatment\n";
print OUT "sortOrder\tAntibody=+ Treatment=+\n";
print OUT "type\tbigWig\n\n";

# write header for depth tracks
print OUT "   track\tdata$stroot\_ViewDepth\n";
print OUT "   parent\tdata$stroot\n";
print OUT "   shortLabel\t$shortLab\n";
print OUT "   longLabel\t$longLab\n";
print OUT "   view\tNORM\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:25:11\n";
print OUT "   viewLimits\t0:30\n";
print OUT "   showSubtrackColorOnUi\ton\n";
print OUT "   viewUi\ton\n\n";

# write depth tracks
foreach $grp (@grps) {
  $bwfile = "$datadir/$grp.hg38.MAPQ5-PP.singlefrag.norm40M.bigWig";
  unless (-e $bwfile) { print "(ERROR?) Skipping $useID (no bigWig file found for \'$bwfile\').\n"; next; }
  $thiscolor = $useColors{$grp};
  if ($thiscolor eq "") { print "ERROR: Did not find color assignment for \'$grp\'.\n"; exit; }
  $prio++;
  @ar = split/\-/, $grp;
  $trt = $ar[0];
  if ($grp =~ /input/) { $ab = "input"; } else { $ab = "$ar[1]"."-"."$ar[2]"; }
  print OUT "      track\t$grp.$stroot\n";
  print OUT "      parent\tdata$stroot\_ViewDepth\n";
  print OUT "      bigDataUrl\t$bwfile\n";
  print OUT "      shortLabel\t$grp depth\n";
  print OUT "      longLabel\tnormalized depth ($grp)\n";
  print OUT "      type\tbigWig\n";
  print OUT "      color\t$thiscolor\n";
  print OUT "      priority\t$prio\n";
  print OUT "      visibility\thide\n";
  print OUT "      subGroups\tAntibody=$ab Treatment=$trt view=NORM\n\n";
}
print OUT "\n\n";

close(OUT);
