@@ -39,6 +39,8 @@ def _init_results_df(donors_info):
3939 fields_in_results = {
4040 "Patient_ID" : [],
4141 "Donor_ID" : [],
42+ "GvH_Mismatches" : [],
43+ "HvG_Mismatches" : [],
4244 "Number_Of_Mismatches" : [],
4345 "Matching_Probability" : [],
4446 "Match_Probability_A_1" : [],
@@ -255,6 +257,35 @@ def __classes_and_subclasses_from_genotype(self, genotype: HashableArray):
255257
256258 return int_classes , subclasses
257259
260+ def count_GvH_HvG (
261+ self ,
262+ pat_geno : Sequence [int ],
263+ don_geno : Sequence [int ],
264+ ) -> Tuple [int , int ]:
265+ """
266+ Count GvH and HvG mismatches locus by locus by set‐difference.
267+ Each locus is two slots in the genotype lists:
268+ A: indices [0,1], B: [2,3], C: [4,5], DQB1: [6,7], DRB1: [8,9]
269+ We drop any “N” (here encoded as 0 or None), then:
270+ GvH = | patient_set – donor_set |
271+ HvG = | donor_set – patient_set |
272+ Sum over all five loci.
273+ """
274+ total_gvh = 0
275+ total_hvg = 0
276+
277+ for i in range (0 , 10 , 2 ):
278+ # build the allele sets, filtering out N/None/0
279+ p_set = {a for a in (pat_geno [i ], pat_geno [i + 1 ]) if a not in (None , 0 )}
280+ d_set = {a for a in (don_geno [i ], don_geno [i + 1 ]) if a not in (None , 0 )}
281+
282+ # how many the patient has that the donor doesn’t:
283+ total_gvh += len (p_set - d_set )
284+ # how many the donor has that the patient doesn’t:
285+ total_hvg += len (d_set - p_set )
286+
287+ return total_gvh , total_hvg
288+
258289 def create_patients_graph (self , f_patients : str ):
259290 """
260291 create patients graph. \n
@@ -597,9 +628,16 @@ def __append_matching_donor(
597628
598629 add_donors ["Matching_Probability" ].append (match_prob )
599630 add_donors ["Number_Of_Mismatches" ].append (mm_number )
600- add_donors ["Permissive/Non-Permissive" ].append (
601- "-"
602- ) # TODO: add permissiveness algorithm
631+
632+ # compute GvH / HvG counts
633+ pat = self .patients [patient ]
634+ don = self .get_most_common_genotype (donor )
635+ gvh , hvg = self .count_GvH_HvG (pat , don )
636+ add_donors ["GvH_Mismatches" ].append (gvh )
637+ add_donors ["HvG_Mismatches" ].append (hvg )
638+
639+ add_donors ["Permissive/Non-Permissive" ].append ("-" )
640+ # TODO: add permissiveness algorithm
603641
604642 # add the other given fields to the results
605643 for field in donors_info :
0 commit comments