-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_outcount.R
60 lines (48 loc) · 1.87 KB
/
merge_outcount.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# author: dani cosme
# email: [email protected]
# version: 0.1
# date: 2017-03-03
# This script loads afni 3dToutCount files and returns 'study_outcount.csv'
#------------------------------------------------------
# load packages
#------------------------------------------------------
osuRepo = 'http://ftp.osuosl.org/pub/cran/'
if(!require(tidyverse)){
install.packages('tidyverse',repos=osuRepo)
}
#------------------------------------------------------
# define variables
#------------------------------------------------------
# paths
afniDir = "/Volumes/psych-cog/dsnlab/auto-motion-output/AFNI/FP/"
outputDir = '/Volumes/psych-cog/dsnlab/auto-motion-output/'
# variables
study = "FP"
pattern = "(FP[0-9]{3})_(.*)_(p[0-9]{1}).csv"
#------------------------------------------------------
# load files and concatenate them
#------------------------------------------------------
# generate file list
file_list = list.files(afniDir, pattern = pattern)
for (file in file_list){
# if the merged dataset doesn't exist, create it
if (!exists("dataset")){
temp = read.table(paste0(afniDir,file)) %>% rename("outliers" = V1)
dataset = data.frame(file = rep(file,count(temp)), temp) %>%
mutate(volume = sprintf("%04d",row_number())) %>%
extract(file,c("subjectID","run","poly"), pattern)
}
# if the merged dataset does exist, append to it
else {
temp = read.table(paste0(afniDir,file)) %>% rename("outliers" = V1)
temp_dataset = data.frame(file = rep(file,count(temp)), temp) %>%
mutate(volume = sprintf("%04d",row_number())) %>%
extract(file,c("subjectID","run","poly"), pattern)
dataset = rbind(dataset, temp_dataset)
rm(temp_dataset)
}
}
#------------------------------------------------------
# write csv
#------------------------------------------------------
write.csv(dataset, paste0(outputDir,study,'_outcount.csv'), row.names = FALSE)