Skip to content

grammar_GARC1

All logic required to parse and generate predictions from a catalogue in GARC1

infer_mutation_affects(position)

Find out which part of the gene this position affects

Parameters:

Name Type Description Default
position int

Gene position

required

Returns:

Name Type Description
str str

Either "PROM" or "CDS"

Source code in piezo/grammar_GARC1.py
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
def infer_mutation_affects(position: int) -> str:
    """Find out which part of the gene this position affects

    Args:
        position (int): Gene position

    Returns:
        str: Either "PROM" or "CDS"
    """

    if position < 0:
        mutation_affects = "PROM"
    else:
        mutation_affects = "CDS"
    return mutation_affects

large_del(predictions, rules, size, minor)

Row prediction, but specifically for large deletions

Parameters:

Name Type Description Default
predictions {int

str}): Predictions dictionary

required
rules DataFrame

Rules from the catalogue

required
size float

Percentage of the gene deleted (as a decimal)

required
minor float

Minors (if existing)

required
Source code in piezo/grammar_GARC1.py
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
def large_del(
    predictions: Dict[int, Tuple[str, Dict]],
    rules: pandas.DataFrame,
    size: float,
    minor: float | None,
) -> None:
    """Row prediction, but specifically for large deletions

    Args:
        predictions ({int: str}): Predictions dictionary
        rules (pandas.DataFrame): Rules from the catalogue
        size (float): Percentage of the gene deleted (as a decimal)
        minor (float): Minors (if existing)
    """
    pred = None
    current: float = -1
    # Find which rules are large deletions and see if the mutation is >= rule
    deletion = re.compile(r"""del_([01]\.[0-9]+)""")
    for _, rule in rules.iterrows():
        del_match = deletion.fullmatch(rule["MUTATION"])
        if del_match is not None:
            percentage = float(del_match.groups()[0])
            if size >= percentage and percentage > current:
                # Match!
                if minor is None and rule["MINOR"] == "":
                    # Neither are minor populations so act normally
                    pred = rule["PREDICTION"]
                    evidence = rule["EVIDENCE"]
                    current = percentage
                elif (
                    minor is not None
                    and rule["MINOR"] != ""
                    and minor < 1
                    and float(rule["MINOR"]) < 1
                ):
                    # We have FRS
                    if minor >= float(rule["MINOR"]):
                        # Match
                        pred = rule["PREDICTION"]
                        evidence = rule["EVIDENCE"]
                        current = percentage

                elif (
                    minor is not None
                    and rule["MINOR"] != ""
                    and minor >= 1
                    and float(rule["MINOR"]) >= 1
                ):
                    # We have COV
                    if minor >= float(rule["MINOR"]):
                        # Match
                        pred = rule["PREDICTION"]
                        evidence = rule["EVIDENCE"]
                        current = percentage
    if pred:
        predictions[1] = (str(pred), evidence)

match_multi(rule, mutation, catalogue)

Determine if a given mutation matches a given rule. This takes into account wildcards.

Parameters:

Name Type Description Default
rule Series

Rule to check for

required
mutation str

Mutation to check for

required
catalogue Catalogue

Catalogue this rule is from

required

Returns:

Name Type Description
bool bool

True if this mutation matches the rule

Source code in piezo/grammar_GARC1.py
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
def match_multi(rule: pandas.Series, mutation: str, catalogue: Catalogue) -> bool:
    """Determine if a given mutation matches a given rule.
    This takes into account wildcards.

    Args:
        rule (pandas.Series): Rule to check for
        mutation (str): Mutation to check for
        catalogue (Catalogue): Catalogue this rule is from

    Returns:
        bool: True if this mutation matches the rule
    """
    if rule["MUTATION"] == mutation:
        # Literal match
        return True

    rule_mutations = rule["MUTATION"].split("&")

    if len(rule_mutations) != len(mutation.split("&")):
        # Not same number of mutations in the rule so not a match
        return False

    # Bit more tricky here as we need to check that every part of the mutation matches
    #   a single part of the rule
    # do this by constructing a dummy catalogue from the multi-rule and checking each
    #   part of the mutation hits it

    # First create a catalogue with just the rules in this multi
    dummy_cat = {
        "EVIDENCE": [
            ujson.dumps({"Rule hit": idx}) for (idx, _) in enumerate(rule_mutations)
        ],
        "MUTATION": rule_mutations,
        "DRUG": ["na" for _ in rule_mutations],
        "PREDICTION": ["R" for _ in rule_mutations],
    }
    r = pandas.DataFrame(dummy_cat)
    r[
        ["GENE", "MUTATION", "POSITION", "MUTATION_AFFECTS", "MUTATION_TYPE", "MINOR"]
    ] = r.apply(split_mutation, axis=1)

    # As this is a named **tuple**, it is immutable so create a new one
    cat = Catalogue(
        catalogue.genbank_reference,
        catalogue.name,
        catalogue.version,
        catalogue.grammar,
        "R",  # Obviously not R but RFUS are needed here
        ["na"],
        r["GENE"].tolist(),
        {"na": r["GENE"].tolist()},
        {gene: ["na"] for gene in r["GENE"].tolist()},
        len(r),
        r,
    )

    for mut in mutation.split("&"):
        try:
            pred = predict_GARC1(cat, mut, True)
            if isinstance(pred, str):
                # pred == "S":
                # No results so not matched
                return False

            # Pull out the multi-rule index this hit from the evidence
            pred_ev = ujson.loads(pred["na"][1])

            rule_idx = int(pred_ev["Rule hit"])
            r.drop([rule_idx], inplace=True)

            # Rebuild the catalogue object with the updated rule set
            cat = Catalogue(
                catalogue.genbank_reference,
                catalogue.name,
                catalogue.version,
                catalogue.grammar,
                "R",  # Obviously not R but RFUS are needed here
                ["na"],
                r["GENE"].tolist(),
                {"na": r["GENE"].tolist()},
                {gene: ["na"] for gene in r["GENE"].tolist()},
                len(r),
                r,
            )
        except ValueError:
            # No rules for this drugs etc
            return False

    if len(r) == 0:
        # Must have matched every rule in the multi
        return True

    # Not matched everything, so not a match
    return False

merge_predictions(predictions, catalogue)

When multi-mutations do not have a hit, they are decomposed and individuals are tried instead This merges these predictions based on the prioritisation defined for this catalogue

Parameters:

Name Type Description Default
predictions {str

{str: str}}): Dictionary mapping mutation -> effects for each mutation in the multi

