sub cluster_diff_expressed_transcripts {
my ($diff_expr_matrix_file) = @_;
my $R_script = "$diff_expr_matrix_file.R";
open (my $ofh, ">$R_script") or die "Error, cannot write to $R_script";
print $ofh "library(cluster)\n";
print $ofh "library(gplots)\n";
print $ofh "library(Biobase)\n";
print $ofh "data = read.table(\"$diff_expr_matrix_file\", header=T, com=\'\', sep=\"\\t\")\n
";
print $ofh "rownames(data) = data[,1] # set rownames to gene identifiers\n";
print $ofh "data = data[,2:length(data[1,])] # remove the gene column since its now the rown
ame value\n";
print $ofh "data = as.matrix(data) # convert to matrix\n";
print $ofh "data = log2(data+1)\n";
print $ofh "centered_data = t(scale(t(data), scale=F)) # center rows, mean substracted\n";
print $ofh "hc_genes = agnes(centered_data, diss=FALSE, metric=\"euclidean\") # cluster gene
s\n";
print $ofh "hc_samples = hclust(as.dist(1-cor(centered_data, method=\"spearman\")), method=\
"complete\") # cluster conditions\n";
print $ofh "myheatcol = redgreen(75)\n";
print $ofh "gene_partition_assignments <- cutree(as.hclust(hc_genes), k=6);\n";
print $ofh "partition_colors = rainbow(length(unique(gene_partition_assignments)), start=0.4
, end=0.95)\n";
print $ofh "gene_colors = partition_colors[gene_partition_assignments]\n";
print $ofh "save(list=ls(all=TRUE), file=\"${R_script}.all.RData\")\n";
print $ofh "postscript(file=\"$diff_expr_matrix_file.heatmap.eps\", horizontal=FALSE, width=
8, height=18, paper=\"special\");\n";
print $ofh "heatmap.2(centered_data, dendrogram='both', Rowv=as.dendrogram(hc_genes), Colv=a
s.dendrogram(hc_samples), col=myheatcol, RowSideColors=gene_colors, scale=\"none\", density.info
=\"none\", trace=\"none\", key=TRUE, keysize=1.2, cexCol=2.5, margins=c(15,15), lhei=c(0.3,2), l
wid=c(2.5,4))\n";
print $ofh "dev.off()\n";
close $ofh;
&process_cmd("R --vanilla -q < $R_script");
return;