From 93e6e4621a8da674ac2b91fbb22ca006985a42ef Mon Sep 17 00:00:00 2001 From: Erik Diers <erik.diers@ruhr-uni-bochum.de> Date: Sun, 2 Feb 2025 23:07:44 +0100 Subject: [PATCH] Added creation by xlsx file --- src/main/Main.java | 99 +++++++++++++++++++++++++++++++++------ src/utils/CSV_Reader.java | 40 ++++++++++++++++ 2 files changed, 125 insertions(+), 14 deletions(-) diff --git a/src/main/Main.java b/src/main/Main.java index f70c89c..c1c5d8d 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -13,9 +13,16 @@ import uiutils.InputFieldPanel; import uiutils.LabelPanel; import uiutils.SimpleFrame; import uiutils.SpinnerINTPanel; +import utils.CSV_Reader; +import utils.SFB_JSON_Util; +import utils.SFB_Util; public class Main { + private static final String XLSX_SESSION_PREFIX = "SessionDate"; + private static final String SFB_SUBJID = "subjectID"; + private static final String SFB_RECORD_DATE = "Record Date"; + private static int xlsx_subjIdHeaderIndex = -1; public static void main(String[] args) { SimpleFrame frame = new SimpleFrame("Create Study", 600, 400); @@ -30,6 +37,12 @@ public class Main { ComboSelectionPanel modalities = new ComboSelectionPanel("Modalities:", "Select", ComboSelectionPanel.SFB_MODALITIES); InputFieldPanel customeModalities = new InputFieldPanel("Modality Folder", "EDA, fMRI, ...").setTextFieldprefWidth(300); ConditionedPanel useCustomModalityFolders = new ConditionedPanel("Enter own modality folder names:", false, customeModalities); + + + FileChooserPanel baseMetaJson = new FileChooserPanel("Base Meta.json").switchToFileSelect("meta.json"); + FileChooserPanel xlsxImportFile = new FileChooserPanel("Excel import file").switchToFileSelect(".xlsx"); + ConditionedPanel importFromXlsx = new ConditionedPanel("Import from Excel", false, baseMetaJson, xlsxImportFile); + ButtonPanel submit = new ButtonPanel("Create", new ActionListener() { @Override @@ -52,25 +65,56 @@ public class Main { return; } String[] codes; + CSV_Reader xlsxReader = null; - if (useCustomSubjectCodes.getOutput()) { - String codesSTR = sfbCodes.getOutput(); - if (codesSTR.trim().isEmpty()) { - SimpleFrame.promt("Please enter patient identifiers"); + int sessions = sessionAmount.getOutput(); + + if (importFromXlsx.getOutput()) { + if (baseMetaJson.getOutput() == null || !baseMetaJson.getOutput().exists()) { + SimpleFrame.promt("Please specify a valid base meta.json file"); return; } - codesSTR = codesSTR.replaceAll(" ", ""); - codes = codesSTR.split(","); + + if (xlsxImportFile.getOutput() == null || !xlsxImportFile.getOutput().exists()) { + SimpleFrame.promt("Please specify a valid excel import file"); + return; + } + + xlsxReader = CSV_Reader.fromXLSX(xlsxImportFile.getOutput(), null); + + for (int i = 0; i < sessions; i++) { + if (xlsxReader.getColumn(XLSX_SESSION_PREFIX + " " + i) == -1) { + SimpleFrame.promt("Excel file has not specifed Session date " + i + " [" + XLSX_SESSION_PREFIX + " " + i + "]"); + return; + } + } + + + xlsx_subjIdHeaderIndex = xlsxReader.getColumnUncased(SFB_SUBJID); // + codes = xlsxReader.getCompleteColumn(xlsx_subjIdHeaderIndex); + } else { - int subj = numberOfConsecutiveSubjects.getOutput(); - codes = new String[subj]; - for(int i = 0; i < subj; i++) { - codes[i] = "sub-" + (i + 1); + + //Does not generate codes when a xlsx file is given! + if (useCustomSubjectCodes.getOutput()) { + String codesSTR = sfbCodes.getOutput(); + if (codesSTR.trim().isEmpty()) { + SimpleFrame.promt("Please enter patient identifiers"); + return; + } + codesSTR = codesSTR.replaceAll(" ", ""); + codes = codesSTR.split(","); + } else { + int subj = numberOfConsecutiveSubjects.getOutput(); + codes = new String[subj]; + for(int i = 0; i < subj; i++) { + codes[i] = "sub-" + (i + 1); + } } } - int sessions = sessionAmount.getOutput(); + String[] modalityFolders = modalities.getOutput(); @@ -94,7 +138,7 @@ public class Main { } try { - createStudy(dest, codes, sessions, modalityFolders); + createStudy(dest, codes, sessions, modalityFolders, xlsxReader, baseMetaJson.getOutput()); } catch (IOException e1) { e1.printStackTrace(); } @@ -112,14 +156,15 @@ public class Main { frame.addCenter(sessionAmount); frame.addCenter(modalities); frame.addCenter(useCustomModalityFolders); + frame.addCenter(importFromXlsx); frame.addCenterNoLeftalignment(submit); frame.submit(); } - private static void createStudy(File studyFile, String[] sfbCodes, int sessionAmount, String[] modalities) throws IOException { + private static void createStudy(File studyFile, String[] sfbCodes, int sessionAmount, String[] modalities, CSV_Reader xlsxReader, File baseMeta) throws IOException { studyFile.mkdir(); - for(String code:sfbCodes) { + for(String code : sfbCodes) { File subjFolder = new File(studyFile.getAbsolutePath() + "\\" + code); subjFolder.mkdir(); @@ -127,14 +172,40 @@ public class Main { File sessionFolder = new File(subjFolder.getAbsolutePath() + "\\ses-" + i); sessionFolder.mkdir(); + if (xlsxReader != null) + createMetaFile(sessionFolder, xlsxReader, baseMeta, code, i); + for(String modality : modalities) { File modFolder = new File(sessionFolder.getAbsolutePath() + "\\" + modality); modFolder.mkdir(); + if (xlsxReader != null) + createMetaFile(modFolder, xlsxReader, baseMeta, code, i); } } } + } + + @SuppressWarnings("unchecked") + private static void createMetaFile(File dir, CSV_Reader xlsxReader, File baseMeta, String sfbSubjCode ,int currentSession) throws IOException { + File metaJson = new File(dir.getAbsolutePath() + File.separator + "meta.json"); + SFB_Util.copy(baseMeta, metaJson); + SFB_JSON_Util.performOnJson(metaJson, (obj) -> { + String[] currentRow = xlsxReader.getCompleteRow(xlsxReader.findRow(sfbSubjCode, xlsx_subjIdHeaderIndex)); + + for(int i = 0; i < currentRow.length; i++) { + String header = xlsxReader.getColumnHead(i); + if (header.startsWith(XLSX_SESSION_PREFIX)) { + if (header.endsWith("" + currentSession)) //Identifies the current session. This breaks in case of more than 9 sessions. But this wont happen. + header = SFB_RECORD_DATE; + else + continue; + } + obj.put(header, currentRow[i]); + + } + }); } } diff --git a/src/utils/CSV_Reader.java b/src/utils/CSV_Reader.java index 108581a..00cf110 100644 --- a/src/utils/CSV_Reader.java +++ b/src/utils/CSV_Reader.java @@ -5,6 +5,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Calendar; +import java.util.Iterator; import java.util.LinkedList; import java.util.Scanner; import java.util.zip.ZipEntry; @@ -87,11 +88,41 @@ public class CSV_Reader { return -1; } + public String getColumnHead(int indx) { + return headers[indx]; + } + + public int getColumnUncased(String header) { + header = header.toLowerCase(); + for(int i = 0; i < headers.length; i++) { + if (headers[i].toLowerCase().equals(header)) return i; + } + return -1; + } + + public String[] getCompleteColumn(int col) { + String[] column = new String[getMaxRows()]; + + for(int i = 0; i < column.length; i++) { + column[i] = entries[i][col]; + } + return column; + } + + public String[] getCompleteRow(int row) { + return entries[row]; + } + public int getMaxRows() { if (entries == null) return 0; return entries.length; } + public int getMaxColumns() { + if (headers == null) return 0; + return headers.length; + } + public String get(int column, int row) { return entries[row][column]; } @@ -108,6 +139,15 @@ public class CSV_Reader { } } + public int findRow(String searchFor, int inColumn) { + for (int row = 0; row < getMaxRows(); row++) { + if (entries[row][inColumn].equals(searchFor)) + return row; + } + return -1; + } + + public void printInlineVersion() { StringBuilder b = new StringBuilder(); b.append("private static CSV_Reader csv = new CSV_Reader(new String[] {"); -- GitLab