0

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.

Evelyn Z
  • 3
  • 3
  • 1
    Welcome to StackOverflow! You may want to take a lookt at how to write a [great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to help people help you. – hrvg May 19 '22 at 01:15
  • 1
    In addition to providing a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), I believe you will find more help on https://bioinformatics.stackexchange.com/ with this type of domain-specific question. – jared_mamrot May 19 '22 at 01:20

1 Answers1

2

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"
hrvg
  • 476
  • 3
  • 6
  • I think this is a terrific synthetic. The OP might first be guided toward a list of lists of the actual data to then pick up from there at `unique_genes <-`, and onward. – Chris May 19 '22 at 03:16
  • Wow, thank you so much for this clear explanation. It worked well for me. I do have one question - does it make sense at the first step to make the gene generating function: `gene <- function(...) { paste(list1, list2, list3, list4, list5, list6, list7, list8, list9, collapse = "") }` Thank you again. – Evelyn Z May 19 '22 at 15:42
  • Happy to help! Don't forget to accept the answer if you found it useful. I made this gene generating function to generate synthetic data as there was none provided with the question. You can check [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for your next question! As Chris mentioned above, you can pick up your workflow after the synthetic data were generated and use your own data from `unique_genes <- unique(unlist(gene_lists))` on. – hrvg May 19 '22 at 16:40