Module: DefaultRecon

Included in:
Reconstruction
Defined in:
lib/default_methods/default_recon.rb,
lib/default_methods/recon/raw_sequence.rb,
lib/default_methods/recon/physionoise_helper.rb

Overview

require ‘default_methods/recon/physionoise_helper’

Defined Under Namespace

Classes: DicomRawSequence, PfileRawSequence, RawSequence

Instance Method Summary collapse

Instance Method Details

#build_physionoise_run_spec(rpipe_scan_spec) ⇒ Object

Build a Run Spec from a Scan Spec This should be moved to the generators and shouldn’t be used here.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/default_methods/recon/physionoise_helper.rb', line 19

def build_physionoise_run_spec(rpipe_scan_spec)
  run = rpipe_scan_spec['physio_files'].dup
  flash "Physionoise Regressors: #{run[:phys_directory]}"
  run[:bold_reps] = rpipe_scan_spec['bold_reps']
  run[:rep_time] = rpipe_scan_spec['rep_time']
  unless Pathname.new(run[:phys_directory]).absolute?
    run[:phys_directory] = File.join(@rawdir, run[:phys_directory])
  end
  run[:run_directory] = @rawdir
  runs = [run]
end

#build_retroicor_cmd(physio_files, file) ⇒ Object

:respiration_trigger: RESPTrig_epiRT_0303201014_46_27_463

:phys_directory: cardiac/
:cardiac_signal: PPGData_epiRT_0303201014_46_27_463
:cardiac_trigger: PPGTrig_epiRT_0303201014_46_27_463


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/default_methods/recon/physionoise_helper.rb', line 52

def build_retroicor_cmd(physio_files, file)
  [:cardiac_signal, :respiration_signal].collect {|req| raise ScriptError, "Missing physio config: #{req}" unless physio_files.include?(req)}
  
  prefix = 'p'
  unless Pathname.new(physio_files[:cardiac_signal]).absolute?
    cardiac_signal = File.join(@rawdir, physio_files[:phys_directory], physio_files[:cardiac_signal])
  end
  
  unless Pathname.new(physio_files[:respiration_signal]).absolute?
    respiration_signal = File.join(@rawdir, physio_files[:phys_directory], physio_files[:respiration_signal])
  end
  
  outfile = prefix + file

  icor_format = "3dretroicor -prefix %s -card %s -resp %s %s"
  icor_options = [outfile, cardiac_signal, respiration_signal, file]
  icor_cmd = icor_format % icor_options
  return icor_cmd, outfile
end

#create_physiosnoise_regressors(scan_spec) ⇒ Object

Create Physionoise Regressors for Inclusion in GLM



6
7
8
9
# File 'lib/default_methods/recon/physionoise_helper.rb', line 6

def create_physiosnoise_regressors(scan_spec)
  runs = build_physionoise_run_spec(scan_spec)
  Physionoise.run_physionoise_on(runs, ["--saveFiles"])
end

#generate_physiospecObject

Generate a Physionoise Spec



12
13
14
15
# File 'lib/default_methods/recon/physionoise_helper.rb', line 12

def generate_physiospec
 physiospec = Physiospec.new(@rawdir, File.join(@rawdir, '..', 'cardiac'))
 physiospec.epis_and_associated_phys_files
end

#recon_visitObject Also known as: perform

Reconstructs, strips, and slice timing corrects all scans specified in the recon_spec. This function assumes a destination directory is set up in the filesystem and begins writing to it with no further checking. It will overwrite data if it already exists, be careful.



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
# File 'lib/default_methods/default_recon.rb', line 11

def recon_visit
	
	setup_directory(@origdir, "RECON")
	
	Dir.chdir(@origdir) do		  
		@scans.each_with_index do |scan_spec, i|
			outfile = "%s_%s.nii" % [@subid, scan_spec['label']]
			
			reconstruct_scan(scan_spec, outfile)
			
			if scan_spec['type'] == "func"
         # if scan_spec['physio_files']
         #   create_physiosnoise_regressors(scan_spec)
         #   outfile = run_retroicor(scan_spec['physio_files'], outfile)
         # end
			  
				slice_time_correct(outfile)
			else
				File.copy('tmp.nii', outfile)
			end
			
			File.delete('tmp.nii') if File.exist? 'tmp.nii'
		end
	end
end

#reconstruct_scan(scan_spec, outfile) ⇒ Object

Reconstructs a scan from dicoms or pfile to nifti, anatomical or functional. Uses a scan_spec hash to drive. Writes the result in current working directory. Raises an error if to3d system call fails. Conventions: I****.dcm filenaming, I0002.dcm is second file in series,



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

def reconstruct_scan(scan_spec, outfile)	
	if scan_spec['dir']
	  sequence = DicomRawSequence.new(scan_spec, @rawdir)
	  sequence.prepare('tmp.nii')
		strip_leading_volumes('tmp.nii', outfile, @volume_skip, scan_spec['bold_reps'])
  elsif scan_spec['pfile']
    sequence = PfileRawSequence.new(scan_spec, @rawdir)
    sequence.prepare(outfile)
   else 
     raise ConfigError, "Scan must list either a pfile or a dicom directory."
   end
end

#run_retroicor(physio_files, file) ⇒ Object

Runs 3dRetroicor for a scan. Returns the output filename if successful or raises an error if there was an error.



33
34
35
36
37
38
39
40
41
# File 'lib/default_methods/recon/physionoise_helper.rb', line 33

def run_retroicor(physio_files, file)
 icor_cmd, outfile = build_retroicor_cmd(physio_files, file)
 flash "3dRetroicor: #{file} \n #{icor_cmd}"
 if run(icor_cmd)
   return outfile
  else
    raise ScriptError, "Problem running #{icor_cmd}"
  end
end

#slice_time_correct(infile) ⇒ Object

Uses to3d to slice time correct a 4D functional nifti file. Writes result in the current working directory.



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

def slice_time_correct(infile)
	$Log.info "Slice Timing Correction: #{infile}"
	cmd = "3dTshift -tzero 0 -tpattern alt+z -prefix a#{infile} #{infile}"
	unless run(cmd)
	  raise ScriptError, "Failed to slice time correct: #{cmd}"
  end
end

#strip_leading_volumes(infile, outfile, volume_skip, bold_reps) ⇒ Object

Removes the specified number of volumes from the beginning of a 4D functional nifti file. In most cases this will be 3 volumes. Writes result in current working directory.



58
59
60
61
62
63
64
65
66
# File 'lib/default_methods/default_recon.rb', line 58

def strip_leading_volumes(infile, outfile, volume_skip, bold_reps)
	$Log.info "Stripping #{volume_skip.to_s} leading volumes: #{infile}"
	cmd_fmt = "fslroi %s %s %s %s"
	cmd_options = [infile, outfile, volume_skip.to_s, bold_reps.to_s]
	cmd = cmd_fmt % cmd_options
	unless run(cmd)
	  raise ScriptError, "Failed to strip volumes: #{cmd}"
  end
end