Skip to content

Instantly share code, notes, and snippets.

@crazyhottommy
Last active October 29, 2022 03:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crazyhottommy/d815b34d62a28e4e2e4a51e083392275 to your computer and use it in GitHub Desktop.
Save crazyhottommy/d815b34d62a28e4e2e4a51e083392275 to your computer and use it in GitHub Desktop.

convert uuid to TCGA barcode

I downloaded the manifest file from gdc, but it only has the uuid, I will need to convert them to TCGA barcode.

read this https://support.bioconductor.org/p/89315/ and https://support.bioconductor.org/p/89021/

A function from the link above

library(httr)
library(jsonlite)
getBarcode <- function(uuid, legacy = TRUE){
    # Get manifest using the API
    uuid <- tolower(uuid)    
    baseURL <- ifelse(legacy,"https://gdc-api.nci.nih.gov/legacy/files/?","https://gdc-api.nci.nih.gov/files/?")
    options.pretty <- "pretty=true"
    options.expand <- "expand=cases.samples.portions.analytes.aliquots"
    options.field <- "fields=cases.samples.portions.analytes.aliquots.submitter_id"
    option.size <- paste0("size=",length(uuid))
    option.format <- paste0("format=JSON")
    options.filter <- paste0("filters=",
                             URLencode('{"op":"and","content":[{"op":"in","content":{"field":"files.file_id","value":['),
                             paste0('"',paste(uuid,collapse = '","')),
                             URLencode('"]}}]}'))
    
    url <- paste0(baseURL,paste(options.pretty, options.expand,option.size, 
                                options.filter, options.field,
                                option.format, sep = "&"))
    json  <- tryCatch(
        fromJSON(url, simplifyDataFrame = TRUE),
        error = function(e) {
            fromJSON(content(GET(url), as = "text", encoding = "UTF-8"), simplifyDataFrame = TRUE)
        }
    )
    df <- stack(unlist(json$data$hits))
    barcode <- df[grep("TCGA",df[,1]),1]
    df <- tibble(uuid = uuid, barcode = barcode)
    return(df)
}

getBarcode("ffa5fff7-6301-4cd8-8e63-a4d8294d1b0e", legacy = TRUE)
getBarcode("D04B63DE-03BA-4A63-92CA-D8054C3E238C", legacy = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment