%%Download data from https://genome-publications.bioinf.unc.edu/jive/GBM_GE_miRNA.zip

Exp = dlmread('FullExpforInt.txt');
miRNA = dlmread('miRNAforInt.txt');
[d1,n] =size(Exp);
[d2,n] = size(miRNA);
Datatypes = {Exp,miRNA};

%Run JIVE for rank 5 joint structure, rank 33 individual to Exp, and rank
%13 individual to miRNA (see below for rank estimation)
%can take ~2-5 min (less time for a higher convergence threshold)

%run help JIVE
[J,A] = JIVE(Datatypes,5, [33,13],'y',10^(-10),3000);

Exp_A = A(1:d1,:);
miRNA_A = A((d1+1):(d1+d2),:);
Exp_J = J(1:d1,:);
miRNA_J = J((d1+1):(d1+d2),:);

%Here, both datatypes are scaled to have sum of squares 1
%Proportion of variability explained by each component:
norm(Exp_A, 'fro')^2
norm(miRNA_A, 'fro')^2
norm(Exp_J, 'fro')^2
norm(miRNA_J, 'fro')^2


%Estimate ranks for joint and individual structure
%This can take some time (~20 min on 3GB, 2.0 GHz computer)
%For reproducible permutations:
rand('seed',02152012);
randn('seed',02152012);
[J,A,r,rIndiv] = JIVE_RankSelect(Datatypes,0.01,100,'y',10^(-9),3000);

