#!/usr/bin/perl

$list = "hub_sample_list.input.txt";
$tracktxt = "trackDb.input.txt";

# Step 0)  Read in info to use for tags & names of subgroups.
open(IN, "cells.txt"); %tagCell = (); %nameCell = ();
while (<IN>) {
  next if ($_ =~ /\#/);
  chomp $_; ($tag, $name, $id) = split/\t/, $_;
  $tagCell{$id} = $tag; $nameCell{$id} = $name;
}
close(IN);
open(IN, "conditions.txt"); %tagCond = (); %nameCond = ();
while (<IN>) {
  next if ($_ =~ /\#/);
  chomp $_; ($tag, $name, $id) = split/\t/, $_;
  $tagCond{$id}	= $tag;	$nameCond{$id} = $name;
}
close(IN);
open(IN, "chiptypes.txt"); %tagChip = (); %nameChip = ();
while (<IN>) {
  next if ($_ =~ /\#/);
  chomp $_; ($tag, $name, $id) = split/\t/, $_;
  $tagChip{$id}	= $tag;	$nameChip{$id} = $name;
}
close(IN);


# Step 1)  Collect cell, condition, data type, and author identifiers for samples in this subset.
%hashCell = (); %hashCond = (); %hashAuth = (); %hashChip = ();
open(IN, "$list");
while (<IN>) {
  chomp $_; ($x1, $x2, $x3, $x4) = split/\_/, $_;
  $hashCell{$x1} = 1;
  $hashCond{$x2} = 1;
  $hashChip{$x3} = 1;
  $hashAuth{$x4} = 1;
}
close(IN);
@listCell = sort(keys %hashCell);
@listCond = sort(keys %hashCond);
@listAuth = sort(keys %hashAuth);
@listChip = sort(keys %hashChip);
$prio = 300;


# Step 2)  Write track view header.
open(OUT, ">$tracktxt");
print OUT "track\tNguyenTracksInput\n";
print OUT "compositeTrack\ton\n";
print OUT "shortLabel\tInput\n";
print OUT "longLabel\tDepth for Input Associated with ChIP-seq Datasets\n";
print OUT "priority\t$prio\n";
print OUT "visibility\tfull\n";
print OUT "subGroup1\tview Views COV=depth PK=peaks\n";
print OUT "subGroup2\tdatatype ChIPseq_Type";
foreach $type (@listChip) { print OUT " $tagChip{$type}=$nameChip{$type}"; } print OUT "\n";
print OUT "subGroup3\tcell Cell";
foreach $cell (@listCell) { print OUT " $tagCell{$cell}=$nameCell{$cell}"; } print OUT "\n";
print OUT "subGroup4\tcondition Condition";
foreach $cond (@listCond) { print OUT " $tagCond{$cond}=$nameCond{$cond}"; } print OUT "\n";
print OUT "subGroup5\tauthor Author";
foreach $auth (@listAuth) { print OUT " $auth=$auth"; } print OUT "\n";
print OUT "dimensions\tdimX=condition dimY=cell\n";
print OUT "sortOrder\tdatatype=+ cell=+ condition=+ author=+ view=-\n";
print OUT "configurable\ton\n";
print OUT "type\tbed 3\n\n";


# Step 3a)  Write header for depth track grouping.
print OUT "   track\tNguyenTracksInputViewDepth\n";
print OUT "   parent\tNguyenTracksInput\n";
print OUT "   shortLabel\tDepth, Input\n";
print OUT "   longLabel\tDepth, Input\n";
print OUT "   view\tCOV\n";
print OUT "   visibility\thide\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 "   viewLimits\t0:200\n";
print OUT "   showSubtrackColorOnUi\ton\n";
print OUT "   viewUi\ton\n\n";

# Step 3b)  Write depth tracks.
open(IN, "$list");
while (<IN>) {
  chomp $_; ($x1, $x2, $x3, $x4) = split/\_/, $_;
  $fullID = $_; $prio++;
  print OUT "      track\t$fullID.D\n";
  print OUT "      parent\tNguyenTracksInputViewDepth on\n";
  print OUT "      bigDataUrl\tDATA/$fullID.bwt.rmdup.ext200.bigWig\n";
  print OUT "      shortLabel\t$fullID depth\n";
  if ($x4 eq "McCorkleRen") { $txt1 = "McCorkle+Ren,downsampled"; } 
  elsif ($x4 eq "Resnick" && $tagCond{$x2} eq "mult") { $txt1 = "Resnick,downsampled"; }
  else { $txt1 = $x4; }
  print OUT "      longLabel\tread depth: $x3, $nameCell{$x1} cells, condition=$nameCond{$x2} ($txt1)\n";
  print OUT "      type\tbigWig\n";
  print OUT "      color\t0,0,204\n";
  print OUT "      priority\t$prio\n";
  print OUT "      visibility\thide\n";
  print OUT "      subGroups\tdatatype=$tagChip{$x3} cell=$tagCell{$x1} condition=$tagCond{$x2} author=$x4 view=COV\n\n";
}
close(IN);
print OUT "\n\n";

close(OUT);
