|
| 1 | +#! /usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import csv |
| 5 | +import logging |
| 6 | +from collections import Counter |
| 7 | + |
| 8 | +from .update_frequency_file_lib import ( |
| 9 | + FREQUENCY_FIELDS, |
| 10 | + FrequencyRowDict, |
| 11 | + NewName, |
| 12 | + OldName, |
| 13 | + parse_nomenclature, |
| 14 | + update_old_frequencies, |
| 15 | +) |
| 16 | +from .utils import HLA_LOCUS |
| 17 | + |
| 18 | +logging.basicConfig() |
| 19 | +logger: logging.Logger = logging.getLogger(__name__) |
| 20 | +logger.setLevel(logging.INFO) |
| 21 | + |
| 22 | + |
| 23 | +def main(): |
| 24 | + parser: argparse.ArgumentParser = argparse.ArgumentParser( |
| 25 | + "Update the format of the old HLA frequencies file" |
| 26 | + ) |
| 27 | + parser.add_argument( |
| 28 | + "old_frequency_file", |
| 29 | + help="Old frequency file path (6-column CSV without header)", |
| 30 | + type=argparse.FileType("r"), |
| 31 | + ) |
| 32 | + parser.add_argument( |
| 33 | + "name_mapping", |
| 34 | + help=( |
| 35 | + "File mapping old allele names to new allele names in the format " |
| 36 | + "kept by IMGT/HLA" |
| 37 | + ), |
| 38 | + type=argparse.FileType("r"), |
| 39 | + ) |
| 40 | + parser.add_argument( |
| 41 | + "--output", |
| 42 | + help="Output CSV file", |
| 43 | + type=argparse.FileType("w"), |
| 44 | + default="hla_frequencies.csv", |
| 45 | + ) |
| 46 | + args: argparse.Namespace = parser.parse_args() |
| 47 | + |
| 48 | + logger.info("Reading in the update mapping....") |
| 49 | + with args.name_mapping: |
| 50 | + old_to_new: dict[OldName, NewName] |
| 51 | + deprecated: list[str] |
| 52 | + deprecated_maps_to_other: list[tuple[str, str]] |
| 53 | + mapping_overrides_deprecated: list[tuple[str, str]] |
| 54 | + ( |
| 55 | + old_to_new, |
| 56 | + deprecated, |
| 57 | + deprecated_maps_to_other, |
| 58 | + mapping_overrides_deprecated, |
| 59 | + ) = parse_nomenclature(args.name_mapping.read()) |
| 60 | + |
| 61 | + for old_name_str in deprecated: |
| 62 | + logger.info(f"Allele {old_name_str} is deprecated.") |
| 63 | + |
| 64 | + for old_name_str, existing_mapping in deprecated_maps_to_other: |
| 65 | + logger.info( |
| 66 | + f"Allele {old_name_str} is deprecated but another allele " |
| 67 | + "with the same first two coordinates maps to " |
| 68 | + f"{existing_mapping}; retaining that mapping." |
| 69 | + ) |
| 70 | + |
| 71 | + for old_name_str, new_name_str in mapping_overrides_deprecated: |
| 72 | + logger.info( |
| 73 | + f"Allele {old_name_str} maps to {new_name_str} but another " |
| 74 | + f"allele with the same first two coordinates was " |
| 75 | + f"deprecated; updating the mapping." |
| 76 | + ) |
| 77 | + |
| 78 | + logger.info("Updating old alleles....") |
| 79 | + with args.old_frequency_file: |
| 80 | + updated_frequencies: list[FrequencyRowDict] |
| 81 | + unmapped_alleles: Counter[tuple[HLA_LOCUS, str]] |
| 82 | + deprecated_alleles_seen: Counter[tuple[HLA_LOCUS, str]] |
| 83 | + updated_frequencies, unmapped_alleles, deprecated_alleles_seen = ( |
| 84 | + update_old_frequencies(args.old_frequency_file, old_to_new) |
| 85 | + ) |
| 86 | + |
| 87 | + if len(unmapped_alleles) > 1: |
| 88 | + logger.info( |
| 89 | + "Alleles present in the old frequencies that do not have a mapping " |
| 90 | + "in the new nomenclature, and their numbers of occurrences:" |
| 91 | + ) |
| 92 | + for locus, name in unmapped_alleles: |
| 93 | + logger.info( |
| 94 | + f'HLA-{locus} allele "{name}": {unmapped_alleles[(locus, name)]}' |
| 95 | + ) |
| 96 | + |
| 97 | + if len(deprecated_alleles_seen) > 1: |
| 98 | + logger.info( |
| 99 | + "Alleles present in the old frequencies that are deprecated " |
| 100 | + "in the new nomenclature, and their numbers of occurrences:" |
| 101 | + ) |
| 102 | + for locus, name in deprecated_alleles_seen: |
| 103 | + logger.info( |
| 104 | + f'HLA-{locus} allele "{name}": {deprecated_alleles_seen[(locus, name)]}' |
| 105 | + ) |
| 106 | + |
| 107 | + with args.output: |
| 108 | + frequencies_csv: csv.DictWriter = csv.DictWriter(args.output, FREQUENCY_FIELDS) |
| 109 | + frequencies_csv.writeheader() |
| 110 | + frequencies_csv.writerows(updated_frequencies) |
| 111 | + |
| 112 | + logger.info("... done.") |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + main() |
0 commit comments