function qdot_track %========================================================= % November 4, 2015 % This code is for finding trajectories of fluorescent dye/quantum dot from %multiple region of interests. % This code assumes that the signal (Qdot or Cy3) is moving in the y-direction % How to use it? %(1) Open the raw movie file using ImageJ. %(2) "Analyze" tab -> "Set Measurements" -> Select "Bounding rectangle" %(3) Designate region of interest (ROI) by dragging the mouse over it. %(4) Ctrl + t %(5) Repeat until regions of all of the events were selected. %(6) In the "ROI Manager" window of ImageJ, click "Measure". This will % give you x- and y-coordinates, width, height of each ROI. %(7) Save it as "(File name).txt" %(8) Now we need to find out background intensity level right next to % each ROI. You can do it by "Ctrl + t" %(9) Save it (only for background ROI - NOT including signal ROI) as % "(Another file name).txt" %(10) Let's say we have 10 DNAs. Then, "(File name).txt" contains % coordinate information for 10 signals, and "(Another file name).txt" % contains those for 10 background levels. %(11) Save averaged image. This can be done by "Image" tab -> "Stacks" -> % "Z Project". The averaged image must be saved under the name "AVG_ % (File name).tif" %(12) Save the raw data movie file as Image sequence. For example, if your % movie file consists of 100 frames, save individual frame as a seperate % TIFF file (= 100 files) %(13) Now Open this "qdot_track.m" Matlab file, and run it. % Details on the code is commented throughout this code. %========================================================= clear all close all % User Inputs % select directory with images to be analyzed filepath=uigetdir('','select directory that has images to be analyzed'); % change matlab "Current Folder" to the selected directory cd(filepath) % load only tif image files D=dir([filepath,filesep,'*.tif']); % get image file prefix % Assume that my image files are named as "abc100mM-0001.tif" % "abc100mM-0002.tif",..., and so on. If your file names are named % differently, simply change below two lines accordingly. fname=D(1).name(1:regexp(D(1).name,'mM-')+2); fnamename=D(1).name(1:regexp(D(1).name,'mM-')+1); % beginning frame frame_min=input('starting frame = '); % ending frame frame_max=input(['ending frame (',num2str(sum(strncmp(fname,{D.name},length(fname)))),... ' images available) = ']); % frame increment step=input('frame increment = '); % ccd integration time in secs int_time=input('time per frame (seconds) = '); % select ROI text file to be analyzed roi_file=uigetfile('*.txt','select Qdot ROI.txt'); % select Qdot background ROI text file % uniform background has only one roi in the file % non-uniform background has multiple rois in the file bgr_roi_file=uigetfile('*.txt','select Qdot background ROI.txt'); % enter a threshold for fitting a particular projection thres=input('threshold (multiplication factor above background) = '); % directory to save analyzed data and figures outdir=uigetdir('','select directory to save output'); outfname=input('name output (e.g. ''kymo1''): '); % read ROI data ROIs=dlmread(roi_file,'\t',1,0); bgrROI=dlmread(bgr_roi_file,'\t',1,0); % for uniform background, load roi information before going through all of % the images if size(bgrROI,1)==1 % Change index of ROIs that marked in ImageJ to matlab format bgrxmin=bgrROI(2)+1; %minimum x pixel number bgrymin=bgrROI(3)+1; %minimum y pixel number bgrwidth=bgrROI(4); %change in x (pixels) bgrheight=bgrROI(5); %change in y (pixels) bgrxmax=bgrxmin+bgrwidth-1; %maximum x pixel number bgrymax=bgrymin+bgrheight-1; %maximum y pixel number end for ii=1:size(ROIs,1) % Change index of ROIs that marked in ImageJ to matlab format xmin=ROIs(ii,2)+1; %minimum x pixel number ymin=ROIs(ii,3)+1; %minimum y pixel number width=ROIs(ii,4); %change in x (pixels) height=ROIs(ii,5); %change in y (pixels) xmax=xmin+width-1; %maximum x pixel number ymax=ymin+height-1; %maximum y pixel number % for non-uniform background, load bgrroi information for every ROI % assume length of ROIs and bgrROI are the same if size(bgrROI,1)>1 % Change index of ROIs that marked in ImageJ to matlab format bgrxmin=bgrROI(ii,2)+1; %minimum x pixel number bgrymin=bgrROI(ii,3)+1; %minimum y pixel number bgrwidth=bgrROI(ii,4); %change in x (pixels) bgrheight=bgrROI(ii,5); %change in y (pixels) bgrxmax=bgrxmin+bgrwidth-1; %maximum x pixel number bgrymax=bgrymin+bgrheight-1; %maximum y pixel number end % create a new ROI of nonspecific bound QDot using z-projection image % with prefix "AVG" created in ImageJ % load z-projection image % 2012-07-27 LS: modified for HK's images img=imread(['AVG_' fnamename],'tiff'); % clips image using user defined rectangle: x=col, y=row img_crop=img(ymin:ymax,xmin:xmax); % manually select a new ROI of the nonspecific bound QDot imshow(img_crop,[],'InitialMagnification',500) imcontrast(gca) % set dimension constraints to the new ROI fcn=makeConstrainToRectFcn('imrect',[1,width],[1,height]); ROI2crop=cell(1,[]); % select multiple new ROIs to be cropped out in a loop ii; % Sometimes, nonspecifically-bound Qdots will bother you and you would %like to remove it. Below are written for this purpose. counter=input('select ROI to be cropped? (yes=1, no=0) = '); %counter=0; while counter==1 h=imrect(gca,'PositionConstraintFcn',fcn); ROI2=wait(h); ROI2crop{end+1}=createMask(h); counter=input('select more ROI? (yes=1, no=0) = '); end close all for i=frame_min:step:frame_max % 2012-07-27 LS: modified name for HK's images %Constructs file name --> meta vue default if i<10 name=[fname '000' int2str(i)]; else if i>9 & i<100 name=[fname '00' int2str(i)]; else if i> 99 & i<1000 name=[fname '0' int2str(i)]; else name=[fname int2str(i)]; end end end % loads image data=imread(name,'tiff'); % calculates dark background using the given ROI back=mean(mean(data(bgrymin:bgrymax,bgrxmin:bgrxmax))); % subtracts background data2=imsubtract(data,back); % clips image using user defined rectangle: x=col, y=row data2_crop=data2(ymin:ymax,xmin:xmax); % crop out any nonspecific bound QDot in ROI using frame_min data3=data2_crop; if ~isempty(ROI2crop) for k=1:size(ROI2crop,2) data3(ROI2crop{k}==1)=0; end end % calculates projection to y-axis j=(i - frame_min)/step+1; proj(:,j)=sum(data3,2); % Fits a gaussian to projection data % Creates axis for fitting X=1:1:height; X=X'; % Determine initial parameters % peak center - y_o [C,I]=max(proj(:,j)); % if multiple maximum is found, return the first one's index y_o=I(1,1); % width along y-axis w=5; % offset off=mean(proj(:,j)); % creates array of initial guesses p0=[y_o w C off]; if C >=thres*((mean(proj(1:y_o-w,j))+mean(proj(y_o+w:end,j))/2)) % fits single Gaussian to projection p1=lsqcurvefit('gauss',p0,X,proj(:,j),[],[]); % creates array of fit parameters x_traj(j)=p1(1); else x_traj(j)=0; end end % constructs time axis Xi=frame_min-1:step:frame_max-1; Ti=int_time.*Xi; % removes "zeros" from x_traj by linear interpolation [Xo,col,v]=find(x_traj); x_traj_int=interp1(step*(col-1)+frame_min,v,Xi,'linear'); % Time (column #1) and x trajectory (column #2) data Ti_HJ=Ti'; x_traj_HJ=x_traj'; Time_traj(:, 1) = Ti_HJ; Time_traj(:, 2) = x_traj_HJ; % save projection %save([outdir '\' outfname '_proj_ROI-' int2str(ii) '.txt'],'proj','-ascii') save([outdir '\' outfname '_proj-' int2str(ii) '.txt'],'proj','-ascii') % save x trajectory %save([outdir '\' outfname '_x_traj_ROI-' int2str(ii) '.txt'],'x_traj','-ascii') save([outdir '\' outfname '_x_traj-' int2str(ii) '.txt'],'x_traj','-ascii') % save interpolated x trajectory %save([outdir '\' outfname '_x_traj_int_ROI-' int2str(ii) '.txt'],'x_traj_int','-ascii') save([outdir '\' outfname '_x_traj_int-' int2str(ii) '.txt'],'x_traj_int','-ascii') % save time axis %save([outdir '\' outfname '_qdot_time_ROI-' int2str(ii) '.txt'],'Ti','-ascii') save([outdir '\' outfname '_qdot_t-' int2str(ii) '.txt'],'Ti','-ascii') % save time versus x trajectory: HJ added on 20120811 %save([outdir '\' outfname '_qdot_time_versus_trajectory-' int2str(ii) '.txt'],'Time_traj','-ascii') save([outdir '\' outfname '_qdot_t_vs_traj-' int2str(ii) '.txt'],'Time_traj','-ascii') % plot x trajectory over time figure,plot(Ti,x_traj,'b.','MarkerSize',8) xlabel('Time [s]') ylabel('Position Tracjectory [pixel]') % save x trajectory plot saveas(gca, [outdir '\' outfname '_qdot_traj-' int2str(ii) '_fig.tif'], 'tif') saveas(gca, [outdir '\' outfname '_qdot_traj-' int2str(ii) '_fig.fig'], 'fig') % plot interpolated x trajectory over time figure,plot(Ti,x_traj_int,'r.','MarkerSize',8) xlabel('Time [s]') ylabel('Position Tracjectory [pixel]') % save interpolated x trajectory plot saveas(gca, [outdir '\' outfname '_qdot_traj_int-' int2str(ii) '_fig.tif'], 'tif') saveas(gca, [outdir '\' outfname '_qdot_traj_int-' int2str(ii) '_fig.fig'], 'fig') clear proj clear x_traj clear x_traj_int close all end