-
Notifications
You must be signed in to change notification settings - Fork 0
/
0.1.Prepare_Annotation_Files.Rmd
174 lines (119 loc) · 4.96 KB
/
0.1.Prepare_Annotation_Files.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
---
title: "RNAseq AAA Prepare Annotations Files"
author: "Gerard Temprano Sagrera"
date: "2022-12-07"
output: html_document
---
```{r, warning=FALSE, message=FALSE, echo=FALSE}
library(openxlsx);library(data.table);library(stringr);library(dplyr)
```
## Filtering:
* 1. Select "protein coding" and "lnRNA" genes.
* 2. Select annotation levels "1" and "2".
### Load, prepare and save v26 annotation file for GENES (used in our quantification):
```{r}
# Load annotation file:
ann <- fread("C://Users/Gerard/Desktop/AAA/RNAseq/Annotation/gencode.v26.annotation.gtf.gz", header = F, nThread = 6)
```
### Select only genes:
```{r}
ann <- ann[ann$V3 == "gene",]
```
```{r}
paste("We have", nrow(ann), "genes before filtering")
```
```{r}
# Keep only protein coding and lncRNA genes:
geneType <- unlist(lapply(ann$V9, function(x) strsplit(x, ";")[[1]][2]))
keepGeneType <- which(grepl("*protein_coding*", geneType) | grepl("*lincRNA*", geneType))
ann <- ann[keepGeneType,]
paste("We have", nrow(ann), "genes keeping only protein_coding and lincRNA annotations.")
```
```{r}
# Keep only annotation levels 1 and 2:
geneLevel <- unlist(lapply(ann$V9, function(x) strsplit(x, ";")[[1]][4]))
keepGeneLevel <- which(grepl("level 1", geneLevel) | grepl("level 2", geneLevel))
ann <- ann[keepGeneLevel,]
paste("We have", nrow(ann), "genes keeping only annotation levels 1 and 2.")
```
```{r}
# Add gene_id and gene_name to the annotation file:
ann <- ann[,c("V4","V5","V9")]
ann$gene_id <- str_split_fixed(ann$V9,";",7)[,1]
ann$gene_id <- gsub("gene_id ","",ann$gene_id)
ann$gene_id <- gsub("[^[:alnum:].]", "", ann$gene_id)
ann$gene_name <- str_split_fixed(ann$V9,";",7)[,3]
ann$gene_name <- gsub("gene_name","",ann$gene_name)
ann$gene_name <- gsub("[^A-Za-z0-9]","",ann$gene_name)
# Add length columns to the annotation file (will be used for TPM calculation):
ann$length <- ann$V5-ann$V4
# Remove old columns:
ann <- ann[,-c("V4","V5","V9")]
```
```{r}
#fwrite(ann, "C://Users/Gerard/Desktop/AAA/RNAseq/Annotation/gencode.v26.annotation.fixed.gtf.gz", sep = "\t")
```
```{r}
cc <- fread("C://Users/Gerard/Desktop/AAA/RNAseq/Gene_counts/Counts.RSEM.All.txt")
cc <- cc %>% remove_rownames %>% column_to_rownames(var="V1")
ann <- fread("C://Users/Gerard/Desktop/AAA/RNAseq/Annotation/gencode.v26.annotation.gtf.gz", header = F, nThread = 6)
ann$V9
ann$gene_id <- regmatches(ann$V9, regexpr("(?<=gene_id \\\")[^\\\"]+", ann$V9, perl=TRUE))
ann$gene_type <- regmatches(ann$V9, regexpr("(?<=gene_type \\\")[^\\\"]+", ann$V9, perl=TRUE))
table(ann[ann$gene_id %in% rownames(cc),]$gene_type)
```
### Load, prepare and save v26 annotation file for TRANSCRIPTS (used in our quantification):
```{r}
# Load annotation file:
ann <- fread("C://Users/Gerard/Desktop/AAA/RNAseq/Annotation/gencode.v26.annotation.gtf.gz", header = F, nThread = 6)
```
### Select only genes:
```{r}
ann <- ann[ann$V3 == "transcript",]
```
```{r}
paste("We have", nrow(ann), "transcripts before filtering")
```
```{r}
# Keep only protein coding and lncRNA genes:
geneType <- unlist(lapply(ann$V9, function(x) strsplit(x, ";")[[1]][3]))
keepGeneType <- which(grepl("*protein_coding*", geneType) | grepl("*lincRNA*", geneType))
ann <- ann[keepGeneType,]
paste("We have", nrow(ann), "transcripts keeping only protein_coding and lincRNA annotations.")
```
```{r}
# Keep only annotation levels 1 and 2:
geneLevel <- unlist(lapply(ann$V9, function(x) strsplit(x, ";")[[1]][7]))
keepGeneLevel <- which(grepl("level 1", geneLevel) | grepl("level 2", geneLevel))
ann <- ann[keepGeneLevel,]
paste("We have", nrow(ann), "genes keeping only annotation levels 1 and 2.")
```
```{r}
# Add gene_id and gene_name to the annotation file:
ann <- ann[,c("V4","V5","V9")]
ann$transcript_id <- str_split_fixed(ann$V9,";",7)[,2]
ann$transcript_id <- gsub("transcript_id ","",ann$transcript_id)
ann$transcript_id <- gsub("[^[:alnum:].]", "", ann$transcript_id)
ann$gene_id <- str_split_fixed(ann$V9,";",7)[,1]
ann$gene_id <- gsub("gene_id ","",ann$gene_id)
ann$gene_id <- gsub("[^[:alnum:].]", "", ann$gene_id)
ann$gene_name <- str_split_fixed(ann$V9,";",7)[,4]
ann$gene_name <- gsub("gene_name","",ann$gene_name)
ann$gene_name <- gsub("[^A-Za-z0-9]","",ann$gene_name)
# Add length columns to the annotation file (will be used for TPM calculation):
ann$length <- ann$V5-ann$V4
# Remove old columns:
ann <- ann[,-c("V4","V5","V9")]
```
```{r}
fwrite(ann, "C://Users/Gerard/Desktop/AAA/RNAseq/Annotation/gencode.v26.annotation.fixed.transcripts.gtf.gz", sep = "\t")
```
```{r}
cc <- fread("C://Users/Gerard/Desktop/AAA/RNAseq/Gene_counts/Counts.RSEM.All.txt")
cc <- cc %>% remove_rownames %>% column_to_rownames(var="V1")
ann <- fread("C://Users/Gerard/Desktop/AAA/RNAseq/Annotation/gencode.v26.annotation.gtf.gz", header = F, nThread = 6)
ann$V9
ann$gene_id <- regmatches(ann$V9, regexpr("(?<=gene_id \\\")[^\\\"]+", ann$V9, perl=TRUE))
ann$gene_type <- regmatches(ann$V9, regexpr("(?<=gene_type \\\")[^\\\"]+", ann$V9, perl=TRUE))
table(ann[ann$gene_id %in% rownames(cc),]$gene_type)
```