Skip to content
Snippets Groups Projects
import_facs.py 5.84 KiB
Newer Older
  • Learn to ignore specific revisions
  • #! /usr/bin/env python
    """
      Script for uploading FACS data for the YeastLab.
    
      FACS data are organized in one main folder which contains a folder with the username. Inside this folder there are folders and subfolders that correspond 
      to different FACS experiments. Inside the username folder there is a file called data_structure.ois which contains the structure of the FACS experiments.
      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.
    
    
      
    """
    
    import os, glob, re, csv, time, shutil
    from java.io import File
    from ch.systemsx.cisd.openbis.dss.etl.dto.api.v1 import SimpleImageDataConfig, ImageMetadata, Location
    from ch.systemsx.cisd.openbis.plugin.screening.shared.api.v1.dto import Geometry
    from ch.systemsx.cisd.openbis.dss.etl.dto.api.v1 import SimpleImageContainerDataConfig, ChannelColor
    from loci.formats import ImageReader
    from time import *
    from datetime import *
    
    
    print "##############################################################"
    
    
    def process(transaction):
    	incoming = transaction.getIncoming()
    	
    	projectNameList=[]
    	experimentNameList=[]
    	sampleNameList=[]
    	directoryToImportList=[]
    	userNameList=[]
    
    
    	# read the data_structure.ois file to get the structure of the FACS experiment. The name of the FACS experiment contains project, experiment, sample info for openBIS.
    	def getProjExpSample(incoming):
    		for userName in os.listdir(incoming):
    			if not userName.startswith('.'):
    				dataStructure = incoming + "/" + userName + "/data_structure.ois" 
    				f = open(dataStructure)
    				lines=f.readlines()
    				for line in lines:
    					path = re.split("/", line)
    					directoryToImport= incoming +"/"+ "/".join(path[:-1])
    					projectName = re.split("-",path[-1])[0]
    
    					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: #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]
    
    					
    
    					projectNameList.append(projectName)
    					experimentNameList.append(experimentName)
    					sampleNameList.append(sampleName)
    					directoryToImportList.append(directoryToImport)
    					userNameList.append(userName)
    
    		return userNameList, projectNameList, experimentNameList, sampleNameList, directoryToImportList
    					
    
    	getProjExpSample(incoming.getPath()) 
    
    
    	for user in set (userNameList):
    		if user == "pontia":
    			space = "AARON"
    		elif user == "ottozd":
    			space = "DIANA"
    		elif user == "elfstrok":
    			space = "KRISTINA"
    
    
    	for proj in set(projectNameList):
    		project = transaction.getProject("/" + space + "/" + proj)
    		if not project:
    	 		project = transaction.createNewProject("/" + space + "/" + proj)
    
    
    	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)
    
      			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)