Extracting CLUT from .annot Files Using R: Difference between revisions

From CCN Wiki
Jump to navigation Jump to search
 
Line 35: Line 35:
  2      "G_and_S_occipital_inf" 23      60      180    0
  2      "G_and_S_occipital_inf" 23      60      180    0


So after you generate your CLUT.txt file, you might want to use <code>sed</code> to strip the quotation marks.
So after you generate your CLUT.txt file, you might want to use <code>sed</code> to strip the quotation marks:
sed -i .bak 's/\"//g' lh.a2009s-CLUT.txt
Note: the '''-i .bak''' flag tells sed to run the command in-place (i.e., overwriting the original), but saving a back-up with the .bak extension. This is the MacOS syntax. I believe linux syntax differs, so you can run <code>man sed</code> to see what options are available on your OS.

Latest revision as of 11:12, 31 March 2020

freesurferformats

There are at least two R packages written to work with FreeSurfer files. The freesurferformats package has utilities to pull the CLUT from a .annot file

Installing freesurferformats

In R:

install.packages('freesurferformats', dependencies=TRUE)

Reading .annot files

With the package installed, you can now read .annot file information:

library('freesurferformats')
annotfile='lh.aparc.annot'
annot=read.fs.annot(annotfile)
colortable=colortable.from.annot(annot)

Scripting

You can run an R script from the terminal using the Rscript shell interpreter by beginning your script with the following:

#!/usr/local/bin/Rscript

Write the rest of your script using normal R syntax. Save it with a .R extension and make it executable and you can run it from the terminal. You can even pass parameters:

#!/usr/local/bin/Rscript
args = commandArgs(trailingOnly=TRUE)
print(args[1]) #this prints the first argument

clutteR.R

I learned all this this morning, and wrote an R script to extract the CLUT from a .annot file. The script, clutteR.R can be found in the /ubfs/Scripts/R folder. Run it as follows:

./clutteR.R annot-file [outfile]

For example:

./clutteR.R lh.aparc.a2009s.annot lh.a2009s-CLUT.txt

If an outfile is not specified, the outputfile will be the name of the input file with -CLUT.txt appended to it.

My one complaint with this script is that R writes strings with quotation marks:

"#No."  "Label Name"    "R"     "G"     "B"     "A"
0       "Unknown"       0       0       0       0
1       "G_and_S_frontomargin"  23      220     60      0 
2       "G_and_S_occipital_inf" 23      60      180     0

So after you generate your CLUT.txt file, you might want to use sed to strip the quotation marks:

sed -i .bak 's/\"//g' lh.a2009s-CLUT.txt

Note: the -i .bak flag tells sed to run the command in-place (i.e., overwriting the original), but saving a back-up with the .bak extension. This is the MacOS syntax. I believe linux syntax differs, so you can run man sed to see what options are available on your OS.