<p>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).  </p>

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

<pre><code># 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 &lt;- ifelse(rownames(ExampleData@meta.data) %in% c("a", "c", "f", "g"), "interesting", "boring")
</code></pre>

<p>From this you will get a new column in the meta data with the values:</p>

<pre><code>interesing, boring, interesting, boring, boring, interesting, interesting
</code></pre>

<p>This way you can use this column in <code>DimPlot</code>:</p>

<pre><code>DimPlot(ExampleData, reduction = "umap", group.by = "cells_of_interest")
</code></pre>

<p>With two colors in the plot one called "interesting" and one "boring"</p>

CC BY-SA 4.0
2020-04-21T15:47:58.257Z
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)`
mmpp
CC BY-SA 4.0
2020-04-21T16:04:07.463Z
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.
PPK
CC BY-SA 4.0
2020-04-21T16:20:03.980Z
yes that is what I want to do, I just don't understand how to go about it.
mmpp
CC BY-SA 4.0
2020-04-23T16:36:29.613Z
@mmpp I added some clarification to the answer, especially regarding where in the meta data the barcodes are stored.
PPK
CC BY-SA 4.0
2020-04-23T21:44:23.013Z
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.
mmpp
CC BY-SA 4.0
2020-04-24T12:23:06.830Z
I made an example with highlighting 4 out of 8 cells to make it easier to understand. You can easily apply this approach to any number of barcodes
PPK
CC BY-SA 4.0
2020-04-24T13:23:13.563Z
i understand that but how do i connect my barcode file to the metadata? because right now, i'm not sure what it is considering boring or interesting.
mmpp
CC BY-SA 4.0
2020-04-24T13:34:25.320Z
if you replace the vector c("a", "c", "f", "g") from the example with the barcode column from your excel these barcodes will be called "interesting" and the rest "boring". Just play around with this a little and it will become clearer.
PPK
CC BY-SA 4.0
2020-04-24T14:12:07.910Z
okay... i have a lot of barcodes to map, i have to type out every single one of them out? is there not an easier to do it by reading the excel file? i appreciate all your help.
mmpp
CC BY-SA 4.0
2020-04-24T14:20:24.883Z
You dont have to type the barcodes. Just read the file `bc <- read.xlsx("Excel.xlsx")` and then use the column with the barcodes (`bc$barcode`) in the place where I used the vector. All this is very basic R so if you are having problems you should look for an introductory tutorial
PPK
CC BY-SA 4.0
2020-04-24T14:47:27.970Z
i did `ptx_human_patient.1$cells_of_interest <- ifelse(rownames(ptx_human_patient.1@meta.data) %in% c(SD_52_JZ33_KRAS$barcode), "interesting", "boring")` and the whole column just says "boring".
mmpp
CC BY-SA 4.0
2020-04-24T15:46:58.933Z
It may be that Seurat is adding a number after the barcode. In this case you need to add this number to the barcodes from the Excel file.
PPK
CC BY-SA 4.0
2020-04-21T15:21:03.667Z
2020-04-23T16:31:19.980Z
2020-04-23T16:31:19.980Z
PPK
<p>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 <code>meta.data</code> slots in your Seurat object. </p>

<p>(<a href="https://github.com/satijalab/seurat/wiki/Seurat#slots" rel="nofollow noreferrer">https://github.com/satijalab/seurat/wiki/Seurat#slots</a>)</p>

<p>Something like this:</p>

<pre><code>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
</code></pre>

CC BY-SA 4.0
2020-04-21T13:57:18.507Z
I tried to add the barcodes but it didn't seem to work. I did it by ptx_human_patient.1$KRAS ="metadata.1"
mmpp
CC BY-SA 4.0
2020-04-21T14:02:33.187Z
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?
Phoenix Mu
CC BY-SA 4.0
2020-04-21T14:07:27.210Z
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")`
mmpp
CC BY-SA 4.0
2020-04-21T15:55:33.593Z
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.
Phoenix Mu
CC BY-SA 4.0
2020-04-21T15:59:23.783Z
I'm not sure how to do that and I don't understand how it's done based on the response.
mmpp
CC BY-SA 4.0
2020-04-21T16:02:43.990Z
Basically to combine two data frames by common rows. Maybe you can try this:
`seurat_obj@meta.data <- left_join(seurat_obj@meta.data, excelDf, by = "barcode")`. Here excelDf is your dataframe from the excel. Then print the seurat_obj@meta.data to check if it is the desired results. I haven't used Seurat for a while. Let me know if you get any error message.
Phoenix Mu
CC BY-SA 4.0
2020-04-21T16:54:11.677Z
I get this error: Error: `by` can't contain join column `barcode` which is missing from LHS
mmpp
CC BY-SA 4.0
2020-04-21T16:57:11.177Z
this is how i upload the excel file : `metadata = read.table("SD-52-JZ33_KRAS.csv", sep=';', header=T, stringsAsFactors=F)`
mmpp
CC BY-SA 4.0
2020-04-21T18:17:47.047Z
sorry about this what is the column for barcode name in seurat_obj@meta.data? looks like it is not barcode. Can you print the data frame to confirm?
Phoenix Mu
CC BY-SA 4.0
2020-04-21T18:34:20.140Z
`barcode            mutation
   <chr>              <chr>   
 1 GAGATGGCACTAGGCC-1 G12V    
 2 CTGAGCGAGACTCAAA-1 G12V    
 3 TACCGGGGTCCGAAGA-1 G12V    
 4 TATACCTCATAGGCGA-1 G12V    
 5 CTGCCATGTATTGGCT-1 G12V    
 6 AGTGTTGTCAGGAGAC-1 G12V    
 7 AATGCCAAGCAGTACG-1 G12V    
 8 TTTGTTGAGGCTTCCG-1 G12V    
 9 ACAGAAATCCATTCGC-1 G12V    
10 GTCATTTCAACTAGAA-1 G12V` this is the dataframe of my excel file.
mmpp
CC BY-SA 4.0
2020-04-21T23:00:58.423Z
Can you try `seurat_obj@meta.data`?
Phoenix Mu
CC BY-SA 4.0
2020-04-22T13:44:48.477Z
`dput(head(ptx_human_patient.1@meta.data)[1:3])
structure(list(orig.ident = c("human_mouse_alignment_ptx", "human_mouse_alignment_ptx", 
"human_mouse_alignment_ptx", "human_mouse_alignment_ptx", "human_mouse_alignment_ptx", 
"human_mouse_alignment_ptx"), nCount_RNA = c(15412, 21185, 5867, 
16330, 14714, 12944), nFeature_RNA = c(3546L, 4221L, 1815L, 4807L, 
4341L, 3055L)), row.names = c("G_AAACCCACAGGTCAAG", "G_AAACCCATCATCACCC", 
"G_AAACGAAAGAGCAGCT", "G_AAACGAAGTCGTGGTC", "G_AAACGAATCCCGAGGT", 
"G_AAACGCTAGGGAACAA"), class = "data.frame")`
mmpp
CC BY-SA 4.0
2020-04-22T13:45:27.433Z
I did the output of `dput(head(ptx_human_patient.1@meta.data)[1:3])`
mmpp
CC BY-SA 4.0
2020-04-22T16:06:21.130Z
sorry about my previous line of code. From your output the cell barcodes are actually in the rownames of `meta.data`. But the barcodes look like "G_AAACGCTAGGGAACAA", which is different from your mutation data frame (like GTCATTTCAACTAGAA-1). So I think you first need to know what causes the difference and try to make them the same.
Phoenix Mu
CC BY-SA 4.0
2020-04-22T17:51:23.730Z
I got the mutation barcodesAs  from cellranger not seurat, that is the way they are given. As for the barcodes I have to add a letter in front of each barcode before I can merge all counts.
mmpp
CC BY-SA 4.0
2020-04-21T00:50:18.410Z
2020-04-22T16:09:02.853Z
2020-04-22T16:09:02.853Z
Phoenix Mu
<p>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?</p>

