|
24 | 24 | import os |
25 | 25 | import sys |
26 | 26 | import argparse |
27 | | -import subprocess |
| 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 |
28 | 81 |
|
29 | 82 |
|
30 | 83 | def main(): |
31 | 84 | """ |
32 | | - Python implementation of tool: payload-add-uniform-ids |
33 | | -
|
34 | | - This is auto-generated Python code, please update as needed! |
| 85 | + Add uniform IDs for donor/specimen/sample to the original input payload JSON |
35 | 86 | """ |
36 | 87 |
|
37 | 88 | parser = argparse.ArgumentParser(description='Tool: payload-add-uniform-ids') |
38 | | - parser.add_argument('-i', '--input-file', dest='input_file', type=str, |
39 | | - help='Input file', required=True) |
40 | | - parser.add_argument('-o', '--output-dir', dest='output_dir', type=str, |
| 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, |
41 | 94 | help='Output directory', required=True) |
42 | 95 | args = parser.parse_args() |
43 | 96 |
|
44 | | - if not os.path.isfile(args.input_file): |
45 | | - sys.exit('Error: specified input file %s does not exist or is not accessible!' % args.input_file) |
| 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) |
46 | 102 |
|
47 | 103 | if not os.path.isdir(args.output_dir): |
48 | 104 | sys.exit('Error: specified output dir %s does not exist or is not accessible!' % args.output_dir) |
49 | 105 |
|
50 | | - subprocess.run(f"cp {args.input_file} {args.output_dir}/", shell=True, check=True) |
| 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)) |
51 | 117 |
|
52 | 118 |
|
53 | 119 | if __name__ == "__main__": |
54 | 120 | main() |
55 | | - |
|
0 commit comments