|
| 1 | +# |
| 2 | +# |
| 3 | +# Quick script to reduce alleles from a CSV file |
| 4 | +# |
| 5 | +# Use `conf.py` to setup configurations that's used here |
| 6 | +# For Excel output, openpyxl library needs to be installed. |
| 7 | +# pip install openpyxl |
| 8 | +# |
| 9 | + |
| 10 | +import pandas as pd |
| 11 | +import pyard |
| 12 | +import re |
| 13 | + |
| 14 | +from conf import ard_config |
| 15 | + |
| 16 | +verbose = ard_config["verbose_log"] |
| 17 | +white_space_regex = re.compile(r"\s+") |
| 18 | + |
| 19 | + |
| 20 | +def is_serology(allele: str) -> bool: |
| 21 | + if len(allele.split(':')) == 1: |
| 22 | + return True |
| 23 | + |
| 24 | + |
| 25 | +def is_3field(allele: str) -> bool: |
| 26 | + return len(allele.split(':')) > 2 |
| 27 | + |
| 28 | + |
| 29 | +def is_P(allele: str) -> bool: |
| 30 | + if allele.endswith('P'): |
| 31 | + fields = allele.split(':') |
| 32 | + if len(fields) == 2: # Ps are 2 fields |
| 33 | + return fields[0].isdigit() and fields[0].isdigit() |
| 34 | + return False |
| 35 | + |
| 36 | + |
| 37 | +def clean_locus(allele: str, column_name: str = 'Unknown') -> str: |
| 38 | + if allele: |
| 39 | + # Remove all white spaces |
| 40 | + allele = white_space_regex.sub('', allele) |
| 41 | + locus = column_name.split('_')[1].upper() |
| 42 | + # If the allele comes in as an allele list, apply reduce to all alleles |
| 43 | + if '/' in allele: |
| 44 | + return "/".join(map(reduce, allele.split('/'), locus)) |
| 45 | + else: |
| 46 | + return reduce(allele, locus) |
| 47 | + return allele |
| 48 | + |
| 49 | + |
| 50 | +def should_be_reduced(allele, locus_allele): |
| 51 | + if is_serology(allele): |
| 52 | + return ard_config["reduce_serology"] |
| 53 | + |
| 54 | + if ard_config["reduce_v2"]: |
| 55 | + if ard.is_v2(locus_allele): |
| 56 | + return True |
| 57 | + |
| 58 | + if ard_config["reduce_3field"]: |
| 59 | + if is_3field(locus_allele): |
| 60 | + return True |
| 61 | + |
| 62 | + if ard_config["reduce_P"]: |
| 63 | + if is_P(allele): |
| 64 | + return True |
| 65 | + |
| 66 | + if ard_config["reduce_XX"]: |
| 67 | + if ard.is_XX(locus_allele): |
| 68 | + return True |
| 69 | + |
| 70 | + if ard_config["reduce_MAC"]: |
| 71 | + if ard.is_mac(locus_allele) and not ard.is_XX(locus_allele): |
| 72 | + return True |
| 73 | + |
| 74 | + return False |
| 75 | + |
| 76 | + |
| 77 | +def reduce(allele, locus): |
| 78 | + # Does the allele name have the locus in it ? |
| 79 | + if '*' in allele: |
| 80 | + locus_allele = allele |
| 81 | + elif ard_config["locus_in_allele_name"]: |
| 82 | + locus_allele = allele |
| 83 | + else: |
| 84 | + locus_allele = f"{locus}*{allele}" |
| 85 | + |
| 86 | + # Check the config if this allele should be reduced |
| 87 | + if should_be_reduced(allele, locus_allele): |
| 88 | + # print(f"reducing '{locus_allele}'") |
| 89 | + reduced_allele = ard.redux_gl(locus_allele, ard_config["redux_type"]) |
| 90 | + # print(f"reduced to '{reduced_allele}'") |
| 91 | + if reduced_allele: |
| 92 | + allele = "/".join(map(lambda a: a.split('*')[1], |
| 93 | + reduced_allele.split('/'))) |
| 94 | + else: |
| 95 | + if verbose: |
| 96 | + print(f"Failed to reduce {locus_allele}") |
| 97 | + |
| 98 | + if verbose: |
| 99 | + print(f"\t{locus_allele} => {allele}") |
| 100 | + return allele |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == '__main__': |
| 104 | + ard = pyard.ARD(remove_invalid=False) |
| 105 | + |
| 106 | + df = pd.read_csv(ard_config["in_csv_filename"], names=ard_config["csv_in_column_names"], header=0, dtype=str) |
| 107 | + df.fillna('', inplace=True) |
| 108 | + |
| 109 | + for column in ard_config["columns_to_check"]: |
| 110 | + if verbose: |
| 111 | + print(f"Column:{column} =>") |
| 112 | + if ard_config["new_column_for_redux"]: |
| 113 | + # insert a new column |
| 114 | + new_column_name = f"reduced_{column}" |
| 115 | + new_column_index = df.columns.get_loc(column) + 1 |
| 116 | + df.insert(new_column_index, new_column_name, df[column].apply(clean_locus, column_name=column)) |
| 117 | + else: |
| 118 | + df[column] = df[column].apply(clean_locus, column_name=column) |
| 119 | + |
| 120 | + if ard_config["output_file_format"] == 'xlsx': |
| 121 | + out_file_name = f"{ard_config['out_csv_filename']}.xlsx" |
| 122 | + df.to_excel(out_file_name, index=False) |
| 123 | + else: |
| 124 | + out_file_name = f"{ard_config['out_csv_filename'] + '.gz' if ard_config['apply_compression'] else ''}" |
| 125 | + df.to_csv(out_file_name, index=False, compression=ard_config["apply_compression"]) |
| 126 | + if verbose: |
| 127 | + print(f"Saved result to file:{out_file_name}") |
0 commit comments