Skip to content

Commit 4171e69

Browse files
committed
Built in review
1 parent d8dcd4a commit 4171e69

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

data/locus_coeruleus/locus_coeruleus.R

+12-25
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ spe <- WeberDivechaLCdata_Visium()
3838

3939
coords <- as.data.frame(spe@int_colData$spatialCoords)
4040
colnames(coords) <- c('x', 'y')
41+
coords_rownames <- rownames(spe@int_colData$spatialCoords)
4142

4243
counts <- spe@assays@data$counts
4344

@@ -51,16 +52,6 @@ counts_func <- function(x){
5152
}
5253
counts_lc <- unlist(lapply(colnames(counts), counts_func))
5354

54-
obs_func <- function(x){
55-
fields <- unlist(strsplit(x, "_"))
56-
if ( length(fields) == 4 ){
57-
return(fields[4])
58-
} else{
59-
return(fields[5])
60-
}
61-
}
62-
observations_lc <- unlist(lapply(colnames(counts), obs_func))
63-
6455
LC_samples <- unique(spe@colData$sample_id)
6556

6657
out_dir <- 'out_dir'
@@ -82,9 +73,10 @@ for (lc in LC_samples){
8273

8374
print(lc)
8475
dir <- paste0(out_dir, "/", lc)
85-
# TODO Please check this again beause I had to flip the coordinates to get the pictures as seen in:
86-
# https://libd.shinyapps.io/locus-c_Visium/. See ggplot below.
76+
77+
# Write coordinates.tsv
8778
coords_subset <- coords[which(spe@colData$sample_id == lc),]
79+
rownames(coords_subset) <- coords_rownames[which(spe@colData$sample_id == lc)]
8880
write.table(coords_subset, file = paste0(dir, "/coordinates.tsv"), col.names = NA,
8981
sep = "\t", quote = FALSE, row.names = TRUE)
9082

@@ -95,31 +87,26 @@ for (lc in LC_samples){
9587
counts_subset <- t(counts_subset)
9688
writeMM(counts_subset, file = paste0(dir, "/counts.mtx"))
9789

98-
observations_subset <- observations_lc[which(counts_lc == lc)]
90+
observations_subset <- spe@colData[which(counts_lc == lc),]
91+
rownames(observations_subset) <- lapply(rownames(observations_subset), function(x){tail(unlist(strsplit(x,"_")),1)})
9992
write.table(observations_subset, file = paste0(dir, "/observations.tsv"), col.names = NA, sep = "\t", quote = FALSE)
10093

10194
labels <- spe@colData$annot_region[which(spe@colData$sample_id == lc)]
10295
labels[which(labels == TRUE)] <- "LC"
10396
labels[which(labels == FALSE)] <- "non_LC"
104-
write.table(labels, file = paste0(dir, "/labels.tsv"), col.names = NA, sep = "\t", quote = FALSE)
105-
106-
# TODO #Create the scatter plot
107-
# library(ggplot2)
108-
# plot_df <- coords[which(spe@colData$sample_id == lc),]
109-
# plot_df$label <- labels
110-
# plot_df
111-
# ggplot(plot_df, aes(x = x, y = y, color=label)) +
112-
# geom_point() +
113-
# coord_fixed() + # Keep aspect ratio equal
114-
# scale_y_reverse() # Flip the y-axis
97+
98+
labels_df <- data.frame(label = labels)
99+
rownames(labels_df) <- rownames(observations_subset)
100+
write.table(labels_df, file = paste0(dir, "/labels.tsv"), col.names = NA, sep = "\t", quote = FALSE)
115101

116102
# Fill metadata
117103
patient_list <- c(patient_list, as.character(unique(spe@colData[which(spe@colData$sample_id == lc),]$donor_id)))
118104
sample_list <- c(sample_list, lc)
119105
directory_list <- c(directory_list, dir)
120106

121107
# Write features.tsv
122-
features <- rownames(spe@assays@data$counts)
108+
features <- as.data.frame(spe@rowRanges)
109+
rownames(features) <- spe@rowRanges$gene_id
123110
write.table(features, file = paste0(dir,"/features.tsv"), col.names = NA, sep = "\t", quote = FALSE)
124111
}
125112

0 commit comments

Comments
 (0)