File size: 6,195 Bytes
c033470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5036080
c033470
 
 
 
 
 
 
 
 
 
 
 
5036080
c033470
 
 
 
 
 
 
 
 
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
# Version info: R 4.2.2, Biobase 2.58.0, GEOquery 2.66.0, limma 3.54.0
################################################################
#   Data plots for selected GEO samples
library(GEOquery)
library(limma)
library(hgu219.db)
library(umap)
library(here)
library(tidyverse)

# load series and platform data from GEO

dir.create("data/gse65682")
gset <- getGEO("GSE65682", destdir = "data/gse65682", GSEMatrix = TRUE, getGPL = FALSE)
if (length(gset) > 1) idx <- grep("GPL13667", attr(gset, "names")) else idx <- 1
gset <- gset[[idx]]

ex <- exprs(gset)
# log2 transform
qx <- as.numeric(quantile(ex, c(0., 0.25, 0.5, 0.75, 0.99, 1.0), na.rm = T))
LogC <- (qx[5] > 100) ||
    (qx[6] - qx[1] > 50 && qx[2] > 0)
if (LogC) {
    ex[which(ex <= 0)] <- NaN
    ex <- log2(ex)
}

# box-and-whisker plot
dev.new(width = 3 + ncol(gset) / 6, height = 5)
par(mar = c(7, 4, 2, 1))
title <- paste("GSE65682", "/", annotation(gset), sep = "")
boxplot(ex, boxwex = 0.7, notch = T, main = title, outline = FALSE, las = 2)
dev.off()

# expression value distribution plot
par(mar = c(4, 4, 2, 1))
title <- paste("GSE65682", "/", annotation(gset), " value distribution", sep = "")
plotDensities(ex, main = title, legend = F)

# mean-variance trend
ex <- na.omit(ex) # eliminate rows with NAs
plotSA(lmFit(ex), main = "Mean variance trend, GSE65682")

# UMAP plot (multi-dimensional scaling)
ex <- ex[!duplicated(ex), ] # remove duplicates
ump <- umap(t(ex), n_neighbors = 15, random_state = 123)
plot(ump$layout, main = "UMAP plot, nbrs=15", xlab = "", ylab = "", pch = 20, cex = 1.5)

clean_char <- function(x, prefix_regex) {
    val <- str_squish(str_remove(x, prefix_regex))
    na_if(val, "NA")
}

pdat_mars <- as_tibble(pData(gset), rownames = "sample_id") |>
    select(sample_id, starts_with("characteristics"))

purrr::imap(pdat_mars |> select(-sample_id), ~ count(tibble(value = .x), value, name = "n") |> arrange(desc(n)))

gse65682_metadata <- as_tibble(pData(gset), rownames = "sample_id") |>
    mutate(
        healthy_control                = str_detect(title, "healthy"),
        title_date                     = str_extract(title, "\\d\\d_\\d{2}_\\d{4}"),
        title_identifier1              = str_extract(title, "(?<=\\d{2}_\\d{2}_\\d{4}_)\\w\\d+"),
        title_identifier2              = str_extract(title, "\\d+(?=\\])"),
        sex                            = clean_char(characteristics_ch1, "^gender:\\s*"),
        age                            = as.numeric(clean_char(characteristics_ch1.1, "^age:\\s*")),
        pneumonia_diagnosis            = clean_char(characteristics_ch1.2, "^pneumonia diagnoses:\\s*"),
        thrombocytopenia               = clean_char(characteristics_ch1.3, "^thrombocytopenia:\\s*"),
        endotype_cohort                = clean_char(characteristics_ch1.4, "^endotype_cohort:\\s*"),
        endotype_class                 = clean_char(characteristics_ch1.5, "^endotype_class:\\s*"),
        mortality_28d                  = as.logical(as.integer(clean_char(characteristics_ch1.6, "^mortality_event_28days:\\s*"))),
        time_to_event_28d              = as.numeric(clean_char(characteristics_ch1.7, "^time_to_event_28days:\\s*")),
        icu_acquired_infection         = clean_char(characteristics_ch1.8, "^icu_acquired_infection:\\s*"),
        icu_acquired_infection_paired  = clean_char(characteristics_ch1.9, "^icu_acquired_infection_paired:\\s*"),
        diabetes_mellitus              = clean_char(characteristics_ch1.10, "^diabetes_mellitus:\\s*"),
        abdominal_sepsis_or_control    = clean_char(characteristics_ch1.11, "^abdominal_sepsis_and_controls:\\s*")
    ) |>
    dplyr::select(
        sample_id, title_identifier1, title_identifier2, healthy_control, sex,
        age, pneumonia_diagnosis, thrombocytopenia, endotype_cohort, endotype_class,
        mortality_28d, time_to_event_28d, icu_acquired_infection, icu_acquired_infection_paired,
        diabetes_mellitus, abdominal_sepsis_or_control
    )

