Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A clojure script for Fiji to help process the Diadem Neuromuscular data set
(import '(ij.io DirectoryChooser))
(import '(java.io File))
(import '(java.io FileWriter))
(import '(ij.plugin FolderOpener))
(import '(ij IJ))
(import '(ij.io FileSaver))
;; d should be a java.io.File object
(defn files-in-directory [d]
(seq (. d (listFiles))))
;; d should be a java.io.File object representing
;; a directory with a TIFF file for each slice,
;; starting at 000.tif
(defn pack-to-single-tiff [d]
(let [contents (files-in-directory d)
n (count contents)
first-file (.getAbsolutePath (new File d "000.tif"))
o (str (.getAbsolutePath d) ".tif")]
(if-not (.exists (new File o))
(do
;; Import the image sequence:
(ij.IJ/run "Image Sequence..." (str "open=" first-file " number=" n " starting=1 increment=1 scale=100 file=[] or=[] sort"))
;; It's a bad idea to just convert 16-bit to 8-bit in this
;; way, of course, so don't do it:
;; (ij.IJ/run "8-bit")
;; Correct the Z separation according to the instructions:
(let [i (IJ/getImage)
c (.getCalibration i)]
(set! (. c pixelDepth) (* 3.04 (.pixelWidth c)))
(.setCalibration i c)
;; ... and save as a single stack:
(let [saver (new FileSaver i)]
(.saveAsTiffStack saver o)
(.close i)))))
o))
;; These offsets are taken from:
;; http://www.diademchallenge.org/neuromuscular_projection_fibers_readme.html
(def filename-to-offset
{
"001.tif" '(7694 0 0)
"002.tif" '(9504 6 0)
"003.tif" '(10410 10 0)
"004.tif" '(13104 4 0)
"005.tif" '(7686 922 0)
"006.tif" '(8592 926 0)
"007.tif" '(9496 930 0)
"008.tif" '(10400 934 0)
"009.tif" '(13120 940 0)
"010.tif" '(14022 944 0)
"011.tif" '(14926 946 0)
"012.tif" '(2254 1866 0)
"013.tif" '(3164 1998 0)
"014.tif" '(4066 2106 0)
"015.tif" '(4966 1834 0)
"016.tif" '(5866 1838 0)
"017.tif" '(8578 1846 0)
"018.tif" '(9486 1852 0)
"019.tif" '(10394 1854 0)
"020.tif" '(13112 1864 0)
"021.tif" '(14014 1866 0)
"022.tif" '(14914 1868 0)
"023.tif" '(4960 2756 0)
"024.tif" '(4540 2456 0)
"025.tif" '(5860 2758 0)
"026.tif" '(6766 2714 0)
"027.tif" '(7668 2820 0)
"028.tif" '(8572 2730 0)
"029.tif" '(9482 2794 0)
"030.tif" '(10386 2766 0)
"031.tif" '(12198 2778 0)
"032.tif" '(13106 2780 0)
"033.tif" '(14006 2782 0)
"034.tif" '(14908 2784 0)
"035.tif" '(4954 3670 0)
"036.tif" '(5858 3674 0)
"037.tif" '(6766 3676 0)
"038.tif" '(7666 3678 0)
"039.tif" '(8570 3680 0)
"040.tif" '(9480 3684 0)
"041.tif" '(10384 3686 0)
"042.tif" '(11288 3690 0)
"043.tif" '(12194 3692 0)
"044.tif" '(13100 3694 0)
"045.tif" '(14000 3696 0)
"046.tif" '(14902 3698 0)
"047.tif" '(6754 4586 0)
"048.tif" '(8568 4592 0)
"049.tif" '(9472 4596 0)
"050.tif" '(10380 4600 0)
"051.tif" '(11286 4602 0)
"052.tif" '(13094 4606 0)
"053.tif" '(13994 4608 0)
"054.tif" '(14894 4610 0)
"055.tif" '(15802 4616 0)
"056.tif" '(6760 5520 0)
"057.tif" '(9470 5510 0)
"058.tif" '(10374 5512 0)
"059.tif" '(11280 5516 0)
"060.tif" '(11268 5508 0)
"061.tif" '(12180 5518 0)
"062.tif" '(13086 5520 0)
"063.tif" '(13988 5520 0)
"064.tif" '(14892 5524 0)
"065.tif" '(15794 5526 0)
"066.tif" '(16698 5528 0)
"067.tif" '(6750 6438 0)
"068.tif" '(7200 5942 0)
"069.tif" '(7656 6440 0)
"070.tif" '(10368 6430 0)
"071.tif" '(11272 6438 0)
"072.tif" '(12174 6442 0)
"073.tif" '(13080 6444 0)
"074.tif" '(13982 6442 0)
"075.tif" '(14882 6444 0)
"076.tif" '(15786 6448 0)
"077.tif" '(16690 6450 0)
"078.tif" '(17594 6452 0)
"079.tif" '(18500 6454 0)
"080.tif" '(7650 7362 0)
"081.tif" '(9452 7348 0)
"082.tif" '(10362 7350 0)
"083.tif" '(11266 7358 0)
"084.tif" '(12166 7362 0)
"085.tif" '(13084 7374 0)
"086.tif" '(13978 7360 0)
"087.tif" '(14878 7362 0)
"088.tif" '(15778 7368 0)
"089.tif" '(16680 7370 0)
"090.tif" '(17590 7372 0)
"091.tif" '(18494 7374 0)
"092.tif" '(5834 8286 0)
"093.tif" '(6736 8288 0)
"094.tif" '(7642 8288 0)
"095.tif" '(8546 8290 0)
"096.tif" '(9454 8292 0)
"097.tif" '(10438 8354 0)
"098.tif" '(13076 8306 0)
"099.tif" '(13968 8290 0)
"100.tif" '(14872 8292 0)
"101.tif" '(15774 8294 0)
"102.tif" '(16674 8300 0)
"103.tif" '(17580 8302 0)
"104.tif" '(17578 8264 0)
"105.tif" '(5828 9206 0)
"106.tif" '(6732 9202 0)
"107.tif" '(7634 9206 0)
"108.tif" '(8540 9210 0)
"109.tif" '(9444 9210 0)
"110.tif" '(13964 9208 0)
"111.tif" '(14864 9212 0)
"112.tif" '(15766 9212 0)
"113.tif" '(16668 9214 0)
"114.tif" '(6724 10122 0)
"115.tif" '(7626 10126 0)
"116.tif" '(7604 10116 0)
"117.tif" '(8530 10130 0)
"118.tif" '(8530 10104 0)
"119.tif" '(9436 10132 0)
"120.tif" '(14858 10132 0)
"121.tif" '(15760 10134 0)
"122.tif" '(16664 10136 0)
"123.tif" '(17566 10138 0)
"124.tif" '(17570 10122 0)
"125.tif" '(18472 10140 0)
"126.tif" '(6716 11046 0)
"127.tif" '(7620 11048 0)
"128.tif" '(8524 11052 0)
"129.tif" '(9432 11056 0)
"130.tif" '(10342 11060 0)
"131.tif" '(14854 11056 0)
"132.tif" '(15754 11058 0)
"133.tif" '(7618 11964 0)
"134.tif" '(8522 11968 0)
"135.tif" '(9428 11970 0)
"136.tif" '(10334 11976 0)
"137.tif" '(15746 11974 0)
"138.tif" '(16652 11976 0)
"139.tif" '(8516 12878 0)
"140.tif" '(9418 12880 0)
"141.tif" '(9412 13796 0)
"142.tif" '(2258 2754 0)
"143.tif" '(1306 3682 0)
"144.tif" '(800 4600 0)
"145.tif" '(416 5524 0)
"146.tif" '(252 6446 0)
"147.tif" '(0 7382 0)
"148.tif" '(1336 2772 0)
"149.tif" '(19400 7378 0)
"150.tif" '(20298 6886 0)
"151.tif" '(21262 6876 0)
"152.tif" '(12554 6636 0) } )
(let [dc (new DirectoryChooser "Open stacks directory")
d (.getDirectory dc)
df (new File d)
;; Only include directories made up of digits:
directories (filter (fn [x] (and (.isDirectory x)
(re-find #"^[0-9]+$" (.getName x))))
(seq (. df (listFiles))))
tiff-stacks (map pack-to-single-tiff directories)
tile-configuration-output-file (new File df "TileConfiguration.txt")]
(if (> (count directories) 0)
(let [out (new FileWriter tile-configuration-output-file)]
(.write out "dim = 3\n")
(doall (map #(let [tf (new File %1)
tfb (.getName tf)
offset (get filename-to-offset tfb)]
(.write out (str %1 "; ; (" (apply str (interpose ", " offset)) ")\n"))) tiff-stacks))
(.close out))
(ij.IJ/error "No directories found - you must select a directory that contains the directories 000, 001, etc.")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.