Remove string from Seurat object metadata
1
0
Entering edit mode
20 months ago

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.3k views
ADD COMMENT
0
Entering edit mode
20 months ago

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)))
ADD COMMENT

Login before adding your answer.

Traffic: 1938 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6