OHDSI Home | Forums | Wiki | Github

Can we update the existing conceptsets and cohort definition using ROhdsiWebApi?

Updating cohorts and conceptsets in atlas is pretty straightforward. Is it possible to update these from the R environment using ROhdsiWebApi?
ROhdsiWebApi does have updateDefinition function that allows users to update the conceptset or cohort name. But can update other settings such as concept ids and entry cohort events and such?

Basic workflow would be pulling the conceptset or cohort using ROhdsiWebApi::getCohortDefinition() function, then making update to this object and posting the update back.

Yes. You can update a cohort definition in place using updateCohortDefinition(cohortDefinition, baseUrl)

I have already tried the updateCohortDefinition function. This function is not able to complete. We keep getting this error. I also looked at the source of the function and it looks like it only updates the cohort name. I have submitted the issue for this.

cd ← getCohortDefinition(1965, baseUrl)
updateCohortDefinition(cd, baseUrl)
Error in value[3L] :
Error with cohort definition: Error in .request():
! http error 403: Forbidden request.
You do not have permission to perform this action.

However, I was able to run updateConceptsetDefinition function but that only updates the conceptset name. I tried updating the other settings such as changing include descendants to not include descendants but those changes did not register.

As of now, we are making copies of the existing cohort and making updates to those and when we post the updated cohort we delete the old ones. The issue is the updated cohort will have a new cohort id. We would want to be able to update the existing cohorts and still use the same cohort id.

updateCohortDefinition works. i know it is not well documented.

based only on what you have shared above

  1. can you check if you have permission to update. you show a permission error. can you update using Atlas?
  2. is the expression you are updating different? note: it is expecting a list object that has more than just cohortExpression. it should have the name, description etc.

hmm… not sure why updateCohortDefinition is not working for me.

  1. can you check if you have permission to update. you show a permission error. can you update using Atlas? Yes, I have permissions to update. I am able to update in atlas without any issue.

  2. is the expression you are updating different? note: it is expecting a list object that has more than just cohortExpression. it should have the name, description etc.
    Yes, the list has all the elements. I used the getCohrotDefinition funciton to pull the cohort object and just tried to update that

updateCohortDefinition(cd, baseUrl)
Error in value[3L] :
Error with cohort definition: Error in .request():
! http error 403: Forbidden request.
You do not have permission to perform this action.
str(cd)
List of 8
$ id : int 2020
$ name : chr “cholera test cs 1”
$ description : NULL
$ createdBy : chr “atlas_rwed”
$ createdDate : POSIXct[1:1], format: “2023-06-26 15:24:00”
$ modifiedBy : chr “”
$ expression :List of 10
…$ cdmVersionRange : chr “>=5.0.0”
…$ PrimaryCriteria :List of 3

baseUrl <- "https://api.ohdsi.org/WebAPI"
textToTest <- paste0("Testing update on ", as.character(Sys.time()))

originalDefinition <- ROhdsiWebApi::getCohortDefinition(cohortId = 1783218, baseUrl = baseUrl)
modifiedCohortDefinition <- originalDefinition
modifiedCohortDefinition$description <- textToTest

ROhdsiWebApi::updateCohortDefinition(cohortDefinition = modifiedCohortDefinition, baseUrl = baseUrl)
retrievedDefinition <- ROhdsiWebApi::getCohortDefinition(cohortId = 1783218, baseUrl = baseUrl)

if (originalDefinition$description != retrievedDefinition$description) {
  print("The description of the original definition differs from the retrieved version.")
  print(retrievedDefinition$description)
}

This code retrieves, modifies, and updates a cohort definition, then checks if the update was successful by comparing the descriptions of the original and retrieved definitions. Please check if it works for you. If you’re still having issues, consider providing more details about your error messages or R environment.

I tried your code using the baseUrl that we use.

baseUrl <- `"https://atlas.astellasrwi.us/WebAPI"`
textToTest <- paste0("Testing update on ", as.character(Sys.time()))

originalDefinition <- ROhdsiWebApi::getCohortDefinition(cohortId = 2020, baseUrl = baseUrl)
modifiedCohortDefinition <- originalDefinition
modifiedCohortDefinition$description <- textToTest

ROhdsiWebApi::updateCohortDefinition(cohortDefinition = modifiedCohortDefinition, baseUrl = baseUrl)

ROhdsiWebApi::updateCohortDefinition(cohortDefinition = modifiedCohortDefinition, baseUrl = baseUrl)
Error in value[3L] :
Error with cohort definition: Error in .request():
! http error 403: Forbidden request.
You do not have permission to perform this action.

traceback()
7: stop(paste(“Error with”, category, “definition:”, error))
6: value[3L]
5: tryCatchOne(expr, names, parentenv, handlers[[1L]])
4: tryCatchList(expr, classes, parentenv, handlers)
3: tryCatch({
response ← .postJson(checkUrl, jsonExpression)
content ← httr::content(response)
}, error = function(error) {
stop(paste(“Error with”, category, “definition:”, error))
})
2: updateDefinition(baseUrl = baseUrl, definition = cohortDefinition,
category = “cohort”)
1: ROhdsiWebApi::updateCohortDefinition(cohortDefinition = modifiedCohortDefinition,
baseUrl = baseUrl)

I looked at the sourcecode for updateDefinition to follow where the error is occuring:

