# TCDD Sex-Separated Pipeline — Li adt AL

**Script:** `TCDD_Li_adt_sex_pipeline.sh`  
**Date:** 2026-04-08  
**Filter:** Tissue = Li, Age = adt, Exposure = TCDD, Lab = AL

---

## Overview

Identifies DAR peaks associated with differentially expressed genes (DEGs) in
adult liver after TCDD exposure, analyzed separately for female and male.

---

## Input Files

| File | Description |
|------|-------------|
| `/home/yan/TaRGET_II/ATAC-seq/DAR/OCR_allTissue.txt` | Master OCR table (all tissues, all exposures) |
| `/home/yan/TaRGET_II/Li_adt/BPA/share/20240130_liver_blood_treatment_deg_after_PseudoCtrl_using_all_control.txt` | Master DEG table (pseudo-control corrected) |
| `/home/yan/TaRGET_II/Li_adt/Target_expose/gencode_mm10_gene_TSS_pc_lncRNA.sorted.bed` | mm10 TSS reference (protein-coding + lncRNA) |

---

## Pipeline Steps

### Step 1 — Extract OCR subsets
Filter `OCR_allTissue.txt` for `Tissue=Li, Age=adt, Exposure=TCDD, Lab=AL`
separately for `Sex=F` and `Sex=M`. Add `status` column:

- `logFC > 0` → `MORE`
- `logFC < 0` → `LESS`

### Step 2 — Build BED files (DAR=TRUE peaks only)
Four header-free BED files, one per sex × direction:

```
TCDD_F_MORE.bed   TCDD_F_LESS.bed
TCDD_M_MORE.bed   TCDD_M_LESS.bed
```

The `annotation` column encodes peak identity:
`TCDD_{sex}_adt_{status}_{chr}_{start}_{end}`
(e.g. `TCDD_F_adt_MORE_chr12_69579660_69579922`)

### Step 3 — Sort BED files
`bedtools sort` in place, required for `bedtools closest -sorted`.

### Step 4 — Nearest-gene annotation
`bedtools closest -D a -k 1 -sorted` against the mm10 TSS reference.
Peaks with no TSS within **1 Mb** are discarded.

Output columns: `chr start end annotation gene_id gene_name gene_type strand dist_to_TSS`

### Step 5 — Extract matching DEGs
Filter the master DEG table:

| Group | DEG filter |
|-------|------------|
| Female | `cond1 = "TCDD_Female_adult"` AND `tissue = "liver"` |
| Male   | `cond1 = "TCDD_Male_adult"`   AND `tissue = "liver"` |

### Step 6 — Join nearest-gene × DEG (inner join)
Join on `gene_name == gene`. Each peak × matching DEG row is kept.
Final table has 26 columns (9 peak/gene + 17 DEG columns).

### Step 7 — Filter by padj ≤ 0.05
Final filtered output retains only rows where `padj <= 0.05`.
Rows with missing or `NA` padj values are dropped.

---

## Output Files

### `TCDD_F/` — Female
| File | Description |
|------|-------------|
| `OCR_Li_adt_F_TCDD_AL.txt` | Filtered OCR subset with status column |
| `DEG_Aylor_liver_adult_female_TCDD.txt` | Female TCDD adult liver DEGs |
| `TCDD_F_MORE.bed` | DAR peaks with increased accessibility |
| `TCDD_F_LESS.bed` | DAR peaks with decreased accessibility |
| `TCDD_F_MORE_nearestGene.txt` | MORE peaks annotated with nearest TSS |
| `TCDD_F_LESS_nearestGene.txt` | LESS peaks annotated with nearest TSS |
| `TCDD_F_MORE_nearestGene_DEG.txt` | MORE peaks joined with female DEGs |
| `TCDD_F_LESS_nearestGene_DEG.txt` | LESS peaks joined with female DEGs |
| `TCDD_F_MORE_nearestGene_DEG_padj05.txt` | Final: MORE peaks, padj ≤ 0.05 |
| `TCDD_F_LESS_nearestGene_DEG_padj05.txt` | Final: LESS peaks, padj ≤ 0.05 |

### `TCDD_M/` — Male
| File | Description |
|------|-------------|
| `OCR_Li_adt_M_TCDD_AL.txt` | Filtered OCR subset with status column |
| `DEG_Aylor_liver_adult_male_TCDD.txt` | Male TCDD adult liver DEGs |
| `TCDD_M_MORE.bed` | DAR peaks with increased accessibility |
| `TCDD_M_LESS.bed` | DAR peaks with decreased accessibility |
| `TCDD_M_MORE_nearestGene.txt` | MORE peaks annotated with nearest TSS |
| `TCDD_M_LESS_nearestGene.txt` | LESS peaks annotated with nearest TSS |
| `TCDD_M_MORE_nearestGene_DEG.txt` | MORE peaks joined with male DEGs |
| `TCDD_M_LESS_nearestGene_DEG.txt` | LESS peaks joined with male DEGs |
| `TCDD_M_MORE_nearestGene_DEG_padj05.txt` | Final: MORE peaks, padj ≤ 0.05 |
| `TCDD_M_LESS_nearestGene_DEG_padj05.txt` | Final: LESS peaks, padj ≤ 0.05 |

---

## Final Output Column Reference

| # | Column | Source | Description |
|---|--------|---------|-------------|
| 1 | chr | peak | Chromosome |
| 2 | start | peak | Peak start (0-based) |
| 3 | end | peak | Peak end |
| 4 | annotation | peak | `TCDD_{sex}_adt_{status}_{chr}_{start}_{end}` |
| 5 | gene_id | TSS | Ensembl gene ID |
| 6 | gene_name | TSS | Gene symbol |
| 7 | gene_type | TSS | `protein_coding` or `lncRNA` |
| 8 | strand | TSS | Gene strand |
| 9 | dist_to_TSS | bedtools | Distance from peak midpoint to TSS (bp, signed) |
| 10 | baseMean | DEG | Mean normalized expression |
| 11 | log2FoldChange | DEG | Log2 fold change (TCDD vs Ctrl) |
| 12 | lfcSE | DEG | Standard error of LFC |
| 13 | stat | DEG | Wald statistic |
| 14 | pvalue | DEG | Raw p-value |
| 15 | padj | DEG | BH-adjusted p-value |
| 16 | cond1 | DEG | Treatment condition |
| 17 | cond2 | DEG | Control condition |
| 18 | gene | DEG | Gene symbol (join key) |
| 19 | lab | DEG | Lab of origin |
| 20 | type | DEG | DEG analysis type |
| 21 | sig | DEG | Significant flag (TRUE/FALSE) |
| 22 | direction | DEG | `up` or `down` |
| 23 | group | DEG | Group label |
| 24 | tissue | DEG | Tissue |
| 25 | stage | DEG | Developmental stage |
| 26 | sex | DEG | Sex |

---

## Run

```bash
bash /home/yan/TaRGET_II/Li_adt/TCDD/TCDD_Li_adt_sex_pipeline.sh
```
