%The present code relates to the analyses on Modifiable Risk Factor described in Manuello et al.,
%2024, Nature Communications

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%% FIRST STAGE %%%%%%%%%%%%%%%%%%
%The first stage separately avaluates the association between the 161
%selected Modifiable Risk Factors (MRFs) and the LIFO network

ID_ws11=UKB.subject_IDs_unique; %This makes the list of available subjects at the moment of the analyses

%The following lines are to manage and minimally pre-process the MRFs
%variables. Each section pertains to 1 of the 15 considered categories of MRFs

%%%%%%  ALCOHOL CONSUMPTION %%%%%%%%%%%%%%%%

%Frequency of drinking alcohol (20414) 
DF20414_headers_all= strfind(UKB.varsHeader,'Frequency of drinking alcohol');
Is_DF20414 = find(not(cellfun('isempty',DF20414_headers_all)));
DF20414_headers=UKB.varsHeader(Is_DF20414);
DF20414_keep_col_1=[1];
DF20414_keep_col_2=Is_DF20414(:,DF20414_keep_col_1);
Alc_DF20414=UKB.vars(:,DF20414_keep_col_2);
%Change prefere not to answer as nan
Alc_DF20414(Alc_DF20414==-818)=nan;

%Alcohol intake frequency (1558) 0=never 5=daily already in funpack
DF1558_headers_all= strfind(UKB.varsHeader,'Alcohol intake');
Is_DF1558 = find(not(cellfun('isempty',DF1558_headers_all)));
DF1558_headers=UKB.varsHeader(Is_DF1558);
DF1558_keep_col_1=[1 2 3];
DF1558_keep_col_2=Is_DF1558(:,DF1558_keep_col_1);
Alc_DF1558=UKB.vars(:,DF1558_keep_col_2); 
%Remove instance 1
Alc_DF1558(:,2)=[];

%Amount of alcohol/day (20403) 
DF20403_headers_all= strfind(UKB.varsHeader,'Amount of alcohol');
Is_DF20403 = find(not(cellfun('isempty',DF20403_headers_all)));
DF20403_headers=UKB.varsHeader(Is_DF20403);
DF20403_keep_col_1=[1];
DF20403_keep_col_2=Is_DF20403(:,DF20403_keep_col_1);
Alc_DF20403=UKB.vars(:,DF20403_keep_col_2); 
%Change prefere not to answer as nan
Alc_DF20403(Alc_DF20403==-818)=nan;
%Child value replacement
Alc_DF20403(Alc_DF20414==0)=0;

%Frequency 6 or more unit (20416) 
DF20416_headers_all= strfind(UKB.varsHeader,'six or more');
Is_DF20416 = find(not(cellfun('isempty',DF20416_headers_all)));
DF20416_headers=UKB.varsHeader(Is_DF20416);
DF20416_keep_col_1=[1];
DF20416_keep_col_2=Is_DF20416(:,DF20416_keep_col_1);
Alc_DF20416=UKB.vars(:,DF20416_keep_col_2);
%Change prefere not to answer as nan
Alc_DF20416(Alc_DF20416==-818)=nan;
Alc_DF20416=(Alc_DF20416)-1;
%Child value replacement
Alc_DF20416(Alc_DF20414==0)=0;

%%%%%%  BODY SIZE MEASUREMENTS %%%%%%%%%%%%%%%%
%BMI (21001)
DF21001_headers_all= strfind(UKB.varsHeader,'BMI');
Is_DF21001 = find(not(cellfun('isempty',DF21001_headers_all)));
DF21001_headers=UKB.varsHeader(Is_DF21001);
DF21001_keep_col_1=[1 2 3];
DF21001_keep_col_2=Is_DF21001(:,DF21001_keep_col_1);
BMI_DF21001=UKB.vars(:,DF21001_keep_col_2);
%Remove instance 1
BMI_DF21001(:,2)=[];

%weight (21002)
DF21002_headers_all= strfind(UKB.varsHeader,'Weight');
Is_DF21002 = find(not(cellfun('isempty',DF21002_headers_all)));
DF21002_headers=UKB.varsHeader(Is_DF21002);
DF21002_keep_col_1=[10 11 12];
DF21002_keep_col_2=Is_DF21002(:,DF21002_keep_col_1);
BMI_DF21002=UKB.vars(:,DF21002_keep_col_2); 
%Remove instance1
BMI_DF21002(:,2)=[];

%waist circumference (48)
DF48_headers_all= strfind(UKB.varsHeader,'Waist');
Is_DF48 = find(not(cellfun('isempty',DF48_headers_all)));
DF48_headers=UKB.varsHeader(Is_DF48);
DF48_keep_col_1=[1 2 3];
DF48_keep_col_2=Is_DF48(:,DF48_keep_col_1);
BMI_DF48=UKB.vars(:,DF48_keep_col_2); 
%Remove instance1
BMI_DF48(:,2)=[];

%hip circumference (49)
DF49_headers_all= strfind(UKB.varsHeader,'Hip');
Is_DF49 = find(not(cellfun('isempty',DF49_headers_all)));
DF49_headers=UKB.varsHeader(Is_DF49);
DF49_keep_col_1=[1 2 3];
DF49_keep_col_2=Is_DF49(:,DF49_keep_col_1);
BMI_DF49=UKB.vars(:,DF49_keep_col_2); 
%Remove instance1
BMI_DF49(:,2)=[];

%%%%%%  BLOOD PRESSURE %%%%%%%%%%%%%%%%

%Age high bp diagnosed (2966) -1 -3 already recoded to nan 
DF2966_headers_all= strfind(UKB.varsHeader,'Age high');
Is_DF2966 = find(not(cellfun('isempty',DF2966_headers_all)));
DF2966_headers=UKB.varsHeader(Is_DF2966);
DF2966_keep_col_1=[1 2 3];
DF2966_keep_col_2=Is_DF2966(:,DF2966_keep_col_1);
BP_DF2966=UKB.vars(:,DF2966_keep_col_2); 
%Remove instance1
BP_DF2966(:,2)=[];

%Pulse rate (95) (ins0_read1, ins0_read2, ins2_read1, ins2_read2)
DF95_headers_all= strfind(UKB.varsHeader,'during blood');
Is_DF95 = find(not(cellfun('isempty',DF95_headers_all)));
DF95_headers=UKB.varsHeader(Is_DF95);
DF95_keep_col_1=[1 2 3 4];
DF95_keep_col_2=Is_DF95(:,DF95_keep_col_1);
BP_DF95=UKB.vars(:,DF95_keep_col_2); 

%Diastolic BP (4079) (2 reads for each instance)
DF4079_headers_all= strfind(UKB.varsHeader,'Diastolic');
Is_DF4079 = find(not(cellfun('isempty',DF4079_headers_all)));
DF4079_headers=UKB.varsHeader(Is_DF4079);
DF4079_keep_col_1=[5:10];
DF4079_keep_col_2=Is_DF4079(:,DF4079_keep_col_1);
BP_DF4079=UKB.vars(:,DF4079_keep_col_2); 
%Remove instance1
BP_DF4079(:,(3:4))=[];

%Systolic BP (4080) (2 reads for each instance)
DF4080_headers_all= strfind(UKB.varsHeader,'Systolic');
Is_DF4080 = find(not(cellfun('isempty',DF4080_headers_all)));
DF4080_headers=UKB.varsHeader(Is_DF4080);
DF4080_keep_col_1=[5:10];
DF4080_keep_col_2=Is_DF4080(:,DF4080_keep_col_1);
BP_DF4080=UKB.vars(:,DF4080_keep_col_2); 
%Remove instance1
BP_DF4080(:,(3:4))=[];

%Medication for BP (6177) -1 -3 already recoded to nan
DF6177_headers_all= strfind(UKB.varsHeader,'or diabetes');
Is_DF6177 = find(not(cellfun('isempty',DF6177_headers_all)));
DF6177_headers=UKB.varsHeader(Is_DF6177);
DF6177_keep_col_1=[1:5];
DF6177_keep_col_2=Is_DF6177(:,DF6177_keep_col_1);
BP_DF6177_orig=UKB.vars(:,DF6177_keep_col_2); 
%Binarize making 2=1 and others =0. Keep nans
BP_DF6177_bin=nan(size(BP_DF6177_orig,1),size(BP_DF6177_orig,2));
BP_DF6177_bin(BP_DF6177_orig==2)=1;
BP_DF6177_bin(BP_DF6177_orig==1)=0;
BP_DF6177_bin(BP_DF6177_orig==3)=0;
%Here none of the above is recoded to 0
BP_DF6177_bin(BP_DF6177_orig==-7)=0;
%Group into instances
DF6177_0=BP_DF6177_bin(:,(1:2));
DF6177_1=BP_DF6177_bin(:,(3));
DF6177_2=BP_DF6177_bin(:,(4:5));
BP_DF6177_M=[max(DF6177_0,[],2) max(DF6177_1,[],2) max(DF6177_2,[],2) ];
%Remove instance1
BP_DF6177_M(:,2)=[];

%Now take 6153 that is the same for females
DF6153_headers_all= strfind(UKB.varsHeader,'or take exogenous');
Is_DF6153 = find(not(cellfun('isempty',DF6153_headers_all)));
DF6153_headers=UKB.varsHeader(Is_DF6153);
DF6153_keep_col_1=[1:8];
DF6153_keep_col_2=Is_DF6153(:,DF6153_keep_col_1);
BP_DF6153_orig=UKB.vars(:,DF6153_keep_col_2); 
%Binarize making 3=1 and others =0. Keep nans
BP_DF6153_bin=nan(size(BP_DF6153_orig,1),size(BP_DF6153_orig,2));
BP_DF6153_bin(BP_DF6153_orig==2)=1;
BP_DF6153_bin(BP_DF6153_orig==1)=0;
BP_DF6153_bin(BP_DF6153_orig==3)=0;
BP_DF6153_bin(BP_DF6153_orig>3)=NaN; %this is hormone and pill
%Here none of the above is recoded to 0
BP_DF6153_bin(BP_DF6153_orig==-7)=0;
%Group into instances
DF6153_0=BP_DF6153_bin(:,(1:3));
DF6153_1=BP_DF6153_bin(:,(4:5));
DF6153_2=BP_DF6153_bin(:,(6:8));
BP_DF6153_F=[max(DF6153_0,[],2) max(DF6153_1,[],2) max(DF6153_2,[],2) ];
%Remove instance1
BP_DF6153_F(:,2)=[];

%Now combine M and F in one variable
BP_MF_in0=max([BP_DF6177_M(:,1) BP_DF6153_F(:,1)],[],2);
BP_MF_in2=max([BP_DF6177_M(:,2) BP_DF6153_F(:,2)],[],2);
BP_MF=[BP_MF_in0 BP_MF_in2];

%Self reported (20002)
DF20002_headers_all= strfind(UKB.varsHeader,'hypertension)');
Is_DF20002 = find(not(cellfun('isempty',DF20002_headers_all)));
DF20002_headers=UKB.varsHeader(Is_DF20002);
DF20002_keep_col_1=[1];
DF20002_keep_col_2=Is_DF20002(:,DF20002_keep_col_1);
BP_DF20002=UKB.vars(:,DF20002_keep_col_2);

