#!/usr/bin/perl

$priMain = 400;

open(IN, "info/hub_tracks_key_28may2014.txt");
open(OUT, ">trackDb.txt");
while (<IN>) {
  next if ($_ =~ /^\s/);
  if ($_ =~ /^composite/) {
    chomp $_; @ar = split/\=/, $_; $compName = $ar[1]; $compLabel = $ar[2]; $rgb = $ar[3];
    unless ($priMain == 400) { print OUT "\n\n"; }
    $priMain += 10; $priMainOut = "$priMain.0";
    $priSpec = 0;
    if ($compLabel =~ /depth/) {
      $currentCompType = "depth";
      print OUT "track             $compName\n";
      print OUT "compositeTrack    on\n";
      print OUT "shortLabel        $compLabel\n";
      print OUT "longLabel         $compLabel\n";
      print OUT "type              bigWig\n";
      print OUT "allButtonPair     on\n";
      print OUT "centerLabelsDense on\n";
      print OUT "dragAndDrop       on\n";
      print OUT "visibility        hide\n";
      print OUT "alwaysZero        on\n";
      print OUT "maxHeightPixels   30\n";
      print OUT "viewLimits        0:40\n";
      print OUT "viewLimitsMax     0:200\n";
      print OUT "priority          $priMainOut\n";
      print OUT "showSubtrackColorOnUi on\n\n";
    }
    elsif ($compLabel =~ /peaks/) {
      $currentCompType = "peaks";
      print OUT "track             $compName\n";
      print OUT "compositeTrack    on\n";
      print OUT "shortLabel        $compLabel\n";
      print OUT "longLabel         $compLabel\n";
      print OUT "type              bigBed\n";
      print OUT "allButtonPair     on\n";
      print OUT "centerLabelsDense on\n";
      print OUT "dragAndDrop       on\n";
      print OUT "visibility        hide\n";
      print OUT "priority          $priMainOut\n";
      print OUT "showSubtrackColorOnUi on\n\n";
    }
    else { print "ERROR: unknown composite subgroup (depth or peaks?)...\n$_\n"; exit; }
    next;
  }
  chomp $_; ($id, $path) = split/\t/, $_;
  $priSpec++; $priOut = sprintf("%.2f", ($priMain + $priSpec/100));
  if ($id eq "MenendezU2OS_ds20m.input") { $labeltxt = "U2OS combined input (20m)"; }
  elsif ($id eq "MCF7_IgG.input") { $labeltxt = "MCF7 IgG"; }
  else {
    ($celltype, $treatment, $chip) = split/\_|\./, $id; $repN = "";
    if ($celltype =~ /both/) { $celltype =~ s/\-both//; }
    if ($celltype =~ /rep/) { @arID = split/\-/, $celltype; $repN = $arID[1]; $repN =~ s/rep//; $celltype = $arID[0]; }
    if ($chip eq "") { # this means treatment was not defined
      $labeltxt = "$celltype $treatment";
    }
    else { $labeltxt = "$celltype $treatment $chip"; }
    if ($repN ne "") { $labeltxt .= " [$repN]"; }
  }
  if ($currentCompType eq "depth") {
    print OUT "  track           $id\_COV\n";
    print OUT "  parent          $compName on\n";
    print OUT "  shortLabel      $labeltxt, depth\n";
    print OUT "  longLabel       $labeltxt, depth\n";
    print OUT "  bigDataUrl      $id.depth.bigWig\n";
    print OUT "  color           $rgb\n";
    print OUT "  priority        $priOut\n";
    print OUT "  type            bigWig\n";
    print OUT "  visibility      full\n\n";
  }
  elsif ($currentCompType eq "peaks") {
    if ($path =~ /sicer/) { $peaktype = "SICER"; } else { $peaktype = "SISSRs"; }
    print OUT "  track           $id\_PEAK\n";
    print OUT "  parent          $compName on\n";
    print OUT "  shortLabel      $labeltxt, $peaktype peaks\n";
    print OUT "  longLabel       $labeltxt, $peaktype peaks\n";
    print OUT "  bigDataUrl      $id.peaks.bigBed\n";
    print OUT "  color           $rgb\n";
    print OUT "  priority        $priOut\n";
    print OUT "  type            bigBed\n";
    print OUT "  visibility      dense\n\n";
  }
  else { print "ERROR: unexpected current composite type for this entry...$_\n"; exit; }
}
close(OUT); close(IN);

