An open-source method for analysis of confocal calcium imaging with sparse cells
The Anaconda environment was downloaded to use these codes. Additionally these are the dependencies for this code. The following packages must be installed for the codes to work:
pip install opencv-python==4.4.0.42 tifffile imagecodecs
The following scripts are used to pre-process .tif files before running TrackMate, as well as to calculate and plot the change in fluorescence of neurons from the TrackMate output files for calcium imaging data.
Sorting Code (gray_sort_tiff_1):
- gray_sort_tiff() has the following functions:
- Reads the Zeiss .tif files one at a time
- Turns the .tif files gray if they are not grayscale already
- Outputs the gray .tif files into folders labeled by z stack positions that can be used by TrackMate.
Fluoresence Extraction Code (individual_dFoverF0_1):
-
fluorescence_extract() has the following functions:
- Combines MEAN_INTENSITY values from the All Spots statistics TrackMate output files for a single neuron.
- Subtracts the background and finds the maximal value for each timepoint.
- Calculates the change in fluorescence (βF/F0) and plots the βF/F0 over time.
- Outputs a .csv with the combined data and calculations, and a "Neuron Plots" folder containing the plot of the βF/F0 as a .png into the "results" folder.
-
loop_fluorescence_extract() has the following functions:
- Runs fluorescence_extract() on all the neurons in a folder. This requires specific file structure as an input. Refer to the Input and Output File Organization section Organization Example C.
- Outputs .csv files and a "Neuron Plots" folder containing the plots into the "results" folder
Merging Code (merge_dFoverF0_1):
- merge_data() has the following functions:
- Combines all the βF/F0 values for each neuron in the folder into one file. This requires specific file structure as an input. Refer to the Input and Output File Organization section and Organization Example E.
- Calculates the average and SEM of βF/F0 values and plots the average βF/F0 over time.
- Outputs "merged_data.csv" and "Average_dF_F0.png" into a "merged_data" folder within the "results" folder.
-
In the paper, Zeiss confocal is used as an example. It automatically saves the .tif file names identically to the folder they are in with a base name that you choose.
- Therefore, the files in the folder will have the same name as the folder with β_h#t#z#c#.tifβ, (where the # stands for a number) at the end of each file to identify the phase (h#), time point (t#), z-position (z#), and channel (c#) of each file.
- In order for this code to work, the original name of the folder must be kept, or if changed, the file folder name must be identical to the beginning of each file.
- For example, in the practice folder, files are saved as βNeuron0to2_h#t#z#c#.tifβ, so the folder name is βNeuron0to2β.
-
Using other confocal microscopes that export .tif files:
-
The files must be exported as .tif. The naming systems are different for Nikon, Leica, Zeiss etc.
-
If other confocal files are to be used with this code, the 4 lines in the code where the "_#h#t#z#c#.tif" is defined must be changed to match the system of the confocal export. For example if a file is exported as Neuron0to2_Z#T#C1.tif, the lines naming the file should be changed from:
file = f'{file_name}_{phase}t{t}z{z}{channel}.tif'
to:
file = f'{file_name}_{phase}Z{z}T{t}{channel}.tif' #where phase = βNaβ and channel = βC1β.
-
Another example if a file is exported as Neuron0to2.lif t01z01.tif, the lines naming the file should be changed to:
file = f'{file_name} {phase}t{t}z{z}{channel}.tif' #where phase = βNaβ and channel = βNaβ
-
-
The code turns the files gray if the is_gray parameter is set to βNoβ and saves them into a new folder labeled with the file name and _gray_stacks one directory above (outside of the input folder) such that the original folder is never changed.
- If the is_gray parameter is set to βYesβ, a copy of the original file is saved into a new folder one directory above.
- The practice set is named βNeuron0to2_gray_stacksβ and has the z-position folders named βNeuron0to2_1β, βNeuron0to2_2β etc.
- Refer to Organization Example A for an example of how the files should be structured and Organization Example B for what the file structure looks like after execution.
βββ Analysis
βββ Neuron0to2
βββ Neuron0to2_h01t01z01c2.tif
βββ Neuron0to2_h01t01z02c2.tif
βββ Neuron0to2_h01t01z03c2.tif
βββ Neuron0to2_h01t01z04c2.tif
βββ Neuron0to2_h01t01z05c2.tif
βββ Neuron0to2_h01t01z06c2.tif
βββ Neuron0to2_h01t01z07c2.tif
βββ Neuron0to2_h01t02z01c2.tif
βββ Neuron0to2_h01t02z02c2.tif
βββ Neuron0to2_h01t02z03c2.tif
βββ Neuron0to2_h01t02z04c2.tif
βββ Neuron0to2_h01t02z05c2.tif
βββ Neuron0to2_h01t02z06c2.tif
βββ Neuron0to2_h01t01z07c2.tif
βββ Neuron0to2_h01t03z01c2.tif
βββ Neuron0to2_h01t03z02c2.tif
βββ Neuron0to2_h01t03z03c2.tif
βββ Neuron0to2_h01t03z04c2.tif
βββ Neuron0to2_h01t03z05c2.tif
βββ Neuron0to2_h01t03z06c2.tif
βββ Neuron0to2_h01t03z07c2.tif
βββ Analysis
βββ Neuron0to2
βββ Neuron0to2_gray_stacks
βββ Neuron0to2_1
β βββ Neuron0to2_h01t01z01c2.tif
β βββ Neuron0to2_h01t02z01c2.tif
β βββ Neuron0to2_h01t03z01c2.tif
βββ Neuron0to2_2
β βββ Neuron0to2_h01t01z02c2.tif
β βββ Neuron0to2_h01t02z02c2.tif
β βββ Neuron0to2_h01t03z02c2.tif
βββ Neuron0to2_3
β βββ Neuron0to2_h01t01z03c2.tif
β βββ Neuron0to2_h01t02z03c2.tif
β βββ Neuron0to2_h01t03z03c2.tif
βββ Neuron0to2_4
β βββ Neuron0to2_h01t01z04c2.tif
β βββ Neuron0to2_h01t02z04c2.tif
β βββ Neuron0to2_h01t03z04c2.tif
βββ Neuron0to2_5
β βββ Neuron0to2_h01t01z05c2.tif
β βββ Neuron0to2_h01t02z05c2.tif
β βββ Neuron0to2_h01t03z05c2.tif
βββ Neuron0to2_6
β βββ Neuron0to2_h01t01z06c2.tif
β βββ Neuron0to2_h01t02z06c2.tif
β βββ Neuron0to2_h01t03z06c2.tif
βββ Neuron0to2_7
βββ Neuron0to2_h01t01z07c2.tif
βββ Neuron0to2_h01t02z07c2.tif
βββ Neuron0to2_h01t03z07c2.tif
-
The βAll Spots statisticsβ files from TrackMate should be saved in the following format:
- "Mean_Intensity#.csv", where # stands for the number of the z-position where values come from.
- For example, TrackMate βAll Spots statisticβ file for z-position 3 should be saved as βMean_Intensity03.csvβ.
- This way, the mean intensity values in each z-position can be tracked by replacing the general MEAN_INTENSITY column names with the file name βMean_Intensity#β in the merged document.
- "Mean_Intensity#.csv", where # stands for the number of the z-position where values come from.
-
If running multiple neurons together, the βMean_Intensity#.csvβ files for each neuron should be saved into a folder labeled βNeuron #β, where # stands for the number of the neuron.
- The first neuron in the set is always βNeuron 0β, the second is βNeuron 1β etc. Organization Example C and Organization Example D show an example of folder organization before and after execution respectively.
-
The loop_fluorescence_extraction() code will combine the mean intensity in the βNeuron #β folder into one file, calculate the maximal intensity for each time point and the change in fluorescence (βF/F0) for that neuron, and output them to a βresultsβ folder with the name βNeuron #.csvβ
- The default creates a folder named βresultsβ in the directory where the βNeuron #β folders reside.
- In Organization Example D, the βresultsβ folder would be created in the βAnalysisβ folder.
- Within the βresultsβ folder, there are the .csv files with the merged mean intensity and the βF/F0 calculation labeled after each neuron.
- There is also a folder called βNeuron Plotsβ that will have the output plots for each βF/F0 value.
- A βresultsβ folder can also be set to any location by setting the results_folder parameter.
- The default creates a folder named βresultsβ in the directory where the βNeuron #β folders reside.
-
A βBackground_list.csvβ file must be present in the same folder as the βNeuron #β folders.
- The background_list has a column labeled βNeuron #β for each neuron and must have the background values for each z-positions. The number of z-positions for each neuron must equal to the number of βMean_Intensity#.csvβ files in each neuron folder. The columns must be in numerical order and must not skip values.
-
Alternatively, you can run one neuron at a time using fluorescence_extraction(). This allows analysis from different folders.
- Instead of a βBackground_list.csvβ, the values for the background must be entered as a list into the background_averages parameter.
- If analyzing multiple neurons separately that belong to the same group, the output result should be set to a same folder so that the merging code can be easily implemented.
βββ Analysis
β βββ Background_list.csv
β βββ Neuron 0
β β βββ Mean_Intensity_01.csv
β β βββ Mean_Intensity_02.csv
β β βββ Mean_Intensity_03.csv
β βββ Neuron 1
β β βββ Mean_Intensity_04.csv
β β βββ Mean_Intensity_05.csv
β β βββ Mean_Intensity_06.csv
β β βββ Mean_Intensity_07.csv
β βββ Neuron 2
β βββ Mean_Intensity_04.csv
β βββ Mean_Intensity_05.csv
β βββ Mean_Intensity_06.csv
β βββ Mean_Intensity_07.csv
βββ Neuron0to2
βββ Neuron0to2_gray_stacks
βββ Analysis
β βββ Background_list.csv
β βββ Neuron 0
β βββ Neuron 1
β βββ Neuron 2
β βββ results
β βββ Neuron 0.csv
β βββ Neuron 1.csv
β βββ Neuron 2.csv
β βββ Neuron Plots
β βββ Neuron 0.png
β βββ Neuron 1.png
β βββ Neuron 2.png
β
βββ Neuron0to2
βββ Neuron0to2_gray_stacks- The merging code saves all the outputs to a βmerged_dataβ folder which is automatically created within the βresultsβ folder as seen in Organization Example E.
- It then combines all the βF/F0 columns from the βNeuron #.csvβ files into one file and renames each column to match the βNeuron #.csvβ file it came from.
- Next, it calculates the average and SEM of the βF/F0 columns.
- This file is named βmerged_data.csvβ and is saved into the βmerged_dataβ folder.
- The code also creates and saves a graph named Average_dF_F0.png which plots the average βF/F0 with the position T into the βmerged_dataβ folder.
- All the βNeuron #.csvβ files have to be in the same folder so the βF/F0 of each neuron can be averaged and plotted.
βββ Analysis
β βββ Background_list.csv
β βββ Neuron 0
β βββ Neuron 1
β βββ Neuron 2
β βββ results
β βββ merged_data
β β βββ Average_dF_F0.png
β β βββ merged_data.csv
β βββ Neuron 0.csv
β βββ Neuron 1.csv
β βββ Neuron 2.csv
β βββ Neuron Plots
βββ Neuron0to2
βββ Neuron0to2_gray_stacks- The background values are entered into an excel file that is saved as a .csv.
- The excel file should have a column for each neuron labeled as βNeuron #β. Make sure there is no space after the number in the neuron, as the following "Neuron # " will show an error.
- Each column must contain the background values for the z-positions being analyzed.
- The number of background values must match the number of βMean_Intensity#.csvβ files in each neuron folder.
-
For example, in "Practice 3", there are 3 neuron folders in βAnalysisβ, βNeuron 0β with 3 βMean_Intensity#.csvβ files, βNeuron 1β with 4 βMean_Intensity#.csvβ, and βNeuron 2β with 4 βMean_Intensity#.csvβ files.
-
The βBackground_list.csvβ would look like the following. Note, since βNeuron 1β and βNeuron 2β appear in the same z-positions, the background values are the same.
Neuron 0 Neuron 1 Neuron 2 1.978 1.084 1.084 2.435 1.150 1.150 2.335 0.430 0.430 0.297 0.297
-
- Creates a folder for each z-position.
- Turns images grayscale.
- Saves grayscale images into the correct z-position folder.
- working_dir: path object or file-like object
- A string path (location) of the folder: all the images that will be sorted and turned gray.
- When loading the file location into the function, make sure that there is no "/" or β\β after the folder name or it will not read the file name properly. For example:
- This is wrong: '/Users/nilabuser/Desktop/Practice/'
- This is correct: '/Users/nilabuser/Desktop/Practice'
- phase: str, default βh01β
- Set the phase if it exists. You can sort files by the phase name but only one phase at a time. The code must be separately run with each different phase name.The folders created by the script should be edited to add phase name upon completion. If the folder name is not edited, the z-position folders will have timepoints for all phases in them.
- The default parameter is set to 'Na'. If there is no phase in the name (starting with h), the parameter should be set "phase='Na'" (default). This is used for the following file names:
- Neuron0to2_ t#z#.tif
- To include the phase name, set the parameter βphase=βh#ββ. For example, if the files are Neuron0to2_h01t#z#c#.tif, set the phase parameter as "phase = 'h01'".
- position_t: int, default 100
- Set the position_t to the number of timepoints in the time-lapse.
- A number smaller than the actual number of timepoints can be used to sort less images. The first image sorted will always be timepoint 1. This method does not allow for using a subset of images that doesnβt incorporate the first image.
- position_z: int, default 15
- Set the position_z to the number of z-positions in the stack.
- A number smaller than the actual number of z-positions can be used to sort less images. The first image sorted will always be z-position 1. This method does not allow for using a subset of images that doesnβt incorporate the first z-position.
- channel: str, default βNaβ
- Set the channel if it exists.You can sort files by the channel but only one channel at a time. The code must be separately run with each different channel name.The folders created by the script should be edited to add channel name upon completion. If the folder name is not edited, the z-position folders will have timepoints for all channels in them.
- The default parameter is set to Na. If there is no channel at the end of the name, parameter should be set "channel='Na'" (default). This is used for the following file names:
- Neuron0to2_h#t#z#.tif
- To include the channel name, set the parameter as the following βchannel=βc#ββ. For example, if the files are Neuron0to2_h#t#z#c3.tif, set the channel parameter as "channel = 'c3'".
- is_gray: str, default βNoβ
- Describe whether the .tif files are grayscale. If they are grayscale, βis_gray = βYesββ or they are not grayscale, βis_gray = βNoββ.
- The default is βis_gray = βNoββ and this will turn the files grayscale for TrackMate use.
fluorescence_extract(working_dir, results_folder = 'results', trial_name = 'Neuron', position_t=100, background_averages=[1])
- Default: Creates a βresultsβ folder in the same directory as the folder of the neuron. The results_folder parameter can be changed to create a folder in a different location or with a different name.
- Merges all the βMean_Intensity#.csvβ files for the neuron based on the POSITION_T and MEAN_INTENSITY columns.
- Renames the MEAN_INTENSITY columns to the matching Mean_Intensity# and keeps track of the timepoint that the values come from. Missing values are left blank.
- Subtracts the background values based on the background_averages inputted list. The background_averages list must be the same length as number of βMean_Intensity#.csvβ files.
- Finds the maximal value for each timepoint from the Mean_Intensity columns.
- Calculates the βF/F0 columns for the given neuron based on the first maximal value found.
- Saves the file as the "trial_name".csv in the βresultsβ folder. The default is "trial_name = 'Neuron'" making the default output file "Neuron.csv".
- Plots βF/F0 against the POSITION_T and uses the "trial_name" parameter as the title. The output file is saved as "trial_name".png. The default is "trial_name = 'Neuron'" making the default output "Neuron.png".
- working_dir: path object or file-like object
- A string path (location) of the folder neuron folder is.
- When loading the file location into the function make sure that there is no "/" or β\β after the folder name or it will not read the file name properly. For example:
- This is wrong: '/Users/nilabuser/Desktop/Example/Neurons_to_analyze/Neuron 0/'
- This is correct: '/Users/nilabuser/Desktop/Example/Neurons_to_analyze/Neuron 0β
- results_folder: str, default βresultsβ
- Set the location the output files are saved. The default creates a βresultsβ folder in the same folder that contains the βNeuron #β folders. The location can be set, and if the folder does not exist, it will be created.
- trial_name: str, default βNeuronβ
- Set the name of the output .csv and .png file. This will also be the title of the βF/F0 plot.
- position_t: int, default 100
- Set the number of time points (given by the POSITION_T column in the βMean _Intensity#.csv file) to be analyzed and merged in each time point. This can be set to less than the actual number of time points that are in the .csv files.
- Set the actual number time points there are. For example, the number of time points is 100 which has time points 0-99 in the Mean_Intensity#.csv file. Set 100 as the number of frames.
- background_averages: list of int, default [1]
- Set the background values for each column. Therefore, the number of background values given must equal to the number of βMean_Intensity#.csvβ files in the folder. The background values must be given in a list (annotated by brackets []). So, if you have 3 columns with the same background average of 2.5, your parameter will look like this:
- background_averages = [2.5, 2.5, 2.5]
- The default is set to one column (one .csv file) with 1 as the background_average.
loop_fluorescence_extract(working_dir, background_list, result_folder = 'results', number_of_position_t=100)
- Default: Creates a βresultsβ folder in the same directory as "Neuron #" folders. The results_folder parameter can be changed to create a folder in a different location or with a different name.
- Merges all the Mean_Intensity#.csv files for each "Neuron #" folder based on the POSITION_T and MEAN_INTENSITY columns.
- Renames the MEAN_INTENSITY columns to the matching Mean_Intensity# and keeps track of the time point that the values come from. Missing values are left blank.
- Subtracts the background values based on each "Background_list.csv" column. The "Backgroung_list.csv" columns must be the same length as number of "Mean_Intensity#.csv" files for each neuron. The labeling must be in order starting with "Neuron 0" and cannot have skipped numbers.
- Finds the maximal value for each timepoint from the Mean_Intensity# columns.
- Calculates the βF/F0 columns for each "Neuron #" folder based on the first maximal value found.
- Saves the file in the "results" folder with the following format βNeuron #.csvβ
- Creates a graph of the βF/F0 with POSITION_T and "Neuron #" as the title. The graph is saved as "Neuron #.png".
- working_dir: path object or file-like object
- A string path (location) of the folder all the Neuron # folders are.
- When loading the file location into the function make sure that there is no "/" or "\" after the folder name or it will not read the file name properly. For example:
- This is wrong: '/Users/nilabuser/Desktop/Example/Neurons_to_analyze/ '
- This is correct: '/Users/nilabuser/Desktop/Example/Neurons_to_analyzeβ
- background_list: path object or file-like object
- Set the path to the "Background_list.csv" file.
- The .csv file should have a column for every neuron in the working_dir. The number of background values given in each column must equal to the number of Mean_Intensity#.csv files in each neuron folder it pertains to. Each column labeled must be identical to the neuron it pertains to. For example, in "Practice 3" there are 3 neuron folders in "Analysis": "Neuron 0" with 3 "Mean_Intensity#.csv" files, "Neuron 1" with 4 "Mean_Intensity#.csv" files, and "Neuron 2" with 4 "Mean_Intensity#.csv" files. Note, since "Neuron 1" and "Neuron 2" appear in the same z-positions, the background values are the same. For an example of the Background_list.csv refer to the Background List File section:
βββ Background List File
- result_folder: path object or file-like object, str, default βresultsβ
- Set the location the output files are saved. The default creates a "results" folder in the same folder that contains the "Neuron #" folders. The location can be set, and if the folder does not exist, it will be created.
- number_of_position_t: int, default 100
- Set the number of time points (given by the POSITION_T column in the "Mean_Intensity,#.csv" files) to be analyzed and merged for each z-position. This can be set to less than the actual number of time points that are in the .csv files.
- Set the actual number time points there are. For example, the number of time points is 100 which has time points 0-99 in the Mean_Intensity#.csv file. Set 100 as the number of frames.
- Creates a "merged_data" folder in the "results" folder.
- Reads all the .csv files in the "results" folder.
- Merges the βF/F0 columns from each Neuron into one file and labels the column with the neuron name ("Neuron 0", "Neuron 1" etc.)
- Calculates the average and SEM of βF/F0 and saves a "merged_data.csv" file in the "merged_data" folder.
- Creates and saves a plot for the average βF/F0 over time in the "merged_data" folder as .png with the plot_title parameter as the name.
- results_folder, path object or file-like object
- A string path (location) of the folder all the "Neuron #.csv" files are.
- When loading the file location into the function make sure that there is no "/" or "\" after the folder name or it will not read the file name properly. For example:
- This is wrong: '/Users/nilabuser/Desktop/Example/Neurons_to_analyze/results/'
- This is correct: '/Users/nilabuser/Desktop/Example/Neurons_to_analyze/resultsβ
- position_t: int, default 999
- Set the number of time points (also given by the POSITION_T column in the "Mean_Intensity#.csv" file) to be analyzed and merged in each z-position. This can be set to less than the actual number of time points that are in the .csv files.
- Set the actual number time points there are. For example, the number of time points is 100 which has time points 0-99 in the "Mean_Intensity#.csv" file. Set 100 as the number of frames.
- plot_title: str, default βAverage βF/F0β
- Set the name of the title the output plot will have. This will also be the name of the .png file when it is saved.