I want to count how many ensembl there are in each GO of GOgenes that are presents in GO_AdLib
# Filter GOgenes to only include categories in GO_AdLib
GOgenes_filtered <- GOgenes[names(GOgenes) %in% GO_AdLib]
# Count ENSEMBL IDs in each GO category
GO_counts <- sapply(GOgenes_filtered, function(x) {
length(unique(x[!is.na(x)])) # Remove NA values and count unique ENSEMBL IDs
})
# Create a data frame with the results
GO_counts_df <- data.frame(
GO_id = names(GO_counts),
ENSEMBL_count = GO_counts,
row.names = NULL
)
# Sort by count (descending)
GO_counts_df <- GO_counts_df[order(-GO_counts_df$ENSEMBL_count), ]
# Print the summary
cat("Total GO categories in GO_AdLib:", length(GO_counts), "\n")
cat("Total ENSEMBL IDs across all significant GO categories:", sum(GO_counts), "\n")
cat("\nTop 10 GO categories by ENSEMBL count:\n")
print(head(GO_counts_df, 10))
If you want to include the GO names as well:
r
# Add GO names to the data frame
GO_counts_df$GO_name <- sapply(GO_counts_df$GO_id, Get_go_name)
# Reorder columns
GO_counts_df <- GO_counts_df[, c("GO_id", "GO_name", "ENSEMBL_count")]
# Print the results
print(head(GO_counts_df, 10))
Alternative approach if you want to see the actual ENSEMBL IDs in each GO category:
r
# Get the actual ENSEMBL IDs for each significant GO category
GO_ensembl_list <- lapply(GOgenes_filtered, function(x) {
unique(x[!is.na(x)]) # Remove NA values and get unique ENSEMBL IDs
})
# Count them
GO_ensembl_counts <- sapply(GO_ensembl_list, length)
# Create a summary data frame
summary_df <- data.frame(
GO_id = names(GO_ensembl_counts),
ENSEMBL_count = GO_ensembl_counts,
GO_name = sapply(names(GO_ensembl_counts), Get_go_name),
row.names = NULL
)
summary_df <- summary_df[order(-summary_df$ENSEMBL_count), ]
This will give you:
GO_counts_df - A data frame with GO IDs, their names, and the count of ENSEMBL IDs
GO_ensembl_list - A list containing the actual ENSEMBL IDs for each GO category (if you need them for further analysis)
Note: The counts might be slightly different from what you saw in GOphase_AL because: