---
title: "SGNEx_Illumina_StarF"
author: "bhaas"
date: '2024-02-01'
output: github_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```


```{r}
# parse starF predictions based on Illumina reads

starF_dir = "data/STAR-Fusion_SG-NEx/"

starF_fusion_files = list.files(starF_dir, pattern="*.abridged.tsv")

StarF_data = NULL

for (starF_file in starF_fusion_files) {
    
    starF_file_path = paste0(starF_dir, "/", starF_file)
    message("parsing ", starF_file_path)
    starF_fusions = read.csv(starF_file_path, header=T, sep="\t", com='') %>%
        rename(FusionName = X.FusionName)
    
    starF_fusions$sample = starF_file
    
    StarF_data = bind_rows(StarF_data, starF_fusions)
    
    
}


StarF_data$sample = str_replace(StarF_data$sample, ".star-fusion.fusion_predictions.abridged.tsv", "")
StarF_data$sample = str_replace(StarF_data$sample, "SGNex_", "")

StarF_data$gene1 = sapply(StarF_data$FusionName, function(x) { str_split(x, "--")[[1]][1]})
StarF_data$gene2 = sapply(StarF_data$FusionName, function(x) { str_split(x, "--")[[1]][2]})

StarF_data$sample = sapply(StarF_data$sample, function(x) { str_split(x, "_")[[1]]}[1])
StarF_data = StarF_data %>% rowwise() %>% mutate(lex_ordered_fusion_name = paste0(sample, "|", paste0(collapse="--", sort(c(gene1, gene2)))))

```

```{r}

# limit to the top number of reads fusion isoform.
StarF_data = StarF_data %>% group_by(lex_ordered_fusion_name) %>% arrange(desc(est_J), desc(est_S)) %>% filter(row_number() == 1) %>% ungroup()

nrow(StarF_data)

```


```{r}

all_LR_pred_fusions = read.csv("../data/preds.collected.gencode_mapped.wAnnot.filt.pass.proxy_assignments.gz", header=T, sep="\t")

all_LR_pred_fusions$fusion_name_use = sapply(all_LR_pred_fusions$proxy_fusion_name, function(x) { str_split(x, "\\|")[[1]][2]  } )

all_LR_pred_fusions = all_LR_pred_fusions %>% rowwise() %>% 
    mutate(lex_ordered_fusion_name = paste0(sample, "|", paste0(collapse="--", sort(str_split(fusion_name_use, "--")[[1]]))))

```


```{r}

all_LR_pred_fusions_progs_agree = all_LR_pred_fusions %>% select(sample, lex_ordered_fusion_name, prog) %>% unique() %>%
    group_by(sample, lex_ordered_fusion_name) %>% arrange(prog) %>%
    mutate(num_progs = n(), progs = paste(prog, collapse=',')) %>%
    ungroup() %>%
    select(sample, lex_ordered_fusion_name, num_progs, progs) %>% unique()

all_LR_pred_fusions_progs_agree %>% head()

```

```{r}


StarF_data = left_join(StarF_data, all_LR_pred_fusions_progs_agree, by=c('sample', 'lex_ordered_fusion_name') )

StarF_data %>% head()

```



```{r}

StarF_data = StarF_data %>% mutate(has_long_read_support = (! is.na(progs)) )

StarF_data %>%  head()

```


```{r}

table(StarF_data$has_long_read_support)

```


```{r}

write.table(StarF_data %>% 
                select(sample, FusionName, est_J, est_S, FFPM, SpliceType, lex_ordered_fusion_name,
                       has_long_read_support, num_progs, progs
                       ) %>%
                arrange(sample, desc(est_J), desc(est_S)),
            file="StarFusion_SGNEx_Illumina.tsv",
            sep="\t", quote=F, row.names=F)


```



