|
| 1 | +#! /usr/bin/env python |
| 2 | + |
| 3 | +import sys |
| 4 | +from collections import Counter |
| 5 | + |
| 6 | +import yaml |
| 7 | +import csv |
| 8 | +from deepdiff import DeepDiff |
| 9 | + |
| 10 | +object_map = { '1 / study': 'MiAIRR_Study', |
| 11 | + '1 / subject': 'MiAIRR_Subject', |
| 12 | + '1 / diag. & intervent.': 'MiAIRR_Diagnosis', |
| 13 | + '2 / sample': 'MiAIRR_Sample', |
| 14 | + '3 / process (cell)': 'MiAIRR_CellProcessing', |
| 15 | + '3 / process (nucl. acid)': 'MiAIRR_NucleicAcidProcessing', |
| 16 | + '5 / process (comput.)': 'MiAIRR_SoftwareProcessing', |
| 17 | + '6 / data (proc. seq.)': 'MiAIRR_Rearrangement' } |
| 18 | + |
| 19 | +with open('AIRR_Minimal_Standard_Data_Elements.tsv', 'r') as ip: |
| 20 | + dictReader = csv.DictReader(ip, dialect='excel-tab') |
| 21 | + miairr_elements = [line for line in dictReader] |
| 22 | + |
| 23 | +with open('AIRR_Minimal_Standard_Data_Elements.tsv', 'r') as ip: |
| 24 | + # header line present |
| 25 | + assert next(ip).split()[0] == 'MiAIRR' |
| 26 | + |
| 27 | + table = [line.split('\t')[6].strip() for line in ip] |
| 28 | + # handle the exceptional 4 / data line |
| 29 | + assert table.count('') == 1 |
| 30 | + _ = table.pop(table.index('')) |
| 31 | + |
| 32 | +with open('specs/definitions.yaml', 'r') as ip: |
| 33 | + definitions = yaml.load(ip) |
| 34 | + properties = [property |
| 35 | + for obj in definitions.values() |
| 36 | + for property in obj['properties'] |
| 37 | + if obj.get('discriminator') == 'MiAIRR'] |
| 38 | + |
| 39 | +failed = False |
| 40 | + |
| 41 | +# check for uniqueness of fields in AIRR_Minimal_Standard_Data_Elements.tsv |
| 42 | +if len(table) != len(set(table)): |
| 43 | + print('Duplicate entries found in AIRR_Minimal_Standard_Data_Elements.tsv', file=sys.stderr) |
| 44 | + for k, v in Counter(table).items(): |
| 45 | + if v > 1: |
| 46 | + print(f'{k:30} found {v} times in tsv when it should be unique\n', file=sys.stderr) |
| 47 | + failed = True |
| 48 | + |
| 49 | +# check for differences in fields between specs/definitions.yaml and |
| 50 | +# AIRR_Minimal_Standard_Data_Elements.tsv |
| 51 | +for key in object_map.keys(): |
| 52 | + elements = [element['AIRR Formats WG field name'] for element in miairr_elements |
| 53 | + if element['MiAIRR data set / subset'] == key] |
| 54 | + definition = definitions.get(object_map[key]) |
| 55 | + if not definition: |
| 56 | + print(f'{object_map[key]} not found in definitions.yaml.\n', file=sys.stderr) |
| 57 | + failed = True |
| 58 | + continue |
| 59 | + |
| 60 | + properties = [property for property in definition['properties']] |
| 61 | + if set(elements) != set(properties): |
| 62 | + print(f'{object_map[key]} does not match TSV', file=sys.stderr) |
| 63 | + for field in set(properties) - set(elements): |
| 64 | + print(f'{field:30} is found in yaml but not tsv for {object_map[key]}', file=sys.stderr) |
| 65 | + for field in set(elements) - set(properties): |
| 66 | + print(f'{field:30} is found in tsv but not yaml for {object_map[key]}', file=sys.stderr) |
| 67 | + failed = True |
| 68 | + |
| 69 | +# check that MiAIRR object definitions contained |
| 70 | +# within AIRR definition |
| 71 | +for definition in definitions.keys(): |
| 72 | + if definitions[definition].get('discriminator') == 'MiAIRR': |
| 73 | + name = definition.split('_')[1] |
| 74 | + if not definitions.get(name): |
| 75 | + print(f'{name} corresponding to {definition} not found in definitions.yaml', file=sys.stderr) |
| 76 | + failed = True |
| 77 | + continue |
| 78 | + |
| 79 | + for prop in definitions[definition]['properties']: |
| 80 | + if not definitions[name]['properties'].get(prop): |
| 81 | + print(f'{prop} in {definition} object is not in {name} object.', file=sys.stderr) |
| 82 | + failed = True |
| 83 | + continue |
| 84 | + ddiff = DeepDiff(definitions[definition]['properties'][prop], definitions[name]['properties'][prop], ignore_order=True) |
| 85 | + if ddiff: |
| 86 | + print(f'{prop} in {definition} object is not the same object in {name}.', file=sys.stderr) |
| 87 | + print(ddiff, file=sys.stderr) |
| 88 | + failed = True |
| 89 | + |
| 90 | +# check consistency with NCBI XML definitions, per @BusseChristian's pseudocode |
| 91 | +# in https://github.com/airr-community/airr-standards/issues/20 |
| 92 | +import pandas as pd |
| 93 | + |
| 94 | +miairr_table = pd.read_csv('AIRR_Minimal_Standard_Data_Elements.tsv', sep='\t', header=0, index_col=None) |
| 95 | +miairr_biosample_rows = miairr_table.iloc[:, 0].isin(["1 / subject", "1 / diag. & intervent.", "2 / sample", "3 / process (cell)"]) |
| 96 | +miairr_identifiers = set(miairr_table[miairr_biosample_rows].iloc[:, 6]) |
| 97 | +miairr_identifiers.add('study_id') # manually add |
| 98 | +miairr_mapping = {} |
| 99 | +with open('NCBI_implementation/mapping_MiAIRR_BioSample.tsv', 'r') as ip: |
| 100 | + dictReader = csv.DictReader(ip, dialect='excel-tab') |
| 101 | + for line in dictReader: |
| 102 | + miairr_mapping[line['AIRR Formats WG field name']] = line['NCBI BioSample attribute'] |
| 103 | +mapped_identifiers = set([miairr_mapping.get(name, name) for name in miairr_identifiers]) |
| 104 | + |
| 105 | +ncbi_biosample = pd.read_excel('NCBI_implementation/templates_XLS/AIRR_BioSample_v1.0.xls', skiprows=13) |
| 106 | +ncbi_identifiers = set([x.lstrip('*') for x in ncbi_biosample.columns]) |
| 107 | + |
| 108 | +if mapped_identifiers != ncbi_identifiers: |
| 109 | + print('AIRR_Minimal_Standard_Data_Elements.tsv does not match AIRR_BioSample_v1.0.xls', file=sys.stderr) |
| 110 | + for field in set(mapped_identifiers) - set(ncbi_identifiers): |
| 111 | + print(f'{field:30} is found in MiAIRR table tsv but not in NCBI Biosample template xls', file=sys.stderr) |
| 112 | + for field in set(ncbi_identifiers) - set(mapped_identifiers): |
| 113 | + print(f'{field:30} is found in NCBI Biosample template xls but not in MiAIRR table tsv', file=sys.stderr) |
| 114 | + failed = True |
| 115 | + |
| 116 | +if failed: |
| 117 | + print('consistency checks failed', file=sys.stderr) |
| 118 | + sys.exit(1) |
0 commit comments