# How can I obtain the percentage gene expression per identity class in Seurat as further processible numbers (e.g. matrix)?

I am analysing my single cell RNA seq data with the Seurat package. I want to know if there is a possibilty to obtain the percentage expression of a list of genes per identity class, as actual numbers (e.g. a matrix) which I can write out to say an excel file. For example I want to be able to say gene X is expressed in 30% of cell A, 10% of cell B etc.

I can generate something comparable with:

DotPlot(object = my_object, genes.plot = "my_gene")


However the results are only graphic and I wish to have further processible numbers.

Furthermore:

AverageExpression(object, genes.use = "my_gene")


Produces expression values which I cannot transform to percentages.

I will be very grateful on any hints.

• Did you read the tutorials of seurat? Why you cannot transform to percentage expression values? – llrs Feb 15 '19 at 8:49
• @IIrs I will be very grateful for any hint. I am not able to figure a way how I can convert expression values for one cell type into percentage expression for that cell type. To say X% of my cell type C expresses this gene G. – Charles Feb 15 '19 at 9:21

This can be solved like this:

library(Seurat)

my_genes <- c("gene1", "gene2", "gene3")

exp <- FetchData(object, my_genes)

matrix <- as.matrix(colMeans(exp  > 0))*100

• Glad you found your way! Perhaps explaining how did you find this solution (what did you look up and where) will help your future self or other people in the same situation. Also are you sure that FetchData retrieve the expession data? (Also which one, the raw data, the scaled data or the imputed data?) – llrs Feb 15 '19 at 11:18

You can get the table that is used to make the dot plot if you modify the DotPlot function to return it instead of the ggplot, and use the argument do.return=T.

To edit the function, the command is:

trace("DotPlot",edit=TRUE)


Then replace the last line "return(p)" by "return(data.to.plot)" and save the edit.

You can call the function and store the return in a variable.

The editing doesn't affect the source code, so the function will be untouched at your next R session.

Best,

Also potentially useful is the solution proposed here: https://github.com/satijalab/seurat/issues/1888

Basically, save DotPlot to an object, and your percentages are found within.