#!/usr/bin/perl

open (FH, "<pheno_sperm.txt");
while (<FH>) {
	$line = $_; chomp($line);
	($samp, $fert, $age) = split(/\s+/, $line);
	$H_fert{$samp} = $fert;
	$H_age{$samp}  = $age;
}

@A_cat = ("Fertile_Young", "Fertile_Old", "Infertile_Young", "Infertile_Old");

foreach $cat (@A_cat) {

	$val = 0;
	$txt = qq {track $cat
type bigWig
container multiWig
shortLabel $cat
longLabel $cat
visibility full
aggregate transparentOverlay
autoScale on
showSubtrackColorOnUi on
maxHeightPixels 100:48:8
priority 1
};
	print $txt;

	my @A_files;
	@A_files = </mnt/www/webroot/hg19/wgb_seq/EMC_Robaire/*CG*.bigwig>;
	foreach $file (@A_files) {
		$file =~ s/\/mnt\/www\/webroot\///;

		($sample) = $file =~ /EMC_Robaire\/(.*)\.bigwig/;
		($number) = $sample =~ /(.*)\_2-.*/;
#print "[$sample]\n";

		$fert = $H_fert{$number};
		$age  = $H_age{$number};

#print "[$number]\t[$fert]\t[$age]\n";

		$match = 0;
		if ($cat eq "Fertile_Young" && $fert eq "F" && $age eq "A") { $match = 1; }
		if ($cat eq "Fertile_Old" && $fert eq "F" && $age eq "C") { $match = 1; }
		if ($cat eq "Infertile_Young" && $fert eq "IF" && $age eq "A") { $match = 1; }
                if ($cat eq "Infertile_Old" && $fert eq "IF" && $age eq "C") { $match = 1; }
		next if ($match == 0);

$color = "0,0,$val";			

		$txt = qq {
track $sample
bigDataUrl http://emc.genome.mcgill.ca/hg19/wgb_seq/EMC_Robaire/$sample.bigwig
shortLabel $sample
longLabel $sample
parent $cat
type bigWig
autoScale on
color $color
};
		print "$txt\n";
		$val+=9;
	}

}
