From 785f0a439cf914655b1ff0c4161a8bcee5081520 Mon Sep 17 00:00:00 2001 From: Eugene Katrukha Date: Mon, 30 Dec 2024 13:44:33 +0100 Subject: [PATCH] traces csv and swc export --- src/main/java/bigtrace/io/ROIsExportCSV.java | 115 +++++++++++++++++ src/main/java/bigtrace/io/ROIsExportSWC.java | 120 ++++++++++++++++++ src/main/java/bigtrace/io/ROIsSaveBG.java | 3 +- .../bigtrace/rois/RoiManager3DDialogs.java | 102 ++++++++++++--- 4 files changed, 318 insertions(+), 22 deletions(-) create mode 100644 src/main/java/bigtrace/io/ROIsExportCSV.java create mode 100644 src/main/java/bigtrace/io/ROIsExportSWC.java diff --git a/src/main/java/bigtrace/io/ROIsExportCSV.java b/src/main/java/bigtrace/io/ROIsExportCSV.java new file mode 100644 index 0000000..e8a1367 --- /dev/null +++ b/src/main/java/bigtrace/io/ROIsExportCSV.java @@ -0,0 +1,115 @@ +package bigtrace.io; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.util.ArrayList; + +import javax.swing.SwingWorker; + +import net.imglib2.RealPoint; +import net.imglib2.type.NativeType; +import net.imglib2.type.numeric.RealType; + +import bigtrace.BigTrace; +import bigtrace.BigTraceBGWorker; +import bigtrace.rois.AbstractCurve3D; +import bigtrace.rois.Roi3D; + +import ij.IJ; + +public class ROIsExportCSV < T extends RealType< T > & NativeType< T > > extends SwingWorker implements BigTraceBGWorker +{ + private String progressState; + public BigTrace bt; + public String sFilename; + + @Override + public String getProgressState() + { + return progressState; + } + @Override + public void setProgressState(String state_) + { + progressState=state_; + } + + @Override + protected Void doInBackground() throws Exception { + int nRoi, nRoiN; + + bt.bInputLock = true; + bt.setLockMode(true); + + try { + final File file = new File(sFilename); + + try (FileWriter writer = new FileWriter(file)) + { + //column titles + + writer.write("ROI_Number,X_coord,Y_coord,Z_coord,ROI_Name,ROI_Type,ROI_Group,ROI_TimePoint\n"); + DecimalFormatSymbols symbols = new DecimalFormatSymbols(); + symbols.setDecimalSeparator('.'); + DecimalFormat df3 = new DecimalFormat ("#.#####", symbols); + nRoiN = bt.roiManager.rois.size(); + String sRoiName; + String sRoiGroup; + String sRoiType; + String sRoiTP; + int nRoiCurrN = 0; + //writer.write("ROIsNumber," + Integer.toString(nRoiN)+"\n"); + for(nRoi=0; nRoi points = currRoi.getJointSegmentResampled(); + for(int nP=0; nP & NativeType< T > > extends SwingWorker implements BigTraceBGWorker +{ + private String progressState; + public BigTrace bt; + public String sFilename; + + @Override + public String getProgressState() + { + return progressState; + } + @Override + public void setProgressState(String state_) + { + progressState=state_; + } + + @Override + protected Void doInBackground() throws Exception { + int nRoi, nRoiN; + + bt.bInputLock = true; + bt.setLockMode(true); + + try { + final File file = new File(sFilename); + + try (FileWriter writer = new FileWriter(file)) + { + DecimalFormatSymbols symbols = new DecimalFormatSymbols(); + symbols.setDecimalSeparator('.'); + DecimalFormat df3 = new DecimalFormat ("#.#####", symbols); + nRoiN = bt.roiManager.rois.size(); + long nPointCount = 1; + //int nCurrActivePoint; + String sRadius; + writer.write("#raw "+ bt.btData.sFileNameFullImg+"\n"); + writer.write("#original_source BigTrace_ver_" +BigTraceData.sVersion+"\n"); + for(nRoi=0; nRoi points = currRoi.getJointSegmentResampled(); + for(int nP=0; nP & NativeType< T > > extends SwingWorker implements BigTraceBGWorker{ +public class ROIsSaveBG < T extends RealType< T > & NativeType< T > > extends SwingWorker implements BigTraceBGWorker +{ private String progressState; public BigTrace bt; diff --git a/src/main/java/bigtrace/rois/RoiManager3DDialogs.java b/src/main/java/bigtrace/rois/RoiManager3DDialogs.java index 5011d48..03d8ced 100644 --- a/src/main/java/bigtrace/rois/RoiManager3DDialogs.java +++ b/src/main/java/bigtrace/rois/RoiManager3DDialogs.java @@ -23,6 +23,8 @@ import bigtrace.BigTraceData; import bigtrace.gui.GBCHelper; import bigtrace.gui.NumberField; +import bigtrace.io.ROIsExportCSV; +import bigtrace.io.ROIsExportSWC; import bigtrace.io.ROIsImportTrackMateBG; import bigtrace.io.ROIsSaveBG; import ij.Prefs; @@ -676,23 +678,85 @@ public void dialGroupVisibility() /** Save ROIS dialog and saving **/ public void diagSaveROIs() { - String filename; - filename = bt.btData.sFileNameFullImg + "_btrois"; - SaveDialog sd = new SaveDialog("Save ROIs ", filename, ".csv"); - String path = sd.getDirectory(); - if (path == null) - return; - filename = path + sd.getFileName(); - - bt.setLockMode(true); - bt.bInputLock = true; + String [] sRoiSaveOptions = new String [] {"Save ROIs BigTrace format","Export interpolated traces CSV", "Export neurite SWC"}; + + String output = (String) JOptionPane.showInputDialog(bt.roiManager, "Save/export ROIs", + "Load mode:", JOptionPane.QUESTION_MESSAGE, null, + sRoiSaveOptions, // Array of choices + sRoiSaveOptions[(int)Prefs.get("BigTrace.SaveRoisMode", 0)]); + if(output == null) + return; + + int nSaveMode = 0; + + for (int i=0;i<3; i++) + { + if(output.equals( sRoiSaveOptions[i] )) + { + nSaveMode = i; + } + } + Prefs.set("BigTrace.SaveRoisMode", nSaveMode); + - ROIsSaveBG saveTask = new ROIsSaveBG<>(); - saveTask.sFilename = filename; - saveTask.bt = this.bt; - saveTask.addPropertyChangeListener(bt.btPanel); - saveTask.execute(); + String filename; + SaveDialog sd; + String path; + switch (nSaveMode) + { + case 0: + filename = bt.btData.sFileNameFullImg + "_btrois"; + sd = new SaveDialog("Save ROIs ", filename, ".csv"); + path = sd.getDirectory(); + if (path == null) + return; + filename = path + sd.getFileName(); + + bt.setLockMode(true); + bt.bInputLock = true; + + ROIsSaveBG saveTask = new ROIsSaveBG<>(); + saveTask.sFilename = filename; + saveTask.bt = this.bt; + saveTask.addPropertyChangeListener(bt.btPanel); + saveTask.execute(); + break; + case 1: + filename = bt.btData.sFileNameFullImg + "_traces"; + sd = new SaveDialog("Export ROIs ", filename, ".csv"); + path = sd.getDirectory(); + if (path == null) + return; + filename = path + sd.getFileName(); + + bt.setLockMode(true); + bt.bInputLock = true; + + ROIsExportCSV exportTask = new ROIsExportCSV<>(); + exportTask.sFilename = filename; + exportTask.bt = this.bt; + exportTask.addPropertyChangeListener(bt.btPanel); + exportTask.execute(); + break; + case 2: + filename = bt.btData.sFileNameFullImg + "_traces"; + sd = new SaveDialog("Export ROIs to SWC ", filename, ".swc"); + path = sd.getDirectory(); + if (path == null) + return; + filename = path + sd.getFileName(); + + bt.setLockMode(true); + bt.bInputLock = true; + + ROIsExportSWC exportSWCTask = new ROIsExportSWC<>(); + exportSWCTask.sFilename = filename; + exportSWCTask.bt = this.bt; + exportSWCTask.addPropertyChangeListener(bt.btPanel); + exportSWCTask.execute(); + break; + } } @@ -721,13 +785,9 @@ void diagLoadROIs() if(input == null) return; - int nLoadMode; + int nLoadMode = 0; - if(input.equals("Clean load ROIs and groups")) - { - nLoadMode = 0; - } - else + if(input.equals(sRoiLoadOptions[1])) { nLoadMode = 1; }