-
Notifications
You must be signed in to change notification settings - Fork 0
/
NINGA_kin.m
100 lines (88 loc) · 2.96 KB
/
NINGA_kin.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
function ped = NINGA_kin(pedfile,phifile)
% Load the pedigree
%
% Usage for pedigree:
% pedigree = loadped('pedindex.csv','phi2.csv')
%
% Usage for empiricalkinship:
% pedigree = loadped('phi2.csv')
% The input files must have been converted to CSV files.
% The output is a struct that contains the headers for
% the pedindex, the pedindex itself (as a double array)
% and the matrices Phi2 and Delta7.
%
% _____________________________________
% Anderson M. Winkler., Habib Ganjgahi
% The University of Warwick/ Statistic Department
% Jan 2015
[pathstr,name,ext] = fileparts(pedfile);
if strcmpi(ext,'.csv') %it means that the phi2 and pedigree are in Solar format
% Read the pedindex.csv file
fid = fopen(pedfile,'r');
pedindex = textscan(fid,repmat('%s',[1 8]),'Delimiter',',');
fclose(fid);
% Move the headers to a separate variable
headers = cell(size(pedindex'));
for c = 1:numel(pedindex),
headers{c} = pedindex{c}{1};
pedindex{c}(1) = [];
end
% Subject IDs
cid = strcmpi('ID',headers);
id = pedindex{cid};
% Convert char to num and to an array
pidx = zeros(numel(pedindex{1}),numel(pedindex));
for c = 1:numel(pedindex),
pedindex{c} = str2double(pedindex{c}(:));
pidx(:,c) = pedindex{c}(:);
end
% Read the phi2.csv file
fid = fopen(phifile,'r');
phitable = textscan(fid,repmat('%f',[1 4]),'Delimiter',',');
fclose(fid);
% Organise as simple table
phitable = [phitable{1} phitable{2} phitable{3} phitable{4}];
% Reorganise as symmetric matrices
tmpvar = phitable(:,1:2); N = max(tmpvar(:));
Phi2 = zeros(N); % Kinship
Delta7 = zeros(N); % Jacquard D7 coefficients
% For each row in the phi2.gz file
for r = 2:size(phitable,1),
Phi2(phitable(r,1),phitable(r,2)) = phitable(r,3);
Phi2(phitable(r,2),phitable(r,1)) = phitable(r,3);
Delta7(phitable(r,1),phitable(r,2)) = phitable(r,4);
Delta7(phitable(r,2),phitable(r,1)) = phitable(r,4);
end
% Organize the output
ped.hdr = headers;
ped.id = id;
ped.pidx = pidx;
ped.phi2 = Phi2;
ped.d7 = Delta7;
else
% %grm from binary file
% tmp = dlmread(pedfile);
% pedindex = tmp(:,1);
% Phi2 = dlmread(phifile);
% ped.phi2 = Phi2;
% ped.id = pedindex;
fid = fopen(pedfile,'r');
pedindex = textscan(fid,'%s %s');
fclose(fid);
% Read the phi2.csv file
% phifile = gunzip(fullfile(pwd,phifile));
fid = fopen(phifile,'r');
phitable = textscan(fid,'%f %f %s %f');
fclose(fid);
phitable = [phitable{1} phitable{2} phitable{4}];
tmpvar = phitable(:,1:2); N = max(tmpvar(:));
Phi2 = zeros(N); % Kinship
% For each row in the phi2.gz file
for r = 1:size(phitable,1),
Phi2(phitable(r,1),phitable(r,2)) = phitable(r,3);
Phi2(phitable(r,2),phitable(r,1)) = phitable(r,3);
end
ped.phi2 = Phi2;
ped.id = pedindex{2};
end
end