How to use gsub inside mapply to assign names to Seurat objects
1
0
Entering edit mode
10 months ago
kousi31 ▴ 100

I wanted to assign the name of the matrix file after substituting the suffix to the corresponding Seurat object that is created.

I wrote the function for multiple files as below:

read_atac <- function(sc_matrix, sc_frag, sc_meta){

  name <-gsub('filtered_peak_bc_matrix.h5','',sc_matrix)

  counts <- Read10X_h5(filename = sc_matrix)

  chrom_assay <- CreateChromatinAssay(
    counts = counts,
    sep = c(":", "-"),
    genome = 'hg38',
    fragments = sc_frag,
    min.cells = 10,
    min.features = 200
  )

  meta <- read.csv(
    file = sc_meta, 
    header = TRUE, 
    row.names = 1)

 assign(name, CreateSeuratObject(counts = chrom_assay, assay = "peaks", meta.data = meta)) 

}

mapply(read_atac, sc_matrix, sc_frag, sc_meta).

But, Seurat objects are created with the full name of the matrix file. gsub is not working.

$`GSM8002547_Chr-Veh_R1_filtered_peak_bc_matrix.h5`
An object of class Seurat 
268284 features across 4316 samples within 1 assay 
Active assay: peaks (268284 features, 0 variable features)
 2 layers present: counts, data

I want object names as only 'GSM8002547_Chr-Veh_R1', 'GSM8002548_Chr-Veh_R2' and so on

sc_matrix has a list of matrix files

sc_matrix
[1] "GSM8002547_Chr-Veh_R1_filtered_peak_bc_matrix.h5"
[2] "GSM8002548_Chr-Veh_R2_filtered_peak_bc_matrix.h5"
[3] "GSM8002549_Veh-Veh_R1_filtered_peak_bc_matrix.h5"
[4] "GSM8002550_Veh-Veh_R2_filtered_peak_bc_matrix.h5"

I tried printing the name inside the function, it prints the name correctly but doesn't assign the name to seurat object

scATAC-seq seurat • 452 views
ADD COMMENT
1
Entering edit mode
10 months ago
kousi31 ▴ 100

I found the answer

I stored the output of mapply to a variable. Then I used for loop to assign() names to individual objects

# assign names to seurat object list obtained from previous step
for (i in 1:length(sc_matrix)){
  name <-gsub('_filtered_peak_bc_matrix.h5','_seurat',sc_matrix[i])
  assign(name, seurat_list[i])
}
ADD COMMENT
0
Entering edit mode

You can accept this answer (green check mark) to provide closure to this thread.

ADD REPLY

Login before adding your answer.

Traffic: 1684 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