%%%This function generates a matrix containing all the SNPs in ChrNum and
%%%the nambers of the samples each one appears
function  [TotalMatrix]=SampCompare(SaVec,ChrNum,NovelOrCa,LocationVec)
n=1;
for i=1:length(LocationVec)
fid = fopen(LocationVec{i});
C =textscan(fid, '%s %f %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s');
fclose(fid);
%Generating a matrix of positions and  sample numbers for each SNP in ChrNum
for j=1:length(C{1,1})
     if  (strcmp(C{1,1}(j), ChrNum)==1)  
      matrix(n,1)=C{1,2}(j);%position
      matrix(n,2)=SaVec(i);%sample number
      n=n+1;
     end 
end
 C=[];
end
%Arranging the matrix such that the samples containing each SNP in charNum
%will be mentioned in the columns following each SNP  

for i=1:length(matrix(:,1))
    pos=matrix(i,1);
    n=1;
    sa(n)=matrix(i,2);
    n=n+1;
    for j=i+1:length(matrix(:,1))
        if (pos==matrix(j,1) && not(matrix(j,1)==0))
            sa(n)=matrix(j,2);
            matrix(j,1)=0;
            n=n+1;
        end
    end
    CommonMatrix(i,1)=pos;
    for k=1:length(sa)
        CommonMatrix(i,k+1)=sa(k);
    end 
    sa=[];
end

%delete repeated matrix rows containing the same position
  for L=1:length(CommonMatrix(:,1));
    if CommonMatrix(L,1)==0
for k=1:length(CommonMatrix(L,:))
  CommonMatrix(L,k)=0;  
  end
    end
  end
  i=1;
  n=1;
while i<=length (CommonMatrix(:,1)) 
    if not(CommonMatrix(i,1)==0)
        for j=1:length(CommonMatrix(i,:))
            TotalMatrix(n,j)=CommonMatrix(i,j);
        end
         n=n+1;
    end
     i=i+1;
        end