%% alternative to D,F

g = transGenes;
m = medianEffect(intint(goodGenes), :);
m = abs(m);
% figure; plotSpread({m(g, 2), m(:, 2), m(g, 3), m(:, 3)});
% set(gca, 'xtick', 1:4, 'xticklabels', {'cis level, trans reg', 'cis level, all', 'trans level, trans reg', 'trans level, all'});
% xtickangle(45);

[p, h, stats] = ranksum(m(g, 3), m(:, 3))
randGenes = datasample(1:length(goodGenes), length(g));
[p, h, stats] = ranksum(m(g, 3), m(randGenes, 3))
% figure; plotSpread({m(g, 2), m(randGenes, 2), m(g, 3), m(randGenes, 3)});
% set(gca, 'xtick', 1:4, 'xticklabels', {'cis level, trans reg', 'cis level, all', 'trans level, trans reg', 'trans level, all'});
% xtickangle(45);
%%

g = intint(goodGenes(transGenes));
[~, sortdidx] = sort(abs(medianEffect(g, 3)));
g = g(sortdidx);
d = comparable_samples_de.trans(g, :);
d = abs(d);
figure; subplot(1,2,1); 
imagesc(abs(medianEffect(g, :)));
subplot(1,2,2);
imagesc(d)
%%
g = intint(goodGenes(transGenes));
d = comparable_samples_de.trans(g, :);
d = abs(d);
pd = pdist(d);
l=linkage(pd);
sortdidx = optimalleaforder(l, pd);
figure; imagesc(d(sortdidx, :))


%%
g = intint(goodGenes(transGenes));
cond_trans_g = sum(genes_cond_trans(g, :), 2);
% randGenes = datasample(intint(goodGenes), length(g));
randGenes = intint(goodGenes);
cond_trans_rand = sum(genes_cond_trans(randGenes, :), 2);

g = intint(goodGenes(cisGenes));
cond_cis_g = sum(genes_cond_cis(g, :), 2);
% randGenes = datasample(intint(goodGenes), length(g));
cond_cis_rand = sum(genes_cond_cis(randGenes, :), 2);

figure; 
subplot(1,2,1);
histogram(cond_trans_rand); hold on;
histogram(cond_trans_g);
[h, p, ci, stats] = ttest2(cond_trans_g, cond_trans_rand);
disp(['trans: t-stat = ', num2str(stats.tstat), ', p-value = ', num2str(p)]);
subplot(1,2,2);
histogram(cond_cis_rand); hold on;
histogram(cond_cis_g);
[h, p, ci, stats] = ttest2(cond_cis_g, cond_cis_rand);
disp(['cis: t-stat = ', num2str(stats.tstat), ', p-value = ', num2str(p)]);