#!/usr/bin/perl

$stroot = "AgCC.ATAC.D";
$shortLab = "AgingCC ATACseq depth";
$longLab = "AgingCC ATACseq Normalized Depth Tracks";

$prio = 200;

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

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

$dataroot = "ATAC";
@oncos = ("APC","AKPS");
@stypes = ("Tumor","Crypt","SI","Sorted");
@ages = ("Young","Old");
@reps = (1,2,3);

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

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 ATAC=ATAC\n";
print OUT "subGroup2\tonc Oncogenes"; foreach $onc (@oncos) { print OUT " $onc=$onc"; } print OUT "\n";
print OUT "subGroup3\tstype SampleType"; foreach $st (@stypes) { print OUT " $st=$st"; } print OUT "\n";
print OUT "subGroup4\tage Age"; foreach $age (@ages) { print OUT " $age=$age"; } print OUT "\n";
print OUT "subGroup5\trep Replicate"; foreach $r (@reps) { print OUT " $r=$r"; } print OUT "\n";
print OUT "dimensions\tdimX=onc dimY=stype dimA=age\n";
print OUT "sortOrder\tview=+ onc=+ stype=+ age=+ rep=+\n";
print OUT "configurable\ton\n";
print OUT "type\tbigWig\n\n";

# write header for ATACseq 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\tATAC\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:80\n";
print OUT "   showSubtrackColorOnUi\ton\n";
print OUT "   viewUi\ton\n\n";

# write ATACseq depth tracks
foreach $onc (@oncos) { foreach $st (@stypes) { foreach $age (@ages) { foreach $r (@reps) {
  $id = "$onc"."_$st"."_$age"."_$dataroot"."_rep$r";
  $bwfile = "$datadir/$id.mm10.bwt2.MAPQ5-PP.rmChrM.rmdup.fragment.norm10M.bigWig";
  unless (-e $bwfile) { print "WARNING: Did not find \'$bwfile\'.\n"; next; }
  $prio++;
  print OUT "      track\t$id.D\n";
  print OUT "      parent\tdata$stroot\_ViewDepth\n";
  print OUT "      bigDataUrl\t$bwfile\n";
  print OUT "      shortLabel\t$id\n";
  print OUT "      longLabel\t$id normalized depth\n";
  print OUT "      type\tbigWig\n";
  print OUT "      color\t0,0,0\n";
  print OUT "      priority\t$prio\n";
  print OUT "      visibility\thide\n";
  print OUT "      subGroups\tview=ATAC onc=$onc stype=$st age=$age rep=$r\n\n";
} } } }

print OUT "\n\n";

close(OUT);
