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

Updated rules to set the default project, cohort #809

Merged
merged 4 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 63 additions & 39 deletions dicom-archive/profileTemplate.pl
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,45 @@ sub getSubjectIDs {
# If patientName is phantom scan or test scan
# CandID is scanner DCCID (based on site alias)
# visitLabel is scan patient name
# Set createVisitLable to
# a. 1 if imaging pipeline should create the visit label (when visit label has not been created yet in the database.
# b. 0 if imaging pipeline should not create the visit label (when visit label has not been created yet in the database.
if ($patientName =~ /PHA/i or $patientName =~ /TEST/i) {

$subjectID{'CandID'} = NeuroDB::MRI::my_trim(NeuroDB::MRI::getScannerCandID($scannerID, $db));
$subjectID{'visitLabel'} = NeuroDB::MRI::my_trim($patientName);
$subjectID{'isPhantom'} = 1;

$subjectID{'createVisitLabel'} = 1;

# When createVisitLabel is set to 1, CohortID must also
# be set to the ID of the cohort that the newly created
# visit should have. Assuming for example that all patient
# names end with "_<myCohortID>", then we could write:
# ($subjectID{'CohortID'}) = $patientName =~ /_(\d+)$/;
# When createVisitLabel is set to 0, $subjectID{'CohortID'} is ignored.

# If config setting 'createVisitLabel' is true
# then $subjectID{'ProjectID'} must be set to the project ID of the
# newly created visit. Assuming for example that all patients
# names that contain the string 'HOSPITAL' are associated to visit
# Override createVisitLabel here if its value needs to be different than the createVisit DB config
# $subjectID{'createVisitLabel'} = undef;
# Set createVisitLabel to
# a. 1 if imaging pipeline should create the visit label (when visit label has not been created yet in the database.
# b. 0 if imaging pipeline should not create the visit label (when visit label has not been created yet in the database.
#
# When $subjectID{'createVisitLabel'} is set to 1:
# - $subjectID{'ProjectID'} must be set to the project ID of the newly created visit
# - $subjectID{'CohortID'} must be set to the cohort ID of the newly created visit
#
# If $subjectID{'ProjectID'} is undef, the following rules will be tried in order to assign a default value:
# - the ProjectID from the session table, if the PSCID and visit labels exist
# - the ProjectID from the candidate table, if the PSCID exists
# - the default_project DB config value
#
# If $subjectID{'CohortID'} is undef, the following rules will be tried in order to assign a default value:
# - the CohortID from the session table, if the PSCID and visit labels exist
# - the default_cohort DB config value
#
# When createVisitLabel is set to 0, $subjectID{'ProjectID'} and $subjectID{'CohortID'} are ignored.

# $subjectID{'ProjectID'} = undef;
# Assuming that all patient names that contain the string 'HOSPITAL' are associated to visit
# done for project with ID 1 and all others to projects with ID 2, we
# could write:
# $subjectID{'ProjectID'} = $patientName =~ /HOSPITAL/
# ? 1 : 2;
# $subjectID{'ProjectID'} = $patientName =~ /HOSPITAL/ ? 1 : 2;
# When createVisitLabel is set to 0, $subjectID{'ProjectID'} is ignored.

# $subjectID{'CohortID'} = undef;
# Assuming that all patient names end with "_<myCohortID>", then we could write:
# $subjectID{'CohortID'} = $patientName =~ /_(\d+)$/;
# When createVisitLabel is set to 0, $subjectID{'CohortID'} is ignored.

# If patient match PSCID_DCCID_VisitLabel
# Determine PSCID, DCCID and visitLabel based on patient name
} elsif ($patientName =~ /([^_]+)_(\d+)_([^_]+)/) {
Expand All @@ -77,30 +88,43 @@ sub getSubjectIDs {
$subjectID{'visitLabel'} = NeuroDB::MRI::my_trim($3);
$subjectID{'isPhantom'} = 0;

$subjectID{'createVisitLabel'} = 0;

# When createVisitLabel is set to 1, CohortID must also
# be set to the ID of the cohort that the newly created
# visit should have. Assuming for example that visits V01 and V02
# are associated with cohort with ID 1 and all others to cohort
# with ID 2, then we could write:
# ($subjectID{'CohortID'}) = $subjectID{'visitLabel'} =~ /^V0[12]$/
# ? 1 : 2;
# When createVisitLabel is set to 0, $subjectID{'CohortID'} is ignored.

# If config setting 'createVisitLabel' is true
# then $subjectID{'ProjectID'} must be set to the project ID of the
# newly created visit. Assuming for example that candidates with a
# candidate ID greater than 400000 are seen in project 1 and others are
# seen in project 2, we could write
# could write:
# Override createVisitLabel here if its value needs to be different than the createVisit DB config
# $subjectID{'createVisitLabel'} = undef;
# Set createVisitLabel to
# a. 1 if imaging pipeline should create the visit label (when visit label has not been created yet in the database.
# b. 0 if imaging pipeline should not create the visit label (when visit label has not been created yet in the database.
#
# When $subjectID{'createVisitLabel'} is set to 1:
# - $subjectID{'ProjectID'} must be set to the project ID of the newly created visit
# - $subjectID{'CohortID'} must be set to the cohort ID of the newly created visit
#
# If $subjectID{'ProjectID'} is undef, the following rules will be tried in order to assign a default value:
# - the ProjectID from the session table, if the PSCID and visit labels exist
# - the ProjectID from the candidate table, if the PSCID exists
# - the default_project DB config value
#
# If $subjectID{'CohortID'} is undef, the following rules will be tried in order to assign a default value:
# - the CohortID from the session table, if the PSCID and visit labels exist
# - the default_cohort DB config value
#
# When createVisitLabel is set to 0, $subjectID{'ProjectID'} and $subjectID{'CohortID'} are ignored.

# $subjectID{'ProjectID'} = undef;
# Assuming for example that candidates with a candidate ID greater than 400000 are seen in project 1
# and others are seen in project 2, we could write:
# $subjectID{'ProjectID'} = $subjectID{'CandID'} > 400000 ? 1 : 2;
# ? 1 : 2;
# When createVisitLabel is set to 0, $subjectID{'ProjectID'} is ignored.

print "PSCID is: " . $subjectID{'PSCID'} .
"\n CandID id: " . $subjectID{'CandID'} .
"\n visit_label is: " . $subjectID{'visitLabel'} . "\n";
# $subjectID{'CohortID'} = undef;
# Assuming for example that visits V01 and V02 are associated with cohort with ID 1 and all others
# to cohort with ID 2, then we could write:
# $subjectID{'CohortID'} = $subjectID{'visitLabel'} =~ /^V0[12]$/ ? 1 : 2;
# When createVisitLabel is set to 0, $subjectID{'CohortID'} is ignored.

print "PSCID is: " . $subjectID{'PSCID'} .
"\n CandID id: " . $subjectID{'CandID'} .
"\n visit_label is: " . $subjectID{'visitLabel'} . "\n" .
"\n ProjectID is: " . $subjectID{'ProjectID'} . "\n";
}

# Return subjectIDs
Expand Down
22 changes: 17 additions & 5 deletions docs/02-Install.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,24 @@ Under the `Imaging Pipeline` section:
typically `/data/$PROJECT/data/tarchive/`
* `Patient identifiers and center name lookup variable`: DICOM header that
contains the LORIS candidate identifiers; typically `PatientName`
* `Upload creation of candidates`: enable or disable candidate creation into LORIS
* `Enable candidate creation`: enable or disable candidate creation into LORIS
when running the insertion pipeline
* `Enable visit creation`: enable or disable visit creation into LORIS
when running the insertion pipeline
* `Default project`: Default cohort used when Enable visit creation is set to true. This value is used when
the following rules fails:
- the $subjectID{'ProjectID'} is undef in the profileTemplate (prod) file;
- the ProjectID from the session table, if the PSCID and visit labels exist;
- the ProjectID from the candidate table, if the PSCID exists.
* `Default cohort`: Default cohort used when Enable visit creation is set to true. This value is used when
the following rules fails:
- the $subjectID{'CohortID'} is undef in the profileTemplate (prod) file;
- the CohortID from the session table, if the PSCID and visit labels exist;
- the default_cohort DB config value.
* `Default visit label for BIDS dataset`: the visit directory in BIDS
structure is optional in the case of only one visit for the whole dataset. In
this case, we need to specify to LORIS what would be the default visit label
the project wants to use to store the electrophysiology datasets (*e.g.* V01).
* `Project batch management used`: enable or disable batch management
* `Number of volumes in native DTI acquisitions`: used by the DTIPrep pipeline
* `Scan type of native T1 acquisition`: name as specified in the `mri_scan_type`
Expand All @@ -170,10 +186,6 @@ Under the `Imaging Pipeline` section:
control. Feature to be integrated in the code base in a **future** release.
* `Name of the Perl MRI config file`: name of the perl-based MRI config file to use when running
the Perl insertion scripts; typically `prod`; used when Auto launch is turned on.
* `Default visit label for BIDS dataset`: the visit directory in BIDS
structure is optional in the case of only one visit for the whole dataset. In
this case, we need to specify to LORIS what would be the default visit label
the project wants to use to store the electrophysiology datasets (*e.g.* V01).
* `Name of the environment file`: name of the MRI environment file to source before
running the insertion scripts; typically `environment`; used when Auto-launch
is turned on for the pipeline.
Expand Down
18 changes: 18 additions & 0 deletions docs/scripts_md/ConfigOB.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ Get the createCandidates Config setting.

RETURN: (boolean) 1 if createCandidates is set to Yes in the Config module, 0 otherwise

### getCreateVisit()

Get the createVisit Config setting.

RETURN: (boolean) 1 if createVisit is set to Yes in the Config module, 0 otherwise

### getDefaultProject()

Get the default_project Config setting.

RETURN: value (string) of the default_project config in the Config table.

### getDefaultCohort()

Get the default_cohort Config setting.

RETURN: value (string) of the default_cohort config in the Config table.

### getPythonConfigFile()

Get the MriPythonConfigFile Config setting.
Expand Down
12 changes: 12 additions & 0 deletions docs/scripts_md/MRI.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ RETURNS: a two element array:
- first is the MRI alias of the PSC or "UNKN"
- second is the `CenterID` or 0

### getProject($patientName, $dbhr, $db)

Looks for the project id using the C<session> table C<ProjectID> as
a first resource, then using the C<candidate> table C<RegistrationProjectID>,
otherwise, look for the default_project config value, and return C<ProjectID>.

INPUTS:
- $subjectIDsref: subject's information hash ref
- $dbhr : database handle reference

RETURNS: the C<ProjectID> or an error if not found

### compute\_hash($file\_ref)

Semi-intelligently generates a hash (MD5 digest) for the `NeuroDB::File` object
Expand Down
2 changes: 1 addition & 1 deletion uploadNeuroDB/NeuroDB/ExitCodes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ our $INVALID_ENVIRONMENT_VAR = 40; # used when an environment variable is
# value
our $PROJECT_CUSTOMIZATION_FAILURE = 41; # used when either missing a function
# or a customization variable

our $PROJECT_INIT_FAILURE = 42; # used when project can't be initialized

## -- Common file manipulation failures (exit codes from 60 to 79)
our $EXTRACTION_FAILURE = 60; # if archive extraction failed
Expand Down
132 changes: 131 additions & 1 deletion uploadNeuroDB/NeuroDB/MRI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ sub getPSC {

my $sth = $${dbhr}->prepare($query);
$sth->execute($PSCID, $visitLabel);
if ( $sth->rows > 0) {
if ($sth->rows > 0) {
my $row = $sth->fetchrow_hashref();
return ($row->{'MRI_alias'},$row->{'CenterID'});
}
Expand All @@ -1125,6 +1125,136 @@ sub getPSC {

=pod

=head3 getProject($subjectIDsref, $dbhr, $db)

Looks for the project id using the C<session> table C<ProjectID> as
a first resource, then using the C<candidate> table C<RegistrationProjectID>,
otherwise, look for the default_project config value, and return C<ProjectID>.

INPUTS:
- $subjectIDsref: subject's information hash ref
- $dbhr : database handle reference
- $db : database object

RETURNS: the C<ProjectID> or an error if not found

=cut

sub getProject {
my ($subjectIDsref, $dbhr, $db) = @_;
my $PSCID = $subjectIDsref->{'PSCID'};
my $visitLabel = $subjectIDsref->{'visitLabel'};
my $configOB = NeuroDB::objectBroker::ConfigOB->new(db => $db);

## Get the ProjectID from the session table, if the PSCID and visit labels exist
## and could be extracted
if ($PSCID && $visitLabel) {
my $query = "SELECT s.ProjectID FROM session s
JOIN candidate c on c.CandID=s.CandID
WHERE c.PSCID = ? AND s.Visit_label = ?";

my $sth = $${dbhr}->prepare($query);
$sth->execute($PSCID, $visitLabel);
if ($sth->rows > 0) {
my $row = $sth->fetchrow_hashref();
return $row->{'ProjectID'};
}
}

## Otherwise get the ProjectID from the candidate table, if the PSCID exists
if ($PSCID) {
my $query = "SELECT RegistrationProjectID
FROM candidate
WHERE PSCID = ?";

my $sth = $${dbhr}->prepare($query);
$sth->execute($PSCID);
if ($sth->rows > 0) {
my $row = $sth->fetchrow_hashref();
return $row->{'RegistrationProjectID'};
}
}

## Otherwise, use the default_project config value
my $query = "SELECT ProjectID
FROM Project
WHERE Name = ?";

my $default_project = $configOB->getDefaultProject();
my $sth = $${dbhr}->prepare($query);
$sth->execute($default_project);
if ( $sth->rows > 0) {
my $row = $sth->fetchrow_hashref();
if ($row->{'ProjectID'}) {
return $row->{'ProjectID'};
}
}

print STDERR "\nERROR: ProjectID cannot be initialized. ".
"Please set the default_project config.\n\n";
exit $NeuroDB::ExitCodes::PROJECT_CUSTOMIZATION_FAILURE;
}

=pod

=head3 getCohort($subjectIDsref, $projectID, $dbhr, $db)

Looks for the cohort id using the C<session> table C<ProjectID> as
a first resource, for the cases where it is created using the front-end,
otherwise, look for the default_cohort config value, and return C<CohortID>.

INPUTS:
- $subjectIDsref: subject's information hash ref
- $projectID : the project ID
- $dbhr : database handle reference
- $db : database object

RETURNS: the C<CohortID> or 0

=cut

sub getCohort {
my ($subjectIDsref, $projectID, $dbhr, $db) = @_;
my $PSCID = $subjectIDsref->{'PSCID'};
my $visitLabel = $subjectIDsref->{'visitLabel'};
my $configOB = NeuroDB::objectBroker::ConfigOB->new(db => $db);

## Get the CohortID from the session table, if the PSCID and visit labels exist
## and could be extracted
if ($PSCID && $visitLabel) {
my $query = "SELECT s.CohortID FROM session s
JOIN candidate c on c.CandID=s.CandID
WHERE c.PSCID = ? AND s.Visit_label = ?";

my $sth = $${dbhr}->prepare($query);
$sth->execute($PSCID, $visitLabel);
if ( $sth->rows > 0) {
my $row = $sth->fetchrow_hashref();
return $row->{'CohortID'};
}
}

## Otherwise, use the default_cohort config value
my $query = "SELECT CohortID
FROM cohort
JOIN project_cohort_rel USING (CohortID)
JOIN Project USING(ProjectID)
WHERE title = ? AND Name = ?";

my $default_cohort = $configOB->getDefaultCohort();
my $default_project = $configOB->getDefaultProject();
my $sth = $${dbhr}->prepare($query);
$sth->execute($default_cohort, $default_project);
if ( $sth->rows > 0) {
my $row = $sth->fetchrow_hashref();
return $row->{'CohortID'};
}

return ("UNKN", 0);
}

=pod

=head3 compute_hash($file_ref)

Semi-intelligently generates a hash (MD5 digest) for the C<NeuroDB::File> object
Expand Down
Loading