required
catalogue namedtuple

The catalogue object (for finding the values)

required

Returns: {str: str}: Merged effects dict mapping drug name -> prediction. Or "S" if no matches

Source code in piezo/grammar_GARC1.py
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
def merge_predictions(
    predictions: Dict[str, Dict[str, Tuple] | Dict[str, str]], catalogue: Catalogue
) -> Dict[str, Tuple] | str:
    """When multi-mutations do not have a hit, they are decomposed and individuals are
        tried instead
    This merges these predictions based on the prioritisation defined for this catalogue

    Args:
        predictions ({str: {str: str}}): Dictionary mapping mutation -> effects for
            each mutation in the multi
        catalogue (collections.namedtuple): The catalogue object
            (for finding the values)
    Returns:
        {str: str}: Merged effects dict mapping drug name -> prediction. Or "S" if
            no matches
    """
    # Pull out all of the drugs which have predictions
    drugs = set()
    for pred in predictions:
        for drug in predictions[pred].keys():
            drugs.add(drug)

    # Default to 'S' for now
    merged: Dict[str, Tuple] = {drug: ("S", dict()) for drug in drugs}

    # Look for all predictions for each drug, and report the most significant
    for drug in drugs:
        for mutation in predictions.keys():
            # Get the prediction for this drug (if exists)
            this_pred = predictions[mutation][drug]

            if isinstance(this_pred, str):
                continue
            else:
                pred, evidence = this_pred

            if catalogue.values.index(pred) <= catalogue.values.index(merged[drug][0]):
                # New highest priority, so set the prediction
                merged[drug] = (pred, evidence)

    # If we have >=1 non-'S' prediction, return the dict
    if len([key for key in merged.keys() if merged[key][0] != "S"]) > 0:
        return {drug: merged[drug] for drug in merged.keys() if merged[drug][0] != "S"}

    # Else give the default 'S'
    return "S"

parse_mutation(mutation)

Take a GENE_MUTATION, and determine what type it is, what it affects etc

Parameters:

Name Type Description Default
mutation str

in the gene_mutation form as defined by GARC e.g. rpoB@S450L or rpoB@1300_ins_3

required

Returns:

Name Type Description
position int

the position of the mutation. This is the amino acid number in a protein, or the number of the nucleotide in the promoter, or a wildcard like '' or '-'

mutation_affects str

whether this mutation affects the promoter (PROM) or coding sequence (CDS)

mutation_type str

is it a SNP or an INDEL?

indel_type str

indel, ins, del or fs

indel_length float

if sufficient information is given, the number of bases in the INDEL

indel_bases str

if sufficient information is given, the bases in the INDEL

before str

the REF amino acid

after str

the ALT amino acid

Source code in piezo/grammar_GARC1.py
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
def parse_mutation(
    mutation: str,
) -> Tuple[
    int | None,
    str | None,
    str | None,
    str | None,
    float | None,
    str | None,
    str | None,
    str | None,
]:
    """
    Take a GENE_MUTATION, and determine what type it is, what it affects etc

    Args:
        mutation (str): in the gene_mutation form as defined by GARC e.g. rpoB@S450L or
            rpoB@1300_ins_3

    Returns:
        position (int): the position of the mutation. This is the amino acid number in
            a protein, or the number of the nucleotide in the promoter, or a
            wildcard like '*' or '-*'
        mutation_affects (str): whether this mutation affects the promoter (PROM) or
            coding sequence (CDS)
        mutation_type (str): is it a SNP or an INDEL?
        indel_type (str): indel, ins, del or fs
        indel_length (float): if sufficient information is given, the number of bases
            in the INDEL
        indel_bases (str): if sufficient information is given, the bases in the INDEL
        before (str): the REF amino acid
        after (str): the ALT amino acid

    """

    # set default values
    mutation_type = None
    mutation_affects = None
    position = None
    indel_type = None
    indel_length = None
    indel_bases = None
    before = None
    after = None

    # split the mutation on underscore
    cols = mutation.split("_")

    # infer what type it is depending on how many elements it contains
    if len(cols) == 1:
        mutation_type = "SNP"

        position = int(mutation[1:-1])

        mutation_affects = infer_mutation_affects(position)

        before = mutation[0]
        after = mutation[-1]

    elif len(cols) in [2, 3]:
        mutation_type = "INDEL"
        # Checking for large deletions
        percentage = False
        if cols[0] == "del":
            try:
                float(cols[1])
                percentage = True
            except ValueError:
                percentage = False

        if percentage:
            mutation_affects = "GENE"
            indel_type = "del"
            indel_length = float(cols[1])
        else:
            try:
                position = int(cols[0])
            except ValueError:
                assert False, "Invalid mutation: " + mutation
            mutation_affects = infer_mutation_affects(position)

            # the third element is one of indel, ins, del or the special case fs
            indel_type = cols[1]

            assert indel_type in ["indel", "ins", "del", "fs", "mixed"], (
                "form of indel not recognised: " + indel_type
            )

            # if there is a fourth and final element to an INDEL it is either _4 or
            #    _ctgc
            if len(cols) == 3:
                assert indel_type in ["ins", "del"], (
                    "form of indel does not make sense when length "
                    + "or bases specified!: "
                    + indel_type
                )
                try:
                    indel_length = int(cols[2])
                except ValueError:
                    indel_length = len(cols[2])
                    indel_bases = cols[2]
                    assert 0 not in [
                        c in ["a", "t", "c", "g", "z", "x"] for c in indel_bases
                    ], "only nucleotides of a,t,c,g,z,x are allowed!"

                assert indel_length > 0, (
                    "makes no sense to have a negative indel length! " + cols[2]
                )

    assert mutation_type in [
        "INDEL",
        "SNP",
    ], "form of mutation_type not recognised: " + str(mutation_type)

    # insist we've been given an amino acid or a wildcard only
    if mutation_type == "SNP":
        sanity_check_snp(before, after)

    return (
        position,
        mutation_affects,
        mutation_type,
        indel_type,
        indel_length,
        indel_bases,
        before,
        after,
    )

predict_GARC1(catalogue, gene_mutation, show_evidence=False)

For the GARC1 grammar, predict the effect of the supplied gene_mutation.

Parameters:

Name Type Description Default
catalogue namedtuple

defines the resistance catalogue

required
gene_mutation str

as specified by the GARC1 grammar, e.g. rpoB@S450L, fabG1@c-15t, ahpC@128_indel

