#!/bin/bash
#SBATCH -J suppa2  #job name
#SBATCH -p normal
#SBATCH -o myjob.out  # log name
#SBATCH -e myjob.err # err log name
#SBATCH --nodes=1
#SBATCH --nodelist=cpu3
#SBATCH --ntasks-per-node=1
#SBATCH --cpus-per-task=8


# =========================
# SUPPA2 Analysis
# =========================

# =========================
# Step a: Generate AS Events with SUPPA2
# =========================

# Input GTF (from gffcompare step)
gtf=/path/to/output/gtf/merged.gtf

# Output prefix for SUPPA2 results
out_prefix=/path/to/output/suppa/project.suppa

# Generate alternative splicing events
suppa.py generateEvents \
    -i ${gtf} \
    -o ${out_prefix} \
    -f ioe \
    -e SE AF MX RI A5 A3 AL



# Merge IOE files into one
awk 'FNR==1 && NR!=1 { while (/^<header>/) getline; } 1 {print}' /path/to/output/suppa/*.ioe \
> /path/to/output/suppa/project.all.events.ioe

# =========================
# Step b: Calculate PSI per Event with SUPPA2
# =========================

# Input merged IOE file (from generateEvents step)
ioe_merge_file=/path/to/output/suppa/project.all.events.ioe

# Isoform expression matrix (TPM values)
expression_matrix=/path/to/expression/isoform-count-matrix.txt

# Output prefix
out_prefix=/path/to/output/suppa/project_events

# Log file
log_file=/path/to/output/suppa/psiPerEvent_log.txt

# Run SUPPA2
suppa.py psiPerEvent \
    --ioe ${ioe_merge_file} \
    --expression-file ${expression_matrix} \
    -o ${out_prefix} \
    1> ${log_file} 2>&1 &



# Differential splicing analysis (events level)
suppa.py diffSplice \
    -m empirical -gc \
    -i /path/to/output/suppa/project.all.events.ioe \
    --save_tpm_events \
    -p cond1.psi cond2.psi \
    -e cond1.tpm cond2.tpm \
    -o /path/to/output/suppa/project_diffSplice

# =========================
# Step c: Differential Splicing with SUPPA2
# =========================

# Input merged IOE file (from generateEvents step)
ioe_merge_file=/path/to/output/suppa/project.all.events.ioe

# PSI files for two conditions
psi_cond1=/path/to/condition1.psi
psi_cond2=/path/to/condition2.psi

# TPM expression files for the same conditions
tpm_cond1=/path/to/condition1.tpm
tpm_cond2=/path/to/condition2.tpm

# Output prefix
out_prefix=/path/to/output/suppa/project_diffSplice

# Run SUPPA2 differential splicing
suppa.py diffSplice \
    -m empirical -gc \
    --input ${ioe_merge_file} \
    --save_tpm_events \
    -p ${psi_cond1} ${psi_cond2} \
    --tpm ${tpm_cond1} ${tpm_cond2} \
    -o ${out_prefix}
