Skip to content
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

Refactor remaining array configs to use ConfigOB instead of the DBI library #770

Merged
merged 11 commits into from
Feb 17, 2023
16 changes: 2 additions & 14 deletions docs/scripts_md/MRIProcessingUtility.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,15 @@ INPUTS:

RETURNS: acquisition protocol ID of the MINC file

### dicom\_to\_minc($study\_dir, $converter, $get\_dicom\_info, $exclude, $mail\_user, $upload\_id)
### dicom\_to\_minc($study\_dir, $converter, $get\_dicom\_info, @exclude, $mail\_user, $upload\_id)

Converts a DICOM study into MINC files.

INPUTS:
- $study\_dir : DICOM study directory to convert
- $converter : converter to be used
- $get\_dicom\_info : get DICOM information setting from the `Config` table
- $exclude : which files to exclude from the `dcm2mnc` command
- @exclude : which files to exclude from the `dcm2mnc` command
- $mail\_user : mail of the user
- $upload\_id : upload ID of the study

Expand Down Expand Up @@ -505,15 +505,3 @@ License: GPLv3

LORIS community <loris.info@mcin.ca> and McGill Centre for Integrative
Neuroscience

# POD ERRORS

Hey! **The above document had some coding errors, which are explained below:**

- Around line 567:

&#x3d;cut found outside a pod block. Skipping to next block.

- Around line 1880:

