Hi Gaofei,
The links you provided seemed to help with the issue, I can now see 530 studies through our Foundry cbioportal API connection. The code I ended up using is pasted below. I can pull clinical data but I did come across another problem you may be able to address when trying to pull genomic data. I'm using the study ID "brca_tcga_pan_can_Atlas_2018" for my example in Foundry and when I request the available genomic data I can retrieve a dataset with the different molecular profiles available. However when I try to pull the mutation data, I am unable to view the "brca_tcga_pan_can_atlas_2018_mutations" molecularprofileID (seems to only try to return gistic and structural variants) and I get an error saying no mutation data is found. I've also pasted the code and error below.
******
#for clinical data
install.packages(c("httr", "jsonlite"), repos = "
https://cran.r-project.org")
library(httr)
library(jsonlite)
# Base URL for cBioPortal API
cbio_base <- "
https://www.cbioportal.org/api"
# Get all public studies
get_all_studies <- function() {
url <- paste0(cbio_base, "/studies?projection=SUMMARY")
res <- GET(url, add_headers(Accept = "application/json"))
stop_for_status(res)
studies <- fromJSON(content(res, as = "text", encoding = "UTF-8"))
return(studies)
}
# Fetch studies and inspect
all_studies <- get_all_studies()
head(all_studies[, c("studyId", "name", "cancerTypeName")])
# Ensure data.frame
all_studies_df <- as.data.frame(all_studies)
str(all_studies_df)
# Drop any nested/list columns (Foundry tabular datasets require flat columns)
all_studies_flat <- dplyr::select(all_studies_df, where(~ !is.list(.)))
str(all_studies_flat)
# Write full studies table to Foundry dataset (simple alias; you can move/organize it in UI)
datasets.write_table(
data = all_studies_flat,
alias = "all_studies")
***********
#where study_id is "tcga_brca_pan_can_atlas_2018"
get_mutations_by_study <- function(study_id) {
cbioportalR::set_cbioportal_db("public")
genetics_list <- cbioportalR::get_genetics_by_study(study_id = study_id)
muts <- genetics_list$mutation
if (!is.null(muts) && nrow(muts) > 0) return(muts)
# Fallback: fetch mutations via REST API (same profile IDs as in genomic_profiles_flat)
mutation_profile_id <- paste0(study_id, "_mutations")
sample_list_id <- paste0(study_id, "_all")
url <- paste0(
cbio_base, "/molecular-profiles/", mutation_profile_id,
"/mutations?sampleListId=", sample_list_id, "&projection=DETAILED"
)
res <- GET(url, add_headers(Accept = "application/json"))
if (httr::status_code(res) != 200) {
message("Mutation API returned ", httr::status_code(res), "; no mutation data.")
return(data.frame())
}
out <- fromJSON(content(res, as = "text", encoding = "UTF-8"))
as.data.frame(out) %>% dplyr::filter(.data$studyId == study_id)
}
cbioportalR::set_cbioportal_db("public")
brca_tcga_pan_can_atlas_muts <- get_mutations_by_study(study_id = study_id)
if (!is.null(brca_tcga_pan_can_atlas_muts) && nrow(brca_tcga_pan_can_atlas_muts) > 0) {
brca_tcga_mutations_flat <- brca_tcga_pan_can_atlas_muts %>%
as.data.frame() %>%
dplyr::select(where(~ !is.list(.)))
datasets.write_table(
data = brca_tcga_mutations_flat,
alias = "brca_tcga_mutations"
)
} else {
message("No mutation data returned for study ", study_id, "; skipping Foundry write.")
}
*****
#error message
ℹ Returning all data for the "brca_tcga_pan_can_atlas_2018_gistic" molecular profile in the "brca_tcga_pan_can_atlas_2018" study
ℹ Returning all data for the "brca_tcga_pan_can_atlas_2018_structural_variants" molecular profile in the "brca_tcga_pan_can_atlas_2018" study
! No "mutation" data returned. Error: In index: 1.
Error in get_mutations_by_study(study_id = study_id) :
object 'cbio_base' not found
>