Last updated: 2021-07-20 09:21:40  

As of 7/20/21::
* When an aquistion/series is repeated, new physio files get _1 suffix.

 

1) Revise task filenames for consistancy -

    find . -type f -name 'sub*_ses-1*' | while read FILE ; do
    newfile="$(echo ${FILE} |sed -e 's/ses-1/ses-01/')" ;
    mv "${FILE}" "${newfile}" ;
    done 


2) Rename .tsv files for BIDS standard & copy to local tmp dir-

setwd("/Volumes/eBACH/Data/")
Subjects<-list.files("/Volumes/eBACH/Data",pattern="E1267")
SessionList=c("1")
TaskList<-c("msit","stroop" , "emoreg")
temp_dir= "/Users/alyshagilmore/eBACH_Data/eBACH_Trash/Reha_event_files/"
df_total<-""

for (i in Subjects){
  for (Session in  SessionList){
    for (Task in TaskList){
      if (Task=="stroop") {
        TASK_DIR<-paste("/Volumes/eBACH/Data/", i , "/T", Session,"/MRI_",Session ,"/sub-",i,"/ses-",Session,"/" , sep = "")
        file_name<-paste("sub-",i, "_ses-0",Session,"_task-", Task, "_acq-mb3_bold.tsv" , sep = "")
        file<- paste(TASK_DIR, file_name, sep = "")
      } else {
        TASK_DIR<-paste("/Volumes/eBACH/Data/", i , "/T", Session,"/MRI_",Session ,"/sub-",i,"/ses-",Session,"/" , sep = "")
        file_name<-paste("sub-",i, "_ses-0",Session,"_task-", Task, "_acq-mb3_events.tsv" , sep = "")
        file<- paste(TASK_DIR, file_name, sep = "")
        
      }
        df.row<-paste(i, Session, Task, sep=" " )
        
        if (file.exists(file)==TRUE) {
          events<-readr::read_tsv(file)
          file_name_local<-paste("sub-",i, "_ses-0",Session,"_task-", Task, "_acq-mb3_events.tsv" , sep = "")
          if (Task=="emoreg") {
            file_name_local<-paste("sub-",i, "_ses-0",Session,"_task-", "emoreap_acq-mb3_events.tsv" , sep = "")
          }
          file_path_local<-paste(temp_dir, file_name_local, sep="")
          readr::write_tsv(events, file_path_local)
          dt<-  data.frame(i, Session, Task, file_path_local)
          df_total <- rbind(df_total,dt)
          df_total<-as.data.frame(df_total)} 
        else {NULL}
    }
  }
}

FILE<-paste(temp_dir,"BIDS_Standard_",Sys.Date(),"csv", sep="")
write.csv(df_total,FILE)


3) Connect to Flywheel SDK -

# Load reticulate
library("reticulate")

path_to_py<-"/Library/Frameworks/Python.framework/Versions/3.9/bin/python3"

# Configure reticulate to use python3
use_python(path_to_py, required = TRUE)

# Import flywheel python module
flywheel <- import_from_path(
  module = "flywheel", 
  path="/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/")

# Import os python module  
os <- import(module = "os")
fw <- flywheel$Client("bridge-center.flywheel.io:rrYiXvK3qqVgIDJBvX")
self <- fw$get_current_user()

# Check connection
sprintf("I am %s %s", self$firstname, self$lastname)


4) Upload timing files to Flywheel from local temp dir -

library(stringr)
temp_dir= "/Users/alyshagilmore/eBACH_Data/eBACH_Trash/Reha_event_files/"
File_list<-list.files(temp_dir, pattern = "E1267")
project =fw$lookup('kericksonlab/eBACH')
project = fw$get_project(project$id)
project_label=project$label
lab=project$group
df<-""
i=File_list[1]
for (i in File_list){
three<-str_split_fixed(i, pattern = c("-"), 2)
two<-str_split_fixed(three, "_", 2)
subject_id=two[2,1]
three<-str_split_fixed(i, pattern = c("_"), 3)
session_id<-three[1,2]
two<-str_split_fixed(i, pattern = c("task-"), 2)
aqu<-str_split_fixed(two, pattern = c("_events.tsv"), 2)
aqu<-aqu[2,1]
aquistion_id<-paste("func-bold_task-",aqu, sep="")
file_id<-paste("func-bold_task-",aqu, sep="")
aquistion_lookup<-c(lab, project_label, subject_id, session_id,aquistion_id )
file_lookup<-c(lab, project_label, subject_id, session_id, i)
aquistion_id
aquistion_lookup<-try(fw$get(fw$lookup(aquistion_lookup)$id), silent = TRUE)
(aquistion_lookup)

if (isTRUE(class(aquistion_lookup)=="try-error")) {NULL} 
else {
  file_lookup<-try(fw$get(fw$lookup(file_lookup)$id), silent = TRUE)
  if (isTRUE(class(file_lookup)=="try-error")) {
      file_url<- paste(temp_dir,i, sep="")
      aquistion_lookup$upload_file(file_url)
      
      v<-paste(subject_id, session_id, aquistion_id)
      dt<-data_frame(subject_id, session_id, aquistion_id)
      df<-rbind(dt, df)
      print(v)
  } else {NULL}
}
}
date=as.Date(Sys.Date(), format="%")
file=paste(temp_dir, "Flywheel_Transferred_",date, ".csv")
write.csv(df, file)


---

 

5) When additional series are collected...

e.g. if scanner crashes, task/series restarted, etc.
Working on code to remove extra/additional aquistions by filtering-in duplicated session aquistions and moving the earliest collected to eBACH trash project..

## WORKING ##

   

6) Remove Extra nifti files generated by flywheel bug

## WORKING ##
project =fw$lookup('kericksonlab/eBACH')
project = fw$get_project(project$id)
project_label=project$label
lab=project$group

 # Test
subject_id="E1018"
session_id="ses-01"
aquistion_id<-"func-bold_task-emoreap_acq-mb3"


aquistion_lookup<-paste(lab,"/", project_label,"/", subject_id ,"/",session_id,"/",aquistion_id,"/", sep="" )


aquistion_lookup<-try(fw$get(fw$lookup(aquistion_lookup)$id), silent = TRUE)
(aquistion_lookup)

       #   'MRAcquisitionType': '2D',

files<-aquistion_lookup$get_files
aquistion_lookup$find(file$type=="qa") 
fw$projects
for (file in aquistion_lookup$) {
  if (file$type=="qa") {print(file$id)}
}