-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrough-draft_handle-matrices-gtfs.R
267 lines (203 loc) · 8.16 KB
/
rough-draft_handle-matrices-gtfs.R
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# rough-draft_handle-matrices-gtfs.R
# KA
# 2023-0331
# ============================================================================
# Determine "types" in combined_AG.sans-chr.gtf ==============================
# ============================================================================
# library(GenomicRanges)
# library(rtracklayer)
# library(tidyverse)
#
# setwd("~/projects-etc/2022-2023_RRP6-NAB3/results/2023-0215")
# getwd()
#
# list.dirs()
# list.files("./infiles_gtf-gff3/already")
#
# combined_AG <- list.files("./infiles_gtf-gff3/already")[2]
# combined_AG <- rtracklayer::import(
# paste("./infiles_gtf-gff3/already", combined_AG, sep = "/")
# )
# combined_AG <- combined_AG %>% as.data.frame()
#
# types <- combined_AG$type %>% as.factor() %>% table() %>% names()
# types
#
# rm(combined_AG, types)
# ============================================================================
# Load in and process htseq-count counts matrix ==============================
#+ ...associated with combined_SC_KL_20S.gff3
# ============================================================================
library(GenomicRanges)
library(rtracklayer)
library(tidyverse)
setwd("~/projects-etc/2022-2023_RRP6-NAB3/results/2023-0215")
getwd()
t_mat <- "all-samples.combined-SC-KL-20S.hc-strd-eq.mRNA.tsv"
t_mat <- readr::read_tsv(t_mat, show_col_types = FALSE) %>%
dplyr::slice(-1) # Need to remove the first row, which contains string
#+ for source bam
colnames(t_mat) <- colnames(t_mat) %>%
gsub("\\.UT_prim_UMI\\.hc-strd-eq\\.tsv$", "", .) %>%
gsub("\\.UT_prim_UMI\\.hc-strd-op\\.tsv$", "", .)
t_mat$features <- t_mat$features %>%
gsub("^transcript\\:", "", .) %>%
gsub("_mRNA", "", .)
# Load in Excel spreadsheet of samples names and variables -------------------
#+ #NOTE To be used later, just loading it in now
p_xl <- "notebook" #INPATH
f_xl <- "variables.xlsx" #INFILE
t_xl <- readxl::read_xlsx(
paste(p_xl, f_xl, sep = "/"), sheet = "master", na = "NA"
)
rm(p_xl, f_xl)
# To associate features (mRNA) with metadata, load combined_SC_KL_20S.gff3 ---
list.files("./infiles_gtf-gff3/already")
t_gff3 <- list.files("./infiles_gtf-gff3/already")[3]
t_gff3 <- rtracklayer::import(
paste("./infiles_gtf-gff3/already", t_gff3, sep = "/")
)
t_gff3 <- t_gff3 %>% as.data.frame() %>% dplyr::as_tibble()
# Subset combined_SC_KL_20S.gff3 for ID "mRNA" -------------------------------
#+ (specified in the call to htseq-count)
t_gff3 <- t_gff3[t_gff3$type == "mRNA", ]
t_gff3$ID <- t_gff3$ID %>%
gsub("^transcript\\:", "", .) %>%
gsub("_mRNA", "", .)
# Subset tibble to keep only relevant columns --------------------------------
keep <- c(
"seqnames", "start", "end", "width", "strand", "type", "ID", "biotype",
"Name"
)
t_gff3 <- t_gff3[, colnames(t_gff3) %in% keep]
rm(keep)
# Convert column Name from list to character vector --------------------------
#+ ...and replace empty fields NA character values
t_gff3$Name <- ifelse(
as.character(t_gff3$Name) == "character(0)",
NA_character_,
as.character(t_gff3$Name)
)
# Rename column "seqnames" to "chr" and column "Name" to "names" -------------
t_gff3 <- t_gff3 %>% dplyr::rename(
c(chr = seqnames, names = Name, features = ID)
)
# Join t_mat and t_gff3 ------------------------------------------------------
t_mat <- dplyr::full_join(t_gff3, t_mat, by = "features")
rm(t_gff3)
# Order tibble by chromosome names and feature start positions ---------------
chr_SC <- c(
"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII",
"XIII", "XIV", "XV", "XVI", "Mito"
)
chr_KL <- c("A", "B", "C", "D", "E", "F")
chr_20S <- "20S"
chr_order <- c(chr_SC, chr_KL, chr_20S)
t_mat$chr <- t_mat$chr %>% as.factor()
t_mat$chr <- ordered(t_mat$chr, levels = chr_order)
t_mat <- t_mat %>% dplyr::arrange(chr, start)
# Categorize chromosomes by genome of origin ---------------------------------
t_mat$genome <- ifelse(
t_mat$chr %in% chr_SC,
"S_cerevisiae",
ifelse(
t_mat$chr %in% chr_KL,
"K_lactis",
ifelse(
t_mat$chr %in% chr_20S,
"20S",
NA
)
)
) %>%
as.factor()
# Move the new column "genome" to a better location in the tibble ------------
t_mat <- t_mat %>% dplyr::relocate("genome", .before = "chr")
# Check on variable/column "genome" ------------------------------------------
levels(t_mat$genome)
t_mat %>%
dplyr::group_by(genome) %>%
dplyr::summarize(tally = length(genome))
# The code returns...
# 20S = 1, K_lactis = 5076, S_cerevisiae = 6600, NA = 5
# Clean up -------------------------------------------------------------------
rm(chr_KL, chr_SC, chr_order)
# ============================================================================
# Load in and process htseq-count counts matrix ==============================
#+ ...associated with combined_AG.sans-chr.gff3
# ============================================================================
library(GenomicRanges)
library(rtracklayer)
library(tidyverse)
setwd("~/projects-etc/2022-2023_RRP6-NAB3/results/2023-0215")
getwd()
t_mat <- "all-samples.combined-AG.hc-strd-eq.XUT.tsv"
t_mat <- readr::read_tsv(t_mat, show_col_types = FALSE) %>%
dplyr::slice(-1) # Need to remove the first row, which contains string
#+ for source bam
colnames(t_mat) <- colnames(t_mat) %>%
gsub("\\.UT_prim_UMI\\.hc-strd-eq\\.tsv$", "", .) %>%
gsub("\\.UT_prim_UMI\\.hc-strd-op\\.tsv$", "", .)
#QUESTION 1/2 No processing needed with type XUT? How about other types in
#QUESTION 2/2 combined_AG.sans-chr.gff3?
# t_mat$features <- t_mat$features %>%
# gsub("^transcript\\:", "", .) %>%
# gsub("_mRNA", "", .)
# Load in Excel spreadsheet of samples names and variables -------------------
#+ #NOTE To be used later, just loading it in now
p_xl <- "notebook" #INPATH
f_xl <- "variables.xlsx" #INFILE
t_xl <- readxl::read_xlsx(
paste(p_xl, f_xl, sep = "/"), sheet = "master", na = "NA"
)
rm(p_xl, f_xl)
# To associate features (mRNA) with metadata, load combined_SC_KL_20S.gff3 ---
list.files("./infiles_gtf-gff3/already")
t_gff3 <- list.files("./infiles_gtf-gff3/already")[2]
t_gff3 <- rtracklayer::import(
paste("./infiles_gtf-gff3/already", t_gff3, sep = "/")
)
t_gff3 <- t_gff3 %>% as.data.frame() %>% dplyr::as_tibble()
# Subset combined_SC_KL_20S.gff3 for ID "mRNA" -------------------------------
#+ (specified in the call to htseq-count)
t_gff3 <- t_gff3[t_gff3$type == "XUT", ]
#QUESTION 1/2 No processing needed with type XUT? How about other types in
#QUESTION 2/2 combined_AG.sans-chr.gff3?
# t_gff3$gene_id <- t_gff3$gene_id %>%
# gsub("^transcript\\:", "", .) %>%
# gsub("_mRNA", "", .)
# Subset tibble to keep only relevant columns --------------------------------
keep <- c(
"seqnames", "start", "end", "width", "strand", "type", "gene_id"
)
t_gff3 <- t_gff3[, colnames(t_gff3) %in% keep]
rm(keep)
# Rename column "seqnames" to "chr" and column "Name" to "names" -------------
t_gff3 <- t_gff3 %>% dplyr::rename(c(chr = seqnames, features = gene_id))
# Join t_mat and t_gff3 ------------------------------------------------------
t_mat <- dplyr::full_join(t_gff3, t_mat, by = "features")
rm(t_gff3)
# Order tibble by chromosome names and feature start positions ---------------
chr_SC <- c(
"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII",
"XIII", "XIV", "XV", "XVI"
)
t_mat$chr <- t_mat$chr %>% as.factor()
t_mat$chr <- ordered(t_mat$chr, levels = chr_SC)
t_mat <- t_mat %>% dplyr::arrange(chr, start)
# Categorize chromosomes by genome of origin ---------------------------------
t_mat$genome <- ifelse(
t_mat$chr %in% chr_SC, "S_cerevisiae", NA
) %>%
as.factor()
# Move the new column "genome" to a better location in the tibble ------------
t_mat <- t_mat %>% dplyr::relocate("genome", .before = "chr")
# Check on variable/column "genome" ------------------------------------------
levels(t_mat$genome)
t_mat %>%
dplyr::group_by(genome) %>%
dplyr::summarize(tally = length(genome))
# The code returns...
# S_cerevisiae = 1657, NA = 5
# Clean up -------------------------------------------------------------------
rm(chr_KL, chr_SC, chr_order)