%%%%%%  CHOLESTEROL %%%%%%%%%%%%%%%%
% %LDL direct (30780) no instance 2
DF30780_headers_all= strfind(UKB.varsHeader,'LDL');
Is_DF30780 = find(not(cellfun('isempty',DF30780_headers_all)));
DF30780_headers=UKB.varsHeader(Is_DF30780);
DF30780_keep_col_1=[1 2];
DF30780_keep_col_2=Is_DF30780(:,DF30780_keep_col_1);
Ch_DF30780=UKB.vars(:,DF30780_keep_col_2); 
%Remove instance 1
Ch_DF30780(:,2)=[];

% %Cholesterol (30690) no instance 2
DF30690_headers_all= strfind(UKB.varsHeader,'Cholesterol');
Is_DF30690 = find(not(cellfun('isempty',DF30690_headers_all)));
DF30690_headers=UKB.varsHeader(Is_DF30690);
DF30690_keep_col_1=[1 2];
DF30690_keep_col_2=Is_DF30690(:,DF30690_keep_col_1);
Ch_DF30690=UKB.vars(:,DF30690_keep_col_2); 
%Remove instance 1
Ch_DF30690(:,2)=[];

% %HDL cholesterol (30760) no instance 2 
DF30760_headers_all= strfind(UKB.varsHeader,'HDL');
Is_DF30760 = find(not(cellfun('isempty',DF30760_headers_all)));
DF30760_headers=UKB.varsHeader(Is_DF30760);
DF30760_keep_col_1=[1 2];
DF30760_keep_col_2=Is_DF30760(:,DF30760_keep_col_1);
Ch_DF30760=UKB.vars(:,DF30760_keep_col_2); 
%Remove instance 1
Ch_DF30760(:,2)=[];

%Med for cholesterol (6177) -1 -3 already recoded to nan
DF6177_headers_all= strfind(UKB.varsHeader,'or diabetes');
Is_DF6177 = find(not(cellfun('isempty',DF6177_headers_all)));
DF6177_headers=UKB.varsHeader(Is_DF6177);
DF6177_keep_col_1=[1:5];
DF6177_keep_col_2=Is_DF6177(:,DF6177_keep_col_1);
Ch_DF6177_orig=UKB.vars(:,DF6177_keep_col_2); 
%Binarize making 1=1 and others =0. Keep nans
Ch_DF6177_bin=nan(size(Ch_DF6177_orig,1),size(Ch_DF6177_orig,2));
Ch_DF6177_bin(Ch_DF6177_orig==2)=0;
Ch_DF6177_bin(Ch_DF6177_orig==1)=1;
Ch_DF6177_bin(Ch_DF6177_orig==3)=0;
%Here none of the above is recoded to 0
Ch_DF6177_bin(Ch_DF6177_orig==-7)=0;

%Group into instances
DF6177_0=Ch_DF6177_bin(:,(1:2));
DF6177_1=Ch_DF6177_bin(:,(3));
DF6177_2=Ch_DF6177_bin(:,(4:5));
Ch_DF6177_M=[max(DF6177_0,[],2) max(DF6177_1,[],2) max(DF6177_2,[],2) ];
%Remove instance1
Ch_DF6177_M(:,2)=[];

%Now take 6153 that is the same for females
DF6153_headers_all= strfind(UKB.varsHeader,'or take exogenous');
Is_DF6153 = find(not(cellfun('isempty',DF6153_headers_all)));
DF6153_headers=UKB.varsHeader(Is_DF6153);
DF6153_keep_col_1=[1:8];
DF6153_keep_col_2=Is_DF6153(:,DF6153_keep_col_1);
Ch_DF6153_orig=UKB.vars(:,DF6153_keep_col_2); 
%Binarize making 3=1 and others =0. Keep nans
Ch_DF6153_bin=nan(size(Ch_DF6153_orig,1),size(Ch_DF6153_orig,2));
Ch_DF6153_bin(Ch_DF6153_orig==2)=0;
Ch_DF6153_bin(Ch_DF6153_orig==1)=1;
Ch_DF6153_bin(Ch_DF6153_orig==3)=0;
Ch_DF6153_bin(Ch_DF6153_orig>3)=NaN; %this is hormone and pill
%Here none of the above is recoded to 0
Ch_DF6153_bin(Ch_DF6153_orig==-7)=0;
%Group into instances
DF6153_0=Ch_DF6153_bin(:,(1:3));
DF6153_1=Ch_DF6153_bin(:,(4:5));
DF6153_2=Ch_DF6153_bin(:,(6:8));
Ch_DF6153_F=[max(DF6153_0,[],2) max(DF6153_1,[],2) max(DF6153_2,[],2) ];
%Remove instance1
Ch_DF6153_F(:,2)=[];

%Now combine M and F in one variable
Ch_MF_in0=max([Ch_DF6177_M(:,1) Ch_DF6153_F(:,1)],[],2);
Ch_MF_in2=max([Ch_DF6177_M(:,2) Ch_DF6153_F(:,2)],[],2);
Ch_MF=[Ch_MF_in0 Ch_MF_in2];

%Self reported (20002)
DF20002_headers_all= strfind(UKB.varsHeader,'cholesterol)');
Is_DF20002 = find(not(cellfun('isempty',DF20002_headers_all)));
DF20002_headers=UKB.varsHeader(Is_DF20002);
DF20002_keep_col_1=[1];
DF20002_keep_col_2=Is_DF20002(:,DF20002_keep_col_1);
Ch_DF20002=UKB.vars(:,DF20002_keep_col_2); 

%%%%%%  DEPRESSION  %%%%%%%%%%%%%%%%

%prolonged feelings of sadness (20446) no instances
DF20446_headers_all= strfind(UKB.varsHeader,'prolonged feelings');
Is_DF20446 = find(not(cellfun('isempty',DF20446_headers_all)));
DF20446_headers=UKB.varsHeader(Is_DF20446);
DF20446_keep_col_1=[1];
DF20446_keep_col_2=Is_DF20446(:,DF20446_keep_col_1);
Dep_DF20446=UKB.vars(:,DF20446_keep_col_2);
%Change prefere not to answer as nan
Dep_DF20446(Dep_DF20446==-818)=nan;

%prolonged loss of interest (20441) no instances
DF20441_headers_all= strfind(UKB.varsHeader,'loss of interest');
Is_DF20441 = find(not(cellfun('isempty',DF20441_headers_all)));
DF20441_headers=UKB.varsHeader(Is_DF20441);
DF20441_keep_col_1=[1];
DF20441_keep_col_2=Is_DF20441(:,DF20441_keep_col_1);
Dep_DF20441=UKB.vars(:,DF20441_keep_col_2); 
%Change prefere not to answer as nan
Dep_DF20441(Dep_DF20441==-818)=nan;

%Substances taken (20546) no instances
DF20546_headers_all= strfind(UKB.varsHeader,'Substances taken');
Is_DF20546 = find(not(cellfun('isempty',DF20546_headers_all)));
DF20546_headers=UKB.varsHeader(Is_DF20546);
DF20546_keep_col_1=[1 2];
DF20546_keep_col_2=Is_DF20546(:,DF20546_keep_col_1);
Dep_DF20546_orig=UKB.vars(:,DF20546_keep_col_2); 
%Make a binary
Dep_DF20546_bin=Dep_DF20546_orig;
Dep_DF20546_bin(Dep_DF20546_orig>0)=1;
Dep_DF20546_bin(Dep_DF20546_orig<0)=nan;
%Replace child values
Dep_DF20546_bin(Dep_DF20446==0)=0;
Dep_DF20546_bin(Dep_DF20441==0)=0;
%Make one column only
Dep_DF20546=max(Dep_DF20546_bin,[],2);


%Recent feelings of depression (20510)
DF20510_headers_all= strfind(UKB.varsHeader,'feelings');
Is_DF20510 = find(not(cellfun('isempty',DF20510_headers_all)));
DF20510_headers=UKB.varsHeader(Is_DF20510);
DF20510_keep_col_1=[24];
DF20510_keep_col_2=Is_DF20510(:,DF20510_keep_col_1);
Dep_DF20510_orig=UKB.vars(:,DF20510_keep_col_2); 
%Recode -818 to nan
Dep_DF20510=nan(size(Dep_DF20510_orig));
%Binarise not at all=0 vs other=1
Dep_DF20510(Dep_DF20510_orig==1)=0;
Dep_DF20510(Dep_DF20510_orig>1)=1;

%n of depressed periods (20442)
DF20442_headers_all= strfind(UKB.varsHeader,'Lifetime');
Is_DF20442 = find(not(cellfun('isempty',DF20442_headers_all)));
DF20442_headers=UKB.varsHeader(Is_DF20442);
DF20442_keep_col_1=[8];
DF20442_keep_col_2=Is_DF20442(:,DF20442_keep_col_1);
Dep_DF20442=UKB.vars(:,DF20442_keep_col_2);
%Change prefere not to answer into nan
Dep_DF20442(Dep_DF20442==-818)=nan;
%Change too many (-999) into nan
Dep_DF20442(Dep_DF20442==-999)=nan;
%Replace child values
Dep_DF20442(Dep_DF20446==0)=0;
Dep_DF20442(Dep_DF20441==0)=0;

%age at first episode (20433)
DF20433_headers_all= strfind(UKB.varsHeader,'Age at first');
Is_DF20433 = find(not(cellfun('isempty',DF20433_headers_all)));
DF20433_headers=UKB.varsHeader(Is_DF20433);
DF20433_keep_col_1=[2];
DF20433_keep_col_2=Is_DF20433(:,DF20433_keep_col_1);
Dep_DF20433=UKB.vars(:,DF20433_keep_col_2);
%Change prefere not to answer and do not know as nan
Dep_DF20433(Dep_DF20433<0)=nan;

%age at last episode (20434)
DF20434_headers_all= strfind(UKB.varsHeader,'Age at last');
Is_DF20434 = find(not(cellfun('isempty',DF20434_headers_all)));
DF20434_headers=UKB.varsHeader(Is_DF20434);
DF20434_keep_col_1=[5];
DF20434_keep_col_2=Is_DF20434(:,DF20434_keep_col_1);
Dep_DF20434=UKB.vars(:,DF20434_keep_col_2);
%Change prefere not to answer and do not know as nan
Dep_DF20434(Dep_DF20434<0)=nan;


%%%%%%  DIABETES  %%%%%%%%%%%%%%%%

%Diabetes diagnosed by doctor (2443)
DF2443_headers_all= strfind(UKB.varsHeader,'Diabetes');
Is_DF2443 = find(not(cellfun('isempty',DF2443_headers_all)));
DF2443_headers=UKB.varsHeader(Is_DF2443);
DF2443_keep_col_1=[1 2 3];
DF2443_keep_col_2=Is_DF2443(:,DF2443_keep_col_1);
Diab_DF2443=UKB.vars(:,DF2443_keep_col_2); 
%Remove instance 1
Diab_DF2443(:,2)=[];

% %Glucose (30740) no instance 2
DF30740_headers_all= strfind(UKB.varsHeader,'Glucose');
Is_DF30740 = find(not(cellfun('isempty',DF30740_headers_all)));
DF30740_headers=UKB.varsHeader(Is_DF30740);
DF30740_keep_col_1=[1 2];
DF30740_keep_col_2=Is_DF30740(:,DF30740_keep_col_1);
Diab_DF30740=UKB.vars(:,DF30740_keep_col_2);
%Remove instance 1
Diab_DF30740(:,2)=[];

