#!/usr/bin/perl

$datadir = "DATA/mCherry";
$trackname = "trackDb.mCherry_G3KO_kidney.txt";
$stroot = "mCh";  # for (hidden) supertrack/track identifiers
$prefix = "mCh";  # to show in sample labels
$shortLab = "mCherry";

@grps = ("P7-KO","P7-WT","P14-KO","P14-WT","P28-KO","P28-WT");
@ages = ("P7","P14","P28");
@gts = ("KO","WT");
@reps = (1,2,3,4,5);
@strands = ("plus","minus");

%outliers = ("P14-KO-4" => 1);

$prio = 200;

$useColor = "0,0,0";
#$useColor = "0,0,204";


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 COV=depth\n";
print OUT "subGroup2\tGenotype Genotype"; foreach $gt (@gts) { print OUT " $gt=$gt"; } print OUT "\n";
print OUT "subGroup3\tAge Age"; foreach $age (@ages) { print OUT " $age=$age"; } print OUT "\n";
print OUT "subGroup4\tRep Rep"; foreach $r (@reps) { print OUT " $r=$r"; } print OUT "\n";
print OUT "subGroup5\tStrand Strand"; foreach $str (@strands) { print OUT " $str=$str"; } print OUT "\n";
print OUT "dimensions\tdimX=Genotype dimY=Age dimA=Strand\n";
print OUT "sortOrder\tGenotype=+ Age=+ 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:10\n";
print OUT "   showSubtrackColorOnUi\ton\n";
print OUT "   viewUi\ton\n\n";

# write depth tracks
foreach $grp (@grps) { foreach $r (@reps) {
  $id = "$grp\-$r";
  if ($outliers{$id} == 1) { print "WARNING: Skipping $id as user-defined outlier.\n"; next; }
  foreach $str (@strands) {
    $idlab = "$prefix-$id";
    $ss = substr($str, 0, 1);
    ($age, $gt) = split/\-/, $grp;
    $bwfile = "$datadir/$prefix\-$grp\-rep$r.depthRPM.$str.bw";
    unless (-e $bwfile) { print "WARNING: Did not find $bwfile.\n"; next; }
    $prio++;
    print OUT "      track\t$id.$stroot.$ss\n";
    print OUT "      parent\tdata$stroot\_ViewDepth\n";
    print OUT "      bigDataUrl\t$bwfile\n";
    print OUT "      shortLabel\t$idlab RNAseq ($str)\n";
    print OUT "      longLabel\tnormalized RNA-seq depth: $idlab kidney ($str)\n";
    print OUT "      type\tbigWig\n";
    print OUT "      color\t$useColor\n";
    print OUT "      priority\t$prio\n";
    print OUT "      visibility\thide\n";
    print OUT "      subGroups\tGenotype=$gt Age=$age Rep=$r Strand=$str view=COV\n\n";
  } 
} }
print OUT "\n\n";

close(OUT);
