From d0d4765c1d96b3d6981842e58d14e6a6daa78bef Mon Sep 17 00:00:00 2001 From: Wonsang You <wsgyou@gmail.com> Date: Sat, 3 Jun 2017 16:52:56 -0400 Subject: [PATCH] Initial commit. --- .project | 18 ++++++ DESCRIPTION | 12 ++++ R/meanROIs.R | 39 ++++++++++++ R/orthoROIs.R | 130 ++++++++++++++++++++++++++++++++++++++++ R/tsROI.R | 37 ++++++++++++ man/easyFMRI-package.Rd | 35 +++++++++++ man/meanROIs.Rd | 62 +++++++++++++++++++ man/orthoROIs.Rd | 93 ++++++++++++++++++++++++++++ man/tsROI.Rd | 61 +++++++++++++++++++ 9 files changed, 487 insertions(+) create mode 100644 .project create mode 100644 DESCRIPTION create mode 100644 R/meanROIs.R create mode 100644 R/orthoROIs.R create mode 100644 R/tsROI.R create mode 100644 man/easyFMRI-package.Rd create mode 100644 man/meanROIs.Rd create mode 100644 man/orthoROIs.Rd create mode 100644 man/tsROI.Rd diff --git a/.project b/.project new file mode 100644 index 0000000..8f90f7b --- /dev/null +++ b/.project @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>easyFMRI</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>de.walware.statet.r.builders.RSupport</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>de.walware.statet.base.StatetNature</nature> + <nature>de.walware.statet.r.RNature</nature> + </natures> +</projectDescription> diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..fbaaf29 --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,12 @@ +Package: easyFMRI +Type: Package +Title: Functions of fMRI analysis +Version: 1.0 +Date: 2010-09-17 +Author: Wonsang You +Maintainer: Wonsang You <you@ifn-magdeburg.de> +Description: Functions for ROI segmentation in EPI sequence. +License: GPL (>=2) +LazyLoad: yes +Depends: oro.nifti, AnalyzeFMRI +Packaged: 2010-09-17 12:12:06 UTC; wyou diff --git a/R/meanROIs.R b/R/meanROIs.R new file mode 100644 index 0000000..b8ac2c5 --- /dev/null +++ b/R/meanROIs.R @@ -0,0 +1,39 @@ +meanROIs <- +function(EPIfname, nROIs=31, ROIdir="", prefix="roi_", outfname="meanROIs", writefile=TRUE) { + + require(oro.nifti) + require(AnalyzeFMRI) + + # Extract ROI data + if(length(ROIdir)>1) + ROIdir<-paste(c(ROIdir,"/"), collapse="") + + # Read the EPI sequence + #dimEPI<-dim(EPIdata) + #nTPs<-dimEPI[4] + EPIimg<-paste(c(EPIfname,".img"),sep="",collapse="") + EPIhdr<-paste(c(EPIfname,".hdr"),sep="",collapse="") + nTPs<-f.read.nifti.header(EPIhdr)$dim[5] + + # Compute mean time series of each ROI + mtsROIs<-mat.or.vec(nROIs, nTPs) + for(i in seq(1,nROIs)) { + ROIfname<-paste(c(ROIdir,prefix,i,".nii.gz"),sep="",collapse="") + roi<-readNIfTI(ROIfname)@.Data + roipos<-roi>0 + for(t in seq(1,nTPs)){ + #epiTP<-epi[,,,t] + epiTP<-f.read.nifti.tpt(EPIimg,t) + epiTPROI<-epiTP[roipos] + mtsROIs[i,t]<-mean(epiTPROI) + } + } + + mtsROIs + + # Write a text file + if(writefile) + write.table(mtsROIs, file = paste(c(outfname,".txt"),sep="",collapse=""), + sep = ";", col.names = NA) +} + diff --git a/R/orthoROIs.R b/R/orthoROIs.R new file mode 100644 index 0000000..1894ed4 --- /dev/null +++ b/R/orthoROIs.R @@ -0,0 +1,130 @@ +orthoROIs <- +function(nROIs=31, ThRes=5, ROIdir="", targetdir="newROIs", mode="minimal", prefix="roi_", newprefix="roi_", writefile=TRUE, gzipped=TRUE, verbose=TRUE) { + + require(oro.nifti) + + # Extract ROI data + if(length(ROIdir)>1) + ROIdir<-paste(c(ROIdir,"/"), collapse="") + + ROIdata<-c() + for(i in seq(1,nROIs)) { + roi<-readNIfTI(paste(c(ROIdir,prefix,i,".nii.gz"),sep="",collapse=""))@.Data + ROIdata<-c(ROIdata,list(roi)) + } + + # Evaluate overlaps + olMat<-mat.or.vec(nROIs, nROIs) + thr<-mat.or.vec(nROIs,1) + for(i in seq(1,nROIs-1)) { + x1<-ROIdata[[i]] + olMat[i,i]<-0 + for(j in seq(i+1,nROIs)) { + x2<-ROIdata[[j]] + olMat[i,j]<-length(x1[x1&x2]) + } + } + + # The number of overlapped ROIs + nOverlapROIpairs<-length(olMat[olMat>0]) + nTotalROIpairs<-nROIs*(nROIs-1)/2 + + # The number of overlapped Voxels + nOverlapVoxels<-sum(olMat[olMat>0]) + + if(verbose){ + msg <- paste(c("The number of overlapped ROI pairs", nOverlapROIpairs,"/",nTotalROIpairs),collapse = " ") + print(msg) + msg <- paste(c(" The number of overlapped Voxels= ", nOverlapVoxels),collapse = " ") + print(msg) + } + + # Resize ROIs to eliminate overlaps + olMat<-mat.or.vec(nROIs, nROIs) + thr<-mat.or.vec(nROIs,1) + for(i in seq(1,nROIs-1)) { + x1<-ROIdata[[i]] + olMat[i,i]<-0 + for(j in seq(i+1,nROIs)) { + x2<-ROIdata[[j]] + olMat[i,j]<-length(x1[x1&x2]) + + if(olMat[i,j]>0){ + if(mode=="minimal"){ + x1<-replace(x1,(x1>0)&(x2>0),0) + ROIdata[[j]]<-replace(x2,(x1>0)&(x2>0),0) + }else{ + nOverlapVoxelsInROI<-olMat[i,j] + + ux1<-x1 + ux2<-x2 + thr1<-thr[i] + thr2<-thr[j] + for(r in seq(1,trunc(ThRes))){ + ThrRes<-1/10^r + while(nOverlapVoxelsInROI>0){ + tmp_thr1<-thr1+ThrRes + tmp_thr2<-thr2+ThrRes + tmp_ux1<-replace(ux1,ux1<thr1,0) + tmp_ux2<-replace(ux2,ux2<thr2,0) + nOverlapVoxelsInROI<-length(tmp_ux1[tmp_ux1&tmp_ux2]) + + if(nOverlapVoxelsInROI>=0){ + thr1<-tmp_thr1 + thr2<-tmp_thr2 + ux1<-tmp_ux1 + ux2<-tmp_ux2 + } + } + } + x1<-ux1 + ROIdata[[j]]<-ux2 + thr[i]<-thr1 + thr[j]<-thr2 + } + } + } + ROIdata[[i]]<-x1 + } + + # Verify overlaps + olMat<-mat.or.vec(nROIs, nROIs) + for(i in seq(1,nROIs-1)) { + x1<-ROIdata[[i]] + olMat[i,i]<-0 + for(j in seq(i+1,nROIs)) { + x2<-ROIdata[[j]] + olMat[i,j]<-length(x1[x1&x2]) + } + } + + nNewOverlapROIpairs<-length(olMat[olMat>0]) # The number of overlapped ROIs + nNewOverlapVoxels<-sum(olMat[olMat>0]) # The number of overlapped Voxels + + if(verbose){ + msg <- paste(c("The number of new overlapped ROI pairs", nNewOverlapROIpairs,"/",nTotalROIpairs),collapse = " ") + print(msg) + msg <- paste(c(" The number of new overlapped Voxels= ", nNewOverlapVoxels),collapse = " ") + print(msg) + } + + # Write NIFTI files + if(writefile){ + dir.create(paste(c(ROIdir,targetdir),sep="",collapse="")) + for(i in seq(1,nROIs)){ + roi.fname<-paste(c(ROIdir,targetdir,"/",newprefix,i),sep="",collapse="") + roi.nifti<-nifti(ROIdata[[i]],datatype=64) # double type + writeNIfTI(roi.nifti, roi.fname, gzipped=gzipped, verbose=FALSE) + } + } + + if(mode=="minimal"){ + dat<-drop(list(ROIData=ROIdata,nOverlapROIpairs,nOverlapVoxels, + nNewOverlapROIpairs,nNewOverlapVoxels)) + }else{ + dat<-drop(list(ROIData=ROIdata,thr=thr,nOverlapROIpairs,nOverlapVoxels, + nNewOverlapROIpairs,nNewOverlapVoxels)) + } + invisible(dat) +} + diff --git a/R/tsROI.R b/R/tsROI.R new file mode 100644 index 0000000..f5eb769 --- /dev/null +++ b/R/tsROI.R @@ -0,0 +1,37 @@ +tsROI <- +function(ROIidx, EPIfname, ROIdir="", prefix="roi_", outfname="tsROI", writefile=TRUE) { + + require(oro.nifti) + require(AnalyzeFMRI) + + # Extract ROI data + if(length(ROIdir)>1) + ROIdir<-paste(c(ROIdir,"/"), collapse="") + + # Read the EPI sequence + #dimEPI<-dim(EPIdata) + #nTPs<-dimEPI[4] + EPIimg<-paste(c(EPIfname,".img"),sep="",collapse="") + EPIhdr<-paste(c(EPIfname,".hdr"),sep="",collapse="") + nTPs<-f.read.nifti.header(EPIhdr)$dim[5] + + # Compute mean time series of each ROI + ROIfname<-paste(c(ROIdir,prefix,ROIidx,".nii.gz"),sep="",collapse="") + roi<-readNIfTI(ROIfname)@.Data + roipos<-roi>0 + ts<-c() + for(t in seq(1,nTPs)){ + epiTP<-f.read.nifti.tpt(EPIimg,t) + #epiTP<-EPIdata[,,,t] + epiTPROI<-epiTP[roipos] + ts<-rbind(ts,epiTPROI) + } + + ts + + # Write a text file + if(writefile) + write.table(ts, file = paste(c(outfname,".txt"),sep="",collapse=""), + sep = ";", col.names = NA) +} + diff --git a/man/easyFMRI-package.Rd b/man/easyFMRI-package.Rd new file mode 100644 index 0000000..95d7804 --- /dev/null +++ b/man/easyFMRI-package.Rd @@ -0,0 +1,35 @@ +\name{easyFMRI-package} +\alias{easyFMRI-package} +\alias{easyFMRI} +\docType{package} +\title{ +Functions for FMRI Analysis +} +\description{ +Functions for ROI segmentation in EPI sequences +} +\details{ +\tabular{ll}{ +Package: \tab easyFMRI\cr +Type: \tab Package\cr +Version: \tab 1.0\cr +Date: \tab 2010-09-17\cr +License: \tab GPL (>=2)\cr +LazyLoad: \tab yes\cr +} +} +\author{ +Wonsang You + +Maintainer: Wonsang You <you@ifn-magdeburg.de> +} +\references{ +Wonsang You (2010) ROI Data Extraction from FMRI BOLD Signals of the Human Brain, Technical Reports of the Leibniz Institute for Neurobiology, TR 10016. +} +\keyword{ package } +\seealso{ + +} +\examples{ +ROIdata<-orthoROIs() +} diff --git a/man/meanROIs.Rd b/man/meanROIs.Rd new file mode 100644 index 0000000..791fcd2 --- /dev/null +++ b/man/meanROIs.Rd @@ -0,0 +1,62 @@ +\name{meanROIs} +\alias{meanROIs} +%- Also NEED an '\alias' for EACH other topic documented here. +\title{ +The average time series of each ROI +} +\description{ +Computing the average time series of each ROI in the given EPI sequence +} +\usage{ +meanROIs(EPIfname, nROIs = 31, ROIdir = "", prefix = "roi_", + outfname = "meanROIs", writefile = TRUE) +} +%- maybe also 'usage' for other objects documented here. +\arguments{ + \item{EPIfname}{ +the file name of a EPI sequence (The file format should be ANALYZE such as \code{hdr} and \code{img}. For example, if the original file is "\code{foo.img}", the input value should be "\code{foo}".) +} + \item{nROIs}{ +the number of ROIs. Default is \code{31}. +} + \item{ROIdir}{ +the directory of ROI definition files. These files should contain the individual zipped NIFTI files (nii.gz) corresponding to ROIs. Default is current working directory. +} + \item{prefix}{ +the common prefix of ROI definition files. Default is \code{roi_}. Then, the files should be titled as \code{roi_1.nii.gz}, \code{roi_2.nii.gz}, and so forth. +} + \item{outfname}{ +the outout text file name. Default is \code{meanROIs}. +} + \item{writefile}{ +Enable to write the output text file. Default is \code{TRUE}. +} +} +\details{ +This function computes the average time series of each ROI in the given EPI sequence. The ROI definition files should be given a priori. +} +\value{ +A matrix of averaged time series for ROIs. Row indicates a time point, and column does an ROI. +} +\references{ +Wonsang You (2010) ROI Data Extraction from FMRI BOLD Signals of the Human Brain, Technical Reports of the Leibniz Institute for Neurobiology, TR 10016. +} +\author{ +Wonsang You +} +\note{ +%% ~~further notes~~ +} + +%% ~Make other sections like Warning with \section{Warning }{....} ~ + +\seealso{ +\code{\link{tsROI}} +} +\examples{ +mts<-meanROIs("foo") +} +% Add one or more standard keywords, see file 'KEYWORDS' in the +% R documentation directory. +\keyword{ averaged ROI } +\keyword{ FMRI }% __ONLY ONE__ keyword per line diff --git a/man/orthoROIs.Rd b/man/orthoROIs.Rd new file mode 100644 index 0000000..a62e760 --- /dev/null +++ b/man/orthoROIs.Rd @@ -0,0 +1,93 @@ +\name{orthoROIs} +\alias{orthoROIs} +%- Also NEED an '\alias' for EACH other topic documented here. +\title{ +Orthogonalization of ROIs +} +\description{ +Orthogonalizes ROIs in the EPI sequence. In other words, it eliminates all overlapped parts among ROIs, and then creates new ROIs which are spatially independent each other. +} +\usage{ +orthoROIs(nROIs = 31, ThRes = 5, ROIdir = "", + targetdir = "newROIs", mode = "minimal", prefix = "roi_", + newprefix = "roi_", writefile = TRUE, gzipped = TRUE, + verbose = TRUE) +} +%- maybe also 'usage' for other objects documented here. +\arguments{ + \item{nROIs}{ +the number of ROIs +} + \item{ThRes}{ +the resolution of threshold. It is applied only when you choose the mode as thresholding. For example, if \code{ThRes=5}, the precision becomes \code{1e-5}. Default is \code{5}. +} + \item{ROIdir}{ +the directory of ROI definition files. These files should contain the individual zipped NIFTI files (nii.gz) corresponding to ROIs. Default is current working directory. +} + \item{targetdir}{ +the directory of output ROI definition files. Default is \code{newROIs}. +} + \item{mode}{ +the mode of orthogonalization. \code{thres} denotes thresholding, and \code{minimal} denotes minimal overlapping elimination. Default is \code{minimal}. +} + \item{prefix}{ +the common prefix of ROI definition files. Default is \code{roi_}. Then, the files should be titled as \code{roi_1.nii.gz}, \code{roi_2.nii.gz}, and so forth. +} + \item{newprefix}{ +the common prefix of output ROI definition files. Default is \code{roi_}. +} + \item{writefile}{ +Enable to write the output text file. Default is \code{TRUE}. +} + \item{gzipped}{ +Enable out NIFTI file to be zipped. Default is \code{TRUE}. +} + \item{verbose}{ +Allows message notification duringexecution of the function. +} +} +\details{ +ROIs is normally defined from the anatomical high-resolution image of the brain. To define the ROIs in the EPI sequence, the Brain Voyager can be exploited, however it can cause seriosu distortion such as spatial and temporal blurring. To avoid temporal blurring, we can match each ROI into the EPI space through rotation, translation, and resampling. In this case, overlaps among ROIs can be generated. This function resizes all ROIs by eliminating all overlapped parts. +} +\value{ + \item{ROIData}{ +A list of matrices of ROI data. Each matrix is three-dimensional, and indicates a segmented ROI region at the 3D volume of FMRI data. The number of matrices is the same as the number of ROIs. +} + \item{thr}{ +A vector of thresholds of ROIs. Notice that each ROI can have different threshold. The length of this vector is the same as the number of ROIs. +} + \item{nOverlapROIpairs}{ +An integer which indicates the number of overlapped ROI pairs. +} + \item{nOverlapVoxels}{ +An integer which indicates the number of overalpped voxels. +} + \item{nNewOverlapROIpairs}{ +An integer which indicates the number of overlapped ROI pairs after orthogonalization. In general, it is expected to be zero. +} + \item{nNewOverlapVoxels}{ +An integer which indicates the number of overalpped voxels after orthogonalization. In general, it is expected to be zero. +} +} +\references{ +Wonsang You (2010) ROI Data Extraction from FMRI BOLD Signals of the Human Brain, Technical Reports of the Leibniz Institute for Neurobiology, TR 10016. +} +\author{ +Wonsang You +} +\note{ +%% ~~further notes~~ +} + +%% ~Make other sections like Warning with \section{Warning }{....} ~ + +\seealso{ +%% ~~objects to See Also as \code{\link{help}}, ~~~ +} +\examples{ +dat<-orthoROIs() +} +% Add one or more standard keywords, see file 'KEYWORDS' in the +% R documentation directory. +\keyword{ ROI orthogonalization } +\keyword{ ROI overlap }% __ONLY ONE__ keyword per line diff --git a/man/tsROI.Rd b/man/tsROI.Rd new file mode 100644 index 0000000..641181a --- /dev/null +++ b/man/tsROI.Rd @@ -0,0 +1,61 @@ +\name{tsROI} +\alias{tsROI} +%- Also NEED an '\alias' for EACH other topic documented here. +\title{ +Extraction of ROI Time Series +} +\description{ +Extracts a set of time series which belong to a predefined ROI in the given EPI sequence. +} +\usage{ +tsROI(ROIidx, EPIfname, ROIdir = "", prefix = "roi_", + outfname = "tsROI", writefile = TRUE) +} +%- maybe also 'usage' for other objects documented here. +\arguments{ + \item{ROIidx}{ +the index of ROI +} + \item{EPIfname}{ +the file name of a EPI sequence (The file format should be ANALYZE such as \code{hdr} and \code{img}. For example, if the original file is "\code{foo.img}", the input value should be "\code{foo}".) +} + \item{ROIdir}{ +the directory of ROI definition files. These files should contain the individual zipped NIFTI files (nii.gz) corresponding to ROIs. Default is current working directory. +} + \item{prefix}{ +the common prefix of ROI definition files. Default is \code{roi_}. Then, the files should be titled as \code{roi_1.nii.gz}, \code{roi_2.nii.gz}, and so forth. +} + \item{outfname}{ +the outout text file name. Default is \code{tsROI}. +} + \item{writefile}{ +Enable to write the output text file. Default is \code{TRUE}. +} +} +\details{ +This function extracts a set of time series of a predefined ROI in the given EPI sequence. The ROI definition file should be given a priori. +} +\value{ +A matrix of time series in a ROI. Row indicates a time point, and column does a voxel in the ROI. +} +\references{ +Wonsang You (2010) ROI Data Extraction from FMRI BOLD Signals of the Human Brain, Technical Reports of the Leibniz Institute for Neurobiology, TR 10016. +} +\author{ +Wonsang You +} +\note{ +%% ~~further notes~~ +} + +%% ~Make other sections like Warning with \section{Warning }{....} ~ + +\seealso{ +\code{\link{meanROIs}} +} +\examples{ +mts<-meanROIs(10,"foo") +} +% Add one or more standard keywords, see file 'KEYWORDS' in the +% R documentation directory. +\keyword{ ROI extraction }