# Human Heart Sex-Biased Chromatin & Gene Expression Analysis

## Overview

This project investigates sex differences in chromatin accessibility (snATAC-seq) and gene expression (bulk RNA-seq) in human left ventricle (LV) heart tissue. Analyses include:

- Differentially Accessible Regions (DAR): female vs. male (snATAC-seq, snATAC CM peaks)
- Differentially Expressed Genes (DEG): old vs. young, stratified by sex (GTEx bulk RNA-seq LV)
- Hormone receptor peak annotation: ER (estrogen receptor) and AR (androgen receptor) overlap with DARs

---

## Data Sources

### snATAC-seq (chromatin accessibility)
- **Dataset**: GSE156704 — adult human heart snATAC-seq
- **Samples**: 25 total — 12 female, 13 male (paired-end replicates per individual)
- **Cell type**: Cardiomyocytes (CM)
- **Peak set**: 158,489 merged peaks across all samples
- **Count matrix**: `analysis/sn_ATAC_analysis/count_matrix_snPeak.txt`
  - Format: tab-separated, columns = chr / start / end / sample_S1 / sample_S2 ...
  - Equivalent file at: `ATAC_heart_openbed/counts_test/count_matrix.txt` (long column names)
  - Also available at: `/home/zhangbo/temp/Human_heart_GSE156704/merged_Audlt_snATAC_peak_counts.txt` (no `end` column)

### Bulk RNA-seq (gene expression)
- **Dataset**: GTEx v8 — Heart Left Ventricle
- **Samples**: 187 male (sex1), 91 female (sex2)
- **Metadata**: `analysis/meta_sex1.txt`, `analysis/meta_sex2.txt`
  - Columns: sample_id / sex / age_band / age_group (young: 20–39, old: 40+)
- **Count tables**: `getx_LV/`

---

## Directory Structure

```
heart/
├── analysis/
│   ├── DAR_analysis_v6.R          # DAR: female vs male, k=1~6 sweep, snATAC (bulk-merged peaks)
│   ├── DAR_analysis_v7.R          # DAR: female vs male, k=6 only (reference pipeline style)
│   ├── DEG_sex1.R                 # DEG: old vs young, male (edgeR, v1)
│   ├── DEG_sex2.R                 # DEG: old vs young, female (edgeR, v1)
│   ├── DEG_sex1_v2.R              # DEG: old vs young, male (DESeq2, v2)
│   ├── DEG_sex2_v2.R              # DEG: old vs young, female (DESeq2, v2)
│   ├── meta_sex1.txt              # GTEx male sample metadata
│   ├── meta_sex2.txt              # GTEx female sample metadata
│   ├── sn_ATAC_analysis/
│   │   ├── count_matrix_snPeak.txt
│   │   ├── heart_DAR_female_vs_male_RUVr_k{1-6}.csv      # All peaks, per k
│   │   ├── heart_DAR_female_vs_male_RUVr_k{1-6}_filter.csv  # Significant DARs, per k
│   │   ├── heart_DAR_female_vs_male_RUVr_k{1-6}_MORE.csv # Female > male, per k
│   │   ├── heart_DAR_female_vs_male_RUVr_k{1-6}_LESS.csv # Female < male, per k
│   │   ├── heart_DAR_female_vs_male_RUVr_k{1-6}_normCPM.csv
│   │   ├── heart_DAR_female_vs_male_k_comparison_summary.csv  # k=1~6 DAR count table
│   │   ├── heart_DAR_overlap_all/MORE/LESS_k.csv          # 6×6 pairwise overlap matrices
│   │   ├── heart_DAR_peak_presence_across_k.csv
│   │   └── results_k6/                                    # v7 reference pipeline output (k=6)
│   │       ├── DAR_heart_femaleVSmale_all.csv
│   │       ├── Sig_DAR_heart_femaleVSmale_FDR001_FC0585.csv
│   │       ├── Sig_DAR_heart_femaleVSmale_FDR001_FC0585_UP.csv   # female > male
│   │       ├── Sig_DAR_heart_femaleVSmale_FDR001_FC0585_Down.csv # female < male
│   │       ├── Normalized_cpm_RUV6.csv
│   │       └── overlap_Yu_k6_identical_peaks.csv          # Peaks shared with Yu_result
│   └── DEG_LV/
│       ├── male/                  # v1 edgeR DEG results (sex1)
│       └── female/                # v1 edgeR DEG results (sex2)
│
├── snACAT_CM/                     # snATAC CM-specific analysis (separate cohort)
│   ├── counts/count_matrix_snACAT_CM.txt
│   └── DAR_results/
│       ├── snACAT_CM_DAR_female_vs_male_RUVr_k6.txt
│       ├── snACAT_CM_DAR_female_vs_male_RUVr_k6_filter.txt
│       └── snACAT_CM_RUVr_k{1-6}_normCounts/PCA/RLE files
│
├── hormon/sn_atac/                # Hormone receptor annotation of snATAC peaks
│   ├── snATAC_CM_peak_DAR_ER_AR.bed   # All 158,489 peaks with DAR/ER/AR flags (TRUE/FALSE)
│   ├── DAR_AR.bed                 # DAR=TRUE & AR=TRUE (503 peaks)
│   ├── DAR_ER.bed                 # DAR=TRUE & ER=TRUE (1,069 peaks)
│   ├── OCR_AR.bed                 # AR=TRUE, any peak (11,083 peaks)
│   └── OCR_ER.bed                 # ER=TRUE, any peak (22,193 peaks)
│
├── Yu_result/CM_r_output/         # External collaborator DAR results (Yu lab, CM)
│   ├── DAR_CM_femaleVSmale_ruvk6_exactTest.csv
│   └── DAR_CM_femaleVSmale_ruvk6_exactTest_filtered.csv  # 10,572 DARs
│
├── amina_res/                     # External collaborator DAR results (Amina, k=4)
│   └── DAR_CM/FB_*_FDR001_logFC58.csv/.bed
│
└── adt_heart_narrowPeak/          # Individual sample MACS2 peak calls
    └── peak_union/                # Merged/union peak sets
```

