0001 function [clist] = gateMake( clist, ind, x0 )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 ncell = size(clist.data,1);
0033
0034 nind = numel(ind);
0035
0036 if nind == 1
0037 clf;
0038 [y,xx] = gateHist( clist, ind);
0039 hold on;
0040
0041 if ~exist( 'x0', 'var' ) || isempty( x0 )
0042 gxx = zeros( 2, 2 );
0043
0044 for i = 1:2
0045 try
0046 tmp = ginput(1);
0047 catch
0048
0049 return;
0050 end
0051 if ~isempty(tmp)
0052 gxx(i,:) = tmp;
0053 plot( gxx(i,1)+[0,0], [min(y(y>0)),max(y)], 'r--' );
0054 end
0055 end
0056 gxx(:,1);
0057 else
0058 gxx(:,1) = x0;
0059 end
0060
0061 if isfield( clist, 'gate' )
0062 ngate = numel(clist.gate)+1;
0063 else
0064 ngate = 1;
0065 clist.gate = [];
0066 end
0067
0068 clist.gate(ngate).x = gxx(:,1);
0069 clist.gate(ngate).ind = ind;
0070 gateHist( clist, ind, xx, 'r' );
0071 hold on;
0072 plot( gxx(1)+[0,0], [min(y(y>0)),max(y)], 'r--' );
0073 hold on;
0074 plot( gxx(2)+[0,0], [min(y(y>0)),max(y)], 'r--' );
0075
0076 elseif nind == 2
0077
0078 clf;
0079 HIST_LIM = 1e3;
0080
0081 if ncell < HIST_LIM
0082 gateDot( clist, ind )
0083 else
0084 [YY,XX] = gateHist( clist, ind );
0085 backer = ag(YY);
0086 imagesc(XX{2},XX{1},cat(3, backer*0,backer*0,backer))
0087 set( gca, 'YDir', 'normal' );
0088
0089 ylabel( clist.def{ind(2)} );
0090 xlabel( clist.def{ind(1)} );
0091 end
0092
0093 hold on;
0094
0095 clist_ = gate( clist);
0096 tmp1 = clist_.data(:,ind(1));
0097 tmp2 = clist_.data(:,ind(2));
0098 dvar = [var(tmp1(~isnan(tmp1))),...
0099 var(tmp1(~isnan(tmp2)))];
0100
0101
0102
0103
0104 c_flag = 1;
0105 disp('Draw polygon. Finish by pressing return' );
0106 i = 0;
0107 xx = zeros( 100, 2 );
0108
0109 while c_flag;
0110 i = i+1;
0111 tmp = ginput(1);
0112 if isempty(tmp)
0113 if i ~= 1
0114 plot( xx([i-1,1],1), xx([i-1,1],2), 'r-' );
0115 end
0116
0117 c_flag = false;
0118 numvert = i-1;
0119 else
0120 dr = (xx(1,:)-tmp );
0121 if (i>1) && (sum((dr.^2)./dvar) < 0.001 )
0122 numvert = i-1;
0123 c_flag = false;
0124 plot( xx([1,numvert],1), xx([1,numvert],2), 'r-' );
0125 else
0126 xx(i,:) = tmp;
0127
0128 if i == 1
0129 plot( xx(1,1), xx(1,2), 'r-' );
0130 else
0131 plot( xx([i,i-1],1), xx([i,i-1],2), 'r-' );
0132 end
0133 end
0134 end
0135 end
0136
0137 xx = xx(1:numvert,:);
0138
0139 if isfield( clist, 'gate' )
0140 ngate = numel(clist.gate)+1;
0141 else
0142 ngate = 1;
0143 clist.gate = [];
0144 end
0145
0146 clist.gate(ngate).ind = ind;
0147 clist.gate(ngate).xx = xx;
0148
0149
0150 if ncell < HIST_LIM
0151 gateDot( clist, ind, 'r' );
0152 else
0153
0154 [xx_,yy_] = meshgrid( XX{2},XX{1} );
0155 mask_it = inpolygon( xx_, yy_, xx(:,1),xx(:,2) );
0156
0157 backerR = backer;
0158 backerR(~mask_it) = 0;
0159 backer(mask_it) = 0;
0160
0161 imagesc(XX{2},XX{1},cat(3, backerR,0*backer,backer));
0162 hold on;
0163 set( gca, 'YDir', 'normal' );
0164 plot( xx([1:end,1],1), xx([1:end,1],2), 'r:' );
0165
0166 end
0167 else
0168 disp( 'Error: wrong number of indices' );
0169 return
0170 end
0171
0172
0173 end