0
$\begingroup$

I want to upload an excel file sheet that has certain barcodes that I would like to show on my umap. How do I go about adding the file and linking it to the metadata?

Below is my following code.

# set up the working directory
wd = "/home/PTX_AAC656
setwd(wd)

# load counts
# SD-52-JZ33 patient sample but aligned to both mouse and human
SD_52_JZ33.data <- Read10X(data.dir = "../PTX_AAC656/metadata/patient_sample/SD-52-JZ33/outs/filtered_feature_bc_matrix/")
BNM_human_only.data <- Read10X(data.dir = "../PTX_AAC656/metadata/human_only_alignment/SD-52-JZ33_BNM_5K/outs/filtered_feature_bc_matrix/")
BNL_human_only.data <- Read10X(data.dir = "../PTX_AAC656/metadata/human_only_alignment/SD-52-JZ33_BNL_5K/outs/filtered_feature_bc_matrix/")
BNL_mouse_only.data <- Read10X(data.dir = "../PTX_AAC656/metadata/mouse_only_alignment/SD-52-JZ33_BNL_5K/outs/filtered_feature_bc_matrix/")
BNM_mouse_only.data <- Read10X(data.dir = "../PTX_AAC656/metadata/mouse_only_alignment/SD-52-JZ33_BNM_5K/outs/filtered_feature_bc_matrix/")


   BNL_both.data <- Read10X(data.dir = "../AAC656/SD-52-JZ33_BNL_5K/outs/filtered_feature_bc_matrix/")
    BNM_both.data <- Read10X(data.dir = "../AAC656/SD-52-JZ33_BNM_5K/outs/filtered_feature_bc_matrix/")

setwd(wd)

# create the Seurat objects
SD_52_JZ33 <- CreateSeuratObject(counts = SD_52_JZ33.data, min.cells = 0, project = "human")
BNL_human_only <- CreateSeuratObject(counts = BNL_human_only.data, min.cells = 0, project = "human_alignment_ptx")
BNM_human_only <- CreateSeuratObject(counts = BNM_human_only.data, min.cells = 0, project = "human_alignment_ptx")
BNM_both_ptx <- CreateSeuratObject(counts = BNM_both.data, min.cells = 0, project = "human_mouse_alignment_ptx")
BNL_both_ptx <- CreateSeuratObject(counts = BNL_both.data, min.cells = 0, project = "human_mouse_alignment_ptx")
BNL_both_ptx$model = "mouse_human_alignment_ptx"
BNM_both_ptx$model = "mouse_human_alignment_ptx"
SD_52_JZ33$model = "patient_aligned_mouse_human"
BNM_human_only$model = "human_alignment_only_ptx"
BNL_human_only$model = "human_alignment_only_ptx"
BNL_both_ptx$model = "mouse_human_alignment_ptx"
BNM_both_ptx$model = "mouse_human_alignment_ptx"
BNM_mouse_only <- CreateSeuratObject(counts = BNM_mouse_only.data, min.cells = 0, project = "mouse_alignment_ptx")
BNL_mouse_only <- CreateSeuratObject(counts = BNL_mouse_only.data, min.cells = 0, project = "mouse_alignment_ptx")
BNL_mouse_only$model = "mouse_alignment_only_ptx"
BNM_mouse_only$model = "mouse_alignment_only_ptx"

# merge samples
mouse_human_patient <- merge(BNL_mouse_only, y = c(BNM_mouse_only, SD_52_JZ33), add.cell.ids = c("A", "B", "C"), project = "HTB2876")
human_ptx_human_patient <- merge(BNL_human_only, y = c(BNM_human_only, SD_52_JZ33), add.cell.ids = c("D", "E", "F"), project = "HTB2876")
ptx_human_patient.1 <- merge(BNL_both_ptx, y = c(BNM_both_ptx, SD_52_JZ33), add.cell.ids = c("G", "H", "I"), project = "HTB2876")

