Module: DefaultPreproc

Included in:
Preprocessing
Defined in:
lib/default_methods/default_preproc.rb

Instance Method Summary collapse

Instance Method Details

#check_permissions(files) ⇒ Object

Raises:

  • (IOError)


33
34
35
36
37
# File 'lib/default_methods/default_preproc.rb', line 33

def check_permissions(files)
 unwritable = []
 files.collect { |file| unwritable << file unless File.writable?(file) }
  raise IOError, "Cannot write to #{unwritable.join(", ")} files." unless unwritable.empty?
end

#customize_templatesObject

Customizes the template job in preproc_spec to be specific for this particular preproc job. Performs to recursive string replacements inside the spm job:

  • path inside template is replaced with destination “proc” directory

  • subid inside template is replaced with the current subid



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/default_methods/default_preproc.rb', line 43

def customize_templates
	flash "Customizing template SPM job: #{@tspec['job']}"
	replacecmd = "spmjobStringReplace.sh"
	
	templatejob = File.join(@spmdir, @tspec['job'])
	thisjob = @subid + '_preproc.mat'
	
	File.copy(templatejob, thisjob)
	system("#{replacecmd} #{thisjob} #{@tspec['path']} #{@procdir} #{thisjob}")
	system("#{replacecmd} #{thisjob} #{@tspec['subid']} #{@subid} #{thisjob}")
end

#deal_with_motionObject

Calculates the realignment motion derivatives and checks that displacement in all directions was less than the MOTION_THRESHOLD. Operates on all files in the current working directory that match rp_a*txt (SPM convention). Uses two shell scripts that must both be available on the local machine:

  • calc_derivs.sh

  • fmri_motion_check.sh



68
69
70
71
72
73
74
# File 'lib/default_methods/default_preproc.rb', line 68

def deal_with_motion
	flash "Calculating motion derivatives and checking for excessive motion"
	Dir.glob("rp_a*txt").each do |rp|
		run("calc_derivs.sh #{rp}")
		run("fmri_motion_check.sh #{rp} #{@motion_threshold}")
	end
end

Links all the slice timing corrected data from the source “orig” directory using a wildcard a$subid*.nii, where subid is the subject id specified in the preproc_spec hash.



22
23
24
25
26
27
28
29
30
31
# File 'lib/default_methods/default_preproc.rb', line 22

def link_files_into_proc
	flash "Linking files from #{@origdir} into #{@procdir}"
	wildcard = File.join(@origdir,"a*#{@subid}*.nii")
	files = Dir.glob(wildcard)
	unless files.empty?
	  system("ln -s #{wildcard} #{@procdir}")
  else
    raise(IOError, "No files matching #{wildcard} found.")
   end
end

#preproc_visitObject Also known as: perform

Runs the preprocessing job, including spm job customization, run spm job, and handling motion issues. This function assumes a destination directory is set up; it will overwrite preexisting data. Careful!



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/default_methods/default_preproc.rb', line 5

def preproc_visit
	$Log.info "Spatial Preprocessing Subject: #{@subid}"
	
	setup_directory(@procdir, "PREPROC")
	
	Dir.chdir(@procdir) do
		link_files_into_proc
		customize_templates
		run_spm_jobs
		deal_with_motion
	end
end

#run_spm_jobsObject

Runs the customized spm job using the shell script runSpmJob.sh. Make sure this is available at your site.



56
57
58
59
60
# File 'lib/default_methods/default_preproc.rb', line 56

def run_spm_jobs
	thisjob = "#{@subid}_preproc.mat"
	flash "Running spatial preprocessing SPM job: #{thisjob}"
	system("runSpmJob.sh #{thisjob}")
end