---
title: "Extracting intronic-only count-matrix for brown-nuclei ref2020-A"
author: Anushka Gupta
date: March 29 2021
---

# For this analysis, cellranger's ref2020-A reference was used as it contains lncRNAs
# This notebook was used to generate intronic only count matrix for brown preadipocyte snRNA-seq dataset.
# We first read in exonic and intronic count matrix and subtracted exonic-only count matrix to get intronic-only count matrix
# Exonic and intronic count matrix was generated using cellranger with a pre-mrna reference as input

```{r setup}
knitr::opts_knit$set(root.dir = '~/Desktop/scRNA-seq_snRNA-seq_adipocyte_lineages/')
```

```{r}
# load libraries
library(Seurat)
library(Matrix)

# Read exonic-only count matrix
mat.exonic <- Read10X(data.dir = "umi_counts/exonic_brown_nuclei_ref-2020-A/")

# Read  count matrix for both intronic and exonic reads
mat.intronic.exonic <- Read10X(data.dir = "umi_counts/intronic_exonic_brown_nuclei_ref-2020-A/")

# Subset both matrices to have high quality barcodes identified by performing QC.
# QC notebook can be found in pre-processing folder
# We will also do an intersection for exonic barcodes since a few high quality barcodes would not have been detected in exonic only analysis
singlets.high.quality.brown.nuclei <- read.table("additional_files/barcodes/singlets.high.quality.brown.nuclei.txt", quote="\"", comment.char="")
barcodes=c(singlets.high.quality.brown.nuclei$V1)
barcodes=intersect(barcodes,colnames(mat.exonic))
mat.exonic.subset=mat.exonic[,barcodes]
mat.intronic.exonic.subset=mat.intronic.exonic[,barcodes]

# Create intronic only count matrix
mat.intronic.subset=mat.intronic.exonic.subset-mat.exonic.subset

# Set any count value less than 0 as 0
mat.intronic.subset[mat.intronic.subset<0]=0

# Write output of intronic-only matrix in a folder organized as filtered_feature_matrix from cellranger
writeMM(obj = mat.intronic.subset,file = "umi_counts/intronic_brown_nuclei_ref-2020-A/matrix.mtx")
write.table(x = barcodes,file = "umi_counts/intronic_brown_nuclei_ref-2020-A/barcodes.tsv",quote = FALSE,sep = "\n",eol = "\n",row.names = FALSE,col.names = FALSE)
```