download_hcp_csvs.R

library(neurohcp)
library(plyr)
library(dplyr)
library(readr)


# group = "HCP_900"
# group = "HCP_1200"
# group = "HCP_Retest"
# group = "HCP"
for (group in c("HCP", "HCP_900", "HCP_1200")) {
  ids = hcp_ids(group = group)
  all_csvs =  paste0(group, "/", ids, "/unprocessed/3T/", ids, "_3T.csv")
  outdir = file.path("data", paste0(group, "_csvs"))
  dir.create(outdir, showWarnings = FALSE)
  destfile = file.path(outdir,
                       basename(all_csvs))
  mapply(function(x, y){
    if (!file.exists(y)) {
      download_hcp_file(path_to_file = x, destfile = y, error = FALSE)
      print(y)
    }
  }, all_csvs, destfile)

  destfile = destfile[file.exists(destfile)]

  system(paste0("grep -l xml data/", group, "_csvs/*.csv | xargs rm"))
  destfile = destfile[file.exists(destfile)]
  sizes = sapply(destfile, file.size)

  if (any(sizes == 0)){
    bad = destfile[ sizes == 0]
    file.remove(bad)
  }
  destfile = destfile[file.exists(destfile)]

  types = paste(rep("c", 17), collapse = "")
  res = llply(destfile, read_csv,
              col_names = FALSE,
              col_types = types,
              .progress = "text")
  ids = gsub("(.*)_3T.*", "\\1", basename(destfile))
  res = mapply(function(df, id) {
    df$id = id
    return(df)
  }, res, ids, SIMPLIFY = FALSE)
  outname = paste0(tolower(group), "_scanning_info")
  df =  bind_rows(res)


  cn = c("Session_Day",
         "Acquisition_Time",
         "Session",
         "Scan_Number",
         "Scan_Type",
         "Scan_Description",
         "Shim_Group",
         "BiasField_group",
         "SE_FieldMap_group",
         "GE_FieldMap_group",
         "PE_Direction",
         "Readout_Direction",
         "E_Prime_Script",
         "Scan_Order",
         "Scan_Complete",
         "Percent_Complete",
         "Percent_Pair_Complete",
         "id")
  cn = tolower(cn)
  colnames(df) = cn

  assign(outname, df)

  outfile = file.path("data", paste0(outname, ".rda"))
  save(list = outname,
       file = outfile,
       compress = "xz")

}
muschellij2/hcp documentation built on Sept. 13, 2021, 4:21 p.m.