%Self reported (20002)
DF20002_headers_all= strfind(UKB.varsHeader,'diabetes)');
Is_DF20002 = find(not(cellfun('isempty',DF20002_headers_all)));
DF20002_headers=UKB.varsHeader(Is_DF20002);
DF20002_keep_col_1=[1:4]; 
DF20002_keep_col_2=Is_DF20002(:,DF20002_keep_col_1);
Diab_DF20002_orig=UKB.vars(:,DF20002_keep_col_2); 
%Now binarize any type of diabetes=1 else =0
Diab_DF20002=sum(Diab_DF20002_orig,2);
Diab_DF20002(Diab_DF20002>1)=1;

% %Treatment/medication code - metformin (20003)
DF20003_headers_all= strfind(UKB.varsHeader,'metformin');
Is_DF20003 = find(not(cellfun('isempty',DF20003_headers_all)));
DF20003_headers=UKB.varsHeader(Is_DF20003);
DF20003_keep_col_1=[1];
DF20003_keep_col_2=Is_DF20003(:,DF20003_keep_col_1);
Diab_DF20003_metformin=UKB.vars(:,DF20003_keep_col_2);

%Medication for diabetes
%Insulin (6177) -1 -3 already recoded to nan
DF6177_headers_all= strfind(UKB.varsHeader,'or diabetes');
Is_DF6177 = find(not(cellfun('isempty',DF6177_headers_all)));
DF6177_headers=UKB.varsHeader(Is_DF6177);
DF6177_keep_col_1=[1:5];
DF6177_keep_col_2=Is_DF6177(:,DF6177_keep_col_1);
Diab_DF6177_orig=UKB.vars(:,DF6177_keep_col_2); 
%Binarize making 3=1 and others =0. Keep nans
Diab_DF6177_bin=nan(size(Diab_DF6177_orig,1),size(Diab_DF6177_orig,2));
Diab_DF6177_bin(Diab_DF6177_orig==2)=0;
Diab_DF6177_bin(Diab_DF6177_orig==1)=0;
Diab_DF6177_bin(Diab_DF6177_orig==3)=1;
%Here none of the above is recoded to 0
Diab_DF6177_bin(Diab_DF6177_orig==-7)=0;
%Group into instances
DF6177_0=Diab_DF6177_bin(:,(1:2));
DF6177_1=Diab_DF6177_bin(:,(3));
DF6177_2=Diab_DF6177_bin(:,(4:5));
Diab_DF6177_M=[max(DF6177_0,[],2) max(DF6177_1,[],2) max(DF6177_2,[],2) ];
%Remove instance1
Diab_DF6177_M(:,2)=[];

%Now take 6153 that is the same for females
DF6153_headers_all= strfind(UKB.varsHeader,'or take exogenous');
Is_DF6153 = find(not(cellfun('isempty',DF6153_headers_all)));
DF6153_headers=UKB.varsHeader(Is_DF6153);
DF6153_keep_col_1=[1:8];
DF6153_keep_col_2=Is_DF6153(:,DF6153_keep_col_1);
Diab_DF6153_orig=UKB.vars(:,DF6153_keep_col_2); 
%Binarize making 3=1 and others =0. Keep nans
Diab_DF6153_bin=nan(size(Diab_DF6153_orig,1),size(Diab_DF6153_orig,2));
Diab_DF6153_bin(Diab_DF6153_orig==2)=0;
Diab_DF6153_bin(Diab_DF6153_orig==1)=0;
Diab_DF6153_bin(Diab_DF6153_orig==3)=1;
Diab_DF6153_bin(Diab_DF6153_orig>3)=NaN; %this is hormone and pill
%Here none of the above is recoded to 0
Diab_DF6153_bin(Diab_DF6153_orig==-7)=0;
%Group into instances
DF6153_0=Diab_DF6153_bin(:,(1:3));
DF6153_1=Diab_DF6153_bin(:,(4:5));
DF6153_2=Diab_DF6153_bin(:,(6:8));
Diab_DF6153_F=[max(DF6153_0,[],2) max(DF6153_1,[],2) max(DF6153_2,[],2) ];
%Remove instance1
Diab_DF6153_F(:,2)=[];

%Now combine M and F in one variable
Diab_MF_in0=max([Diab_DF6177_M(:,1) Diab_DF6153_F(:,1)],[],2);
Diab_MF_in2=max([Diab_DF6177_M(:,2) Diab_DF6153_F(:,2)],[],2);
Diab_MF=[Diab_MF_in0 Diab_MF_in2];

%%%%%%  DIET/SUPPLEMENTATION %%%%%%%%%%%%%%%%
%Oily fish intake (1329) -1 -3 coded to nan in funpack
DF1329_headers_all= strfind(UKB.varsHeader,'Oily');
Is_DF1329 = find(not(cellfun('isempty',DF1329_headers_all)));
DF1329_headers=UKB.varsHeader(Is_DF1329);
DF1329_keep_col_1=[1 2 3];
DF1329_keep_col_2=Is_DF1329(:,DF1329_keep_col_1);
Diet_DF1329=UKB.vars(:,DF1329_keep_col_2); 
%Remove instance 1
Diet_DF1329(:,2)=[];

%Mineral (6179) -1 -3 coded to nan in funpack
DF6179_headers_all= strfind(UKB.varsHeader,'Mineral');
Is_DF6179 = find(not(cellfun('isempty',DF6179_headers_all)));
DF6179_headers=UKB.varsHeader(Is_DF6179);
DF6179_keep_col_1=[1:15];
DF6179_keep_col_2=Is_DF6179(:,DF6179_keep_col_1);
Diet_DF6179_orig=UKB.vars(:,DF6179_keep_col_2); 
%Code -7 to nan
Diet_DF6179_orig(Diet_DF6179_orig==-7)=nan;
%Group by istance
% 1(f-oil) is only in the first col of each instance
DF6179_0=Diet_DF6179_orig(:,1); 
DF6179_1=Diet_DF6179_orig(:,6);
DF6179_2=Diet_DF6179_orig(:,11);
%Binarise fish oil=1 rest=0
DF6179_0(DF6179_0>1)=0;
DF6179_1(DF6179_1>1)=0;
DF6179_2(DF6179_2>1)=0;
Diet_DF6179=[DF6179_0 DF6179_1 DF6179_2];
%Remove instance 1
Diet_DF6179(:,2)=[];


%Vegetable (103990)
DF103990_headers_all= strfind(UKB.varsHeader,'Vegetable');
Is_DF103990 = find(not(cellfun('isempty',DF103990_headers_all)));
DF103990_headers=UKB.varsHeader(Is_DF103990);
DF103990_keep_col_1=[1 2 3];
DF103990_keep_col_2=Is_DF103990(:,DF103990_keep_col_1);
Diet_DF103990=UKB.vars(:,DF103990_keep_col_2); 
%Remove instance 1
Diet_DF103990(:,2)=[];

%Non-butter (2654) -1 -3 coded to nan in funpack
DF2654_headers_all= strfind(UKB.varsHeader,'Non-butter');
Is_DF2654 = find(not(cellfun('isempty',DF2654_headers_all)));
DF2654_headers=UKB.varsHeader(Is_DF2654);
DF2654_keep_col_1=[1 2 3];
DF2654_keep_col_2=Is_DF2654(:,DF2654_keep_col_1);
Diet_DF2654_orig=UKB.vars(:,DF2654_keep_col_2); 
%Binarise 6=1 rest=0
Diet_DF2654=Diet_DF2654_orig;
Diet_DF2654(Diet_DF2654>6)=0;
Diet_DF2654(Diet_DF2654<6)=0;
Diet_DF2654(Diet_DF2654==6)=1;
%Here take the parent variable
DF1428_headers_all= strfind(UKB.varsHeader,'Spread type');
Is_DF1428 = find(not(cellfun('isempty',DF1428_headers_all)));
DF1428_headers=UKB.varsHeader(Is_DF1428);
DF1428_keep_col_1=[1 2 3];
DF1428_keep_col_2=Is_DF1428(:,DF1428_keep_col_1);
Nest_DF1428=UKB.vars(:,DF1428_keep_col_2);
%Child value replacement
Diet_DF2654(Nest_DF1428<3)=0;
%Remove instance 1
Diet_DF2654(:,2)=[];

%Vitamin (20084)
DF20084_headers_all= strfind(UKB.varsHeader,'Vitamin and/or');
Is_DF20084 = find(not(cellfun('isempty',DF20084_headers_all)));
DF20084_headers=UKB.varsHeader(Is_DF20084);
DF20084_keep_col_1=[1:26];
DF20084_keep_col_2=Is_DF20084(:,DF20084_keep_col_1);
Diet_DF20084_orig=UKB.vars(:,DF20084_keep_col_2);
%Make a binary version for each nutrient
DF20084_FO=Diet_DF20084_orig;
DF20084_FO(DF20084_FO<472 | DF20084_FO>472)=0;
DF20084_FO(DF20084_FO==472)=1;

DF20084_B12=Diet_DF20084_orig;
DF20084_B12(DF20084_B12<477 | DF20084_B12>477)=0;
DF20084_B12(DF20084_B12==477)=1;

DF20084_Folic=Diet_DF20084_orig;
DF20084_Folic(DF20084_Folic<481 | DF20084_Folic>481)=0;
DF20084_Folic(DF20084_Folic==481)=1;

DF20084_D=Diet_DF20084_orig;
DF20084_D(DF20084_D<479 | DF20084_D>479)=0;
DF20084_D(DF20084_D==479)=1;

%Group by istance
DF20084_FO_0=DF20084_FO(:,(1:8));
DF20084_FO_1=DF20084_FO(:,(9:17));
DF20084_FO_2=DF20084_FO(:,(18:26));

DF20084_B12_0=DF20084_B12(:,(1:8));
DF20084_B12_1=DF20084_B12(:,(9:17));
DF20084_B12_2=DF20084_B12(:,(18:26));

DF20084_Folic_0=DF20084_Folic(:,(1:8));
DF20084_Folic_1=DF20084_Folic(:,(9:17));
DF20084_Folic_2=DF20084_Folic(:,(18:26));

DF20084_D_0=DF20084_D(:,(1:8));
DF20084_D_1=DF20084_D(:,(9:17));
DF20084_D_2=DF20084_D(:,(18:26));

%Take the max for each instance
Diet_DF20084_FO=[max(DF20084_FO_0,[],2) max(DF20084_FO_1,[],2) max(DF20084_FO_2,[],2) ];
Diet_DF20084_B12=[max(DF20084_B12_0,[],2) max(DF20084_B12_1,[],2) max(DF20084_B12_2,[],2) ];
Diet_DF20084_Folic=[max(DF20084_Folic_0,[],2) max(DF20084_Folic_1,[],2) max(DF20084_Folic_2,[],2) ];
Diet_DF20084_D=[max(DF20084_D_0,[],2) max(DF20084_D_1,[],2) max(DF20084_D_2,[],2) ];
%Child value replacement
DF104670_headers_all= strfind(UKB.varsHeader,'Vitamin supplement');
Is_DF104670 = find(not(cellfun('isempty',DF104670_headers_all)));
DF104670_headers=UKB.varsHeader(Is_DF104670);
DF104670_keep_col_1=[2:4];
DF104670_keep_col_2=Is_DF104670(:,DF104670_keep_col_1);
Nest_DF104670=UKB.vars(:,DF104670_keep_col_2);

Diet_DF20084_FO(Nest_DF104670==0)=0;
Diet_DF20084_B12(Nest_DF104670==0)=0;
Diet_DF20084_Folic(Nest_DF104670==0)=0;
Diet_DF20084_D(Nest_DF104670==0)=0;
%Remove instance1
Diet_DF20084_FO(:,2)=[];
Diet_DF20084_B12(:,2)=[];
Diet_DF20084_Folic(:,2)=[];
Diet_DF20084_D(:,2)=[];

