function [ vpath,entropy ] = hmm_fun( no_founders,founder_matrix,location_vector )
%UNTITLED5 Summary of this function goes here
%   Detailed explanation goes here
edim=dim(founder_matrix);
%Emission Probability
delta=0.05;
errorval=0.1;
E=zeros(edim(1),no_founders+1);
genome_length=135000000;
no_cm=500;
%filestart='Transion_matrix_';
V=zeros(edim(1),no_founders+1);
pointer=zeros(edim(1),no_founders+1);
F=zeros(edim(1),no_founders+1);
B=zeros(edim(1),no_founders+1);
  %Emission matrix
    for j=1:no_founders+1
        if j==no_founders+1
            E(i,j)=delta;
        elseif founder_matrix(i,j)==1
                E(i,j)=1-errorval;
        else
            E(i,j)=errorval;
        end
    end
for i=1:edim(1)
    q=edim(1)+1-i;
    if i==1
        last_SNP_f=0;
        last_SNP_B=0;
   else
        last_SNP=location_vector(i-1);
        last_SNP=location_vector(i+1);
    end
    no_generations=10;
    phi_f=(location_vector(i)-last_SNP_f)*no_cm/genome_length*no_generations;
    phi_b=(-location_vector(q)+last_SNP_B)*no_cm/genome_length*no_generations;
  

    %Viterbi Algorithm
    for t=1:no_founders+1
        
		if (i==1) 
            V(i,t)= log(E(i,t));
        else
            for k=1:no_founders+1
                if t==k
                T(k,t)=phi_f+(1-phi_f)/(no_founders+1);
            else
                T(k,t)=(1-phi_f)/(no_founders+1);
                end
            end
                vki=V(i-1,t)+log(T(k,t));
                if (vki>max_v) 
                    max_v=vki;
                    pointer(i,:)=0;
                    pointer(i,k)=1;
                end
                if (vki==max_v) 
                    pointer(i,k)=1;
                    
                end
                if k==1
                    max_v=vki;
                    pointer(i,k)=1;
                end
                
			V(i,t)=log(E(i,t))+max_v;
            end
        end
    end
    %forward algorithm
%     for s=1:no_founders+1
% 			  foundersum=0;
% 			for k=1:no_founders+1
% 				if (i==0) 
%                     foundersum=0;
%                 else
%                     for k=1:no_founders+1
%                 for k=1:no_founders+1
%                 if s==k
%                 T(k,s)=phi_f+(1-phi_f)/(no_founders+1);
%                 else
%                 T(k,s)=(1-phi_f)/(no_founders+1);
%                 end
%                 end
%                     foundersum=foundersum+F(i-1,k)+log(T(k,s));
%                 end
%             end
%                 if (i==0) 
%                     F(i,s)=log(E(i,s));
%                 else F(i,s)=log(E(i,s))+foundersum;
%                 end
%    end
    %backward algorithm
% 		for r=1:no_founders+1
%             if s==k
%                 T(k,s)=phi_b+(1-phi_b)/(no_founders+1);
%                 else
%                 T(k,s)=(1-phi_b)/(no_founders+1);
%                 end
% 				for k=1:no_founders+1
% 					if (q==edim(1)) 
%                         foundersum=0;
% 					else foundersum=foundersum+log(T(q+1,k))+log(B(q+1,k));
%                     end
%                 end
%                 if (q==edim(1))
%                     B(q,r)=log(Emission(q,r));
% 				else B(q,r)=log(Emission(q,r))+foundersum;
%                 end
%         end
%end

%forward and back probability
P=zeros(edim(1),no_founders+1);
vpath=zeros(edim(1),1);
for i=1:edim(1)
%     for J=1:no_founders+1
%         for K=1:no_founders+1
%             if (i==1)
%                 %F(0)=1
%                 it=num2str(i+1);
%             new_filename = [filestart it ];
%             load(new_filename);
%             T2=T;
%                 P(i,l)=P(i,l)+log(B(i+1,K))+log(T2(K,l))+log(E(i,l));
%             elseif (i==edim(1))
%                 %B(0)=1
%                 it=num2str(i-1);
%             new_filename = [filestart it ];
%             load(new_filename);
%             T1=T;
%                 P(i,l)=P(i,l)+log(F(i-1,J))+log(T1(J,l))+log(E(i,l));
%             else
%                 it=num2str(i-1);
%             new_filename = [filestart it ];
%             load(new_filename);
%             T1=T;
%             it=num2str(i+1);
%             new_filename = [filestart it ];
%             load(new_filename);
%             T2=T;
%             P(i,l)=P(i,l)+log(F(i-1,J))+log(T1(J,l))+(B(i+1,K))+log(T2(K,l))+log(E(i,l));
%             end
%         end
%     end
    %Viterbi path setting
    q=edim(1)+1-i;
    path_op=[];
    for h=1:no_founders
        if pointer(q,h)==1
            path_op=[path_op h];
        end
    end
    if q==edim(1)
    else
        if pointer(q,vpath(q+1))==1 
            vpath(q)=vpath(q+1);
        end
        op_length=length(path_op);
        perm=randperm(op_length);
        vpath(q)=path_op(perm(1));
    end
end
%entropy
entropy=zeros(edim(1),1);
for i=1:edim(1)
    pathp=0;%P(i,vpath(i));
    entropy(i)=0;%pathp*exp(pathp);
end


end

%  %Transision matrix
%     T=zeros(no_founders+1,no_founders+1);
%     phi=exp(-recombination_rate);
%     for j=1:no_founders+1
%         for k=1:no_founders+1
%             if j==k
%                 T(j,k)=phi+(1-phi)/(no_founders+1);
%             else
%                 T(j,k)=(1-phi)/(no_founders+1);
%             end
%         end
%         file2=num2str(i);
%         new_filename = [filestart file2 ];
%         save(new_filename,'T');
%     end