required
show_evidence bool

Flag for whether to return the evidence for this prediction

False

Returns:

Type Description
Dict[str, Tuple] | Dict[str, str] | str

{str: (str, str) | str} | str: Either a dictionary mapping drug name -> (prediction, evidence) if show_evidence is True or "S" if no hits in the catalogue

Source code in piezo/grammar_GARC1.py
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
def predict_GARC1(
    catalogue: Catalogue, gene_mutation: str, show_evidence: bool = False
) -> Dict[str, Tuple] | Dict[str, str] | str:
    """
    For the GARC1 grammar, predict the effect of the supplied gene_mutation.

    Args:
        catalogue (collections.namedtuple): defines the resistance catalogue
        gene_mutation (str): as specified by the GARC1 grammar, e.g. rpoB@S450L,
            fabG1@c-15t, ahpC@128_indel
        show_evidence (bool, optional): Flag for whether to return the evidence for
            this prediction

    Returns:
        {str: (str, str) | str} | str: Either a dictionary mapping drug name ->
            (prediction, evidence) if show_evidence is True or "S" if no hits
            in the catalogue
    """

    # create the result dictionary e.g. {"RIF":'R', "RFB":'R'}
    # most of the time the predictions will be identical, but this allows them to
    #   diverge in future
    result: Dict[str, Tuple] = {}

    if "&" in gene_mutation:
        # Multi-gene so treat differently
        result_multi: Dict[str, Tuple] | str = predict_multi(catalogue, gene_mutation)
        if show_evidence or isinstance(result_multi, str):
            return result_multi
        else:
            return {key: result_multi[key][0] for key in result_multi.keys()}

    components = gene_mutation.split("@")

    gene = components[0]
    mutation = gene_mutation.split(gene + "@")[1]

    # Check for minors
    if ":" in mutation:
        try:
            minor = float(mutation.split(":")[-1])
        except ValueError:
            assert False, "Malformed mutation! " + mutation
        mutation = mutation.split(":")[0]
        assert minor > 0, "Minor population given with no evidence! " + mutation
    else:
        minor = None

    # parse the mutation to work out what type of mutation it is, where it acts etc
    (
        position,
        mutation_affects,
        mutation_type,
        indel_type,
        indel_length,
        indel_bases,
        before,
        after,
    ) = parse_mutation(mutation)

    # if the gene isn't in the catalogue, then assume it has no effect
    if gene not in catalogue.genes:
        return "S"

    # find out the drugs to which changes in this gene can confer resistance
    drugs = catalogue.gene_lookup[gene]

    # create the vectors of Booleans that will apply
    position_vector = catalogue.rules.POSITION.isin(
        [position, str(position), "*", "-*"]
    )
    mutation_affects_vector = catalogue.rules.MUTATION_AFFECTS == mutation_affects
    mutation_type_vector = catalogue.rules.MUTATION_TYPE == mutation_type
    gene_vector = catalogue.rules.GENE == gene
    subset_vector = (
        position_vector & mutation_affects_vector & mutation_type_vector & gene_vector
    )
    # deal with each compound, one at a time
    for compound in drugs:
        subset_rules = catalogue.rules.loc[
            (subset_vector) & (catalogue.rules.DRUG == compound)
        ]

        # prepare a dictionary to store hits with the priority as the key:
        #   e.g. {10:'R',5:'U'}
        predictions: Dict[int, Tuple[str, Dict]] = {}

        if not subset_rules.empty:
            subset_position_vector = subset_rules.POSITION == str(position)
            subset_mutation_type_vector = subset_rules.MUTATION_TYPE == mutation_type

            if mutation_type == "SNP":
                process_snp_variants(
                    mutation_affects,
                    predictions,
                    before,
                    after,
                    mutation,
                    subset_rules,
                    subset_mutation_type_vector,
                    subset_position_vector,
                    minor,
                )
            elif mutation_type == "INDEL":
                process_indel_variants(
                    mutation_affects,
                    predictions,
                    subset_rules,
                    subset_mutation_type_vector,
                    subset_position_vector,
                    indel_length,
                    indel_type,
                    indel_bases,
                    position,
                    minor,
                )

        if not predictions:
            # all mutations should hit at least one of the default entries, so if this
            #   doesn't happen, something is wrong UNLESS the mutation given is a minor allele
            if ":" in gene_mutation:
                result[compound] = ("S", {})
            else:
                raise ValueError(
                    "No entry found in the catalogue for "
                    + gene_mutation
                    + " "
                    + compound
                )
        else:
            final_prediction: Tuple = predictions[sorted(predictions)[-1]]
            result[compound] = final_prediction

    if show_evidence or isinstance(result, str):
        return result
    else:
        return {key: result[key][0] for key in result.keys()}

predict_multi(catalogue, gene_mutation)

Get the predictions for a given multi-mutation. Mutli-mutations are (currently) a lot stricter than others, and do not support wildcards or subsets of the mutations

Parameters:

Name Type Description Default
catalogue namedtuple

The resistance catalogue

required
gene_mutation str

String of the mutations, each in GARC, separated by &

required

Returns: {str: str} | str : Either a dict with drugs as the keys, or 'S' when no predictions in catalogue

Source code in piezo/grammar_GARC1.py
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
def predict_multi(catalogue: Catalogue, gene_mutation: str) -> Dict[str, Tuple] | str:
    """Get the predictions for a given multi-mutation.
    Mutli-mutations are (currently) a lot stricter than others, and do not support
        wildcards or subsets of the mutations

    Args:
        catalogue (collections.namedtuple): The resistance catalogue
        gene_mutation (str): String of the mutations, each in GARC, separated by `&`
    Returns:
        {str: str} | str : Either a dict with drugs as the keys, or 'S' when no
            predictions in catalogue
    """
    # Ensure that the mutations are in a reproducable order
    sorted_mutation = "&".join(sorted(list(gene_mutation.split("&"))))

    # Check for minor populations (and remove, we'll check these separately)
    minors = [
        float(mut.split(":")[-1]) if ":" in mut else None
        for mut in sorted_mutation.split("&")
    ]
    sorted_mutation = "&".join(
        [mut.split(":")[0] for mut in sorted_mutation.split("&")]
    )

    # Check epistasis first
    epi_rules = catalogue.rules[catalogue.rules["MUTATION_TYPE"] == "EPISTASIS"]
    # Note the use of "" here instead of "{}" to allow empty evidence fields
    epi_drugs = {drug: ("S", "") for drug in catalogue.drugs}
    values = catalogue.values
    for _, rule in epi_rules.iterrows():
        if match_multi(rule, sorted_mutation, catalogue):
            # Epistasis rule hit
            drug = rule["DRUG"]
            if epi_drugs[drug][1] != "":
                # This drug already has an epistasis rule hit so throw an error
                raise ValueError(
                    f"Conflicting epistasis rules for {gene_mutation}:{drug}! "
                    "Check your catalogue!"
                )
            # Valid, so check for minors
            for cat, minor in zip(rule["MINOR"].split(","), minors):
                if minor is None and cat == "":
                    # Neither are minors so add
                    epi_drugs[drug] = (rule["PREDICTION"], rule["EVIDENCE"])
                elif cat != "" and minor is not None and minor < 1 and float(cat) < 1:
                    # FRS
                    if minor >= float(cat):
                        # Match
                        epi_drugs[drug] = (rule["PREDICTION"], rule["EVIDENCE"])
                elif cat != "" and minor is not None and minor >= 1 and float(cat) >= 1:
                    # COV
                    if minor >= float(cat):
                        # Match
                        epi_drugs[drug] = (rule["PREDICTION"], rule["EVIDENCE"])

    # Get the multi rules
    multi_rules = catalogue.rules[catalogue.rules["MUTATION_TYPE"] == "MULTI"]

    drugs = {drug: ("S", "{}") for drug in catalogue.drugs}
    for _, rule in multi_rules.iterrows():
        drug = rule["DRUG"]
        if epi_drugs[drug][1] != "":
            # Epistasis rule already covers this, so skip it
            continue
        if match_multi(rule, sorted_mutation, catalogue):
            # We have a match! Prioritise predictions based on values
            if values.index(rule["PREDICTION"]) < values.index(drugs[drug][0]):
                # The prediction is closer to the start of the values list, so should
                #   take priority
                # Check for minor populations first though
                for cat, minor in zip(rule["MINOR"].split(","), minors):
                    if minor is None and cat == "":
                        # Neither are minors so add
                        drugs[drug] = (rule["PREDICTION"], rule["EVIDENCE"])
                    elif (
                        cat != "" and minor is not None and minor < 1 and float(cat) < 1
                    ):
                        # FRS
                        if minor >= float(cat):
                            # Match
                            drugs[drug] = (rule["PREDICTION"], rule["EVIDENCE"])
                    elif (
                        cat != ""
                        and minor is not None
                        and minor >= 1
                        and float(cat) >= 1
                    ):
                        # COV
                        if minor >= float(cat):
                            # Match
                            drugs[drug] = (rule["PREDICTION"], rule["EVIDENCE"])

    # Check to ensure we have at least 1 prediction
    if len([key for key in epi_drugs.keys() if epi_drugs[key][1] != ""]) > 0:
        # Epistasis hit(s) so join with the multis
        to_return = drugs
        for drug in epi_drugs:
            if epi_drugs[drug][1] != "":
                # This drug has an epi match
                to_return[drug] = epi_drugs[drug]
            elif drugs[drug][0] == "S":
                # Drop default `S` predictions
                del to_return[drug]
        # For whatever reason, this appeases mypy
        return {key: value for key, value in to_return.items()}

    if len([key for key in drugs.keys() if drugs[key][0] != "S"]) > 0:
        # At least one multi hit
        return {drug: drugs[drug] for drug in drugs.keys() if drugs[drug][0] != "S"}

    # Nothing predicted, so try each individual mutation
    predictions = {}
    for mutation, minor in zip(sorted_mutation.split("&"), minors):
        # Put minor populations back into mutations as required
        if minor is not None:
            mutation = mutation + ":" + str(minor)

        # Get the prediction for the mutation
        pred = predict_GARC1(catalogue, mutation, True)
        if isinstance(pred, str):
            # We have a 'S' prediction so ignore it
            continue
        else:
            predictions[mutation] = pred

    return merge_predictions(predictions, catalogue)

process_catalogue_GARC1(rules, drugs, catalogue_genes_only)

For the GARC1 grammar, add some additional columns to the rules dataframe that can be inferred from the mutation.

Parameters:

Name Type Description Default
rules DataFrame

Pandas dataframe of the AMR catalogue in the general form

required
drugs [str]

list of drugs in the AMR catalogue

required
catalogue_genes_only bool

whether to subset the catalogue down so it ONLY includes (DRUG,GENE) pairs that include at least one row predicting resistance

required

Returns: (pandas.DataFrame, [str], {str: [str]}, {str: [str]}) : Tuple of values. In order: DataFrame of the rules List of the genes Dictionary mapping drug name -> [gene names] Dictionary mapping gene name -> [drug names]

Source code in piezo/grammar_GARC1.py
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
def process_catalogue_GARC1(
    rules: pandas.DataFrame, drugs: List[str], catalogue_genes_only: bool
) -> Tuple[pandas.DataFrame, List[str], Dict[str, List[str]], Dict[str, List[str]]]:
    """
    For the GARC1 grammar, add some additional columns to the rules dataframe that
        can be inferred from the mutation.

    Args:
        rules (pandas.DataFrame): Pandas dataframe of the AMR catalogue in the
            general form
        drugs ([str]): list of drugs in the AMR catalogue
        catalogue_genes_only (bool): whether to subset the catalogue down so it ONLY
            includes (DRUG,GENE) pairs that include at least one row predicting
            resistance
    Returns:
        (pandas.DataFrame, [str], {str: [str]}, {str: [str]}) : Tuple of values.
            In order:
                DataFrame of the rules
                List of the genes
                Dictionary mapping drug name -> [gene names]
                Dictionary mapping gene name -> [drug names]

    """

    # infer these columns
    rules[
        ["GENE", "MUTATION", "POSITION", "MUTATION_AFFECTS", "MUTATION_TYPE", "MINOR"]
    ] = rules.apply(split_mutation, axis=1)

    if catalogue_genes_only:
        # create an index of which (drug,gene) pairs have at least one row predicting
        #   resistance or uncertainty
        drug_list_index = (
            rules.loc[(rules.PREDICTION == "R") | (rules.PREDICTION == "U")][
                ["DRUG", "GENE"]
            ]
            .groupby(["DRUG", "GENE"])
            .count()
        )

        # Exclude multi/epistasis rules from this as epistasis rules override R
        multis = rules[
            (rules["MUTATION_TYPE"] == "EPISTASIS")
            | (rules["MUTATION_TYPE"] == "MULTI")
        ]

        # index the rules so we can right join
        rules.set_index(["DRUG", "GENE"], inplace=True)

        # do the right join; this has the effect of removing all the rows for
        #   "genes of interest"
        rules = rules.join(drug_list_index, how="right")

        # remove the index
        rules.reset_index(inplace=True)

        # Add back multi/epistais rules now
        rules = pandas.concat([rules, multis])
        # Some multis (but not all) can be duplicated here, so drop duplicate entries
        # Note that drop_duplicates doesn't like the JSON columns, so de-duplicate on other fields
        rules.drop_duplicates(
            inplace=True,
            subset=[
                "DRUG",
                "GENE",
                "MUTATION",
                "POSITION",
                "MUTATION_AFFECTS",
                "MUTATION_TYPE",
                "MINOR",
            ],
        )

    # create a list of the genes mentioned in the catalogue
    genes = list(rules.GENE.unique())

    # create a dictionary where the genes are keys and the entries tell which drugs
    #   are affected
    gene_lookup = {}
    for gene_name in genes:
        df = rules.loc[rules.GENE == gene_name]
        gene_lookup[gene_name] = sorted(list(df.DRUG.unique()))

    # create a dictionary where the drugs are keys and the entries tell which genes
    #   are affected
    drug_lookup = {}
    for drug_name in drugs:
        df = rules.loc[rules.DRUG == drug_name]
        drug_lookup[drug_name] = list(df.GENE.unique())

    return rules, genes, drug_lookup, gene_lookup

process_indel_variants(mutation_affects, predictions, rules, rules_mutation_type_vector, rules_position_vector, indel_length, indel_type, indel_bases, position, minor)

Apply the cascading rules for INDELs in GARC

Parameters:

Name Type Description Default
mutation_affects str

Which area this mutation affects. i.e PROM/CDS

required
predictions {int

str}): Dictionary mapping priority -> prediction. This is updated for implict return

required
rules DataFrame

Rules DataFrame

required
rules_mutation_type_vector Series

Series for the rules which refer to this mutation type

required
rules_position_vector Series

Series for the rules which refer to this position

required
indel_length int

Length of the indel

required
indel_type str

Type of the indel. i.e ins/del/fs/indel

required
indel_bases str

Bases inserted/deleted (if applicable)

required
position int

Position of the indel

required
minor float

Float for supporting evidence of minor populations (or None if not a minor population)

required
Source code in piezo/grammar_GARC1.py
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
def process_indel_variants(
    mutation_affects: str | None,
    predictions: Dict[int, Tuple[str, Dict]],
    rules: pandas.DataFrame,
    rules_mutation_type_vector: pandas.Series,
    rules_position_vector: pandas.Series,
    indel_length: float | None,
    indel_type: str | None,
    indel_bases: str | None,
    position: int | None,
    minor: float | None,
) -> None:
    """Apply the cascading rules for INDELs in GARC

    Args:
        mutation_affects (str): Which area this mutation affects. i.e PROM/CDS
        predictions ({int: str}): Dictionary mapping priority -> prediction. This is
            updated for implict return
        rules (pandas.DataFrame): Rules DataFrame
        rules_mutation_type_vector (pandas.Series): Series for the rules which refer
            to this mutation type
        rules_position_vector (pandas.Series): Series for the rules which refer to
            this position
        indel_length (int): Length of the indel
        indel_type (str): Type of the indel. i.e ins/del/fs/indel
        indel_bases (str): Bases inserted/deleted (if applicable)
        position (int): Position of the indel
        minor (float): Float for supporting evidence of minor populations (or None if
            not a minor population)
    """
    if mutation_affects == "GENE" and indel_length is not None:
        # Large deletions are priority 1
        large_del(
            predictions, rules.loc[rules_mutation_type_vector], indel_length, minor
        )

    # PRIORITY 1: any insertion or deletion in the CDS or PROM (e.g. rpoB@*_indel
    #   or rpoB@-*_indel)
    if mutation_affects == "CDS":
        row = rules.loc[
            rules_mutation_type_vector
            & ((rules.MUTATION == "*_indel") | (rules.MUTATION == "*_mixed"))
        ]
    else:
        row = rules.loc[
            rules_mutation_type_vector
            & ((rules.MUTATION == "-*_indel") | (rules.MUTATION == "-*mixed"))
        ]
    # any insertion or deletion in the CDS or PROM
    row_prediction(row, predictions, 1, minor)

    # PRIORITY 2: rpoB@*_ins, rpoB@*_del any insertion (or deletion) in the CDS or PROM
    if indel_type is not None:
        if mutation_affects == "CDS":
            row = rules.loc[
                rules_mutation_type_vector & (rules.MUTATION.isin(["*_" + indel_type]))
            ]
        else:
            row = rules.loc[
                rules_mutation_type_vector & (rules.MUTATION.isin(["-*_" + indel_type]))
            ]
        # any insertion (or deletion) in the CDS or PROM
        row_prediction(row, predictions, 2, minor)

    # PRIORITY 3: any insertion of a specified length (or deletion) in the CDS or PROM
    #   (e.g. rpoB@*_ins_2, rpoB@*_del_3, rpoB@-*_ins_1, rpoB@-*_del_200)
    if indel_length is not None and indel_type is not None and indel_type != "indel":
        if mutation_affects == "CDS":
            row = rules.loc[
                rules_mutation_type_vector
                & (rules.MUTATION.isin(["*_" + indel_type + "_" + str(indel_length)]))
            ]
        else:
            row = rules.loc[
                rules_mutation_type_vector
                & (rules.MUTATION.isin(["-*_" + indel_type + "_" + str(indel_length)]))
            ]
        # any insertion of a specified length (or deletion) in the CDS or PROM
        row_prediction(row, predictions, 3, minor)

    # PRIORITY=4: any frameshifting insertion or deletion in the CDS (e.g. rpoB@*_fs)
    if indel_length is not None and (indel_length % 3) != 0:
        row = rules.loc[rules_mutation_type_vector & (rules.MUTATION == "*_fs")]
        # any frameshifting insertion or deletion in the CDS
        row_prediction(row, predictions, 4, minor)

    # PRIORITY=5: any indel at a specific position in the CDS or PROM
    #   (e.g. rpoB@1300_indel or rpoB@-15_indel)
    row = rules.loc[
        rules_mutation_type_vector
        & rules_position_vector
        & (rules.MUTATION.str.contains("indel"))
    ]
    # any indel at a specific position in the CDS or PROM (e.g. rpoB@1300_indel
    #   or rpoB@-15_indel)
    row_prediction(row, predictions, 5, minor)

    # PRIORITY=6: an insertion (or deletion) at a specific position in the CDS or PROM
    #   (e.g. rpoB@1300_ins or rpoB@-15_del)
    if indel_type != "indel" and indel_type is not None:
        row = rules.loc[
            rules_mutation_type_vector
            & rules_position_vector
            & (rules.MUTATION.str.contains(indel_type))
        ]
        # any insertion (or deletion) at a specific position in the CDS or PROM
        #   (e.g. rpoB@1300_ins or rpoB@-15_del)
        row_prediction(row, predictions, 6, minor)

    # PRIORITY=7: an insertion (or deletion) of a specified length at a specific
    #   position in the CDS or PROM (e.g. rpoB@1300_ins_2 or rpoB@-15_del_200)
    if indel_type != "indel" and indel_type is not None and indel_length is not None:
        row = rules.loc[
            rules_mutation_type_vector
            & rules_position_vector
            & (
                rules.MUTATION
                == str(position) + "_" + indel_type + "_" + str(indel_length)
            )
        ]
        # an insertion (or deletion) of a specified length at a specific position in
        #   the CDS or PROM (e.g. rpoB@1300_ins_2 or rpoB@-15_del_200)
        row_prediction(row, predictions, 7, minor)

    # PRIORITY=8: a frameshifting mutation at a specific position in the CDS
    #   (e.g. rpoB@100_fs)
    if indel_length is not None and (indel_length % 3) != 0 and position is not None:
        row = rules.loc[
            rules_mutation_type_vector
            & rules_position_vector
            & (rules.MUTATION == str(position) + "_fs")
        ]
        # a frameshifting mutation at a specific position in the CDS (e.g. rpoB@100_fs)
        row_prediction(row, predictions, 8, minor)

    # PRIORITY=9: an insertion of a specified series of nucleotides at a position in
    #   the CDS or PROM (e.g. rpoB@1300_ins_ca)
    if indel_type == "ins" and indel_length is not None and indel_bases is not None:
        row = rules.loc[
            rules_mutation_type_vector
            & rules_position_vector
            & (rules.MUTATION == str(position) + "_ins_" + indel_bases)
        ]
        # an insertion of a specified series of nucleotides at a position in the CDS or
        #   PROM (e.g. rpoB@1300_ins_ca)
        row_prediction(row, predictions, 9, minor)

