# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .r
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: R 4.0
#     language: R
#     name: ir40
# ---

# + [markdown] toc=true
# <h1>Table of Contents<span class="tocSkip"></span></h1>
# <div class="toc"><ul class="toc-item"><li><span><a href="#Dependencies" data-toc-modified-id="Dependencies-1">Dependencies</a></span></li><li><span><a href="#Functions" data-toc-modified-id="Functions-2">Functions</a></span></li><li><span><a href="#Paths" data-toc-modified-id="Paths-3">Paths</a></span></li><li><span><a href="#Main" data-toc-modified-id="Main-4">Main</a></span><ul class="toc-item"><li><span><a href="#CBTN" data-toc-modified-id="CBTN-4.1">CBTN</a></span></li><li><span><a href="#DKFZ" data-toc-modified-id="DKFZ-4.2">DKFZ</a></span></li></ul></li></ul></div>
# -

# # Dependencies

library(data.table)

# # Functions

# # Paths

manifestpath <- "/Users/anabbi/OneDrive - UHN/Documents/IPD2/Manifests/"
datapath <- "/Users/anabbi/OneDrive - UHN/Documents/IPD2/Data/"
plotpath <- "/Users/anabbi/OneDrive - UHN/Documents/IPD2/Plots/"

# # Main

load(paste0(datapath, "ESTIMATE/estimate_manifest_primary_clean_final.RData"))

# ## CBTN

# This is just manifest export for arriba tsv files on cavatica

arriba_manifest <- read.csv(paste0(manifestpath,"arriba_manifest.csv"),header = T, stringsAsFactors = F)

head(arriba_manifest)

# This file was made on cavatica notebook (analysis matrices) by compiling all arriba tsv files

arriba_cbtn <- read.table(paste0(datapath, "fusion/all_arriba_CBTTC.tsv"),sep = "\t", header = T, 
                               comment.char = "", quote = "", stringsAsFactors = F)

head(arriba_cbtn)

arriba_manifest$Filename <- gsub(".arriba.fusions.tsv", "",arriba_manifest$name)

arriba_df_manifest <- merge(arriba_manifest[,c("sample_id", "aliquot_id","Filename")], arriba_cbtn, by = "Filename")

arriba_df_cbtn <- merge(arriba_df_manifest, estimate_manifest_primary_clean[,c("sample_id", "aliquot_id")], 
                         by = c("sample_id", "aliquot_id"))

table(estimate_manifest_primary_clean$group)

# +
length(unique(arriba_cbtn$Filename))

length(unique(arriba_df_cbtn$Filename))
# -

colnames(arriba_df_cbtn)[colnames(arriba_df_cbtn) == "X.gene1"] <- "gene1"

head(arriba_df_cbtn)

arriba_df_cbtn$Filename <- NULL

# ## DKFZ

# This is from Pengbo. Use colnames from cbttc

arriba_dkfz <- fread(paste0(datapath, "fusion/arriba_ICGC_pengbo.txt"), sep = "\t", header = F)

head(arriba_dkfz)

colnames(arriba_dkfz)[2:25] <- colnames(arriba_df_cbtn)[3:26]

colnames(arriba_dkfz)[1] <- "sample_id"

arriba_df_dkfz <- merge(arriba_dkfz, estimate_manifest_primary_clean[,c("sample_id", "aliquot_id")], by = "sample_id")

# +
length(unique(arriba_dkfz$sample_id))

length(unique(arriba_df_dkfz$sample_id))
# -

dkfz <- estimate_manifest_primary_clean[estimate_manifest_primary_clean$group == "ICGC",]

allsampleswitharriba <- unique(arriba_dkfz$sample_id)

dkfz$sample_id[!dkfz$sample_id %in% allsampleswitharriba]

# There was no Arriba call available for the samples above

arriba_df <- rbind(arriba_df_cbtn, arriba_df_dkfz)

save(arriba_df, file = paste0(datapath, "fusion/arriba_df.RData"))

table(arriba_df$confidence)

# Remove low confidence

arriba_df_conf <- arriba_df[arriba_df$confidence != "low",]

# Only splice-site to splice-site

head(arriba_df_conf)

dim(arriba_df_conf)

arriba_df_conf_ss <- arriba_df_conf[ arriba_df_conf$site1 == "splice-site",]
arriba_df_conf_ss <- arriba_df_conf_ss[ arriba_df_conf_ss$site2 == "splice-site",]

dim(arriba_df_conf_ss)

save(arriba_df_conf_ss, file = paste0(datapath, "fusion/arriba_df_conf_ss.RData"))



StarcoderdataJupyterScriptsDedupFiltered