Skip to content

Commit

Permalink
new nki stuff; setup subject lists; run subdists
Browse files Browse the repository at this point in the history
  • Loading branch information
czarrar committed May 10, 2013
1 parent 187a9bb commit 22d1ea1
Show file tree
Hide file tree
Showing 262 changed files with 6,464 additions and 2 deletions.
20 changes: 20 additions & 0 deletions nki/03_post-process/22_funcpaths_to_4mm.bash
@@ -0,0 +1,20 @@
#!/bin/bash

cd /home2/data/Projects/CWAS/share/nki/subinfo

strategies=("compcor" "global")
scans=( "short" "medium" "long" )
scan_folder=("40_Set1_N104" "40_Set1_N104" "40_Set2_N92")

for strategy in ${strategies}; do
echo "strategy: ${strategy}"
for i in $( count -digits 1 0 2); do
scan=${scans[$i]}
folder=${scan_folder[$i]}
echo " scan: ${scan}"

inpath="${folder}/${scan}_${strategy}_funcpaths.txt"
outpath="${folder}/${scan}_${strategy}_funcpaths_4mm.txt"
sed s/functional_mni/functional_mni_4mm/g ${inpath} > ${outpath}
done
done
72 changes: 72 additions & 0 deletions nki/03_post-process/24_generate_mask_4mm.R
@@ -0,0 +1,72 @@
#!/usr/bin/env Rscript

# This script generates brain mask for each subject in 4mm space
# and then creates the group brain mask

library(connectir)

###
# Setup
###

# Paths
basedir <- "/home2/data/Projects/CWAS/share/nki"
subinfo <- file.path(basedir, "subinfo")
roidir <- "/home2/data/Projects/CWAS/nki/rois"

# Scan stuff
scans <- c("short", "medium", "long")
scan_folder <- c("40_Set1_N104", "40_Set1_N104", "40_Set2_N92")

# Other
strategies <- c("compcor", "global")
strategy <- strategies[1]


###
# Do it
###

maskfiles <- lapply(1:length(scans), function(si) {
vcat(T, "scan: %s", scans[si])

## Individual Subjects
vcat(T, "...individual subjects")
flist <- file.path(subinfo, scan_folder[si], sprintf("%s_%s_funcpaths_4mm.txt", scans[si], strategy))
raw <- read.table(flist)[,1]
funcpaths <- as.character(raw)
funcpaths <- sub("/home/", "/home2/", funcpaths)

cmd <- "fslmaths %s -Tstd -abs -bin %s"
maskfiles <- sapply(funcpaths, function(funcpath) {
outdir <- dirname(dirname(funcpath))
outfile <- file.path(outdir, "functional_brain_mask_to_standard_4mm.nii.gz")
real_cmd <- sprintf(cmd, funcpath, outfile)

if (file.exists(outfile)) {
cat("skipping, file exists", outfile, "\n")
return(outfile)
} else {
cat(real_cmd, "\n")
system(real_cmd)
return(outfile)
}
})

return(maskfiles)
})
maskfiles <- unlist(maskfiles)


## Group Mask
vcat(T, "...group mask")
outfile <- file.path(roidir, "mask_gray_4mm.nii.gz")
std_mask_file <- file.path(roidir, "grey_matter_4mm.nii.gz")

hdr <- read.nifti.header(std_mask_file)
overlap_mask <- read.mask(std_mask_file)

for (maskfile in maskfiles)
overlap_mask <- overlap_mask & read.mask(maskfile)

write.nifti(overlap_mask*1, hdr, outfile=outfile, odt="int")
Expand Up @@ -26,7 +26,7 @@

# name of the maskfile that we will be using
roidir = "/home2/data/Projects/CWAS/nki/rois"
maskfile = path.join(roidir, "mask_gray_2mm.nii.gz")
maskfile = path.join(roidir, "mask_gray_4mm.nii.gz")


###
Expand All @@ -46,7 +46,7 @@
print "3. Clustering"

from binfile_parcellation import *

ks = [25,50,100,200,400,800,1600,3200,6400]

# For random custering, this is all we need to do, there is no need for group
Expand Down
100 changes: 100 additions & 0 deletions nki/03_post-process/28_extract_rois_4mm.R
@@ -0,0 +1,100 @@
#!/usr/bin/env Rscript

# This script will extract the time-series from each of the ROI sets


###
# Setup
###

library(connectir)
library(tools)

# Paths
basedir <- "/home2/data/Projects/CWAS/share/nki"
subinfo <- file.path(basedir, "subinfo")

# Scan stuff
scans <- c("short", "medium", "long")
scan_folder <- c("40_Set1_N104", "40_Set1_N104", "40_Set2_N92")

# Other
strategies <- c("compcor", "global")

# ROI stuff
rbase <- "/home2/data/Projects/CWAS/nki/rois"
mask_file <- file.path(rbase, "mask_gray_4mm.nii.gz")
ks <- c(25,50,100,200,400,800,1600,3200,6400)
#ks <- c(3200)
roi_files <- file.path(rbase, sprintf("rois_random_k%04i.nii.gz", ks))

