cmatkhan commited on
Commit
db55b4e
·
verified ·
1 Parent(s): 653a9f6

Upload pull_gse236892.R with huggingface_hub

Browse files
Files changed (1) hide show
  1. pull_gse236892.R +138 -0
pull_gse236892.R ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ library(GEOquery)
2
+ library(SummarizedExperiment)
3
+ library(tidyverse)
4
+ library(here)
5
+ library(AnnotationDbi)
6
+ library(org.Hs.eg.db)
7
+ library(GenomicRanges)
8
+ library(txdbmaker)
9
+ library(org.Hs.eg.db)
10
+
11
+ # retrieve annotation info
12
+ txdb_path <- here("~/projects/earli/data/txdb_gencode_v49.rds")
13
+
14
+ if (file.exists(txdb_path)) {
15
+ txdb <- AnnotationDbi::loadDb(txdb_path)
16
+ } else {
17
+ gtf_url <- "https://ftp.ebi.ac.uk/pub/databases/gencode/Gencode_human/release_49/gencode.v49.annotation.gtf.gz"
18
+ gtf_tmp <- tempfile(fileext = ".gtf.gz")
19
+ curl::curl_download(gtf_url, gtf_tmp, quiet = FALSE)
20
+ txdb <- txdbmaker::makeTxDbFromGFF(gtf_tmp, format = "gtf")
21
+ AnnotationDbi::saveDb(txdb, txdb_path)
22
+ }
23
+
24
+ hemoglobin_genes <- c(
25
+ ENSG00000206172 = "HBA1",
26
+ ENSG00000188536 = "HBA2",
27
+ ENSG00000244734 = "HBB",
28
+ ENSG00000229988 = "HBBP1",
29
+ ENSG00000223609 = "HBD",
30
+ ENSG00000213931 = "HBE1",
31
+ ENSG00000213934 = "HBG1",
32
+ ENSG00000196565 = "HBG2",
33
+ ENSG00000206177 = "HBM",
34
+ ENSG00000086506 = "HBQ1",
35
+ ENSG00000130656 = "HBZ",
36
+ ENSG00000206178 = "HBZP1"
37
+ )
38
+
39
+ # retrieve phenotype data
40
+ gset <- getGEO("GSE236892", GSEMatrix = TRUE, getGPL = TRUE)
41
+ gset <- gset[[1]]
42
+
43
+ pdata <- pData(gset) |>
44
+ as_tibble(rownames = "geo_accession_row") |>
45
+ mutate(
46
+ sepsis_group = str_extract(title, "(?<=_)(Hyper|Hypo)$"),
47
+ cnts_colnames = str_remove(title, "_(?<=_)(Hyper|Hypo)$")
48
+ )
49
+
50
+ GSE236892_meta <- pdata |>
51
+ dplyr::select(geo_accession, cnts_colnames, `age:ch1`, `gender:ch1`, `lca:ch1`) |>
52
+ dplyr::rename(age = `age:ch1`, sex = `gender:ch1`, lca_label = `lca:ch1`) |>
53
+ column_to_rownames("cnts_colnames") |>
54
+ mutate(
55
+ lca_label = factor(lca_label, levels = c("Hypo", "Hyper")),
56
+ sex = factor(sex),
57
+ # label columns which had non-numeric age
58
+ imputed_age = age == "90+",
59
+ # strip non-numeric characters and cast to integer
60
+ age = as.integer(str_remove(age, "\\+")),
61
+ # scale age to avoid near collinearity with the intercept. Note
62
+ # that this is only necessary or useful if age is used as a predictor
63
+ age_scaled = as.numeric(scale(age))
64
+ ) |>
65
+ as_tibble(rownames = "sample_id")
66
+
67
+ # retrieve count matrix
68
+ supp_files <- getGEOSuppFiles("GSE236892", baseDir = tempdir(), fetch_files = TRUE)
69
+ cnt_path <- rownames(supp_files)[str_detect(rownames(supp_files), "cnt_data")]
70
+
71
+ cnt_mat <- read_csv(cnt_path) |>
72
+ dplyr::rename(ensg = `...1`) |>
73
+ column_to_rownames("ensg") |>
74
+ dplyr::select(all_of(GSE236892_meta$cnts_colnames)) |>
75
+ as.matrix()
76
+ storage.mode(cnt_mat) <- "integer"
77
+
78
+
79
+ gencode_genes <- suppressMessages(genes(txdb))
80
+ names(gencode_genes) <- str_remove(names(gencode_genes), "\\.\\d+$")
81
+
82
+ ensembl_meta <- AnnotationDbi::select(
83
+ org.Hs.eg.db,
84
+ keys = names(gencode_genes),
85
+ columns = c("ENSEMBL", "SYMBOL", "GENENAME", "GENETYPE"),
86
+ keytype = "ENSEMBL"
87
+ ) |>
88
+ as_tibble() |>
89
+ filter(!is.na(ENSEMBL)) |>
90
+ distinct(ENSEMBL, .keep_all = TRUE)
91
+
92
+ mcols(gencode_genes) <- ensembl_meta[
93
+ match(names(gencode_genes), ensembl_meta$ENSEMBL),
94
+ c("ENSEMBL", "SYMBOL", "GENENAME", "GENETYPE")
95
+ ]
96
+
97
+ cnt_ensg <- rownames(cnt_mat)
98
+ mapped <- gencode_genes[names(gencode_genes) %in% cnt_ensg]
99
+ unmapped_ids <- setdiff(cnt_ensg, names(gencode_genes))
100
+
101
+ unmapped <- GRanges(
102
+ seqnames = rep("chrUn", length(unmapped_ids)),
103
+ ranges = IRanges(start = seq_along(unmapped_ids), width = 1),
104
+ ENSEMBL = unmapped_ids,
105
+ SYMBOL = NA_character_,
106
+ GENENAME = NA_character_,
107
+ GENETYPE = NA_character_
108
+ )
109
+ names(unmapped) <- unmapped_ids
110
+
111
+ row_gr <- c(mapped, unmapped)[cnt_ensg]
112
+
113
+ mcols(row_gr)$autosomal_protein_coding <- (
114
+ !is.na(mcols(row_gr)$GENETYPE) &
115
+ mcols(row_gr)$GENETYPE == "protein-coding" &
116
+ as.character(seqnames(row_gr)) %in% paste0("chr", 1:22)
117
+ )
118
+
119
+ mcols(row_gr)$hemoglobin_related <- names(row_gr) %in% names(hemoglobin_genes)
120
+
121
+ feature_meta <- feature_meta <- as_tibble(row_gr) |>
122
+ dplyr::rename(feature_id = ENSEMBL) |>
123
+ dplyr::relocate(feature_id) |>
124
+ janitor::clean_names()
125
+
126
+ stopifnot(identical(colnames(cnt_mat), GSE236892_meta$sample_id))
127
+ stopifnot(identical(rownames(cnt_mat), feature_meta$feature_id))
128
+
129
+ GSE236892_expr_long <- cnt_mat |>
130
+ as_tibble(rownames = "feature_id") |>
131
+ pivot_longer(-feature_id, names_to = "sample_id", values_to = "value") |>
132
+ arrange(sample_id, feature_id)
133
+
134
+ dir.create("~/projects/hf_sepsis_collection/GSE236892")
135
+
136
+ arrow::write_parquet(GSE236892_expr_long, "~/projects/hf_sepsis_collection/GSE236892/expression.parquet")
137
+ arrow::write_parquet(feature_meta, "~/projects/hf_sepsis_collection/GSE236892/feature_metadata.parquet")
138
+ arrow::write_parquet(GSE236892_meta, "~/projects/hf_sepsis_collection/GSE236892/sample_metadata.parquet")