{"id":23032006,"url":"https://github.com/uwrit/leaf-scripts","last_synced_at":"2025-04-02T21:41:13.287Z","repository":{"id":55654410,"uuid":"180640427","full_name":"uwrit/leaf-scripts","owner":"uwrit","description":"Collection of useful scripts for bootstrapping and managing a Leaf instance.","archived":false,"fork":false,"pushed_at":"2023-04-17T22:57:06.000Z","size":1765,"stargazers_count":8,"open_issues_count":0,"forks_count":2,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-02-08T12:13:17.860Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/uwrit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-10T18:26:29.000Z","updated_at":"2024-10-03T20:49:57.000Z","dependencies_parsed_at":"2024-12-15T15:49:33.384Z","dependency_job_id":"b8aac99a-cf71-4de9-9a27-3f0d5fdc1ca7","html_url":"https://github.com/uwrit/leaf-scripts","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uwrit%2Fleaf-scripts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uwrit%2Fleaf-scripts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uwrit%2Fleaf-scripts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uwrit%2Fleaf-scripts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uwrit","download_url":"https://codeload.github.com/uwrit/leaf-scripts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246899625,"owners_count":20851893,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-15T15:49:29.031Z","updated_at":"2025-04-02T21:41:13.265Z","avatar_url":"https://github.com/uwrit.png","language":"TSQL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Leaf Scripts\nCreating Leaf Concepts manually can be done quickly using the Leaf Admin Panel, but certain types of Concepts are best created by SQL script. Here are a few examples of how to do so.\n\nThese example assume that you have an active [UMLS license](https://uts.nlm.nih.gov/license.html) and a database named `UMLS` on the same server as your Leaf application database. Note that ICD10 diagnoses are used just as an example, and you can apply this pattern to ICD9, CPT, LOINC and other coding systems just as easily.\n\nAlternatively, for convenience you can use the scripts in the [concepts/ontologies](https://github.com/uwrit/leaf-scripts/tree/master/concepts/ontologies) folder to generate the ontologies directly and skip the process of creating temp tables.\n\n## ICD10 diagnoses using UMLS\nYou can find the full example script used here wrapped as a stored procedure at https://github.com/uwrit/leaf-scripts/blob/master/concepts/sp_InsertConceptsFromUMLS.sql.\n\n1) Use the example [sp_GetOntologyFromUMLS](https://github.com/uwrit/leaf-scripts/blob/master/concepts/sp_GetOntologyFromUMLS.sql) stored procedure to populate a temporary table (or create the table directly using the [ICD10CM.sql](https://github.com/uwrit/leaf-scripts/tree/master/concepts/ontologies/ICD10CM.sql) script):\n\n```sql\nCREATE TABLE #Output\n(\n\tAUI NVARCHAR(20) NULL,\n\tParentAUI NVARCHAR(20) NULL,\n\tMinCode NVARCHAR(20) NULL,\n\tMaxCode NVARCHAR(20) NULL,\n\tCodeCount INT NULL,\n\tOntologyType NVARCHAR(20) NULL,\n\tSqlSetWhere NVARCHAR(1000) NULL,\n\tUiDisplayName NVARCHAR(1000) NULL\n)\n\nINSERT INTO #Output\nEXEC dbo.sp_GetConceptOntologyFromUMLS 'ICD10CM'\n```\n\nOutput:\n\n| AUI       | ParentAUI | MinCode | MaxCode | CodeCount | OntologyType | SqlSetWhere                  | UiDisplayName |\n| --------- | --------- | ------- | ------- | --------- | ------------ | ---------------------------- | ------------- |\n| A20...    | NULL      | A00.0   | Z99.89  | 69823     | ICD10CM      | BETWEEN 'A00.0' AND 'Z99.89' | ICD-10-CM...  |\n| A18...    | A17...    | A02.29  | A02.29  | 1         | ICD10CM      | = 'A02.29'                   | Salmonella... |\n| A17...    | A174...   | A41.81  | A41.89  | 2         | ICD10CM      | IN ('A41.81','A41.89')       | Sepsis...     |\n\n\nEach row contains a reference to its parent row via `ParentAUI`, and a SQL expression in `SqlSetWhere` which we can plug into our datamodel by prepending our column names.\n\n2) Find the diagnosis `SQL Set` ID and insert the hierarchical UMLS rows into the `app.Concept` table:\n\n```sql\nDECLARE @SqlSetId INT = (SELECT TOP (1) S.Id FROM app.ConceptSqlSet S WHERE S.SqlSetFrom = 'dbo.diagnosis')\n\nINSERT INTO app.Concept\n(\n   [ExternalId]\n  ,[ExternalParentId]\n  ,[IsPatientCountAutoCalculated]\n  ,[IsNumeric]\n  ,[IsParent]\n  ,[IsRoot]\n  ,[IsSpecializable]\n  ,[SqlSetId]\n  ,[SqlSetWhere]\n  ,[UiDisplayName]\n  ,[UiDisplayText]\n  ,[AddDateTime]\n  ,[ContentLastUpdateDateTime]\n)\nSELECT \n    [ExternalId]\t\t   = 'UMLS_AUI:' + O.AUI\n   ,[ExternalParentId]\t\t   = 'UMLS_AUI:' + O.ParentAUI\n   ,[IsPatientCountAutoCalculated] = 1\n   ,[IsNumeric]\t\t\t   = 0\t\t\n   ,[IsParent]\t\t           = CASE WHEN EXISTS (SELECT 1 FROM #Output O2 WHERE O.AUI = O2.ParentAUI) THEN 1 ELSE 0 END\n   ,[IsRoot]\t\t\t   = CASE WHEN ParentAUI IS NULL THEN 1 ELSE 0 END\n   ,[IsSpecializable]\t\t   = 0\n   ,[SqlSetId]\t\t\t   = @SqlSetId\n   ,[SqlSetWhere]\t\t   = '@.CodingSystem = ''ICD10'' AND @.Code ' + O.SqlSetWhere\n   ,[UiDisplayName]\t           = O.uiDisplayName\n   ,[UiDisplayText]\t\t   = 'Had diagnosis of ' + O.UiDisplayName\n   ,[AddDateTime]\t\t   = GETDATE()\n   ,[ContentLastUpdateDateTime]    = GETDATE()\nFROM #Output O\nWHERE NOT EXISTS (SELECT 1\n\t\t  FROM app.Concept c\n\t\t  WHERE 'UMLS_AUI:' + o.AUI = c.ExternalID)\n```\n\nNote that above in \n\n```\n,[SqlSetWhere] = '@.CodingSystem = ''ICD10'' AND @.Code ' + O.SqlSetWhere\n```\n\nThe example assumes a hypothetical diagnosis table structure in your clinical database like:\n\n | PatientId | CodingSystem | Code  | ... |\n | --------- | ------------ | ----- | --- |\n | A         | ICD10        | E11.2 |     |\n | B         | ICD10        | T34.5 |     |\n | C         | ICD10        | S09.1 |     |\n\nYour clinical database columns will likely differ, so tailor this section appropriately for your data.\n\n3) Update the `app.Concept.ParentId` values using the `ExternalId` and `ExternalParentIds`:\n\n```sql\nUPDATE app.Concept\nSET ParentId = p.Id\nFROM app.Concept c\n\t INNER JOIN (SELECT p.Id, p.ParentId, p.ExternalId\n\t\t\t\t FROM app.Concept p) p \n\t\tON c.ExternalParentID = p.ExternalID\nWHERE EXISTS (SELECT 1 FROM #Output o WHERE 'UMLS_AUI:' + o.AUI = c.ExternalId)\n```\n\nAnd that's it! You'll likely want to have the Leaf Admin Panel open and inspect/tweak these after running the script to make sure everything looks as expected.\n\nYou can use this general pattern to insert diagnosis, procedure, LOINC, or other codes.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuwrit%2Fleaf-scripts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuwrit%2Fleaf-scripts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuwrit%2Fleaf-scripts/lists"}