Class: WadrcBcpScripts::FreesurferRoiTask

Inherits:
BasicTask
  • Object
show all
Defined in:
lib/wadrc-bcp-scripts/freesurfer_roi_task.rb

Overview

We want to sample to ASL perfusion data (PET data, or other data) from ROIs, determined in subject space by running a high-resolution T1 through Freesurfer. As part of it’s recon-all, freesurfer outputs segmentation images (volume parcellation) that include ~ 100 ROIs for cortical gm, wm and sub-cortical structures. All we need to do is get that segmentation into a common space with the image of interest, and then we can sample it to get meaningful estimates of the data.

The Process: 1) Do Basic Reconstruction for Anatomical Images 2) Run a high resolution T1 through Freesurfer 3) Convert the aparc.a2009s+aseg.mgz back to NIfTI in T1 space for sampling. 4) Rigidly register & reslice Target modality to T1 space 5) Sample mean and measures of interest

Instance Attribute Summary

Attributes inherited from BasicTask

#config

Instance Method Summary collapse

Methods inherited from BasicTask

#check_setup, #config_requires, #ensure_file_exists, #environment_requires

Constructor Details

#initialize(raw_directory, output_directory, config) ⇒ FreesurferRoiTask

Returns a new instance of FreesurferRoiTask.



18
19
20
21
22
23
# File 'lib/wadrc-bcp-scripts/freesurfer_roi_task.rb', line 18

def initialize(raw_directory, output_directory, config)
  @config = config
  @raw_directory = raw_directory
  @output_directory = output_directory
  @commands = ShellQueue.new(:dry_run => true)
end

Instance Method Details

#basic_anatomical_reconstructionObject

Do Basic Reconstruction for Anatomical Images



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wadrc-bcp-scripts/freesurfer_roi_task.rb', line 26

def basic_anatomical_reconstruction

  # Create the T1 & other Anatomicals
  @commands << "convert_visit.rb #{@raw_directory} #{@config[:subj_processed_dir]}"

  # Create the ASL
  modality_dir = File.join(@config[:subj_processed_dir], @config[:modality])
  Dir.mkdir_p modality_dir unless File.exists? modality_dir 
  Dir.chdir modality_dir do
    # fmap_make /Data/vtrak1/raw/dempsey.plaque.visit1/plq20005_1959_04072011/009
    #(Or, to search automatically: )
    @commands << "fmap_make `list_visit #{@raw_directory} -g #{@config[:modality]}`"

    # Link the T1 into the ASL directory for easy visualization if you want.

    # File.symlink("../unknown/plq02002_Ax-FSPGR-BRAVO_003.nii", "plq02002_Ax-FSPGR-BRAVO_003.nii")
  end
end

#prepare_segmentationObject

Convert the aparc.a2009s+aseg.mgz back to NIfTI in T1 space for sampling.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wadrc-bcp-scripts/freesurfer_roi_task.rb', line 56

def prepare_segmentation
  aparc_base = "aparc.a2009s+aseg"
  freesurfer_subj_mri_dir = File.join(OPTIONS[:freesurfer_subjects_dir], OPTIONS[:subid], "mri")

  Dir.chdir modality_dir do 
    system("mri_convert #{File.join(freesurfer_subj_mri_dir, aparc_base)}.mgz #{apar_base}.nii")

    # Resample the Segementation image to the T1 space (using nearest neighbor so as
    # to not change any values):
    system("
      flirt -in aparc.a2009s+aseg.nii -ref plq20005_Ax-FSPGR-BRAVO_003.nii \
      -out raparc.a2009s+aseg.nii -applyxfm -init $FSLDIR/etc/flirtsch/ident.mat \
      -interp nearestneighbour"
    )
  end

  # You could resample with SPM as well (Coregister - Write) but this is a nice
  # command line option. For the actual registration, we are using SPM because
  # it's a somewhat better (qualitatively) algorithm.
end

#register_modality_to_t1Object

Rigidly register & reslice Target modality to T1 space



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wadrc-bcp-scripts/freesurfer_roi_task.rb', line 78

def register_modality_to_t1
  # For ASL, we ill use the PD image because it's information is closer to
  # anatomical than the computed flow maps, bringing along the flow maps. 

  # system("spm8")
  # Click Coregister - Estimate and Reslice
  # Reference Image: Select the BRAVO
  # Source Image: Select the PD Map
  # Other Images: Select the ASL Map
  # Use other defaults (NMI, etc.) 
end

#run_t1_through_freesurferObject

Run a high resolution T1 through Freesurfer



46
47
48
49
50
51
52
53
# File 'lib/wadrc-bcp-scripts/freesurfer_roi_task.rb', line 46

def run_t1_through_freesurfer
  ENV[:SUBJECTS_DIR] = @proc_options[:freesurfer_subjects_dir]

  system("recon-all -all -s #{OPTIONS[:subid]} -i #{File.join(OPTIONS[:subj_raw_dir], OPTIONS[:subid], "003/I0001.dcm")}")

  # This will run for 20 hours, and return a pretty subject directory. See below
  # for a sample manifest.
end

#sample_roiObject

Sample mean and measures of interest



91
92
93
# File 'lib/wadrc-bcp-scripts/freesurfer_roi_task.rb', line 91

def sample_roi
  system("3dROIstats -mask_f2short -mask raparc.a2009s+aseg.nii plq20005_Ax-FSPGR-BRAVO_003.nii rASL_plq20005_fmap.nii > stats.txt")
end