%%This program is designed to measure paired pulse traces from a abf file and then make comparisons to an average trace, such that a success of failure can be determined.
%In command prompt (>>) enter the following:
%>> file='c:\adrian\Minstim2\filtered\08430007Gauss1KHz19ms.abf '; exptraces=[17:219];
%(Use colons to span (from-to) sweep groups; use commas to separate sweeps (gaps))
%>> minstimdl2fil
%A graph will show up, select a region to get the SD of baseline from(two points right to left)
%First pulse will graph, select region 1, (two points from right to left)
%Second pulse will graph, select region 2, (two points from right to left)
%Average trace from successes will graph click ONCE to continue (after this step it takes awhile to calculate the sum of the %squares of the differences)
%Repeat from here:
%NOTE: clicking to the very right of any histogram window will crash the program
%NOTE2: the a key will give the coordinates of the mouse cursor in the main window (units are pA and ms).
%1) A histogram will show up, click a region to review (the traces to the left will be looked at)
%2) Then for the traces selected (previous step) all of the traces will be run through, click left for success, middle for maybe, and %right for no
%3) A second histogram will show up, red for success, blue traces for maybe or failures. Select a region to create a new %average trace, (those to the left will be used)
%4) The new average trace will show up, Click ONCE and WAIT
%5) A third histogram will show up in all blue, click a region to review (the traces to the left will be looked at using the new %average trace)
%6) Then for the traces selected(previous step) all of the traces will be run through, click left for success, middle for maybe, and %right for no
%7) A fourth histogram will show up, red for success, blue traces for maybe or failures. Click ONCE to continue
%The fifth histogram will show up. This is actually the first Histogram for Response two. Repeat steps 1-7 for Response two.
%>> datasum
%Save the analysis files in Workspace (In main Matlab window File Save Workspace As)
%In Workspace open Tracevsqual (Double Click)
%Copy and paste data table into excel/prostat. This data provides sweep numbers and if it is a success (1), failure (0), or %maybe (2 also use when two distinct events, early and late, occur together) for R1 and R2
%Also of interest:
%In Workspace r1 shows which traces (in sequential order) were used to make the first average traces (0 is not used, 1 is used) %for R1 and R2
%In Workspace, sortedindex ranks the sum of squares of differences of each individual trace from the average trace for the first %response. sortedindex2 does the same for the second response.
hold off;
%file='nameoffile.abf';
%exptraces=[];%traces to be analyzed
m=length(exptraces);
var2=10;%std from baseline
count1=0;
count2=0;
count3=0;
%reads in the data
for n=1:m;
test=import_abf10aminstimfiltered(file,exptraces(n),.01);
trace(:,n+1)=test(:,2);
end;
trace(:,1)=test(:,1);
trace2(:,1)=test(:,1);
plot (trace(:,1),trace(:,2:m+1));
clear test;
%normalization method
for n=2:(m+1);
trace(:,n)=trace(:,n)-mean(trace(1:50,n));
trace2(:,n)=trace(:,n)-mean(trace(4850:4950,n));
end;
%baseline standard deviation
plot (trace(:,1),trace(:,2:m))
b=ginput(2);
while(b(1,1)>b(2,1))
disp('please select baseline P1<P2')
b=ginput(2);
end;
for n=2:(m+1);
baseline(n-1)=std(trace(floor(b(1,1)/.01):floor(b(2,1)/.01),n));
end;
blstd=mean(baseline);
%stim artifact timing
for n=2:m+1;
[c,d]=min(trace(1:540,n));
[c,e]=min(trace2(5001:5540,n));
sa1(n-1,:)=d;
sa2(n-1,:)=e+5000;
end;
clear c; clear e;
%define region 1
plot (trace(sa1:sa1+5000,1),trace(sa1:sa1+5000,2:m))
c=ginput(2);
while(c(1,1)>c(2,1))
disp('please select region P1<P2')
c=ginput(2);
end;
%define region 2
plot (trace(sa2:sa2+5000,1),trace(sa2:sa2+5000,2:m))
d=ginput(2);
while(d(1,1)>d(2,1));
disp('please select region P1<P2')
d=ginput(2);
end;
d=floor(d/.01);
c=floor(c/.01);
%remove failures
for n=2:m+1;
%if (min(trace(c(1,1):c(2,1),n))>(blstd*limbase))
if (abs(min(trace(c(1,1):c(2,1),n)))<(blstd*var2))
count1=count1+1;
r1(n,1)=0;
else
r1(n,1)=1;
end;
%if (min(trace(d(1,1):d(2,1),n))>(blstd*limbase))
if (abs(min(trace2(d(1,1):d(2,1),n)))<(blstd*var2))
r1(n,2)=0;
count2=count2+1;
else
r1(n,2)=1;
end;
end;
%sets up arrays with the appropriate sets of data
clear n;
ptrace3=trace;
for n=2:m+1;
ptrace1(:,n)=trace(:,n)*r1(n,1);
ptrace2(:,n)=trace2(:,n)*r1(n,2);
if ((r1(n,1)==0)&&(r1(n,2)==0));
ptrace3(:,n)=0;
count3=count3+1;
else
end;
end;
plot (trace(:,1),ptrace3(:,2:m+1))
%creates an average trace ?? representational
sumtrace1=sum(ptrace1')/sum(r1(:,1));
sumtrace2=sum(ptrace2')/sum(r1(:,2));
plot (sumtrace1); hold on; plot (sumtrace2); hold off;
ginput(1);
min12(m+1)=0; min34(m+1)=0;
%find min of each curve
[a,testmin1]=min(sumtrace1(1,c(1,1):c(2,1)));
min12(1)=testmin1+c(1,1);
[a,testmin2]=min(sumtrace2(1,d(1,1):d(2,1)));
min34(1)=testmin2+d(1,1);
%normalize traces
%find mintime point for each trace
for n=2:m+1;
[a,testmin1]=min(trace(c(1,1):c(2,1),n));
min12(n)=testmin1+c(1,1);
[a,testmin2]=min(trace2(d(1,1):d(2,1),n));
min34(n)=testmin2+d(1,1);
end;
%adjust trace for trace to average
for n=2:m+1;
adjustedtrace(:,n)= (trace(:,n))*mean(sumtrace1(1,(min12(1)-50):(min12(1)+50)))/mean(trace((min12(n)-50):(min12(n)+50),n));
adjustedtrace2(:,n)=(trace2(:,n))*mean(sumtrace2(1,(min34(1)-50):(min34(1)+50)))/mean(trace2((min34(n)-50):(min34(n)+50),n));
%plot(1:12000,adjustedtrace(:,n),1:12000,sumtrace1)
% ginput(1);
end;
%adjust trace average to trace
for n=2:m+1;
adjustedsum(:,n)= (sumtrace1(1,:)')*mean(trace((min12(n)-50):(min12(n)+50),n))/mean(sumtrace1(1,(min12(1)-50):(min12(1)+50)));
adjustedsum2(:,n)=(sumtrace2(1,:)')*mean(trace2((min34(n)-50):(min34(n)+50),n))/mean(sumtrace2(1,(min34(1)-50):(min34(1)+50)));
end;
clear ptrace1 ptrace2 ptrace3 testmin1 testmin2
%clear n sa1 c d a b baseline blstd
%repeat???? from here
n=2;
len=size(adjustedtrace);
%calculate differences
for n=2:m+1;
n
for a=1:len(1);
est(n,a)=(sumtrace1(1,a)-adjustedtrace(a,n))^2;
est2(n,a)=(sumtrace2(1,a)-adjustedtrace2(a,n))^2;
end;
end;
%calculate sum of squares
sumsqu12=sum(est(:,c(1,1):c(2,1))');
sumsqu34=sum(est2(:,d(1,1):d(2,1))');
data=log(sumsqu12(2:m+1));
data2=log(sumsqu34(2:m+1));
%for pulse number one
%calculate histogram
sumtrace=sumtrace1;
[choice, sumtrace1b, histamp, histindex,temp3,sortedindex,sortvarify]=traceselectionp(trace,m,data,adjustedtrace,adjustedsum,sumtrace); %needs (trace*,m*,data*n,adjustedtrace*n,sumtrace),
%returns(choice, sumtrace1b, histamp, histindex)
clear sumtrace;
%creates list of min12 time for the traces
%plots the averaged trace, versus peak time for those selected traces
%adjust trace average to trace
[a,testmin1]=min(sumtrace1(1,c(1,1):c(2,1)));
min12(1)=testmin1+c(1,1);
for n=2:m+1;
%adjustedsum(:,n)= (sumtrace1b(1,:)')*trace(min12(n),n)/sumtrace1b(1,min12(1));
adjustedsum(:,n)= (sumtrace1b(1,:)')*mean(trace((min12(n)-50):(min12(n)+50),n))/mean(sumtrace1b(1,(min12(1)-50):(min12(1)+50)));
end;
for j=1:m;
sortemin12time(j)=min12(sortedindex(j)+1);
end;
plot (sa1:sa1+5000,sumtrace1(sa1:sa1+5000))
hold on;
plot (sortemin12time(1:size(sortvarify,2)),10*(sortvarify(1:size(sortvarify,2))),'o')
hold off;
ginput(1);
%loops the selection parameters
count6=0;
while (count6==0)
var1=2/3;
n=2;
%calculate differences
for n=2:m+1;
for a=1:len(1);
estb(n,a)=(sumtrace1b(1,a)-adjustedtrace(a,n))^2;
end;
end;
%calculate sum of squares
sumsqu12b=sum(estb(:,c(1,1):c(2,1))');
datab=log(sumsqu12b(2:m+1));
%calculate histogram
sumtrace=sumtrace1b;
[choice, sumtrace1b, histamp, histindex,temp3,sortedindex,sortvarify]=traceselectionp(trace,m,datab,adjustedtrace,adjustedsum,sumtrace); %needs (trace*,m*,data*n,adjustedtrace*n,sumtrace),
%needs (trace*,m*,data*,adjustedtrace*,sumtrace),
%returns(choice, sumtrace1b, histamp, histindex)
clear sumtraceb sumtrace;
count6=count6+1;
end;
minstimdl2filb
%removing sets of variables from memory
clear j n temp3;
clear histamp histindex choice data;
clear var2 ptrace1 ptrace2 ptrace3 sa2 testmin1 testmin2
clear sa1 c d a b baseline blstd m
clear count1 count2 count3 min34 sumtrace2 test;