#!/usr/bin/perl

$stroot = "RNA13";
$shortLab = "RNAseq gonad Ketodiet Dec2025";
$longLab = "RNAseq Normalized Depth Tracks - gonad Keto diet Dec2025";
$datasetLab = "Keto";  # mostly for differentiating controls samples from multiple datasets

$prio = 400;

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

$datadir = "data/NS20225-gonad-Keto";
$trackname = "trackDb.$stroot.txt";

@diets = ("Ctrl","Keto");
@sexes = ("M","F");
@reps = (1,2,3,4);
@strands = ("plus","minus");
%sexLabel = ("M" => "Male", "F" => "Female");
%dietLabel = ("Ctrl" => "Ctrl", "Keto" => "Keto");

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

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 RNA=RNA\n";
print OUT "subGroup2\tdiet Diet"; foreach $diet (@diets) { print OUT " $diet=$diet"; } print OUT "\n";
print OUT "subGroup3\tsex Sex"; foreach $mf (@sexes) { print OUT " $mf=$mf"; } print OUT "\n";
print OUT "subGroup4\trep Replicate"; foreach $r (@reps) { print OUT " $r=$r"; } print OUT "\n";
print OUT "subGroup5\tstr Strand"; foreach $str (@strands) { print OUT " $str=$str"; } print OUT "\n";
print OUT "dimensions\tdimX=diet dimY=sex dimA=str\n";
print OUT "sortOrder\tview=+ diet=+ sex=+ rep=+ str=+\n";
print OUT "configurable\ton\n";
print OUT "type\tbigWig\n\n";

# write header for RNAseq 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\tRNA\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 RNAseq depth tracks
foreach $diet (@diets) { foreach $mf (@sexes) { foreach $r (@reps) { foreach $str (@strands) {
  $id = "$diet"."_$mf-rep$r";
  $label_id = $id;
  if ($diet eq "Ctrl") { $shortuse = "$id\[$datasetLab]"; } else { $shortuse = $id; }
  $ss = uc(substr($str, 0, 1));
  $bwfile = "$datadir/$id.depthRPM.$str.bw";
  unless (-e $bwfile) { print "WARNING: Did not find \'$bwfile\'.\n"; next; }
  $prio++;
  print OUT "      track\t$id.$datasetLab.$ss\n";
  print OUT "      parent\tdata$stroot\_ViewDepth\n";
  print OUT "      bigDataUrl\t$bwfile\n";
  print OUT "      shortLabel\t$shortuse ($str)\n";
  print OUT "      longLabel\t$dietLabel{$diet} diet [$datasetLab], $sexLabel{$mf} rep$r RPM-normalized depth ($str)\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=RNA diet=$diet sex=$mf rep=$r str=$str\n\n";
} } } }

print OUT "\n\n";

close(OUT);
