def validate_pcrPlateRow_for_existing_samples(row_list, sampleset_ids): """ Validates for uniqueness of PCR plate position and mandatory input for AmpliSeqOnChef sample sets """ msgs = dict() count = 0 for row in row_list: count += 1 pcrPlateRow = row.get(COLUMN_PCR_PLATE_POSITION, "").strip() for sampleset_id in sampleset_ids: sampleset = models.SampleSet.objects.get(pk=sampleset_id) samplesetitems = sampleset.samples.all() # validate when adding samples to pre-existing sampleset if samplesetitems.count(): for item in samplesetitems: pcrPlateRow1 = item.pcrPlateRow item_id = item.pk # ensure only 1 pcr plate position per sample if (pcrPlateRow and pcrPlateRow1 and pcrPlateRow.upper() == pcrPlateRow1.upper()): isValid = False errorMessage = validation.format( ugettext_lazy( "samplesets.samplesetitem.messages.validate.pcrPlateRow.unique" ), message_params={ "pcrPlateRow": pcrPlateRow.upper() }, include_error_prefix=True, ) msgs[count] = (COLUMN_PCR_PLATE_POSITION, errorMessage) if (sampleset and not pcrPlateRow and "amps_on_chef" in sampleset.libraryPrepType.lower()): errorMessage = validation.format( ugettext_lazy( "samplesets.samplesetitem.messages.validate.pcrPlateRow.required" ), include_error_prefix=True, ) msgs[count] = (COLUMN_PCR_PLATE_POSITION, errorMessage) else: if (sampleset and not pcrPlateRow and "amps_on_chef" in sampleset.libraryPrepType.lower()): errorMessage = validation.format( ugettext_lazy( "samplesets.samplesetitem.messages.validate.pcrPlateRow.required" ), include_error_prefix=True, ) msgs[count] = (COLUMN_PCR_PLATE_POSITION, errorMessage) return msgs
def validate_barcodes_are_unique(row_list): barcodekits = [] dnabarcodes = [] msgs = dict() count = 0 for row in row_list: count += 1 barcodeKit = row.get(COLUMN_BARCODE_KIT, "").strip() barcode = row.get(COLUMN_BARCODE, "").strip() if barcodeKit and barcode: barcodekits.append(barcodeKit) if len(set(barcodekits)) > 1: msgs[count] = ( COLUMN_BARCODE_KIT, validation.format( ugettext_lazy( "samplesets.samplesetitem.messages.validate.barcodekit.notequal" ), include_error_prefix=True, ), ) dnabarcode = models.dnaBarcode.objects.filter( name__iexact=barcodeKit, id_str__iexact=barcode) if dnabarcode.count() != 1: # choices = cvs.order_by('value').values_list('value', flat=True) # validation.invalid_choice(field_label, cvValue, choices, include_error_prefix=True) msgs[count] = ( COLUMN_BARCODE_KIT, validation.format( ugettext_lazy( "samplesets.samplesetitem.messages.validate.barcodekit.invalid_choice" ), { "barcode": barcode, "barcodeSet": barcodeKit, "barcodeSetLabel": COLUMN_BARCODE_KIT, "barcodeLabel": COLUMN_BARCODE, }, include_error_prefix=True, ), ) # "Error, %s %s is an invalid barcodeKit and barcode combination" % (barcodeKit, barcode) if dnabarcode.count() > 0: if dnabarcode[0] in dnabarcodes: msgs[count] = ( COLUMN_BARCODE, validation.format( ugettext_lazy( "samplesets.samplesetitem.messages.validate.barcode.unique" ), {"barcode": barcode}, include_error_prefix=True, ), ) # "Error, A barcode can be assigned to only one sample in the sample set and %s has been assigned to another sample" % (barcode) else: dnabarcodes.append(dnabarcode[0]) return msgs
def validate_barcodes_for_existing_samples(row_list, sampleset_ids): msgs = dict() count = 0 for row in row_list: count += 1 barcode = row.get(COLUMN_BARCODE, "").strip() if barcode: dnabarcode = models.dnaBarcode.objects.filter( id_str__iexact=barcode) barcodeKit = dnabarcode[0].name row[COLUMN_BARCODE] = dnabarcode[0].id_str else: barcode = None barcodeKit = None for sampleset_id in sampleset_ids: sampleset = models.SampleSet.objects.get(pk=sampleset_id) samplesetitems = sampleset.samples.all() for item in samplesetitems: # first validate that all barcode kits are the same for all samples if item.dnabarcode: barcodeKit1 = item.dnabarcode.name barcode1 = item.dnabarcode.id_str else: barcodeKit1 = None barcode1 = None if barcodeKit and barcodeKit1 and barcodeKit != barcodeKit1: msgs[count] = ( COLUMN_BARCODE, validation.format( ugettext_lazy( "samplesets.samplesetitem.messages.validate.barcodekit.notequal" ), include_error_prefix=True, ), ) # next validate that all barcodes are unique per each sample if barcode and barcode1 and barcode == barcode1: msgs[count] = ( COLUMN_BARCODE, validation.format( ugettext_lazy( "samplesets.samplesetitem.messages.validate.barcode.unique" ), {"barcode": barcode}, include_error_prefix=True, ), ) return msgs
def validate_barcode_sample_association(selectedBarcodes, selectedBarcodeKit): errors = {"MISSING_BARCODE": "", "DUPLICATE_BARCODE": ""} if not selectedBarcodeKit: return errors prior_barcodes = [] if not selectedBarcodes: errors["MISSING_BARCODE"] = _( "workflow.step.sample.messages.validate.barcodepersample" ) # "Please select a barcode for each sample" else: dupBarcodes = validation.list_duplicates(selectedBarcodes) for selectedBarcode in dupBarcodes: # only include unique barcode selection error messages message = validation.format( _("workflow.step.sample.messages.validate.barcode.unique"), {"barcode": selectedBarcode}, ) # "Barcode %s selections have to be unique\n" % selectedBarcode errors["DUPLICATE_BARCODE"] = errors[ "DUPLICATE_BARCODE"] + message + "\n" # logger.debug("errors=%s" %(errors)) return errors
def validate_sample_name( value, field_label, isTemplate=None, isTemplate_label=PlanTemplate.verbose_name, barcodeId=None, barcodeId_label=ugettext_lazy("workflow.step.kits.fields.barcodeId.label"), ): # TODO: i18n errors = [] if not value: if not isTemplate: errors.append(validation.required_error(field_label)) else: if isTemplate: errors.append( validation.format( ugettext_lazy( "template.messages.validation.invalidsamples"), {"name": isTemplate_label}, ) ) # "Invalid input. Sample information cannot be saved in the %(name)s" if barcodeId: errors.append( validation.format( ugettext_lazy( "plannedexperiment.messages.validation.nonbarcoded.barcodesetnotrequired" ), { "barcodeSetName": barcodeId_label, "barcodeSetValue": barcodeId }, ) ) # "Invalid input. %(barcodeSetName)s (%(barcodeSetValue)s) should not be provided for non barcoded plan" if not validation.is_valid_chars(value): errors.append(validation.invalid_chars_error(field_label)) if not validation.is_valid_leading_chars(value): errors.append(validation.invalid_leading_chars(field_label)) if not validation.is_valid_length(value, MAX_LENGTH_SAMPLE_NAME): errors.append( validation.invalid_length_error(field_label, MAX_LENGTH_SAMPLE_NAME, value)) return errors
def validate_pcrPlateRow_are_unique(row_list): positions = [] msgs = dict() count = 0 for row in row_list: count += 1 pcrPlateRow = row.get(COLUMN_PCR_PLATE_POSITION, "").strip().upper() if pcrPlateRow: if pcrPlateRow in positions: msgs[count] = ( COLUMN_PCR_PLATE_POSITION, validation.format( ugettext_lazy( "samplesets.samplesetitem.messages.validate.pcrPlateRow.unique" ), {"pcrPlateRow": pcrPlateRow}, include_error_prefix=True, ), ) else: positions.append(pcrPlateRow) return msgs
def validate_kit_chip_combination( bundle, chipType_label="chipType", templatingKitName_label="templatingKitName", sequencekitname_label="sequencekitname", librarykitname_label="librarykitname", runType_label="runType", ): errorMsg = None chipType = bundle.data.get("chipType", None) runType = bundle.data.get("runType", None) templatingKitName = bundle.data.get("templatingKitName", None) sequencekitname = bundle.data.get("sequencekitname", None) librarykitname = bundle.data.get("librarykitname", None) planExp_Kits = [ (templatingKitName, templatingKitName_label), (sequencekitname, sequencekitname_label), (librarykitname, librarykitname_label), ] # since these kits are already been validated, just concentrate on validating the chip/instrument/kits combination try: if chipType: query_args = (Q(name__iexact=chipType) | Q(description__iexact=chipType), ) selectedChips = Chip.objects.filter(*query_args) if selectedChips: selectedChip = selectedChips[0] for kit_name, kit_label in planExp_Kits: if not kit_name: continue selectedKitInfos = KitInfo.objects.filter( kitType__in=[ "TemplatingKit", "IonChefPrepKit", "SequencingKit", "LibraryKit", "LibraryPrepKit", ], name__iexact=kit_name, ) if not selectedKitInfos.count(): continue selectedKitInfo = selectedKitInfos[0] # # validate selected chip and kit instrument type combination is supported # selectedChip_instType = selectedChip.instrumentType selectedKit_instType = selectedKitInfo.instrumentType if ( selectedKit_instType and selectedKit_instType not in selectedChip_instType ): # BUG: 'proton;S5' not in 'S5' reports True... find alternate way to validate... # The %(kitLabel)s (%(kitName)s) instrument type of %(kitInstrumentType)s is # incompatible with the %(chipLabel)s (%(chipName)s) instrument type of # %(chipInstrumentType)s. Specify a different %(kitLabel)s or %(chipLabel)s. errorMsg = validation.format( ugettext_lazy( "plannedexperiment.messages.validate.invalid.kitandchip.instrumenttype" ), { "kitLabel": kit_label, "kitName": selectedKitInfo.name, "kitInstrumentType": selectedKitInfo.get_instrument_types_list(), "chipLabel": chipType_label, "chipName": chipType, "chipInstrumentType": selectedChip_instType, }, ) return errorMsg # # if instrument type is valid: validate if chip type of selected kit is in the supported list # if selectedKitInfo.chipTypes: selectedKit_chipTypes = selectedKitInfo.get_chip_types_list( ) if (selectedKit_chipTypes and chipType not in selectedKit_chipTypes): errorMsg = validation.invalid_choice_related_choice( chipType_label, chipType, selectedKit_chipTypes, kit_label, selectedKitInfo.name, ) return errorMsg # # if instrument type and chip type are valid: validate if application type of selected kit is in the supported list # if (runType and selectedKitInfo.applicationType and runType not in selectedKitInfo.get_kit_application_list()): errorMsg = validation.invalid_invalid_value_related_value( kit_label, selectedKitInfo.name + "[%s]" % selectedKitInfo.get_applicationType_display(), runType_label, runType, ) return errorMsg except Exception as Err: logger.debug("Error during plan creation %s" % str(Err)) errorMsg = str(Err) return errorMsg