process_snp_variants(mutation_affects, predictions, before, after, mutation, rules, rules_mutation_type_vector, rules_position_vector, minor)

Apply cascading rules for SNPs according to GARC

Parameters:

Name Type Description Default
mutation_affects str

Where this mutation affects. i.e PROM/CDS

required
predictions {int

str}): Predictions dictionary mapping priority -> prediction. This is updated for implict return

required
before str

Reference base/AA

required
after str

Alt base/AA

required
mutation str

Mutation in GARC

required
rules DataFrame

Rules DataFrame

required
rules_mutation_type_vector Series

Series relating to rules for this type of mutation

required
rules_position_vector Series

Series relating to rules for this position

required
minor float | None

Float for supporting evidence of minor populations (or None if not a minor population).

required
Source code in piezo/grammar_GARC1.py
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
def process_snp_variants(
    mutation_affects: str | None,
    predictions: Dict[int, Tuple[str, Dict]],
    before: str | None,
    after: str | None,
    mutation: str,
    rules: pandas.DataFrame,
    rules_mutation_type_vector: pandas.Series,
    rules_position_vector: pandas.Series,
    minor: float | None,
) -> None:
    """Apply cascading rules for SNPs according to GARC

    Args:
        mutation_affects (str): Where this mutation affects. i.e PROM/CDS
        predictions ({int: str}): Predictions dictionary mapping priority -> prediction.
            This is updated for implict return
        before (str): Reference base/AA
        after (str): Alt base/AA
        mutation (str): Mutation in GARC
        rules (pandas.DataFrame): Rules DataFrame
        rules_mutation_type_vector (pandas.Series): Series relating to rules for this
            type of mutation
        rules_position_vector (pandas.Series): Series relating to rules for this
            position
        minor (float | None): Float for supporting evidence of minor populations
            (or None if not a minor population).
    """

    if before == after:
        # PRIORITY=0: no change, i.e. wildtype specified

        # PRIORITY=1: synonymous mutation at any position in the CDS (e.g. rpoB_*=)
        row = rules.loc[rules_mutation_type_vector & (rules.MUTATION == "*=")]
        # syn SNP at any position in the CDS
        row_prediction(row, predictions, 1, minor)

    elif before != after:
        # PRIORITY=2: nonsynoymous mutation at any position in the CDS or PROM
        #   (e.g. rpoB_*? or rpoB_-*?)
        if mutation_affects == "CDS":
            row = rules.loc[rules_mutation_type_vector & (rules.MUTATION == "*?")]
            # nonsyn SNP at any position in the CDS
            row_prediction(row, predictions, 2, minor)
        else:
            row = rules.loc[rules_mutation_type_vector & (rules.MUTATION == "-*?")]
            # nonsyn SNP at any position in the PROM
            row_prediction(row, predictions, 2, minor)
        # PRIORITY=3: het mutation at any position in the CDS or PROM (e.g. rpoB@*Z
        #   or rpoB@-*z)
        if mutation[-1] in ["Z", "z", "O", "o", "X", "x"]:
            if mutation_affects == "CDS":
                if mutation[-1] == "Z":
                    row = rules.loc[
                        (rules_mutation_type_vector) & (rules.MUTATION == "*Z")
                    ]
                    # het SNP at any position in the CDS
                    row_prediction(row, predictions, 3, minor)
                elif mutation[-1] == "O":
                    row = rules.loc[
                        (rules_mutation_type_vector) & (rules.MUTATION == "*O")
                    ]
                    # filter fail at any position in the CDS
                    row_prediction(row, predictions, 3, minor)
                elif mutation[-1] == "X":
                    row = rules.loc[
                        (rules_mutation_type_vector) & (rules.MUTATION == "*X")
                    ]
                    # null at any position in the CDS
                    row_prediction(row, predictions, 3, minor)
            else:
                if mutation[-1] == "z":
                    row = rules.loc[
                        rules_mutation_type_vector & (rules.MUTATION == "-*z")
                    ]
                    # het SNP at any position in the PROM
                    row_prediction(row, predictions, 3, minor)
                elif mutation[-1] == "o":
                    row = rules.loc[
                        rules_mutation_type_vector & (rules.MUTATION == "-*o")
                    ]
                    # filter fail at any position in the PROM
                    row_prediction(row, predictions, 3, minor)
                elif mutation[-1] == "x":
                    row = rules.loc[
                        rules_mutation_type_vector & (rules.MUTATION == "-*x")
                    ]
                    # null at any position in the PROM
                    row_prediction(row, predictions, 3, minor)

        # PRIORITY=4: specific mutation at any position in the CDS: only Stop codons
        #   make sense for now (e.g. rpoB@*!)
        if mutation[-1] in ["!"]:
            if mutation_affects == "CDS":
                row = rules.loc[rules_mutation_type_vector & (rules.MUTATION == "*!")]
                # stop codon at any position in the CDS
                row_prediction(row, predictions, 4, minor)

        # PRIORITY=5: no change at specific position
        # PRIORITY=6: synonymous mutations at specific location (usually picked up by
        #   e.g. L202L, rather than L202=)

        # PRIORITY=7: any het mutation at this specific position in the CDS or PROM
        #   (e.g. rpoB@S450Z or rpoB@c-15z)
        if mutation[-1] in ["Z", "z"]:
            row = rules.loc[
                (rules_mutation_type_vector)
                & (rules_position_vector)
                & (rules.MUTATION.str[-1].isin(["Z", "z"]))
            ]
            # het SNP at specified position in the CDS
            row_prediction(row, predictions, 7, minor)

        # PRIORITY=8: any nonsynoymous mutation at this specific position in the CDS or
        #   PROM  (e.g. rpoB@S450? or rpoB@c-15?)
        row = rules.loc[
            rules_mutation_type_vector
            & rules_position_vector
            & (rules.MUTATION.str[-1] == "?")
        ]
        # nonsyn SNP at specified position in the CDS
        row_prediction(row, predictions, 8, minor)

    # PRIORITY=9: an exact match
    row = rules.loc[(rules_mutation_type_vector) & (rules.MUTATION == mutation)]
    # exact SNP match
    row_prediction(row, predictions, 9, minor)

