#!/usr/bin/perl

$stroot = "RNA01";
$shortLab = "RNAseq MouseObesity June2025";
$longLab = "RNAseq Normalized Depth Tracks - MouseObesity June2025";

$prio = 100;

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

$datadir = "data/RNA-02jun2025-NOV0726";
$trackname = "trackDb.$stroot.txt";

@sexes = ("M","F");
@diets = ("CelHFD","CRHFD","HFD","Keto","LFD","Chow");
@genotypes = ("WT","ob","Mbd2");
@replicates = (1,2,3);
@strands = ("plus","minus");

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

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 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 "subGroup6\tstr Strand"; foreach $s (@strands) { print OUT " $s=$s"; } print OUT "\n";
print OUT "dimensions\tdimX=diet dimY=gt dimA=sex dimB=str\n";
print OUT "sortOrder\tview=+ diet=+ gt=+ 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 $d (@diets) { foreach $gt (@genotypes) { foreach $sex (@sexes) { foreach $r (@replicates) { foreach $str (@strands) {
  $id = "$d"."-$gt"."-$sex"."-$r";
  $label_id = $id;
  $ss = uc(substr($str, 0, 1));
  $bwfile = "$datadir/$id.depthRPM.$str.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.$ss\n";
  print OUT "      parent\tdata$stroot\_ViewDepth\n";
  print OUT "      bigDataUrl\t$bwfile\n";
  print OUT "      shortLabel\t$label_id ($str)\n";
  print OUT "      longLabel\t$label_id 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=$d gt=$gt sex=$sex rep=rep$r str=$str\n\n";
} } } } }

print OUT "\n\n";

close(OUT);
