Skip to content

anlo5007/Autocorrelation_Calcium

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Autocorrelation Analysis Pipeline

A MATLAB pipeline for analyzing neural calcium imaging data through autocorrelation analysis, oscillation detection, and statistical validation using block-bootstrapping.

Overview

This pipeline processes calcium imaging traces to identify oscillatory patterns in neural activity. It performs detrending, autocorrelation analysis, frequency domain analysis via FFT, and rigorous statistical testing with false discovery rate (FDR) correction.

Features

  • Automated batch processing of multiple trace files
  • Interactive neuron selection based on custom tags
  • Flexible time window selection for analysis
  • Polynomial detrending to remove slow drift
  • Autocorrelation analysis to detect rhythmic activity
  • FFT-based frequency analysis for oscillation characterization
  • Block-bootstrapping for robust statistical testing
  • FDR correction for multiple comparisons
  • Automatic figure generation and export

Requirements

MATLAB Version

  • MATLAB R2020b or later (uses argument validation syntax)

Required Toolboxes

  • Statistics and Machine Learning Toolbox (for mafdr)
  • Signal Processing Toolbox (for autocorrelation and FFT functions)

Installation

  1. Clone this repository:
git clone https://github.com/yourusername/autocorrelation-pipeline.git
  1. Add the repository to your MATLAB path:
addpath(genpath('path/to/autocorrelation-pipeline'))

Repository Structure

autocorrelation-calcium/
├── Main/
│   ├── Main_Autocorrelogram_script_standard_wrapped.m    # Main pipeline script for multifile analysis
│                              
├── Functions/
│   ├── data_extraction.m                                  # Load trace data and metadata
│   ├── select_neurons.m                                   # Interactive neuron selection
│   ├── time_selection.m                                   # Interactive time window selection
│   ├── polyfit_detrending.m                               # Polynomial detrending
│   ├── mp_autocorrelogram.m                               # Autocorrelation analysis
│   ├── autocorrelogram_freq_fft.m                         # FFT frequency analysis of the autocorrelogram
│   ├── shiftlinearcorr.m                                  # Block-bootstrapping statistics
│   └── blockbootstrp.m                                    # Block bootstrap resampling
└── README.md                                              # This file

Usage

Quick Start

  1. Organize your data files with the naming convention: *traces.csv
  2. Run the main script:
Main_Autocorrelogram_script_standard_wrapped.m 
  1. Select the folder containing your trace files
  2. Follow the interactive prompts to select neurons and time windows

Input Data Format

The script expects CSV files with the following structure:

Time Neuron_1 Neuron_2 Neuron_3 ...
[Header Row] tag_A tag_B tag_A ...
0.0 0.15 0.23 0.18 ...
0.033 0.17 0.25 0.19 ...
... ... ... ... ...
  • Row 1: Variable names (neuron identifiers) - becomes table header
  • Row 2: Neuron tags/categories for filtering
  • Row 3+: Time series data (first column = time, subsequent columns = neural traces)

Output

For each processed file, the pipeline generates:

Excel Results File (Autocorrelation_results.xlsx)

Contains the following columns:

  • Names - Neuron identifiers
  • max_autocorr_coeff - Maximum autocorrelation coefficient
  • oscillation_freq - Dominant oscillation frequency (Hz)
  • oscillation_freq_fft - FFT-derived oscillation frequency (Hz)
  • outside_CI - Bootstrap confidence interval test result
    • 1 = above upper CI bound
    • -1 = below lower CI bound
    • 0 = within CI
  • lower_CI - Lower confidence interval boundary
  • upper_CI - Upper confidence interval boundary
  • p_value - Bootstrap p-value
  • BH_correction - Benjamini-Hochberg FDR significance (true/false)

Figures

All figures are automatically saved as PDFs in the same folder as input data:

  • traces.pdf - Raw and selected time windows
  • Detrending visualization
  • Autocorrelogram plots
  • FFT frequency analysis plots

Configuration

Main Parameters

Edit these variables in Main_Autocorrelogram_script.m:

% File selection
file_pattern = '**\*traces.csv';  % File search pattern

% Statistical parameters
fdr_correction = true;             % Enable/disable FDR correction
fdr_q = 0.05;                      % FDR significance threshold (default: 5%)

Advanced Parameters

Modify parameters in function calls for fine-tuning:

Bootstrap Parameters (line ~30)

