API

简要介绍一下,API是一种能够通过不同软件系统相互通信、完成数据传输的机制。比如我们可以通过和缺德地图开放平台的API通信,通过代码完成搜索地点、路径等工作,和在手机上的工作内容相似;再或者可以在DeepSeek等开放平台通过API用编程完成和AI的对话。这些数据只要有供应商提供服务,都可以通过API调用实现。API的出现,能够让之前手动完成的工作以编程自动化、批量化的方式实现,能够显著提高工作效率。

CDISC Library API

CDISC Library API是由CDISC开发和部署的一个API数据库,用户可以通过官方提供的API接口检索CDISC标准相关的内容,同时也能够用数据库查询特定信息,这个API标准库属于是基础建设内容,以次为基础可以方便各家公司开发自己的适配工具。

意义

  • 消除手动操作与版本混乱,不需要手动复制粘贴或使用Excel等文件版本,能够提高效率;

  • 实现流程自动化,比如自动化检索CDISC的标准内容并立即下载;

  • 作为CDISC标准的唯一信源,方便依据官方给出的API开发验证工具、映射工具等系统,保证项目数据一致性;

  • 支持链接数据,后续解析的JSON里面'_links'其实可以链接到相关资源。

快速开始

和绝大多数API平台申请流程类似,我们需要先有一个CDISC账户,然后去网站(https://api.developer.library.cdisc.org/profile)申请个人的API key。

之后就可以在官方给出的API文档(https://api.developer.library.cdisc.org/api-details)里测试API以及获取数据。

示例:调用API获取SDTMIG.AE数据集信息

在官方API文档里,可以测试获取SDTM、ADaM、CT、甚至版本间差异Diff等很多内容,本次先测试获取SDTMIG里AE Domain的信息。官方文档里给出了HTTP,C#,Java,JS,Python,PHP等调用案例代码。

Python示例

根据这个request,填写版本3-4和 AE进去:

https://api.library.cdisc.org/api/mdr/sdtmig/{version}/datasets/{dataset}

根据官方示例:

########### Python 3.2 #############
import urllib.request, json
​
try:
    url = "https://api.library.cdisc.org/api/mdr/sdtmig/3-4/datasets/AE"
​
    hdr ={
    # Request headers
    'Cache-Control': 'no-cache',
    'api-key': '填写你的API KEY',
    'Accept': 'application/json',
    }
​
    req = urllib.request.Request(url, headers=hdr)
​
    req.get_method = lambda: 'GET'
    response = urllib.request.urlopen(req)  
    data = response.read().decode('utf-8')
    print(data)
​
    with open('ae.json', 'w') as f:
        f.write(data)
except Exception as e:
    print(e)

随后就能得到获得的JSON数据,解析第二层的['datasetVariables']过后,可以得到:

Ordinal

Name

Label

Type

Role

Description

Core

1

STUDYID

Study Identifier

Char

Identifier

Unique identifier for a study.

Req

2

DOMAIN

Domain Abbreviation

Char

Identifier

Two-character abbreviation for the domain.

Req

3

USUBJID

Unique Subject Identifier

Char

Identifier

Identifier used to uniquely identify a subject across all studies for all applications or submissions involving the product.

Req

4

SPDEVID

Sponsor Device Identifier

Char

Identifier

A sequence of characters used by the sponsor to uniquely identify a specific device. Used to represent a device associated in some way with the adverse event. SPDEVID values are defined in the Device Identifiers (DI) domain.

Perm

5

AESEQ

Sequence Number

Num

Identifier

Sequence number given to ensure uniqueness of subject records within a domain. May be any valid number.

Req

6

AEGRPID

Group ID

Char

Identifier

Used to tie together a block of related records in a single domain for a subject.

Perm

省略

...

...

...

...

...

...

这个数据集,其实就是SDTMIG里AE部分变量的数据集,包含对变量的说明;此外,我们还在JSON的第一层直接解析到AE数据集的metadata:

{
    "ordinal": "1",
    "name": "AE",
    "label": "Adverse Events",
    "description": "An events domain that contains data describing untoward medical occurrences in a patient or subjects that are administered a pharmaceutical product and which may not necessarily have a causal relationship with the treatment.",
    "datasetStructure": "One record per adverse event per subject"
}

R语言示例

首先,调API获得数据:

library(httr)
library(jsonlite)
​
url <- "https://api.library.cdisc.org/api/mdr/sdtmig/3-4/datasets/AE"
​
# 设置 API Key 
api_key <- "设置 API Key" 
​
# 发送 GET 请求
response <- GET(
    url, 
    add_headers(
        "api-key" = api_key,
        "Cache-Control" = "no-cache",
        "Accept" = "application/json"
    )
)
​
# 状态码200表示成功
print(status_code(response))
​
if (status_code(response) == 200) {
    content_text <- content(response, "text", encoding = "UTF-8")
    json_data <- fromJSON(content_text)
    print(json_data)
} else {
    print(paste("Error:", status_code(response)))
}

得到JSON数据之后,用$不断向下解析即可得到变量信息和meta data。

SAS示例

filename resp temp;
​
/* 设置 API Key */
%let api_key = 12345678910;
​
/* 调用 PROC HTTP */
proc http
   url="https://api.library.cdisc.org/api/mdr/sdtmig/3-4/datasets/AE"
   method="GET"
   out=resp;
   
   /* 设置请求头 */
   headers 
      "api-key"="&api_key"
      "Cache-Control"="no-cache"
      "Accept"="application/json";
run;
​
%put HTTP Status Code: &SYS_PROCHTTP_STATUS_CODE;
%put HTTP Status Phrase: &SYS_PROCHTTP_STATUS_PHRASE;
​
data _null_;
   infile resp;
   input;
   put _infile_;
run;
​
/* 用JSON引擎解析 */
libname jsonlib JSON fileref=resp;
​
proc copy in=jsonlib out=work;
run;

SAS的libname JSON解析文件后,会出现多个小数据集,认准datasetvariables和root,这两者一个是变量信息,一个是AE的meta data即可。