diff --git a/screening/bsa_lab/czi-metadata-dropbox.py b/screening/bsa_lab/czi-metadata-dropbox.py index 0b5f7f77b06a17f8d93b94afec64a1020367f3c6..9a4920371036e0593e2492cd8b2357281220b1c1 100644 --- a/screening/bsa_lab/czi-metadata-dropbox.py +++ b/screening/bsa_lab/czi-metadata-dropbox.py @@ -11,8 +11,12 @@ from time import * from datetime import * ''' -Dropbox script for uploading a .lsm/ .czi file. Images contained in the file are displayed in openBIS and metadate is extracted from the lsm file and -stored in a file called "metadata.txt" uploaded in the dataset. The lsm or czi file needs to be put in a directory inside the incoming folder. The name of this directory is then taken as sample name for openBIS. +Dropbox script for uploading a .czi file. Images contained in the file are displayed in openBIS and metadata is extracted from the czi file and +stored in a file called "samplename-metadata.txt" uploaded in the dataset. The czi file needs to be put in a directory inside the incoming folder. +The name of this directory should be Y-X, where Y is name of the project (already existing in openBIS) and X the name of the experiment +where the sample will be uploaded. If the experiment does not exist yet, it will be created. +As many samples are created as many czi files are contained in the incoming directory. The samples are named like the czi file. + ''' @@ -22,74 +26,166 @@ d=datetime.now() print d.strftime("%Y-%m-%d %H:%M:%S GMT"+"%+.2d" % tz+":00") class BioFormatsHelper(ImageReader): - def tryToCreateReaderForFile(fileName): - for reader in ImageReader().getReaders(): - if reader.isThisType(fileName): - return reader - return None + def tryToCreateReaderForFile(fileName): + for reader in ImageReader().getReaders(): + if reader.isThisType(fileName): + return reader + return None def process(transaction): incoming = transaction.getIncoming() +#read each czi file contained in the incoming directory and extract all the metadata to a metadata.txt file +#extract also some specific metadata which will be set as properties in openBIS def readCzi(incoming): filename_list = {} for cziFile in glob.glob(os.path.join(incoming, '*.czi')): objective ='' scaling = None + z_step_size = None + pixel_dwell_time = None + channel_name_0 = '' + channel_name_1 = '' + channel_name_2 = '' + channel_name_3 = '' + channel_name_4 = '' + channel_name_5 = '' + channel_name_6 = '' + channel_name_7 = '' + detector_gain_0 = None + detector_gain_1 = None + detector_gain_2 = None + detector_gain_3 = None + detector_gain_4 = None + detector_gain_5 = None + detector_gain_6 = None + detector_gain_7 = None (dir, file) = os.path.split(cziFile) (filename, extension) = os.path.splitext(file) helper = BioFormatsHelper() reader = helper.getReader(cziFile) reader.setId(cziFile) - - globalMetadata = str(reader.getGlobalMetadata().toString()) - + + globalMetadata = str(reader.getGlobalMetadata().toString()) + list_globalMetadata = re.split(r"[,]",globalMetadata) metadataFile = dir+"/" + filename + "-metadata.txt" f = open(metadataFile, "a") for item in list_globalMetadata: - f.write(item) - f.write('\n') - if re.search ("=", item): - item_total = re.split(r"[=]", item) - item_label = item_total[0] - item_value = item_total[1] - if re.search ("Metadata Experiment ExperimentBlocks AcquisitionBlock AcquisitionModeSetup Objective 0", item_label): - objective = item_value + f.write(item) + f.write('\n') + if re.search ("=", item): + item_total = re.split(r"[=]", item) + item_label = item_total[0] + item_value = item_total[1] + if re.search ("Metadata Experiment ExperimentBlocks AcquisitionBlock AcquisitionModeSetup Objective 0", item_label): + objective = item_value - if re.search ("Metadata Experiment ExperimentBlocks AcquisitionBlock AcquisitionModeSetup ScalingX 0", item_label): - scaling = item_value + if re.search ("Metadata Experiment ExperimentBlocks AcquisitionBlock AcquisitionModeSetup ScalingX 0", item_label): + scaling = item_value + + if re.search ("Metadata Information Image Dimensions Z Positions Interval Increment 0", item_label): + z_step_size = item_value + + if re.search ("Metadata Experiment ExperimentBlocks AcquisitionBlock AcquisitionModeSetup PixelPeriod 0", item_label): + pixel_dwell_time = item_value + + if re.search ("Metadata Experiment ExperimentBlocks AcquisitionBlock MultiTrackSetup TrackSetup Detectors Detector ImageChannelName 0", item_label): + channel_name_0 = item_value + + if re.search ("Metadata Experiment ExperimentBlocks AcquisitionBlock MultiTrackSetup TrackSetup Detectors Detector ImageChannelName 1", item_label): + channel_name_1 = item_value + + if re.search ("Metadata Experiment ExperimentBlocks AcquisitionBlock MultiTrackSetup TrackSetup Detectors Detector ImageChannelName 2", item_label): + channel_name_2 = item_value + + if re.search ("Metadata Experiment ExperimentBlocks AcquisitionBlock MultiTrackSetup TrackSetup Detectors Detector ImageChannelName 3", item_label): + channel_name_3 = item_value + + if re.search ("Metadata Experiment ExperimentBlocks AcquisitionBlock MultiTrackSetup TrackSetup Detectors Detector ImageChannelName 4", item_label): + channel_name_4 = item_value + + if re.search ("Metadata Experiment ExperimentBlocks AcquisitionBlock MultiTrackSetup TrackSetup Detectors Detector ImageChannelName 5", item_label): + channel_name_5 = item_value + + if re.search ("Metadata Experiment ExperimentBlocks AcquisitionBlock MultiTrackSetup TrackSetup Detectors Detector ImageChannelName 6", item_label): + channel_name_6 = item_value + + if re.search ("Metadata Experiment ExperimentBlocks AcquisitionBlock MultiTrackSetup TrackSetup Detectors Detector ImageChannelName 7", item_label): + channel_name_7 = item_value + + if re.search ("Metadata Information Image Dimensions Channels Channel DetectorSettings Gain 0", item_label): + detector_gain_0 = item_value + + if re.search ("Metadata Information Image Dimensions Channels Channel DetectorSettings Gain 1", item_label): + detector_gain_1 = item_value + + if re.search ("Metadata Information Image Dimensions Channels Channel DetectorSettings Gain 2", item_label): + detector_gain_2 = item_value + + if re.search ("Metadata Information Image Dimensions Channels Channel DetectorSettings Gain 3", item_label): + detector_gain_3 = item_value + + if re.search ("Metadata Information Image Dimensions Channels Channel DetectorSettings Gain 4", item_label): + detector_gain_4 = item_value + + if re.search ("Metadata Information Image Dimensions Channels Channel DetectorSettings Gain 5", item_label): + detector_gain_5 = item_value + + if re.search ("Metadata Information Image Dimensions Channels Channel DetectorSettings Gain 6", item_label): + detector_gain_6 = item_value + + if re.search ("Metadata Information Image Dimensions Channels Channel DetectorSettings Gain 7", item_label): + detector_gain_7 = None + + f.close() reader.close(); - filename_list[filename] = { "objective" : objective, "scaling" : scaling} + filename_list[filename] = { "objective" : objective, "scaling" : scaling, "z_step_size" : z_step_size, "pixel_dwell_time" : pixel_dwell_time, "channel_name_0" : channel_name_0, "channel_name_1" : channel_name_1, "channel_name_2" : channel_name_2, "channel_name_3" : channel_name_3, "channel_name_4" : channel_name_4, "channel_name_5" : channel_name_5, "channel_name_6" : channel_name_6, "channel_name_7" : channel_name_7, "detector_gain_0" : detector_gain_0, "detector_gain_1" : detector_gain_1, "detector_gain_2" : detector_gain_2, "detector_gain_3" : detector_gain_3, "detector_gain_4" : detector_gain_4, "detector_gain_5" : detector_gain_5, "detector_gain_6" : detector_gain_6, "detector_gain_7" : detector_gain_7 } return filename_list cziInfo = readCzi(incoming.getPath()) - - directoryName = os.path.basename(incoming.getPath()) + + directoryName = os.path.basename(incoming.getPath()) names = re.split(r"[-]",directoryName) projectName = names[0] + "-IMG" experimentName = names[1] - + experiment = "/PANTAZIS_GROUP/" + projectName + "/" + experimentName + exp = transaction.getExperiment(experiment) + if not exp: + exp = transaction.createNewExperiment(experiment, 'IMAGING') for name in cziInfo.keys(): - print "name", name sampleIdentifier = "/PANTAZIS_GROUP/" + name + print "sample is", sampleIdentifier sample = transaction.createNewSample(sampleIdentifier, "MICROSCOPY_IMG") - experiment = "/PANTAZIS_GROUP/" + projectName + "/" + experimentName - exp = transaction.getExperiment(experiment) - if not exp: - exp = transaction.createNewExperiment(experiment, 'IMAGING') - sample.setExperiment(exp) sample.setPropertyValue("OBJECTIVE", cziInfo[name]["objective"]) sample.setPropertyValue("SCALING", cziInfo[name]["scaling"]) +# sample.setPropertyValue("Z_STEP_SIZE", cziInfo[name]["z_step_size"]) +# sample.setPropertyValue("PIXEL_DWELL_TIME", cziInfo[name]["pixel_dwell_time"]) +# sample.setPropertyValue("CHANNEL_NAME_0", cziInfo[name]["channel_name_0"]) +# sample.setPropertyValue("CHANNEL_NAME_1", cziInfo[name]["channel_name_1"]) +# sample.setPropertyValue("CHANNEL_NAME_2", cziInfo[name]["channel_name_2"]) +# sample.setPropertyValue("CHANNEL_NAME_3", cziInfo[name]["channel_name_3"]) +# sample.setPropertyValue("CHANNEL_NAME_4", cziInfo[name]["channel_name_4"]) +# sample.setPropertyValue("CHANNEL_NAME_5", cziInfo[name]["channel_name_5"]) +# sample.setPropertyValue("CHANNEL_NAME_6", cziInfo[name]["channel_name_6"]) +# sample.setPropertyValue("CHANNEL_NAME_7", cziInfo[name]["channel_name_7"]) +# sample.setPropertyValue("DETECTOR_GAIN_0", cziInfo[name]["detector_gain_0"]) +# sample.setPropertyValue("DETECTOR_GAIN_1", cziInfo[name]["detector_gain_1"]) +# sample.setPropertyValue("DETECTOR_GAIN_2", cziInfo[name]["detector_gain_2"]) +# sample.setPropertyValue("DETECTOR_GAIN_3", cziInfo[name]["detector_gain_3"]) +# sample.setPropertyValue("DETECTOR_GAIN_4", cziInfo[name]["detector_gain_4"]) +# sample.setPropertyValue("DETECTOR_GAIN_5", cziInfo[name]["detector_gain_5"]) +# sample.setPropertyValue("DETECTOR_GAIN_6", cziInfo[name]["detector_gain_6"]) +# sample.setPropertyValue("DETECTOR_GAIN_7", cziInfo[name]["detector_gain_7"]) imageDataset = SimpleImageContainerDataConfig() imageDataset.setPlate("PANTAZIS_GROUP", name) @@ -99,33 +195,14 @@ def process(transaction): imageDataset.setImageLibrary("BioFormats") imageDataset.setMeasuredData(True) - sampleNamePath = incoming.getPath() + '/' + name + '.czi' - metadataPath = incoming.getPath() + '/' + name + '-metadata.txt' - dataSet = transaction.createNewImageDataSet(imageDataset, File(sampleNamePath)) + sampleNamePath = incoming.getPath() + '/' + name + '.czi' + metadataPath = incoming.getPath() + '/' + name + '-metadata.txt' + dataSet = transaction.createNewImageDataSet(imageDataset, File(sampleNamePath)) transaction.moveFile(sampleNamePath, dataSet) transaction.moveFile(metadataPath, dataSet) -# transaction.moveFile(sampleNamePath, dataSet) - - -# def moveFiles(incoming): -# for cziFile in glob.glob(os.path.join(incoming, '*.czi')): -# (incoming, file) = os.path.split(cziFile) -# (filename, extension) = os.path.splitext(file) -# newDir = incoming + '/' + filename -# print "newdir", newDir -# if not os.path.exists(newDir): -# os.makedirs(newDir) -# shutil.move(incoming + '/' + file, newDir) -# for txtFile in glob.glob(os.path.join(incoming, '*.txt')): -# print "textfile", txtFile -# (incoming, file) = os.path.split(txtFile) -# (filename, extension) = os.path.splitext(file) -# shutil.move(incoming + '/' + file, newDir) -# -# -# moveFiles(incoming.getPath()) + @@ -133,8 +210,7 @@ def process(transaction): -# -# +