---

## Analysis Methods

### DAR Analysis (snATAC-seq)

**Script**: `analysis/DAR_analysis_v6.R` (k sweep) / `DAR_analysis_v7.R` (k=6)

Pipeline:
1. Load count matrix (158,489 peaks × 25 samples)
2. Build metadata: 12 female, 13 male
3. **Filter**: CPM > 1 in ≥ 12 samples (`min(table(sex))`)
4. TMM normalization (`calcNormFactors`)
5. Pre-batch PCA (log1p + prcomp)
6. RUVr batch correction:
   - edgeR GLM fit (common + tagwise dispersion)
   - Deviance residuals → `RUVr(seqUQ, k=k, residuals)`
7. DAR: edgeR `exactTest(pair=c("male","female"))`
   - logFC > 0 = female > male (MORE)
   - logFC < 0 = female < male (LESS)
8. Cutoffs: FDR < 0.001, |logFC| > log2(1.5) ≈ 0.585
9. Per-k: RLE plot, PCA, volcano plot, normalized CPM export
10. Overlap matrices across k values (6×6)

**k=1~6 DAR count summary**:

| k | MORE (F>M) | LESS (F<M) | Total DAR |
|---|-----------|-----------|-----------|
| 1 | 117 | 162 | 279 |
| 2 | 211 | 338 | 549 |
| 3 | 307 | 625 | 932 |
| 4 | 428 | 1,095 | 1,523 |
| 5 | 504 | 1,418 | 1,922 |
| 6 | 562 | 1,721 | 2,283 |

### DEG Analysis (bulk RNA-seq GTEx LV)

**Scripts**: `analysis/DEG_sex1.R` / `DEG_sex2.R` (edgeR v1), `DEG_sex1_v2.R` / `DEG_sex2_v2.R` (DESeq2 v2)

Pipeline (per sex):
1. Load GTEx LV count table for one sex
2. Pre-batch PCA + correlation heatmap
3. Filter: CPM > 1 in ≥ min(group size) samples
4. RUVr batch correction (k=1~6)
5. DEG: edgeR exactTest (old vs young) [v1] or DESeq2 Wald test [v2]
6. Cutoffs: FDR < 0.05 (v1) / FDR < 0.01 (v2), |logFC| > 0.58
7. Per-k: volcano plots, UP/DOWN gene tables
8. Overlap matrices across k values (6×6, UP and DOWN separate)

### Hormone Receptor Annotation

**Tool**: bedtools intersect

