Skip to content

Commit

Permalink
traces csv and swc export
Browse files Browse the repository at this point in the history
  • Loading branch information
ekatrukha committed Dec 30, 2024
1 parent 5edeb79 commit 785f0a4
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 22 deletions.
115 changes: 115 additions & 0 deletions src/main/java/bigtrace/io/ROIsExportCSV.java
Original file line number Diff line number Diff line change
@@ -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<Void, String> implements BigTraceBGWorker
{
private String progressState;
public BigTrace<T> 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<nRoiN; nRoi++)
{
//Sleep for up to one second.
try {
Thread.sleep(1);
} catch (InterruptedException ignore) {}
setProgress(nRoi*100/nRoiN);
if(bt.roiManager.rois.get( nRoi ) instanceof AbstractCurve3D)
{
nRoiCurrN++;
AbstractCurve3D currRoi = ((AbstractCurve3D)bt.roiManager.rois.get( nRoi ));
sRoiTP = Integer.toString( currRoi.getTimePoint() );
sRoiName = currRoi.getName();
sRoiType = Roi3D.intTypeToString( currRoi.getType());
sRoiGroup = bt.roiManager.groups.get(currRoi.getGroupInd()).getName();
ArrayList< RealPoint > points = currRoi.getJointSegmentResampled();
for(int nP=0; nP<points.size(); nP++)
{
writer.write( Integer.toString( nRoiCurrN ) +"," );
for (int d=0;d<3;d++)
{
writer.write( df3.format(points.get( nP ).getDoublePosition( d ) ) +"," );
}
writer.write( sRoiName + "," + sRoiType + "," + sRoiGroup + "," + sRoiTP + "\n");
}
}

}
writer.close();
}
setProgress(100);
setProgressState("exporting ROIs done.");
} catch (IOException e) {
IJ.log(e.getMessage());
//e.printStackTrace();
}
return null;
}
/*
* Executed in event dispatching thread
*/
@Override
public void done()
{
//unlock user interaction
bt.bInputLock = false;
bt.setLockMode(false);
setProgress(100);
setProgressState("export ROIs to CSV done.");
}
}
120 changes: 120 additions & 0 deletions src/main/java/bigtrace/io/ROIsExportSWC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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.BigTraceData;
import bigtrace.rois.AbstractCurve3D;

import ij.IJ;

public class ROIsExportSWC< T extends RealType< T > & NativeType< T > > extends SwingWorker<Void, String> implements BigTraceBGWorker
{
private String progressState;
public BigTrace<T> 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<nRoiN; nRoi++)
{

//Sleep for up to one second.
try {
Thread.sleep(1);
} catch (InterruptedException ignore) {}
setProgress(nRoi*100/nRoiN);
if(bt.roiManager.rois.get( nRoi ) instanceof AbstractCurve3D)
{
AbstractCurve3D currRoi = ((AbstractCurve3D)bt.roiManager.rois.get( nRoi ));
sRadius = df3.format( currRoi.getLineThickness()*0.5*BigTraceData.dMinVoxelSize);
ArrayList< RealPoint > points = currRoi.getJointSegmentResampled();
for(int nP=0; nP<points.size(); nP++)
{
writer.write( Long.toString( nPointCount ) +" 0 " );
for (int d=0;d<3;d++)
{
writer.write( df3.format(points.get( nP ).getDoublePosition( d ) ) +" " );
}
writer.write( sRadius + " ");
if(nP == 0)
{
writer.write( "-1 \n");

}
else
{
writer.write( Long.toString( nPointCount-1 ) + " \n");
}
nPointCount++;
}
}

}
writer.close();
}
setProgress(100);
setProgressState("exporting curve ROIs done.");
} catch (IOException e) {
IJ.log(e.getMessage());
//e.printStackTrace();
}
return null;
}
/*
* Executed in event dispatching thread
*/
@Override
public void done()
{
//unlock user interaction
bt.bInputLock = false;
bt.setLockMode(false);
setProgress(100);
setProgressState("export traces to SWC done.");
}


}
3 changes: 2 additions & 1 deletion src/main/java/bigtrace/io/ROIsSaveBG.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.RealType;

public class ROIsSaveBG < T extends RealType< T > & NativeType< T > > extends SwingWorker<Void, String> implements BigTraceBGWorker{
public class ROIsSaveBG < T extends RealType< T > & NativeType< T > > extends SwingWorker<Void, String> implements BigTraceBGWorker
{

private String progressState;
public BigTrace<T> bt;
Expand Down
102 changes: 81 additions & 21 deletions src/main/java/bigtrace/rois/RoiManager3DDialogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T> 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<T> 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<T> 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<T> exportSWCTask = new ROIsExportSWC<>();
exportSWCTask.sFilename = filename;
exportSWCTask.bt = this.bt;
exportSWCTask.addPropertyChangeListener(bt.btPanel);
exportSWCTask.execute();
break;
}
}


Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 785f0a4

Please sign in to comment.