美文网首页生物信息
linux&R批量合并多个组织单细胞表达矩阵文件

linux&R批量合并多个组织单细胞表达矩阵文件

作者: 不学无数YD | 来源:发表于2021-07-22 10:14 被阅读0次

    首先,

    将需要合并的矩阵的文件名称导出到.txt.


    image.png

    git bash
    cd 到文件所在的文件夹

     ls | grep ".txt.gz" > list.txt
    
    image.png

    然后R语言读取合并矩阵:

    方法一:(别人的代码)

    library(data.table)
    library(stringr)
    library(tidyverse) 
    df_empty <- data.frame()
    allDat <- lapply(list.files('500more_dge/',pattern='*_dge.txt.gz'),function(f){
      # f="NeonatalPancreas_dge.txt.gz";
      print(f);
      # tmp=fread(file.path('500more_dge/',f))
      tmp=read.table(file.path('500more_dge/',f),sep = ",", header = T)
      df_empty=merge(df_empty,tmp, all.y=TRUE)  
      return(df_empty)
    })
    

    方法二:(自己写的)

    a=read.table('list.txt',sep = '\t',stringsAsFactors = F)
    c <- list(a$V1)
    c[[1]][1]
    class(a)
    df_empty <- read.table(c[[1]][1])
    class(df_empty)
    for(i in 2:3) {
      tmp=read.table(c[[1]][i])
      df_empty=merge(df_empty,tmp, all.y=TRUE)
      return(df_empty)
    }
    
    image.png

    这个报错令我倒下了,读不了不早说????
    但是这个对于一般的小数据是有用的。

    明天试试这个py代码

    #!/usr/bin/python
    import pandas as pd
    import os
    dict={}
    #import sys
    #args=sys.argv
    
    name_list=os.listdir('D:/cell_Complexity/111')
    print(type(name_list))
    print(name_list)#列出countdir下的文件
    data0=pd.read_table('D:/cell_Complexity/111/%s' % name_list[0], names=['gene', name_list[0]])       #pd.merge好像要指定两个参数才可以merge,所以先指定一个data0
    print(type(data0))
    for i in range(1,len(name_list)):                                                      #因为第一个count已经被读取了,所以从第二个看起建立词典,注意列表和字典的索引方式的不同
        dict[i]=pd.read_table('D:/cell_Complexity/111/%s' % name_list[i], names=['gene', name_list[i]])    #建立了所有count文件的索引,keys为数字
        data0=pd.merge(data0, dict[i], on='gene', how='outer')                               #记得建立交集,
        data0.to_csv('result.csv')
    

    10x合并多个样品

    #
    #合并多个样品
    #
    library(Seurat)
    a.data <- Read10X(data.dir = "Bladder-10X_P4_3")
    a <- CreateSeuratObject(counts = a.data, project = "a")
    a
    list <-c(list.files('droplet/',pattern='*-10x*'))
    list[28]
    for (i in 1:10) {
      b.data <- Read10X(data.dir =list[2])
      b <- CreateSeuratObject(counts = b.data, project = "b")
      a <- merge(a, y = b, add.cell.ids = c("Bladder-10X_P4_3", list[i]), project = "list[i]")
    }
    

    All FACS analysis

    ---
    title: "Tabula Muris: All FACS analysis"
    output: html_notebook
    ---
    
    # Preprocessing
    
    Load the requisite packages and some additional helper functions.
    
    ```{r}
    library(Seurat)
    library(dplyr)
    library(Matrix)
    library(stringr)
    library(readr)
    library(here)
    
    FACS_files = list.files(here("00_data_ingest","00_facs_raw_data","FACS"), full.names = TRUE)
    
    raw.data.list = list()
    for (file in FACS_files){
      raw.data <- read.csv(file, row.names = 1)
      raw.data <- Matrix(as.matrix(raw.data), sparse = TRUE)
      raw.data.list <- append(raw.data.list, raw.data)
    }
    
    raw.data <- do.call(cbind, raw.data.list)
    
    cell_order_FACS <- order(colnames(raw.data))
    raw.data = raw.data[,cell_order_FACS]
    
    meta.data <- read.csv(here("00_data_ingest","00_facs_raw_data", "metadata_FACS.csv"))
    
    plates <- str_split(colnames(raw.data),"[.]", simplify = TRUE)[,2]
    
    rownames(meta.data) <- meta.data$plate.barcode
    cell.meta.data <- meta.data[plates,]
    rownames(cell.meta.data) <- colnames(raw.data)
    
    # Find ERCC's, compute the percent ERCC, and drop them from the raw data.
    erccs <- grep(pattern = "^ERCC-", x = rownames(x = raw.data), value = TRUE)
    percent.ercc <- Matrix::colSums(raw.data[erccs, ])/Matrix::colSums(raw.data)
    ercc.index <- grep(pattern = "^ERCC-", x = rownames(x = raw.data), value = FALSE)
    raw.data <- raw.data[-ercc.index,]
    
    # Create the Seurat object with all the data
    tiss <- CreateSeuratObject(counts = raw.data)
    
    tiss <- AddMetaData(object = tiss, cell.meta.data)
    tiss <- AddMetaData(object = tiss, percent.ercc, col.name = "percent.ercc")
    # Change default name for sums of counts from nUMI to nReads
    colnames(tiss@meta.data)[colnames(tiss@meta.data) == 'nUMI'] <- 'nReads'
    
    ribo.genes <- grep(pattern = "^Rp[sl][[:digit:]]", x = rownames(x = tiss@assays$RNA@data), value = TRUE)
    percent.ribo <- Matrix::colSums(tiss@assays$RNA@counts[ribo.genes, ])/Matrix::colSums(tiss@assays$RNA@counts)
    tiss <- AddMetaData(object = tiss, metadata = percent.ribo, col.name = "percent.ribo")
    
    percent.Rn45s <- tiss@assays$RNA@counts['Rn45s', ]/Matrix::colSums(tiss@assays$RNA@counts)
    tiss <- AddMetaData(object = tiss, metadata = percent.Rn45s, col.name = "percent.Rn45s")
    

    阶段性保存数据

    save(tiss, file = "tiss_FACS.Rdata")
    rm(list = ls())
    load("tiss_FACS.Rdata")
    

    All droplet analysis

    ---
    title: "Tabula Muris: All droplet analysis"
    output: html_notebook
    ---
    
    Load the requisite packages and some additional helper functions.
    
    ```{r}
    library(Seurat)
    library(dplyr)
    library(stringr)
    library(readr)
    library(Matrix)
    library(here)
    

    Load the count data for all organ and add it to the Seurat object.

    channel_folders = list.dirs(here("00_data_ingest","01_droplet_raw_data","droplet"), recursive = FALSE)
    
    n = length(strsplit(channel_folders[1],"[/]")[[1]])
    
    raw.data.list = list()
    channel.list = list()
    for (channel_folder in channel_folders){
      raw.data <- Read10X(channel_folder)
      channel = str_split(str_split(channel_folder,"/", simplify = TRUE)[1,n], "-", simplify = TRUE)[1,2]
      colnames(raw.data) <-  lapply(colnames(raw.data), function(x) paste0(channel, '_', x))
      raw.data.list <- append(raw.data.list, raw.data)
      channel.list <- append(channel.list, rep(channel, length(colnames(raw.data))))
    }
    
    raw.data <- do.call(cbind, raw.data.list)
    cell.channels <- unlist(channel.list)
    

    Order cells lexicographically.

    ordered_cell_names = order(colnames(raw.data))
    raw.data = raw.data[,ordered_cell_names]
    
    meta.data <- read.csv(here("00_data_ingest","01_droplet_raw_data", "metadata_droplet.csv"))
    rownames(meta.data) <- meta.data$channel
    
    channel_regex = "(.*?_.*?_.*?)_"
    cell.channels <- str_match(colnames(raw.data), channel_regex)[,2]
    
    cell.meta.data <- meta.data[cell.channels,]
    rownames(cell.meta.data) <- colnames(raw.data)
    
    # Find ERCC's, compute the percent ERCC, and drop them from the raw data.
    erccs <- grep(pattern = "^ERCC-", x = rownames(x = raw.data), value = TRUE)
    percent.ercc <- Matrix::colSums(raw.data[erccs, ])/Matrix::colSums(raw.data)
    ercc.index <- grep(pattern = "^ERCC-", x = rownames(x = raw.data), value = FALSE)
    raw.data <- raw.data[-ercc.index,]
    
    # Create the Seurat object with all the data
    tiss <- CreateSeuratObject(raw.data = raw.data)
    
    tiss <- AddMetaData(object = tiss, cell.meta.data)
    tiss <- AddMetaData(object = tiss, percent.ercc, col.name = "percent.ercc")
    
    ribo.genes <- grep(pattern = "^Rp[sl][[:digit:]]", x = rownames(x = tiss@data), value = TRUE)
    percent.ribo <- Matrix::colSums(tiss@raw.data[ribo.genes, ])/Matrix::colSums(tiss@raw.data)
    tiss <- AddMetaData(object = tiss, metadata = percent.ribo, col.name = "percent.ribo")
    
    percent.Rn45s <- tiss@raw.data[c('Rn45s'), ]/Matrix::colSums(tiss@raw.data)
    tiss <- AddMetaData(object = tiss, metadata = percent.Rn45s, col.name = "percent.Rn45s")
    

    相关文章

      网友评论

        本文标题:linux&R批量合并多个组织单细胞表达矩阵文件

        本文链接:https://www.haomeiwen.com/subject/txqjmltx.html