row_prediction(rows, predictions, priority, minor)

Get the predictions from the catalogue for the applicable rows

Parameters:

Name Type Description Default
rows DataFrame

DataFrame of the rows within the catalogue

required
predictions dict

Predictions dict mapping priorities to predictions. This is updated for implict return

required
priority int

Priority of this mutation type

required
minor float | None

Reads/FRS supporting this, or None if not a minor population

required
Source code in piezo/grammar_GARC1.py
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
def row_prediction(
    rows: pandas.DataFrame,
    predictions: Dict[int, Tuple[str, Dict]],
    priority: int,
    minor: float | None,
) -> None:
    """Get the predictions from the catalogue for the applicable rows

    Args:
        rows (pandas.DataFrame): DataFrame of the rows within the catalogue
        predictions (dict): Predictions dict mapping priorities to predictions. This is
            updated for implict return
        priority (int): Priority of this mutation type
        minor (float | None): Reads/FRS supporting this, or None if not a
            minor population
    """
    pred = None
    evidence = dict()
    values = ["R", "U", "F", "S", None]
    for _, row in rows.iterrows():
        if not row.empty:
            assert int(priority) in range(
                1, 11
            ), "priority must be an integer in range 1,2..10"
            if values.index(row["PREDICTION"]) < values.index(pred):
                # This row's prediction is more important than the current, so check
                #   for minors and prioritise
                if minor is None and row["MINOR"] == "":
                    # Neither are minor populations so act normally
                    pred = row["PREDICTION"]
                    evidence = row["EVIDENCE"]

                elif (
                    minor is not None
                    and row["MINOR"] != ""
                    and minor < 1
                    and float(row["MINOR"]) < 1
                ):
                    # We have FRS
                    if minor >= float(row["MINOR"]):
                        # Match
                        pred = row["PREDICTION"]
                        evidence = row["EVIDENCE"]

                elif (
                    minor is not None
                    and row["MINOR"] != ""
                    and minor >= 1
                    and float(row["MINOR"]) >= 1
                ):
                    # We have COV
                    if minor >= float(row["MINOR"]):
                        # Match
                        pred = row["PREDICTION"]
                        evidence = row["EVIDENCE"]
    if pred:
        predictions[int(priority)] = (str(pred), evidence)

sanity_check_snp(before, after)

Sanity check that a given SNP is valid. i.e check that the bases are valid

Parameters:

Name Type Description Default
before str

Reference bases/AA

required
after str

Alt bases/AA

required

Raises:

Type Description
AssertationError

Raised in cases in which the SNP is invalid.

Source code in piezo/grammar_GARC1.py
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
def sanity_check_snp(before: str | None, after: str | None) -> None:
    """Sanity check that a given SNP is valid. i.e check that the bases are valid

    Args:
        before (str): Reference bases/AA
        after (str): Alt bases/AA

    Raises:
        AssertationError: Raised in cases in which the SNP is invalid.
    """

    assert after in [
        "a",
        "c",
        "t",
        "g",
        "x",
        "z",
        "o",
        "?",
        "!",
        "A",
        "C",
        "D",
        "E",
        "F",
        "G",
        "H",
        "I",
        "K",
        "L",
        "M",
        "N",
        "O",
        "P",
        "Q",
        "R",
        "S",
        "T",
        "V",
        "W",
        "X",
        "Y",
        "Z",
    ], (
        str(after) + " is not a recognised amino acid or base!"
    )
    assert before in [
        "a",
        "c",
        "t",
        "g",
        "?",
        "!",
        "A",
        "C",
        "D",
        "E",
        "F",
        "G",
        "H",
        "I",
        "K",
        "L",
        "M",
        "N",
        "P",
        "Q",
        "R",
        "S",
        "T",
        "V",
        "W",
        "Y",
    ], (
        str(before) + " is not a recognised amino acid or base!"
    )

    if before.islower():
        assert after.islower(), "nucleotides must be lowercase!"
        assert (
            before != after
        ), "makes no sense for the nucleotide to be the same in a mutation!"
    elif before.isupper():
        assert after.isupper() or after == "!", "amino acids must be UPPERCASE!"

split_mutation(row)

