flowchart LR

%%{init: {
  "themeVariables": {
     "background": "transparent",
     "fontSize": "45px" },
  "flowchart": {
    "nodeSpacing": 80,
    "rankSpacing": 80,
    "padding": 35,
    "htmlLabels": true
}}}%%

  %% Input
  subgraph INPUT["<span style='white-space:nowrap'>Input Check</span>"]
    Raw["Raw Fastqs"]:::input
    Raw --> Check["Check inputs"]:::process
    Samplesheet["Sample Sheet"]:::input --> Check
    Contrasts:::input --> Check
    Check --> valid_raw["Valid Raw Fastqs"]:::input
  end


  %% Quality Control
  subgraph QC["<span style='white-space:nowrap'>Quality Control</span>"]
    valid_raw --> Trimming["Cutadapt: Adapter removal & quality trimming"]:::tool
    Trimming --> Trimmed["Trimmed Fastqs"]:::output
    Trimmed --> fqscreen["FastqScreen"]:::tool
    valid_raw --> fqc["FASTQC"]:::tool
    Trimmed --> fqc
  end

%% make sure MultiQC is outside any subgraph
fqc --> MultiQC["multiqc report"]:::output
fqscreen --> MultiQC

%% Library & Complexity Assessment
BAMs --> preseq["Preseq - library complexity curve"]:::tool
BAMs --> ppqt["PhantomPeakQualtools"]:::tool
preseq ---> MultiQC
ppqt --> MultiQC

  %% Alignment
  subgraph ALIGNMENT["Alignment"]
    Trimmed --> Blacklist["Align to blacklist regions; discard reads"]:::process
    Blacklist --> Align["Align to reference genome, deduplicate, filter"]:::process
    Align --> BAMs["Deduplicated BAM & tagalign files"]:::output
    %% Normalization
    BAMs --> Spike["Spike-in normalization (optional)"]:::process
    Spike --> Scale["Scaling factors"]:::output
  end


  %% deepTools Analysis
  subgraph DEEPTOOLS["deepTools"]
    BAMs --> BAMcov["BAM Coverage"]:::tool
    BAMcov --> Bigwig["BigWig files"]:::output
    Bigwig --> Normalize["Normalize Input (ChIP/Input)"]:::tool
    Bigwig --> Correlation["Plot Correlation"]:::tool
    Bigwig --> PCA["Plot PCA"]:::tool
    Normalize --> CorrelationNorm["Plot Correlation (normalized)"]:::tool
    Bigwig --> Matrix["Compute Matrix"]:::tool
    Matrix --> Profile["Plot Profile"]:::tool
    Matrix --> Heatmap["Plot Heatmap"]:::tool
    BAMs --> Fingerprint["Plot Fingerprint"]:::tool

  end

  Scale -.-> BAMcov

  PCA --> MultiQC
  Profile --> MultiQC
  Heatmap --> MultiQC
  Fingerprint --> MultiQC
  Correlation --> MultiQC
  CorrelationNorm --> MultiQC

  %% Peak Calling
  subgraph PEAK["<span style='white-space:nowrap'>Call Peaks</span>"]
    BAMs --> Peakcalling["Identify peaks"]:::process
    Peakcalling --> MACS2broad["MACS2 broad"]:::tool
    Peakcalling --> MACS2narrow["MACS2 narrow"]:::tool
    Peakcalling --> GEM["GEM"]:::tool
    Peakcalling --> SICER["SICER"]:::tool
    MACS2narrow --> Consensus["Consensus peaks"]:::process
    GEM --> Consensus
    MACS2broad --> Consensus
    SICER --> Consensus
  end

  subgraph DIFFERENTIAL["<span style='white-space:nowrap'>Differential Analysis</span>"]
    Consensus --> RepCheck["Check replicates"]:::process
    RepCheck --> Diffbind["<span style='white-space:nowrap'>DiffBind<br>(>=2 reps)</span>"]:::tool
    RepCheck --> Manorm["<span style='white-space:nowrap'>MAnorm<br>(1 rep)</span>"]:::tool
  end

  subgraph ANNOTATION["<span style='white-space:nowrap'>Annotation &amp; Motifs</span>"]
    Consensus --> Annotate["<span style='white-space:nowrap'>Annotate peaks<br>(ChIPseeker)</span>"]:::tool
    Consensus --> Motifs["<span style='white-space:nowrap'>Find motifs<br>(HOMER, MEME-AME)</span>"]:::tool
  end

  %% Styles - FNL branding theme
  classDef input fill:#ffffff,stroke:#528230,stroke-width:2px;
  classDef process fill:#dceef7,stroke:#19424e,stroke-width:2px;
  classDef tool fill:#b1ee85,stroke:#528230,stroke-width:2px;
  classDef output fill:#ecba4c,stroke:none;

  %% Subgraph styling
  style INPUT fill:#e6e6e6,stroke:none;
  style QC fill:#e6e6e6,stroke:none;
  style ALIGNMENT fill:#e6e6e6,stroke:none;
  style DEEPTOOLS fill:#e6e6e6,stroke:none;
  style PEAK fill:#e6e6e6,stroke:none;
  style DIFFERENTIAL fill:#e6e6e6,stroke:none;
  style ANNOTATION fill:#e6e6e6,stroke:none;