<p>Below is my following code.</p>

<pre><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 &lt;- Read10X(data.dir = "../PTX_AAC656/metadata/patient_sample/SD-52-JZ33/outs/filtered_feature_bc_matrix/")
BNM_human_only.data &lt;- Read10X(data.dir = "../PTX_AAC656/metadata/human_only_alignment/SD-52-JZ33_BNM_5K/outs/filtered_feature_bc_matrix/")
BNL_human_only.data &lt;- Read10X(data.dir = "../PTX_AAC656/metadata/human_only_alignment/SD-52-JZ33_BNL_5K/outs/filtered_feature_bc_matrix/")
BNL_mouse_only.data &lt;- Read10X(data.dir = "../PTX_AAC656/metadata/mouse_only_alignment/SD-52-JZ33_BNL_5K/outs/filtered_feature_bc_matrix/")
BNM_mouse_only.data &lt;- Read10X(data.dir = "../PTX_AAC656/metadata/mouse_only_alignment/SD-52-JZ33_BNM_5K/outs/filtered_feature_bc_matrix/")


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

setwd(wd)

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

# merge samples
mouse_human_patient &lt;- 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 &lt;- 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 &lt;- 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 &lt;- grep(pattern = "^MT-", x = rownames(x = mouse_human_patient), value = TRUE)
mouse_human_patient[["percent.mt"]] &lt;- PercentageFeatureSet(mouse_human_patient, pattern = "^MT-")
mouse_human_patient[["log_nCount_RNA"]] &lt;- log2(mouse_human_patient[["nCount_RNA"]]+1)