%%%%%%  EXERCISE %%%%%%%%%%%%%%%%
%Type of physical activity (6164) -3 coded to nan in funpack
DF6164_headers_all= strfind(UKB.varsHeader,'weeks');
Is_DF6164 = find(not(cellfun('isempty',DF6164_headers_all)));
DF6164_headers=UKB.varsHeader(Is_DF6164);
DF6164_keep_col_1=[25:36];
DF6164_keep_col_2=Is_DF6164(:,DF6164_keep_col_1);
Ex_DF6164_orig=UKB.vars(:,DF6164_keep_col_2);
%Here none of the above is coded as nan
%Create separate var for walk moderate vigorous
DF6164_walk=Ex_DF6164_orig;
DF6164_walk(Ex_DF6164_orig<1)=nan;
DF6164_walk(Ex_DF6164_orig>1)=0;

DF6164_mod=Ex_DF6164_orig;
DF6164_mod(Ex_DF6164_orig<0)=nan;
DF6164_mod(Ex_DF6164_orig==1)=0;
DF6164_mod(Ex_DF6164_orig==2)=1;
DF6164_mod(Ex_DF6164_orig>2)=0;

DF6164_vigor=Ex_DF6164_orig;
DF6164_vigor(Ex_DF6164_orig<0)=nan;
DF6164_vigor(Ex_DF6164_orig==1)=0;
DF6164_vigor(Ex_DF6164_orig==2)=0;
DF6164_vigor(Ex_DF6164_orig==3)=1;
DF6164_vigor(Ex_DF6164_orig>3)=0;

%Group by istance
DF6164_w_0=DF6164_walk(:,(1:4));
DF6164_w_1=DF6164_walk(:,(5:8));
DF6164_w_2=DF6164_walk(:,(9:12));

DF6164_m_0=DF6164_mod(:,(1:4));
DF6164_m_1=DF6164_mod(:,(5:8));
DF6164_m_2=DF6164_mod(:,(9:12));

DF6164_v_0=DF6164_vigor(:,(1:4));
DF6164_v_1=DF6164_vigor(:,(5:8));
DF6164_v_2=DF6164_vigor(:,(9:12));

%Take the max for each instance
Ex_DF6164_walk=[max(DF6164_w_0,[],2) max(DF6164_w_1,[],2) max(DF6164_w_2,[],2) ];
Ex_DF6164_mod=[max(DF6164_m_0,[],2) max(DF6164_m_1,[],2) max(DF6164_m_2,[],2) ];
Ex_DF6164_vigor=[max(DF6164_v_0,[],2) max(DF6164_v_1,[],2) max(DF6164_v_2,[],2) ];
%Remove instance1
Ex_DF6164_walk(:,2)=[];
Ex_DF6164_mod(:,2)=[];
Ex_DF6164_vigor(:,2)=[];

%Freq of walking (971) -1 -3 coded to nan in funpack
DF971_headers_all= strfind(UKB.varsHeader,'Frequency of walking');
Is_DF971 = find(not(cellfun('isempty',DF971_headers_all)));
DF971_headers=UKB.varsHeader(Is_DF971);
DF971_keep_col_1=[1 2 3];
DF971_keep_col_2=Is_DF971(:,DF971_keep_col_1);
Ex_DF971=UKB.vars(:,DF971_keep_col_2);
%Recode funpack 0 to nan
Ex_DF971(Ex_DF971==0)=nan;
%Child value replacement
Nest_DF6164=[max(Ex_DF6164_orig(:,(1:4)),[],2) max(Ex_DF6164_orig(:,(5:8)),[],2) max(Ex_DF6164_orig(:,(9:12)),[],2)]; 
Ex_DF971(Nest_DF6164>1)=0;
%Remove instance1
Ex_DF971(:,2)=[];

%Freq of sport (991) -1 -3 coded to nan in funpack
DF991_headers_all= strfind(UKB.varsHeader,'Frequency of strenuous');
Is_DF991 = find(not(cellfun('isempty',DF991_headers_all)));
DF991_headers=UKB.varsHeader(Is_DF991);
DF991_keep_col_1=[1 2 3];
DF991_keep_col_2=Is_DF991(:,DF991_keep_col_1);
Ex_DF991=UKB.vars(:,DF991_keep_col_2);
%Recode funpack 0 to nan
Ex_DF991(Ex_DF991==0)=nan;
%Child value replacement
Nest_DF6164=Ex_DF6164_orig;
Nest_DF6164(Nest_DF6164==3)=10;
Nest_DF6164=[max(Nest_DF6164(:,(1:4)),[],2) max(Nest_DF6164(:,(5:8)),[],2) max(Nest_DF6164(:,(9:12)),[],2)]; 
%Recode negative values to nan
Nest_DF6164(Nest_DF6164<0)=nan;
Ex_DF991(Nest_DF6164<10)=0;
%Remove instance 1
Ex_DF991(:,2)=[];

%Freq of stair (943) -1 -3 coded to nan in funpack
DF943_headers_all= strfind(UKB.varsHeader,'Frequency of stair');
Is_DF943 = find(not(cellfun('isempty',DF943_headers_all)));
DF943_headers=UKB.varsHeader(Is_DF943);
DF943_keep_col_1=[1 2 3];
DF943_keep_col_2=Is_DF943(:,DF943_keep_col_1);
Ex_DF943=UKB.vars(:,DF943_keep_col_2);
%Remove instance1
Ex_DF943(:,2)=[];

%Freq of exercise (3637) -1 -3 coded to nan in funpack
DF3637_headers_all= strfind(UKB.varsHeader,'Frequency of other');
Is_DF3637 = find(not(cellfun('isempty',DF3637_headers_all)));
DF3637_headers=UKB.varsHeader(Is_DF3637);
DF3637_keep_col_1=[1 2 3];
DF3637_keep_col_2=Is_DF3637(:,DF3637_keep_col_1);
Ex_DF3637=UKB.vars(:,DF3637_keep_col_2);
%Child value replacement
Nest_DF6164=Ex_DF6164_orig;
Nest_DF6164(Nest_DF6164==2)=10;
Nest_DF6164=[max(Nest_DF6164(:,(1:4)),[],2) max(Nest_DF6164(:,(5:8)),[],2) max(Nest_DF6164(:,(9:12)),[],2)]; 
%Recode negative values to nan
Nest_DF6164(Nest_DF6164<0)=nan;
Ex_DF3637(Nest_DF6164<10)=0;
%Remove instance 1
Ex_DF3637(:,2)=[];

%N of days walked (864) -1 -2 -3 coded to nan in funpack
DF864_headers_all= strfind(UKB.varsHeader,'days/week walked');
Is_DF864 = find(not(cellfun('isempty',DF864_headers_all)));
DF864_headers=UKB.varsHeader(Is_DF864);
DF864_keep_col_1=[1 2 3];
DF864_keep_col_2=Is_DF864(:,DF864_keep_col_1);
Ex_DF864=UKB.vars(:,DF864_keep_col_2);
%Remove instance 1
Ex_DF864(:,2)=[];

%N of days mod exercise (884) -1 -3 coded to nan in funpack
DF884_headers_all= strfind(UKB.varsHeader,'days/week of moderate');
Is_DF884 = find(not(cellfun('isempty',DF884_headers_all)));
DF884_headers=UKB.varsHeader(Is_DF884);
DF884_keep_col_1=[1 2 3];
DF884_keep_col_2=Is_DF884(:,DF884_keep_col_1);
Ex_DF884=UKB.vars(:,DF884_keep_col_2);
%Remove instance 1
Ex_DF884(:,2)=[];

%N of days vigorous exercise (904) -1 -3 coded to nan in funpack
DF904_headers_all= strfind(UKB.varsHeader,'days/week of vigorous');
Is_DF904 = find(not(cellfun('isempty',DF904_headers_all)));
DF904_headers=UKB.varsHeader(Is_DF904);
DF904_keep_col_1=[1 2 3];
DF904_keep_col_2=Is_DF904(:,DF904_keep_col_1);
Ex_DF904=UKB.vars(:,DF904_keep_col_2);
%Remove instance 1
Ex_DF904(:,2)=[];

%%%%%%  HEARING  %%%%%%%%%%%%%%%%

%triplet correct (left) (4232)
DF4232_headers_all= strfind(UKB.varsHeader,'Triplet correct');
Is_DF4232 = find(not(cellfun('isempty',DF4232_headers_all)));
DF4232_headers=UKB.varsHeader(Is_DF4232);
DF4232_keep_col_1=[1:45];
DF4232_keep_col_2=Is_DF4232(:,DF4232_keep_col_1);
HL_DF4232_orig=UKB.vars(:,DF4232_keep_col_2);
%Recode 9 to 0 and nan to 100
HL_DF4232_rec=zeros(size(HL_DF4232_orig,1),size(HL_DF4232_orig,2));
HL_DF4232_rec(HL_DF4232_orig==1)=1;
nan_DF4232=isnan(HL_DF4232_orig);
HL_DF4232_rec(nan_DF4232)=100;
%Collapse into 3 instances
in0=HL_DF4232_rec(:,1:15);
in1=HL_DF4232_rec(:,16:30);
in2=HL_DF4232_rec(:,31:45);
HL_DF4232(:,1)=sum(in0,2);
HL_DF4232(:,2)=sum(in1,2);
HL_DF4232(:,3)=sum(in2,2);
HL_DF4232(HL_DF4232>15)=nan;
%Remove instance1
HL_DF4232(:,2)=[];

%triplet correct (right) (4243)
DF4243_headers_all= strfind(UKB.varsHeader,'Triplet correct');
Is_DF4243 = find(not(cellfun('isempty',DF4243_headers_all)));
DF4243_headers=UKB.varsHeader(Is_DF4243);
DF4243_keep_col_1=[61:105];
DF4243_keep_col_2=Is_DF4243(:,DF4243_keep_col_1);
HL_DF4243_orig=UKB.vars(:,DF4243_keep_col_2);
%Recode 9 to 0 and nan to 100
HL_DF4243_rec=zeros(size(HL_DF4243_orig,1),size(HL_DF4243_orig,2));
HL_DF4243_rec(HL_DF4243_orig==1)=1;
nan_DF4243=isnan(HL_DF4243_orig);
HL_DF4243_rec(nan_DF4243)=100;
%Collapse into 3 instances
in0=HL_DF4243_rec(:,1:15);
in1=HL_DF4243_rec(:,16:30);
in2=HL_DF4243_rec(:,31:45);
HL_DF4243(:,1)=sum(in0,2);
HL_DF4243(:,2)=sum(in1,2);
HL_DF4243(:,3)=sum(in2,2);
HL_DF4243(HL_DF4243>15)=nan;
%Remove instance1
HL_DF4243(:,2)=[];

%hearing difficulty/background (2257) -1 -3 already recoded to nan
DF2257_headers_all= strfind(UKB.varsHeader,'background');
Is_DF2257 = find(not(cellfun('isempty',DF2257_headers_all)));
DF2257_headers=UKB.varsHeader(Is_DF2257);
DF2257_keep_col_1=[1 2 3];
DF2257_keep_col_2=Is_DF2257(:,DF2257_keep_col_1);
HL_DF2257=UKB.vars(:,DF2257_keep_col_2);
%Remove instance1
HL_DF2257(:,2)=[];

