This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Remove string from Seurat object metadata

enter image description here

I have the following:

I am trying to retain only the information before the second underscore. For reference, I contain 18 samples, all of which are different biological genotypes/conditions. How would I automatically remove all the information after the second underscore to have for instance, "CKO_AO2" ? Keep in mind that I have multiple samples, so while I can do this manually, I need to apply this as a loop or automatic statement.

seurat scrna

1 answer

this was my original idea using gsub, \\1 and \\2 match the first and second wildcards inside the parentheses (.*) :

seurat_object$meta <- gsub('^(.*)_(.*)_([AGCTN]+.*)$','\\1_\\2', colnames(seurat_object))

you could also use strsplit

seurat_object$group <- sapply(colnames(seurat_object),function(x) paste0(unlist(strsplit(x,'_'))[1:2],collapse="_"))

OR more dangerously

seurat_object$group <- gsub("_[AGCTN]+\\-1$","",colnames(seurat_object)))

Log in to answer this question.