#!/usr/bin/perl use strict; use warnings; use NEURO qw(load_study); use File::Find::Rule; use Data::Dump qw(dump); my $study = "bioface"; my %std = load_study($study); #unless ($itype) {$itype='t1';} #my %dtype = ("t1" => "mri", ); my $src_dir = '/nas/bioface_raw'; #my $raw_dir = $std{'DATA'}.'/raw'; my $conv_file = $std{'DATA'}.'/converted_dti.csv'; my $ids_file = $std{'DATA'}.'/ids.csv'; my $names_file =$std{'DATA'}.'/ni_names.csv'; my $proj_file = $std{'DATA'}.'/bioface.csv'; my @other_exts = ("nii.gz","bval","bvec","json"); my @b0_exts = ("nii.gz","json"); my $tag = "DTIep2d_diff_mddw_48dir_p3_AP"; my $b0_tag = "DTIep2d_diff_mddw_4b0_PA"; #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){ if(exists($idsinfo{$guy})){ #DTIep2d_diff_mddw_48dir_p3_AP my $order = 'dcm2niix -z i -b y -o '.$std{'DATA'}.'/tmp/ '.$src_dir.'/'.$guy; print "$order\n"; system($order); my @conv_files = find(file => 'name' => "*$tag*.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/; if (-e $file_name) { $order = sprintf "mv \"%s\" %s/dti/bfa%04ds0020.%s", $file_name, $std{'DATA'}, $idsinfo{$guy}, $ext; print "$order\n"; system($order); } } } } print "Getting P>>A B0\n"; #DTIep2d_diff_mddw_4b0_PA @conv_files = find(file => 'name' => "*$b0_tag*.nii.gz", in => $std{'DATA'}.'/tmp/'); foreach my $nii_file (@conv_files){ foreach my $ext (@b0_exts){ (my $file_name = $nii_file) =~ s/nii.gz$/$ext/; $order = sprintf "mv \"%s\" %s/dti/bfa%04d_p2a_b0.%s", $file_name, $std{'DATA'}, $idsinfo{$guy}, $ext; 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;