-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added creation of bvec and bval files when creating NIfTI files #335
Changes from 3 commits
27141d8
7cce1e5
c5d2e41
a926eb8
f849c42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <loris.info@mcin.ca> and McGill Centre for Integrative | ||
Neuroscience |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
#! /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 <loris.info@mcin.ca> 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 = <<HELP; | ||
****************************************************************************** | ||
CREATE NIFTI BVAL BVEC DWI FILES | ||
****************************************************************************** | ||
|
||
This will check if the configuration flag for NIfTI files creation is set to | ||
'yes' and will create .bvec and .bval files for the DWI acquisitions that | ||
need to accompany the NIfTI DWI files (bug in mnc2nii that do not create | ||
those files so we create them ourselves based on values present in the MINC | ||
header for acquisition:bvalues, acquisition:direction_x, | ||
acquisition:direction_y and acquisition:direction_z). | ||
|
||
Documentation: perldoc create_nifti_bval_bvec.pl | ||
|
||
HELP | ||
|
||
my $Usage = <<USAGE; | ||
Usage: $0 [options] | ||
$0 -help to list options | ||
USAGE | ||
|
||
&Getopt::Tabular::SetHelp($Help, $Usage); | ||
&Getopt::Tabular::GetOptions(\@opt_table, \@ARGV) | ||
|| exit $NeuroDB::ExitCodes::GETOPT_FAILURE; | ||
|
||
|
||
|
||
## input error checking | ||
|
||
if (!$ENV{LORIS_CONFIG}) { | ||
print STDERR "\n\tERROR: Environment variable 'LORIS_CONFIG' not set\n\n"; | ||
exit $NeuroDB::ExitCodes::INVALID_ENVIRONMENT_VAR; | ||
} | ||
|
||
if (!defined $profile || !-e "$ENV{LORIS_CONFIG}/.loris_mri/$profile") { | ||
print $Help; | ||
print STDERR "$Usage\n\tERROR: You must specify a valid and existing profile.\n\n"; | ||
exit $NeuroDB::ExitCodes::PROFILE_FAILURE; | ||
} | ||
|
||
{ package Settings; do "$ENV{LORIS_CONFIG}/.loris_mri/$profile" } | ||
|
||
if ( !@Settings::db ) { | ||
print STDERR "\n\tERROR: You don't have a \@db setting in the file " | ||
. "$ENV{LORIS_CONFIG}/.loris_mri/$profile \n\n"; | ||
exit $NeuroDB::ExitCodes::DB_SETTINGS_FAILURE; | ||
} | ||
|
||
|
||
|
||
## establish database connection | ||
|
||
my $dbh = &NeuroDB::DBI::connect_to_db(@Settings::db); | ||
print "\n==> 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 = <<QUERY ) =~ s/\n/ /g; | ||
SELECT | ||
FileID | ||
FROM | ||
parameter_file | ||
JOIN parameter_type USING (ParameterTypeID) | ||
WHERE | ||
parameter_type.Name=? | ||
QUERY | ||
|
||
my $sth = $dbh->prepare($query); | ||
$sth->execute('acquisition:bvalues'); | ||
|
||
my @file_ids; | ||
while( my $fileID = $sth->fetchrow_array) { | ||
push @file_ids, $fileID; | ||
} | ||
|
||
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) = map { $minc }(0 .. 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you are creating an array of identical elements, use the repetiton ( my ($bval_file, $bvec_file) = ($minc) x 2; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A lot simpler. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you are matching the end of the file name, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right. Removed the |
||
$bval_file =~ s/mnc$/bval/g; | ||
$bvec_file =~ s/mnc$/bvec/g; | ||
|
||
# create complementary nifti files for DWI acquisitions | ||
my $bval_success = NeuroDB::MRI::create_dwi_nifti_bval_file( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done! |
||
\$file, $data_dir . '/' . $bval_file | ||
); | ||
my $bvec_success = NeuroDB::MRI::create_dwi_nifti_bvec_file( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
\$file, $data_dir . '/' . $bvec_file | ||
); | ||
if ($bval_success && $bvec_success) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is is possible that one file creation succeeds and the other one fails? Should the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. separated them and moved the update to |
||
print "\n==> Succesfully created bval/bvec files for $minc \n"; | ||
} | ||
|
||
# update parameter_file table with bvec/bval paths | ||
$file->setParameter('check_bval_filename', $bval_file) if $bval_success; | ||
$file->setParameter('check_bvec_filename', $bvec_file) if $bvec_success; | ||
} | ||
|
||
|
||
|
||
## disconnect from the database and exit the script with SUCCESS exit code | ||
$dbh->disconnect(); | ||
exit $NeuroDB::ExitCodes::SUCCESS; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...but if you think it is too cryptic, leave the current code as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. Thanks for the suggestion!