lpc_item.get("b6h12_pos_ctrl_pct"), f"{lot_id}.lot_positive_controls[{lpc_idx}].b6h12_pos_ctrl_pct", lo=0.0, hi=100.0 ) if lpc_b6_err is not None or lpc_b6 is None: entry_ok = False lot_pos_ctrl_valid = False _lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", lpc_b6_err or "Invalid lot_positive_controls.b6h12_pos_ctrl_pct") elif lpc_b6 < 90.0: entry_ok = False lot_pos_ctrl_valid = False _lot_escalate( "HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"Lot-specific B6H12 anti-CD47 positive control ({lpc_run_c or lpc_idx}) {lpc_b6:.2f}% < 90.0% minimum run dynamic-range threshold", ) if lpc_run_c: if entry_ok: candidate_passing_ctrl_run_ids.add(lpc_run_c) else: conflicted_or_invalid_ctrl_run_ids.add(lpc_run_c) # Finalize Pass 1 strictly BEFORE adjudicating any observation or fab2_deconvolution row! passing_ctrl_run_ids: set[str] = candidate_passing_ctrl_run_ids - conflicted_or_invalid_ctrl_run_ids obs_list = lot.get("observations") if not isinstance(obs_list, list) or len(obs_list) == 0: _lot_escalate( "HOLD_INVALID_OR_INCOMPLETE_ASSAY", "Missing typed observations list (anonymous unindexed arrays are rejected fail-closed)", ) per_lot_verdicts[lot_id] = { "lot_verdict": lot_state, "lot_positive_controls_verified": lot_pos_ctrl_valid, "covered_positive_control_run_ids": sorted(passing_ctrl_run_ids), "conflicted_or_invalid_positive_control_run_ids": sorted(conflicted_or_invalid_ctrl_run_ids), "independent_matched_igg_provenance_verified": False, "verified_independent_igg_control_run_ids": [], "conflicted_or_invalid_independent_igg_run_ids": sorted(conflicted_or_invalid_ind_igg_run_ids), "observed_run_ids": [], "endpoints": lot_endpoints, } continue # Pass 2a: Pre-scan observations to detect duplicate/conflicting (donor_id, stratum, dose_ug_ml) keys order-independently! obs_key_counts: dict[tuple[str, str, float], int] = {} for r_idx, rec in enumerate(obs_list): if not isinstance(rec, dict): continue raw_did = rec.get("donor_id") did_p = raw_did.strip() if type(raw_did) is str else "" raw_strat = rec.get("stratum") dose_p, dose_p_err = _check_xreact_domain_number( rec.get("dose_ug_ml"), f"{lot_id}.observations[{r_idx}].dose_ug_ml", lo=0.0, hi=None ) if did_p and type(raw_strat) is str and raw_strat in _XREACT_REQUIRED_STRATA and dose_p_err is None and dose_p in _XREACT_REQUIRED_DOSES_UG_ML: k_p = (did_p, raw_strat, dose_p) obs_key_counts[k_p] = obs_key_counts.get(k_p, 0) + 1 conflicted_obs_keys: set[tuple[str, str, float]] = {k for k, cnt in obs_key_counts.items() if cnt > 1} for c_did, c_strat, c_dose in sorted(conflicted_obs_keys): _lot_escalate( "HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"Duplicate/conflicting donor observation rejected (excluded from biological adjudication): " f"donor_id={c_did!r}, stratum={c_strat}, dose={c_dose} (count={obs_key_counts[(c_did, c_strat, c_dose)]})", ) # Pass 2b: Parse & index typed observations by (stratum, dose_ug_ml) -> {donor_id: validated_record} by_sd: dict[tuple[str, float], dict[str, dict[str, Any]]] = {} observed_run_ids: set[str] = set() observed_fab_run_ids: set[str] = set() excluded_invalid_control_rows = 0 linked_endpoint_conflicts_quarantined = 0 lot_inherited_invalid_global_igg = False lot_ind_igg_reconciliation_conflict = False lot_ind_igg_any_row_failed_provenance = False verified_ind_igg_run_ids_used: set[str] = set() for r_idx, rec in enumerate(obs_list): if not isinstance(rec, dict): _lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"observations[{r_idx}] is not a dict") continue raw_did = rec.get("donor_id") if type(raw_did) is not str or not raw_did.strip(): _lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"observations[{r_idx}] missing non-empty string donor_id") continue did = raw_did.strip() raw_strat = rec.get("stratum") if type(raw_strat) is not str or raw_strat not in _XREACT_REQUIRED_STRATA: _lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"observations[{r_idx}] invalid stratum: {raw_strat!r}") continue strat = raw_strat dose_val, dose_err = _check_xreact_domain_number( rec.get("dose_ug_ml"), f"{lot_id}.observations[{r_idx}].dose_ug_ml", lo=0.0, hi=None ) if dose_err is not None or dose_val is None or dose_val not in _XREACT_REQUIRED_DOSES_UG_ML: _lot_escalate( "HOLD_INVALID_OR_INCOMPLETE_ASSAY", dose_err or f"observations[{r_idx}] dose_ug_ml={dose_val!r} not in required 5-point curve {_XREACT_REQUIRED_DOSES_UG_ML}", ) continue dup_key = (did, strat, dose_val) is_conflicted_obs = dup_key in conflicted_obs_keys # R07 B4 + R08 B1: Require non-empty plate_or_run_id covered by finalized passing_ctrl_run_ids for this lot! obs_run_ctrl_valid = True raw_obs_run = rec.get("plate_or_run_id") obs_run_id = raw_obs_run.strip() if type(raw_obs_run) is str else "" if not obs_run_id: obs_run_ctrl_valid = False lot_pos_ctrl_valid = False _lot_escalate( "HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"Observation {did}/{strat}/{dose_val} missing non-empty string plate_or_run_id", ) else: observed_run_ids.add(obs_run_id) if obs_run_id not in passing_ctrl_run_ids: obs_run_ctrl_valid = False lot_pos_ctrl_valid = False _lot_escalate( "HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"Observation {did}/{strat}/{dose_val} plate_or_run_id={obs_run_id!r} not covered by passing lot_positive_controls (covered={sorted(passing_ctrl_run_ids)})", ) igg_baseline_valid = True if rec.get("control_matched_rabbit_igg") is not True: igg_baseline_valid = False _lot_escalate( "HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"Missing matched normal rabbit IgG control flag for {did}/{strat}/{dose_val}", ) # R08 B2 + R09 B1: Reconcile global normal_rabbit_igg_isotype_valid AND all lot/observation/nested # independent matched-IgG provenance declarations strictly BEFORE biological adjudication! # Explicit override semantics: # - Every declared level (lot, observation, nested matched_rabbit_igg) must be unpadded & format-valid. # - If > 1 level declares an independent matched-IgG lot ID, ALL declared levels MUST agree # character-for-character (silent row-level override of a conflicting lot-level ID is PROHIBITED). # - Every claimed independent matched-IgG ID MUST resolve to a verified, non-conflicted run control # record in independent_matched_igg_controls for (lot_id, plate_or_run_id) with matching lot ID. row_ind_igg_valid, row_ind_igg_explicit_bad, row_ind_igg_id, row_ind_igg_err = _parse_independent_igg_provenance( rec, f"{lot_id}.{did}.{strat}.{dose_val}" ) if row_ind_igg_err is not None: igg_baseline_valid = False lot_ind_igg_any_row_failed_provenance = True _lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", row_ind_igg_err) igg_raw = rec.get("matched_rabbit_igg") nested_ind_igg_valid = False nested_ind_igg_explicit_bad = False nested_ind_igg_id: str | None = None if isinstance(igg_raw, dict): nested_ind_igg_valid, nested_ind_igg_explicit_bad, nested_ind_igg_id, nested_ind_igg_err = _parse_independent_igg_provenance( igg_raw, f"{lot_id}.{did}.{strat}.{dose_val}.matched_rabbit_igg" ) if nested_ind_igg_err is not None: igg_baseline_valid = False lot_ind_igg_any_row_failed_provenance = True _lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", nested_ind_igg_err) declared_ind_levels: list[tuple[str, str]] = [] if lot_ind_igg_valid and lot_ind_igg_id is not None: declared_ind_levels.append(("lot", lot_ind_igg_id)) if row_ind_igg_valid and row_ind_igg_id is not None: declared_ind_levels.append(("observation", row_ind_igg_id)) if nested_ind_igg_valid and nested_ind_igg_id is not None: declared_ind_levels.append(("matched_rabbit_igg", nested_ind_igg_id)) cross_level_ind_conflict = len({cid for _, cid in declared_ind_levels}) > 1 if cross_level_ind_conflict: igg_baseline_valid = False lot_ind_igg_reconciliation_conflict = True lot_ind_igg_any_row_failed_provenance = True _lot_escalate( "HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"Conflicting independent_matched_igg_isotype_lot_id across declaration levels " f"({declared_ind_levels}) for {did}/{strat}/{dose_val}; silent row-level override prohibited", ) explicit_ind_bad = ( lot_ind_igg_explicit_bad or row_ind_igg_explicit_bad or nested_ind_igg_explicit_bad or cross_level_ind_conflict ) has_independent_igg_provenance = False matched_ind_ctrl_max_lysis: float | None = None if explicit_ind_bad: igg_baseline_valid = False lot_ind_igg_any_row_failed_provenance = True elif declared_ind_levels: claimed_ind_id = declared_ind_levels[0][1] run_ind_ctrl = valid_ind_igg_by_run.get(obs_run_id) if run_ind_ctrl is None or run_ind_ctrl["control_lot_id"] != claimed_ind_id: igg_baseline_valid = False lot_ind_igg_any_row_failed_provenance = True _lot_escalate( "HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"Untraceable or mismatched independent_matched_igg_isotype_lot_id={claimed_ind_id!r} " f"for run={obs_run_id!r} on {did}/{strat}/{dose_val} " f"(requires matching verified non-conflicted record in independent_matched_igg_controls)", ) else: has_independent_igg_provenance = True matched_ind_ctrl_max_lysis = float(run_ind_ctrl["max_lysis_pct"]) verified_ind_igg_run_ids_used.add(obs_run_id) elif not global_matched_igg_valid: igg_baseline_valid = False lot_inherited_invalid_global_igg = True fab_flag_valid = True if rec.get("fab2_deconvolution_paired") is not True: fab_flag_valid = False _lot_escalate( "HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"Missing paired F(ab')2 vs intact IgG deconvolution flag for {did}/{strat}/{dose_val}", ) parsed_rec: dict[str, Any] = { "donor_id": did, "stratum": strat, "dose_ug_ml": dose_val, "plate_or_run_id": obs_run_id, } # R07 B6: Require direct HLA-E binding retention alongside CD47-SIRPa retention and complement/lysis! req_pct_fields = [ "cd47_sirpa_retention_pct", "hla_e_binding_retention_pct", "c3d_pct", "c5b9_pct", "lysis_pct", ] if strat in _XREACT_CD64T_POS_STRATA: req_pct_fields.append("cd64t_displacement_pct") if strat in _XREACT_SC_ISLET_STRATA: req_pct_fields.append("sc_islet_viability_pct") if strat in _XREACT_IEC_STRATA: req_pct_fields.append("iec_viability_pct") for fkey in req_pct_fields: if fkey not in rec: _lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"Missing {fkey} for {did}/{strat}/{dose_val}") continue fv, ferr = _check_xreact_domain_number( rec[fkey], f"{lot_id}.{did}.{strat}.{dose_val}.{fkey}", lo=0.0, hi=100.0 ) if ferr is not None or fv is None: _lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", ferr or f"Invalid {fkey}") else: parsed_rec[fkey] = fv