Take a row of the catalogue and get info about the mutation from it. This includes the type, ref, alt, position and minor populations

Parameters:

Name Type Description Default
row Series

Catalogue row

required

Raises:

Type Description
ValueError

Raised when mutations are malformed

Returns:

Type Description
Series

pandas.Series: Series about the mutation. In order: gene, mutation, position, type of position (i.e PROM/CDS), mutation type (i.e INDEL/SNP/MULTI), support for minor populations

Source code in piezo/grammar_GARC1.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
def split_mutation(row: pandas.Series) -> pandas.Series:
    """Take a row of the catalogue and get info about the mutation from it.
    This includes the type, ref, alt, position and minor populations

    Args:
        row (pandas.Series): Catalogue row

    Raises:
        ValueError: Raised when mutations are malformed

    Returns:
        pandas.Series: Series about the mutation. In order: gene, mutation, position,
            type of position (i.e PROM/CDS), mutation type (i.e INDEL/SNP/MULTI),
            support for minor populations
    """
    # Strip any whitespace as this can cause issues
    row["MUTATION"] = row["MUTATION"].strip()
    if "&" in row["MUTATION"]:
        # Multi-mutation so don't try to split it out in the same way
        gene = "MULTI"

        if row["MUTATION"][0] == "^":
            mutation_type = "EPISTASIS"
            row["MUTATION"] = row["MUTATION"][1::]
        else:
            mutation_type = "MULTI"

        # Ensure mutations are in a reproducable order
        mutation = "&".join(sorted(list(row["MUTATION"].split("&"))))

        position = None
        mutation_affects = None

        # Check for minor populations (and remove, we'll check these separately)
        minors = ",".join(
            [mut.split(":")[-1] if ":" in mut else "" for mut in mutation.split("&")]
        )

        mutation = "&".join([mut.split(":")[0] for mut in mutation.split("&")])

        # Check for validity
        validate_multi(mutation)

        return pandas.Series(
            [gene, mutation, position, mutation_affects, mutation_type, minors]
        )

    # split the supplied mutation on _ which separates the components
    components = row["MUTATION"].split("@")

    # the gene name is always the first component
    gene = components[0]

    # ..and the remainder is the mutation
    mutation = row["MUTATION"].split(gene + "@")[1]

    # Check for minor populations (and remove minors, we'll check for those separately)
    if ":" in mutation:
        minor = mutation.split(":")[-1]
        mutation = mutation.split(":")[0]
    else:
        minor = ""

    cols = mutation.split("_")
    valid = True

    if len(cols) == 1:
        mutation_type = "SNP"
        mutation_affects = "CDS"
        try:
            position = cols[0][1:-1]
            if int(position) < 0:
                mutation_affects = "PROM"
            elif int(position) == 0:
                valid = False
        except ValueError:
            position = cols[0][:-1]
            if position[0] == "-":
                mutation_affects = "PROM"

    elif len(cols) in [2, 3]:
        mutation_type = "INDEL"
        percentage = False
        if len(cols) == 2:
            # Checking for percentage deletions (of form "del_<percent>")
            try:
                float(cols[1])
                percentage = True
            except ValueError:
                percentage = False
            if percentage:
                position = None
        if percentage:
            mutation_affects = "GENE"
        else:
            mutation_affects = "CDS"
            position = cols[0]
            try:
                if int(position) < 0:
                    mutation_affects = "PROM"
                elif int(position) == 0:
                    valid = False
            except ValueError:
                try:
                    if position[0] == "-":
                        mutation_affects = "PROM"
                except IndexError:
                    valid = False

    else:
        raise ValueError("Badly formed mutation: " + row["MUTATION"])

    if not valid:
        # Valid mutation not found, so complain
        raise ValueError("Badly formed mutation: " + row["MUTATION"])

    return pandas.Series(
        [gene, mutation, position, mutation_affects, mutation_type, minor]
    )

validate_multi(mutation)

Validate that we have a valid multi-mutation.

This checks that if a multi contains specific mutations, they should not be covered by general mutations which this multi includes.

Parameters:

Name Type Description Default
mutation str

Multi-mutation

required
Source code in piezo/grammar_GARC1.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def validate_multi(mutation: str) -> None:
    """Validate that we have a valid multi-mutation.

    This checks that if a multi contains specific mutations, they should not be covered
    by general mutations which this multi includes.

    Args:
        mutation (str): Multi-mutation
    """
    # If we have a specific rule, combined with a general rule which covers it,
    #   that should be reduced, so it's invalid.
    # If we were to allow defaults which cover specifics, the multi matching would break
    #
    # Easiest way to do this is to take general rules and construct a catalogue
    #   from them, then, pass in all specific rules, and if we get a match on any of
    #   them, they are covered by a general rule
    rule_mutations = mutation.split("&")
    dummy_cat = {
        "EVIDENCE": [
            ujson.dumps({"Rule hit": idx}) for (idx, _) in enumerate(rule_mutations)
        ],
        "MUTATION": rule_mutations,
        "DRUG": ["na" for _ in rule_mutations],
        "PREDICTION": ["R" for _ in rule_mutations],
    }
    r = pandas.DataFrame(dummy_cat)
    r[
        ["GENE", "MUTATION", "POSITION", "MUTATION_AFFECTS", "MUTATION_TYPE", "MINOR"]
    ] = r.apply(split_mutation, axis=1)

    generals = r[(r["POSITION"] == "*") | (r["MUTATION"].str.contains(r"\?"))]
    specifics = r[(r["POSITION"] != "*") & (~r["MUTATION"].str.contains(r"\?"))]
    # As this is a named **tuple**, it is immutable so create a new one
    cat = Catalogue(
        "NC_000962.3",
        "generic",
        "1.0",
        "GARC1",
        "R",  # Obviously not R but RFUS are needed here
        ["na"],
        generals["GENE"].tolist(),
        {"na": generals["GENE"].tolist()},
        {gene: ["na"] for gene in generals["GENE"].tolist()},
        len(generals),
        generals,
    )
    for _, row in specifics.iterrows():
        try:
            pred = predict_GARC1(cat, "@".join(row[["GENE", "MUTATION"]]))
            if pred != "S":
                raise ValueError(
                    "Badly formed mutation: "
                    + mutation
                    + " contains generic rules which cover specific rules!"
                )
        except ValueError as e:
            if "Badly formed mutation: " in str(e):
                # If we're catching the exception we just threw, re-throw it
                raise e
            continue