#!/usr/bin/perl use strict; use warnings; use NEURO qw(load_study); use File::Find::Rule; use Data::Dump qw(dump); my $study = "mopead"; my %std = load_study($study); my $src_dir = '/nas/corachan/MOPEAD/'; #my $raw_dir = $std{'DATA'}.'/raw'; my $conv_file = $std{'DATA'}.'/converted_fmri.csv'; my $ids_file = $std{'DATA'}.'/ids.csv'; my $names_file =$std{'DATA'}.'/ni_names.csv'; my $proj_file = $std{'DATA'}.'/mopead.csv'; my @other_exts = ("nii.gz","json"); #Leo la DB my %idsinfo; open IDS, "<$ids_file" or die $!; while(){ chomp; my ($key, $value) = split(/,\s?/); $idsinfo{$key} = $value; } close IDS; #Leo los que ya he descomprimido my @orig_str = qx/ls $src_dir/; chomp @orig_str; #Leo los que se han convertido my @conv_str; open CONV, "<$conv_file" or die $!; chomp (@conv_str = ); close CONV; #Y a ver cuantos faltan por convertir! my %conv_str = map { $_ => 1 } @conv_str; my @not_conv = grep !$conv_str{$_}, @orig_str; # y a convertirlos! foreach my $guy (@not_conv){ my $order = 'for x in `find '.$src_dir.'/'.$guy.' -type f`; do if [[ `dckey -k "SeriesDescription" $x 2>&1 | grep ep2d_bold_p2_resting_state` ]]; then dirname $x; fi; done | uniq > '.$std{'DATA'}.'/tmp/tmp_list.dirs; for x in `cat '.$std{'DATA'}.'/tmp/tmp_list.dirs`; do dcm2niix -z y -o '.$std{'DATA'}.'/tmp $x; done; rm '.$std{'DATA'}.'/tmp/tmp_list.dirs'; print "$order\n"; system($order); my @conv_files = find(file => 'name' => "*.nii.gz", in => $std{'DATA'}.'/tmp/'); #dump @conv_files; foreach my $nii_file (@conv_files){ $order = 'fslinfo '.$nii_file; my %xinfo; foreach (qx/$order/){ my ( $key, $value ) = /(\S+)\s+(\S+)\s*.*/; $xinfo{$key} =$value; } if($xinfo{"dim4"}>1){ print "Choosing and moving files\n"; my $file_name = $nii_file; foreach my $ext (@other_exts){ ($file_name = $nii_file) =~ s/nii.gz$/$ext/; $order = sprintf "mv %s %s/fmri/mop%04ds0012.%s", $file_name, $std{'DATA'}, $idsinfo{$guy}, $ext; print "$order\n"; system($order); } } } print "Cleaning house\n"; $order = "rm $std{'DATA'}/tmp/*"; system($order); #Convierto las imagenes para corregir (A>>P y P>>A) print "Getting A>>P\n"; $order = 'for x in `find '.$src_dir.'/'.$guy.' -type f`; do if [[ `dckey -k "SeriesDescription" $x 2>&1 | grep ep2d_fid_basic_bold_p2_AP` ]]; then dcm2niix -z y -o '.$std{'DATA'}.'/tmp $x; fi;done'; system($order); $order = sprintf "mv %s/tmp/*.nii.gz %s/fmri/mop%04d_a2p.nii.gz", $std{'DATA'}, $std{'DATA'}, $idsinfo{$guy}; print "$order\n"; system($order); print "Getting P>>A\n"; $order = 'for x in `find '.$src_dir.'/'.$guy.' -type f`; do if [[ `dckey -k "SeriesDescription" $x 2>&1 | grep ep2d_fid_basic_bold_p2_PA` ]]; then dcm2niix -z y -o '.$std{'DATA'}.'/tmp $x; fi;done'; system($order); $order = sprintf "mv %s/tmp/*.nii.gz %s/fmri/mop%04d_p2a.nii.gz", $std{'DATA'}, $std{'DATA'}, $idsinfo{$guy}; print "$order\n"; system($order); print "Cleaning house\n"; $order = "rm $std{'DATA'}/tmp/*"; system($order); } #actualizo los convertidos push(@conv_str, @not_conv); open CONV, ">$conv_file" or die $!; print CONV "$_\n" foreach @conv_str; close CONV;