User Tools

Site Tools


documents:101029imagej_cui_lifconversion

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
documents:101029imagej_cui_lifconversion [2010/10/29 15:13] – created kotadocuments:101029imagej_cui_lifconversion [2010/10/29 15:54] – added macro kota
Line 1: Line 1:
 ====== Command Line: LIF to Tiff conversion ====== ====== Command Line: LIF to Tiff conversion ======
  
-LIF is a grouped-stack file format containing multiple stacks. One could access each of these stacks by LOCI Bio-Format plugin on desktop, but when file size is huge and data is in data server, data transfer rate becomes a bottle neck for the work. +LIF is a grouped-stack file format containing multiple stacks. One could access each stack by [[http://www.loci.wisc.edu/bio-formats/imagej|LOCI Bio-Format plugin installed to ImageJ]], but when file size is huge and data is in data server, data transfer rate becomes a bottle neck for the progress of work. 
  
-TO avoid this, one could do the file conversion via command line. This could be done by "bfconvert" command line tool available from Bio-format web site, but its problem is that physical scales in x, y and z are not read out from LIF so that resulting Tiff stacks are devoid of these parameters. In addition, dimensional values such as number of slices per time point, and number of time points are not converted.+TO avoid this, one could do the file conversion via command line. This could be done by [[http://www.farsight-toolkit.org/wiki/FARSIGHT_Tutorials/Bio-Formats|"bfconvert" command line tool]] available from Bio-format web site. Following command will do the jobconverting all.  
 +<code> 
 +sh /g/almf/software/bftools/bfconvert <filename.lif>   converted_%s_%n_C%c.tiff 
 +</code>  
 +One missing function with this CL tool is that physical scales in x, y and z are not read out from LIF so that resulting Tiff stacks are devoid of these parameters. In addition, dimensional values such as number of slices per time point, and number of time points are not converted.
  
-For this reason, use following shell script. +For this reason, following shell script was created to transfer physical scales in together with conversion
 <code> <code>
 sh /g/almf/software/ij/lifcon.sh </full/path/to/file.lif> sh /g/almf/software/ij/lifcon.sh </full/path/to/file.lif>
-<code>+</code>
 This script will extract metadata from LIF, convert each stack to Tiff stack, and set the physical parameters of the image by extracting these values from metadata. Tiff stack will be saved under a newly created directly where the LIF is located. Name of the directory will be This script will extract metadata from LIF, convert each stack to Tiff stack, and set the physical parameters of the image by extracting these values from metadata. Tiff stack will be saved under a newly created directly where the LIF is located. Name of the directory will be
  
Line 15: Line 19:
 A text file named A text file named
  
-<filename.lif>.meta.txt is also generated, where extracted metadata is stored. +<filename.lif>.meta.txt  
 + 
 +is also generated, where extracted metadata is stored.  
 + 
 +===== How Metadata is Extracted ===== 
 + 
 +Bioformat commandline tool "**showinf**" is used to save metadata as a text file. Then During the extraction of tiff stacks, physical scales and dimensionality information is extracted from the text and applied to the tiff stack.  
 + 
 +Since macro command run("Properties...", arg1, arg2) does not work when IJ is headless since Properties command uses AWT. To avoid this, a small plugin (that does not appear in the menu) was created for setting image properties. This plugin is called from macro to set physical scales. 
 + 
 +Link for the Image Property setter.   
 + 
 +===== Shell Script lifcon.sh ===== 
 + 
 +<sxh shell> 
 +#!/bin/sh 
 +#script for converting Lif file to tif, and set physical scale.   
 +
 +imgfullpath=$1 
 + 
 +# path to IJ jar file 
 +IJJARS="/g/almf/software/ij" 
 + 
 +# path to image  
 +IMGFILE="${imgfullpath##*/}" 
 +IMGPATH="${imgfullpath%/*}" 
 +METAFULLPATH="${imgfullpath}.meta.txt" 
 +echo ${METAFULLPATH} 
 + 
 +# Lif converting IJ macro name 
 +IJMACRONAME="LifOpenerCUI.ijm" 
 + 
 +#timer 
 +jobstart=$(date +%s) 
 + 
 +sh /g/almf/software/bftools/showinf -nopix -nocore ${imgfullpath}>${METAFULLPATH} 
 + 
 +chmod ugo+x ${METAFULLPATH} 
 + 
 +/usr/struct/bin/java -cp ${IJJARS}/headless.jar:${IJJARS}/ij-1.44h.jar -Djava.awt.headless=true ij.ImageJ -ijpath ${IJ 
 +JARS} -batch ${IJJARS}/${IJMACRONAME} ${imgfullpath} 
 + 
 +# timer 
 +jobend=$(date +%s) 
 +echo "Time: $((jobend-jobstart)) secs." 
 + 
 +</sxh> 
 + 
 +===== bfconvert help ===== 
 + 
 +<quote> 
 +To convert a file between formats, run: 
 +  bfconvert [-debug] [-stitch] [-separate] [-merge] [-expand] 
 +    [-bigtiff] [-compression codec] [-series series] [-map id] 
 +    in_file out_file 
 + 
 +      -debug: turn on debugging output 
 +     -stitch: stitch input files with similar names 
 +   -separate: split RGB images into separate channels 
 +      -merge: combine separate channels into RGB image 
 +     -expand: expand indexed color to RGB 
 +    -bigtiff: force BigTIFF files to be written 
 +-compression: specify the codec to use when saving images 
 +     -series: specify which image series to convert 
 +        -map: specify file on disk to which name should be mapped 
 + 
 +If any of the following patterns are present in out_file, they will 
 +be replaced with the indicated metadata value from the input file. 
 + 
 +   Pattern:     Metadata value: 
 +   --------------------------- 
 +   %s           series index 
 +   %n           series name 
 +   %c           channel index 
 +   %w           channel name 
 +   %z           Z index 
 +   %t           T index 
 + 
 +If any of these patterns are present, then the images to be saved 
 +will be split into multiple files.  For example, if the input file 
 +contains 5 Z sections and 3 timepoints, and out_file is 
 + 
 +  converted_Z%z_T%t.tiff 
 + 
 +then 15 files will be created, with the names 
 + 
 +  converted_Z0_T0.tiff 
 +  converted_Z0_T1.tiff 
 +  converted_Z0_T2.tiff 
 +  converted_Z1_T0.tiff 
 +  ... 
 +  converted_Z4_T2.tiff 
 + 
 +Each file would have a single image plane. 
 +</quote> 
 + 
 +===== LifOpenerCUI.ijm ===== 
 + 
 +ImageJ macro for converting LIF stack series to multiple tiff stacks, together with setting physical parameters.  
 +<code> 
 +// converts LIF file to channel-separated tif stacks.  
 +// output files will be saved under the folder <LIF file name>_tifStack 
 + 
 +//bach command example 
 +/* 
 +/usr/struct/bin/java -cp /g/almf/software/ij/headless.jar:/g/almf/software/ij/ij-1.44h.jar -Djava.awt.headless=true ij.ImageJ -ijpath /g/almf/software/ij -batch /g/almf/software/ij/LifOpenerCUI.ijm /g/almf/miura/lif/laminedapi23012.lif 
 +*/ 
 +// TODO: scale should be set as the propertiy of tiff file.  
 +// reading out of the scale from LIF is somehow not working, unlike it was done in 2009 
 +// metadata of .lif file would be saved as <filename.lif>.meta.txt 
 +//will be saved in the same directory.  
 +// TODO: program might return error if sequence is terminated abruptly.  
 + 
 +// for setting scale, plugin " ImpProp.class" is required. 
 + 
 + srcfile = getArgument(); //cui 
 +// srcfile = File.openDialog("Select a LIF File"); //gui  
 + requires("1.43d"); 
 + run("Bio-Formats Macro Extensions"); 
 + 
 + path = srcfile; 
 + name = File.getName(path); 
 + dir = File.getParent(path);  
 + DAPIch = 0;//getNumber("DAPI ch=?", 0); 
 + FISHch = 1;//getNumber("FISH ch=?", 1); 
 + metaname = name + ".meta.txt"; 
 + 
 + q = File.separator; //090912 
 + metafullpath = dir+q+metaname; 
 + metastr = File.openAsString(metafullpath); 
 + workdir = File.getParent(srcfile);  //getDirectory("Choose a work space directory to save resulting files"); 
 + 
 + //create "tiffStack" folder under the gene folder (tiff stacks will have series No. and channel so no overlaps) 
 + pathtifstack = workdir + q + name+ "_tifStack"; print(pathtifstack ); 
 + if (File.isDirectory(pathtifstack)==0) File.makeDirectory(pathtifstack); 
 + 
 + Ext.setId(path); 
 + Ext.getSeriesCount(seriesCount); 
 + Ext.getCurrentFile(file); 
 + Ext.close();  
 + print("File:"+ file); 
 + print("series total number" + seriesCount); 
 + 
 + for (s=0; s<seriesCount; s++) { 
 + 
 + seriesNum = s; 
 + //Ext.setId(path); //out 090925 v4n 
 + //Ext.setSeries(seriesNum); 
 + //Ext.getSeriesName(seriesName); 
 + //Ext.close(); //out 090925 v4n 
 + 
 + if (s>0) refresh =0; 
 + 
 + seriesName = OpenLIFSeriesOneChannel(path, name, seriesNum, DAPIch, 0, metastr); //modified 090925 
 + G_GID = getImageID(); 
 + savepath = pathtifstack + q+getTitle(); 
 + print(savepath); 
 + saveAs("tiff", savepath); 
 + close(); 
 + 
 + OpenLIFSeriesOneChannel(path, name, seriesNum, FISHch, 0, metastr); 
 + G_RID = getImageID();  
 + savepath = pathtifstack  + q+ getTitle(); 
 + print(savepath); 
 + saveAs("tiff", savepath); 
 + close(); 
 +
 + 
 + 
 +//working  maybe memory flashing problem, but not sure.  
 +/* 090907  
 + 090908 
 + - save tif file in a specified directory 
 + - problem with DAPI appearance in the resulting window.  ---> seperate 2D analysis files in other macro? 
 +*/ 
 +function OpenLIFSeriesOneChannel(id, name, seriesNum, ch, datasetOpened, metastr){ 
 + run("Bio-Formats Macro Extensions"); 
 + 
 + //if (datasetOpened ==0) Ext.setId(id); 
 + Ext.setId(id); 
 + Ext.setSeries(seriesNum); 
 + Ext.getSeriesName(seriesName); print(seriesName); 
 + Ext.getSizeZ(sizeZ); 
 + Ext.getSizeC(sizeC); 
 + Ext.getSizeT(sizeT); 
 + Ext.getImageCount(imageCount); 
 + print("ImageCount:"+imageCount); 
 + calculatedCount = sizeZ*sizeC*sizeT; 
 + print("...calculated"+sizeZ*sizeC*sizeT); 
 + if (imageCount != calculatedCount) { 
 + print(); 
 + exit(); 
 +
 + sizeT = imageCount/sizeC/sizeZ; 
 + print("C:"+sizeC+" Z:"+sizeZ+" T:"+sizeT); 
 + newname = name+"_"+seriesNum+"_ch"+ch+".tif"; 
 + setBatchMode(true); 
 + for (j=0; j<sizeT; j++){  
 + for (i=0; i<sizeZ; i++){ 
 + currentZch0 = i*sizeC; 
 + currentPlane = j*sizeZ*sizeC + i*sizeC; 
 + Ext.openImage("t"+j+"_z"+i, currentPlane+ch); 
 + if ((i==0) && (j==0)) 
 + stackID=getImageID(); 
 + else { 
 + run("Copy"); 
 + close; 
 + selectImage(stackID); 
 + run("Add Slice"); 
 + run("Paste"); 
 +
 + //print(currentPlane); 
 +
 +
 + rename(newname); 
 + xscale = returnXscale(metastr); 
 + yscale = returnYscale(metastr); 
 + zscale = returnZscale(metastr);  
 + tscale = 1;//returnTscale(metastr);  
 + xscalemicron = parseFloat(xscale) * pow(10, 6); 
 + yscalemicron = parseFloat(yscale) * pow(10, 6); 
 + zscalemicron = parseFloat(zscale) * pow(10, 6); 
 + op = "channels=1 slices="+sizeZ+" frames="+sizeT+" unit=micron pixel_width="+xscalemicron +" pixel_height="+yscalemicron +" voxel_depth="+zscalemicron +" frame=[0 sec] origin=0,0"; 
 + //since run("Properties...", op); cannot be used (AWT problem in headless) 
 + //follwoing is a new plugin for setting image properties.  
 + // ImpProp.class 
 + SsizeC = ""+1; 
 + SsizeZ = ""+sizeZ; 
 + SsizeT = ""+sizeT; 
 + Sxscalemicron = ""+xscalemicron; 
 + Syscalemicron = ""+yscalemicron; 
 + Szscalemicron = ""+zscalemicron;  
 + call("ImpProps.setCalibration",  
 + SsizeC, SsizeZ, SsizeT, "micron",  
 + Sxscalemicron, Syscalemicron, Szscalemicron); 
 + 
 + setBatchMode(false); 
 + //if (datasetOpened ==0) Ext.close(); 
 + Ext.close();  
 + return seriesName;  
 +
 + 
 +//zscale in micron 
 +function returnZscale(metastr){ 
 + metaA = split(metastr, "\n"); 
 + zscale = 1; 
 + for (i=0; i<metaA.length; i++){ 
 + if (startsWith(metaA[i], "HardwareSetting|ScannerSettingRecord|dblStepSize")){ 
 + lineA = split(metaA[i], " "); 
 + //for (j=0; j<lineA.length; j++)print(lineA[j]); 
 + if (lineA.length>=3) { 
 + zscale = parseFloat(lineA[2]); 
 + print(zscale); 
 +
 +
 +
 + return zscale;  
 +
 + 
 +//Xscale in micron 
 +function returnXscale(metastr){ 
 + metaA = split(metastr, "\n"); 
 + xscale = 1; 
 + for (i=0; i<metaA.length; i++){ 
 + if (startsWith(metaA[i], "HardwareSetting|ScannerSettingRecord|dblVoxelX")){ 
 + lineA = split(metaA[i], " "); 
 + //for (j=0; j<lineA.length; j++)print(lineA[j]); 
 + if (lineA.length>=3) {  
 + xscale = parseFloat(lineA[2]); 
 + print(xscale); 
 +
 +
 +
 + return xscale;  
 +
 + 
 +function returnYscale(metastr){ 
 + metaA = split(metastr, "\n"); 
 + yscale = 1; 
 + for (i=0; i<metaA.length; i++){ 
 + if (startsWith(metaA[i], "HardwareSetting|ScannerSettingRecord|dblVoxelY")){ 
 + lineA = split(metaA[i], " "); 
 + //for (j=0; j<lineA.length; j++)print(lineA[j]); 
 + if (lineA.length>=3){ 
 + yscale = parseFloat(lineA[2]); 
 + print(yscale); 
 +
 +
 +
 + return yscale;  
 +}
  
 +function returnTscale(){
 + metaA = split(metastr, "\n");
 + for (i=0; i<metaA.length; i++){
 + if (startsWith(metaA[i], "HardwareSetting|ScannerSettingRecord|dblStepSize")){
 + lineA = split(metaA[i], " ");
 + //for (j=0; j<lineA.length; j++)print(lineA[j]);
 + if (lineA.length>=3) print(parseFloat(lineA[2]));
 + }
 + }  
 +}
 +</code> 
documents/101029imagej_cui_lifconversion.txt · Last modified: 2020/11/26 09:11 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki