def _getChildSamples(parentSampleType, parentSamplePermId, sampleType): """Return the samples of given type for specified parent sample.""" # The samples are of type 'sampleType' searchCriteria = SearchCriteria() searchCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, sampleType)) # The samples have given parent expSampleCriteria = SearchCriteria() expSampleCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, parentSampleType)) expSampleCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, parentSamplePermId)) searchCriteria.addSubCriteria( SearchSubCriteria.createSampleParentCriteria(expSampleCriteria)) # Now search samples = searchService.searchForSamples(searchCriteria) # Return the samples return samples
def _getDataSetsForSample(sampleIdentifier, dataSetType): """Return the dataSet of given type for specified sample.""" # Set search criteria to retrieve the dataSet associated with the sample searchCriteria = SearchCriteria() searchCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, dataSetType) ) sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.CODE, sampleIdentifier) ) searchCriteria.addSubCriteria( SearchSubCriteria.createSampleCriteria( sampleCriteria) ) dataSetList = searchService.searchForDataSets(searchCriteria) if len(dataSetList) != 1: [] # Return the dataSet return dataSetList
def _retrieveAllSamplesWithType(self, sampleType): """ Retrieve all samples belonging to current experiment sample and collection having specified type. """ # The samples are of type 'sampleType' searchCriteria = SearchCriteria() searchCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, sampleType)) # The samples have parent _EXPERIMENT_SAMPLE expSampleCriteria = SearchCriteria() expSampleCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, self._expSampleType)) expSampleCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, self._expSamplePermId)) searchCriteria.addSubCriteria( SearchSubCriteria.createSampleParentCriteria(expSampleCriteria)) # Now search samples = searchService.searchForSamples(searchCriteria) # Return the samples return samples
def _retrieveAllSamplesWithTypeAndParent(self, sampleType, parentSampleId, parentSampleType): """ Retrieve all samples belonging to current experiment sample and collection having specified type. """ if _DEBUG: self._logger.info("Retrieving samples of type " + sampleType + " with parent sample with id " + parentSampleId + " and type " + parentSampleType) # The samples are of type 'sampleType' searchCriteria = SearchCriteria() searchCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, sampleType)) # The samples have given parent expSampleCriteria = SearchCriteria() expSampleCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, parentSampleType)) expSampleCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.CODE, parentSampleId)) searchCriteria.addSubCriteria( SearchSubCriteria.createSampleParentCriteria(expSampleCriteria)) # Now search samples = searchService.searchForSamples(searchCriteria) # Return the samples return samples
def _getFlowExperimentSample(self): """Find the {FLOW}_EXPERIMENT sample with given Id.""" # Inform if _DEBUG: self._logger.info("Retrieving experiment sample of code " + \ self._expSampleId + ", permId " + self._expSamplePermId + \ " and type " + self._expSampleType) # Search sample of type MICROSCOPY_EXPERIMENT with specified CODE sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, self._expSampleType)) sampleCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, self._expSamplePermId)) # Search samples = searchService.searchForSamples(sampleCriteria) if len(samples) == 0: samples = [] self._message = "Could not retrieve " + self._expSampleType + " sample with code " + \ self._expSampleId + ", permId " + self._expSamplePermId + \ " and type " + self._expSampleType + "." self._logger.error(self._message) return samples if _DEBUG: self._logger.info("Successfully returned sample " + self._expSampleId) # Return return samples[0]
def _retrieveSampleWithTypeAndPermId(self, samplePermId, sampleType): """ Retrieve a sample belonging to current experiment sample and collection having specified type and perm id. """ # The sample is of type 'sampleType' and has id 'sampleId' searchCriteria = SearchCriteria() searchCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, sampleType)) searchCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, samplePermId)) # Now search samples = searchService.searchForSamples(searchCriteria) if len(samples) != 1: self._message = "Sample with id " + sampleId + \ " and type " + sampleType + "not found!" self._logger.error(self._message) return None # Return the sample return samples[0]
def _getChildSamples(parentSampleType, parentSamplePermId, sampleType): """Return the samples of given type for specified parent sample.""" # The samples are of type 'sampleType' searchCriteria = SearchCriteria() searchCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, sampleType) ) # The samples have given parent expSampleCriteria = SearchCriteria() expSampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, parentSampleType) ) expSampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, parentSamplePermId) ) searchCriteria.addSubCriteria( SearchSubCriteria.createSampleParentCriteria(expSampleCriteria) ) # Now search samples = searchService.searchForSamples(searchCriteria) # Return the samples return samples
def _getDataSetsForExperiment(self): """ Return a list of datasets belonging to the experiment and optionally to the sample. If the sample ID is empty, only the experiment is used in the search criteria. If none are found, return []. """ # Set search criteria to retrieve all datasets for the experiment. # If the sample code is set, we also filter by it. searchCriteria = SearchCriteria() searchCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, "MICROSCOPY_IMG_CONTAINER")) expCriteria = SearchCriteria() expCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, self._experiment.permId)) searchCriteria.addSubCriteria(SearchSubCriteria.createExperimentCriteria(expCriteria)) if self._sample is not None: self._logger.info("Filter by sample " + self._sampleId) sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, self._sample.permId)) searchCriteria.addSubCriteria(SearchSubCriteria.createSampleCriteria(sampleCriteria)) dataSets = searchService.searchForDataSets(searchCriteria) if len(dataSets) == 0: dataSets = [] self._message = "Could not retrieve datasets for experiment " \ "with id " + self._experimentId if self._sampleId != "": self._message = self._message + " and sample with id " + \ self._sampleId self._logger.error(self._message) # Return return dataSets
def _getAllTubes(self): """ Get all tubes in the experiment. If the specimen is set (self._specimen), then return only those tubes that belong to it. Returns [] if none are found. """ # Set search criteria to retrieve all tubes in the experiment # All tubes belong to a virtual tubeset - so the set of tubes in the # experiment is exactly the same as the set of tubes in the virtual # tubeset searchCriteria = SearchCriteria() searchCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, self._experimentPrefix + "_TUBE")) expCriteria = SearchCriteria() expCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, self._experiment.permId)) searchCriteria.addSubCriteria(SearchSubCriteria.createExperimentCriteria(expCriteria)) tubes = searchService.searchForSamples(searchCriteria) if len(tubes) == 0: self._message = "The experiment with code " + \ self._experimentCode + "does not contain tubes." self._logger.error(self._message) return tubes # Check that the specimen matches (if needed) if self._specimen != "": tubes = [tube for tube in tubes if \ tube.getPropertyValue(self._experimentPrefix + "_SPECIMEN") == self._specimen] # Return the (filtered) tubes return tubes
def _getDataSetsForExperiment(self): """ Return a list of datasets belonging to the experiment and optionally to the sample. If the sample ID is empty, only the experiment is used in the search criteria. If none are found, return []. """ # Set search criteria to retrieve all datasets of type MICROSCOPY_IMG_CONTAINER # for the experiment. If the sample code is set, we also filter by it. searchCriteria = SearchCriteria() searchCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, "MICROSCOPY_IMG_CONTAINER")) expCriteria = SearchCriteria() expCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, self._experiment.permId)) searchCriteria.addSubCriteria(SearchSubCriteria.createExperimentCriteria(expCriteria)) if self._sample is not None: self._logger.info("Filter by sample " + self._sampleId) sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, self._sample.permId)) searchCriteria.addSubCriteria(SearchSubCriteria.createSampleCriteria(sampleCriteria)) dataSets = searchService.searchForDataSets(searchCriteria) if len(dataSets) == 0: dataSets = [] self._message = "Could not retrieve datasets for experiment " \ "with id " + self._experimentId if self._sampleId != "": self._message = self._message + " and sample with id " + \ self._sampleId self._logger.error(self._message) # Return return dataSets
def _getExperimentSample(collectionPermId, expSamplePermId): """Retrieve the experiment sample.""" # Get the experiment sample sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, expSamplePermId) ) expCriteria = SearchCriteria() expCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, collectionPermId) ) # Add the experiment subcriteria sampleCriteria.addSubCriteria( SearchSubCriteria.createExperimentCriteria( expCriteria) ) # Search expSampleList = searchService.searchForSamples(sampleCriteria) if len(expSampleList) != 1: return None # Return the experiment sample return expSampleList[0]
def _getMicroscopySampleTypeSample(self): # Search sample of type MICROSCOPY_SAMPLE_TYPE with specified CODE sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, self._sampleType) ) sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, self._samplePermId) ) # Search parent sample of type MICROSCOPY_EXPERIMENT with specified permId sampleParentCriteria = SearchCriteria() sampleParentCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, self._expSampleType)) sampleParentCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, self._expSamplePermId)) # Add the parent sample subcriteria sampleCriteria.addSubCriteria( SearchSubCriteria.createSampleParentCriteria( sampleParentCriteria ) ) # Search samples = searchService.searchForSamples(sampleCriteria) if len(samples) == 0: samples = [] self._message = "Could not retrieve MICROSCOPY_SAMPLE_TYPE sample with id " + \ self._sampleId + " for parent sample MICROSCOPY_EXPERIMENT with id " + \ self._expSampleId + " from COLLECTION experiment " + self._collectionId + "." self._logger.error(self._message) return samples if _DEBUG: self._logger.info("Retrieved " + str(len(samples)) + \ " samples of type MICROSCOPY_SAMPLE_TYPE " + \ "for parent sample MICROSCOPY_EXPERIMENT " + "with ID " + self._expSamplePermId) # Return return samples[0]
def _getMicroscopySampleTypeSample(self): # Search sample of type MICROSCOPY_SAMPLE_TYPE with specified CODE sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, self._sampleType) ) sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, self._samplePermId) ) # Search parent sample of type MICROSCOPY_EXPERIMENT with specified permId sampleParentCriteria = SearchCriteria() sampleParentCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, self._expSampleType)) sampleParentCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, self._expSamplePermId)) # Add the parent sample subcriteria sampleCriteria.addSubCriteria( SearchSubCriteria.createSampleParentCriteria( sampleParentCriteria ) ) # Search samples = searchService.searchForSamples(sampleCriteria) if len(samples) == 0: samples = [] self._message = "Could not retrieve MICROSCOPY_SAMPLE_TYPE sample with id " + \ self._sampleId + " for parent sample MICROSCOPY_EXPERIMENT with id " + \ self._expSampleId + " from COLLECTION experiment " + self._collectionId + "." self._logger.error(self._message) return samples if _DEBUG: self._logger.info("Retrieved " + str(len(samples)) + \ " samples of type MICROSCOPY_SAMPLE_TYPE " + \ "for parent sample MICROSCOPY_EXPERIMENT " + "with ID " + self._expSamplePermId) # Return return samples[0]
def _getDataSetForWell(self, wellCode=None): """ Get the datasets belonging to the well with specified code. If none are found, return []. If no wellCode is given, it is assumed that the well is the passed entity with code self._entityCode. """ if wellCode is None: wellCode = self._entityCode # Set search criteria to retrieve the dataset contained in the well searchCriteria = SearchCriteria() wellCriteria = SearchCriteria() wellCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.CODE, wellCode)) searchCriteria.addSubCriteria(SearchSubCriteria.createSampleCriteria(wellCriteria)) dataSets = searchService.searchForDataSets(searchCriteria) if len(dataSets) == 0: self._message = "Could not retrieve datasets for well " \ "with code " + wellCode + "." # Return return dataSets
def _getDataSetForWell(self, wellId=None): """ Get the datasets belonging to the well with specified code. If none are found, return []. If no wellId is given, it is assumed that the well is the passed entity with code self._entityId. """ if wellId is None: wellId = self._entityId # Set search criteria to retrieve the dataset contained in the well searchCriteria = SearchCriteria() wellCriteria = SearchCriteria() wellCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.CODE, wellId)) searchCriteria.addSubCriteria( SearchSubCriteria.createSampleCriteria(wellCriteria)) dataSets = searchService.searchForDataSets(searchCriteria) if len(dataSets) == 0: self._message = "Could not retrieve datasets for well " \ "with code " + wellId + "." self._logger.error(self._message) # Return return dataSets
def _getDataSetForTube(self, tubeCode): """ Get the datasets belonging to the tube with specified tube code. If none is found, return []. """ if _DEBUG: self._logger.info("Searching for tube with code " + tubeCode) # Set search criteria to retrieve the dataset contained in the tube searchCriteria = SearchCriteria() tubeCriteria = SearchCriteria() tubeCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.CODE, tubeCode)) searchCriteria.addSubCriteria( SearchSubCriteria.createSampleCriteria(tubeCriteria)) dataSets = searchService.searchForDataSets(searchCriteria) if _DEBUG: self._logger.info("Retrieved " + str(len(dataSets)) + \ " datasets for tube with code " + tubeCode) if len(dataSets) == 0: self._message = "Could not retrieve datasets for tube " \ "with code " + tubeCode + "." self._logger.error(self._message) # Return return dataSets
def _getDataSetForTube(self, tubeCode=None): """ Get the datasets belonging to the tube with specified tube code. If none is found, return []. If no tubeCode is given, it is assumed that the tube is the passed entity with code self._entityCode. """ if tubeCode is None: tubeCode = self._entityCode # Set search criteria to retrieve the dataset contained in the tube searchCriteria = SearchCriteria() tubeCriteria = SearchCriteria() tubeCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.CODE, tubeCode)) searchCriteria.addSubCriteria(SearchSubCriteria.createSampleCriteria(tubeCriteria)) dataSets = searchService.searchForDataSets(searchCriteria) if len(dataSets) == 0: self._message = "Could not retrieve datasets for tube " \ "with code " + tubeCode + "." self._logger.error(self._message) # Return return dataSets
def _getAllPlates(self): """ Get all plates in the experiment. Returns [] if none are found. """ # Set search criteria to retrieve all plates in the experiment searchCriteria = SearchCriteria() searchCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, self._experimentPrefix + "_PLATE")) expCriteria = SearchCriteria() expCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, self._experiment.permId)) searchCriteria.addSubCriteria(SearchSubCriteria.createExperimentCriteria(expCriteria)) plates = searchService.searchForSamples(searchCriteria) if len(plates) == 0: self._message = "Could not retrieve plates for experiment with code " + self._experimentCode + "." return plates # Return the plates return plates
def _getSamples(self, expSampleType, expSamplePermId, sampleType): """ Return a list of datasets of requested type belonging to the MICROSCOPY_EXPERIMENT sample and a specific sample of type MICROSCOPY_SAMPLE_TYPE. If none are found, return []. """ if _DEBUG: self._logger.info("* Requested experiment sample type: " + expSampleType) self._logger.info("* Requested experiment sample permId: " + expSamplePermId) self._logger.info("* Requested sample type: " + sampleType) # Search samples of type MICROSCOPY_SAMPLE_TYPE sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, sampleType) ) # Search parent sample of type MICROSCOPY_EXPERIMENT with specified permId sampleParentCriteria = SearchCriteria() sampleParentCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, expSampleType)) sampleParentCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, expSamplePermId)) # Add the parent sample subcriteria sampleCriteria.addSubCriteria( SearchSubCriteria.createSampleParentCriteria( sampleParentCriteria ) ) # Search samples = searchService.searchForSamples(sampleCriteria) # Return return samples
def _getSamples(self, expSampleType, expSamplePermId, sampleType): """ Return a list of datasets of requested type belonging to the MICROSCOPY_EXPERIMENT sample and a specific sample of type MICROSCOPY_SAMPLE_TYPE. If none are found, return []. """ if _DEBUG: self._logger.info("* Requested experiment sample type: " + expSampleType) self._logger.info("* Requested experiment sample permId: " + expSamplePermId) self._logger.info("* Requested sample type: " + sampleType) # Search samples of type MICROSCOPY_SAMPLE_TYPE sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, sampleType) ) # Search parent sample of type MICROSCOPY_EXPERIMENT with specified permId sampleParentCriteria = SearchCriteria() sampleParentCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, expSampleType)) sampleParentCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, expSamplePermId)) # Add the parent sample subcriteria sampleCriteria.addSubCriteria( SearchSubCriteria.createSampleParentCriteria( sampleParentCriteria ) ) # Search samples = searchService.searchForSamples(sampleCriteria) # Return return samples
def get_dataset_for_permid(transaction, permid): search_service = transaction.getSearchService() criteria = SearchCriteria() criteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.CODE, permid)) found = list(search_service.searchForDataSets(criteria)) if len(found) == 1: return found[0] else: return None
def get_dataset_for_name(transaction, dataset_name): search_service = transaction.getSearchService() criteria = SearchCriteria() criteria.addMatchClause( MatchClause.createPropertyMatch('NAME', dataset_name)) found = list(search_service.searchForDataSets(criteria)) if len(found) == 1: print("DataSetCode of found dataset = " + found[0].getDataSetCode()) return transaction.getDataSetForUpdate(found[0].getDataSetCode()) #return found[0] else: return None
def _getDataSetsForSample(sampleIdentifier, dataSetType): """Return the dataSet of given type for specified sample.""" # Set search criteria to retrieve the dataSet associated with the sample searchCriteria = SearchCriteria() searchCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, dataSetType)) sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.CODE, sampleIdentifier)) searchCriteria.addSubCriteria( SearchSubCriteria.createSampleCriteria(sampleCriteria)) dataSetList = searchService.searchForDataSets(searchCriteria) if len(dataSetList) != 1: [] # Return the dataSet return dataSetList
def _getMicroscopyExperimentSample(self): """Find the MICROSCOPY_EXPERIMENT sample with given Id.""" if _DEBUG: self._logger.info("Retrieving sample with permId " + self._expSamplePermId + " and type " + self._expSampleType + " from experiment " + "with permId " + self._collection.permId + " and type " + self._collection.getExperimentType()) # Search sample of type MICROSCOPY_EXPERIMENT with specified permId sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, self._expSampleType)) sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, self._expSamplePermId)) # Search samples = searchService.searchForSamples(sampleCriteria) if len(samples) == 0: samples = [] self._message = "Could not retrieve " + self._expSampleType + \ " sample with permId " + self._expSamplePermId + \ " from experiment with permId " + self._collection.permId + "." self._logger.error(self._message) return samples if _DEBUG: self._logger.info("Successfully returned sample with permId " + self._expSamplePermId) # Return return samples[0]
def _getMicroscopyExperimentSample(self): """Find the MICROSCOPY_EXPERIMENT sample with given Id.""" if _DEBUG: self._logger.info("Retrieving sample with permId " + self._expSamplePermId + " and type " + self._expSampleType + " from experiment " + "with permId " + self._collection.permId + " and type " + self._collection.getExperimentType()) # Search sample of type MICROSCOPY_EXPERIMENT with specified permId sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, self._expSampleType)) sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, self._expSamplePermId)) # Search samples = searchService.searchForSamples(sampleCriteria) if len(samples) == 0: samples = [] self._message = "Could not retrieve " + self._expSampleType + \ " sample with permId " + self._expSamplePermId + \ " from experiment with permId " + self._collection.permId + "." self._logger.error(self._message) return samples if _DEBUG: self._logger.info("Successfully returned sample with permId " + self._expSamplePermId) # Return return samples[0]
def _getAccessoryDataSetsForExperiment(self): """ Return a list of datasets belonging to the experiment and optionally to the sample. If the sample ID is empty, only the experiment is used in the search criteria. If none are found, return []. """ # Set search criteria to retrieve all datasets of type for the experiment. # If the sample code is set, we also filter by it. searchCriteria = SearchCriteria() searchCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, "MICROSCOPY_ACCESSORY_FILE")) expCriteria = SearchCriteria() expCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, self._experiment.permId)) searchCriteria.addSubCriteria(SearchSubCriteria.createExperimentCriteria(expCriteria)) if self._sample is not None: self._logger.info("Filter by sample " + self._sampleId) sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, self._sample.permId)) searchCriteria.addSubCriteria(SearchSubCriteria.createSampleCriteria(sampleCriteria)) accessoryDataSets = searchService.searchForDataSets(searchCriteria) # Append the accessory datasets if len(accessoryDataSets) != 0: self._message = "Found " + str(len(accessoryDataSets)) + \ " accessory datasets for experiment " \ "with id " + self._experimentId if self._sampleId != "": self._message = self._message + " and sample with id " + \ self._sampleId self._logger.info(self._message) # Return return accessoryDataSets
def _getExperimentSample(collectionPermId, expSamplePermId): """Retrieve the experiment sample.""" # Get the experiment sample sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, expSamplePermId)) expCriteria = SearchCriteria() expCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, collectionPermId)) # Add the experiment subcriteria sampleCriteria.addSubCriteria( SearchSubCriteria.createExperimentCriteria(expCriteria)) # Search expSampleList = searchService.searchForSamples(sampleCriteria) if len(expSampleList) != 1: return None # Return the experiment sample return expSampleList[0]
def _getDataSetsForPlate(self, plateCode=None): """ Return a list of datasets belonging to the plate with specified ID optionally filtered by self._specimen. If none are found, return []. If no plateCode is given, it is assumed that the plate is the passed entity with code self._entityCode. """ if plateCode is None: plateCode = self._entityCode # Set search criteria to retrieve all wells contained in the plate searchCriteria = SearchCriteria() plateCriteria = SearchCriteria() plateCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.CODE, plateCode)) searchCriteria.addSubCriteria(SearchSubCriteria.createSampleContainerCriteria(plateCriteria)) wells = searchService.searchForSamples(searchCriteria) if len(wells) == 0: self._message = "Could not retrieve wells for plate with " \ "code " + plateCode + "." self._logger.error(self._message) return wells # Check that the specimen matches (if needed) if self._specimen != "": wells = [well for well in wells if \ well.getPropertyValue(self._experimentPrefix + "_SPECIMEN") == self._specimen] # Now iterate over the samples and retrieve their datasets dataSets = [] for well in wells: wellCode = well.getCode() dataSetsForWell = self._getDataSetForWell(wellCode) dataSets.extend(dataSetsForWell) if len(dataSets) == 0: self._message = "Could not retrieve datasets for wells in " \ "plate with code " + plateCode + " from experiment " \ "with code " + self._experimentCode + "." self._logger.error(self._message) # Return return dataSets
def _getDataSets(self, expSampleType, expSamplePermId, sampleType, samplePermId, requestedDatasetType="MICROSCOPY_IMG_CONTAINER"): """ Return a list of datasets of requested type belonging to the MICROSCOPY_EXPERIMENT sample and a specific sample of type MICROSCOPY_SAMPLE_TYPE. If none are found, return []. """ # Only two types of experiment are allowed assert requestedDatasetType == "MICROSCOPY_IMG_CONTAINER" or requestedDatasetType == "MICROSCOPY_ACCESSORY_FILE", \ "Input argument 'requestedDatasetType' must be one of MICROSCOPY_IMG_CONTAINER or MICROSCOPY_ACCESSORY_FILE." self._logger.info("_getDataSetsForMicroscopySampleType() called " + "with requested data type " + requestedDatasetType) if _DEBUG: self._logger.info("* Requested dataset type: " + requestedDatasetType) self._logger.info("* Requested experiment sample type: " + expSampleType) self._logger.info("* Requested experiment sample permId: " + expSamplePermId) self._logger.info("* Requested sample type: " + sampleType) self._logger.info("* Requested sample permId: " + samplePermId) # Dataset criteria datasetSearchCriteria = SearchCriteria() datasetSearchCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, requestedDatasetType) ) # Add search criteria for sample of type MICROSCOPY_EXPERIMENT with specified CODE sampleExpCriteria = SearchCriteria() sampleExpCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, expSampleType)) sampleExpCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, expSamplePermId) ) # Add search criteria for sample of type MICROSCOPY_SAMPLE_TYPE with specified CODE sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, sampleType) ) sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, samplePermId) ) sampleCriteria.addSubCriteria( SearchSubCriteria.createSampleParentCriteria( sampleExpCriteria) ) # Add search for a parent sample of type MICROSCOPY_SAMPLE_TYPE as subcriterion datasetSearchCriteria.addSubCriteria( SearchSubCriteria.createSampleCriteria(sampleCriteria) ) # Retrieve the datasets dataSets = searchService.searchForDataSets(datasetSearchCriteria) # Inform self._logger.info("Retrieved " + str(len(dataSets)) + " dataSets of type " + requestedDatasetType + ".") # Return return dataSets
def _getDataSets(self, expSampleType, expSamplePermId, sampleType, samplePermId, requestedDatasetType="MICROSCOPY_IMG_CONTAINER"): """ Return a list of datasets of requested type belonging to the MICROSCOPY_EXPERIMENT sample and a specific sample of type MICROSCOPY_SAMPLE_TYPE. If none are found, return []. """ # Only two types of experiment are allowed assert requestedDatasetType == "MICROSCOPY_IMG_CONTAINER" or requestedDatasetType == "MICROSCOPY_ACCESSORY_FILE", \ "Input argument 'requestedDatasetType' must be one of MICROSCOPY_IMG_CONTAINER or MICROSCOPY_ACCESSORY_FILE." self._logger.info("_getDataSetsForMicroscopySampleType() called " + "with requested data type " + requestedDatasetType) if _DEBUG: self._logger.info("* Requested dataset type: " + requestedDatasetType) self._logger.info("* Requested experiment sample type: " + expSampleType) self._logger.info("* Requested experiment sample permId: " + expSamplePermId) self._logger.info("* Requested sample type: " + sampleType) self._logger.info("* Requested sample permId: " + samplePermId) # Dataset criteria datasetSearchCriteria = SearchCriteria() datasetSearchCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, requestedDatasetType) ) # Add search criteria for sample of type MICROSCOPY_EXPERIMENT with specified CODE sampleExpCriteria = SearchCriteria() sampleExpCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, expSampleType)) sampleExpCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, expSamplePermId) ) # Add search criteria for sample of type MICROSCOPY_SAMPLE_TYPE with specified CODE sampleCriteria = SearchCriteria() sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.TYPE, sampleType) ) sampleCriteria.addMatchClause( MatchClause.createAttributeMatch( MatchClauseAttribute.PERM_ID, samplePermId) ) sampleCriteria.addSubCriteria( SearchSubCriteria.createSampleParentCriteria( sampleExpCriteria) ) # Add search for a parent sample of type MICROSCOPY_SAMPLE_TYPE as subcriterion datasetSearchCriteria.addSubCriteria( SearchSubCriteria.createSampleCriteria(sampleCriteria) ) # Retrieve the datasets dataSets = searchService.searchForDataSets(datasetSearchCriteria) # Inform self._logger.info("Retrieved " + str(len(dataSets)) + " dataSets of type " + requestedDatasetType + ".") # Return return dataSets
def process(transaction, parameters, tableBuilder): """Update old flow experiments that have some missing or incorrect information. """ # Latest experiment version EXPERIMENT_VERSION = 1 # Set up logging _logger = setUpLogging() # Prepare the return table tableBuilder.addHeader("success") tableBuilder.addHeader("message") # Add a row for the results row = tableBuilder.addRow() # Retrieve parameters from client expPermId = parameters.get("expPermId") # Log parameter info _logger.info("Requested update of experiment " + expPermId + ".") # Get the experiment expCriteria = SearchCriteria() expCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, expPermId)) experiments = searchService.searchForExperiments(expCriteria) # If we did not get the experiment, return here with an error if len(experiments) != 1: # Prepare the return arguments success = False message = "The experiment with permID " + expPermId + " could not be found." # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return # Get the experiment experiment = experiments[0] # Get the experiment type experimentType = experiment.getExperimentType() # Log _logger.info("Successfully retrieved Experiment with permId " + expPermId + " and type " + experimentType + ".") # Build the corresponding dataset type experimentPrefix = experimentType[0:experimentType.find("_EXPERIMENT")] dataSetType = experimentPrefix + "_FCSFILE" # Retrieve all FCS files contained in the experiment searchCriteria = SearchCriteria() searchCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, dataSetType)) expCriteria = SearchCriteria() expCriteria.addMatchClause(MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, expPermId)) searchCriteria.addSubCriteria(SearchSubCriteria.createExperimentCriteria(expCriteria)) dataSets = searchService.searchForDataSets(searchCriteria) # Log _logger.info("Retrieved " + str(len(dataSets)) + " dataset(s) for experiment with permId " + expPermId + ".") # If we did not get the datasets, return here with an error if dataSets is None: # Prepare the return arguments success = False message = "No FCS files could be found for experiment with permID " + \ expPermId + "." # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return # Get the file from the first dataset files = getFileForCode(dataSets[0].getDataSetCode()) if len(files) != 1: # Prepare the return arguments success = False message = "Could not retrieve the FCS file to process!" # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return # Get the file fcsFile = files[0] # Log _logger.info("Reading file " + fcsFile + ".") # Open the FCS file reader = FCSReader(java.io.File(fcsFile), False) # Parse the file with data if not reader.parse(): # Prepare the return arguments success = False message = "Could not process file " + os.path.basename(fcsFile) # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return # # # EXPERIMENT NAME # # # Get the experiment name from the file expNameFromFile = reader.getCustomKeyword("EXPERIMENT NAME") # Get the experiment name from the registered Experiment currentExpName = experiment.getPropertyValue(experimentType + "_NAME") # We need the Experiment to be mutable mutableExperiment = transaction.makeExperimentMutable(experiment) # Are the experiment names matching? if expNameFromFile == currentExpName: # Log _logger.info("Registered experiment name matches the experiment " + "name from the FCS file.") else: # Update the registered Experiment name mutableExperiment.setPropertyValue(experimentType + "_NAME", expNameFromFile) # Log _logger.info("Updated registered experiment name from '" + currentExpName + "' to '" + expNameFromFile + "'.") # # # FCS FILE PARAMETERS AND ACQUISITION DATE # # hardwareString = experimentType[0:experimentType.find("_EXPERIMENT")] parameterProperty = hardwareString + "_FCSFILE_PARAMETERS" acqDateProperty = hardwareString + "_FCSFILE_ACQ_DATE" # Log _logger.info("Checking properties of " + str(len(dataSets)) + " file(s).") for dataSet in dataSets: # Check whether the parameters are stored for the file parameters = dataSet.getPropertyValue(parameterProperty) # Check whether the acquisition date is stored for the file acqDate = dataSet.getPropertyValue(acqDateProperty) if parameters is None or acqDate is None: # Make the DataSet mutable for update mutableDataSet = transaction.makeDataSetMutable(dataSet) # Get the file from the dataset files = getFileForCode(dataSet.getDataSetCode()) if len(files) != 1: # Prepare the return arguments success = False message = "Could not retrieve the FCS file to process!" # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return # Get the FCS file fcsFile = files[0] # Open and parse the FCS file reader = FCSReader(java.io.File(fcsFile), True); # Parse the file with data if not reader.parse(): # Prepare the return arguments success = False message = "Could not process file " + os.path.basename(fcsFile) # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return if acqDate is None: # Get and format the acquisition date dateStr = formatExpDateForPostgreSQL( reader.getStandardKeyword("$DATE")) # Update the dataSet mutableDataSet.setPropertyValue(acqDateProperty, dateStr) # Log _logger.info("The acquisition date of file " + str(fcsFile) + " was set to: " + dateStr + ".") if parameters is None: # Get the parameters parametersAttr = reader.parametersAttr if parametersAttr is None: # Prepare the return arguments success = False message = "Could not read parameters from file " + \ os.path.basename(fcsFile) # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return # Convert the parameters to XML parametersXML = dictToXML(parametersAttr) # Now store them in the dataSet mutableDataSet.setPropertyValue(parameterProperty, parametersXML) # Log _logger.info("The parameters for file " + str(fcsFile) + " were successfully stored (in XML).") # Update the version of the experiment mutableExperiment.setPropertyValue(experimentType + "_VERSION", str(EXPERIMENT_VERSION)) success = True message = "Congratulations! The experiment was successfully upgraded " + \ "to the latest version." # Log _logger.info(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message)
def process(transaction, parameters, tableBuilder): """Update old flow experiments that have some missing or incorrect information. """ # Latest experiment version EXPERIMENT_VERSION = 1 # Set up logging _logger = setUpLogging() # Prepare the return table tableBuilder.addHeader("success") tableBuilder.addHeader("message") # Add a row for the results row = tableBuilder.addRow() # Retrieve parameters from client expPermId = parameters.get("expPermId") # Log parameter info _logger.info("Requested update of experiment " + expPermId + ".") # Get the experiment expCriteria = SearchCriteria() expCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, expPermId)) experiments = searchService.searchForExperiments(expCriteria) # If we did not get the experiment, return here with an error if len(experiments) != 1: # Prepare the return arguments success = False message = "The experiment with permID " + expPermId + " could not be found." # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return # Get the experiment experiment = experiments[0] # Get the experiment type experimentType = experiment.getExperimentType() # Log _logger.info("Successfully retrieved Experiment with permId " + expPermId + " and type " + experimentType + ".") # Build the corresponding dataset type experimentPrefix = experimentType[0:experimentType.find("_EXPERIMENT")] dataSetType = experimentPrefix + "_FCSFILE" # Retrieve all FCS files contained in the experiment searchCriteria = SearchCriteria() searchCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.TYPE, dataSetType)) expCriteria = SearchCriteria() expCriteria.addMatchClause( MatchClause.createAttributeMatch(MatchClauseAttribute.PERM_ID, expPermId)) searchCriteria.addSubCriteria( SearchSubCriteria.createExperimentCriteria(expCriteria)) dataSets = searchService.searchForDataSets(searchCriteria) # Log _logger.info("Retrieved " + str(len(dataSets)) + " dataset(s) for experiment with permId " + expPermId + ".") # If we did not get the datasets, return here with an error if dataSets is None: # Prepare the return arguments success = False message = "No FCS files could be found for experiment with permID " + \ expPermId + "." # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return # Get the file from the first dataset files = getFileForCode(dataSets[0].getDataSetCode()) if len(files) != 1: # Prepare the return arguments success = False message = "Could not retrieve the FCS file to process!" # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return # Get the file fcsFile = files[0] # Log _logger.info("Reading file " + fcsFile + ".") # Open the FCS file reader = FCSReader(java.io.File(fcsFile), False) # Parse the file with data if not reader.parse(): # Prepare the return arguments success = False message = "Could not process file " + os.path.basename(fcsFile) # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return # # # EXPERIMENT NAME # # # Get the experiment name from the file expNameFromFile = reader.getCustomKeyword("EXPERIMENT NAME") # Get the experiment name from the registered Experiment currentExpName = experiment.getPropertyValue(experimentType + "_NAME") # We need the Experiment to be mutable mutableExperiment = transaction.makeExperimentMutable(experiment) # Are the experiment names matching? if expNameFromFile == currentExpName: # Log _logger.info("Registered experiment name matches the experiment " + "name from the FCS file.") else: # Update the registered Experiment name mutableExperiment.setPropertyValue(experimentType + "_NAME", expNameFromFile) # Log _logger.info("Updated registered experiment name from '" + currentExpName + "' to '" + expNameFromFile + "'.") # # # FCS FILE PARAMETERS AND ACQUISITION DATE # # hardwareString = experimentType[0:experimentType.find("_EXPERIMENT")] parameterProperty = hardwareString + "_FCSFILE_PARAMETERS" acqDateProperty = hardwareString + "_FCSFILE_ACQ_DATE" # Log _logger.info("Checking properties of " + str(len(dataSets)) + " file(s).") for dataSet in dataSets: # Check whether the parameters are stored for the file parameters = dataSet.getPropertyValue(parameterProperty) # Check whether the acquisition date is stored for the file acqDate = dataSet.getPropertyValue(acqDateProperty) if parameters is None or acqDate is None: # Make the DataSet mutable for update mutableDataSet = transaction.makeDataSetMutable(dataSet) # Get the file from the dataset files = getFileForCode(dataSet.getDataSetCode()) if len(files) != 1: # Prepare the return arguments success = False message = "Could not retrieve the FCS file to process!" # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return # Get the FCS file fcsFile = files[0] # Open and parse the FCS file reader = FCSReader(java.io.File(fcsFile), True) # Parse the file with data if not reader.parse(): # Prepare the return arguments success = False message = "Could not process file " + os.path.basename(fcsFile) # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return if acqDate is None: # Get and format the acquisition date dateStr = formatExpDateForPostgreSQL( reader.getStandardKeyword("$DATE")) # Update the dataSet mutableDataSet.setPropertyValue(acqDateProperty, dateStr) # Log _logger.info("The acquisition date of file " + str(fcsFile) + " was set to: " + dateStr + ".") if parameters is None: # Get the parameters parametersAttr = reader.parametersAttr if parametersAttr is None: # Prepare the return arguments success = False message = "Could not read parameters from file " + \ os.path.basename(fcsFile) # Log the error _logger.error(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message) # Return here return # Convert the parameters to XML parametersXML = dictToXML(parametersAttr) # Now store them in the dataSet mutableDataSet.setPropertyValue(parameterProperty, parametersXML) # Log _logger.info("The parameters for file " + str(fcsFile) + " were successfully stored (in XML).") # Update the version of the experiment mutableExperiment.setPropertyValue(experimentType + "_VERSION", str(EXPERIMENT_VERSION)) success = True message = "Congratulations! The experiment was successfully upgraded " + \ "to the latest version." # Log _logger.info(message) # Add the results to current row row.setCell("success", success) row.setCell("message", message)