# remove cells with &lt;200 RNA molecules, or &gt;6000 molecules, or &gt;30% mito
mouse_human_patient &lt;- subset(mouse_human_patient, subset = nFeature_RNA &gt; 200 &amp; nFeature_RNA &lt; 6000 &amp; percent.mt &lt; 30)
mouse_human_patient &lt;- NormalizeData(mouse_human_patient, normalization.method = "LogNormalize", scale.factor = 10000)
mouse_human_patient &lt;- FindVariableFeatures(mouse_human_patient, selection.method = "vst", nfeatures = 2000)
all.genes.mouse &lt;- rownames(mouse_human_patient)
mouse_human_patient &lt;- ScaleData(mouse_human_patient, features = VariableFeatures(object = mouse_human_patient), vars.to.regress = c("nCount_RNA"))
mouse_human_patient &lt;- RunPCA(mouse_human_patient, features = VariableFeatures(object = mouse_human_patient))
mouse_human_patient &lt;- FindNeighbors(mouse_human_patient, dims = 1:20)
mouse_human_patient &lt;- FindClusters(mouse_human_patient, resolution = 0.4)
mouse_human_patient &lt;- 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 &lt;- grep(pattern = "^MT-", x = rownames(x = human_ptx_human_patient), value = TRUE)
human_ptx_human_patient[["percent.mt"]] &lt;- PercentageFeatureSet(human_ptx_human_patient, pattern = "^MT-")
human_ptx_human_patient[["log_nCount_RNA"]] &lt;- log2(human_ptx_human_patient[["nCount_RNA"]]+1)
human_ptx_human_patient &lt;- subset(human_ptx_human_patient, subset = nFeature_RNA &gt; 200 &amp; nFeature_RNA &lt; 6000 &amp; percent.mt &lt; 30)