baseUrl <- gsub("/$", "", baseUrl)
arguments <- ROhdsiWebApi:::.getStandardCategories()
argument <- arguments %>% dplyr::filter(categoryStandard == 'cohort')
jsonExpression <- ROhdsiWebApi:::.toJSON(modifiedCohortDefinition$expression)
checkUrl <- paste(baseUrl, argument$categoryUrl, "check", 
                  sep = "/")

Wondering if this checkUrl is correct ?

cat(checkUrl)
https://atlas.astellasrwi.us/WebAPI/cohortdefinition/check

response ← ROhdsiWebApi:::.postJson(checkUrl, jsonExpression)
Error in .request():
! http error 403: Forbidden request.
You do not have permission to perform this action.
Run rlang::last_trace() to see where the error occurred.

I am using latest version of ROhdsiWebApi 1.3.3 on R version 4.2.2.

I am unable to open the url link in the browser:

BaseUrl link does open in browser: https://atlas.astellasrwi.us/WebAPI/

Let me know if anything else is needed. I could not find the Atlas version we are using.

Could you test by running just the code i shared? That way atleast we will know if it works

I am not able to run your code since system won’t let the connection happen to the api.

> originalDefinition <- ROhdsiWebApi::getCohortDefinition(cohortId = 1783218, baseUrl = "https://api.ohdsi.org/WebAPI")
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Timeout was reached: [api.ohdsi.org] Connection timed out after 10000 milliseconds

Thanks. It seems like you have a local infrastructure issue

I am able use our company baseUrl (https://atlas.astellasrwi.us/WebAPI/) to pull cohorts, post new cohorts and get results. The only part that is not working is the updating existing cohorts.

Any suggestion on how we can fix this?

Is there anything that can be done to fix this on our side? We would really appreciate if we can somehow get this to work

I think you should try the code i gave as is on different computers on various networks. The code should work somewhere. Then try to isolate the problem.

It sounds like the backing table has Read permissions set for your user but not Update permissions.

I am able to post cohorts and conceptsets so I have the write/update access. I also am able to make updates in atlas tool.

1 Like

Is it security enabled ? Have you authenticated using ROhdsiWebApi

  1. I am able to use updateDefinition() to make updates to names, description of ConceptSets successfully. But looks like this does not update any expressions i.e. settings such as concept ids to include, exclude. Only names,descriptions are updatable.

  2. I copied the sourcecode for updateDefinition() and commented out following section and now I can run the function successfully for updating cohorts. However, here also we can only update the name, description but not the expressions.

       if (category == "cohort") {
         jsonExpression <- ROhdsiWebApi:::.toJSON(definition$expression)
         checkUrl <- paste(baseUrl, argument$categoryUrl, "check",
                           sep = "/")
         cat(checkUrl)
         tryCatch({
           response <- ROhdsiWebApi:::.postJson(checkUrl, jsonExpression)
           content <- httr::content(response)
         }, error = function(error) {
           stop(paste("Error with", category, "definition:",
                      error))
         })
       }
    

PostDefinition() uses ROhdsiWebApi:::.putJson() to post JSON while updateDefinition uses ROhdsiWebApi:::.PUT() to update. PUT() is not updating the expression part.

> cd <- getCohortDefinition(2041, baseUrl)
> cd$expression$PrimaryCriteria$CriteriaList[[1]]$ConditionOccurrence$Age$Value <- 20
> updatecd(cd, baseUrl, "cohort")
https://atlas.astellasrwi.us/WebAPI/cohortdefinition/2041 
Success: updated Cohort 2041 cholera test cs 2

I just retested it in the public atlas instance and it works. see this

baseUrl ← “https://api.ohdsi.org/WebAPI
textToTest ← paste0("Testing update on ", as.character(Sys.time()))

originalDefinition ←
ROhdsiWebApi::getCohortDefinition(cohortId = 1783218, baseUrl = baseUrl)

originalCohortDefinitionEraPad ←
originalDefinition$expression$CollapseSettings$EraPad
modifiedCohortDefinitionEraPad ←
sample(setdiff(0:9, originalCohortDefinitionEraPad),
1)

modifiedCohortDefinition ← originalDefinition
modifiedCohortDefinition$description ← textToTest
modifiedCohortDefinition$expression$CollapseSettings$EraPad ←
modifiedCohortDefinitionEraPad

ROhdsiWebApi::updateCohortDefinition(cohortDefinition = modifiedCohortDefinition, baseUrl = baseUrl)
retrievedDefinition ← ROhdsiWebApi::getCohortDefinition(cohortId = 1783218, baseUrl = baseUrl)

if (originalDefinition$description != retrievedDefinition$description) {
print(“The description of the original definition differs from the retrieved version.”)
print(retrievedDefinition$description)
}

if (originalCohortDefinitionEraPad != retrievedDefinition$expression$CollapseSettings$EraPad) {
print(“This is proof that the cohort definition was updated.”)
print(retrievedDefinition$expression$CollapseSettings$EraPad)
}

I am not familiar with CollapseSettings$EraPad. I ran my code and was able to update the EraPad. When I retrieved the cohort object it turns out EraPad did update but I dont know what that is in Atlas tool.

Can you try updating something else that can be checked easily in Atlas? Something like age, gender cirteria or date or anything interms of cohort events. I hope that makes sense.

@Gowtham_Rao Ignore my previous comment. I was able to successfully update the age criteria.

However I had to make update the updateDefinition() function to get it working for me. That chuck of code that says “if (category == “cohort”) {}” had to be commented out. It was causing error for me.

t