#!/usr/bin/perl

$datadir = "data/RNAseq_circ-Feb2021";
$trackname = "trackDb.RNA-circ-Feb2021.txt";
$stroot = "RNA05";
$shortLab = "MD_Con-Feb2021";

@diets = ("MD","Con");
@times = ("ZT1","ZT5","ZT9","ZT13","ZT17","ZT21");
@reps = (1,2,3,4);
@strands = ("plus","minus");

$prio = 500;

$dkBlue = "0,0,204";   # plus strand
$ltBlue = "0,128,255"; # minus strand
%useColor = ("plus" => $dkBlue, "minus" => $ltBlue);
%useS = ("plus" => "P", "minus" => "M");


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 RNAseq\n";
print OUT "longLabel\t$shortLab RNA-seq data (normalized depth)\n";
print OUT "priority\t$prio\n";
print OUT "subGroup1\tview View NORM=normDepth\n";
print OUT "subGroup2\tDiet Diet"; foreach $diet (@diets) { print OUT " $diet=$diet"; } print OUT "\n";
print OUT "subGroup3\tZeitgeber Zeitgeber"; foreach $time (@times) { print OUT " $time=$time"; } print OUT "\n";
print OUT "subGroup4\tRep Rep"; foreach $r (@reps) { print OUT " $r=$r"; } print OUT "\n";
print OUT "subGroup5\tStrand Strand"; foreach $s (@strands) { print OUT " $s=$s"; } print OUT "\n";
print OUT "dimensions\tdimX=Diet dimY=Zeitgeber dimA=Strand\n";
print OUT "sortOrder\tDiet=+ Zeitgeber=+ Rep=+ Strand=+\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 RNAseq\n";
print OUT "   longLabel\t$shortLab RNA-seq data (normalized depth)\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:30:11\n";
print OUT "   viewLimits\t0:100\n";
print OUT "   showSubtrackColorOnUi\ton\n";
print OUT "   viewUi\ton\n\n";

# write depth tracks
foreach $diet (@diets) { foreach $time (@times) { foreach $r (@reps) { foreach $str (@strands) {
  $id = "$diet-$time-$r";
  $bwfile = "$datadir/$id.depthNorm.$str.bw";
  unless (-e $bwfile) { print "WARNING: Did not find $bwfile.\n"; next; }
  $prio++;
  print OUT "      track\t$id.$stroot$useS{$str}\n";
  print OUT "      parent\tdata$stroot\_ViewDepth\n";
  print OUT "      bigDataUrl\t$bwfile\n";
  print OUT "      shortLabel\t$id\n";
  print OUT "      longLabel\tnormalized RNA-seq depth: $id ($str)\n";
  print OUT "      type\tbigWig\n";
  print OUT "      color\t$useColor{$str}\n";
  print OUT "      priority\t$prio\n";
  print OUT "      visibility\thide\n";
  print OUT "      subGroups\tDiet=$diet Zeitgeber=$time Rep=$r Strand=$str view=NORM\n\n";
} } } }
print OUT "\n\n";


close(OUT);