%hearing difficulty (2247) -1 -3 already recoded to nan
DF2247_headers_all= strfind(UKB.varsHeader,'Hearing difficulty');
Is_DF2247 = find(not(cellfun('isempty',DF2247_headers_all)));
DF2247_headers=UKB.varsHeader(Is_DF2247);
DF2247_keep_col_1=[2 3 4];
DF2247_keep_col_2=Is_DF2247(:,DF2247_keep_col_1);
HL_DF2247=UKB.vars(:,DF2247_keep_col_2);
%Recode 99=completely deaf to nan
HL_DF2247(HL_DF2247==99)=nan;
%Remove instance1
HL_DF2247(:,2)=[];

%%%%%%  INFLAMMATION  %%%%%%%%%%%%%%%%

%Hayfever (6152)
DF6152_headers_all= strfind(UKB.varsHeader,'DVT');
Is_DF6152 = find(not(cellfun('isempty',DF6152_headers_all)));
DF6152_headers=UKB.varsHeader(Is_DF6152);
DF6152_keep_col_1=[4:11];
DF6152_keep_col_2=Is_DF6152(:,DF6152_keep_col_1);
Infl_DF6152_orig=UKB.vars(:,DF6152_keep_col_2);
%Put to 0 everything that is not 9 (Hayfever)
Infl_DF6152_long=nan(size(Infl_DF6152_orig,1),size(Infl_DF6152_orig,2));
Infl_DF6152_long(Infl_DF6152_orig==9)=1;
Infl_DF6152_long(Infl_DF6152_orig<9)=0;
%Here none of the above is recoded as 0
Infl_DF6152_long(Infl_DF6152_orig==-7)=0;
%Group instances
in0=Infl_DF6152_long(:,1:3);
in1=Infl_DF6152_long(:,4:5);
in2=Infl_DF6152_long(:,6:8);
Infl_DF6152=[max(in0,[],2) max(in1,[],2) max(in2,[],2) ];
%Remove instance1
Infl_DF6152(:,2)=[];

%Med for pain relief, constipation, heartburn (6154)
DF6154_headers_all= strfind(UKB.varsHeader,'heartburn');
Is_DF6154 = find(not(cellfun('isempty',DF6154_headers_all)));
DF6154_headers=UKB.varsHeader(Is_DF6154);
DF6154_keep_col_1=[1:11];
DF6154_keep_col_2=Is_DF6154(:,DF6154_keep_col_1);
Infl_DF6154_orig=UKB.vars(:,DF6154_keep_col_2);
%Put to 0 everything that is not 1(aspirin) or 2 (ibuprofen)
Infl_DF6154_long=nan(size(Infl_DF6154_orig,1),size(Infl_DF6154_orig,2));
%Here none of the above is recoded to 0
Infl_DF6154_long(Infl_DF6154_orig<0)=0;
Infl_DF6154_long(Infl_DF6154_orig>2)=0; 
Infl_DF6154_long(Infl_DF6154_orig==1)=1;
Infl_DF6154_long(Infl_DF6154_orig==2)=1;
%Group into instances
in0=Infl_DF6154_long(:,1:4);
in1=Infl_DF6154_long(:,5:7);
in2=Infl_DF6154_long(:,8:11);
Infl_DF6154=[max(in0,[],2) max(in1,[],2) max(in2,[],2) ];
%remove instance 1
Infl_DF6154(:,2)=[];

%C-reactive protein (30710) no instance 2
DF30710_headers_all= strfind(UKB.varsHeader,'C-reactive');
Is_DF30710 = find(not(cellfun('isempty',DF30710_headers_all)));
DF30710_headers=UKB.varsHeader(Is_DF30710);
DF30710_keep_col_1=[1 2]; 
DF30710_keep_col_2=Is_DF30710(:,DF30710_keep_col_1);
Infl_DF30710=UKB.vars(:,DF30710_keep_col_2); 
%remove instance 1
Infl_DF30710(:,2)=[];

%Rheumatoid factor (30820) no instance 2
DF30820_headers_all= strfind(UKB.varsHeader,'Rheumatoid');
Is_DF30820 = find(not(cellfun('isempty',DF30820_headers_all)));
DF30820_headers=UKB.varsHeader(Is_DF30820);
DF30820_keep_col_1=[1 2]; 
DF30820_keep_col_2=Is_DF30820(:,DF30820_keep_col_1);
Infl_DF30820=UKB.vars(:,DF30820_keep_col_2);
%remove instance 1
Infl_DF30820(:,2)=[];

%Anti-inflammatory (ibuprofen, naproxen, aspirin)
% %Treatment/medication code - ibuprofen (20003)
DF20003_headers_all= strfind(UKB.varsHeader,'ibuprofen');
Is_DF20003 = find(not(cellfun('isempty',DF20003_headers_all)));
DF20003_headers=UKB.varsHeader(Is_DF20003);
DF20003_keep_col_1=[1];
DF20003_keep_col_2=Is_DF20003(:,DF20003_keep_col_1);
ibuprofen=UKB.vars(:,DF20003_keep_col_2); 
% 
% %Treatment/medication code - naproxen (20003)
DF20003_headers_all= strfind(UKB.varsHeader,'naproxen');
Is_DF20003 = find(not(cellfun('isempty',DF20003_headers_all)));
DF20003_headers=UKB.varsHeader(Is_DF20003);
DF20003_keep_col_1=[1];
DF20003_keep_col_2=Is_DF20003(:,DF20003_keep_col_1);
naproxen=UKB.vars(:,DF20003_keep_col_2); 

% %Treatment/medication code - aspirin (20003)
DF20003_headers_all= strfind(UKB.varsHeader,'aspirin');
Is_DF20003 = find(not(cellfun('isempty',DF20003_headers_all)));
DF20003_headers=UKB.varsHeader(Is_DF20003);
DF20003_keep_col_1=[2];
DF20003_keep_col_2=Is_DF20003(:,DF20003_keep_col_1);
aspirin=UKB.vars(:,DF20003_keep_col_2); 

ant_inf=[ibuprofen aspirin naproxen];
Infl_DF20003_ant_inf=sum(ant_inf,2);
Infl_DF20003_ant_inf(Infl_DF20003_ant_inf>1)=1;

%anti-allergies (loratadine, fexofenadine, cetirizine)
% %Treatment/medication code - loratadine (20003)
DF20003_headers_all= strfind(UKB.varsHeader,'loratadine');
Is_DF20003 = find(not(cellfun('isempty',DF20003_headers_all)));
DF20003_headers=UKB.varsHeader(Is_DF20003);
DF20003_keep_col_1=[2];
DF20003_keep_col_2=Is_DF20003(:,DF20003_keep_col_1);
loratadine=UKB.vars(:,DF20003_keep_col_2); 

% %Treatment/medication code - fexofenadine (20003)
DF20003_headers_all= strfind(UKB.varsHeader,'fexofenadine');
Is_DF20003 = find(not(cellfun('isempty',DF20003_headers_all)));
DF20003_headers=UKB.varsHeader(Is_DF20003);
DF20003_keep_col_1=[1];
DF20003_keep_col_2=Is_DF20003(:,DF20003_keep_col_1);
fexofenadine=UKB.vars(:,DF20003_keep_col_2); 

% %Treatment/medication code - cetirizine (20003)
DF20003_headers_all= strfind(UKB.varsHeader,'cetirizine');
Is_DF20003 = find(not(cellfun('isempty',DF20003_headers_all)));
DF20003_headers=UKB.varsHeader(Is_DF20003);
DF20003_keep_col_1=[1];
DF20003_keep_col_2=Is_DF20003(:,DF20003_keep_col_1);
cetirizine=UKB.vars(:,DF20003_keep_col_2);

ant_allergic=[loratadine fexofenadine cetirizine];
Infl_DF20003_ant_allerg=sum(ant_allergic,2);
Infl_DF20003_ant_allerg(Infl_DF20003_ant_allerg>1)=1;

% %Treatment/medication code - beclometasone (20003)
DF20003_headers_all= strfind(UKB.varsHeader,'beclomet');
Is_DF20003 = find(not(cellfun('isempty',DF20003_headers_all)));
DF20003_headers=UKB.varsHeader(Is_DF20003);
DF20003_keep_col_1=[2 3];
DF20003_keep_col_2=Is_DF20003(:,DF20003_keep_col_1);
beclomet=UKB.vars(:,DF20003_keep_col_2);
Infl_DF20003_beclometasone=sum(beclomet,2);
Infl_DF20003_beclometasone(Infl_DF20003_beclometasone>1)=1;

%%%%%%  POLLUTION %%%%%%%%%%%%%%%%

%Nitrogen dioxide 2010 (24003)
DF24003_headers_all= strfind(UKB.varsHeader,'Nitrogen');
Is_DF24003 = find(not(cellfun('isempty',DF24003_headers_all)));
DF24003_headers=UKB.varsHeader(Is_DF24003);
DF24003_keep_col_1=[1];
DF24003_keep_col_2=Is_DF24003(:,DF24003_keep_col_1);
Pol_DF24003=UKB.vars(:,DF24003_keep_col_2);

%Nitrogen dioxide 2005 (24016)
DF24016_headers_all= strfind(UKB.varsHeader,'Nitrogen');
Is_DF24016 = find(not(cellfun('isempty',DF24016_headers_all)));
DF24016_headers=UKB.varsHeader(Is_DF24016);
DF24016_keep_col_1=[3];
DF24016_keep_col_2=Is_DF24016(:,DF24016_keep_col_1);
Pol_DF24016=UKB.vars(:,DF24016_keep_col_2);

%Nitrogen oxide 2010 (24004)
DF24004_headers_all= strfind(UKB.varsHeader,'Nitrogen');
Is_DF24004 = find(not(cellfun('isempty',DF24004_headers_all)));
DF24004_headers=UKB.varsHeader(Is_DF24004);
DF24004_keep_col_1=[2];
DF24004_keep_col_2=Is_DF24004(:,DF24004_keep_col_1);
Pol_DF24004=UKB.vars(:,DF24004_keep_col_2);

%Pm10 2010 (24005)
DF24005_headers_all= strfind(UKB.varsHeader,'pm10');
Is_DF24005 = find(not(cellfun('isempty',DF24005_headers_all)));
DF24005_headers=UKB.varsHeader(Is_DF24005);
DF24005_keep_col_1=[1];
DF24005_keep_col_2=Is_DF24005(:,DF24005_keep_col_1);
Pol_DF24005=UKB.vars(:,DF24005_keep_col_2);

%Pm2.5 2010 (24006)
DF24006_headers_all= strfind(UKB.varsHeader,'pm2.5');
Is_DF24006 = find(not(cellfun('isempty',DF24006_headers_all)));
DF24006_headers=UKB.varsHeader(Is_DF24006);
DF24006_keep_col_1=[1];
DF24006_keep_col_2=Is_DF24006(:,DF24006_keep_col_1);
Pol_DF24006=UKB.vars(:,DF24006_keep_col_2);

%Pm2.5 absorbance 2010 (24007)
DF24007_headers_all= strfind(UKB.varsHeader,'pm2.5');
Is_DF24007 = find(not(cellfun('isempty',DF24007_headers_all)));
DF24007_headers=UKB.varsHeader(Is_DF24007);
DF24007_keep_col_1=[2];
DF24007_keep_col_2=Is_DF24007(:,DF24007_keep_col_1);
Pol_DF24007=UKB.vars(:,DF24007_keep_col_2);