# mouse ptx model with patient
# mark the mito genes
mito.genes <- grep(pattern = "^MT-", x = rownames(x = mouse_human_patient), value = TRUE)
mouse_human_patient[["percent.mt"]] <- PercentageFeatureSet(mouse_human_patient, pattern = "^MT-")
mouse_human_patient[["log_nCount_RNA"]] <- log2(mouse_human_patient[["nCount_RNA"]]+1)

# remove cells with <200 RNA molecules, or >6000 molecules, or >30% mito
mouse_human_patient <- subset(mouse_human_patient, subset = nFeature_RNA > 200 & nFeature_RNA < 6000 & percent.mt < 30)
mouse_human_patient <- NormalizeData(mouse_human_patient, normalization.method = "LogNormalize", scale.factor = 10000)
mouse_human_patient <- FindVariableFeatures(mouse_human_patient, selection.method = "vst", nfeatures = 2000)
all.genes.mouse <- rownames(mouse_human_patient)
mouse_human_patient <- ScaleData(mouse_human_patient, features = VariableFeatures(object = mouse_human_patient), vars.to.regress = c("nCount_RNA"))
mouse_human_patient <- RunPCA(mouse_human_patient, features = VariableFeatures(object = mouse_human_patient))
mouse_human_patient <- FindNeighbors(mouse_human_patient, dims = 1:20)
mouse_human_patient <- FindClusters(mouse_human_patient, resolution = 0.4)
mouse_human_patient <- RunUMAP(mouse_human_patient, dims = 1:20)
DimPlot(mouse_human_patient, reduction = "umap", group.by = "model")

# human only alignment pix with patient 
# mark the mito genes
mito.genes <- grep(pattern = "^MT-", x = rownames(x = human_ptx_human_patient), value = TRUE)
human_ptx_human_patient[["percent.mt"]] <- PercentageFeatureSet(human_ptx_human_patient, pattern = "^MT-")
human_ptx_human_patient[["log_nCount_RNA"]] <- log2(human_ptx_human_patient[["nCount_RNA"]]+1)
human_ptx_human_patient <- subset(human_ptx_human_patient, subset = nFeature_RNA > 200 & nFeature_RNA < 6000 & percent.mt < 30)

# remove cells with <200 RNA molecules, or >6000 molecules, or >30% mito
human_ptx_human_patient <- NormalizeData(human_ptx_human_patient, normalization.method = "LogNormalize", scale.factor = 10000)
human_ptx_human_patient <- FindVariableFeatures(human_ptx_human_patient, selection.method = "vst", nfeatures = 2000)
all.genes.human.ptx <- rownames(human_ptx_human_patient)
human_ptx_human_patient <- ScaleData(human_ptx_human_patient, features = VariableFeatures(object = human_ptx_human_patient), vars.to.regress = c("nCount_RNA"))
human_ptx_human_patient <- RunPCA(human_ptx_human_patient, features = VariableFeatures(object = human_ptx_human_patient))
DimPlot(human_ptx_human_patient, reduction = "pca")
human_ptx_human_patient <- FindNeighbors(human_ptx_human_patient, dims = 1:20)
human_ptx_human_patient <- FindClusters(human_ptx_human_patient, resolution = 0.4)
human_ptx_human_patient <- RunUMAP(human_ptx_human_patient, dims = 1:20)
DimPlot(human_ptx_human_patient, reduction = "umap", group.by = "model")

# ptx and human
# mark the mito genes
mito.genes.ptx_patient <- grep(pattern = "^MT-", x = rownames(x = ptx_human_patient.1), value = TRUE)
ptx_human_patient.1[["percent.mt"]] <- PercentageFeatureSet(ptx_human_patient.1, pattern = "^MT-")
ptx_human_patient.1[["log_nCount_RNA"]] <- log2(ptx_human_patient.1[["nCount_RNA"]]+1)

