-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_File_merging_namematch.R
More file actions
83 lines (54 loc) · 2.52 KB
/
1_File_merging_namematch.R
File metadata and controls
83 lines (54 loc) · 2.52 KB
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
rm(list = ls())
################
# dependencies #
################
library(stringr)
library(data.table)
library(dplyr)
results_folder <- paste0(getwd(), "/Results" ) # Full path to the subfolder
# Create the subfolder if it doesn't exist
if (!file.exists(results_folder)) {
dir.create(results_folder)
}
######################
# Variables #
# adapt for own data #
######################
category_folder <- "[add_path]"
phenotype_folder <- "[add_path]"
cell_intensity_folder <- "[add_path]"
metadata_path <- "[add_path]"
coords_path <- "[add_path]"
##########
# Script #
##########
category_files <- list.files(category_folder, pattern = ".csv", full.names = TRUE)
phenotype_files <- list.files(phenotype_folder, pattern = ".csv", full.names = TRUE)
cell_intensity_files <- list.files(cell_intensity_folder, pattern = ".csv", full.names = TRUE)
metadata <- read.csv((metadata_path), header = TRUE, sep = ";")
metadata <- metadata %>% rename(ROI = id)
#coords_files <- list.files(coords_folder, pattern = ".csv", full.names = TRUE)
coords <- read.csv((coords_path), header = TRUE, sep = ",")
coords <- coords %>%
mutate(ROI = str_extract(PathName_nucleus, "[^\\\\]+$") %>% str_remove("\\.ome$"))
coords <- coords %>% select(Location_Center_X, Location_Center_Y, ROI, ObjectNumber)
all_cells <- data.frame()
for (i in 1:length(phenotype_files)) {
ROI <- metadata$ROI[i]
phenotype <- fread(phenotype_files[grep(ROI, phenotype_files)], header = FALSE, sep = ",")
category <- fread(category_files[grep(ROI, category_files)], header = FALSE, sep = ",")
metrics<- fread(cell_intensity_files[grep(ROI, cell_intensity_files)], header = TRUE)
metrics <- metrics %>% mutate(ObjectNumber = row_number())
metrics$phenotype <- phenotype$V1
metrics$category <- category$V1
metrics <- cbind(metrics, ROI)
#print(paste0(ROI," ", nrow(phenotype)," ", nrow(metrics), " ", nrow(coords)))
all_cells <- rbind(all_cells, metrics)
}
all_cells_combined <- inner_join(all_cells, coords, by = c("ROI", "ObjectNumber"))
all_cells_combined <-inner_join(all_cells_combined, metadata, by = c("ROI"))
save(all_cells_combined, file = paste0(results_folder, "/Cell_matrix.Rdata"))
metadata_sample <- metadata %>% select(-ROI) %>% distinct()
save(metadata, file = paste0(results_folder, "/Metadata.Rdata"))
save(metadata_sample, file = paste0(results_folder, "/Metadata_sample.Rdata"))
print("created & saved cell matrix")