|
| 1 | +# drbx blender |
| 2 | + |
| 3 | + |
| 4 | +def blender(drb1, drb3="", drb4="", drb5=""): |
| 5 | + try: |
| 6 | + drb1_1, drb1_2 = drb1.split("+") |
| 7 | + drb1_allele_1 = drb1_1.split("*")[1] |
| 8 | + drb1_allele_2 = drb1_2.split("*")[1] |
| 9 | + drb1_fam_1 = drb1_allele_1.split(":")[0] |
| 10 | + drb1_fam_2 = drb1_allele_2.split(":")[0] |
| 11 | + except Exception: |
| 12 | + return "" |
| 13 | + x1 = expdrbx(drb1_fam_1) |
| 14 | + x2 = expdrbx(drb1_fam_2) |
| 15 | + xx = "".join(sorted([x1, x2])) |
| 16 | + |
| 17 | + if xx == "00": |
| 18 | + if drb3 != "": |
| 19 | + raise DRBXBlenderError("DRB3", "none") |
| 20 | + if drb4 != "": |
| 21 | + raise DRBXBlenderError("DRB4", "none") |
| 22 | + if drb5 != "": |
| 23 | + raise DRBXBlenderError("DRB5", "none") |
| 24 | + return "" |
| 25 | + |
| 26 | + # handle 03 |
| 27 | + if xx == "03": |
| 28 | + if drb4 != "": |
| 29 | + raise DRBXBlenderError("DRB4", "none") |
| 30 | + if drb5 != "": |
| 31 | + raise DRBXBlenderError("DRB5", "none") |
| 32 | + if drb3 != "": |
| 33 | + # if 2 copies |
| 34 | + drb3_g = drb3.split("+") |
| 35 | + if len(drb3_g) == 2: |
| 36 | + # homozygous, return one copy |
| 37 | + if drb3_g[1] == drb3_g[0]: |
| 38 | + return drb3_g[0] |
| 39 | + else: |
| 40 | + raise DRBXBlenderError("DRB3 het", "hom") |
| 41 | + else: |
| 42 | + return drb3 |
| 43 | + return "" |
| 44 | + |
| 45 | + # handle 04 |
| 46 | + if xx == "04": |
| 47 | + if drb3 != "": |
| 48 | + raise DRBXBlenderError("DRB3", "none") |
| 49 | + if drb5 != "": |
| 50 | + raise DRBXBlenderError("DRB5", "none") |
| 51 | + if drb4 != "": |
| 52 | + # if 2 copies |
| 53 | + drb4_g = drb4.split("+") |
| 54 | + if len(drb4_g) == 2: |
| 55 | + # homozygous, return one copy |
| 56 | + if drb4_g[1] == drb4_g[0]: |
| 57 | + return drb4_g[0] |
| 58 | + else: |
| 59 | + raise DRBXBlenderError("DRB4 het", "hom") |
| 60 | + else: |
| 61 | + return drb4 |
| 62 | + return "" |
| 63 | + |
| 64 | + # handle 05 |
| 65 | + if xx == "05": |
| 66 | + if drb3 != "": |
| 67 | + raise DRBXBlenderError("DRB3", "none") |
| 68 | + if drb4 != "": |
| 69 | + raise DRBXBlenderError("DRB4", "none") |
| 70 | + if drb5 != "": |
| 71 | + # if 2 copies |
| 72 | + drb5_g = drb5.split("+") |
| 73 | + if len(drb5_g) == 2: |
| 74 | + # homozygous, return one copy |
| 75 | + if drb5_g[1] == drb5_g[0]: |
| 76 | + return drb5_g[0] |
| 77 | + else: |
| 78 | + raise DRBXBlenderError("DRB5 het", "hom") |
| 79 | + else: |
| 80 | + return drb5 |
| 81 | + return "" |
| 82 | + # handle 33 |
| 83 | + if xx == "33": |
| 84 | + if drb4 != "": |
| 85 | + raise DRBXBlenderError("DRB4", "none") |
| 86 | + if drb5 != "": |
| 87 | + raise DRBXBlenderError("DRB5", "none") |
| 88 | + if drb3 != "": |
| 89 | + return drb3 |
| 90 | + return "" |
| 91 | + |
| 92 | + # handle 44 |
| 93 | + if xx == "44": |
| 94 | + if drb3 != "": |
| 95 | + raise DRBXBlenderError("DRB3", "none") |
| 96 | + if drb5 != "": |
| 97 | + raise DRBXBlenderError("DRB5", "none") |
| 98 | + if drb4 != "": |
| 99 | + return drb4 |
| 100 | + return "" |
| 101 | + |
| 102 | + # handle 55 |
| 103 | + if xx == "55": |
| 104 | + if drb3 != "": |
| 105 | + raise DRBXBlenderError("DRB3", "none") |
| 106 | + if drb4 != "": |
| 107 | + raise DRBXBlenderError("DRB4", "none") |
| 108 | + if drb5 != "": |
| 109 | + return drb5 |
| 110 | + return "" |
| 111 | + |
| 112 | + # handle 34 |
| 113 | + if xx == "34": |
| 114 | + if drb5 != "": |
| 115 | + raise DRBXBlenderError("DRB5", "none") |
| 116 | + retg = [] |
| 117 | + |
| 118 | + if drb3 != "": |
| 119 | + # if 2 copies |
| 120 | + drb3_g = drb3.split("+") |
| 121 | + if len(drb3_g) == 2: |
| 122 | + # homozygous, return one copy |
| 123 | + if drb3_g[1] == drb3_g[0]: |
| 124 | + retg.append(drb3_g[0]) |
| 125 | + else: |
| 126 | + raise DRBXBlenderError("DRB3 het", "hom") |
| 127 | + elif len(drb3_g) == 1: |
| 128 | + retg.append(drb3_g[0]) |
| 129 | + if drb4 != "": |
| 130 | + # if 2 copies |
| 131 | + drb4_g = drb4.split("+") |
| 132 | + if len(drb4_g) == 2: |
| 133 | + # homozygous, return one copy |
| 134 | + if drb4_g[1] == drb4_g[0]: |
| 135 | + retg.append(drb4_g[0]) |
| 136 | + else: |
| 137 | + raise DRBXBlenderError("DRB4 het", "hom") |
| 138 | + elif len(drb4_g) == 1: |
| 139 | + retg.append(drb4_g[0]) |
| 140 | + |
| 141 | + return "+".join(retg) |
| 142 | + |
| 143 | + # handle 35 |
| 144 | + if xx == "35": |
| 145 | + if drb4 != "": |
| 146 | + raise DRBXBlenderError("DRB4", "none") |
| 147 | + retg = [] |
| 148 | + |
| 149 | + if drb3 != "": |
| 150 | + # if 2 copies |
| 151 | + drb3_g = drb3.split("+") |
| 152 | + if len(drb3_g) == 2: |
| 153 | + # homozygous, return one copy |
| 154 | + if drb3_g[1] == drb3_g[0]: |
| 155 | + retg.append(drb3_g[0]) |
| 156 | + else: |
| 157 | + raise DRBXBlenderError("DRB3 het", "hom") |
| 158 | + elif len(drb3_g) == 1: |
| 159 | + retg.append(drb3_g[0]) |
| 160 | + if drb5 != "": |
| 161 | + # if 2 copies |
| 162 | + drb5_g = drb5.split("+") |
| 163 | + if len(drb5_g) == 2: |
| 164 | + # homozygous, return one copy |
| 165 | + if drb5_g[1] == drb5_g[0]: |
| 166 | + retg.append(drb5_g[0]) |
| 167 | + else: |
| 168 | + raise DRBXBlenderError("DRB5 het", "hom") |
| 169 | + elif len(drb5_g) == 1: |
| 170 | + retg.append(drb5_g[0]) |
| 171 | + |
| 172 | + return "+".join(retg) |
| 173 | + |
| 174 | + # handle 45 |
| 175 | + if xx == "45": |
| 176 | + if drb3 != "": |
| 177 | + raise DRBXBlenderError("DRB3", "none") |
| 178 | + retg = [] |
| 179 | + |
| 180 | + if drb4 != "": |
| 181 | + # if 2 copies |
| 182 | + drb4_g = drb4.split("+") |
| 183 | + if len(drb4_g) == 2: |
| 184 | + # homozygous, return one copy |
| 185 | + if drb4_g[1] == drb4_g[0]: |
| 186 | + retg.append(drb4_g[0]) |
| 187 | + else: |
| 188 | + raise DRBXBlenderError("DRB4 het", "hom") |
| 189 | + elif len(drb4_g) == 1: |
| 190 | + retg.append(drb4_g[0]) |
| 191 | + if drb5 != "": |
| 192 | + # if 2 copies |
| 193 | + drb5_g = drb5.split("+") |
| 194 | + if len(drb5_g) == 2: |
| 195 | + # homozygous, return one copy |
| 196 | + if drb5_g[1] == drb5_g[0]: |
| 197 | + retg.append(drb5_g[0]) |
| 198 | + else: |
| 199 | + raise DRBXBlenderError("DRB5 het", "hom") |
| 200 | + elif len(drb5_g) == 1: |
| 201 | + retg.append(drb5_g[0]) |
| 202 | + |
| 203 | + return "+".join(retg) |
| 204 | + |
| 205 | + print("blender fail", xx, drb1_fam_1, drb1_fam_2) |
| 206 | + return "" |
| 207 | + |
| 208 | + |
| 209 | +def expdrbx(drb1_fam): |
| 210 | + if drb1_fam in ["03", "05", "06", "11", "12", "13", "14"]: |
| 211 | + return "3" |
| 212 | + if drb1_fam in ["04", "07", "09"]: |
| 213 | + return "4" |
| 214 | + if drb1_fam in ["02", "15", "16"]: |
| 215 | + return "5" |
| 216 | + return "0" |
| 217 | + |
| 218 | + |
| 219 | +class DRBXBlenderError(Exception): |
| 220 | + def __init__(self, found, expected): |
| 221 | + self.found = found |
| 222 | + self.expected = expected |
| 223 | + |
| 224 | + def __str__(self): |
| 225 | + return f"{self.found} where {self.expected} expected" |
0 commit comments