# remove cells with <200 RNA molecules, or >6000 molecules, or >30% mito
ptx_human_patient.1 <- subset(ptx_human_patient.1, subset = nFeature_RNA > 200 & nFeature_RNA < 6000 & percent.mt < 30)
ptx_human_patient.1 <- NormalizeData(ptx_human_patient.1, normalization.method = "LogNormalize", scale.factor = 10000)
ptx_human_patient.1 <- FindVariableFeatures(ptx_human_patient.1, selection.method = "vst", nfeatures = 2000)
all.genes.both.patient.1 <- rownames(ptx_human_patient.1)
ptx_human_patient.1 <- ScaleData(ptx_human_patient.1, features = VariableFeatures(object = ptx_human_patient.1), vars.to.regress = c("nCount_RNA"))
ptx_human_patient.1 <- RunPCA(ptx_human_patient.1, features = VariableFeatures(object = ptx_human_patient.1))
ptx_human_patient.1 <- FindNeighbors(ptx_human_patient.1, dims = 1:20)
ptx_human_patient.1 <- FindClusters(ptx_human_patient.1, resolution = 0.4)
ptx_human_patient.1 <- RunUMAP(ptx_human_patient.1, dims = 1:20)

#run plots
DimPlot(ptx_human_patient.1, reduction = "umap", group.by = "model")
DimPlot(ptx_human_patient.1, reduction = "umap", label = "TRUE")
saveRDS(ptx_human_patient.1, file = "derived_data/PTX_human_2876_res04_6000g.rds")
DimPlot(ptx_human_patient.1, reduction = "umap", group.by = "model")
DimPlot(ptx_human_patient.1, reduction = "umap", label = TRUE, pt.size = 0.5) + NoLegend()
ptx_human_patient.1$clustering.idents <- Idents(object = ptx_human_patient.1)
Idents(ptx_human_patient.1) <- "clustering.idents"
new.cluster.ids <- c("Human PTX Tumor", "PTX Fibroblast", "Human Fibroblast", "PTX Macrophage", "Human PTX Tumor", "Human Mitochondrial", "Human Macrophage", "Human Endothelial", "Human T-cells", "Human PTX Tumor","Human Fibroblast", "PTX Fibroblast", "Human Tumor Ductal", "Human Acinar", "PTX Macrophage", "PTX Stellate", "PTX Endothelial")
names(new.cluster.ids) <- levels(ptx_human_patient.1)
ptx_human_patient.1 <- RenameIdents(ptx_human_patient.1, new.cluster.ids)
saveRDS(ptx_human_patient.1, file = "derived_data/PTX_human_2876_res04_6000g.rds")
p1 = DimPlot(ptx_human_patient.1, reduction = "umap", label = TRUE, pt.size = 0.5,  group.by = "clustering.idents") + NoLegend()
p2 = DimPlot(ptx_human_patient.1, reduction = "umap", label = TRUE, pt.size = 0.5) + NoLegend()
print(CombinePlots(plots = list(p1, p2)))
markers2 <- FindAllMarkers(ptx_human_patient.1, min.pct = 0.25, logfc.threshold = 0.25)
markers2 %>% group_by(cluster) %>% top_n(n = 2, wt = avg_logFC)
saveRDS(markers, file="derived_data/markers_HTB2876_PC20_res04_6000g.rds")

Part of my excel file

barcode muTaTionsTaTus
GAGATGGCACTAGGCC-1  G12V
CTGAGCGAGACTCAAA-1  G12V
TACCGGGGTCCGAAGA-1  G12V
TATACCTCATAGGCGA-1  G12V
CTGCCATGTATTGGCT-1  G12V
AGTGTTGTCAGGAGAC-1  G12V
AATGCCAAGCAGTACG-1  G12V
TTTGTTGAGGCTTCCG-1  G12V
ACAGAAATCCATTCGC-1  G12V
GTCATTTCAACTAGAA-1  G12V
GAGTGTTGTGAGATTA-1  G12V
TCTTGCGCATAACGGG-1  G12V
ACATCGATCGGCATCG-1  G12V
CATCCCACACACAGCC-1  G12V
GACAGCCGTAGCTGTT-1  G12V
CACAGATGTTAGGCCC-1  G12V
AACGGGATCACAACCA-1  G12V
$\endgroup$
2
$\begingroup$

