#!/usr/bin/perl

$list = "hub_sample_list.other.txt";
$tracktxt = "trackDb.other.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 = 200;


# Step 2)  Write track view header.
open(OUT, ">$tracktxt");
print OUT "track\tNguyenTracksOther\n";
print OUT "compositeTrack\ton\n";
print OUT "shortLabel\tOther ChIPs\n";
print OUT "longLabel\tDepth and Peaks for non-p53-ChIPseq 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 dimA=datatype\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\tNguyenTracksOtherViewDepth\n";
print OUT "   parent\tNguyenTracksOther\n";
print OUT "   shortLabel\tDepth, Other Protocols\n";
print OUT "   longLabel\tDepth, Other Protocols\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\tNguyenTracksOtherViewDepth on\n";
  print OUT "      bigDataUrl\tDATA/$fullID.bwt.rmdup.ext200.bigWig\n";
  print OUT "      shortLabel\t$fullID depth\n";
  if ($x3 eq "ATAC") { $txt1 = "ATAC-seq"; } else { $txt1 = "$x3 ChIP-seq"; }
  print OUT "      longLabel\tread depth: $txt1, $nameCell{$x1} cells, condition=$nameCond{$x2} ($x4)\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);


# Step 4a)  Write header for peaks track grouping.
print OUT "   track\tNguyenTracksOtherViewPeaks\n";
print OUT "   parent\tNguyenTracksOther\n";
print OUT "   shortLabel\tPeaks, Other Protocols\n";
print OUT "   longLabel\tPeaks, Other Protocols\n";
print OUT "   view\tPK\n";
print OUT "   visibility\thide\n";
print OUT "   type\tbigBed 3\n";
print OUT "   allButtonPair\ton\n";
print OUT "   centerLabelsDense\ton\n";
print OUT "   dragAndDrop\ton\n";
print OUT "   showSubtrackColorOnUi\ton\n";
print OUT "   viewUi\ton\n\n";

# Step 4b)  Write peaks tracks.
open(IN, "$list");
while (<IN>) {
  chomp $_; ($x1, $x2, $x3, $x4) = split/\_/, $_;
  next if ($x3 eq "RNAPII" || $x3 eq "ATAC"); # no peak calls for these types
  if ($x3 eq "H3" || $x3 eq "H3K4me1" || $x3 eq "H3K4me3" || $x3 eq "H3K27ac" || $x3 eq "H4K16ac") { $pt = "sicer"; } else { $pt = "sissrs"; }
  $fullID = $_; $prio++;
  print OUT "      track\t$fullID.P\n";
  print OUT "      parent\tNguyenTracksOtherViewPeaks on\n";
  if ($pt eq "sicer") { $bbfile = "DATA/$fullID.$pt\-peakcalls.bigBed"; }
  else { $bbfile = "DATA/$fullID.$pt\-peakcalls.min200.bigBed"; }
  print OUT "      bigDataUrl\t$bbfile\n";
  print OUT "      shortLabel\t$fullID peaks\n";
  $txt1 = uc($pt);
  print OUT "      longLabel\t$txt1 peak calls: $x3 ChIP-seq, $nameCell{$x1} cells, condition=$nameCond{$x2} ($x4)\n";
  print OUT "      type\tbigBed 3\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=PK\n\n";
}
close(IN);
print OUT "\n\n";

close(OUT);