# remove cells with &lt;200 RNA molecules, or &gt;6000 molecules, or &gt;30% mito
human_ptx_human_patient &lt;- NormalizeData(human_ptx_human_patient, normalization.method = "LogNormalize", scale.factor = 10000)
human_ptx_human_patient &lt;- FindVariableFeatures(human_ptx_human_patient, selection.method = "vst", nfeatures = 2000)
all.genes.human.ptx &lt;- rownames(human_ptx_human_patient)
human_ptx_human_patient &lt;- ScaleData(human_ptx_human_patient, features = VariableFeatures(object = human_ptx_human_patient), vars.to.regress = c("nCount_RNA"))
human_ptx_human_patient &lt;- RunPCA(human_ptx_human_patient, features = VariableFeatures(object = human_ptx_human_patient))
DimPlot(human_ptx_human_patient, reduction = "pca")
human_ptx_human_patient &lt;- FindNeighbors(human_ptx_human_patient, dims = 1:20)
human_ptx_human_patient &lt;- FindClusters(human_ptx_human_patient, resolution = 0.4)
human_ptx_human_patient &lt;- 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 &lt;- grep(pattern = "^MT-", x = rownames(x = ptx_human_patient.1), value = TRUE)
ptx_human_patient.1[["percent.mt"]] &lt;- PercentageFeatureSet(ptx_human_patient.1, pattern = "^MT-")
ptx_human_patient.1[["log_nCount_RNA"]] &lt;- log2(ptx_human_patient.1[["nCount_RNA"]]+1)

# remove cells with &lt;200 RNA molecules, or &gt;6000 molecules, or &gt;30% mito
ptx_human_patient.1 &lt;- subset(ptx_human_patient.1, subset = nFeature_RNA &gt; 200 &amp; nFeature_RNA &lt; 6000 &amp; percent.mt &lt; 30)
ptx_human_patient.1 &lt;- NormalizeData(ptx_human_patient.1, normalization.method = "LogNormalize", scale.factor = 10000)
ptx_human_patient.1 &lt;- FindVariableFeatures(ptx_human_patient.1, selection.method = "vst", nfeatures = 2000)
all.genes.both.patient.1 &lt;- rownames(ptx_human_patient.1)
ptx_human_patient.1 &lt;- ScaleData(ptx_human_patient.1, features = VariableFeatures(object = ptx_human_patient.1), vars.to.regress = c("nCount_RNA"))
ptx_human_patient.1 &lt;- RunPCA(ptx_human_patient.1, features = VariableFeatures(object = ptx_human_patient.1))
ptx_human_patient.1 &lt;- FindNeighbors(ptx_human_patient.1, dims = 1:20)
ptx_human_patient.1 &lt;- FindClusters(ptx_human_patient.1, resolution = 0.4)
ptx_human_patient.1 &lt;- 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 &lt;- Idents(object = ptx_human_patient.1)
Idents(ptx_human_patient.1) &lt;- "clustering.idents"
new.cluster.ids &lt;- 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) &lt;- levels(ptx_human_patient.1)
ptx_human_patient.1 &lt;- 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 &lt;- FindAllMarkers(ptx_human_patient.1, min.pct = 0.25, logfc.threshold = 0.25)
markers2 %&gt;% group_by(cluster) %&gt;% top_n(n = 2, wt = avg_logFC)
saveRDS(markers, file="derived_data/markers_HTB2876_PC20_res04_6000g.rds")
</code></pre>

<p>Part of my excel file </p>

<pre><code>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
</code></pre>

<seurat><single-cell><umap>
Adding certain barcodes to metadata in Seurat
# Adding certain barcodes to metadata in Seurat

(Asked by: username_0 on 2020-04-20)

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

**Tags:** seurat, single-cell, umap

---

## 2 answers

### Answer by username_1 on 2020-04-21. Score: 2

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"

