This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Help with R code to get a unique list

Hi,

I have gene ontology data that looks like this (sample data). I manually downloaded the gene ontology data from the Uniprot website.

Data view

I want to:

  • Make a list of unique GO terms from the column 'Gene Ontology(molecular function)
  • Make the list of genes associated with each GO term
  • Also make the list of GO term descriptions in the next column
  • Make the list of number of gene associated to a GO term

The output data would look like this

Output data

Please note that GO terms do not repeat in a specific cell, however, they repeat in other rows. Kindly help me to do this in R.

I did several unsuccessful trials. Hope to get help from someone. I will highly appreciate your help. Thank you so much.

gene-ontology r

df[!duplicated(df$'Gene Ontology (molecular function)'),] will work, and it will only keep the first entry of the duplicated values.

group by GO IDs, collapse gene_id column, expand GO IDs, group by GO IDs, and count genes.

1 answer

Tidyverse answer

library("tidyverse")

df %>%
  select(gene_id, `Gene ontology (molecular function)`) %>%
  distinct %>%
  group_by(`Gene ontology (molecular function)`) %>%
  summarize(gene_id=str_c(gene_id, collapse=", "), `No. of Genes`=n(), .groups="drop") %>%
  separate_rows(`Gene ontology (molecular function)`, sep="; ") %>%
  separate(
    `Gene ontology (molecular function)`, sep="\\s(?=\\[GO)",
    into=c("Gene ontology (molecular function)", "Gene Ontology IDs"))

Thank you so much for this code. I tried it. It works wonderfully. Some of the GO terms are repeating (present in multiple rows_ in the output table (GO:0003735,GO:0019843). Also, can we add corresponding UniprotKBs in one more column? Thank you again.

Also, I tried exporting it as CSV. It gives out the same file. How do you export the updated dataset as CSV? Thank you again for your kind help.

You need to save the results to a variable, such as changing the first line to df2 <- df %>%, and then you can save df2 to a csv.

I'd strongly suggest making gene_id a list-column of vectors though. Else this ain't tidy.

Log in to answer this question.