Uses Chris Rorden's dcm2nii from
http://www.mccauslandcenter.sc.edu/mricro/mricron/dcm2nii.html to convert
DICOM files to NIfTI format. Should Need run install_dcm2nii
before running.
dcm2nii(basedir = ".", files = NULL, copy_files = TRUE,
progdir = system.file(package = "dcm2niir"), verbose = TRUE,
dcm2niicmd = c("dcm2niix", "dcm2nii_2009", "dcm2nii"),
merge_files = FALSE, ignore_derived = FALSE, unzip_files = TRUE,
opts = paste0("-9 ", ifelse(ignore_derived, "-i y ", ""),
ifelse(merge_files, " -m y ", ""), paste0(" -v ", as.numeric(verbose)),
" -z y -f %p_%t_%s"), ...)
dcm2nii_par_rec(file = list.files(pattern = "[.](par|PAR)"),
copy_files = TRUE, verbose = TRUE, ...)
dcm2nii_bids_sidecar(basedir, progdir = system.file(package =
"dcm2niir"), dcm2niicmd = c("dcm2niix", "dcm2nii_2009", "dcm2nii"),
...)
fix_sidecar(file)
basedir | (character) directory to get files |
---|---|
files | (character) vector of files to segment. Will override
|
copy_files | (logical) Should files be copied to a temporary directory? |
progdir | (character) directory of bash scripts, no user input needed unless
binaries were installed elsewhere. Passed to |
verbose | (logical) print diagnostic printouts |
dcm2niicmd | (character) either "dcm2niix", "dcm2nii", or "dcm2nii_2009", which
are different versions of dcm2nii. Passed to |
merge_files | Should files be merged,
passed do |
ignore_derived | Should derived images be ignored,
passed do |
unzip_files | if |
opts | list of arguments to pass to |
... | arguments to be passed to |
file | A Par/REC file |
List of result of system
command, names of files before and after
conversion
library(utils)
install_dir = tempdir()
sysname = tolower(Sys.info()["sysname"])
cmake = Sys.which("cmake")
if (file.exists(cmake) && sysname == "windows" &&
nzchar(Sys.getenv("APPVEYOR")) && FALSE) {
source_clone_dir = fs::path(Sys.getenv("APPVEYOR_BUILD_FOLDER"),
"inst", "dcm2niix_clone")
install_dcm2nii(
progdir = install_dir,
overwrite = TRUE,
from_source = TRUE,
verbose = 2,
source_clone_dir = source_clone_dir)
} else {
install_dcm2nii(progdir = install_dir)
}
#>
|
| | 0%
|
|======================================================================| 100%
#>
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 9%
|
|======= | 10%
|
|======= | 11%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 18%
|
|============= | 19%
|
|============== | 19%
|
|============== | 20%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 22%
|
|================ | 23%
|
|================ | 24%
|
|================= | 24%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 29%
|
|===================== | 30%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 37%
|
|========================== | 38%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 39%
|
|============================ | 40%
|
|============================ | 41%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 46%
|
|================================= | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 50%
|
|=================================== | 51%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 52%
|
|===================================== | 53%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 63%
|
|============================================ | 64%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 70%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 73%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 77%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 79%
|
|======================================================== | 80%
|
|========================================================= | 81%
|
|========================================================== | 83%
|
|========================================================== | 84%
|
|=========================================================== | 84%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|============================================================== | 89%
|
|=============================================================== | 90%
|
|=============================================================== | 91%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|==================================================================== | 98%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 100%
#> [1] TRUE
## dcm_file = paste0("ftp://medical.nema.org/medical/Dicom/",
## "DataSets/WG30/MGH/MR/MouseBrainSiemens15T_20150410/",
## "Converted/DICOM/mghmousetoenhancedmr_T1w_pre.dcm")
dcm_file = paste0("http://johnmuschelli.com/dcm2niir/",
"mghmousetoenhancedmr_T1w_pre.dcm")
tdir = tempfile()
dir.create(tdir)
destfile = tempfile(fileext = ".dcm", tmpdir = tdir)
ci = Sys.getenv("CI")
method = ifelse(ci == "", "auto", "curl")
dl = download.file(url = dcm_file, method = method,
destfile = destfile, mode = "wb")
dl == 0
#> [1] TRUE
stopifnot(file.exists(destfile))
fs = file.size(destfile)
fs
#> [1] 0
if (fs <= 2e5) {
dl = download.file(url = dcm_file, destfile = destfile, mode = "wb")
}
fs = file.size(destfile)
fs
#> [1] 2002638
stopifnot(fs > 2e5)
list.files(tdir)
#> [1] "file8cd1564a9332.dcm"
dcm2niir::install_dcm2nii(progdir = install_dir)
#> [1] TRUE
res = dcm2niir::dcm2nii(basedir = tdir, verbose = 1)
#>
|
| | 0%
|
|======================================================================| 100%
#>
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 10%
|
|======= | 11%
|
|======== | 11%
|
|========= | 12%
|
|========= | 13%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============= | 18%
|
|============== | 19%
|
|============== | 20%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 23%
|
|================= | 24%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 30%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 33%
|
|======================= | 34%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 37%
|
|========================== | 38%
|
|=========================== | 38%
|
|============================ | 40%
|
|============================ | 41%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|=================================== | 49%
|
|======================================== | 57%
|
|========================================= | 58%
|
|========================================= | 59%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 69%
|
|================================================= | 70%
|
|================================================== | 71%
|
|=================================================== | 72%
|
|==================================================== | 74%
|
|===================================================== | 76%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 79%
|
|======================================================== | 80%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 82%
|
|=========================================================== | 84%
|
|============================================================ | 85%
|
|============================================================= | 86%
|
|============================================================== | 89%
|
|=============================================================== | 90%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 96%
|
|==================================================================== | 97%
|
|===================================================================== | 99%
|
|======================================================================| 100%
#> #Copying Files
#> # Converting to nii
#> '/home/runner/work/_temp/Library/dcm2niir/dcm2niix_linux' -9 -v 1 -z y -f %p_%t_%s '/tmp/RtmpCLDM83/file8cd1e8e21bc'
stopifnot(res$result == 0)
res = dcm2niir::dcm2nii(files = destfile, verbose = 1)
#> #Copying Files
#> # Converting to nii
#> '/home/runner/work/_temp/Library/dcm2niir/dcm2niix_linux' -9 -v 1 -z y -f %p_%t_%s '/tmp/RtmpCLDM83/file8cd14b192c79'
destfile = R.utils::gzip(destfile)
res = dcm2niir::dcm2nii(files = destfile, verbose = 1, unzip_files = FALSE)
#> #Copying Files
#> # Converting to nii
#> '/home/runner/work/_temp/Library/dcm2niir/dcm2niix_linux' -9 -v 1 -z y -f %p_%t_%s '/tmp/RtmpCLDM83/file8cd134df71af'
#> Warning: Result indicated an error! Please check results.
stopifnot(res$result != 0)
res = dcm2niir::dcm2nii(files = destfile, verbose = 1, unzip_files = TRUE)
#> #Copying Files
#> # Converting to nii
#> '/home/runner/work/_temp/Library/dcm2niir/dcm2niix_linux' -9 -v 1 -z y -f %p_%t_%s '/tmp/RtmpCLDM83/file8cd174390b88'
stopifnot(res$result == 0)