|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | + Copyright (C) 2021, Ontario Institute for Cancer Research |
| 6 | +
|
| 7 | + This program is free software: you can redistribute it and/or modify |
| 8 | + it under the terms of the GNU Affero General Public License as published by |
| 9 | + the Free Software Foundation, either version 3 of the License, or |
| 10 | + (at your option) any later version. |
| 11 | +
|
| 12 | + This program is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + GNU Affero General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Affero General Public License |
| 18 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +
|
| 20 | + Authors: |
| 21 | + Junjun Zhang |
| 22 | +""" |
| 23 | + |
| 24 | +import os |
| 25 | +import sys |
| 26 | +import argparse |
| 27 | +import csv |
| 28 | +import json |
| 29 | + |
| 30 | + |
| 31 | +def get_id_mapping(id_mapping_tsv): |
| 32 | + id_mapping = dict() |
| 33 | + with open(id_mapping_tsv, 'r') as f: |
| 34 | + for row in csv.DictReader(f, delimiter='\t'): |
| 35 | + type = row['type'] |
| 36 | + submitter_id = row['submitter_id'] |
| 37 | + uniform_id = row['uniform_id'] |
| 38 | + if type in id_mapping: |
| 39 | + sys.exit(f"Values in 'type' field duplicated. Offending value: {type}, in file: {args.id_mapping_tsv}") |
| 40 | + else: |
| 41 | + id_mapping[type] = dict() |
| 42 | + |
| 43 | + if submitter_id in id_mapping[type]: |
| 44 | + sys.exit(f"Values in 'submitter_id' field duplicated. Offending value: {submitter_id}, for type: {type}, in file: {args.id_mapping_tsv}" ) |
| 45 | + else: |
| 46 | + id_mapping[type][submitter_id] = uniform_id |
| 47 | + |
| 48 | + if 'donor' not in id_mapping or 'specimen' not in id_mapping or 'sample' not in id_mapping: |
| 49 | + sys.exit(f"Provided id_mapping_tsv file '{args.id_mapping_tsv}' is required to have ID mappings for 'donor', 'specimen' and 'sample'") |
| 50 | + |
| 51 | + return id_mapping |
| 52 | + |
| 53 | + |
| 54 | +def add_uniform_ids(payload, id_mapping): |
| 55 | + samples = payload.pop('samples', []) |
| 56 | + if not samples: |
| 57 | + sys.exit("Error: no 'samples' found in the input payload JSON") |
| 58 | + |
| 59 | + updated_samples = [] |
| 60 | + for sample in samples: |
| 61 | + if id_mapping['sample'].get(sample['submitterSampleId']): |
| 62 | + sample['sampleId'] = id_mapping['sample'][sample['submitterSampleId']] |
| 63 | + else: |
| 64 | + sys.exit(f"Provided id_mapping_tsv misses mapping for submitter sample ID: {sample['submitterSampleId']}") |
| 65 | + |
| 66 | + if id_mapping['specimen'].get(sample['specimen']['submitterSpecimenId']): |
| 67 | + sample['specimenId'] = id_mapping['specimen'][sample['specimen']['submitterSpecimenId']] |
| 68 | + sample['specimen']['specimenId'] = sample["specimenId"] |
| 69 | + else: |
| 70 | + sys.exit(f"Provided id_mapping_tsv misses mapping for submitter specimen ID: {sample['specimen']['submitterSpecimenId']}") |
| 71 | + |
| 72 | + if id_mapping['donor'].get(sample['donor']['submitterDonorId']): |
| 73 | + sample['donor']['donorId'] = id_mapping['donor'][sample['donor']['submitterDonorId']] |
| 74 | + sample['specimen']['donorId'] = sample['donor']['donorId'] |
| 75 | + else: |
| 76 | + sys.exit(f"Provided id_mapping_tsv misses mapping for submitter donor ID: {sample['donor']['submitterDonorId']}") |
| 77 | + |
| 78 | + updated_samples.append(sample) |
| 79 | + |
| 80 | + payload['samples'] = updated_samples |
| 81 | + |
| 82 | + |
| 83 | +def main(): |
| 84 | + """ |
| 85 | + Add uniform IDs for donor/specimen/sample to the original input payload JSON |
| 86 | + """ |
| 87 | + |
| 88 | + parser = argparse.ArgumentParser(description='Tool: payload-add-uniform-ids') |
| 89 | + parser.add_argument('-p', '--payload-json', type=str, |
| 90 | + help='Input payload JSON', required=True) |
| 91 | + parser.add_argument('-i', '--id-mapping-tsv', type=str, |
| 92 | + help='TSV file containing mapping between submitter IDs and uniform IDs', required=True) |
| 93 | + parser.add_argument('-o', '--output-dir', type=str, |
| 94 | + help='Output directory', required=True) |
| 95 | + args = parser.parse_args() |
| 96 | + |
| 97 | + if not os.path.isfile(args.payload_json): |
| 98 | + sys.exit('Error: specified input payload JSON %s does not exist or is not accessible!' % args.payload_json) |
| 99 | + |
| 100 | + if not os.path.isfile(args.id_mapping_tsv): |
| 101 | + sys.exit('Error: specified ID mapping TSV %s does not exist or is not accessible!' % args.id_mapping_tsv) |
| 102 | + |
| 103 | + if not os.path.isdir(args.output_dir): |
| 104 | + sys.exit('Error: specified output dir %s does not exist or is not accessible!' % args.output_dir) |
| 105 | + |
| 106 | + with open(args.payload_json, 'r') as p: |
| 107 | + payload = json.loads(p.read()) |
| 108 | + |
| 109 | + id_mapping = get_id_mapping(args.id_mapping_tsv) |
| 110 | + |
| 111 | + add_uniform_ids(payload, id_mapping) |
| 112 | + |
| 113 | + output_payload_file = "%s.uniform_id_added.json" % os.path.splitext(os.path.basename(args.payload_json))[0] |
| 114 | + |
| 115 | + with open(os.path.join(args.output_dir, output_payload_file), 'w') as o: |
| 116 | + o.write(json.dumps(payload, indent=2)) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments