Using the Rxnat Package

Adi Gherman


What is XNAT?

XNAT is an open source imaging informatics platform developed by the Neuroinformatics Research Group at Washington University. XNAT was originally developed in the Buckner Lab at Washington University, now at Harvard University. It facilitates common management, productivity, and quality assurance tasks for imaging and associated data. Thanks to its extensibility, XNAT can be used to support a wide range of imaging-based projects.

Who is using XNAT?

There are several projects that currently use XNAT to manage a vast number of datasets.

For a more complete list of XNAT implementations around the world you can click here.

Installing the Rxnat package

You can install Rxnat from github with:

# install.packages("devtools")

Accessing XNAT Data

How to get a username/password for a XNAT project

XNAT projects can be public or private. In order to access a private repository a set of credentials are required. To obtain a user name and password combo you will need to visit:

Establishing a XNAT connection with the Rxnat package

The Rxnat package will accept credentials provided in the function call or read from the system environment.

Function parameters

To establish a connection using the credentials as function parameters we can call the xnat_connect function:

nitrc <- xnat_connect('', username='XXXX', password='YYYY', xnat_name=NULL)

Setting up system environment variables

To use system environment variables we need to add them to the .Renviron file located in the user’s home folder. Use Sys.getenv("HOME") to get the path (for unix/osx users the location can be easily accessed with ~, eg. vi ~/.Renviron). The Rxnat package will be able to automatically read / use a system environment variable provided the following format is used: XXXX_Rxnat_USER and XXXX_Rxnat_PASS. XXXX is provided as an argument when an XNAT connection is initiated.

As an example NITRC is used as argument and the system environment variables names should be NITRC_Rxnat_USER, and NITRC_Rxnat_PASS.

nitrc <- xnat_connect('', xnat_name='NITRC')

NITRC has some public image repositories, a username and password are not required to connect/query them. To create a connection to the public NITRC resources we call the xnat_connect function.

nitrc <- xnat_connect('')

Get list of available XNAT projects

Once a connection is established using the xnat_connect function a list of available projects can be easily retrieved by using the class internal function projects:

nitrc_projects <- nitrc$projects()
#>            ID                                                         name
#> 1       ABIDE                                                        ABIDE
#> 2    ABIDE_II                                                     ABIDE_II
#> 3         GSP                           Brain Genomics Superstruct Project
#> 4        PING Pediatric Imaging, Neurocognition, and Genetics (PING) Study
#> 5    adhd_200                                                     ADHD-200
#> 6 beijing_enh                                             Beijing Enhanced

Retrieve a list with all accessible subjects

A full list of subjects for each XNAT connection can be retrieved using the subjects function:

nitrc_subjects <- nitrc$subjects()
#>          project          ID       label gender handedness yob education
#> 1 cs_schizbull08 xnat_S02582 BPDwPsy_065      M          R  NA        NA
#> 2 cs_schizbull08 xnat_S02583 BPDwPsy_066      F          L  NA        NA
#> 3 cs_schizbull08 xnat_S02584 BPDwPsy_067      M          R  NA        NA
#> 4 cs_schizbull08 xnat_S02585 BPDwPsy_068      M          R  NA        NA
#> 5 cs_schizbull08 xnat_S02586 BPDwPsy_069      F          R  NA        NA
#> 6 cs_schizbull08 xnat_S02587 BPDwPsy_070      M          R  NA        NA
#>   ses                            group race ethnicity
#> 1  NA Bipolar Disorder: With Psychosis   NA        NA
#> 2  NA Bipolar Disorder: With Psychosis   NA        NA
#> 3  NA Bipolar Disorder: With Psychosis   NA        NA
#> 4  NA Bipolar Disorder: With Psychosis   NA        NA
#> 5  NA Bipolar Disorder: With Psychosis   NA        NA
#> 6  NA Bipolar Disorder: With Psychosis   NA        NA

Get full list of experiments

To obtain a full list of experiments the experiments function will be used:

