function [A,rsId,genotypes] = readData()

% Function to read in data sets corresponding to SNPs of 12 patients from
% 23andMe
% 
% No inputs are required, place the script in the same folder as the
% patient file
% 
% Note: Script is specific to this input, no other txt input will load
% properly
% 

fid = fopen('combinedPatients.txt','r');
rsId(1:566406,1) = {''};
genotypes(1:566406,12) = {''};
for i = 1:566406
    tline = fgetl(fid);
    if ~ischar(tline)
        break
    end
    fields = splitString(tline,'\t');
    
    rsId(i) = fields(1);
    genotypes(i,:) = fields(2:13);
end

A = zeros(566406,12);
for i = 1:566406
    tmp = genotypes(i,:);
    tmp = unique(tmp);
    val = ones(1,length(tmp));
    
    for j = 1:length(tmp)
        tmp2 = tmp{j};
        if ~strcmp(tmp2(1),tmp2(2))
            val(j) = 0;
        end
    end
    
    for j = 1:length(tmp)
        if val(j) == 1
            val(j) = -1;
            break
        end
    end
    
    for j = 1:length(tmp)
        loc = strmatch(tmp{j},genotypes(i,:),'exact');
        A(i,loc) = val(j);
    end
end

A = A';

function fields = splitString(string,delimiter)

if (nargin < 2)
    delimiter = '\s';
end

if iscell(string)
    stringList = string;
    for i = 1:length(stringList)
        fields{i} = splitOneString(stringList{i},delimiter);
    end
else
    fields = splitOneString(string,delimiter);
end

fields = columnVector(fields);

function fields = splitOneString(string,delimiter)

[startIndex,endIndex] = regexp(string,delimiter);

if (~isempty(startIndex))
cnt = 0;
for i = 1:length(startIndex)+1
    if (i == 1)
        if (endIndex(i) > 1)
            cnt = cnt + 1;
            fields{cnt} = string(1:endIndex(i)-1);    
        end
    elseif (i == length(startIndex)+1)
        if (startIndex(i-1) < length(string))
            cnt = cnt + 1;
            fields{cnt} = string(startIndex(i-1)+1:end);
        end
    else
        cnt = cnt + 1;
        fields{cnt} = string(startIndex(i-1)+1:endIndex(i)-1);
    end
end
else
    fields{1} = string;
end

fieldsOut = {};
cnt = 0;
for i = 1:length(fields)
    if (~isempty(fields{i}))
        cnt = cnt+1;
        fieldsOut{cnt} = fields{i};
    end
end
fields = fieldsOut;
