单细胞数据分析计算每个细胞MT-比例
1、awk + >>追加实现
for i in $(seq 2 $(head -n 1 a.txt | awk '{print NF}')); do cut -f 1,$i a.txt | awk 'BEGIN{sum =0 } {if(NR != 1) sum += $2} {if($1 ~ /^MT-/ && NR != 1) {sum1 += $2}} END {print 100 * sum1/sum}' >> result.txt; done
2、awk + shell数组实现
sed 1d a.txt | awk '{for(i = 2; i <= NF; i++) {array1[i] += $i; if($1 ~ /^MT-/) {array2[i] += $i}}} END {for(i = 2; i <= NF; i++) {print 100 * array2[i]/array1[i]}}' > result.txt
3、R实现
001、
dir() dat <- read.table("a.txt", header = T) dim(dat) mt <- dat[grep("^MT-", rownames(dat)),] dim(mt) result <- apply(mt, 2, sum)/apply(dat, 2, sum) * 100 head(result) tail(result)
002、
dir() dat <- read.table("a.txt", header = T) sum1 <- rep(0, ncol(dat)) sum2 <- rep(0, ncol(dat)) for (i in 1:ncol(dat)) { for (j in 1:nrow(dat)) { sum1[i] <- sum1[i] + dat[j,i] if (substring(rownames(dat)[j],1, 3) == "MT-") { sum2[i] <- sum2[i] + dat[j, i] } } } head(sum1) head(sum2) result <- sum2/sum1 head(result)
003、
dir() dat <- read.table("a.txt", header = T) sum1 <- rep(0, ncol(dat)) sum2 <- rep(0, ncol(dat)) mt <- dat[grep("^MT-", rownames(dat)),] for (i in 1:ncol(dat)) { sum1[i] <- sum(dat[,i]) sum2[i] <- sum(mt[,i]) } result <- sum2/sum1 head(result)
004、
dat <- read.table("a.txt", row.names = 1, header = T) dim(dat) result <- apply(dat[grepl("^MT-",rownames(dat)),], 2, sum)/apply(dat, 2, sum) * 100 head(result)