[outside_CI(j), boundaries_CI(j,:), p_values(j)] = shiftlinearcorr(...
    detrended(:,j+1), detrended(:,1), coeff(j), ...
    "blocksize", 50, ...              % Block size for bootstrap
    "pValueCorrection", false);       % Conservative p-value correction

Additional parameters available in shiftlinearcorr.m:

  • CI - Confidence interval percentiles (default: [99 1])
  • iterations - Number of bootstrap iterations (default: 1000)

Detrending Parameters

Modify in polyfit_detrending.m function call

Autocorrelation Parameters

Modify in mp_autocorrelogram.m function call

FFT Parameters

Modify in autocorrelogram_freq_fft.m function call

Workflow

Input CSV Files
      ↓
[data_extraction]
      ↓
[select_neurons] - Interactive tag-based filtering
      ↓
[time_selection] - Interactive time window selection
      ↓
[polyfit_detrending] - Remove slow drift
      ↓
[mp_autocorrelogram] - Compute autocorrelation
      ↓
[autocorrelogram_freq_fft] - Frequency analysis
      ↓
[shiftlinearcorr] - Block-bootstrap testing
      ↓
[mafdr] - FDR correction
      ↓
Export Results & Figures

Statistical Methods

Autocorrelation Analysis

  • Computes autocorrelation to detect rhythmic patterns
  • Identifies dominant oscillation frequencies
  • Normalizes coefficients for comparison across neurons

Block-Bootstrapping

  • Assesses statistical significance of autocorrelation coefficients
  • Preserves temporal structure through block resampling
  • Default settings:
    • Block size: 50 samples
    • Iterations: 1000
    • Confidence interval: 99th/1st percentiles
  • P-value calculation:
    • Standard: p = sum(|bootstrap| >= |coeff|) / N
    • Conservative (optional): p = (sum(|bootstrap| >= |coeff|) + 1) / (N + 1)

False Discovery Rate (FDR)

  • Benjamini-Hochberg procedure for multiple comparison correction
  • Controls expected proportion of false positives
  • Default q-value: 0.05 (5% FDR)
  • Can be disabled by setting fdr_correction = false d in each file's directory

Function Reference

Core Functions

data_extraction(filepath)

Loads CSV data and extracts neuron tags.

  • Input: Path to CSV file
  • Output: Data table and tag array

select_neurons(traces, ar)

Interactive dialog for neuron selection based on tags.

  • Input: Trace data and tag array
  • Output: Selected data, names, breakpoints, and time vector

time_selection(data, time, breakpoints)

Interactive selection of time windows for analysis.

  • Input: Data, time vector, and breakpoints
  • Output: Selected data and updated time vector

polyfit_detrending(traces, options)

Removes polynomial trends from data.

  • Input: Time series data
  • Output: Detrended data, trend, and figure handle

mp_autocorrelogram(data, options)

Computes autocorrelation and identifies oscillations.

  • Input: Detrended data
  • Output: Coefficients, frequencies, correlogram, and figure

autocorrelogram_freq_fft(correlogram, options)

FFT-based frequency analysis of autocorrelation.

  • Input: Correlogram data
  • Output: FFT frequencies, frequency domain data, and figure

shiftlinearcorr(x1, t, coeff, options)

Block-bootstrap statistical testing.

  • Input: Signal, time, coefficient, and bootstrap options
  • Output: CI test results, boundaries, and p-value

blockbootstrp(x, blocksize, nsamples)

Block bootstrap resampling preserving temporal structure.

  • Input: Signal, block size, number of samples
  • Output: Bootstrapped samples

Troubleshooting

Common Issues

"No files found matching pattern"

  • Verify files end with traces.csv
  • Check the selected folder path
  • Ensure the file pattern matches your naming convention

"Table variable names must be nonempty text"

  • Verify Row 1 contains valid column headers
  • Ensure no empty cells in the header row
  • Check for numeric-only headers (will be converted to strings)

"No tag selected. Using all neurons."

  • This is a warning, not an error
  • All neurons will be included in the analysis
  • To filter, select tags in the dialog

Memory errors with large datasets

  • Reduce bootstrap iterations (edit line ~30)
  • Process smaller time windows
  • Process files individually instead of batch
  • Close other MATLAB figures/variables

Figures not saving

  • Check folder write permissions
  • Ensure sufficient disk space
  • Verify SavingFolder parameter is set correctly

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages