# Set new Idents based on gene expression in Seurat and mix n match identities to compare using FindAllMarkers

I am relatively new to Bioinformatics and scRNA-seq data analysis. I am using Seurat V3 to analyze a scRNA-seq dataset in R. Currently, I have merged three scRNA-seq samples from the same donor into one Seurat object, All_Samples. We'll call them:

1. Uninfected
2. Virus1
3. Virus2

All_Samples <- merge(x = Uninfected, y = c(Virus1, Virus2))


Uninfected cells did not receive virus (i.e. express no viral genes), the other two samples were infected with a virus introducing new genes into the cells. I would like to accomplish two things.

First: I want to create a new Ident for Virus1 and Virus2 samples based on expression of a viral gene of interest: "GeneA". All cells from Virus1 and Virus2 samples that have "GeneA" expression > 0.5 would be labeled "Pos", those with "GeneA" < 0.5 would be labeled "Neg" in a the new Column called GeneA.

I am able to subset the objects based on GeneA expression that applies to all samples in the object. For example:

All_Samples_GeneA_Pos <- subset(All_Samples, subset = GeneA > 0.5)
All_Samples_GeneA_Neg <- subset(All_Samples, subset = GeneA < 0.5)


I assume I would have to modify All_Samples@meta.data based on this post and this post, but I admit I'm not entirely sure how to implement the suggested answers into my data.

Second: Once I have separated the data from Virus1 and Virus2 into "Pos" and "Neg" cells for "GeneA", I want to look for differentially expressed genes between all Uninfected cells and "Pos" cells from Virus1 or Virus2. I have been using something like this to compare samples:

 FindMarkers(object = All_Samples, group.by = 'virus', ident.1 = "1", ident.2 = "2")


But how would I do something equivalent to this:

 FindMarkers(object = All_Samples, ident.1 = "Uninfected", ident.2 = "Virus1_GeneA_Pos")


Or maybe

 FindMarkers(object = All_Samples, ident.1 = "Uninfected_GeneA_Neg", ident.2 = "Virus1_GeneA_Pos")


Let me know if I can clarify any points. I appreciate any help!

For the first question, you can use ifelse() to create a new column in the meta.data slot:

All_Samples@meta.data\$new_column <-
ifelse(
rownames(All_Samples@meta.data) %in% colnames(All_Samples_GeneA_Pos),
"GeneA_Pos",
"GeneB_Pos"
)


colnames(seurat_object) provides a vector of cell names in a given Seurat object. Here whatever cell that is in the All_Samples_GeneA_Pos object would be GeneA_Pos and whatever is not GeneB_Pos. To better control the behavior, you can use a "nested" ifelse(); you can have another ifelse() instead of the "GeneB_Pos" bit above.

For the second question, the rationale is the same as above, you can define an extra column with the "identities of interest", then use these identities with SetIdent() and then in turn using two identities of interest in the differential expression call.

• Thank you for your response! It was very helpful. Apr 10, 2020 at 14:46