> **Comments:**
> **username_0**: 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)`  
> **username_1**: 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.  
> **username_0**: yes that is what I want to do, I just don't understand how to go about it.  
> **username_1**: @mmpp I added some clarification to the answer, especially regarding where in the meta data the barcodes are stored.  
> **username_0**: 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.  
> **username_1**: I made an example with highlighting 4 out of 8 cells to make it easier to understand. You can easily apply this approach to any number of barcodes  
> **username_0**: i understand that but how do i connect my barcode file to the metadata? because right now, i'm not sure what it is considering boring or interesting.  
> **username_1**: if you replace the vector c("a", "c", "f", "g") from the example with the barcode column from your excel these barcodes will be called "interesting" and the rest "boring". Just play around with this a little and it will become clearer.  
> **username_0**: okay... i have a lot of barcodes to map, i have to type out every single one of them out? is there not an easier to do it by reading the excel file? i appreciate all your help.  
> **username_1**: You dont have to type the barcodes. Just read the file `bc <- read.xlsx("Excel.xlsx")` and then use the column with the barcodes (`bc$barcode`) in the place where I used the vector. All this is very basic R so if you are having problems you should look for an introductory tutorial  
> **username_0**: i did `ptx_human_patient.1$cells_of_interest <- ifelse(rownames(ptx_human_patient.1@meta.data) %in% c(SD_52_JZ33_KRAS$barcode), "interesting", "boring")` and the whole column just says "boring".  
> **username_1**: It may be that Seurat is adding a number after the barcode. In this case you need to add this number to the barcodes from the Excel file.  

---

### Answer by username_2 on 2020-04-21. Score: 0

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

> **Comments:**
> **username_0**: I tried to add the barcodes but it didn't seem to work. I did it by ptx_human_patient.1$KRAS ="metadata.1"  
> **username_2**: 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?  
> **username_0**: 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")`  
> **username_2**: 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.  
> **username_0**: I'm not sure how to do that and I don't understand how it's done based on the response.  
> **username_2**: Basically to combine two data frames by common rows. Maybe you can try this: `seurat_obj@meta.data <- left_join(seurat_obj@meta.data, excelDf, by = "barcode")`. Here excelDf is your dataframe from the excel. Then print the seurat_obj@meta.data to check if it is the desired results. I haven't used Seurat for a while. Let me know if you get any error message.  
> **username_0**: I get this error: Error: `by` can't contain join column `barcode` which is missing from LHS  
> **username_0**: this is how i upload the excel file : `metadata = read.table("SD-52-JZ33_KRAS.csv", sep=';', header=T, stringsAsFactors=F)`  
> **username_2**: sorry about this what is the column for barcode name in seurat_obj@meta.data? looks like it is not barcode. Can you print the data frame to confirm?  
> **username_0**: `barcode            mutation    <chr>              <chr>     1 GAGATGGCACTAGGCC-1 G12V      2 CTGAGCGAGACTCAAA-1 G12V      3 TACCGGGGTCCGAAGA-1 G12V      4 TATACCTCATAGGCGA-1 G12V      5 CTGCCATGTATTGGCT-1 G12V      6 AGTGTTGTCAGGAGAC-1 G12V      7 AATGCCAAGCAGTACG-1 G12V      8 TTTGTTGAGGCTTCCG-1 G12V      9 ACAGAAATCCATTCGC-1 G12V     10 GTCATTTCAACTAGAA-1 G12V` this is the dataframe of my excel file.  
> **username_2**: Can you try `seurat_obj@meta.data`?  
> **username_0**: `dput(head(ptx_human_patient.1@meta.data)[1:3]) structure(list(orig.ident = c("human_mouse_alignment_ptx", "human_mouse_alignment_ptx",  "human_mouse_alignment_ptx", "human_mouse_alignment_ptx", "human_mouse_alignment_ptx",  "human_mouse_alignment_ptx"), nCount_RNA = c(15412, 21185, 5867,  16330, 14714, 12944), nFeature_RNA = c(3546L, 4221L, 1815L, 4807L,  4341L, 3055L)), row.names = c("G_AAACCCACAGGTCAAG", "G_AAACCCATCATCACCC",  "G_AAACGAAAGAGCAGCT", "G_AAACGAAGTCGTGGTC", "G_AAACGAATCCCGAGGT",  "G_AAACGCTAGGGAACAA"), class = "data.frame")`  
> **username_0**: I did the output of `dput(head(ptx_human_patient.1@meta.data)[1:3])`  
> **username_2**: sorry about my previous line of code. From your output the cell barcodes are actually in the rownames of `meta.data`. But the barcodes look like "G_AAACGCTAGGGAACAA", which is different from your mutation data frame (like GTCATTTCAACTAGAA-1). So I think you first need to know what causes the difference and try to make them the same.  
> **username_0**: I got the mutation barcodesAs  from cellranger not seurat, that is the way they are given. As for the barcodes I have to add a letter in front of each barcode before I can merge all counts.  

---