nitrc_experiments <- nitrc$experiments()
#>          project     subject          ID               type          label
#> 1 cs_schizbull08 BPDwPsy_065 xnat_E02577 xnat:mrSessionData BPDwPsy_065_MR
#> 2 cs_schizbull08 BPDwPsy_066 xnat_E02579 xnat:mrSessionData BPDwPsy_066_MR
#> 3 cs_schizbull08 BPDwPsy_067 xnat_E02581 xnat:mrSessionData BPDwPsy_067_MR
#> 4 cs_schizbull08 BPDwPsy_068 xnat_E02583 xnat:mrSessionData BPDwPsy_068_MR
#> 5 cs_schizbull08 BPDwPsy_069 xnat_E02585 xnat:mrSessionData BPDwPsy_069_MR
#> 6 cs_schizbull08 BPDwPsy_070 xnat_E02587 xnat:mrSessionData BPDwPsy_070_MR
#>   age
#> 1   7
#> 2  11
#> 3   8
#> 4   7
#> 5  16
#> 6  13

Get the complete list of resources for a specific experiment

The scan resources for an experiment can be retrieved using the get_xnat_experiment_resources function:

xnat_E00026_resources <- nitrc$get_xnat_experiment_resources('xnat_E00026')
#>                               Name
#> 1    scan_mprage_anonymized.nii.gz
#> 2 scan_mprage_skullstripped.nii.gz
#> 3                 scan_rest.nii.gz
#> 4    scan_mprage_anonymized.nii.gz
#> 5 scan_mprage_skullstripped.nii.gz
#> 6                 scan_rest.nii.gz
#>                                                                                                                    URI
#> 1        /data/experiments/xnat_E00026/scans/anat_mprage_anonymized/resources/3967/files/scan_mprage_anonymized.nii.gz
#> 2  /data/experiments/xnat_E00026/scans/anat_mprage_skullstripped/resources/3968/files/scan_mprage_skullstripped.nii.gz
#> 3                                  /data/experiments/xnat_E00026/scans/func_rest/resources/3969/files/scan_rest.nii.gz
#> 4       /data/experiments/xnat_E00026/scans/anat_mprage_anonymized/resources/11985/files/scan_mprage_anonymized.nii.gz
#> 5 /data/experiments/xnat_E00026/scans/anat_mprage_skullstripped/resources/11986/files/scan_mprage_skullstripped.nii.gz
#> 6                                 /data/experiments/xnat_E00026/scans/func_rest/resources/11987/files/scan_rest.nii.gz

Query the XNAT projects for matching entries

If you are interested just in a subset of subjects/experiments that match a certain criteria you can use the query_scan_resources function. Accepted query parameters are:

To retrieve a list of all subject IDs and associated experiment IDs we can use the query_scan_resources function. In the example below, we are querying the HCP XNAT database for all subjects belonging the the HCP_500 project with scans taken at age 26.

nitrc_age_26 <- query_scan_resources(nitrc,age='26')
head(nitrc_age_26[c("subject_ID","experiment_ID", "Project", "Age")])
#>        subject_ID   experiment_ID Project Age
#> 1 NITRC_IR_S05244 NITRC_IR_E10507     ixi  26
#> 2 NITRC_IR_S05267 NITRC_IR_E10530     ixi  26
#> 3 NITRC_IR_S05270 NITRC_IR_E10533     ixi  26
#> 4 NITRC_IR_S05278 NITRC_IR_E10541     ixi  26
#> 5 NITRC_IR_S05394 NITRC_IR_E10657     ixi  26
#> 6 NITRC_IR_S05427 NITRC_IR_E10690     ixi  26

Getting Data: Download a single scan image/resource file

To download a single file we will use the download_file function. Using the first experiment_ID from the above example, we will get all scan resources associated with it first.

scan_resources <- get_scan_resources(nitrc,'xnat_E00026')

To download the resource file (100307_3T_BIAS_BC.nii.gz) we will do:

> download_xnat_file(nitrc,"/data/experiments/xnat_E00026/scans/anat_mprage_anonymized/resources/3967/files/scan_mprage_anonymized.nii.gz", file_dir = tempdir())
[1] "/var/folders/kr/05bm5krj0r3fpwxfdmx4xthm0000gn/T//RtmpTNSN5E/scan_mprage_anonymized.nii.gz"

Getting Data: Download a directory of data

To download all the T1 type images from experiment ConnectomeDB_E03657 we will use the download_xnat_dir function.

download_xnat_dir(nitrc, experiment_ID='NITRC_IR_E10507',scan_type='T2', file_dir = tempdir(), verbose=TRUE)
Downloading: 5.2 MB     [1] "/var/folders/kr/05bm5krj0r3fpwxfdmx4xthm0000gn/T//RtmpTNSN5E/"