set_parallel_procs(nforks=1, nthreads=4)


###
# ROI Extraction (Random) with compcor
###

strategy <- "compcor"
vcat(T, strategy)

for (si in 1:length(scans)) {
# Paths to functionals
fn <- file.path(subinfo, scan_folder[si],
sprintf("%s_%s_funcpaths_4mm.txt", scans[si], strategy))
func_files <- read.table(fn)[,1]
func_files <- as.character(func_files)

# Go through ROIs
for (roi_file in roi_files) {
vcat(T, "ROI: %s", roi_file)
roi_base <- file_path_sans_ext(file_path_sans_ext(basename(roi_file)))
out_files <- sapply(func_files, function(func_file) {
out_file <- file.path(dirname(func_file),
paste(roi_base, ".nii.gz", sep=""))
if (!file.exists(out_file)) {
roi_mean_wrapper(func_file, roi_file, mask_file, out_file)
}
return(out_file)
})
out_files <- unlist(out_files)

ofile <- file.path(subinfo, scan_folder[si], sprintf("%s_%s_%s.txt", scans[si], strategy, roi_base))
write.table(out_files, file=ofile, row.names=F, col.names=F)
}
}



####
## ROI Extraction (Random) with global
####
#
#strategy <- "global"
#vcat(T, strategy)
#
#for (si in 1:length(scans)) {
# # Paths to functionals
# fn <- file.path(subinfo, scan_folder[si],
# sprintf("%s_%s_funcpaths.txt", scans[si], strategy))
# func_files <- read.table(fn)[,1]
# func_files <- as.character(func_files)
#
# # Go through ROIs
# for (roi_file in roi_files) {
# vcat(T, "ROI: %s", roi_file)
# roi_base <- file_path_sans_ext(file_path_sans_ext(basename(roi_file)))
# out_files <- sapply(func_files, function(func_file) {
# out_file <- file.path(dirname(func_file), paste(roi_base, ".nii.gz", sep=""))
# roi_mean_wrapper(func_file, roi_file, mask_file, out_file)
# return(out_file)
# })
# out_files <- unlist(out_files)
#
# ofile <- file.path(subinfo, scan_folder[si], sprintf("%s_%s_%s.txt", scans[si], strategy, roi_base))
# write.table(out_files, file=ofile, row.names=F, col.names=F)
# }
#}



57 changes: 57 additions & 0 deletions nki/03_post-process/30_drop_bad_participant.R
@@ -0,0 +1,57 @@
#!/usr/bin/env Rscript

# This script will take out the participant below from everything!
# and then resave

# Paths
basedir <- "/home2/data/Projects/CWAS/share/nki"
subinfo <- file.path(basedir, "subinfo")
roidir <- "/home2/data/Projects/CWAS/nki/rois"

# Scan stuff
scan_folders <- c("40_Set1_N104", "40_Set2_N92")

# Subject
bad_subject <- "M10982376"

# Loop through
for (folder in scan_folders) {
cat("changing directory\n")
cat(file.path(subinfo, folder), "\n")
setwd(file.path(subinfo, folder))

txt_files <- list.files(pattern="txt$")
csv_files <- list.files(pattern="csv$")

if (!file.exists("archive")) {
cat("creating + copying stuff\n")
dir.create("archive", FALSE)
file.copy(txt_files, file.path("archive", txt_files))
file.copy(csv_files, file.path("archive", csv_files))
}

cat("textify\n")
for (txt_file in txt_files) {
cat("-", txt_file, "\n")
txt <- as.character(read.table(txt_file)[,1])
find <- grep(bad_subject, txt)
if (length(find) > 0) {
cat("-- replacing\n")
txt <- txt[-find]
write.table(txt, file=txt_file)
}
}

cat("csvify\n")
for (csv_file in csv_files) {
cat("-", csv_file, "\n")
csv <- read.csv(csv_file)
find <- grep(bad_subject, csv$Id)
if (length(find) > 0) {
cat("-- replacing\n")
csv <- csv[-find,]
row.names(csv) <- 1:nrow(csv)
write.csv(csv, file=csv_file)
}
}
}
Binary file added nki/05_cwas/ /bg_image.nii.gz
Binary file not shown.
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0001.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0002.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0003.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0004.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0005.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0006.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0007.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0008.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0009.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0010.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0011.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0012.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0013.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0014.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0015.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0016.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0017.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0018.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0019.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0020.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0021.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0022.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0023.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0024.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0025.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0026.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0027.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0028.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0029.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0030.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0031.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0032.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0033.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0034.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0035.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0036.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0037.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0038.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0039.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0040.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0041.nii.gz
1 change: 1 addition & 0 deletions nki/05_cwas/ /input_funcs/scan0042.nii.gz

0 comments on commit 22d1ea1

Please sign in to comment.