如果大多数基因列表中存在,如何保留基因?

发布于 2025-01-30 05:11:21 字数 97 浏览 3 评论 0原文

我有9个基因列表,每个基因的长度为2000个基因。我想保留基因在6个或更多列表中。我不确定如何指定这一点,我一直在使用Intersect函数。

任何帮助都将受到赞赏。

I have 9 gene lists, each 2000 genes in length. I want to keep genes if present in 6 or more of lists. I am not sure how to specify this, I have been using the intersect function.

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画骨成沙 2025-02-06 05:11:21

首先,使用少量辅助功能重新创建数据:

# gene generating function as a trigram of lower case letters
gene <- function(...) {
  paste(sample(letters, 3), collapse = "")
}

# creating lists of genes
gene_lists <- lapply(seq(9), function(x) sapply(seq(2000), gene))

然后提取唯一元素:

# getting unique genes
unique_genes <- unique(unlist(gene_lists))
length(unique_genes)
[1] 10661

在这里,我们可以检查合成数据是否具有一定的冗余:

# checking if there are enough redundant genes
stopifnot(length(unique_genes) < length(unlist(gene_lists)))

然后在计数发生时迭代在唯一的基因和列表上:

# iterating over unique_genes
gene_occurence <- sapply(unique_genes, function(gene) {
  # iterating over lists
  # sum counts the total number of occurence
    sum(sapply(gene_lists, function(x) { gene %in% x }))
  })
length(gene_occurence)
[1] 10661
table(gene_occurence)
   1    2    3    4    5    6 
6017 3330 1050  231   31    2 

然后获取常见基因:

limit <- 6
common_genes <- unique_genes[which(gene_occurence >= limit)]
common_genes
[1] "ngu" "het"

First, recreate the data with a little helper function:

# gene generating function as a trigram of lower case letters
gene <- function(...) {
  paste(sample(letters, 3), collapse = "")
}

# creating lists of genes
gene_lists <- lapply(seq(9), function(x) sapply(seq(2000), gene))

Then extracts the unique elements:

# getting unique genes
unique_genes <- unique(unlist(gene_lists))
length(unique_genes)
[1] 10661

Here we can check that the synthetic data have some redundancy:

# checking if there are enough redundant genes
stopifnot(length(unique_genes) < length(unlist(gene_lists)))

Then iterate over unique gene and list while counting occurences:

# iterating over unique_genes
gene_occurence <- sapply(unique_genes, function(gene) {
  # iterating over lists
  # sum counts the total number of occurence
    sum(sapply(gene_lists, function(x) { gene %in% x }))
  })
length(gene_occurence)
[1] 10661
table(gene_occurence)
   1    2    3    4    5    6 
6017 3330 1050  231   31    2 

Then get the common genes:

limit <- 6
common_genes <- unique_genes[which(gene_occurence >= limit)]
common_genes
[1] "ngu" "het"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文