diff --git a/docs/scripts_md/MRI.md b/docs/scripts_md/MRI.md index 9c2f943f4..f70619067 100644 --- a/docs/scripts_md/MRI.md +++ b/docs/scripts_md/MRI.md @@ -346,6 +346,46 @@ INPUTS: - $fileref : file hash ref - $data\_dir: data directory (e.g. `/data/$PROJECT/data`) +### create\_dwi\_nifti\_bval\_file($file\_ref, $bval\_file) + +Creates the NIfTI `.bval` file required for DWI acquisitions based on the +returned value of `acquisition:bvalues`. + +INPUTS: + - $file\_ref : file hash ref + - $bval\_file: path to the `.bval` file to write into + +RETURNS: + - undef if no `acquisition:bvalues` were found (skipping the creation + of the `.bval` file since there is nothing to write into) + - 1 after the `.bval` file was created + +### create\_dwi\_nifti\_bvec\_file($file\_ref, $bvec\_file) + +Creates the NIfTI `.bvec` file required for DWI acquisitions based on the +returned value of `acquisition:direction_x`, `acquisition:direction_y` and +`acquisition:direction_z`. + +INPUTS: + - $file\_ref : file hash ref + - $bvec\_file: path to the `.bvec` file to write into + +RETURNS: + - undef if no `acquisition:direction_x`, `acquisition:direction_y` and + `acquisition:direction_z` were found (skipping the creation + of the `.bvec` file since there is nothing to write into) + - 1 after the `.bvec` file was created + +### write\_to\_file($file, $value, $mode) + +This method writes into a file `$file` values stored in `$value`. The mode +in which the file should be open with is specified in `$mode`. + +INPUTS: + - $file : output file to write into + - $value: value that needs to be written in the file + - $mode : mode with which the file should be open with (`'\`'> or `'\`\\>'>) + ### make\_minc\_pics($dbhr, $TarchiveSource, $profile, $minFileID, $debug, $verbose) Creates pics associated with MINC files. diff --git a/docs/scripts_md/create_nifti_bval_bvec.md b/docs/scripts_md/create_nifti_bval_bvec.md new file mode 100644 index 000000000..f503f3886 --- /dev/null +++ b/docs/scripts_md/create_nifti_bval_bvec.md @@ -0,0 +1,27 @@ +# NAME + +create\_nifti\_bval\_bvec.pl -- a script that creates the missing bval and bvec +files for DWI NIfTI acquisitions. + +# SYNOPSIS + +perl tools/create\_nifti\_bval\_bvec.pl `[options]` + +Available options are: + +\-profile: name of the config file in `../dicom-archive/.loris_mri` +\-verbose: be verbose + +# DESCRIPTION + +This script will create the missing NIfTI bval and bvec files for DWI +acquisitions. + +# LICENSING + +License: GPLv3 + +# AUTHORS + +LORIS community and McGill Centre for Integrative +Neuroscience diff --git a/tools/create_nifti_bval_bvec.pl b/tools/create_nifti_bval_bvec.pl new file mode 100644 index 000000000..f100570a9 --- /dev/null +++ b/tools/create_nifti_bval_bvec.pl @@ -0,0 +1,204 @@ +#! /usr/bin/perl + +=pod + +=head1 NAME + +create_nifti_bval_bvec.pl -- a script that creates the missing bval and bvec +files for DWI NIfTI acquisitions. + + +=head1 SYNOPSIS + +perl tools/create_nifti_bval_bvec.pl C<[options]> + +Available options are: + +-profile: name of the config file in C<../dicom-archive/.loris_mri> +-verbose: be verbose + + +=head1 DESCRIPTION + +This script will create the missing NIfTI bval and bvec files for DWI +acquisitions. + + +=head1 LICENSING + +License: GPLv3 + + +=head1 AUTHORS + +LORIS community and McGill Centre for Integrative +Neuroscience + +=cut + + + +use strict; +use warnings; +use Getopt::Tabular; + +use NeuroDB::File; +use NeuroDB::MRI; +use NeuroDB::DBI; +use NeuroDB::ExitCodes; + + + + +### Set up Getopt::Tabular + +my $profile; +my $verbose = 0; +my $profile_desc = "Name of the config file in ../dicom-archive/.loris_mri"; + +my @opt_table = ( + [ "-profile", "string", 1, \$profile, $profile_desc ], + [ "-verbose", "boolean", 1, \$verbose, "Be verbose" ] +); + +my $Help = < Successfully connected to the database \n" if $verbose; + + + +## get config settings + +my $data_dir = NeuroDB::DBI::getConfigSetting(\$dbh, 'dataDirBasepath'); +my $create_nii = NeuroDB::DBI::getConfigSetting(\$dbh, 'create_nii'); + + + +## exit if create_nii is set to No + +unless ($create_nii) { + print "\nConfig option 'create_nii' set to no. bvec/bval files will not be " + . "created as NIfTI files are not created by the imaging pipeline\n\n"; + exit $NeuroDB::ExitCodes::SUCCESS; +} + + + +## grep all FileIDs for which acquisition:bvalues are set + +print "\n==> Fetching all FileIDs with acquisition:bvalues. \n" if $verbose; + +( my $query = <prepare($query); +$sth->execute('acquisition:bvalues'); + +my @file_ids = map { $_->{'FileID'} } @{ $sth->fetchall_arrayref( {} ) }; + +unless (@file_ids) { + print "\n No files were found with header 'acquisition:bvalues' so no need " + . "to create bval/bvec files \n"; + exit $NeuroDB::ExitCodes::SUCCESS; +} + + + +## loop through all FileIDs and create bvec/bval files +foreach my $file_id (@file_ids) { + + # load the file based on the FileID + my $file = NeuroDB::File->new(\$dbh); + $file->loadFile($file_id); + + # determine paths for bval/bvec files + my $minc = $file->getFileDatum('File'); + my ($bval_file, $bvec_file) = ($minc) x 2; + $bval_file =~ s/mnc$/bval/; + $bvec_file =~ s/mnc$/bvec/; + + # create complementary nifti files for DWI acquisitions + my $bval_success = NeuroDB::MRI::create_dwi_nifti_bval_file( + \$file, "$data_dir/$bval_file" + ); + my $bvec_success = NeuroDB::MRI::create_dwi_nifti_bvec_file( + \$file, "$data_dir/$bvec_file" + ); + + # check if bval/bvec created & update parameter_file table with their paths + if ($bval_success) { + print "\n==> Successfully created bval file for $minc \n"; + # update parameter_file table with bval path + $file->setParameter('check_bval_filename', $bval_file); + } + if ($bvec_success) { + print "\n==> Successfully created bvec file for $minc \n"; + # update parameter_file table with bvec path + $file->setParameter('check_bvec_filename', $bvec_file); + } + +} + + + +## disconnect from the database and exit the script with SUCCESS exit code +$dbh->disconnect(); +exit $NeuroDB::ExitCodes::SUCCESS; diff --git a/uploadNeuroDB/NeuroDB/MRI.pm b/uploadNeuroDB/NeuroDB/MRI.pm index bb2577f4b..cc6cc97ee 100755 --- a/uploadNeuroDB/NeuroDB/MRI.pm +++ b/uploadNeuroDB/NeuroDB/MRI.pm @@ -1394,19 +1394,109 @@ sub make_nii { # Get MINC filename and NIfTI filename my $file = $$fileref; my $minc = $file->getFileDatum('File'); - my $nifti = $minc; - $nifti =~ s/mnc$/nii/g; + my ($nifti, $bval_file, $bvec_file) = ($minc) x 3; + $nifti =~ s/mnc$/nii/; + $bval_file =~ s/mnc$/bval/; + $bvec_file =~ s/mnc$/bvec/; # mnc2nii command - my $m2n_cmd = "mnc2nii -nii -quiet " . - $data_dir . "/" . $minc . " " . - $data_dir . "/" . $nifti; + my $m2n_cmd = "mnc2nii -nii -quiet $data_dir/$minc $data_dir/$nifti"; system($m2n_cmd); + # create complementary nifti files for DWI acquisitions + my $bval_success = create_dwi_nifti_bval_file($fileref, "$data_dir/$bval_file"); + my $bvec_success = create_dwi_nifti_bvec_file($fileref, "$data_dir/$bvec_file"); + # update mri table (parameter_file table) $file->setParameter('check_nii_filename', $nifti); + $file->setParameter('check_bval_filename', $bval_file) if $bval_success; + $file->setParameter('check_bvec_filename', $bvec_file) if $bvec_success; +} + + +=pod + +=head3 create_dwi_nifti_bval_file($file_ref, $bval_file) + +Creates the NIfTI C<.bval> file required for DWI acquisitions based on the +returned value of C. + +INPUTS: + - $file_ref : file hash ref + - $bval_file: path to the C<.bval> file to write into + +RETURNS: + - undef if no C were found (skipping the creation + of the C<.bval> file since there is nothing to write into) + - 1 after the C<.bval> file was created + +=cut + +sub create_dwi_nifti_bval_file { + my ($file_ref, $bval_file) = @_; + + # grep bvals from the header acquisition:bvalues + my $file = $$file_ref; + my $bvals = $file->getParameter('acquisition:bvalues'); + + return undef unless $bvals; + + # clean up the bvals string + $bvals =~ s/\.\,//g; # remove all '.,' from the string + $bvals =~ s/\.$//; # remove the last trailing '.' from the string + + # print bvals into bval_file + open(FILE, '>', $bval_file) or die "Could not open file $bval_file: $!\n"; + print FILE $bvals; + close FILE; + + return -e $bval_file; } + +=pod + +=head3 create_dwi_nifti_bvec_file($file_ref, $bvec_file) + +Creates the NIfTI C<.bvec> file required for DWI acquisitions based on the +returned value of C, C and +C. + +INPUTS: + - $file_ref : file hash ref + - $bvec_file: path to the C<.bvec> file to write into + +RETURNS: + - undef if no C, C and + C were found (skipping the creation + of the C<.bvec> file since there is nothing to write into) + - 1 after the C<.bvec> file was created + +=cut + +sub create_dwi_nifti_bvec_file { + my ($file_ref, $bvec_file) = @_; + + # grep bvecs from headers acquisition:direction_x, y and z + my $file = $$file_ref; + my @bvecs = ( + $file->getParameter('acquisition:direction_x'), + $file->getParameter('acquisition:direction_y'), + $file->getParameter('acquisition:direction_z') + ); + + return undef unless ($bvecs[0] && $bvecs[1] && $bvecs[2]); + + # loop through all bvecs, clean them up and print them into the bvec file + s/^\"+|\"$//g for @bvecs; + open(OUT, '>', $bvec_file) or die "Cannot write to file $bvec_file: $!\n"; + print OUT map { "$_\n" } @bvecs; + close(OUT); + + return -e $bvec_file; +} + + =pod =head3 make_minc_pics($dbhr, $TarchiveSource, $profile, $minFileID, $debug, $verbose)