-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalization_comparison_markdown.Rmd
617 lines (451 loc) · 20.8 KB
/
normalization_comparison_markdown.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
---
title: "nCounter_RNA_Analysis"
author: "Ned Cauley"
date: "12/13/23"
output: html_document
---
# This markdown and analysis based on https://github.com/bhattacharya-a-bt/CBCS_normalization/blob/master/sabry_analysis.R
# The above analysis is from the publication https://academic.oup.com/bib/article/22/3/bbaa163/5891144?login=true
```{r setup}
knitr::opts_chunk$set(echo = TRUE)
```
## Set up libraries and QC functions libraries
```{r Libraries and RUVseq functions, message=FALSE, warning=FALSE}
library(NanoStringQCPro)
library(ggplot2)
library(EnvStats)
library(MASS)
library(RUVSeq)
library(DESeq2)
library(limma)
library(matrixStats)
library(limma)
library(NanoStringNorm)
library(readxl)
library(plotly)
# The library in RSW to use is located at
# /home/cauleyes/ccbr-data/renv_cache/ncounter_rna/Snapshot-environment_method/renv/library/R-4.1/x86_64-pc-linux-gnu
# Set the working directory to your project folder if it is not set already
setwd("/home/cauleyes/ccbr-data/users/Ned/nCounter_rna/CCBR1245_Ramaswami_nCounter")
# Functions for RUVseq analysis
source("R/ncounter_analysis_functions.R")
# Load the annotation file
annotation_file_path <- "annotations/5_2_24_ncounter_rna_annotation_edit_NC.xlsx"
annotation_df <- read_excel(annotation_file_path)
```
```{r Define the contrast for the DEG analysis, message=FALSE, warning=FALSE}
# Define the contrast and levels from the annotation columns for QC and DEG analysis
#contrast <- "CD4_group"
contrast <- "KS_alone"
#contrast <- "KSHV_VL_Bin"
# Levels are: Reference, Effect.
# This means a positive log2foldchnage = lower reference, higher effect
#contrast_levels <- c("High", "Low")
contrast_levels <- c("Y", "N")
#contrast_levels <- c("0", "1")
```
## Setup the initial dataframes with read counts and metadata/QC
```{r Initial Dataframes and QC, message=FALSE, warning=FALSE}
# Gather the names of the RCC files
files.RCC = list.files("/rstudio-files/ccbr-data/users/Ned/nCounter_rna/data/CCBR1245/rcc")
files.RCC = files.RCC[grepl('RCC',files.RCC)]
# Add name of sub folder containing RCC files
rcc_folder <- "/rstudio-files/ccbr-data/users/Ned/nCounter_rna/data/CCBR1245/rcc"
#files.RCC <- file.path(rcc_folder, files.RCC)
# Gather number of genes and samples
rcc.example.file <- file.path(rcc_folder, files.RCC[1])
#genes.count = nrow(readRcc(file.path(rcc_folder, files.RCC[1])$Code_Summary))
genes.count = nrow(readRcc(rcc.example.file)$Code_Summary)
ncol = length(files.RCC)
# Set up the dataframe to hold all sample and gene data
raw_expression = as.data.frame(matrix(nrow = genes.count,ncol = length(files.RCC)+2))
colnames(raw_expression)[1:2] = c('Gene','Class')
# Set up a dataframe to hold metadata and QC
pData = as.data.frame(matrix(nrow = length(files.RCC),ncol = 11))
colnames(pData) = c('BCAC_ID','SampleID','Owner','Comments','Date','GeneRLF','SystemAPF','imagingQC',
'bindingDensityQC','limitOfDetectionQC','positiveLinearityQC')
# Populate gene and class columns of expression df
raw_expression[,1:2] = readRcc(rcc.example.file)$Code_Summary[,c(2,1)]
print("RCC files to be processed:")
# Populate expression df, run QC, and record flags for each sample
for (i in 1:length(files.RCC)){
print(files.RCC[i])
rcc.path <- file.path(rcc_folder, files.RCC[i])
rcc = readRcc(rcc.path)
raw = rcc$Code_Summary
raw_expression[,i+2] = as.numeric(raw$Count)
colnames(raw_expression)[i+2] = strsplit(files.RCC[i],'_')[[1]][3]
pData[i,2:7] = as.vector(rcc$Sample_Attributes)
pData$imagingQC[i] = imagingQC(rcc)
pData$imaging[i] <- as.numeric(rcc$Lane_Attributes[3])
pData$bindingDensityQC[i] = bindingDensityQC(rcc,.05,2.25)
pData$bindingDensity[i] <- as.numeric(rcc$Lane_Attributes[6])
pData$limitOfDetectionQC[i] = limitOfDetectionQC(rcc)
pData$limitOfDetection[i] <- limitOfDetectionScore(rcc)
pData$positiveLinearityQC[i] = positiveLinQC(rcc)
pData$positiveLinearityQC[i] <- positiveLinScore(rcc)
}
# Correct the sample ID column name and sample name IDs
# Correct for annotation
colnames(annotation_df)[4] <- "SampleID"
annotation_df$"SampleID" <- gsub("-", " ", annotation_df$"SampleID")
annotation_df$"SampleID" <- gsub("\\sSB", "SB", annotation_df$"SampleID")
# Correct for pdata
pData$"SampleID" <- gsub("-", " ", pData$"SampleID")
pData$"SampleID" <- gsub("\\sSB", "SB", pData$"SampleID")
# Rename and factor the KS alone field and sort by Yes or No
colnames(annotation_df)[colnames(annotation_df) == "KS alone"] <- "KS_alone"
colnames(annotation_df)[colnames(annotation_df) == "CD4 count...11"] <- "CD4_group"
colnames(annotation_df)[colnames(annotation_df) == "KSHV VL Bin"] <- "KSHV_VL_Bin"
# Combine all metadata into a single dataframe
merged_pData <- merge(pData, annotation_df, by = "SampleID")
merged_pData[[contrast]] <- factor(merged_pData[[contrast]], levels = contrast_levels)
# Create a feature df
raw = raw_expression[,-c(1:2)]
fData = raw_expression[,c(1:2)]
rownames(raw) = fData$Gene
# Correct the Sample ID column names for preceding spaces and hyphens
colnames(raw) <- gsub("^\\s+", "", colnames(raw))
colnames(raw) <- gsub("-", " ", colnames(raw))
# List the information for the feature data frame
str(fData)
count_endog <- sum(fData$Class == "Endogenous")
count_neg <- sum(fData$Class == "Negative")
count_pos <- sum(fData$Class == "Positive")
count_hk <- sum(fData$Class == "Housekeeping")
print(paste0("Endogenous genes: ",count_endog))
print(paste0("Negative genes: ",count_neg))
print(paste0("Positive genes: ",count_pos))
print(paste0("Housekeeping geness: ",count_hk))
# List the housekeeping genes and label in the fData df
cIdx <- fData$Gene[fData$Class == "Housekeeping"]
# Identify missing housekeeping genes in any samples
merged_pData$HK_Gene_Miss = colSums(raw[cIdx,] == 0)
# Estbalish all of the rownames of the three dfs
rownames(fData) = fData$Gene
rownames(raw) = fData$Gene
rownames(merged_pData) <- merged_pData$SampleID
# Ensure the order of the SampleID columns in the expression data frame match the order of the rows in the metadata
raw_reorder <- raw[, rownames(merged_pData)]
# Remove outlier sample
remove_outlier <- FALSE
if(remove_outlier == TRUE){
outlier <- "SB 19 5920"
# Remove from annotation
merged_pData <- merged_pData[merged_pData$SampleID != outlier, ]
# Remove from counts
raw_reorder <- raw_reorder[, !names(raw_reorder) %in% outlier]
}
print("Initial data frames")
head(raw_reorder)
head(merged_pData)
head(fData)
```
## Verify that housekeeping genes are not DEGs
```{r, message=FALSE, warning=FALSE}
###
#### CHECK IF HK ARE ASSOCIATED WITH PRIMARY PHENO
###
# Gather the read counts for housekeeping genes
hk_raw = raw_reorder[cIdx,]
pval = vector(length = nrow(hk_raw))
# Run general linear model to get pvalue for housekeeping
for (i in 1:nrow(hk_raw)){
reg = glm.nb(as.numeric(hk_raw[i,]) ~ as.factor(merged_pData[[contrast]]))
pval[i] = coef(summary(reg))[2,4]
}
print("Housekeeping genes:")
print(rownames(hk_raw))
# Number of housekeeping that are DE
print("Number of housekeeping genes that are DE:")
sum(pval <= .05)
```
## Run RUVseq differential expression analysis
```{r, message=FALSE, warning=FALSE}
# Number of sources of variance to identify with RUVseq
k = 1
# Remove samples with NA from the read counts dataframe
NA_rows <- which(!complete.cases(merged_pData[[contrast]]))
NA_samples <- rownames(merged_pData[NA_rows, ])
if(length(NA_rows) > 0){
raw_reorder <- raw_reorder[, -which(names(raw_reorder) %in% NA_samples)]
}
# Remove sample with NA from the annotation
if(length(NA_rows) > 0){
merged_pData <- merged_pData[complete.cases(merged_pData[[contrast]]), ]
}
# DEseq transform for RUVseq
vsd = RUV_total(raw_reorder,merged_pData,fData,k = k)$vsd
# Set Expression Set
set = RUV_total(raw_reorder,merged_pData,fData,k = k)$set
# Remove row of the contrast that contain NA values
set <- set[complete.cases(pData(set)$KSHV_VL_Bin), ]
# Save the results
save(vsd,file = paste0("cauley_ruv_vsd_k_",k,".rda"))
save(set,file = paste0("cauley_ruv_set_k_",k,".rda"))
i = 1
results = paste0("cauley_deg_k",i,".csv")
load(paste0("cauley_ruv_set_k_",i,".rda"))
# DEseq Dataset
dds <- DESeqDataSetFromMatrix(countData = counts(set)[1:nrow(set),],
colData = pData(set),
design = as.formula(paste("~ W_1 + Date + ", contrast)))
# Run DEseq
dds <- DESeq(dds)
# Generate contrasts per group
contrast_ruv <- as.data.frame(results(dds,contrast = c(contrast, contrast_levels)))
contrast_ruv_deg_list <- contrast_ruv[contrast_ruv$pvalue < 0.05, ]
# Reformat the DEG table to be exported
ruv_deg_list_export <- contrast_ruv_deg_list
ruv_deg_list_export$gene <- rownames(ruv_deg_list_export)
rownames(ruv_deg_list_export) <- NULL
ruv_deg_list_export <- ruv_deg_list_export[, c("gene", setdiff(names(ruv_deg_list_export), "gene"))]
ruv_deg_list_export$normalization <- "RUVseq"
# Export Normalized read counts
export_norm_counts <- FALSE
if(export_norm_counts == TRUE){
# Grab the normalized_counts
normalized_counts <- as.data.frame(counts(dds, normalized = TRUE))
# Add a gene column
gene <- rownames(normalized_counts)
normalized_counts <- cbind(gene, normalized_counts)
# Export
write.csv(normalized_counts, file = paste0("Results/RUV_normalized_counts.csv"), row.names = FALSE)
}
# Plot PCA to check for batch effects
# Prepare the counts
rlog_dds <- rlog(dds)
# Annotation to view PCA by
PCA.group <- "Date"
# PCA Plot
PCA.plot <- plotPCA(rlog_dds, intgroup = PCA.group, ntop = 700)
# Plotly PCA for interactivity
PCA.plot.data <- plotPCA(rlog_dds, intgroup = PCA.group, ntop = 700, returnData = TRUE)
PCA.plotly <- plot_ly(data = PCA.plot.data, x = ~PC1, y = ~PC2, text = ~name, mode = "markers", color = ~factor(group)) %>%
layout(title = "PCA Plot",
xaxis = list(title = "PC1"),
yaxis = list(title = "PC2"))
print(PCA.plotly)
# Export the PCA plot
export.PCA <- FALSE
if(export.PCA == TRUE){
ggsave(paste0("Results/QC/PCA.pdf"), plot = PCA.plot)
}
```
## Run nSolver differential expression analysis
```{r, message=FALSE, warning=FALSE}
# Set up the DEseq matrix for nsolver
dds.nsolver <- DESeqDataSetFromMatrix(countData = counts(set)[1:nrow(set),],
colData = pData(set),
as.formula(paste("~", contrast)))
pos = raw_reorder[grepl('POS',rownames(raw_reorder)),]
# Housekeeping genes expression
hk = raw_reorder[grepl('Housekeeping',raw_expression$Class),]
# Set up nSolver
pos.factors = mean(as.numeric(apply(pos,2,geoMean)))/as.numeric(apply(pos,2,geoMean))
hk.factors = mean(as.numeric(apply(hk,2,geoMean)))/as.numeric(apply(hk,2,geoMean))
sizeFactors(dds.nsolver) <- pos.factors * hk.factors
# Run DEseq
dds.nsolver <- DESeq(dds.nsolver)
# Generate contrasts for each group for nSolver analysis
contrast_nsolver <- as.data.frame(results(dds.nsolver,contrast = c(contrast,contrast_levels)))
contrast_nsolver_deg_list <- contrast_nsolver[contrast_nsolver$pvalue < 0.05, ]
# Reformat the DEG table to be exported
nsolver_deg_list_export <- contrast_nsolver_deg_list
nsolver_deg_list_export$gene <- rownames(nsolver_deg_list_export)
rownames(nsolver_deg_list_export) <- NULL
nsolver_deg_list_export <- nsolver_deg_list_export[, c("gene", setdiff(names(nsolver_deg_list_export), "gene"))]
nsolver_deg_list_export$normalization <- "nCounter"
# Overlapping genes between the methods
contrast_nsolver_deg_genes <- rownames(contrast_nsolver_deg_list)
contrast_ruv_deg_genes <- rownames(contrast_ruv_deg_list)
shared_gene_list <- intersect(contrast_nsolver_deg_genes, contrast_ruv_deg_genes)
# Create the total DEG list from each norm type for export
combined_DEG_list_export <- rbind(ruv_deg_list_export, nsolver_deg_list_export)
# Write out the DEG list, if desired
# Only significant
export_sig_DEG_list <- TRUE
if(export_sig_DEG_list == TRUE){
write.csv(combined_DEG_list_export, file = paste0("Results/DEG/DEG_list_",contrast,".csv"), row.names = FALSE)
}
# All results
export_all_DEG_list <- TRUE
if(export_all_DEG_list == TRUE){
write.csv(contrast_ruv, file = paste0("Results/DEG/all_DEG_results_",contrast,".csv"), row.names = TRUE)
}
```
# Create the plot for comparing RUVseq vs. nSolver p-values
```{r pvalue_ruvseq_v_nsolver, message=FALSE, warning=FALSE}
# Create comparison plots between RUVseq and nSolver analysis
pval.plot.contrast = data.frame(Method = rep(c('nSolver','RUVSeq'),each = nrow(contrast_ruv)),
P = c(contrast_nsolver$pvalue,contrast_ruv$pvalue))
pval.plot.contrast$Contrast = contrast
pval.plot.contrast$Contrast = as.factor(pval.plot.contrast$Contrast)
pvals = ggplot(data = pval.plot.contrast,
aes(x = P,color = Method,
fill = Method)) + geom_histogram(alpha = .2) +
facet_wrap(~Contrast) + theme_minimal() +
theme(axis.text=element_text(size=12),
axis.title=element_text(size=24),
plot.title = element_text(size = 30),
legend.title=element_text(size=20),
legend.text=element_text(size=20),
strip.text = element_text(size=24),
panel.spacing=unit(1, "lines"),
panel.border = element_rect(color = "grey",
fill = NA, size = .1),
legend.position = 'bottom') +
scale_color_manual(values = c("#377EB8","#E41A1C","#4DAF4A")) +
scale_fill_manual(values = c("#377EB8","#E41A1C","#4DAF4A")) +
ylab('Count')
print(pvals)
# Export the plot, if desired
export_pval_plot <- TRUE
if(export_pval_plot == TRUE){
ggsave(paste0("Results/QC/pval_plot_",contrast,".pdf"), plot = pvals)
}
```
## Set up boxplots for comparing counts
```{r, message=FALSE, warning=FALSE}
#### IL2
# Create RUVseq vs. nSolver comparison plot for IL2
# Gather stat sig DEGs from each method
contrast_ruv_degs = subset(contrast_ruv,pvalue < 0.05)
contrast_nsolver_degs = subset(contrast_nsolver,pvalue < 0.05)
nrow(contrast_ruv_degs)
nrow(contrast_nsolver_degs)
# Overlapping DEGs
length(intersect(rownames(contrast_ruv_degs),rownames(contrast_nsolver_degs)))
degs.both = intersect(rownames(contrast_ruv_degs),rownames(contrast_nsolver_degs))
# Label DEGS from each method
contrast_ruv_degs_unique = rownames(contrast_ruv_degs)[!(rownames(contrast_ruv_degs) %in% degs.both)]
contrast_nsolver_degs_unique = rownames(contrast_nsolver_degs)[!(row.names(contrast_nsolver_degs) %in% degs.both)]
# Set up matrix for il2 comparison boxplot
boxplot.df = data.frame(Method = c(rep('Both',length(degs.both)),
rep('RUVSeq',length(contrast_ruv_degs_unique)),
rep('nSolver',length(contrast_nsolver_degs_unique))),
Median = c(rowMedians(as.matrix(log2(raw_reorder[rownames(raw_reorder) %in% degs.both,]+1))),
rowMedians(as.matrix(log2(raw_reorder[rownames(raw_reorder) %in% contrast_ruv_degs_unique,]+1))),
rowMedians(as.matrix(log2(raw_reorder[rownames(raw_reorder) %in% contrast_nsolver_degs_unique,]+1)))))
#boxplot.df$Contrast = 'KS_alone'
#boxplot.df$Contrast = 'CD4_group'
boxplot.df$Contrast = contrast
# Run Nanostring normalization
colnames(raw_expression)[1:2] = c('Name','Code.Class')
NanoString.mRNA.norm <- NanoStringNorm(
x = raw_expression,
anno = NA,
CodeCount = 'sum',
Background = 'none',
SampleContent = 'housekeeping.sum',
round.values = FALSE,
take.log = TRUE,
return.matrix.of.endogenous.probes = TRUE
)
boxplot.df$Contrast = as.factor(boxplot.df$Contrast)
```
## Create the boxplot for count comparison
```{r compare_counts_boxplot, message=FALSE, warning=FALSE}
# Create boxplot
boxplot = ggplot(data = boxplot.df,
aes(x = Method,
y = Median)) + geom_boxplot() +
geom_jitter(shape=16, position=position_jitter(0.2)) +
theme_minimal() +
theme(axis.text=element_text(size=12),
axis.title=element_text(size=24),
plot.title = element_text(size = 30),
legend.title=element_text(size=20),
legend.text=element_text(size=20),
strip.text = element_text(size=24),
panel.spacing=unit(1, "lines"),
panel.border = element_rect(color = "grey",
fill = NA, size = .1),
legend.position = 'bottom') + ylab('Median raw log-counts') + facet_wrap(~Contrast)
print(boxplot)
# Export the plot, if desired
export_boxplot_plot <- TRUE
if(export_boxplot_plot == TRUE){
ggsave(paste0("Results/QC/boxplot_plot_",contrast,".pdf"), plot = boxplot)
}
```
# Set up the plot to compare log fold change
```{r, message=FALSE, warning=FALSE}
# Set up dot plot
# Gather genes for each method and overlapping genes
#res_il2.plot = subset(res_il2,rownames(res_il2) %in% c(il2.both,rownames(il2.nsolver),
# rownames(il2.ruv)))
contrast.plot = subset(contrast_ruv,rownames(contrast_ruv) %in% c(degs.both,rownames(contrast_nsolver_degs),
rownames(contrast_ruv_degs)))
contrast.nsolver.plot = subset(contrast_nsolver,
rownames(contrast_ruv) %in%
c(degs.both,rownames(contrast_nsolver_degs),rownames(contrast_ruv_degs)))
# Matrix of fold change per method
contrast.plot.tot = data.frame(Gene = rownames(contrast.plot),
nSolverFC = contrast.nsolver.plot$log2FoldChange,
nSolverSE = contrast.nsolver.plot$lfcSE,
RUVFC = contrast.plot$log2FoldChange,
RUVSE = contrast.plot$lfcSE)
# Label fold change matrix
contrast.plot.tot$Method = ifelse(contrast.plot.tot$Gene %in% degs.both,'Both',
ifelse(contrast.plot.tot$Gene %in% rownames(contrast_nsolver_degs),
'nSolver only','RUVSeq only'))
#contrast.plot.tot$Contrast = 'ks_alone'
#contrast.plot.tot$Contrast = 'CD4_group'
contrast.plot.tot$Contrast = contrast
# Create the comparison dot plot data
contrast.plot.tot$Contrast = as.factor(contrast.plot.tot$Contrast)
contrast.plot.tot$Method = factor(contrast.plot.tot$Method,
levels = c('Both','nSolver only','RUVSeq only'))
contrast.plot.tot = contrast.plot.tot[order(contrast.plot.tot$Method,decreasing = T),]
```
## Create the plot for comparing fold change between analysis methods
```{r fold_change_comparison_plot, message=FALSE, warning=FALSE}
fc.plot = ggplot(data = contrast.plot.tot,
aes(x = RUVFC, y = nSolverFC, color = Method, text = Gene)) + geom_point(aes(size = RUVSE), alpha = 0.5) +
scale_color_manual(values = c("#4DAF4A","#377EB8","#E41A1C")) +
geom_hline(yintercept = 0,linetype = 2) +
geom_vline(xintercept = 0,linetype = 2) + theme_minimal() +
theme(axis.text=element_text(size=12),
axis.title=element_text(size=10),
plot.title = element_text(size = 10),
legend.title=element_text(size=10),
legend.text=element_text(size=10),
strip.text = element_text(size=10),
panel.spacing=unit(1, "lines"),
panel.border = element_rect(color = "grey",
fill = NA, size = .1),
legend.position = 'bottom') + facet_wrap(~Contrast) +
xlab('Log-fold change (RUVSeq)') + ylab('Log-fold change (nSolver)') +
labs(size = 'SE (RUVSeq)') + geom_abline(slope = 1,intercept = 0,linetype = 2, color='grey') +
guides(colour = guide_legend(override.aes = list(size=4)))
#print(fc.plot)
fc.plotly <- ggplotly(fc.plot, tooltip = "text")
# Print the plot
print(fc.plotly)
# Export the plot, if desired
export_fc_plot <- TRUE
if(export_fc_plot == TRUE){
ggsave(paste0("Results/QC/foldchange_plot_",contrast,".pdf"), plot = fc.plot)
}
```
# Get the read count information for CXCL8
``` {r}
normalized_CXCL8 <- set@assayData$counts[rownames(set@assayData$counts) == "CXCL8", ]
CXCL8_df <- data.frame(CXCL8_normalized = normalized_CXCL8, SampleID = names(normalized_CXCL8))
rownames(CXCL8_df) = NULL
CXCL8_anno_columns <- c("SampleID","KS_alone","CD4_group")
subset_anno_df <- annotation_df[CXCL8_anno_columns]
CXCL8_df <- merge(CXCL8_df, subset_anno_df, by = "SampleID")
CXCL8_mean_KS_Y <- mean(CXCL8_df$CXCL8_normalized[CXCL8_df$KS_alone == "Y"], na.rm = TRUE)
CXCL8_mean_KS_N <- mean(CXCL8_df$CXCL8_normalized[CXCL8_df$KS_alone == "N"], na.rm = TRUE)
CXCL8_df$KS_alone_mean <- ifelse(CXCL8_df$KS_alone == "Y", CXCL8_mean_KS_Y, CXCL8_mean_KS_N)
CXCL8_mean_CD4_High <- mean(CXCL8_df$CXCL8_normalized[CXCL8_df$CD4_group == "High"], na.rm = TRUE)
CXCL8_mean_CD4_Low <- mean(CXCL8_df$CXCL8_normalized[CXCL8_df$CD4_group == "Low"], na.rm = TRUE)
CXCL8_df$CD4_group_mean <- ifelse(CXCL8_df$CD4_group == "High", CXCL8_mean_CD4_High, CXCL8_mean_CD4_Low)
CXCL8_deg <- ruv_deg_list_export[ruv_deg_list_export$gene == "CXCL8", ]
export_CXCL8_summary <- TRUE
if(export_CXCL8_summary == TRUE){
write.csv(CXCL8_df, file = "Results/CXCL8_summary.csv", row.names = FALSE)
}
```