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

[fli-iam#1864] Add NIRS type BIDS #1865

Merged
merged 14 commits into from
Nov 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.shanoir.ng.dataset.DatasetDescription;
import org.shanoir.ng.dataset.controler.DatasetApiController.CoordinatesSystem;
import org.shanoir.ng.dataset.modality.BidsDataset;
import org.shanoir.ng.dataset.modality.EegDataSetDescription;
import org.shanoir.ng.dataset.modality.EegDataset;
import org.shanoir.ng.dataset.modality.MrDataset;
import org.shanoir.ng.dataset.modality.MrDatasetNature;
import org.shanoir.ng.dataset.modality.PetDataset;
import org.shanoir.ng.dataset.modality.*;
import org.shanoir.ng.dataset.model.Dataset;
import org.shanoir.ng.dataset.model.DatasetExpression;
import org.shanoir.ng.dataset.model.DatasetExpressionFormat;
Expand Down Expand Up @@ -203,7 +199,7 @@ public void deleteBidsFolder(Long studyId, String studyName) {
studyName = this.studyRepo.findById(studyId).get().getName();
}
// Try to delete the BIDS folder recursively if possible
File bidsDir = new File(bidsStorageDir + File.separator + STUDY_PREFIX + studyId + '_' + studyName);
File bidsDir = new File(bidsStorageDir + File.separator + STUDY_PREFIX + studyId + studyName);
if (bidsDir.exists()) {
FileUtils.deleteDirectory(bidsDir);
}
Expand All @@ -214,15 +210,14 @@ public void deleteBidsFolder(Long studyId, String studyName) {

/**
* Returns data from the study formatted as BIDS in a .zip file.
* @param study the study we want to export as BIDS
* @return data from the study formatted as BIDS in a .zip file.
* @throws IOException
*/
@Override
public File exportAsBids(final Long studyId, String studyName) throws IOException {
// Get folder
studyName = studyName.replaceAll("[ _] ", "");
String tmpFilePath = bidsStorageDir + File.separator + STUDY_PREFIX + studyId + '_' + studyName;
studyName = this.formatLabel(studyName);
String tmpFilePath = bidsStorageDir + File.separator + STUDY_PREFIX + studyId + studyName;
File workFolder = new File(tmpFilePath);
if (workFolder.exists()) {
// If the file already exists, just return it
Expand Down Expand Up @@ -267,7 +262,6 @@ private List<Subject> getSubjectsForStudy(final Long studyId) throws JsonParseEx
/**
* Create the study/BASE BIDS folder.
* @param studyName the study name
* @param studyId the study id
* @return the base folder newly created
*/
private File createBaseBidsFolder(File workFolder, String studyName) {
Expand Down Expand Up @@ -317,11 +311,7 @@ private void exportAsBids(final Subject subject, final String studyName, Long st
.append(NEW_LINE);

for (Examination examination : examinationList) {
String sessionLabel = "" + examination.getId();
sessionLabel += (examination.getComment() != null ? "-" + examination.getComment() : "");

sessionLabel = sessionLabel.replaceAll(" ", "");
sessionLabel = sessionLabel.replaceAll("_", "");
String sessionLabel = this.getSessionLabel(examination);

buffer.append(sessionLabel).append(TABULATION)
.append(examination.getExaminationDate()).append(TABULATION)
Expand All @@ -334,7 +324,7 @@ private void exportAsBids(final Subject subject, final String studyName, Long st
File examDir = subjDir;
for (Examination exam : examinationList) {
if (useSessionFolder) {
examDir = createExaminationFolder(exam, subjDir, sessionFile);
examDir = createExaminationFolder(exam, subjDir);
}
exportAsBids(exam, examDir, studyName, subject.getName());
}
Expand All @@ -353,10 +343,9 @@ private void exportAsBids(final Subject subject, final String studyName, Long st
*/
private File createSubjectFolder(String subjectName, final int index, final File baseDir) throws IOException {
// Generate another ID here ?
subjectName = subjectName.replaceAll(" ", "");
subjectName = subjectName.replaceAll("_", "");
subjectName = this.formatLabel(subjectName);

File subjectFolder = new File(baseDir.getAbsolutePath() + File.separator + SUBJECT_PREFIX + index + "-" + subjectName);
File subjectFolder = new File(baseDir.getAbsolutePath() + File.separator + SUBJECT_PREFIX + index + subjectName);
if (!subjectFolder.exists()) {
subjectFolder.mkdirs();
}
Expand All @@ -366,7 +355,6 @@ private File createSubjectFolder(String subjectName, final int index, final File
/**
* Returns data from the examination formatted as BIDS in a .zip file.
* @param examination the examination we want to export as BIDS
* @param subjDir examination BIDS directory where we are working.
* @param studyName the study name
* @param subjectName the subject name
* @return data from the examination formatted as BIDS in a .zip file.
Expand Down Expand Up @@ -403,16 +391,11 @@ private void exportAsBids(final Examination examination, final File examDir, fin
* Create the session/examination BIDS folder
* @param examination the examination for which we want to create the folder
* @param subjectDir the parent folder
* @param sessionFile the session file to complete
* @return the newly created folder
* @throws IOException
*/
private File createExaminationFolder(final Examination examination, final File subjectDir, File sessionFile) throws IOException {
String sessionLabel = "" + examination.getId();
sessionLabel += (examination.getComment() != null ? "-" + examination.getComment() : "");

sessionLabel = sessionLabel.replaceAll(" ", "");
sessionLabel = sessionLabel.replaceAll("_", "");
private File createExaminationFolder(final Examination examination, final File subjectDir) throws IOException {
String sessionLabel = this.getSessionLabel(examination);

// Create exam/session folder
File examFolder = new File(subjectDir.getAbsolutePath() + File.separator + SESSION_PREFIX + sessionLabel);
Expand All @@ -422,6 +405,15 @@ private File createExaminationFolder(final Examination examination, final File s
return examFolder;
}

private String getSessionLabel(Examination examination) {
String label = "" + examination.getId();
if(!StringUtils.isBlank(examination.getComment())){
label += examination.getComment();
}

return formatLabel(label);
}

/**
* Create the list of BIDS files associated to a dataset.
* @param dataset the dataset from which we want the specific BIDS files to be created
Expand All @@ -434,7 +426,7 @@ private File createExaminationFolder(final Examination examination, final File s
private void createDatasetBidsFiles(final Dataset dataset, final File workDir, final String studyName, final String subjectName) throws IOException {
File dataFolder = null;

String subjectNameUpdated = subjectName.replaceAll("[ _] ", "");
String subjectNameUpdated = this.formatLabel(subjectName);
String datasetFilePrefix = workDir.getName().contains(SESSION_PREFIX) ? workDir.getParentFile().getName() + "_" + workDir.getName() : workDir.getName();

dataFolder = createSpecificDataFolder(dataset, workDir, dataFolder, subjectNameUpdated, studyName);
Expand Down Expand Up @@ -556,8 +548,7 @@ private void deleteIfExists(String filePath) {

private File getScansFile(File parentFile, String subjectName) throws IOException {
String fileName = parentFile.getName() + SCANS_FILE_EXTENSION;
subjectName = subjectName.replaceAll(" ", "");
subjectName = subjectName.replaceAll("_", "");
subjectName = this.formatLabel(subjectName);
if (!parentFile.getName().contains(subjectName)) {
fileName = SUBJECT_PREFIX + subjectName + "_" + parentFile.getName() + SCANS_FILE_EXTENSION;
}
Expand Down Expand Up @@ -618,8 +609,6 @@ private void getDatasetFilePathURLs(final Dataset dataset, final List<URL> pathU
* See https://bids-specification.readthedocs.io/en/latest/04-modality-specific-files/03-electroencephalography.html
* for more informations
* @param dataset the dataset we want to export in BIDS
* @param workFolder the examination work folder in which we are working
* @param pathURLs list of file URL
* @param studyName the name of associated study
* @param subjectName the subject name associated
* @param sessionId the session ID / examination ID associated
Expand Down Expand Up @@ -760,8 +749,7 @@ private void participantsSerializer(File parentFolder, List<Subject> subjs) {

for (Subject subject : subjs) {
String subjectName = subject.getName();
subjectName = subjectName.replaceAll(" ", "");
subjectName = subjectName.replaceAll("_", "");
subjectName = this.formatLabel(subjectName);
// Write in the file the values
buffer.append(SUBJECT_PREFIX).append(index++).append("_").append(subjectName).append(CSV_SEPARATOR)
.append(subject.getId()).append(CSV_SEPARATOR)
Expand All @@ -775,4 +763,8 @@ private void participantsSerializer(File parentFolder, List<Subject> subjs) {
}
}

private String formatLabel(String label){
michaelkain marked this conversation as resolved.
Show resolved Hide resolved
return label.replaceAll("[^a-zA-Z0-9]+", "");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public enum BidsDataType {
PET("pet"),

/* Microscopy.*/
MICR("micr");
MICR("micr"),

/* Near-Infrared Spectroscopy */
NIRS("nirs");

private String folderName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public enum DatasetModalityType {
MICR_DATASET(9),

// Behavioural Dataset
BEH_DATASET(10);
BEH_DATASET(10),

// Near-Infrared Spectroscopy dataset
NIRS_DATASET(11);

private int id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,36 +568,70 @@ public boolean hasUpdateRightOnDataset(Dataset dataset, String rightStr) throws
}
}

/**
* Check the connected user has the given right for the given dataset acquisition.
* If the study is updated, check the user has the given right in both former and new studies.
*
* @param datasetAcq the dataset acquisition
* @param rightStr the right
* @return true or false
* @throws EntityNotFoundException
*/
public boolean hasUpdateRightOnDatasetAcquisition(DatasetAcquisition datasetAcq, String rightStr) throws EntityNotFoundException {
youennmerel marked this conversation as resolved.
Show resolved Hide resolved
if (KeycloakUtil.getTokenRoles().contains("ROLE_ADMIN")) {
return true;
}
if (datasetAcq == null) {
throw new IllegalArgumentException("Dataset acquisition cannot be null here.");
}
if (datasetAcq.getId() == null) {
throw new IllegalArgumentException("Dataset acquisition id cannot be null here.");
}
if (datasetAcq.getExamination() == null || datasetAcq.getExamination().getStudyId() == null) {
return false;
}
DatasetAcquisition dbDatasetAcq = datasetAcquisitionRepository.findById(datasetAcq.getId()).orElse(null);
if (dbDatasetAcq == null) {
throw new EntityNotFoundException("Cannot find dataset acquisition with id " + datasetAcq.getId());
}
if (datasetAcq.getExamination().getStudyId() == dbDatasetAcq.getExamination().getStudyId()) { // study hasn't changed
return this.hasRightOnStudyCenter(datasetAcq.getExamination().getCenterId(), datasetAcq.getExamination().getStudyId(), rightStr);
} else { // study has changed : check user has right on both studies
return this.hasRightOnStudyCenter(datasetAcq.getExamination().getCenterId(), datasetAcq.getExamination().getStudyId(), rightStr) &&
this.hasRightOnStudyCenter(dbDatasetAcq.getExamination().getCenterId(), dbDatasetAcq.getExamination().getStudyId(), rightStr);
}
}

/**
* Check the connected user has the given right for the given dataset acquisition.
* Check the connected user has the given right for the given dataset acquisition DTO.
* If the study is updated, check the user has the given right in both former and new studies.
*
* @param datasetAcq the dataset acquisition
* @param datasetAcqDto the dataset acquisition dto
* @param rightStr the right
* @return true or false
* @throws EntityNotFoundException
*/
public boolean hasUpdateRightOnDatasetAcquisition(DatasetAcquisition datasetAcq, String rightStr) throws EntityNotFoundException {
public boolean hasUpdateRightOnDatasetAcquisitionDTO(DatasetAcquisitionDTO datasetAcqDto, String rightStr) throws EntityNotFoundException {
if (KeycloakUtil.getTokenRoles().contains("ROLE_ADMIN")) {
return true;
}
if (datasetAcq == null) {
if (datasetAcqDto == null) {
throw new IllegalArgumentException("Dataset acquisition cannot be null here.");
}
if (datasetAcq.getId() == null) {
if (datasetAcqDto.getId() == null) {
throw new IllegalArgumentException("Dataset acquisition id cannot be null here.");
}
if (datasetAcq.getExamination() == null || datasetAcq.getExamination().getStudyId() == null) {
if (datasetAcqDto.getExamination() == null || datasetAcqDto.getExamination().getStudyId() == null) {
return false;
}
DatasetAcquisition dbDatasetAcq = datasetAcquisitionRepository.findById(datasetAcq.getId()).orElse(null);
DatasetAcquisition dbDatasetAcq = datasetAcquisitionRepository.findById(datasetAcqDto.getId()).orElse(null);
if (dbDatasetAcq == null) {
throw new EntityNotFoundException("Cannot find dataset acquisition with id " + datasetAcq.getId());
throw new EntityNotFoundException("Cannot find dataset acquisition with id " + datasetAcqDto.getId());
}
if (datasetAcq.getExamination().getStudyId() == dbDatasetAcq.getExamination().getStudyId()) { // study hasn't changed
return this.hasRightOnStudyCenter(datasetAcq.getExamination().getCenterId(), datasetAcq.getExamination().getStudyId(), rightStr);
} else { // study has changed : check user has right on both studies
return this.hasRightOnStudyCenter(datasetAcq.getExamination().getCenterId(), datasetAcq.getExamination().getStudyId(), rightStr) &&
if (datasetAcqDto.getExamination().getStudyId() == dbDatasetAcq.getExamination().getStudyId()) { // study hasn't changed
return this.hasRightOnStudyCenter(datasetAcqDto.getExamination().getCenterId(), datasetAcqDto.getExamination().getStudyId(), rightStr);
} else { // study has changed : check user has right on both studies
return this.hasRightOnStudyCenter(datasetAcqDto.getExamination().getCenterId(), datasetAcqDto.getExamination().getStudyId(), rightStr) &&
this.hasRightOnStudyCenter(dbDatasetAcq.getExamination().getCenterId(), dbDatasetAcq.getExamination().getStudyId(), rightStr);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,7 @@
import java.util.Iterator;
import java.util.List;

import org.shanoir.ng.dataset.modality.BidsDataset;
import org.shanoir.ng.dataset.modality.CalibrationDataset;
import org.shanoir.ng.dataset.modality.CtDataset;
import org.shanoir.ng.dataset.modality.EegDataset;
import org.shanoir.ng.dataset.modality.GenericDataset;
import org.shanoir.ng.dataset.modality.MegDataset;
import org.shanoir.ng.dataset.modality.MeshDataset;
import org.shanoir.ng.dataset.modality.MrDataset;
import org.shanoir.ng.dataset.modality.ParameterQuantificationDataset;
import org.shanoir.ng.dataset.modality.PetDataset;
import org.shanoir.ng.dataset.modality.RegistrationDataset;
import org.shanoir.ng.dataset.modality.SegmentationDataset;
import org.shanoir.ng.dataset.modality.SpectDataset;
import org.shanoir.ng.dataset.modality.StatisticalDataset;
import org.shanoir.ng.dataset.modality.TemplateDataset;
import org.shanoir.ng.dataset.modality.*;
import org.shanoir.ng.dataset.model.Dataset;
import org.shanoir.ng.dataset.model.DatasetExpression;
import org.shanoir.ng.dataset.model.DatasetExpressionFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ ResponseEntity<List<DatasetAcquisitionDatasetsDTO>> findDatasetAcquisitionByData
@ApiResponse(responseCode = "500", description = "unexpected error") })
@RequestMapping(value = "/datasetacquisition/{datasetAcquisitionId}", produces = { "application/json" }, consumes = {
"application/json" }, method = RequestMethod.PUT)
@PreAuthorize("hasAnyRole('ADMIN', 'EXPERT') and #datasetAcquisitionId == #datasetAcquisition.getId() and @datasetSecurityService.hasUpdateRightOnDatasetAcquisition(#datasetAcquisition, 'CAN_ADMINISTRATE')")
@PreAuthorize("hasAnyRole('ADMIN', 'EXPERT') and #datasetAcquisitionId == #datasetAcquisition.getId() and @datasetSecurityService.hasRightOnExamination(#datasetAcquisition.examination.id, 'CAN_ADMINISTRATE')")
ResponseEntity<Void> updateDatasetAcquisition(
@Parameter(name = "id of the datasetAcquisition", required = true) @PathVariable("datasetAcquisitionId") Long datasetAcquisitionId,
@Parameter(name = "datasetAcquisition to update", required = true) @Valid @RequestBody DatasetAcquisitionDTO datasetAcquisition, BindingResult result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
import org.shanoir.ng.dataset.service.DatasetUtils;
import org.shanoir.ng.datasetacquisition.dto.DatasetAcquisitionDatasetsDTO;
import org.shanoir.ng.datasetacquisition.model.DatasetAcquisition;
import org.shanoir.ng.datasetacquisition.model.GenericDatasetAcquisition;
import org.shanoir.ng.datasetacquisition.model.bids.BidsDatasetAcquisition;
import org.shanoir.ng.datasetacquisition.model.ct.CtDatasetAcquisition;
import org.shanoir.ng.datasetacquisition.model.eeg.EegDatasetAcquisition;
import org.shanoir.ng.datasetacquisition.model.mr.MrDatasetAcquisition;
import org.shanoir.ng.datasetacquisition.model.pet.PetDatasetAcquisition;
import org.shanoir.ng.examination.dto.mapper.ExaminationMapper;
Expand All @@ -54,8 +57,11 @@ DatasetAcquisitionDatasetsDTO datasetAcquisitionToDatasetAcquisitionDatasetsDTO(
default DatasetAcquisition createDatasetAcquisition(DatasetAcquisitionDatasetsDTO dto) {
if (dto.getType().equals("Mr")) return new MrDatasetAcquisition();
else if (dto.getType().equals("Pet")) return new PetDatasetAcquisition();
else if (dto.getType().equals("Ct")) return new CtDatasetAcquisition();
else throw new IllegalStateException("Cannot map from a dataset acquisition dto that don't provide a valid type. Given type = " + dto.getType());
else if (dto.getType().equals("Ct")) return new CtDatasetAcquisition();
else if (dto.getType().equals("BIDS")) return new BidsDatasetAcquisition();
else if (dto.getType().equals("Eeg")) return new EegDatasetAcquisition();
else if (dto.getType().equals("Generic")) return new GenericDatasetAcquisition();
else throw new IllegalStateException("Cannot map from a dataset acquisition dto that don't provide a valid type. Given type = " + dto.getType());
}

@ObjectFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import org.mapstruct.ObjectFactory;
import org.shanoir.ng.datasetacquisition.dto.DatasetAcquisitionDTO;
import org.shanoir.ng.datasetacquisition.model.DatasetAcquisition;
import org.shanoir.ng.datasetacquisition.model.GenericDatasetAcquisition;
import org.shanoir.ng.datasetacquisition.model.bids.BidsDatasetAcquisition;
import org.shanoir.ng.datasetacquisition.model.ct.CtDatasetAcquisition;
import org.shanoir.ng.datasetacquisition.model.eeg.EegDatasetAcquisition;
import org.shanoir.ng.datasetacquisition.model.mr.MrDatasetAcquisition;
import org.shanoir.ng.datasetacquisition.model.pet.PetDatasetAcquisition;
import org.shanoir.ng.examination.dto.mapper.ExaminationMapper;
Expand All @@ -49,9 +52,12 @@ DatasetAcquisitionDTO datasetAcquisitionToDatasetAcquisitionDTO(

@ObjectFactory
default DatasetAcquisition createDatasetAcquisition(DatasetAcquisitionDTO dto) {
if (dto.getType().equals("Mr")) return new MrDatasetAcquisition();
else if (dto.getType().equals("Pet")) return new PetDatasetAcquisition();
else if (dto.getType().equals("Ct")) return new CtDatasetAcquisition();
if (dto.getType().equals("Mr")) return new MrDatasetAcquisition();
else if (dto.getType().equals("Pet")) return new PetDatasetAcquisition();
else if (dto.getType().equals("Ct")) return new CtDatasetAcquisition();
else if (dto.getType().equals("BIDS")) return new BidsDatasetAcquisition();
else if (dto.getType().equals("Eeg")) return new EegDatasetAcquisition();
else if (dto.getType().equals("Generic")) return new GenericDatasetAcquisition();
else throw new IllegalStateException("Cannot map from a dataset acquisition dto that don't provide a valid type. Given type = " + dto.getType());
}

Expand Down
Loading
Loading