Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
wonsang committed Jun 3, 2017
0 parents commit d0d4765
Show file tree
Hide file tree
Showing 9 changed files with 487 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -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>
12 changes: 12 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions R/meanROIs.R
Original file line number Diff line number Diff line change
@@ -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)
}

130 changes: 130 additions & 0 deletions R/orthoROIs.R
Original file line number Diff line number Diff line change
@@ -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)
}

37 changes: 37 additions & 0 deletions R/tsROI.R
Original file line number Diff line number Diff line change
@@ -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)
}

35 changes: 35 additions & 0 deletions man/easyFMRI-package.Rd
Original file line number Diff line number Diff line change
@@ -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()
}
62 changes: 62 additions & 0 deletions man/meanROIs.Rd
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit d0d4765

Please sign in to comment.