stopifnot(gse65682_metadata |> filter(!is.na(endotype_class)) |> count(is.na(mortality_28d)) |> pull(n) == 479)

your_probes_mars <- rownames(exprs(gset))
biocu219_probes <- keys(hgu219.db, keytype = "PROBEID")

length(your_probes_mars)
length(biocu219_probes)
length(intersect(your_probes_mars, biocu219_probes))
stopifnot(mean(!your_probes_mars %in% biocu219_probes) == 0)

anno_u219 <- AnnotationDbi::select(
    hgu219.db,
    keys = your_probes_mars,
    columns = c("SYMBOL", "ENTREZID", "ENSEMBL", "GENENAME", "UNIPROT"),
    keytype = "PROBEID"
)

gse65682_feature_metadata <- anno_u219 |>
    group_by(PROBEID) |>
    reframe(
        ENSEMBL = paste(unique(na.omit(ENSEMBL)), collapse = ";"),
        ENTREZID = dplyr::first(ENTREZID),
        SYMBOL = dplyr::first(SYMBOL),
        GENENAME = dplyr::first(GENENAME),
        UNIPROT = paste(unique(na.omit(UNIPROT)), collapse = ";")
    ) |>
    ungroup() |>
    mutate(across(c(ENSEMBL, UNIPROT), ~ na_if(.x, ""))) |>
    rename(feature_id = PROBEID) |>
    right_join(tibble(feature_id = your_probes_mars), by = "feature_id")

stopifnot(nrow(gse65682_feature_metadata) == length(your_probes_mars))
stopifnot(!any(duplicated(gse65682_feature_metadata$feature_id)))

mean(is.na(gse65682_feature_metadata$SYMBOL)) # for comparison against the other datasets' rates

gse65682_expr_long <- as_tibble(exprs(gset), rownames = "feature_id") |>
    pivot_longer(-feature_id, names_to = "sample_id", values_to = "value") |>
    arrange(sample_id, feature_id)

stopifnot(nrow(gse65682_expr_long) == nrow(exprs(gset)) * ncol(exprs(gset)))
stopifnot(all(unique(gse65682_expr_long$sample_id) %in% gse65682_metadata$sample_id))
stopifnot(all(unique(gse65682_expr_long$feature_id) %in% gse65682_feature_metadata$feature_id))

stopifnot(!any(duplicated(gse65682_metadata$sample_id)))
stopifnot(nrow(gse65682_metadata) == 802)

dir.create("data/gse65682/parquet", recursive = TRUE, showWarnings = FALSE)

arrow::write_parquet(gse65682_metadata, "data/gse65682/parquet/sample_metadata.parquet")
arrow::write_parquet(gse65682_feature_metadata, "data/gse65682/parquet/feature_metadata.parquet")
arrow::write_parquet(gse65682_expr_long, "data/gse65682/parquet/expression.parquet")