samp_model = rec.get('sampling_independence_model', 'INDEPENDENT_BIOLOGICAL_OPPORTUNITIES') if samp_model != 'INDEPENDENT_BIOLOGICAL_OPPORTUNITIES': return _hold_unqual('UNSUPPORTED_SAMPLING_INDEPENDENCE_MODEL') panel = rec.get('presort_lineage_panel') if panel != TIER6B_ALLOWED_PRESORT_LINEAGE_PANEL_IIP0031: return _hold_unqual('INVALID_OR_BLINDED_PRESORT_LINEAGE_PANEL') risk_unit = rec.get('clonogenic_risk_unit') if ( not isinstance(risk_unit, str) or not risk_unit.strip() or risk_unit != risk_unit.strip() or risk_unit not in TIER6B_ALLOWED_RISK_UNITS_IIP0031 ): return _hold_unqual('INVALID_OR_UNSUBSTANTIATED_CLONOGENIC_RISK_UNIT') if lot_risk_unit is None: lot_risk_unit = risk_unit elif lot_risk_unit != risk_unit: return _hold_unqual('INCONSISTENT_CLONOGENIC_RISK_UNIT_ACROSS_FRACTIONS') if rec.get('endogenous_denominator_verified') is not True: return _hold_unqual('OMITTED_OR_UNVERIFIED_ENDOGENOUS_DENOMINATOR') endo_basis = rec.get('endogenous_denominator_basis') if not isinstance(endo_basis, str) or endo_basis not in TIER6B_ALLOWED_ENDOGENOUS_BASES_IIP0031: return _hold_unqual('INVALID_ENDOGENOUS_DENOMINATOR_BASIS') # Require closed-schema source-linked disjoint untreated (no-AP1903) CFU control tuple on EVERY observation (#27, T7): untr_ctrl = rec.get('untreated_cfu_control') if not isinstance(untr_ctrl, dict): return _hold_unqual('MISSING_SOURCE_LINKED_UNTREATED_CFU_CONTROL') if any(not isinstance(uk_k, str) or uk_k not in _allowed_untr_keys for uk_k in untr_ctrl): return _hold_unqual('INVALID_UNTREATED_CFU_CONTROL_SCHEMA') for _tr_flag_k in ('treated', 'ap1903_treated'): if _tr_flag_k in untr_ctrl and untr_ctrl[_tr_flag_k] is not False: return _hold_unqual('CONTRADICTORY_TREATED_FLAG_IN_UNTREATED_CFU_CONTROL') if 'is_untreated' in untr_ctrl and untr_ctrl['is_untreated'] is not True: return _hold_unqual('CONTRADICTORY_TREATED_FLAG_IN_UNTREATED_CFU_CONTROL') if 'control_role' in untr_ctrl and untr_ctrl['control_role'] != 'UNTREATED_CFU_CONTROL': return _hold_unqual('CONTRADICTORY_CONTROL_ROLE_IN_UNTREATED_CFU_CONTROL') untr_sid = untr_ctrl.get('untreated_sample_id') untr_k = untr_ctrl.get('untreated_cfu_colonies_k') untr_n = untr_ctrl.get('untreated_plated_cells_n') if ( not isinstance(untr_sid, str) or not untr_sid.strip() or untr_sid != untr_sid.strip() or type(untr_k) is not int or type(untr_n) is not int or untr_n < 1 or not (1 <= untr_k <= untr_n) ): return _hold_unqual('INVALID_OR_ZERO_GROWTH_UNTREATED_CFU_CONTROL') untr_all_sids: list[str] = [untr_sid] for _ua_k in _TIER6B_SAMPLE_ALIASES + _TIER6B_RECORD_ID_ALIASES: if _ua_k in untr_ctrl: _ua_v = untr_ctrl[_ua_k] if not isinstance(_ua_v, str) or not _ua_v.strip() or _ua_v != _ua_v.strip(): return _hold_unqual('INVALID_OR_ZERO_GROWTH_UNTREATED_CFU_CONTROL') if _ua_v not in untr_all_sids: untr_all_sids.append(_ua_v) if untr_n < IIP0031_MIN_UNTREATED_CFU_PLATED_CELLS and untr_n.bit_length() <= 53: return _hold_unqual('UNDERPOWERED_UNTREATED_CFU_CONTROL_DENOMINATOR') # Live untreated CFU assays are ALWAYS finite samples; forbid opting out of 3-way Clopper-Pearson coverage: if 'statistically_estimated_denominator_or_share' in rec and rec['statistically_estimated_denominator_or_share'] is not None: stat_est_flag = rec['statistically_estimated_denominator_or_share'] if type(stat_est_flag) is not bool: return _hold_unqual('INVALID_STATISTICAL_ESTIMATION_FLAG') if stat_est_flag is False: return _hold_unqual('EXACT_FINITE_CFU_OPT_OUT_PROHIBITED') # Require explicit integer pre-sort cell balance, disposal attestation for sort losses, & physical plated-cell bounds: if rec.get('presort_mass_balance_reconciled') is not True: return _hold_unqual('UNRECONCILED_PRESORT_MASS_BALANCE') unrec_loss = rec.get('unreconciled_loss_fraction') if not _is_strict_finite_number_iip0024(unrec_loss) or float(unrec_loss) < 0.0 or float(unrec_loss) > 1e-12: return _hold_unqual('UNRECONCILED_SORTING_OR_PROCESSING_LOSSES') ps_tot = rec.get('presort_total_input_cells') ps_loss = rec.get('presort_reconciled_loss_cells') ps_frac = rec.get('presort_fraction_cells') plated_n = rec.get('plated_treated_cells_n') if ( type(ps_tot) is not int or ps_tot < 1 or type(ps_loss) is not int or ps_loss < 0 or type(ps_frac) is not int or ps_frac < 1 or type(plated_n) is not int or plated_n < n_raw ): return _hold_unqual('MISSING_OR_INVALID_INTEGER_PRESORT_CELL_MASS_BALANCE') loss_disp_flag = rec.get('excluded_or_lost_material_discarded_not_in_graft') if loss_disp_flag is not None and type(loss_disp_flag) is not bool: return _hold_unqual('UNVERIFIED_DISPOSAL_OF_RECONCILED_SORT_LOSSES') if loss_disp_flag is False or (ps_loss > 0 and loss_disp_flag is not True): return _hold_unqual('UNVERIFIED_DISPOSAL_OF_RECONCILED_SORT_LOSSES') lot_presort_total_cells = ps_tot lot_reconciled_loss_cells = ps_loss # Validate EVERY observation's separate dual calibrants BEFORE fraction aggregation (closed schema + disjoint IDs, #27, T8): if rec.get('pooled_calibrants') is True or ( rec.get('pooled_calibrants') is not None and type(rec.get('pooled_calibrants')) is not bool ): return _hold_unqual(f'POOLED_CALIBRANTS_PROHIBITED:{rec["fraction_id"]}') c_hpsc = rec.get('hpsc_calibrant') c_diff = rec.get('edited_differentiated_calibrant') if not isinstance(c_hpsc, (list, tuple, dict)) or not isinstance(c_diff, (list, tuple, dict)): return _hold_unqual(f'MISSING_SEPARATE_DUAL_CALIBRANTS:{rec["fraction_id"]}') hpsc_cal_ids: list[str] = [] diff_cal_ids: list[str] = [] if isinstance(c_hpsc, dict): if any(not isinstance(ck_k, str) or ck_k not in _allowed_cal_dict_keys for ck_k in c_hpsc): return _hold_unqual(f'INVALID_HPSC_CALIBRANT_PAIR:{rec["fraction_id"]}') if 'calibrant_role' in c_hpsc and c_hpsc['calibrant_role'] != 'HPSC_CALIBRANT': return _hold_unqual(f'INVALID_HPSC_CALIBRANT_PAIR:{rec["fraction_id"]}') for _tr_ck in ('treated', 'ap1903_treated'): if _tr_ck in c_hpsc and c_hpsc[_tr_ck] is not True: return _hold_unqual(f'INVALID_HPSC_CALIBRANT_PAIR:{rec["fraction_id"]}') for _cid_k in ('cal_batch_id',) + _TIER6B_SAMPLE_ALIASES + _TIER6B_RECORD_ID_ALIASES: if _cid_k in c_hpsc: _cid_v = c_hpsc[_cid_k] if not isinstance(_cid_v, str) or not _cid_v.strip() or _cid_v != _cid_v.strip(): return _hold_unqual(f'INVALID_HPSC_CALIBRANT_PAIR:{rec["fraction_id"]}') if _cid_v not in hpsc_cal_ids: hpsc_cal_ids.append(_cid_v) rk_h, rn_h = c_hpsc.get('r_k'), c_hpsc.get('r_n') elif len(c_hpsc) == 2: rk_h, rn_h = c_hpsc[0], c_hpsc[1] else: return _hold_unqual(f'INVALID_HPSC_CALIBRANT_PAIR:{rec["fraction_id"]}') if isinstance(c_diff, dict): if any(not isinstance(ck_k, str) or ck_k not in _allowed_cal_dict_keys for ck_k in c_diff): return _hold_unqual(f'INVALID_DIFF_CALIBRANT_PAIR:{rec["fraction_id"]}') if 'calibrant_role' in c_diff and c_diff['calibrant_role'] != 'EDITED_DIFFERENTIATED_CALIBRANT': return _hold_unqual(f'INVALID_DIFF_CALIBRANT_PAIR:{rec["fraction_id"]}') for _tr_ck in ('treated', 'ap1903_treated'): if _tr_ck in c_diff and c_diff[_tr_ck] is not True: return _hold_unqual(f'INVALID_DIFF_CALIBRANT_PAIR:{rec["fraction_id"]}') for _cid_k in ('cal_batch_id',) + _TIER6B_SAMPLE_ALIASES + _TIER6B_RECORD_ID_ALIASES: if _cid_k in c_diff: _cid_v = c_diff[_cid_k] if not isinstance(_cid_v, str) or not _cid_v.strip() or _cid_v != _cid_v.strip(): return _hold_unqual(f'INVALID_DIFF_CALIBRANT_PAIR:{rec["fraction_id"]}') if _cid_v not in diff_cal_ids: diff_cal_ids.append(_cid_v) rk_d, rn_d = c_diff.get('r_k'), c_diff.get('r_n') elif len(c_diff) == 2: rk_d, rn_d = c_diff[0], c_diff[1] else: return _hold_unqual(f'INVALID_DIFF_CALIBRANT_PAIR:{rec["fraction_id"]}') if set(hpsc_cal_ids).intersection(diff_cal_ids): return _hold_unqual(f'NON_DISJOINT_HPSC_AND_DIFF_CALIBRANT_SPECIMEN_ID:{rec["fraction_id"]}') if ( type(rk_h) is not int or type(rn_h) is not int or type(rk_d) is not int or type(rn_d) is not int or rn_h < 1 or rn_d < 1 or not (0 <= rk_h <= rn_h) or not (0 <= rk_d <= rn_d) ): return _hold_unqual(f'NON_INTEGER_OR_OUT_OF_BOUNDS_CALIBRANT_COUNTS:{rec["fraction_id"]}') if any(count.bit_length() > 53 for count in (n_raw, ps_tot, ps_loss, ps_frac, plated_n, untr_n, rn_h, rn_d)): return _hold_unqual('TIER6B_COUNTS_EXCEED_SUPPORTED_NUMERIC_PRECISION') if n_raw > (plated_n * untr_k) // untr_n: return _hold_unqual( 'UNTREATED_CFU_DOES_NOT_SUPPORT_CLAIMED_OPPORTUNITIES', untreated_cfu_max_supported_N=(plated_n * untr_k) // untr_n, claimed_N=n_raw, ) cal_tuple_obs = (rk_h, rn_h, rk_d, rn_d) cal_batch_raw = rec.get('cal_batch_id') if cal_batch_raw is not None: if not isinstance(cal_batch_raw, str) or not cal_batch_raw.strip() or cal_batch_raw != cal_batch_raw.strip(): return _hold_unqual(f'INVALID_CAL_BATCH_ID:{rec["fraction_id"]}') roster_raw = rec.get('declared_fraction_roster') if ( not isinstance(roster_raw, (list, tuple)) or len(roster_raw) < 2 or any(not isinstance(f, str) or not f.strip() or f != f.strip() for f in roster_raw) or len(set(roster_raw)) != len(roster_raw) or len({re.sub(r'[^a-z0-9]+', '_', f.lower()).strip('_') for f in roster_raw}) != len(roster_raw) ): return _hold_unqual('INVALID_OR_MISSING_EXHAUSTIVE_FRACTION_ROSTER') lot_roster = list(roster_raw) fid = rec['fraction_id'] sid = rec['sample_id'] if fid not in lot_roster: return _hold_unqual(f'UNDECLARED_FRACTION_ID:{fid}') for _usid_tok in untr_all_sids: if _usid_tok in aliquot_tokens: return _hold_unqual(f'NON_DISJOINT_UNTREATED_CFU_SAMPLE_ID:{sid}') if _usid_tok in seen_untr_sid_to_frac: if seen_untr_sid_to_frac[_usid_tok] != fid: return _hold_unqual(f'NON_DISJOINT_UNTREATED_CFU_SAMPLE_ID_ACROSS_FRACTIONS:{_usid_tok}') return _hold_unqual(f'BORROWED_OR_SHARED_UNTREATED_CFU_CONTROL_ACROSS_OBSERVATIONS:{_usid_tok}') for _usid_tok in untr_all_sids: seen_untr_sid_to_frac[_usid_tok] = fid if cal_batch_raw is not None: if cal_batch_raw in seen_cal_batch_to_frac: if seen_cal_batch_to_frac[cal_batch_raw] != fid: return _hold_unqual(f'NON_DISJOINT_CALIBRATION_BATCH_ID_ACROSS_FRACTIONS:{cal_batch_raw}') return _hold_unqual(f'BORROWED_OR_SHARED_CALIBRATION_BATCH_ACROSS_OBSERVATIONS:{cal_batch_raw}') seen_cal_batch_to_frac[cal_batch_raw] = fid for _cspec_id in hpsc_cal_ids + diff_cal_ids: if _cspec_id in aliquot_tokens or _cspec_id in untr_all_sids: return _hold_unqual(f'NON_DISJOINT_CALIBRANT_SPECIMEN_ID:{_cspec_id}') if _cspec_id in seen_cal_specimen_to_frac: if seen_cal_specimen_to_frac[_cspec_id] != fid: return _hold_unqual(f'NON_DISJOINT_CALIBRATION_BATCH_ID_ACROSS_FRACTIONS:{_cspec_id}') return _hold_unqual(f'BORROWED_OR_SHARED_CALIBRATION_BATCH_ACROSS_OBSERVATIONS:{_cspec_id}') for _cspec_id in hpsc_cal_ids + diff_cal_ids: seen_cal_specimen_to_frac[_cspec_id] = fid all_treated_sample_ids.update(aliquot_tokens) all_untreated_sample_ids.update(untr_all_sids) all_calibrant_specimen_ids.update(hpsc_cal_ids) all_calibrant_specimen_ids.update(diff_cal_ids) canon_tok = sid cal_key_obs = ('cal_batch_id', cal_batch_raw) if cal_batch_raw is not None else ('obs_sample_id', canon_tok) _allowed_tier6b_roles = frozenset({'ENRICHED', 'FLOW_THROUGH_OR_EXCLUDED'}) _explicit_frole: str | None = None if 'fraction_role' in rec: _raw_frole = rec.get('fraction_role') if ( not isinstance(_raw_frole, str) or not _raw_frole.strip() or _raw_frole != _raw_frole.strip() or _raw_frole not in _allowed_tier6b_roles ): return _hold_unqual(f'INVALID_TIER6B_FRACTION_ROLE:{fid}') _explicit_frole = _raw_frole if fid not in agg_by_frac: agg_by_frac[fid] = { 'N': n_raw, 'k': k_raw, 'plated_treated_cells_sum': plated_n, 'presort_fraction_cells': ps_frac, 'declared_clonogenic_opportunities': [],