&#x3d;cut found outside a pod block. Skipping to next block.
4 changes: 2 additions & 2 deletions tools/delete_imaging_upload.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2002,8 +2002,8 @@ sub getScanTypesToDelete {
return () if !defined $scanTypeList && !$keepDefaced;

if($keepDefaced) {
my $scanTypesRef = &NeuroDB::DBI::getConfigSetting(\$dbh, 'modalities_to_deface');
return defined $scanTypesRef ? (map { $_ => 1 } @$scanTypesRef) : ();
my @scanTypes = $configOB->getModalitiesToDeface();
return (map { $_ => 1 } @scanTypes);
}

my %types = map { $_=> 1 } split(/,/, $scanTypeList);
Expand Down
37 changes: 23 additions & 14 deletions tools/minc_to_bids_converter.pl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ =head2 METHODS
use NeuroDB::MRI;
use NeuroDB::ExitCodes;
use NeuroDB::File;
use NeuroDB::objectBroker::ConfigOB;


# # Set script's constants here
Expand Down Expand Up @@ -203,15 +204,25 @@ =head2 METHODS
my $dbh = &NeuroDB::DBI::connect_to_db(@Settings::db);
print "\n==> Successfully connected to database \n";

# Get settings from the ConfigSettings table
my $data_dir = &NeuroDB::DBI::getConfigSetting(\$dbh,'dataDirBasepath');
my $bin_dir = &NeuroDB::DBI::getConfigSetting(\$dbh,'MRICodePath');
my $authors = &NeuroDB::DBI::getConfigSetting(\$dbh, 'bids_dataset_authors');
my $acknowledgments = &NeuroDB::DBI::getConfigSetting(\$dbh, 'bids_acknowledgments_text');
my $readme_content = &NeuroDB::DBI::getConfigSetting(\$dbh, 'bids_readme_text');
my $validator_ignore_opts = &NeuroDB::DBI::getConfigSetting(\$dbh, 'bids_validator_options_to_ignore');

unless (defined $authors && defined $acknowledgments && defined $readme_content) {
# new Moose database connection
my $db = NeuroDB::Database->new(
databaseName => $Settings::db[0],
userName => $Settings::db[1],
password => $Settings::db[2],
hostName => $Settings::db[3]
);
$db->connect();

# Get settings from the Config table
my $configOB = NeuroDB::objectBroker::ConfigOB->new(db => $db);
my $data_dir = $configOB->getDataDirPath();
my $bin_dir = $configOB->getMriCodePath();
my @authors = $configOB->getBidsDatasetAuthors();
my $acknowledgments = $configOB->getBidsAcknowledgmentsText();
my $readme_content = $configOB->getBidsReadmeText();
my @validator_ignore_opts = $configOB->getBidsValidatorOptionsToIgnore();

unless (@authors && defined $acknowledgments && defined $readme_content) {
print STDERR "\n ERROR: Some 'MINC to BIDS Converter Tool Options' are not set in the configuration module."
. " 'BIDS Dataset Authors', 'BIDS Dataset Acknowledgments' and 'BIDS Dataset README' need to be"
. " defined.\n\n";
Expand Down Expand Up @@ -259,7 +270,7 @@ =head2 METHODS
'BIDSVersion' => $BIDS_VERSION,
'Name' => $dataset_name,
'LORISScriptVersion' => $LORIS_SCRIPT_VERSION,
'Authors' => $authors,
'Authors' => @authors,
'HowToAcknowledge' => $acknowledgments,
'LORISReleaseVersion' => $loris_mri_version
);
Expand Down Expand Up @@ -290,11 +301,9 @@ =head2 METHODS
# specifications, not the same number of files per session etc...)
# =============================================================================
my $bids_validator_config_file = $dest_dir . "/.bids-validator-config.json";
if (!-e $bids_validator_config_file && defined $validator_ignore_opts) {
if (!-e $bids_validator_config_file && @validator_ignore_opts) {
print "\n******* Creating the .bids-validator-config.json file $bids_validator_config_file *******\n";
my $validator_ignore_string = ref($validator_ignore_opts) eq 'ARRAY'
? join(", ", @$validator_ignore_opts)
: $validator_ignore_opts;
my $validator_ignore_string = join(", ", @validator_ignore_opts);
my $bids_validator_config_content = <<TEXT;
{
"ignore": [$validator_ignore_string]
Expand Down
32 changes: 16 additions & 16 deletions tools/run_defacing_script.pl
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ =head1 METHODS

my $data_dir = $configOB->getDataDirPath();
my $ref_scan_type = $configOB->getDefacingRefScanType();
my @to_deface = $configOB->getModalitiesToDeface();


# -----------------------------------------------------------------
## Get config setting using the old database calls
# -----------------------------------------------------------------

my $to_deface = &NeuroDB::DBI::getConfigSetting(\$dbh, 'modalities_to_deface');
unless ($ref_scan_type && $to_deface) {
unless ($ref_scan_type && @to_deface) {
print STDERR "\n==> ERROR: you need to configure both the "
. "reference_scan_type_for_defacing & modalities_to_deface config "
. "settings in the imaging pipeline section of the Config module.\n"
Expand Down Expand Up @@ -235,14 +235,14 @@ =head1 METHODS
print "\n==> Fetching all FileIDs to deface.\n" if $verbose;
my @session_ids = defined $session_ids ? split(",", $session_ids) : ();

unless ($to_deface) {
unless (@to_deface) {
print "\nNo modalities were set to be defaced in the Config module. Ensure"
. " to select modalities to deface in the Config module under the imaging"
. " pipeline section (setting called modalities_to_deface. \n\n";
exit $NeuroDB::ExitCodes::SUCCESS;
}

my %files_hash = grep_FileIDs_to_deface(\@session_ids, $to_deface);
my %files_hash = grep_FileIDs_to_deface(\@session_ids, @to_deface);



Expand Down Expand Up @@ -293,15 +293,15 @@ =head1 METHODS

=pod

=head3 grep_FileIDs_to_deface($session_id_arr, $modalities_to_deface_arr)
=head3 grep_FileIDs_to_deface($session_id_arr, @modalities_to_deface_arr)
cmadjar marked this conversation as resolved.
Show resolved Hide resolved

Queries the database for the list of acquisitions' FileID to be used to run the
defacing algorithm based on the provided list of SessionID and Scan_type to
restrict the search.

INPUTS:
- $session_id_arr : array of SessionIDs to use when grepping FileIDs
- $modalities_to_deface_arr: array of Scan_type to use when grepping FileIDs
- @modalities_to_deface_arr: array of Scan_type to use when grepping FileIDs
cmadjar marked this conversation as resolved.
Show resolved Hide resolved

RETURNS: hash of matching FileIDs to be used to run the defacing algorithm
organized in a hash as follows:
Expand All @@ -318,18 +318,18 @@ =head3 grep_FileIDs_to_deface($session_id_arr, $modalities_to_deface_arr)
=cut

sub grep_FileIDs_to_deface {
my ($session_id_arr, $modalities_to_deface_arr) = @_;
my ($session_id_arr, @modalities_to_deface_arr) = @_;
cmadjar marked this conversation as resolved.
Show resolved Hide resolved

# separate the special modalities specified in %SPECIAL_ACQUISITIONS from the
# standard scan types
my @special_scan_types = keys %SPECIAL_ACQUISITIONS_FILTER;
my @special_cases;
foreach my $special (@special_scan_types) {
# push the special modalities to a new array @special_cases
push @special_cases, grep(/$special/, @$modalities_to_deface_arr);
push @special_cases, grep(/$special/, @modalities_to_deface_arr);
# remove the special modalities from the modalities array as they will be
# dealt with differently than standard modalities
@$modalities_to_deface_arr = grep(! /$special/, @$modalities_to_deface_arr);
@modalities_to_deface_arr = grep(! /$special/, @modalities_to_deface_arr);
}

# base query
Expand All @@ -342,8 +342,8 @@ sub grep_FileIDs_to_deface {

# add where clause for the different standard scan types to deface
my @where;
if (@$modalities_to_deface_arr) {
@where = map { "mst.Scan_type = ?" } @$modalities_to_deface_arr;
if (@modalities_to_deface_arr) {
@where = map { "mst.Scan_type = ?" } @modalities_to_deface_arr;
$query .= sprintf(" %s ", join(" OR ", @where));
}

Expand All @@ -366,7 +366,7 @@ sub grep_FileIDs_to_deface {
my $sth = $dbh->prepare($query);

# create array of parameters
my @bind_param = @$modalities_to_deface_arr;
my @bind_param = @modalities_to_deface_arr;
foreach my $special_scan_type (@special_cases) {
push @bind_param, $special_scan_type;
push @bind_param, $SPECIAL_ACQUISITIONS_FILTER{$special_scan_type};
Expand Down Expand Up @@ -434,7 +434,7 @@ =head3 check_if_deface_files_already_in_db($session_files, $session_id)
sub check_if_deface_files_already_in_db {
my ($session_files, $session_id) = @_;

my @defaced_scan_types = map { $_ . '-defaced' } keys $session_files;
my @defaced_scan_types = map { $_ . '-defaced' } keys %{ $session_files };

# base query
my $query = "SELECT COUNT(*) "
Expand Down Expand Up @@ -554,7 +554,7 @@ sub deface_session {

# add multi-constrast modalities to cmd line & remove them from $session_files
foreach my $multi (@MULTI_CONTRAST_ACQUISITIONS_BASE_NAMES) {
my @scan_types = keys $session_files;
my @scan_types = keys %{ $session_files };
my @matching_types = grep (/$multi/i, @scan_types);
my @non_matching_types = grep (!/$multi/i, @scan_types);
my (@multi_files_list, @other_files);
Expand Down Expand Up @@ -623,7 +623,7 @@ sub fetch_defaced_files {
$defaced_images{$deface_ref}{Scan_type} = $$ref_file{Scan_type};

# for each files in $session_files, append the defaced images to the hash
foreach my $scan_type (keys $session_files) {
foreach my $scan_type (keys %{ $session_files }) {
my %files = %{ $$session_files{$scan_type} };
foreach my $fileID (keys %files) {
my $deface_file = $deface_dir . '/' . basename($files{$fileID});
Expand Down Expand Up @@ -663,7 +663,7 @@ sub register_defaced_files {
. " -coordinateSpace native "
. " -outputType defaced ";

foreach my $file (keys $defaced_images) {
foreach my $file (keys %{ $defaced_images }) {
my $input_fileID = $$defaced_images{$file}{InputFileID};
my $scan_type = $$defaced_images{$file}{Scan_type} . "-defaced";

Expand Down
41 changes: 0 additions & 41 deletions uploadNeuroDB/NeuroDB/DBI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -72,47 +72,6 @@ sub connect_to_db
return $dbh;
}

=pod

=head3 getConfigSetting($dbh, $name)

This method fetches the value (C<$value>) stored in the C<Config> table for a
specific config setting (C<$name>) specified as an input.

INPUTS:
- $dbh : database handler
- $name: name of the config setting

RETURNS: value corresponding to the config setting in the C<Config> table
of LORIS

=cut
sub getConfigSetting
{
my ($dbh, $name) = @_;
my ($message, $query, $where) = '';
my $value = undef;

$where = " WHERE c.ConfigID=(Select cs.ID from ConfigSettings cs where cs.Name=?)";
$query = " SELECT c.Value FROM Config c";
$query = $query . $where;
my $sth = $$dbh->prepare($query);
$sth->execute($name);

if ( $sth->rows > 1 ){
# if more than one row returned, push data into an array that will be
# dereferenced into $value
my @values;
while (my $row = $sth->fetchrow_array()) {
push (@values, $row);
}
$value = \@values;
} elsif ( $sth->rows > 0 ) {
$value = $sth->fetchrow_array();
}
return $value;
}

1;

=pod
Expand Down
14 changes: 4 additions & 10 deletions uploadNeuroDB/NeuroDB/MRIProcessingUtility.pm
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,6 @@ sub createMriUploadArray {
return %{ $mriUploadInfoRef->[0] };
}

=cut

=pod

Expand Down Expand Up @@ -1390,23 +1389,23 @@ sub registerScanIntoDB {

=pod

=head3 dicom_to_minc($study_dir, $converter, $get_dicom_info, $exclude, $mail_user, $upload_id)
=head3 dicom_to_minc($study_dir, $converter, $get_dicom_info, @exclude, $mail_user, $upload_id)
cmadjar marked this conversation as resolved.
Show resolved Hide resolved

Converts a DICOM study into MINC files.

INPUTS:
- $study_dir : DICOM study directory to convert
- $converter : converter to be used
- $get_dicom_info : get DICOM information setting from the C<Config> table
- $exclude : which files to exclude from the C<dcm2mnc> command
- @exclude : which files to exclude from the C<dcm2mnc> command
- $mail_user : mail of the user
- $upload_id : upload ID of the study

=cut

sub dicom_to_minc {
my $this = shift;
my ($study_dir, $converter, $get_dicom_info, $exclude,$mail_user, $upload_id) = @_;
my ($study_dir, $converter, $get_dicom_info, @exclude,$mail_user, $upload_id) = @_;
my ($d2m_cmd, $d2m_log, $exit_code, $excluded_regex);
my $message = '';

Expand All @@ -1415,11 +1414,7 @@ sub dicom_to_minc {
# series description specified in the Config Setting excluded_series_description #
# If there are no series to exclude, $excluded_regex remains undef #
#--------------------------------------------------------------------------------#
if ($exclude && ref($exclude) eq 'ARRAY') {
$excluded_regex = join('|', map { quotemeta($_) } @$exclude);
} elsif ($exclude) {
$excluded_regex = $exclude;
}
$excluded_regex = join('|', map { quotemeta($_) } @exclude) if (@exclude);

#-----------------------------------------------------------------------------------#
# Run get_dicom_info on all the DICOM files and build the set of all the distinct #
Expand Down Expand Up @@ -1884,7 +1879,6 @@ sub validate_tarchive_id_against_upload_id {
}
}

=cut

=pod

Expand Down
Loading