%Pm2.5-10 absorbance 2010 (24008)
DF24008_headers_all= strfind(UKB.varsHeader,'Particulate');
Is_DF24008 = find(not(cellfun('isempty',DF24008_headers_all)));
DF24008_headers=UKB.varsHeader(Is_DF24008);
DF24008_keep_col_1=[4];
DF24008_keep_col_2=Is_DF24008(:,DF24008_keep_col_1);
Pol_DF24008=UKB.vars(:,DF24008_keep_col_2);

%Traffic intensity nearest major roead (24011)
DF24011_headers_all= strfind(UKB.varsHeader,'Traffic');
Is_DF24011 = find(not(cellfun('isempty',DF24011_headers_all)));
DF24011_headers=UKB.varsHeader(Is_DF24011);
DF24011_keep_col_1=[2];
DF24011_keep_col_2=Is_DF24011(:,DF24011_keep_col_1);
Pol_DF24011=UKB.vars(:,DF24011_keep_col_2);

%Close to major road (24014)
DF24014_headers_all= strfind(UKB.varsHeader,'major road');
Is_DF24014 = find(not(cellfun('isempty',DF24014_headers_all)));
DF24014_headers=UKB.varsHeader(Is_DF24014);
DF24014_keep_col_1=[4];
DF24014_keep_col_2=Is_DF24014(:,DF24014_keep_col_1);
Pol_DF24014=UKB.vars(:,DF24014_keep_col_2);

%%%%%%  SLEEP  %%%%%%%%%%%%%%%%

%chronotype (1180) -1 -3 already recoded to nan 
DF1180_headers_all= strfind(UKB.varsHeader,'chronotype');
Is_DF1180 = find(not(cellfun('isempty',DF1180_headers_all)));
DF1180_headers=UKB.varsHeader(Is_DF1180);
DF1180_keep_col_1=[1 2 3];
DF1180_keep_col_2=Is_DF1180(:,DF1180_keep_col_1);
Sl_DF1180=UKB.vars(:,DF1180_keep_col_2);
%Remove instance1
Sl_DF1180(:,2)=[];
%Split into dummies
Sl_DF1180_m=Sl_DF1180;
Sl_DF1180_m(Sl_DF1180_m>1)=0;
Sl_DF1180_m_e=Sl_DF1180;
Sl_DF1180_m_e(Sl_DF1180_m_e<2)=0;
Sl_DF1180_m_e(Sl_DF1180_m_e>2)=0;
Sl_DF1180_m_e(Sl_DF1180_m_e==2)=1;
Sl_DF1180_e_m=Sl_DF1180;
Sl_DF1180_e_m(Sl_DF1180_e_m<3)=0;
Sl_DF1180_e_m(Sl_DF1180_e_m>3)=0;
Sl_DF1180_e_m(Sl_DF1180_e_m==3)=1;
Sl_DF1180_e=Sl_DF1180;
Sl_DF1180_e(Sl_DF1180_e<4)=0;
Sl_DF1180_e(Sl_DF1180_e==4)=1;

%getting up in morning (1170) -1 -3 already recoded to nan 
DF1170_headers_all= strfind(UKB.varsHeader,'morning');
Is_DF1170 = find(not(cellfun('isempty',DF1170_headers_all)));
DF1170_headers=UKB.varsHeader(Is_DF1170);
DF1170_keep_col_1=[1 2 3];
DF1170_keep_col_2=Is_DF1170(:,DF1170_keep_col_1);
Sl_DF1170=UKB.vars(:,DF1170_keep_col_2);
%Remove instance1
Sl_DF1170(:,2)=[];

%sleep duration (1160) -1 -3 already recoded to nan 
DF1160_headers_all= strfind(UKB.varsHeader,'Sleep duration');
Is_DF1160 = find(not(cellfun('isempty',DF1160_headers_all)));
DF1160_headers=UKB.varsHeader(Is_DF1160);
DF1160_keep_col_1=[1 2 3];
DF1160_keep_col_2=Is_DF1160(:,DF1160_keep_col_1);
Sl_DF1160=UKB.vars(:,DF1160_keep_col_2);
%Remove instance1
Sl_DF1160(:,2)=[];

%trouble sleep/awake (20517) -1 -3 already recoded to nan 
DF20517_headers_all= strfind(UKB.varsHeader,'Trouble falling');
Is_DF20517 = find(not(cellfun('isempty',DF20517_headers_all)));
DF20517_headers=UKB.varsHeader(Is_DF20517);
DF20517_keep_col_1=[1];
DF20517_keep_col_2=Is_DF20517(:,DF20517_keep_col_1);
Sl_DF20517=UKB.vars(:,DF20517_keep_col_2);
%Remove pna (-818)
Sl_DF20517(Sl_DF20517==-818)=nan;

%Frequency of tiredness (2080) -1 -3 already recoded to nan 
DF2080_headers_all= strfind(UKB.varsHeader,'tiredness');
Is_DF2080 = find(not(cellfun('isempty',DF2080_headers_all)));
DF2080_headers=UKB.varsHeader(Is_DF2080);
DF2080_keep_col_1=[1 2 3];
DF2080_keep_col_2=Is_DF2080(:,DF2080_keep_col_1);
Sl_DF2080=UKB.vars(:,DF2080_keep_col_2);
%Shift it back to start from 0
Sl_DF2080=Sl_DF2080-1;
%Remove instance 1
Sl_DF2080(:,2)=[];

%Insomnia (1200) -3 already recoded to nan 
DF1200_headers_all= strfind(UKB.varsHeader,'insomnia');
Is_DF1200 = find(not(cellfun('isempty',DF1200_headers_all)));
DF1200_headers=UKB.varsHeader(Is_DF1200);
DF1200_keep_col_1=[1 2 3];
DF1200_keep_col_2=Is_DF1200(:,DF1200_keep_col_1);
Sl_DF1200=UKB.vars(:,DF1200_keep_col_2);
%Shift it back to start from 0
Sl_DF1200=Sl_DF1200-1;
%Remove instance 1
Sl_DF1200(:,2)=[];

%%%%%%  SMOKING  %%%%%%%%%%%%%%%%

%past tobacco smoking (1249) -3 already recoded to nan
DF1249_headers_all= strfind(UKB.varsHeader,'Past tobacco');
Is_DF1249 = find(not(cellfun('isempty',DF1249_headers_all)));
DF1249_headers=UKB.varsHeader(Is_DF1249);
DF1249_keep_col_1=[1 2 3];
DF1249_keep_col_2=Is_DF1249(:,DF1249_keep_col_1);
Smk_DF1249_orig=UKB.vars(:,DF1249_keep_col_2);
%Invert coding to have 1=never 4=every day
Smk_DF1249=nan(size(Smk_DF1249_orig,1),size(Smk_DF1249_orig,2));
Smk_DF1249(Smk_DF1249_orig==1)=4;
Smk_DF1249(Smk_DF1249_orig==2)=3;
Smk_DF1249(Smk_DF1249_orig==3)=2;
Smk_DF1249(Smk_DF1249_orig==4)=1;
%Remove instance1
Smk_DF1249(:,2)=[];

%current tobacco smoking (1239) -3 already recoded to nan
DF1239_headers_all= strfind(UKB.varsHeader,'Current tobacco');
Is_DF1239 = find(not(cellfun('isempty',DF1239_headers_all)));
DF1239_headers=UKB.varsHeader(Is_DF1239);
DF1239_keep_col_1=[1 2 3];
DF1239_keep_col_2=Is_DF1239(:,DF1239_keep_col_1);
Smk_DF1239=UKB.vars(:,DF1239_keep_col_2);
%Remove instance1
Smk_DF1239(:,2)=[];

%Age started in current (3436) -1 -3 already recoded to nan
DF3436_headers_all= strfind(UKB.varsHeader,'in current smokers');
Is_DF3436 = find(not(cellfun('isempty',DF3436_headers_all)));
DF3436_headers=UKB.varsHeader(Is_DF3436);
DF3436_keep_col_1=[1 2 3];
DF3436_keep_col_2=Is_DF3436(:,DF3436_keep_col_1);
Smk_DF3436=UKB.vars(:,DF3436_keep_col_2);
%Remove instance1
Smk_DF3436(:,2)=[];

%Age started in former (2867) -1 -3 already recoded to nan 
DF2867_headers_all= strfind(UKB.varsHeader,'in former smokers');
Is_DF2867 = find(not(cellfun('isempty',DF2867_headers_all)));
DF2867_headers=UKB.varsHeader(Is_DF2867);
DF2867_keep_col_1=[1 2 3];
DF2867_keep_col_2=Is_DF2867(:,DF2867_keep_col_1);
Smk_DF2867=UKB.vars(:,DF2867_keep_col_2);
%Remove instance1
Smk_DF2867(:,2)=[];
%Changing 0 replaced in funpack to nan 
Smk_DF2867(Smk_DF2867==0)=nan;

%Age started current+former
cur=Smk_DF3436;
cur(isnan(cur))=0;
form=Smk_DF2867;
form(isnan(form))=0;
Smk_AST=cur+form;
Smk_AST(Smk_AST==0)=nan;

%Age stopped smoking (2897) -1 -3 already recoded to nan
DF2897_headers_all= strfind(UKB.varsHeader,'Age stopped smoking');
Is_DF2897 = find(not(cellfun('isempty',DF2897_headers_all)));
DF2897_headers=UKB.varsHeader(Is_DF2897);
DF2897_keep_col_1=[1 2 3];
DF2897_keep_col_2=Is_DF2897(:,DF2897_keep_col_1);
Smk_DF2897=UKB.vars(:,DF2897_keep_col_2);
%Remove instance1
Smk_DF2897(:,2)=[];
%Changing 0 replaced in funpack to nan
Smk_DF2897(Smk_DF2897==0)=nan;
%Child value replacement
Smk_DF2897_repl=Smk_DF2897;
Smk_DF2897_repl(Smk_DF1249==1)=0;

%n cigarettes previously smoked (2887) -1 already recoded to nan
DF2887_headers_all= strfind(UKB.varsHeader,'cigarettes previously');
Is_DF2887 = find(not(cellfun('isempty',DF2887_headers_all)));
DF2887_headers=UKB.varsHeader(Is_DF2887);
DF2887_keep_col_1=[1 2 3];
DF2887_keep_col_2=Is_DF2887(:,DF2887_keep_col_1);
Smk_DF2887=UKB.vars(:,DF2887_keep_col_2); 
%recoded -10=less than 1/day to nan
Smk_DF2887(Smk_DF2887==-10)=nan; 
%Changing 0 replaced in funpack to nan
Smk_DF2887(Smk_DF2887==0)=nan;
%Remove instance 1
Smk_DF2887(:,2)=[];
%Child value replacement
Smk_DF2887(Smk_DF1249==1)=0;

%n cigarettes currently smoked (3456) -1 -3 already recoded to nan
DF3456_headers_all= strfind(UKB.varsHeader,'cigarettes currently');
Is_DF3456 = find(not(cellfun('isempty',DF3456_headers_all)));
DF3456_headers=UKB.varsHeader(Is_DF3456);
DF3456_keep_col_1=[1 2 3];
DF3456_keep_col_2=Is_DF3456(:,DF3456_keep_col_1);
Smk_DF3456=UKB.vars(:,DF3456_keep_col_2);
%recoded -10=less than 1/day to nan
Smk_DF3456(Smk_DF3456==-10)=nan; 
%Child value replacement
Nest_DF1239=UKB.vars(:,DF1239_keep_col_2);
Smk_DF3456(Nest_DF1239==0)=0;
%Remove instance 1
Smk_DF3456(:,2)=[];

