
%%% This code snippet shows how to get subject-weight vectors associated with the 6 ICA modes of netmat edges from Elliott 2018.

addpath ~steve/NETWORKS/FSLNets % get FSLNets
cd /vols/Data/ukbiobank/FMRIB

%%% Load the netmat-edges-weights vectors that you downloaded from our website.
%%% First here is how we computed them for dissemination.
%%% Note that the ICA had already been run - see the primary matlab code DO_meta_6_disseminate.m for that.
% OLD=load('/vols/Data/ukbiobank/FMRIB/ANALYSIS/GWAS_paper/DO_meta_5b_genes8k.mat');
% icaSdb=OLD.icaSdb;
% save('/vols/Data/ukbiobank/FMRIB/ANALYSIS/GWAS_paper/ICA6_weights.mat','icaSdb');
load('ICA6_weights.mat');

%%% Load your main working UKB workspace according to your local file organisation, e.g.:
% ukb=load('/vols/Data/ukbiobank/FMRIB/ANALYSIS/workspace10.mat');                 % Our big UKB workspace at FMRIB.
% NETS=ukb.ALL_IDPs_i_deconf(:,(end+1-size(ukb.NET25,2)-size(ukb.NET100,2)):end);  % You probably organise yours differently.
%%% *OR* if you just want to load the raw subjectsXedges netmat matrices in from file, e.g.:
% NET25  = load('/vols/Data/ukbiobank/FMRIB/IMAGING/data3/group/22k/rfMRI_d25_partialcorr_v1.txt');
% NET100 = load('/vols/Data/ukbiobank/FMRIB/IMAGING/data3/group/22k/rfMRI_d100_partialcorr_v1.txt');
% NETS=[NET25 NET100]; % this assumes there are no subject ID columns in these matrices
%%% Make sure you now apply any relevant deconfounding to NETS

%%% combine the ICA weights on the netmat edges with your subjectsXedges data matrix
NK=~isnan(sum(NETS,2));                                      % Deal with missing data if necessary
NETICA_i_deconf=zeros(size(NETS,1),size(icaSdb,1))/0;        % Create empty matrix of NaNs
NETICA_i_deconf(NK,:)=nets_normalise(NETS(NK,:) * icaSdb');  % Fill in non-missing data with netmats*ica-weights

%%% The final set of ICA subject-weight vectors is NETICA_i_deconf

