From a3ba03dc2a0c2e4ee91a98ffd2a37aedc589f686 Mon Sep 17 00:00:00 2001
From: barillac <barillac>
Date: Fri, 5 Sep 2014 08:31:33 +0000
Subject: [PATCH] added code for automatically generating FREEFORM_TABLE_STATE
 property based on the content of the data uploaded from the FACS

SVN: 32415
---
 .../drop-boxes/facs_data/import_facs.py       | 44 +++++++++++++++----
 1 file changed, 36 insertions(+), 8 deletions(-)

diff --git a/plasmid/source/drop-boxes/facs_data/import_facs.py b/plasmid/source/drop-boxes/facs_data/import_facs.py
index f72488ea919..5b75ff543fc 100644
--- a/plasmid/source/drop-boxes/facs_data/import_facs.py
+++ b/plasmid/source/drop-boxes/facs_data/import_facs.py
@@ -7,6 +7,9 @@
   This script creates a sample in openBIS for each FACS experiment and uploads the corresponding data in one dataset connected to the samples.
   The space where the sample should go is detected from the username. The project and experiment are detected from the name of the folders given by the user.
   The FACS experiment should be named by the users as PROJECT-EXPERIMENT-SAMPLE.  
+  The script also automatically assign teh FREEFORM_TABLE_STATE property, which creates tables in the ELN UI. If there are plates in the directories (these are directories named "96 Wells") 
+  it creates 96-well plates table (8x12); if there are .fcs files it creates a table with one row and as many columns as there are .fcs files. Multiple tables for one sample can be created.
+
   
 """
 
@@ -19,6 +22,7 @@ from loci.formats import ImageReader
 from time import *
 from datetime import *
 
+print "##############################################################"
 
 def process(transaction):
 	incoming = transaction.getIncoming()
@@ -41,14 +45,14 @@ def process(transaction):
 					path = re.split("/", line)
 					directoryToImport= incoming +"/"+ "/".join(path[:-1])
 					projectName = re.split("-",path[-1])[0]
-					if len(re.split("-",path[-1])) == 3:
+					if len(re.split("-",path[-1])) == 3: #in some cases the user specifies the sample name in the file name
 						experimentName = re.split("-",path[-1])[1]
 						sampleNameFile = re.split("-",path[-1])[2]
 						sampleName = re.split("_properties.oix", sampleNameFile)[0]
-					elif len(re.split("-",path[-1]))==2:
+					elif len(re.split("-",path[-1]))==2: #in some cases the sample name is not in the file name, and it will be generated by openBIS
 						experimentNameProp = re.split("-",path[-1])[1]
 						experimentName = re.split("_properties.oix", experimentNameProp)[0]
-						sampleName = "na"
+						sampleName = "na" 
 					
 
 					projectNameList.append(projectName)
@@ -62,7 +66,8 @@ def process(transaction):
 
 	getProjExpSample(incoming.getPath()) 
 
-	
+
+	#get the space according to the user	
 	for user in set (userNameList):
 		if user == "pontia":
 			space = "AARON"
@@ -71,33 +76,56 @@ def process(transaction):
 		elif user == "elfstrok":
 			space = "KRISTINA"
 
-
+	#get or create the project
 	for proj in set(projectNameList):
-		print space
 		project = transaction.getProject("/" + space + "/" + proj)
 		if not project:
 	 		project = transaction.createNewProject("/" + space + "/" + proj)
 
+	#get or create the experiment
 	for exp in set(experimentNameList):
 		experiment = transaction.getExperiment("/" + space  + "/" + proj + "/" + exp)
 		if not experiment:
   			experiment = transaction.createNewExperiment("/" + space  + "/" + proj + "/" + exp, "ANALYSIS")	
 
+  	#create te samples, upload the datasets for the sample, create the tables for the ELN UI
   	for sample, directory in zip(sampleNameList,directoryToImportList):
+		
+		fcs_number = len(glob.glob1(directory, '*.fcs'))
+		plate_number= len(glob.glob1(directory, '96 Well*'))  		
+		table =[]
+  		ilist=[]
+  		
+  		for i in range(1,fcs_number+1):
+  			ilist.append(i)
+
+  		s = "[\"" + "\",\"".join(str(x) for x in ilist) + "\"]"
+ 		t = "{\"name\":\"\",\"modelDetailed\":[" + s  + "],\"modelMini\":{\"rows\":[\"\"],\"columns\":" + s + "}}"
+ 		table.append(t)
+
+  		
+  		for p in range(1,plate_number+1):
+  			t = "{\"name\":\"\",\"modelDetailed\":[[null,null,null,null,null,null,null,null,null,null,null,null],[null,null,null,null,null,null,null,null,null,null,null,null],[null,null,null,null,null,null,null,null,null,null,null,null],[null,null,null,null,null,null,null,null,null,null,null,null],[null,null,null,null,null,null,null,null,null,null,null,null],[null,null,null,null,null,null,null,null,null,null,null,null],[null,null,null,null,null,null,null,null,null,null,null,null],[null,null,null,null,null,null,null,null,null,null,null,null]],\"modelMini\":{\"rows\":[null,null,null,null,null,null,null,null],\"columns\":[null,null,null,null,null,null,null,null,null,null,null,null]}}"
+  			table.append(t)
+  			
+  		freeform_table = "[" +','.join(map(str,table)) +"]"
+  		
   		if sample == "na":
   			sampleNew = transaction.createNewSampleWithGeneratedCode(space, "RESULT")
   			sampleNew.setExperiment(experiment)
+  			sampleNew.setPropertyValue("FREEFORM_TABLE_STATE", freeform_table)
   		else:
   			sampleNew = transaction.createNewSample("/" + space + "/" + sample, "RESULT" )
 			sampleNew.setExperiment(experiment)
-		
+			sampleNew.setPropertyValue("FREEFORM_TABLE_STATE", freeform_table)
+
+
 		dataSet = transaction.createNewDataSet()
 		dataSet.setDataSetType("FACS_DATA_RAW")
 		dataSet.setSample(sampleNew)
 		transaction.moveFile(directory, dataSet)					
 	
 
-	
 		
 
 	
-- 
GitLab