%%%%%%  SOCIALISATION %%%%%%%%%%%%%%%%
%Social activities (6160) -3 coded to nan in funpack
DF6160_headers_all= strfind(UKB.varsHeader,'Leisure');
Is_DF6160 = find(not(cellfun('isempty',DF6160_headers_all)));
DF6160_headers=UKB.varsHeader(Is_DF6160);
DF6160_keep_col_1=[1:12];
DF6160_keep_col_2=Is_DF6160(:,DF6160_keep_col_1);
Soc_DF6160_orig=UKB.vars(:,DF6160_keep_col_2);
%Recode -7 to 0
Soc_DF6160_orig(Soc_DF6160_orig==-7)=0; 
%Group instances
DF6160_0=Soc_DF6160_orig(:,(1:4));
DF6160_1=Soc_DF6160_orig(:,(5:8));
DF6160_2=Soc_DF6160_orig(:,(9:12));

%Make binary for sport
DF6160_s_0=DF6160_0;
DF6160_s_0(DF6160_s_0>1)=0;
DF6160_s_1=DF6160_1;
DF6160_s_1(DF6160_s_1>1)=0;
DF6160_s_2=DF6160_2;
DF6160_s_2(DF6160_s_2>1)=0;
Soc_DF6160_sport=[max(DF6160_s_0,[],2) max(DF6160_s_1,[],2) max(DF6160_s_2,[],2) ];
%Remove instance1
Soc_DF6160_sport(:,2)=[];

%Make binary for pub or social
DF6160_p_0=DF6160_0;
DF6160_p_0(DF6160_p_0<2 | DF6160_p_0>2)=0;
DF6160_p_0(DF6160_p_0==2)=1;
DF6160_p_1=DF6160_1;
DF6160_p_1(DF6160_p_1<2 | DF6160_p_1>2)=0;
DF6160_p_1(DF6160_p_1==2)=1;
DF6160_p_2=DF6160_2;
DF6160_p_2(DF6160_p_2<2 | DF6160_p_2>2)=0;
DF6160_p_2(DF6160_p_2==2)=1;
Soc_DF6160_pub=[max(DF6160_p_0,[],2) max(DF6160_p_1,[],2) max(DF6160_p_2,[],2) ];
%Remove instance1
Soc_DF6160_pub(:,2)=[];

%Make binary for religion
DF6160_r_0=DF6160_0;
DF6160_r_0(DF6160_r_0<3 | DF6160_r_0>3)=0;
DF6160_r_0(DF6160_r_0==3)=1;
DF6160_r_1=DF6160_1;
DF6160_r_1(DF6160_r_1<3 | DF6160_r_1>3)=0;
DF6160_r_1(DF6160_r_1==3)=1;
DF6160_r_2=DF6160_2;
DF6160_r_2(DF6160_r_2<3 | DF6160_r_2>3)=0;
DF6160_r_2(DF6160_r_2==3)=1;
Soc_DF6160_religion=[max(DF6160_r_0,[],2) max(DF6160_r_1,[],2) max(DF6160_r_2,[],2) ];
%Remove instance1
Soc_DF6160_religion(:,2)=[];

%Make binary for adult education
DF6160_e_0=DF6160_0;
DF6160_e_0(DF6160_e_0<4 | DF6160_e_0>4)=0;
DF6160_e_0(DF6160_e_0==4)=1;
DF6160_e_1=DF6160_1;
DF6160_e_1(DF6160_e_1<4 | DF6160_e_1>4)=0;
DF6160_e_1(DF6160_e_1==4)=1;
DF6160_e_2=DF6160_2;
DF6160_e_2(DF6160_e_2<4 | DF6160_e_2>4)=0;
DF6160_e_2(DF6160_e_2==4)=1;
Soc_DF6160_edu=[max(DF6160_e_0,[],2) max(DF6160_e_1,[],2) max(DF6160_e_2,[],2) ];
%Remove instance1
Soc_DF6160_edu(:,2)=[];

%Make binary for other
DF6160_o_0=DF6160_0;
DF6160_o_0(DF6160_o_0<5 | DF6160_o_0>5)=0;
DF6160_o_0(DF6160_o_0==5)=1;
DF6160_o_1=DF6160_1;
DF6160_o_1(DF6160_o_1<5 | DF6160_o_1>5)=0;
DF6160_o_1(DF6160_o_1==5)=1;
DF6160_o_2=DF6160_2;
DF6160_o_2(DF6160_o_2<5 | DF6160_o_2>5)=0;
DF6160_o_2(DF6160_o_2==5)=1;
Soc_DF6160_other=[max(DF6160_o_0,[],2) max(DF6160_o_1,[],2) max(DF6160_o_2,[],2) ];
%Remove instance1
Soc_DF6160_other(:,2)=[];

%Make the total binary
Soc_DF6160_v2=UKB.vars(:,DF6160_keep_col_2);
Soc_DF6160_v2(Soc_DF6160_v2==-7)=0;
Soc_DF6160_v2(Soc_DF6160_v2>0)=1;

%Group instances
DF6160_0_v2=Soc_DF6160_v2(:,(1:4));
DF6160_1_v2=Soc_DF6160_v2(:,(5:8));
DF6160_2_v2=Soc_DF6160_v2(:,(9:12));

Soc_DF6160_all=[max(DF6160_0_v2,[],2) max(DF6160_1_v2,[],2) max(DF6160_2_v2,[],2) ];
%Remove instance 1
Soc_DF6160_all(:,2)=[];

%%%%%%%%%%%%%% EDUCATION %%%%%%%%%%%%%%%%%%%
%Age completed fulltime education (845)
DF845_headers_all= strfind(UKB.varsHeader,'full time');
Is_DF845 = find(not(cellfun('isempty',DF845_headers_all)));
DF845_headers=UKB.varsHeader(Is_DF845);
DF845_keep_col_1=[2 3 4];
DF845_keep_col_2=Is_DF845(:,DF845_keep_col_1);
EDU_DF845=UKB.vars(:,DF845_keep_col_2);
%Remove instance 1
EDU_DF845(:,2)=[];

%Qualifications (6138) 
edu_raw = struct2array(edu_raw); %Each instance has 6 consecutive columns

%Now I change to 9 those levels we want to exclude, missing and nan 
edu_raw(edu_raw==5)=9;
edu_raw(edu_raw==6)=9;
edu_raw(edu_raw==-7)=9;
edu_raw(edu_raw==-3)=9;
edu_nan=isnan(edu_raw);
edu_raw(edu_nan==1)=9;
edu_raw_in0=edu_raw(:,(2:7));
edu_raw_in1=edu_raw(:,(8:13));
edu_raw_in2=edu_raw(:,(14:19));
edu_raw_in3=edu_raw(:,(20:25));
edu_h = [min(edu_raw_in0,[],2) min(edu_raw_in1,[],2) min(edu_raw_in2,[],2) min(edu_raw_in3,[],2)];

%Now I invert the coding to have 1=CSE and 4=university
edu_h_inv_42k=edu_h;
edu_h_inv_42k(edu_h==1)=4;
edu_h_inv_42k(edu_h==2)=3;
edu_h_inv_42k(edu_h==3)=2;
edu_h_inv_42k(edu_h==4)=1;
edu_h_inv_42k(edu_h==9)=nan;

%Here I take the eid of the 42k in edu raw
ID_edu_42k=edu_raw(:,1);

%and extract the values in scan1
[Cc,iia,iib]=intersect(ID_ws11,ID_edu_42k,'stable');

EDU_DF6138=edu_h_inv_42k(iib,:);
%Now only keep instances 0 and 2
EDU_DF6138(:,2)=[];
EDU_DF6138(:,3)=[];

%Now create one array for each category containing the related MRFs

%Lines for smoking
smoking_40k=[Smk_AST,Smk_DF1239,Smk_DF1249,Smk_DF2867,Smk_DF2887,Smk_DF2897_repl,Smk_DF3436,Smk_DF3456];

%Lines for sleep 
sleep_40k=[Sl_DF1160,Sl_DF1170,Sl_DF1180_m,Sl_DF1180_m_e,Sl_DF1180_e_m,Sl_DF1180_e,Sl_DF20517,Sl_DF2080,Sl_DF1200];

%Lines for hearing
hl_40k=[HL_DF2247,HL_DF2257,HL_DF4232,HL_DF4243];

%Lines for inflammation
infl_40k=[Infl_DF20003_ant_allerg,Infl_DF20003_ant_inf,Infl_DF20003_beclometasone,Infl_DF30710,Infl_DF30820,Infl_DF6152,Infl_DF6154];

%Lines for diabetes
diab_40k=[Diab_DF2443,Diab_DF20002,Diab_DF20003_metformin,Diab_DF30740,Diab_MF];

%Lines for pollution
pol_40k=[Pol_DF24003,Pol_DF24004,Pol_DF24005,Pol_DF24006,Pol_DF24007,Pol_DF24008,Pol_DF24011,Pol_DF24014,Pol_DF24016];

%Lines for depression
dep_40k=[Dep_DF20433,Dep_DF20434,Dep_DF20441,Dep_DF20442,Dep_DF20446,Dep_DF20510,Dep_DF20546];

%Lines for alcohol consumption
alc_40k=[Alc_DF1558,Alc_DF20403,Alc_DF20414,Alc_DF20416];

%Lines for blood pressure
bp_40k=[BP_DF20002,BP_DF2966,BP_DF4079,BP_DF4080,BP_MF,BP_DF95,BP_mean,BP_pp];

%Lines for cholesterol
chol_40k=[Ch_MF,Ch_DF20002,Ch_DF30690,Ch_DF30760,Ch_DF30780];

%Lines for body size measurements
BMI_40k=[BMI_DF21001,BMI_DF21002,BMI_DF48,BMI_DF49];

%Lines for diet/supplementation
diet_40k=[Diet_DF103990,Diet_DF1329,Diet_DF20084_B12,Diet_DF20084_D,Diet_DF20084_FO,Diet_DF20084_Folic,Diet_DF2654,Diet_DF6179];

%Lines for exercise
ex_40k=[Ex_DF3637,Ex_DF6164_mod,Ex_DF6164_vigor,Ex_DF6164_walk,Ex_DF864,Ex_DF884,Ex_DF904,Ex_DF943,Ex_DF971,Ex_DF991];

%Lines for socialisation
soc_40k=[Soc_DF6160_edu,Soc_DF6160_other,Soc_DF6160_pub,Soc_DF6160_religion,Soc_DF6160_sport,Soc_DF6160_all];
%labels={'Soc_DF6160_edu','Soc_DF6160_other','Soc_DF6160_pub','Soc_DF6160_religion','Soc_DF6160_sport','Soc_DF6160_all'}';

%Lines for education
edu_40k=[EDU_DF845,EDU_DF6138];

