From af3e489fed3e1fb9f9bf6f2436f3f80cbee949a9 Mon Sep 17 00:00:00 2001
From: Constantin Pape <constantin.pape@iwr.uni-heidelberg.de>
Date: Wed, 11 Sep 2019 17:11:46 +0200
Subject: [PATCH] Add Hernando's prospr transfer scripts

---
 .../ProSPr_copy_and_mirror.ijm                | 120 ++++++++++++++++++
 .../ProSPr_copy_and_mirror.sh                 |  21 +++
 2 files changed, 141 insertions(+)
 create mode 100644 registration/transfer_ProSPr_data/ProSPr_copy_and_mirror.ijm
 create mode 100644 registration/transfer_ProSPr_data/ProSPr_copy_and_mirror.sh

diff --git a/registration/transfer_ProSPr_data/ProSPr_copy_and_mirror.ijm b/registration/transfer_ProSPr_data/ProSPr_copy_and_mirror.ijm
new file mode 100644
index 0000000..16d9c1b
--- /dev/null
+++ b/registration/transfer_ProSPr_data/ProSPr_copy_and_mirror.ijm
@@ -0,0 +1,120 @@
+// Copy files from ProSPr to the EM working directory
+// Mirror in the x axis (because the SP8 does it)
+// Add dimension information
+// Threshold them (Not the reference)
+// Manage names
+
+// Hernando MV. Sep2019
+
+setBatchMode(true);
+// directory of the curated MEDs
+meds_dir = "/g/arendt/PrImR/ProSPr6/4SPM_Binarization/CuratedMEDs_Good/";
+// ProSPr reference
+ref_file = "/g/arendt/PrImR/ProSPr6/ProSPr6_Ref.tif";
+// segmented data
+trackem_dir = "/g/arendt/PrImR/ProSPr6/TrackEM/";
+// output directory
+outdir = "/g/arendt/EM_6dpf_segmentation/platy-browser-data/data/rawdata/prospr/";
+
+// read dictionary
+dic_file = "/g/arendt/EM_6dpf_segmentation/GenerationOfVirtualCells/ProSPr_VirtualCells/helper_files/OlegGenesDiccionary.txt";
+filestring=File.openAsString(dic_file);
+rows=split(filestring, "\n");
+badname=newArray(rows.length);
+goodname=newArray(rows.length);
+for(i=0; i<rows.length; i++){
+	columns=split(rows[i],",");
+	badname[i]=columns[0];
+	goodname[i]=columns[1];
+}
+
+// get a list of the med files
+meds_files = getFileList(meds_dir);
+filenames = newArray();
+for (i=0;i<meds_files.length;i++){
+	subfiles = getFileList(meds_dir+meds_files[i]);
+	for (j=0;j<subfiles.length;j++){
+		if (endsWith(subfiles[j],"_MEDs.tif")){
+			pieces = split(subfiles[j],"_");
+			// avoid inserting Pty2
+			if (pieces[0] != "Pty2"){
+				filenames = Array.concat(filenames,meds_dir+meds_files[i]+"/"+subfiles[j]);
+			}
+		}
+	}
+}
+// add the segmented data
+trackem_files = getFileList(trackem_dir);
+for (i=0;i<trackem_files.length;i++){
+	subfiles = getFileList(trackem_dir+trackem_files[i]);
+	for (j=0;j<subfiles.length;j++){
+		if (endsWith(subfiles[j],"_MEDs.tif")){
+			pieces = split(subfiles[j],"_");
+			// avoid inserting MB
+			if (pieces[0] != "MB"){
+				filenames = Array.concat(filenames,trackem_dir+trackem_files[i]+"/"+subfiles[j]);
+			}
+		}
+	}
+}
+
+for (i = 0; i < filenames.length; i++) {
+	// open
+	open(filenames[i]);
+	f_name = getTitle();
+	print(f_name);
+	// flip
+	run("Flip Horizontally", "stack");
+	// change properties
+	run("Properties...", "channels=1 slices=251 frames=1 unit=micrometers pixel_width=0.55 pixel_height=0.55 voxel_depth=0.55");
+	// threshold
+	setThreshold(1, 255); 
+	setOption("BlackBackground", false);
+	run("Convert to Mask", "stack");
+	// save
+	// parse title
+	pieces = split(f_name, "_");
+	// find index of name
+	idx = getIndex(pieces[0], badname);
+	print(idx);
+	// get correspondent name
+	if(idx<9000){
+		genename = goodname[idx];
+		print(genename);
+		new_name = pieces[0] + "-" + genename + "--prosprspaceMEDs.tif";
+	}
+	else{
+		new_name = pieces[0] + "--prosprspaceMEDs.tif";
+	}
+	// correct Hox5
+	if(pieces[0]=="Hox5"){
+		new_name = "Hox4--prosprspaceMEDs.tif";
+	}
+	saveAs("Tiff", outdir + new_name);
+	close(new_name);
+}
+
+// copy the reference
+open(ref_file);
+print(getTitle());
+// flip
+run("Flip Horizontally", "stack");
+// change properties
+run("Properties...", "channels=1 slices=251 frames=1 unit=micrometers pixel_width=0.55 pixel_height=0.55 voxel_depth=0.55");
+// save
+new_name = "ProSPr6-Ref--prosprspaceMEDs.tif";
+saveAs("Tiff", outdir + new_name);
+close(new_name);
+
+run("Close All");
+print("DONE");
+run("Quit");
+
+function getIndex(word, array){
+	for (i = 0; i < array.length; i++) {
+		if (word == array[i]) {
+			return(i);		
+		}
+	}
+	return(10000); // could not figure out how to make NaN work...
+}
diff --git a/registration/transfer_ProSPr_data/ProSPr_copy_and_mirror.sh b/registration/transfer_ProSPr_data/ProSPr_copy_and_mirror.sh
new file mode 100644
index 0000000..5672365
--- /dev/null
+++ b/registration/transfer_ProSPr_data/ProSPr_copy_and_mirror.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+#SBATCH -o "/g/arendt/EM_6dpf_segmentation/platy-browser-data/registration/transfer_ProSPr_data/copandmir.out"
+#SBATCH -e "/g/arendt/EM_6dpf_segmentation/platy-browser-data/registration/transfer_ProSPr_data/copandmir.err"
+#SBATCH --mem 16000
+#SBATCH -c 4
+#SBATCH -N 1
+#SBATCH -n 1
+#SBATCH -p htc
+#SBATCH -t 400
+echo "starting job"
+touch "/g/arendt/EM_6dpf_segmentation/platy-browser-data/registration/transfer_ProSPr_data/copandmir.started"
+START_TIME=$SECONDS
+ulimit -c 0
+hostname
+head -1 /proc/meminfo
+module load X11
+module load Java
+xvfb-run -a -e "/g/arendt/EM_6dpf_segmentation/platy-browser-data/registration/transfer_ProSPr_data/copandmir.err" /g/almf/software/Fiji.app/ImageJ-linux64 -batch "/g/arendt/EM_6dpf_segmentation/platy-browser-data/registration/transfer_ProSPr_data/ProSPr_copy_and_mirror.ijm"
+ELAPSED_TIME=$(($SECONDS - $START_TIME))
+echo "job finished"
+touch "/g/arendt/EM_6dpf_segmentation/platy-browser-data/registration/transfer_ProSPr_data/copandmir.finished"
\ No newline at end of file
-- 
GitLab