#!/usr/bin/perl

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

@A_cat = ("RNA_Fertile_Young", "RNA_Fertile_Old", "RNA_Infertile_Young", "RNA_Infertile_Old");
@A_strand = ("forward", "reverse");

foreach $cat (@A_cat) {
	foreach $str (@A_strand) {

		$val = 128;
		$txt = qq {track $cat.$str
type bigWig
container multiWig
shortLabel $cat.$str
longLabel $cat.$str
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/rna_seq/v_2/EMC_Robaire/S*$str.bw>;
		foreach $file (@A_files) {
			$file =~ s/\/mnt\/www\/webroot\///;

			($sample) = $file =~ /EMC_Robaire\/(.*)\.bw/;
			($number) = $sample =~ /S(.*)\.$str/;

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

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

			$match = 0;
			if ($cat eq "RNA_Fertile_Young" && $fert eq "F" && $age eq "A") { $match = 1; }
			if ($cat eq "RNA_Fertile_Old" && $fert eq "F" && $age eq "C") { $match = 1; }
			if ($cat eq "RNA_Infertile_Young" && $fert eq "IF" && $age eq "A") { $match = 1; }
			if ($cat eq "RNA_Infertile_Old" && $fert eq "IF" && $age eq "C") { $match = 1; }
			next if ($match == 0);

if ($str eq "forward")    { $color = "0,0,$val"; }
elsif ($str eq "reverse") { $color = "$val,0,0"; }
#$color = "0,0,$val";			

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