#!/usr/bin/perl

$stroot = "ATAC01";
$shortLab = "ATACseq MouseObesity Feb2025";
$longLab = "ATACseq Normalized Depth Tracks - MouseObesity Feb2025";

$prio = 200;

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

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

@sexes = ("M","F");
@diets = ("HFDcele","HFDCR","HFD","Keto","LFD","Chow");
@genotypes = ("WT","ob","MBD2");
@replicates = (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 PK=PK\n";
print OUT "subGroup2\tdiet Diets"; foreach $d (@diets) { print OUT " $d=$d"; } print OUT "\n";
print OUT "subGroup3\tgt Genotype"; foreach $gt (@genotypes) { print OUT " $gt=$gt"; } print OUT "\n";
print OUT "subGroup4\tsex Sex"; foreach $s (@sexes) { print OUT " $s=$s"; } print OUT "\n";
print OUT "subGroup5\trep Replicate"; foreach $r (@replicates) { print OUT " rep$r=rep$r"; } print OUT "\n";
print OUT "dimensions\tdimX=diet dimY=gt dimA=sex\n";
print OUT "sortOrder\tview=+ diet=+ gt=+ sex=+ 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:40:11\n";
print OUT "   autoScale\tgroup\n";
#print OUT "   viewLimits\t0:5\n";
print OUT "   showSubtrackColorOnUi\ton\n";
print OUT "   viewUi\ton\n\n";

# write ATACseq depth tracks
foreach $d (@diets) { foreach $gt (@genotypes) { foreach $sex (@sexes) { foreach $r (@replicates) {
  $id = "$d"."-$gt"."-$sex"."-ATAC-$r";
  $label_id = $id;
  $bwfile = "$datadir/$id.mm39.bwt2.MAPQ5.rmChrM.rmdup.fragment.norm5M.bigWig";
  unless (-e $bwfile) { next; } # print "WARNING: Did not find \'$bwfile\'.\n"; next; } ## don't print warning since there are tons of combinations that don't exist for this
  $prio++;
  print OUT "      track\t$label_id\n";
  print OUT "      parent\tdata$stroot\_ViewDepth\n";
  print OUT "      bigDataUrl\t$bwfile\n";
  print OUT "      shortLabel\t$label_id\n";
  print OUT "      longLabel\t$label_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 diet=$d gt=$gt sex=$sex rep=rep$r\n\n";
} } } }

# write header for peak tracks
print OUT "   track\tdata$stroot\_Peaks\n";
print OUT "   parent\tdata$stroot\n";
print OUT "   shortLabel\t$shortLab\n";
print OUT "   longLabel\t$longLab\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 individual tracks here, if you want to include them (skipping this for now)

# write unified peak track
$bbfile = "$datadir/union_ATAC_peaks.bigBed";
unless (-e $bbfile) { print "ERROR: Did not find $bbfile.\n"; exit; }
$prio++;
print OUT "      track\tuni.$stroot\n";
print OUT "      parent\tdata$stroot\_Peaks\n";
print OUT "      bigDataUrl\t$bbfile\n";
print OUT "      shortLabel\tATAC unified peaks\n";
print OUT "      longLabel\tATAC unified peaks\n";
print OUT "      type\tbigBed 3\n";
print OUT "      color\t0,0,0\n";
print OUT "      priority\t$prio\n";
print OUT "      visibility\tdense\n";
print OUT "      subGroups\tView=PK\n\n";

print OUT "\n\n";

close(OUT);