%Now join all the categories
all_vars_40k=[ID_ws11,alc_40k,BMI_40k,bp_40k,chol_40k,dep_40k,diab_40k,diet_40k,ex_40k,hl_40k,infl_40k,pol_40k,sleep_40k,smoking_40k,soc_40k,edu_40k];
vars_headers_40k={'Alc_DF1558_in0','Alc_DF1558_in2','Alc_DF20403','Alc_DF20414','Alc_DF20416','BMI_DF21001_in0','BMI_DF21001_in2','BMI_DF21002_in0','BMI_DF21002_in2','BMI_DF48_in0','BMI_DF48_in2','BMI_DF49_in0','BMI_DF49_in2','BP_DF20002','BP_DF2966_in0','BP_DF2966_in2','BP_DF4079_in0_r1','BP_DF4079_in0_r2','BP_DF4079_in2_r1','BP_DF4079_in2_r2','BP_DF4080_in0_r1','BP_DF4080_in0_r2','BP_DF4080_in2_r1','BP_DF4080_in2_r2','BP_MF_in0','BP_MF_in2','BP_DF95_in0_r1','BP_DF95_in0_r2','BP_DF95_in2_r1','BP_DF95_in2_r2','BP_mean_in0_r1','BP_mean_in0_r2','BP_mean_in2_r1','BP_mean_in2_r2','BP_pp_in0_r1','BP_pp_in0_r2','BP_pp_in2_r1','BP_pp_in2_r2','Ch_MF_in0','Ch_MF_in2','Ch_DF20002','Ch_DF30690_in0','Ch_DF30760_in0','Ch_DF30780_in0','Dep_DF20433','Dep_DF20434','Dep_DF20441','Dep_DF20442','Dep_DF20446','Dep_DF20510','Dep_DF20546','Diab_DF2443_in0','Diab_DF2443_in2','Diab_DF20002','Diab_DF20003_metformin','Diab_DF30740','Diab_MF_in0','Diab_MF_in2','Diet_DF103990_in0','Diet_DF103990_in2','Diet_DF1329_in0','Diet_DF1329_in2','Diet_DF20084_B12_in0','Diet_DF20084_B12_in2','Diet_DF20084_D_in0','Diet_DF20084_D_in2','Diet_DF20084_FO_in0','Diet_DF20084_FO_in2','Diet_DF20084_Folic_in0','Diet_DF20084_Folic_in2','Diet_DF2654_in0','Diet_DF2654_in2','Diet_DF6179_in0','Diet_DF6179_in2','Ex_DF3637_in0','Ex_DF3637_in2','Ex_DF6164_mod_in0','Ex_DF6164_mod_in2','Ex_DF6164_vigor_in0','Ex_DF6164_vigor_in2','Ex_DF6164_walk_in0','Ex_DF6164_walk_in2','Ex_DF864_in0','Ex_DF864_in2','Ex_DF884_in0','Ex_DF884_in2','Ex_DF904_in0','Ex_DF904_in2','Ex_DF943_in0','Ex_DF943_in2','Ex_DF971_in0','Ex_DF971_in2','Ex_DF991_in0','Ex_DF991_in2','HL_DF2247_in0','HL_DF2247_in2','HL_DF2257_in0','HL_DF2257_in2','HL_DF4232_in0','HL_DF4232_in2','HL_DF4243_in0','HL_DF4243_in2','Infl_DF20003_anti_allerg','Infl_DF20003_anti_infl','Infl_DF20003_beclomet','Infl_DF30710_in0','Infl_DF30820_in0','Infl_DF6152_in0','Infl_DF6152_in2','Infl_DF6154_in0','Infl_DF6154_in2','Pol_DF24003','Pol_DF24004','Pol_DF24005','Pol_DF24006','Pol_DF24007','Pol_DF24008','Pol_DF24011','Pol_DF24014','Pol_DF24016','Sl_DF1160_in0','Sl_DF1160_in2','Sl_DF1170_in0','Sl_DF1170_in2','Sl_DF1180_m_in0','Sl_DF1180_m_in2','Sl_DF1180_m_e_in0','Sl_DF1180_m_e_in2','Sl_DF1180_e_m_in0','Sl_DF1180_e_m_in2','Sl_DF1180_e_in0','Sl_DF1180_e_in2','Sl_DF20517','Sl_DF2080_in0','Sl_DF2080_in2','Sl_DF1200_in0','Sl_DF1200_in2','Smk_AST_in0','Smk_AST_in2','Smk_DF1239_in0','Smk_DF1239_in2','Smk_DF1249_in0','Smk_DF1249_in2','Smk_DF2867_in0','Smk_DF2867_in2','Smk_DF2887_in0','Smk_DF2887_in2','Smk_DF2897_in0','Smk_DF2897_in2','Smk_DF3436_in0','Smk_DF3436_in2','Smk_DF3456_in0','Smk_DF3456_in2','Soc_DF6160_edu_in0','Soc_DF6160_edu_in2','Soc_DF6160_other_in0','Soc_DF6160_other_in2','Soc_DF6160_pub_in0','Soc_DF6160_pub_in2','Soc_DF6160_religion_in0','Soc_DF6160_religion_in2','Soc_DF6160_sport_in0','Soc_DF6160_sport_in2','Soc_DF6160_all_in0','Soc_DF6160_all_in2','Edu_DF845_in0','Edu_DF845_in2','Edu_DF6138_in0','Edu_DF6138_in2'}';

%Load the loadings for the LIFO network

load('path/to/a/local/dir'); %This creates a variable called GLMbeta

%As GLMbeta contains values for several networks, extrat the LIFO one here
IC3=GLMbeta(4,:);
IC3=IC3';
IC3=double(nets_demean(IC3));

%Load the list of subjects for which the loadings of the LIFO network are
%available
FIDs=textread('path/to/a/local/dir.txt');

%Find subjects to be used for the analyses
[C2,ia2,ib2]=intersect(all_vars_40k(:,1),FIDs,'stable');
all_vars_40k=all_vars_40k(ia2,:);

id=all_vars_40k(:,1)
[C3,ia3,ib3]=intersect(id,UKB.subject_IDs,'stable');

%Now work on the confounders
d_Age=nets_demean(double(UKB.age1(ia2,:)));
Sex_dem=nets_demean(double(UKB.sex12(ia2,:)));
d_Age_sqr=nets_demean((nets_demean(double(UKB.age1(ia2,:))).^2));
d_Age_d_Sex=nets_demean(d_Age.*Sex_dem);
d_Age_sqr_d_Sex=nets_demean(d_Age_sqr.*Sex_dem);
Head=nets_demean(double(UKB.head_size_scaling(ib3,:)));
Z = [d_Age Sex_dem d_Age_sqr d_Age_d_Sex d_Age_sqr_d_Sex Head];
C = [1 zeros(1,size(Z,2))]';
%C=1; %Use this to run the model without confounders

%Initialize variables before running the GLM
sum_check = zeros(size(all_vars_40k,2)-1,1);
grotN = zeros(size(all_vars_40k,2)-1,1);
grotB = zeros(size(all_vars_40k,2)-1,1);
grotSSRES = zeros(size(all_vars_40k,2)-1,1);
grotMSRES = zeros(size(all_vars_40k,2)-1,1);
grotT = zeros(size(all_vars_40k,2)-1,1);
grotP = double(zeros(size(all_vars_40k,2)-1,1));
grotDF =zeros(size(all_vars_40k,2)-1,1);
warns = zeros(size(all_vars_40k,2)-1,1);
grotR2 = zeros(size(all_vars_40k,2)-1,1);
grotR = zeros(size(all_vars_40k,2)-1,1);

%Run the GLM
for i=2:size(all_vars_40k,2)
    X = nets_demean(all_vars_40k(:,i));
    reg=[X Z]; %this is for the model with confounders
    %reg=X; %Use this to run the model without confounders
    check = ~(any(isnan(reg),2) | any(isinf(reg),2));
    N = sum(check);
    sum_check(i-1,1)=sum(reg(check,1));
    Y = IC3(check,:);
    M = reg(check,:);
    betas = M\Y;
 
    res   = Y - M*betas;
    ssres = sum(res.^2);
    df = N-rank(M);
    msres = ssres/df;
    t = C'*betas*(pinv(C'*pinv(M'*M)*C)./msres).^.5;
    p=2*(tcdf(-abs(t),df));
    R2=(t.^2)./((t.^2)+df);
    r=sign(t)*sqrt(R2);
    
    grotN(i-1,1) = N;
    grotB(i-1,1) = betas(1,1);
    grotSSRES(i-1,1) = ssres;
    grotMSRES(i-1,1) = msres;
    grotT(i-1,1) = t;
    grotP(i-1,1) = p;
    grotDF(i-1,1) = df;
    grotR2(i-1,1) = R2;
    grotR(i-1,1) = r;

    [wM,wID]=lastwarn;
    if not(isempty(wM))
        warns(i-1,1)=1;
        else warns(i-1,1)=0;
    end
    warning('');

end

%This first part of the code generates the values showed in Table 2 of the
%main text

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%% SECOND STAGE %%%%%%%%%%%%%%%%%%
%The second stage took the best MRF of each category (i.e. the one
%explaining more variance) and combined them in a unique model.

%Now join all the categories
all_vars_40k=[ID_ws11 Alc_DF1558_in2 BP_MF_in2 BMI_DF48_in0 Ch_MF_in2 Diab_DF2443_in2 Ex_DF943_in2 HL_DF2257_in0 Infl_DF6154_in2 Pol_DF24016 Sl_DF1160_in2 Smk_DF1249_in2 Soc_DF6160_pub_in0];
vars_headers_40k={'Alc_DF1558_in2','BP_MF_in2','BMI_DF48_in0','Ch_MF_in2','Diab_DF2443_in2','Ex_DF943_in2','HL_DF2257_in0','Infl_DF6154_in2','Pol_DF24016','Sl_DF1160_in2','Smk_DF1249_in2','Soc_DF6160_pub_in0'}';
 
%Build the contrasts matrix for the GLM
%%%%%%%%%%%% 12 MRFs version %%%%%%%%
C(:,1) = [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]';
C(:,2) = [0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]';
C(:,3) = [0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]';
C(:,4) = [0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0]';
C(:,5) = [0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0]';
C(:,6) = [0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0]';
C(:,7) = [0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0]';
C(:,8) = [0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]';
C(:,9) = [0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0]';
C(:,10) = [0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]';
C(:,11) = [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0]';
C(:,12) = [0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0]';

%Initialize variables before running the GLM
grotT = zeros(size(C,2),1);
grotP = double(zeros(size(C,2),1));
grotR2 = zeros(size(C,2),1);
grotR = zeros(size(C,2),1);

%Run the GLM
for i=2:size(all_vars_40k,2)
    X(:,i-1) = nets_demean(all_vars_40k(:,i));
end
reg=[X Z];
check = ~(any(isnan(reg),2) | any(isinf(reg),2));
N = sum(check);
Y = IC3(check,:);
M = reg(check,:);
betas = M\Y;
res   = Y - M*betas;
ssres = sum(res.^2);
df = N-rank(M);
msres = ssres/df;
for i=1:size(C,2)
    t = C(:,i)'*betas*(pinv(C(:,i)'*pinv(M'*M)*C(:,i))./msres).^.5;
    p=2*(tcdf(-abs(t),df));
    R2=(t.^2)./((t.^2)+df);
    r=sign(t)*sqrt(R2);
    grotT(i,1) = t;
    grotP(i,1) = p;
    grotR2(i,1) = R2;
    grotR(i,1) = r;
end

F=((betas'*C*(pinv(C'*pinv(M'*M)*C))*C'*betas)./rank(C))./msres;
R2_model=F/(((size(M,1)-rank(M))/rank(C))+F);
grotB=[betas(1:size(vars_headers_40k,1),1)];

%This second part of the code generates the values showed in Table 3 of the
%main text
