#!/usr/bin/perl use strict; use warnings; use Data::Dump qw(dump); use File::Find::Rule; use File::Copy::Recursive qw(dirmove); use NEURO qw(populate check_subj load_study print_help get_petdata get_pair achtung shit_done check_or_make); #my $study; my $cfile; my $sok = 0; @ARGV = ("-h") unless @ARGV; while (@ARGV and $ARGV[0] =~ /^-/) { $_ = shift; last if /^--$/; #if (/^-e/) { $study = shift; chomp($study);} if (/^-cut/) { $cfile = shift; chomp($cfile);} if (/^-h/) { print_help $ENV{'PIPEDIR'}.'/doc/fbb_reg.hlp'; exit;} } my $study = shift; unless ($study) { print_help $ENV{'PIPEDIR'}.'/doc/fbb_reg.hlp'; exit;} my %std = load_study($study); my $subj_dir = $ENV{'SUBJECTS_DIR'}; #Run this script ONLY on "Detritus" #or change the paths to the correct ones my $w_dir=$std{'WORKING'}; #my $pet_dir = $spect?$std{'SPECT'}:$std{'PET-FDG'}; my $pet_dir = $std{'PET-FBB'}; #my $petnc_dir = $std{'FBB-NC'}; my $db = $std{'DATABASE'}; my $data_dir=$std{'DATA'}; my $outdir = "$std{'DATA'}/slurm"; check_or_make($outdir); my %plist = populate($db); my @ok_pets; print "Collecting needed files\n"; my %pets; if ($cfile){ my %cuts = get_pair($data_dir."/".$cfile); foreach my $cut (keys %cuts){ if(grep {/$cut/} %plist){ $pets{$cut} = $plist{$cut}; } } }else{ %pets = %plist; } my $err_file = $data_dir."/.fbb_errors.debug"; open ERRF, ">$err_file" or die "Could not open error file!"; foreach my $subject (sort keys %pets){ my @names = find(file => 'name' => "$pets{$subject}$subject*", '!name' => '*_*', 'name' => '*.nii.gz', in => $pet_dir); dump @names; if(@names){ my @mri = find(file => 'name' => "brain.mgz", in => $subj_dir."/".$study."_".$pets{$subject}.$subject."/mri"); if(@mri){ push @ok_pets, $subject; }else{ print ERRF "Subject $subject has no MRI brain extracted!!!\n"; } } } foreach my $subject (@ok_pets){ #my @mri = find(file => 'name' => $pets{$subject}.$subject."_brain.nii.gz", in => "$w_dir/"); my $orderfile = $outdir.'/'.$subject.'_get_subj.sh'; my $order = $ENV{'PIPEDIR'}."/bin/get_fs_subj.sh ".$study." ".$pets{$subject}.$subject." ".$w_dir; open ORD, ">$orderfile"; print ORD '#!/bin/bash'."\n"; print ORD '#SBATCH -J fbb_correct_'.$study."\n"; print ORD '#SBATCH --time=1:0:0'."\n"; #si no ha terminado en X horas matalo print ORD '#SBATCH --mail-type=FAIL,STAGE_OUT'."\n"; #no quieres que te mande email de todo print ORD '#SBATCH -o '.$outdir.'/fbb_get_subj-%j'."\n"; print ORD '#SBATCH --mail-user='."$ENV{'USER'}\n"; print ORD "srun $order\n"; close ORD; my $jobid = `sbatch $orderfile`; $jobid = ( split ' ', $jobid )[ -1 ]; $order = $ENV{'PIPEDIR'}."/bin/fbbtemp_reg.sh ".$study." ".$pets{$subject}.$subject." ".$pet_dir." ".$w_dir; $orderfile = $outdir.'/'.$subject.'_fbb_reg.sh'; open ORD, ">$orderfile"; print ORD '#!/bin/bash'."\n"; print ORD '#SBATCH -J fbb_correct_'.$study."\n"; print ORD '#SBATCH --time=1:0:0'."\n"; #si no ha terminado en X horas matalo print ORD '#SBATCH --mail-type=FAIL,STAGE_OUT'."\n"; #no quieres que te mande email de todo print ORD '#SBATCH -o '.$outdir.'/fbb_reg-%j'."\n"; print ORD '#SBATCH --mail-user='."$ENV{'USER'}\n"; print ORD "srun $order\n"; close ORD; my $xorder = 'sbatch --depend=afterok:'.$jobid.' '.$orderfile; system("$xorder"); } my $order = $ENV{'PIPEDIR'}."/bin/make_fbb_report.pl ".$study; my $orderfile = $outdir.'/fbb_report.sh'; open ORD, ">$orderfile"; print ORD '#!/bin/bash'."\n"; print ORD '#SBATCH -J fbb_correct_'.$study."\n"; print ORD '#SBATCH --time=2:0:0'."\n"; #si no ha terminado en X horas matalo print ORD '#SBATCH --mail-type=FAIL,END'."\n"; #email cuando termine o falle print ORD '#SBATCH --mail-user='."$ENV{'USER'}\n"; print ORD '#SBATCH -o '.$outdir.'/fbb_rep-%j'."\n"; print ORD "srun $order\n"; close ORD; my $xorder = 'sbatch --dependency=singleton'.' '.$orderfile; exec($xorder);