#!/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 NEURO qw(populate check_subj load_study print_help get_pair check_or_make); my $study; my $cfile; my $debug=1; my $corr=1; my $legacy=0; my $old=0; my $chop=0; @ARGV = ("-h") unless @ARGV; while (@ARGV and $ARGV[0] =~ /^-/) { $_ = shift; last if /^--$/; if (/^-cut/) { $cfile = shift; chomp($cfile);} if (/^-nocorr/) {$corr=0;} if (/^-legacy/) {$legacy=1;} if (/^-old/) {$old=1;} if (/^-chop/) {$old=1; $chop=1;} if (/^-h/) { print_help $ENV{'PIPEDIR'}.'/doc/dti_reg.hlp'; exit;} } $study = shift; unless ($study) { print_help $ENV{'PIPEDIR'}.'/doc/dti_reg.hlp'; exit;} my %std = load_study($study); my $subj_dir = $ENV{'SUBJECTS_DIR'}; my $pipe_dir = $ENV{'PIPEDIR'}; #my $pipe_dir = "/nas/software/neuro.dev/bin/"; # Redirect ouput to logfile (do it only when everything is fine) my $logfile = "$std{'DATA'}/.debug_dti_register.log"; open STDOUT, ">$logfile" or die "Can't redirect stdout"; open STDERR, ">&STDOUT" or die "Can't dup stdout"; $debug ? open DBG, ">$logfile" :0; #Run this script ONLY on "Detritus" #or change the paths to the correct ones my $w_dir=$std{'WORKING'}; my $dti_dir=$std{'DTI'}; my $mri_dir=$std{'MRI'}; my $db = $std{'DATABASE'}; my $data_dir=$std{'DATA'}; my $outdir = "$std{'DATA'}/slurm"; check_or_make($outdir); my %plist = populate($db); my @ok_dtis; print "Collecting needed files\n"; my %dtis; if ($cfile){ my %cuts = get_pair($data_dir."/".$cfile); foreach my $cut (keys %cuts){ if(grep {/$cut/} %plist){ $dtis{$cut} = $plist{$cut}; } } }else{ %dtis = %plist; } my $count = 0; my @jobs_list; foreach my $subject (sort keys %dtis){ if($subject){ my @names = find(file => 'name' => "$dtis{$subject}$subject*.nii.gz", in => $dti_dir); my @t1_names = find(file => 'name' => "$dtis{$subject}$subject*.nii.gz", in => $mri_dir); if(@names && @t1_names){ my $order; unless($old){ if($corr){ if($legacy){ $order = $pipe_dir."/bin/dti_proc_legacy.sh ".$names[0]." ".$t1_names[0]." ".$dtis{$subject}.$subject." ".$w_dir; }else{ $order = $pipe_dir."/bin/dti_proc_epi.sh ".$study." ".$names[0]." ".$t1_names[0]." ".$dtis{$subject}.$subject." ".$w_dir; } }else{ if($legacy){ $order = $pipe_dir."/bin/dti_proc_nocorr_legacy.sh ".$names[0]." ".$t1_names[0]." ".$dtis{$subject}.$subject." ".$w_dir; }else{ $order = $pipe_dir."/bin/dti_proc_nocorr.sh ".$names[0]." ".$t1_names[0]." ".$dtis{$subject}.$subject." ".$w_dir; } } }else{ if($chop){ $order = $pipe_dir."/bin/dti_proc_x.sh ".$names[0]." ".$t1_names[0]." ".$dtis{$subject}.$subject." ".$w_dir; }else{ $order = $pipe_dir."/bin/dti_proc_deprecated.sh ".$names[0]." ".$dtis{$subject}.$subject." ".$w_dir; } } my $orderfile = $outdir.'/'.$subject.'dti_orders.sh'; open ORD, ">$orderfile"; print ORD '#!/bin/bash'."\n"; print ORD '#SBATCH -J dti_reg-'.$study."\n"; print ORD '#SBATCH --time=15:0:0'."\n"; #si no ha terminado en X horas matalo print ORD '#SBATCH -c 4'."\n"; print ORD '#SBATCH --mail-type=FAIL,STAGE_OUT'."\n"; #no quieres que te mande email de todo print ORD '#SBATCH -o '.$outdir.'/dti_reg-slurm-%j'."\n"; unless($legacy or $old){ print ORD '#SBATCH --gres=gpu:1'."\n"; } print ORD '#SBATCH --mail-user='."$ENV{'USER'}\n"; unless($legacy or $old){ print ORD '#SBATCH -p cuda'."\n"; } print ORD "srun $order\n"; close ORD; my $jobid = `sbatch $orderfile`; $jobid = ( split ' ', $jobid )[ -1 ]; push @jobs_list, $jobid; $debug ? print DBG "$order\n" :0; } } } $debug ? close DBG:0; my $sjobs = join(',',@jobs_list); my $orderfile = $outdir.'/dti_reg_end.sh'; open ORD, ">$orderfile"; print ORD '#!/bin/bash'."\n"; print ORD '#SBATCH -J dti_reg-'.$study."\n"; print ORD '#SBATCH --mail-type=END'."\n"; #email cuando termine print ORD '#SBATCH --mail-user='."$ENV{'USER'}\n"; print ORD '#SBATCH -o '.$outdir.'/dti_reg_end-%j'."\n"; print ORD ":\n"; close ORD; my $order = 'sbatch --depend=afterany:'.$sjobs.' '.$orderfile; exec($order);