/*-
* #%L
* Fiji distribution of ImageJ for the life sciences.
* %%
* Copyright (C) 2007 - 2017 Fiji developers.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
package mpicbg.spim;
import java.io.BufferedReader;
import java.io.File;
import java.io.FilenameFilter;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import mpicbg.spim.io.ConfigurationParserGeneral;
import mpicbg.spim.io.IOFunctions;
import mpicbg.spim.io.ProgramConfiguration;
import mpicbg.spim.io.TextFileAccess;
public class ClusterReconstruction
{
public static void main(String[] args)
{
// read&parse configuration file
ProgramConfiguration conf = null;
try
{
conf = ConfigurationParserGeneral.parseFile( "config/configuration.txt" );
} catch (Exception e)
{
IOFunctions.printErr("Cannot open configuration file: " + e);
e.printStackTrace();
return;
}
conf.printParameters();
for (int timePointIndex = 0; timePointIndex < conf.timepoints.length; timePointIndex++)
{
//
// create spim configuration file
//
String spimConfigFile = conf.baseFolder + conf.configFolder + conf.configFileTemplate + "_" + timePointIndex + ".txt";
String spimConfigTemplate = conf.baseFolder + conf.configFolder + conf.configFileTemplate;
try
{
BufferedReader in = TextFileAccess.openFileRead(spimConfigTemplate);
PrintWriter out = TextFileAccess.openFileWrite(spimConfigFile);
while (in.ready())
{
String line = in.readLine();
if (line.startsWith("Time Point Pattern = "))
line = "Time Point Pattern = \"" + conf.timepoints[timePointIndex] + "\"";
out.println(line);
}
in.close();
out.close();
}
catch (Exception e)
{
IOFunctions.printErr("Cannot read spim configuration template from: '" + spimConfigTemplate + "'");
IOFunctions.printErr("Cannot write spim configuration to: '" + spimConfigFile + "'");
IOFunctions.printErr(e);
e.printStackTrace();
System.exit(0);
}
//
// write job files
//
String jobFile = conf.baseFolder + conf.jobFolder + "jobspim_" + timePointIndex;
try
{
PrintWriter job = TextFileAccess.openFileWrite(jobFile);
job.println("#!/bin/sh");
job.println("#$ -S /bin/sh");
job.println("cd " + conf.baseFolder);
job.println(conf.javaExecutable + " " + conf.javaArguments + " -cp " + conf.binariesFolder +
getLibraries(conf.baseFolder + conf.librariesFolder) + ":/home/tomancak/ImageJ/ij.jar" +
" mpi.fruitfly.spim.Reconstruction " + spimConfigFile);
job.close();
}
catch (Exception e)
{
IOFunctions.printErr("Cannot write spim job file: '" + jobFile + "'");
IOFunctions.printErr(e);
e.printStackTrace();
System.exit(0);
}
File f = new File(jobFile);
setReadable(f, true, false);
setWritable(f, true, false);
setExecutable(f, true, false);
}
//
// write submission script
//
String submissionFile = conf.baseFolder + "submitjobs";
try
{
PrintWriter submissionScript = TextFileAccess.openFileWrite(submissionFile);
submissionScript.println("#!/bin/sh");
submissionScript.println("");
for (int timePointIndex = 0; timePointIndex < conf.timepoints.length; timePointIndex++)
submissionScript.println("qsub -P spim " + conf.baseFolder + conf.jobFolder + "jobspim_" + timePointIndex);
submissionScript.close();
}
catch (Exception e)
{
IOFunctions.printErr("Cannot write submission file: '" + submissionFile + "'");
IOFunctions.printErr(e);
e.printStackTrace();
System.exit(0);
}
File f = new File(submissionFile);
setReadable(f, true, false);
setWritable(f, true, false);
setExecutable(f, true, false);
}
protected static String getLibraries(String directory)
{
String libString = "";
String[] libraries = ClusterReconstruction.getDirectoryListingOfFiles(directory, ".jar");
for (String lib : libraries)
libString = libString + ":" + lib;
return libString;
}
private static String[] getDirectoryListingOfFiles(String directory, final String filterFileName)
{
FilenameFilter filter = new FilenameFilter()
{
public boolean accept( File dir, String name )
{
if (name.toUpperCase().endsWith(filterFileName.toUpperCase()))
{
File f = new File(dir, name);
if (f.isFile())
return true;
else
return false;
}
else
{
return false;
}
}
};
File dir = new File(directory);
String[] children = dir.list(filter);
java.util.Arrays.sort(children);
for (int i = 0; i < children.length; i++)
{
children[i] = dir + "/" + children[i];
children[i] = children[i].replace('\\', '/');
}
return children;
}
public static void getMovements(String directory, String extension)
{
String[] regFiles = getDirectoryListingOfFiles(directory, extension);
IOFunctions.println("Timepoint" + "\t" + "x" + "\t" + "y" + "\t" + "z");
String lasttimepoint = "";
for (String file : regFiles)
{
float x = 0, y = 0, z = 0;
String timepoint = file.substring(file.indexOf("TL")+2, file.indexOf("Angle") - 1);
try
{
BufferedReader in = TextFileAccess.openFileRead(file);
while (in.ready())
{
String line = in.readLine().trim();
if (line.startsWith("m03:"))
x = Float.parseFloat(line.substring(line.indexOf(":") + 1, line.length()));
else if (line.startsWith("m13:"))
y = Float.parseFloat(line.substring(line.indexOf(":") + 1, line.length()));
else if (line.startsWith("m23:"))
z = Float.parseFloat(line.substring(line.indexOf(":") + 1, line.length()));
}
in.close();
if (!timepoint.equals(lasttimepoint))
IOFunctions.println(timepoint + "\t" + x + "\t" + y + "\t" + z);
lasttimepoint = timepoint;
}
catch (Exception e)
{
IOFunctions.printErr("Cannot read file: " + file);
e.printStackTrace();
System.exit(0);
}
}
}
public static void getDisplacements(String directory)
{
String[] regFiles = getDirectoryListingOfFiles(directory, ".registration");
IOFunctions.println("Timepoint" + "\t" + "avg" + "\t" + "min" + "\t" + "max");
String lasttimepoint = "";
for (String file : regFiles)
{
float min = 0, avg = 0, max = 0;
String timepoint = file.substring(file.indexOf("TL")+2, file.indexOf("Angle") - 1);
try
{
BufferedReader in = TextFileAccess.openFileRead(file);
while (in.ready())
{
String line = in.readLine().trim();
if (line.startsWith("minError:"))
min = Float.parseFloat(line.substring(line.indexOf(":") + 1, line.length()));
else if (line.startsWith("maxError:"))
max = Float.parseFloat(line.substring(line.indexOf(":") + 1, line.length()));
else if (line.startsWith("avgError:"))
avg = Float.parseFloat(line.substring(line.indexOf(":") + 1, line.length()));
}
in.close();
if (!timepoint.equals(lasttimepoint))
IOFunctions.println(timepoint + "\t" + avg + "\t" + min + "\t" + max);
lasttimepoint = timepoint;
}
catch (Exception e)
{
IOFunctions.printErr("Cannot read file: " + file);
e.printStackTrace();
System.exit(0);
}
}
}
protected static Method setReadable, setWritable, setExecutable;
static {
try {
Class file = Class.forName("java.io.File");
Class[] types = { boolean.class, boolean.class };
setReadable = file.getMethod("setReadable", types);
setWritable = file.getMethod("setWritable", types);
setExecutable = file.getMethod("setExecutable", types);
} catch (Exception e) { /* ignore */ }
}
protected static boolean setReadable(File file, boolean readable, boolean ownerOnly)
{
try {
return ((Boolean)setReadable.invoke(file, new Object[] { readable, ownerOnly })).booleanValue();
} catch (Exception e) { return false; }
}
protected static boolean setWritable(File file, boolean writable, boolean ownerOnly)
{
try {
return ((Boolean)setWritable.invoke(file, new Object[] { writable, ownerOnly })).booleanValue();
} catch (Exception e) { return false; }
}
protected static boolean setExecutable(File file, boolean executable, boolean ownerOnly)
{
try {
return ((Boolean)setExecutable.invoke(file, new Object[] { executable, ownerOnly })).booleanValue();
} catch (Exception e) { return false; }
}
}