Annotation file: `hormon/sn_atac/snATAC_CM_peak_DAR_ER_AR.bed`
- Column 4 (DAR): overlaps with significant DAR peaks (female vs male, k=6)
- Column 5 (ER): overlaps with ER ChIP-seq peaks (`ER_peaks.bed`)
- Column 6 (AR): overlaps with AR binding sites (`masterlist_DHSs_myResults_nonovl_any_chunkIDs.bed`)

| Annotation | Peaks |
|-----------|-------|
| DAR | 6,452 |
| ER | 22,193 |
| AR | 11,083 |
| ER & AR | 1,909 |
| DAR & ER | 1,069 |
| DAR & AR | 503 |

### Cross-study Peak Overlap (Yu lab vs this study)

**Files compared**:
- Yu lab: `Yu_result/CM_r_output/DAR_CM_femaleVSmale_ruvk6_exactTest_filtered.csv`
  - 10,572 DARs; snATAC CM-specific cohort (5F / 5M); logFC > 0 = female > male
  - Yu MORE (F>M): 5,898 | Yu LESS (F<M): 4,674
- This study (v7, k=6): `analysis/sn_ATAC_analysis/results_k6/DAR_heart_femaleVSmale_all.csv`
  - 148,056 peaks tested; bulk snATAC GSE156704 (12F / 13M)

**Peak set overlap**: 10,533 of 10,572 Yu peaks found in my peak set (39 not tested)

**Replication at each FDR threshold (my results)**:

| FDR in my results | Count | % of 10,533 |
|-------------------|-------|-------------|
| FDR < 0.001 | 741 | 7.0% |
| FDR < 0.01 | 1,382 | 13.1% |
| FDR < 0.05 | 2,265 | 21.5% |
| FDR ≥ 0.05 (not replicated) | 8,268 | 78.5% |

**Direction concordance (same logFC sign in both studies)**:

| FDR threshold (mine) | n | Same direction | Opposite direction |
|----------------------|---|----------------|-------------------|
| FDR < 0.001 | 741 | 309 (42%) | 432 (58%) |
| FDR < 0.01 | 1,382 | 571 (41%) | 811 (59%) |
| FDR < 0.05 | 2,265 | 958 (42%) | 1,307 (58%) |

Direction breakdown at FDR < 0.05 (my results):
- Yu MORE & mine MORE: 802 peaks (both female > male)
- Yu LESS & mine LESS: 1,463 peaks (both female < male)
- Discordant (opposite): 1,307 peaks

**Peaks significant in both studies** (FDR < 0.001 & |logFC| > 0.585 in both):
- 208 peaks — saved to `analysis/sn_ATAC_analysis/results_k6/overlap_Yu_k6_identical_peaks.csv`
- Direction concordance: 114/208 (54%); 17 MORE/MORE, 97 LESS/LESS, 81 Yu-MORE/Mine-LESS, 13 Yu-LESS/Mine-MORE
- logFC Pearson r = 0.31 (mean |logFC|: Yu = 1.08, mine = 0.83)
- Overall logFC correlation (all 10,533 matched peaks): r = −0.02

**Interpretation**: Low replication (22% at FDR < 0.05) and near-random direction concordance (~42%) indicate the two datasets are largely independent. Key differences: different patient cohorts, different sample sizes (10 vs 25), and potentially different cell-type compositions (CM-specific vs bulk snATAC). The 208 peaks significant in both likely represent the most robust sex-biased chromatin loci in heart CM, predominantly with LESS direction (female < male accessibility).

---

## Key File Formats

- **DAR result CSVs**: row = peak ID (`chr:start-end`), columns = genes / logFC / logCPM / PValue / FDR [/ CPM per sample]
- **BED files** (4-column): chr / start / end / peak_ID (`chr_start_end`)
- **Count matrices**: tab-separated, header row, first 3 cols = chr / start / end (or chr / start)
- **Annotation BED**: chr / start / end / DAR / ER / AR (TRUE/FALSE)

---

## Notes

- snATAC peak IDs use two formats across files:
  - Colon-dash: `chr1:9955-10505` (R CSVs)
  - Underscore: `chr1_9955_10505` (Yu lab, BED 4th column)
- v6 and v7 DAR scripts are equivalent at k=6 after CPM filter alignment; v6 runs k=1~6 sweep, v7 runs k=6 only following the original reference pipeline structure
- GTEx sex labels: sex1 = male, sex2 = female
