#!/usr/bin/perl use strict; use warnings; use File::Slurp qw(read_file); use File::Find::Rule; use File::Basename qw(basename); use Data::Dump qw(dump); use File::Copy::Recursive qw(dirmove); use File::Copy qw(copy); use NEURO qw(populate check_subj load_study print_help get_pair check_or_make); my $study; my $cfile; @ARGV = ("-h") unless @ARGV; while (@ARGV and $ARGV[0] =~ /^-/) { $_ = shift; last if /^--$/; if (/^-cut/) { $cfile = shift; chomp($cfile);} if (/^-h/) { print_help $ENV{'PIPEDIR'}.'/doc/rs_ica_one.hlp'; exit;} } $study = shift; unless ($study) { print_help $ENV{'PIPEDIR'}.'/doc/rs_ica_one.hlp'; exit;} my %std = load_study($study); my $subj_dir = $ENV{'SUBJECTS_DIR'}; my $pipe_dir = $ENV{'PIPEDIR'}; my $fsl = $ENV{'FSLDIR'}; my $w_dir=$std{'WORKING'}; my $fmri_dir=$std{'fMRI'}; my $mri_dir=$std{'MRI'}; my $db = $std{'DATABASE'}; my $data_dir=$std{'DATA'}; my $lone_template_file = $pipe_dir.'/lib/fsf/notalone_ica_template.fsf'; my %template_files = ( "begin" => $pipe_dir.'/lib/fsf/group_ica_template_p1.fsf', "data" => $pipe_dir.'/lib/fsf/group_ica_template_targets.fsf', "end" => $pipe_dir.'/lib/fsf/group_ica_template_p2.fsf'); print "Collecting needed files\n"; my %fmris; my %plist = populate($db); if ($cfile){ my %cuts = get_pair($data_dir."/".$cfile); foreach my $cut (keys %cuts){ if(grep {/$cut/} %plist){ $fmris{$cut} = $plist{$cut}; } } }else{ %fmris = %plist; } my $count = 0; my %subjects; print "Counting available subjects\n"; foreach my $subject (sort keys %fmris){ my @names = find(file => 'name' => $fmris{$subject}.$subject."s*.nii.gz", in => $fmri_dir); if(@names){ my $sname = $fmris{$subject}.$subject; $subjects{$sname} = $names[0]; $count++; } } my $pollos = $count; print "Getting info from images\n"; my $test_subject = ( sort keys %subjects )[0]; my $test_path = $subjects{$test_subject}; my $test_order = $fsl.'/bin/fslinfo '.$test_path; open(my $test_info, '-|', $test_order ) or die $!; my %test_data; while (<$test_info>){ if (/^dim1\s*(\d{1,3}).*/) {$test_data{'dim1'}=$1;} if (/^dim2\s*(\d{1,3}).*/) {$test_data{'dim2'}=$1;} if (/^dim3\s*(\d{1,3}).*/) {$test_data{'dim3'}=$1;} if (/^dim4\s*(\d{1,3}).*/) {$test_data{'dim4'}=$1;} if (/^pixdim4\s*(\d{1,2}\.\d{1,6})/) {$test_data{'tr'}=$1;} } my $data_size = $test_data{'dim1'}*$test_data{'dim2'}*$test_data{'dim3'}*$test_data{'dim4'}; print "Copying FSL files and setting directories\n"; my @feats = ("feat1", "feat2"); my $gfeat = "feat4_ica"; check_or_make($w_dir.'/.files'); system("cp $fsl'/doc/fsl.css' $w_dir'/.files'"); system("cp -r $fsl'/doc/images' $w_dir'/.files/images'"); chdir($w_dir); my @jobs_list; my $gdata_dir = $w_dir.'/rs.gica'; my $output_dir = $w_dir.'/rsout_gica'; check_or_make($output_dir); check_or_make($gdata_dir); my $dsg_file = $w_dir.'/gica_design.fsf'; open DSG,">$dsg_file"; open TPF,"<$template_files{'begin'}"; print "Making global .fsf file\n"; while(){ s//\"$output_dir\"/; s//$pollos/; s//$test_data{'dim4'}/; s//$data_size/; s//$test_data{'tr'}/; print DSG; } close TPF; $count = 1; my $filtered_list = $gdata_dir.'/.filelist'; open FCK, ">$filtered_list"; foreach my $subject (sort keys %subjects){ my $idata = $w_dir.'/'.$subject.'_rs.ica'; my $fck_line = $idata.'/reg_standard/filtered_func_data'; print FCK "$fck_line\n"; open TPF,"<$template_files{'data'}"; while(){ s//$idata/; s//$count/; print DSG; } $count++; close TPF; } close FCK; open TPF,"<$template_files{'end'}"; while(){ print DSG; } close TPF; close DSG; print "Making individual .fsf files and scripts\n"; my $tranca=""; $count = 1; foreach my $subject (sort keys %subjects){ my $idata = $w_dir.'/'.$subject.'_rs'; open TPF,"<$template_files{'data'}"; while(){ s//$idata/; s//$count/; $tranca .= $_; } $count++; close TPF; } $count = 1; foreach my $subject (sort keys %subjects){ my $ioutput_dir = $w_dir.'/'.$subject.'_rsout'; check_or_make($ioutput_dir); my $idata = $w_dir.'/'.$subject.'_rs'; my $idata_dir = $idata.'.ica'; my $ilogs_dir = $idata_dir.'/logs'; my $iscripts_dir = $idata_dir.'/scripts'; check_or_make($idata_dir); check_or_make($ilogs_dir); check_or_make($iscripts_dir); my $idsg_file = $w_dir.'/'.$subject.'_rs.ica/design.fsf'; open TPF,"<$lone_template_file"; open DSG,">$idsg_file"; while(){ s//\"$ioutput_dir\"/; s//$tranca/; s//$test_data{'tr'}/; s//$test_data{'dim4'}/; s//$data_size/; s//$pollos/; print DSG; } close DSG; close TPF; system("$fsl'/bin/imcp' $subjects{$subject} $idata"); my $jobid; foreach my $feat (sort @feats){ my $template = $pipe_dir.'/lib/script_templates/'.$feat.'.template'; my $script = $iscripts_dir.'/'.$feat.'.sh'; open TPF, "<$template"; open SF, ">$script"; while(){ s//$subject/; s//$ENV{'USER'}/; s//$ioutput_dir/; s//$idsg_file/; s//$idata_dir/g; s//$count/; s//$test_data{'tr'}/; print SF; } close SF; close TPF; unless($jobid){ my $order = 'sbatch '.$script; print "$order\n"; $jobid = `$order`; $jobid = ( split ' ', $jobid )[ -1 ]; }else{ my $order = 'sbatch --depend=afterok:'.$jobid.' '.$script; print "$order\n"; $jobid = `$order`; $jobid = ( split ' ', $jobid )[ -1 ]; push @jobs_list, $jobid; } } $jobid=""; $count++; } print "Making global script\n"; my $logs_dir = $gdata_dir.'/logs'; my $scripts_dir = $gdata_dir.'/scripts'; check_or_make($logs_dir); check_or_make($scripts_dir); my $template = $pipe_dir.'/lib/script_templates/'.$gfeat.'.template'; my $script = $scripts_dir.'/'.$gfeat.'.sh'; open TPF, "<$template"; open SF, ">$script"; while(){ s//$ENV{'USER'}/; s//$output_dir/; s//$dsg_file/; s//$gdata_dir/g; print SF; } close SF; close TPF; my $sjobs = join(',',@jobs_list); my $order = 'sbatch --depend=afterok:'.$sjobs.' '.$script; print "$order\n"; exec($order);