2P imaging preprocess functions I used in RinbergLab
This preprocessing packages is written to analyze odor and light stimulation responses of mitral/tufted cells and glomeruli. I have inherited some parts from Hirofumi Nakayama's imaging analysis packages (check https://github.com/olfa-lab/2P_Pipeline and https://github.com/hirofuminakayama/Imaging).
We used followings for data collection
Imaging: ScanImage 2019. Data are in int16 and saved as tif files.
Behavior and Stimulus Control: Voyeur ( https://github.com/olfa-lab/Voyeur ) and olfactometers (https://github.com/olfa-lab/olfactometry ) developed in rinberglab. Data (sniff signal,licks, frametrigger, metadata for stimulus and timing information are in) saved as H5 file.
optional: As our behavior box during its communication with PC can drop frames and has low sampling rate, we occasionally use Wavesurfer to record critical parameters. Critical parameters are sniff signal and frame trigger generated by ScanImage.
Most of analysis relies on the following packages:
NORMCorre: This package is used for motion correction. https://github.com/flatironinstitute/NoRMCorre
Wavesurfer: This package is required for blanked odor recording. Each trial has its own TIF file. For continuous recording, this package is not necessary. https://github.com/JaneliaSciComp/Wavesurfer
Breathmetrics: This package is optional and used for sniff processing. It is useful if you want to realign inhalation onset. Voyeur's inhalation onset can be slightly inaccurate. https://github.com/zelanolab/breathmetrics
Steps for preprocessing:
- Motion correction to register the field to reference field
- Drawing ROIs
- Calculating dFF and sniff responses for each trials
At the end you will get 'fieldname_S_v73' .MAT file
Do necessary changes in bp_motioncorrection_mk.s file. Example batch file to create slurm jobs for motion correction. Critical parameters are current directory and array range (this number should be higher than maximum tiffs need to be corrected)
#SBATCH --array=0-250
module purge
module load matlab/R2018a
cd /gpfs/scratch/yourID/2Pdata/SC18/220908/
tifs=(/gpfs/scratch/yourID/2Pdata/SC18/220908/SC18_220908_field1stim*.tif)
tif=${tifs[$SLURM_ARRAY_TASK_ID]}
{
echo $tif
matlab -nodisplay -r "normcorremotioncorrection_single('$tif','Ref.tif'); exit"
} > $tif.log 2>&
It relies on normcorremotioncorrection_single.m file to be in the path. Do necessaary changes in following lines so that it can reach to NoRMCorre.
addpath(genpath(fullfile('/gpfs/scratch/yourID','NoRMCorre-master')));
Ref.tif is your best tif file that imaging session can be aligned. You can acquire 200 frames before session starting odor/stim trials to create your own single Ref.tif file.
I use FIJI to draw ROIs
https://imagej.net/software/fiji/
-
Be cautious when drawing and avoid including dendrites from other cells.
-
After drawing all the regions of interest (ROIs), use the "combine" and then "split" functions (under properties in ROI manager window). All ROIs will be labeled from top-to-bottom order.
-
Save the ROIs with the following naming convention: mouseID_YYMMDD_session_ROISet.zip
Acquisition is discrete (this is our regular way of data acqusition )
Must have: ScanImage Tiff Files and Voyeur h5 File and Wavesurfer H5
Optional : Breathmetric ('inh_realign' get_prepocessed_odordata/get_prepocessed_stimdata parameter rely on this package)
folderpath = '/gpfs/scratch/karadm01/2Pdata/OMP02/230207';
roifile ='OMP02_230207';
fieldname = 'OMP02_230207_HighConc';
VoyeurH5_file = 'OMP02_230207_HighConc_1_01';
WSfieldname = 'OMP02_230207_HighConcWS';
get_prepocessed_odordata('tiffpath',folderpath, 'fieldname', fieldname, 'roiname', roifile,'VoyeurH5_name', VoyeurH5_file, 'WSfieldname',WSfieldname,'isOdor',true)
addpath(genpath('/gpfs/scratch/yourID/ImagingPreProcess/'))
%%
tiffpath = '/gpfs/data/rinberglab/Jon/JG44524/230614';
roifile ='JG44524_230614_glomfield4_stim';
fieldname = 'JG44524_230614_glomfield4_stim_00001';
VoyeurH5_file = 'JG44524_230614_glomfield4_stim_1_01';
get_prepocessed_stimdata('tiffpath',folderpath, 'fieldname', fieldname, 'roiname', roifile,'VoyeurH5_name', VoyeurH5_file,'isOdor',false,'calculate_diff_image',true)
Must have: ScanImage Tiff Files and Voyeur h5 File Optional: Breathmetric
All above code will generate .MAT files
%% Data format, I used it to share data with Jon and Saeed. My Python scripts are also written based on this format
Session.OdorResponse = {};
Session.F= Fluo_cell_Kalman';
Session.blockTrials = {};
Session.fieldname = fieldname;
for i = 1: size(OdorInfo.odors,1)
Session.OdorResponse{i} = permute(dffKalman(:,:,OdorInfo.odorTrials{i}),[2,1,3]);
Session.blockTrials{i} = ones(length(OdorInfo.odorTrials{i}),1);
end
% df images [-0.5s 1s] wrt inhalation onset
if calculate_diff_image
Session.diff_image = img_df_percond;
end
% Odor/stim conditions
Session.UniqueConds = cellstr(OdorInfo.odors);
% Odor/stim trial numbers
Session.OdorTrials = OdorInfo.odorTrials;
%sniff per trial
Session.Sniffs = Sniff_trial';
Session.CellMask = cellMask_vec;
Session.Infos.fps = fps;
Session.Infos.ImgFormat = img_format;
Session.Infos.imgwithROIs = img;
Session.Infos.pre_inh = pre_inh;
Session.Infos.post_inh = post_inh;
Session.Infos.TrialsRead = trials_read;
% Voyeur data
Session.VoyeurData = Data;
save(strcat(fieldname,'_S_v73.mat'), 'Session','-v7.3')
MAT file has following parameters
Parameter name | Description |
---|---|
OdorResponse |
dF/F responses, 1X N unique odor/stim condition cell array, every cell is 3D array, Ntime x Nroi x Ntrial |
F |
Fluorescence data, NtotalFrame x Nroi |
overlap_pre |
size of overlapping region in each direction before upsampling |
fieldname |
fieldname is retrieved from tiffs |
UniqueConds |
Unique trial condition presented |
OdorTrials |
Odor trials for each unique conditions |
Sniffs |
Ntime x Ntrials sniff data, [-pre post] wrt inhalation onset |
CellMask |
Cell masks for each ROI, NxNy x Nroi |
VoyeurData |
Voyeur data with prepocessed infos |
Infos |
it has extra information regarding pre, post inhalation duration, fps, image format and trial read infos |