%% Data Analysis

% Get Photon streams through import function
 FRET=stimporter(fileindex)

%% Discretazation if neccessary
photonizer %Automatic
%or
FNorm = normalize(odata) %Manual selected threshold
%% Error correction
% calculate background and crosstalk correction factors through bleaching markers
goodCFRET=errorcalc(goodCFRET)

% apply correction factors to trajectory
corTEST=errorcorr(goodCFRET)

% sort for convenience in order of trajectorie length 
for i=1:length(goodCFRET)
sortTime(i)=length(goodCFRET(1,i).counts)
end
[sortedTime Xperm]=sort(sortTime)
for j=1:length(goodCFRET)
goodSCFRET(:,j)=goodCFRET(:,Xperm(j));
end
% 
for i=1:length(goodSCFRET)
sortTime2(i)=goodSCFRET(1,i).A_bleached;
end
[sortedTime2 Xperm2]=sort(sortTime2)
for j=1:length(goodSCFRET)
goodTSCFRET(:,j)=goodSCFRET(:,Xperm2(j))
end

%% State recognition through naive efficiency trajectory
tsInt = timewindow(tsin,n)                                   
effiT = effer(tsInt)

for i=1:length(CSingleStates)
CSingleStates(1,i).ints10=timewindow(CSingleStates(1,i).counts,10);
CSingleStates(1,i).ints20=timewindow(CSingleStates(1,i).counts,20);
CSingleStates(1,i).ints50=timewindow(CSingleStates(1,i).counts,50);
CSingleStates(1,i).ints100=timewindow(CSingleStates(1,i).counts,100);
end

for i=1:length(CSingleStates)
CSingleStates(1,i).eff10=effer(CSingleStates(1,i).ints10);
CSingleStates(1,i).eff20=effer(CSingleStates(1,i).ints20);
CSingleStates(1,i).eff50=effer(CSingleStates(1,i).ints50);
CSingleStates(1,i).eff100=effer(CSingleStates(1,i).ints100);
end

%% for many states per trajectorie use
SortedStateChanges=Statelizer(state_INFO)
%reorder $SortedStateChanges to $SChange
SingleStates=Singelizer(nuFRET, visualStates, SChange)

%% for trajectories staying in one state

for i=1:13
CSingleStates(i).counts=goodTSCFRET(3,i).counts(1:goodTSCFRET(1,i).A_bleached,1:2);
CSingleStates(i).names=goodTSCFRET(3,i).names;
CSingleStates(i).part='1/1';
end

%% Converting to single photon trajectory
for i=1:length(CSingleStates)
CSingleStates(1,i).maxPhoton=max(max(CSingleStates(1,i).counts));
CSingleStates(3,i).monocounts=SinglePhotonizer(CSingleStates(1,i).counts);
end


% Enhancement: HMM for efficiency estimation
multifret(exp_data,tw1,tw2,tw3,start,time)
fret_filter



%% Photon Analysis
%  NProbe Efficiency

for i=1:length(CSingleStates)
CSingleStates(3,i).NPmaxPeff=NPanalyse(CSingleStates(3,i).monocounts,CSingleStates(1,i).maxPhoton,10);
end

for i=1:length(CSingleStates)
CSingleStates(3,i).NPmaxmax=NPanalyse(CSingleStates(3,i).monocounts,CSingleStates(1,i).maxPhoton,2*CSingleStates(1,i).maxPhoton);
end

for i=1:length(CSingleStates)
CSingleStates(3,i).NPmaxmax=NPanalyse(CSingleStates(3,i).monocounts,CSingleStates(1,i).maxPhoton,2*CSingleStates(1,i).maxPhoton);
end
for i=1:length(CSingleStates)
CSingleStates(3,i).NPmaxund2=NPanalyse(CSingleStates(3,i).monocounts,CSingleStates(1,i).maxPhoton,CSingleStates(1,i).maxPhoton+2);
end


% State recognition through efficiency Analysis
%TransitionMatrix from Trajectory
TransM=TransitionMatrixFromTraj(conIn, samples, stepsize, countmode)	

% Divide Trajectories into 
% I) Visual recognized 
% and/or 
% II) automatic clustered States. 
% For uncorrected Photonstreams the bleaching startingpoint is set as end of trajectory. 
% Make a list of trajectories showing state changes. 

visualStates = [] ;

SortedStateChanges=Statelizer(state_INFO)
SingleStates=Singelizer(nuFRET, visualStates, SChange)
% Visual States are imported through marking every change point with the
% DataTip tool, export the cursor_info structure of all trajectories to create $StateChange

StateChange=Statelizer(cursor_info);

SingleStates = Singelizer(oxyFRET, visualStates, SChange );
StateAnalysis=SingleStateAnalyser(struct SingleStates)

%% HMM Analysis
% random Viterbi path
RandVitPath=VPGenerate(length,TransitionMatrix)

% Viterbipath from Transition Matrix   
TMviterbiPfad=randViterbi(States,length)

%% NProbe Analysis
NPAnalysis=Npanalyse(Np, tw)