If you want to add metadata to a Seurat object the new metadata needs to have the same length as the old metadata (== number of cells).

For an example, if you have a 8 cells c("a", "b", "c", "d", "e", "f", "g") and you want to be able to highlight the 4 cells with the "barcodes" c("a", "c", "f", "g") in a umap plot you have to add a new grouping column:

# The Seurat object is called "ExampleData" and columns can be directly adressed using "$"
# The barcodes of all cells are stored as the rownames of the meta.data slot 
ExampleData$cells_of_interest <- ifelse(rownames(ExampleData@meta.data) %in% c("a", "c", "f", "g"), "interesting", "boring")

From this you will get a new column in the meta data with the values:

interesing, boring, interesting, boring, boring, interesting, interesting

This way you can use this column in DimPlot:

DimPlot(ExampleData, reduction = "umap", group.by = "cells_of_interest")

With two colors in the plot one called "interesting" and one "boring"

$\endgroup$
12
  • $\begingroup$ so I would do ptx_human_patient.1$cells_of_interest <- ifelse(ptx_human_patient.1$barcode %in% c("", "", "", ""), "", "") I don't know what you mean by 8 cells vs. wanting to show 4 cells. I want to show all the barcodes from my excel file in the umap. I've uploaded the excel file by doing metadata = read.table("metadata.csv", sep=';', header=T, stringsAsFactors=F) $\endgroup$ – mmpp Apr 21 '20 at 15:47
  • $\begingroup$ As I understand you want to highlight a subset of your cells using the barcodes you uploaded from file. Unsing barcode names in the original metadat you can tell Seurat wich barcodes are of interest for you and then highlight them. $\endgroup$ – PPK Apr 21 '20 at 16:04
  • $\begingroup$ yes that is what I want to do, I just don't understand how to go about it. $\endgroup$ – mmpp Apr 21 '20 at 16:20
  • $\begingroup$ @mmpp I added some clarification to the answer, especially regarding where in the meta data the barcodes are stored. $\endgroup$ – PPK Apr 23 '20 at 16:36
  • $\begingroup$ I'm not sure my question is being understood correctly. I already have a file with the barcodes that I want to show on the umap. I don't want to show only four cells, I want to show all of the barcodes from my file. $\endgroup$ – mmpp Apr 23 '20 at 21:44
0
$\begingroup$

From my understanding, you are just trying to add some new information to the metadata of your Seurat object. To do so you can just add the column to meta.data slots in your Seurat object.

(https://github.com/satijalab/seurat/wiki/Seurat#slots)

Something like this:

myBarcode = rownames(seurat_obj@meta.data) #get barcode from seurat
mutation.df = mutation.df[match(myBarcode, mutation.df$barcode), ] #match the order of seurat barcode with you data
seurat_obj$mutation = mutation.df$muTaTionsTaTus #put mutation into metadata
$\endgroup$
15
  • $\begingroup$ I tried to add the barcodes but it didn't seem to work. I did it by ptx_human_patient.1$KRAS ="metadata.1" $\endgroup$ – mmpp Apr 21 '20 at 13:57
  • $\begingroup$ Can you describe your question more specifically? Is there a barcode that you want to plot for each of the cell, or just a subset of cells? Can you post several of your scRNA barcodes here? $\endgroup$ – Phoenix Mu Apr 21 '20 at 14:02
  • $\begingroup$ I copied some of the barcodes in the question. They are just a subset of cells. I have KRAS variants and would like to map on the umap. I also tried this ptx_human_patient.1 <- AddMetaData(object = ptx_human_patient.1, metadata = metadata.1, col.name = "variant") $\endgroup$ – mmpp Apr 21 '20 at 14:07
  • $\begingroup$ I think @PPK's answer is correct. Before you add the new metadata column to Seurat, you need to make sure that they have the same number of rows and the cell barcodes are in the same order. $\endgroup$ – Phoenix Mu Apr 21 '20 at 15:55
  • $\begingroup$ I'm not sure how to do that and I don't understand how it's done based on the response. $\endgroup$ – mmpp Apr 21 '20 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.