#!/usr/bin/perl -w use strict; use warnings; unless ($ARGV[0]){ print "sm_timecourse.pl version 2.0 (4/30/07) - szymon\@nmr.mgh.harvard.edu\n"; print "\n"; print "Takes screenshots of functional overlays on cortical surface at multiple offsets using TkSurfer and generates SVG images viewable/printable with Inkscape. Screenshot PNGs are saved separately and are linked (as opposed to embedded) in the SVG file. Hence, if moving SVG file, make sure to move the linked PNG files and vice versa, or relative links will be broken.\n"; print "\n"; print "Usage: sm_timecourse [options]\n"; print "\n"; print "Options:\n"; print "--offsets : offsets to display\n"; print "--hemi : lh or rh, defaults to both\n"; print "--curv : name of curvature file to load (, sulc, etc.)\n"; print "--surf : name of surface file to load (, pial, etc.)\n"; print "--group : name of group\n"; print "--analysis : name of functional analysis\n"; print "--contrast : contrast name\n"; print "--sub : name of subject on which to paint data\n"; print "--map : , minsig, iminsig, t\n"; print "--space : space in which to average (, native, tal)\n"; print "--spacedir : get data from spacedir\n"; print "--isxavg : ffx, rfx\n"; print "--fthresh : overlay threshold minimum\n"; print "--fmid : overlay threshold midpoint\n"; print "--fslope : overlay threshold slope\n"; print "--lablist : path to file with list of labels to overlay\n"; print "(each line should contain the label name followed by three integers representing label RGB values, all separated by spaces)\n"; print "--comment : optional comment to be included in the composite image\n"; print "\n"; exit; } # set defaults here: my $SUBJECTS_DIR=$ENV{SUBJECTS_DIR}; my $format='png'; my @hemi=('lh', 'rh'); my $sub='average7'; my $comment=''; my $curv='curv'; my $surf='inflated'; my $map='sig'; my $width=145; # horizontal sie size of each brain image (px) my %height=('lat',89,'med',89,'inf',52); # vertical size of corresponding brain images (px) my $rowspace=10; # spacing between rows (px) my $colspace=10; # spacing bewteen columns (px) my $sizex=990; # width of page in px (use 990 for 8.5x11) my $sizey=765; # height of page in px (use 765 for 8.5x11) my $xmin=25; # left margin width (px) my $ymin=100; # top edge of topmost brain (px) my($group,$analysis,$contrast,$space,$spacedir,$isxavg,$fthresh,$fmid,$fslope,$lablist); my(@offsets,@labels); my($i,$j); my @args=@ARGV; my $command="$0 @args"; # replace quotes on --comment string unless($command=~s/--comment (.*?) --/--comment '$1' --/){ $command=~s/--comment (.*)$/--comment '$1'/; } if ($ARGV[0]){ while($ARGV[0]){ my $flag=shift; if($flag eq '--offsets'){ @offsets=(); while (($ARGV[0]||$ARGV[0]==0)&&$ARGV[0]!~m/^--/){ $offsets[$i++]=shift; } }elsif($flag eq '--hemi'){ @hemi=(); while ($ARGV[0]&&$ARGV[0]!~m/^--/){ $hemi[$j++]=shift; } }elsif($flag eq '--surf'){ $surf=shift; }elsif($flag eq '--curv'){ $curv=shift; }elsif($flag eq '--group'){ $group=shift; }elsif($flag eq '--analysis'){ $analysis=shift; }elsif($flag eq '--contrast'){ $contrast=shift; }elsif($flag eq '--sub'){ $sub=shift; }elsif($flag eq '--map'){ $map=shift; }elsif($flag eq '--space'){ $space=shift; }elsif($flag eq '--spacedir'){ $spacedir=shift; }elsif($flag eq '--isxavg'){ $isxavg=shift; }elsif($flag eq '--fthresh'){ $fthresh=shift; }elsif($flag eq '--fmid'){ $fmid=shift; }elsif($flag eq '--fslope'){ $fslope=shift; }elsif($flag eq '--comment'){ $comment=(shift); }elsif($flag eq '--lablist'){ $lablist=(shift); }else{ print "\nInvalid Argument: $flag !\nRun without arguments for help.\n\n"; exit 1; } } } my $dir="$SUBJECTS_DIR/$group/bold/$analysis/$spacedir-$isxavg/$contrast/SVG"; my $labeldir="$SUBJECTS_DIR/$sub/label/"; unless (-d"$dir"){ system("mkdir -p $dir"); } my $unique=int(rand 10000000)+1; my $tmpf="/usr/tmp/sm_timecourse_${unique}.tcl"; my $pval=10**(-$fthresh); my $pstr=sprintf("%.4f",$pval); $pstr=~s/0\./p/; if($lablist){ open (LL,"<$lablist"); @labels=; close(LL); } foreach my $h (@hemi){ open (TCL,">$tmpf") or die; print TCL "#! /usr/bin/tclsh\n"; print TCL "set curv $h.$curv\n"; print TCL "read_binary_curv\n"; print TCL "set gaLinkedVars(curvflag) 1\n"; print TCL "SendLinkedVarGroup view\n"; print TCL "set gaLinkedVars(forcegraycurvatureflag) 1\n"; print TCL "SendLinkedVarGroup curvature\n"; print TCL "set gaLinkedVars(fthresh) $fthresh\n"; print TCL "set gaLinkedVars(fmid) $fmid\n"; print TCL "set gaLinkedVars(fslope) $fslope\n"; print TCL "SendLinkedVarGroup overlay\n"; my $l=0; foreach my $lab (@labels){ chomp $lab; my @curlab=split(" ",$lab); if($curlab[0]=~m/.*-$h.label/){ print TCL "labl_load $labeldir/${curlab[0]}\n"; print TCL "labl_set_color $l ${curlab[1]} ${curlab[2]} ${curlab[3]}\n"; print TCL "set gaLinkedVars(labelstyle) 1\n"; print TCL "set gaLinkedVars(labels_before_overlay_flag) 1\n"; print TCL "SendLinkedVarGroup label\n"; $l++; } } foreach my $off (@offsets){ print TCL "labl_select -1\n"; print "$SUBJECTS_DIR/$group/bold/$analysis/$spacedir-$isxavg/$contrast/$map-$off-$sub-$h.w\n"; print TCL "set val $SUBJECTS_DIR/$group/bold/$analysis/$spacedir-$isxavg/$contrast/$map-$off-$sub-$h.w\n"; print TCL "sclv_read_binary_values 0\n"; print TCL "make_lateral_view\n"; print TCL "redraw\n"; print TCL "save_tiff $dir/${pstr}_${off}_lat-${h}.tif\n"; print TCL "make_lateral_view\n"; print TCL "rotate_brain_y 180\n"; print TCL "redraw\n"; print TCL "save_tiff $dir/${pstr}_${off}_med-${h}.tif\n"; print TCL "make_lateral_view\n"; print TCL "rotate_brain_x 90\n"; print TCL "redraw\n"; print TCL "save_tiff $dir/${pstr}_${off}_inf-${h}.tif\n"; } print TCL "exit\n"; close (TCL); system("tksurfer average7 $h $surf -tcl $tmpf"); foreach my $off (@offsets){ foreach my $v ('lat','med','inf'){ system("/usr/bin/mogrify -fill \"#fff\" -opaque \"#000\" -trim $dir/${pstr}_${off}_${v}-${h}.tif"); system("/usr/bin/convert $dir/${pstr}_${off}_${v}-${h}.tif $dir/${pstr}_${off}_${v}-${h}.png"); system("rm $dir/${pstr}_${off}_${v}-${h}.tif"); } } } my $hy=$ymin-50; # vertical position of header (px) my $ymax=$sizey-$hy; # maximum y position allowed my $xmax=$sizex-$xmin; # maximum x position before going onto next page my $txmin=$xmin+$width/2; # horizontal position of timepoint (offset) labels relative to brains my $tymin=$ymin-5; # vertical position of timepoint (offset) labels relative to brains my $tx=$txmin; # initialize x-position my $x=$xmin; # initialize y-position my $pp=1; foreach my $off (@offsets){ my $delay=2*$off-4; my $y=$ymin; my $ty=$tymin; if($x==$xmin){ my $svgf="$dir/${group}_${contrast}_${pstr}_$pp.svg"; open (SVG,">$svgf") or die; print SVG "\n"; print SVG "\n"; print SVG "\n"; print SVG "\n"; print SVG "\n"; print SVG "\n\n"; my $hy=$ymin-50; print SVG "\n"; print SVG "Analysis: $analysis Group: $group Contrast: $contrast p=%.4f ($fthresh;$fmid;$fslope) $comment\n",$pval; print SVG "\n"; print SVG "page $pp\n"; } foreach my $h (@hemi){ print SVG "\n"; print SVG "$delay s (off $off)\n"; foreach my $v ('lat','med','inf'){ print SVG "\n"; $y=$y+$height{$v}+$colspace; } $y=$y+$colspace*3; $ty=$y-5; } $y=$ymin; $x=$x+$width+$colspace; $tx=$tx+$width+$colspace; if($x+$width>$xmax || $off==$offsets[$#offsets]){ my $fheight=50; my $fy=$ymax-$fheight; my $fwidth=$sizex-2*$xmin; print SVG "\n"; print SVG "$command\n"; print SVG "\n"; close(SVG); $x=$xmin; $y=$ymin; $ty=$tymin; $tx=$txmin; $pp++; } }