|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import argparse |
| 5 | +import time |
| 6 | +from unipressed.id_mapping.types import From, To |
| 7 | +from unipressed import IdMappingClient |
| 8 | +from typing import get_args |
| 9 | + |
| 10 | + |
| 11 | +def validate_db(db_str, valid_dbs, db_type): |
| 12 | + if not db_str in get_args(valid_dbs): |
| 13 | + print( |
| 14 | + f'Error: The specified {db_type} database is not valid. It must be one of: {get_args(valid_dbs)}') |
| 15 | + exit(1) |
| 16 | + |
| 17 | + |
| 18 | +def map_ids_unipressed(from_db, to_db, ids): |
| 19 | + request = IdMappingClient.submit(source=from_db, dest=to_db, ids=ids) |
| 20 | + |
| 21 | + while request.get_status() != "FINISHED": |
| 22 | + time.sleep(1) |
| 23 | + |
| 24 | + return list(request.each_result()) |
| 25 | + |
| 26 | + |
| 27 | +def load_input(input_file): |
| 28 | + if input_file and os.path.isfile(input_file) and os.access(input_file, os.R_OK): |
| 29 | + with open(input_file, "r") as f: |
| 30 | + return [line.strip() for line in f.readlines()] |
| 31 | + else: |
| 32 | + print("Error: The input file is missing or not readable.") |
| 33 | + exit(1) |
| 34 | + |
| 35 | + |
| 36 | +def load_cache_and_subset_ids(cache_dir, from_db, to_db, source_ids): |
| 37 | + cached_data = {} |
| 38 | + source_ids_not_cached = source_ids |
| 39 | + |
| 40 | + if cache_dir and os.path.isdir(cache_dir) and os.access(cache_dir, os.R_OK): |
| 41 | + cache_file = os.path.join(cache_dir, f"cache_{from_db}_{to_db}.tsv") |
| 42 | + if os.path.isfile(cache_file) and os.access(cache_file, os.R_OK): |
| 43 | + with open(cache_file, "r") as f: |
| 44 | + for line in f: |
| 45 | + key, value = line.strip().split("\t") |
| 46 | + cached_data[key] = value |
| 47 | + print(f"Loaded data from cache. Size: {len(cached_data)}") |
| 48 | + |
| 49 | + source_ids_not_cached = [item for item in source_ids if item not in cached_data] |
| 50 | + |
| 51 | + return source_ids_not_cached, cached_data |
| 52 | + |
| 53 | + |
| 54 | +def map_ids(ids, from_db, to_db, batch_size, delay): |
| 55 | + total_items = len(ids) |
| 56 | + num_batches = (total_items + batch_size - 1) // batch_size |
| 57 | + |
| 58 | + mapped_ids = [] |
| 59 | + for i in range(num_batches): |
| 60 | + start_idx = i * batch_size |
| 61 | + end_idx = min(start_idx + batch_size, total_items) |
| 62 | + batch_data = ids[start_idx:end_idx] |
| 63 | + |
| 64 | + print(f"Mapping batch {i+1}") |
| 65 | + mapped_ids.extend(map_ids_unipressed(from_db, to_db, batch_data)) |
| 66 | + |
| 67 | + time.sleep(delay) |
| 68 | + |
| 69 | + mapped_ids_dict = {} |
| 70 | + for mapping in mapped_ids: |
| 71 | + mapped_ids_dict[mapping['from']] = mapping['to'] |
| 72 | + |
| 73 | + return mapped_ids_dict |
| 74 | + |
| 75 | + |
| 76 | +def write_mapped_ids(output_file, source_ids, mapped_ids_dict, cached_data): |
| 77 | + with open(output_file, "w") as output: |
| 78 | + for source_id in source_ids: |
| 79 | + if source_id in cached_data: |
| 80 | + output.write(f"{source_id}\t{cached_data[source_id]}\n") |
| 81 | + elif source_id in mapped_ids_dict: |
| 82 | + output.write(f"{source_id}\t{mapped_ids_dict[source_id]}\n") |
| 83 | + else: |
| 84 | + output.write(f"{source_id}\t-\n") |
| 85 | + |
| 86 | + |
| 87 | +def save_cache(cache_dir, from_db, to_db, mapped_ids_dict): |
| 88 | + os.makedirs(cache_dir, exist_ok=True) |
| 89 | + if cache_dir and os.path.isdir(cache_dir) and os.access(cache_dir, os.R_OK): |
| 90 | + cache_file = os.path.join(cache_dir, f"cache_{from_db}_{to_db}.tsv") |
| 91 | + cached_data = {} |
| 92 | + with open(cache_file, "a") as cache_file: |
| 93 | + for key in mapped_ids_dict: |
| 94 | + cache_file.write(f"{key}\t{mapped_ids_dict[key]}\n") |
| 95 | + |
| 96 | + |
| 97 | +def main(from_db, to_db, input_file, output_file, batch_size=10, delay=1, cache_dir=""): |
| 98 | + validate_db(from_db, From, 'from') |
| 99 | + validate_db(to_db, To, 'to') |
| 100 | + |
| 101 | + print(f"Mapping IDs from '{from_db}' to '{to_db}' in batches of {batch_size} with a delay of {delay} second(s).") |
| 102 | + print(f"Cache directory: '{cache_dir}'") |
| 103 | + print(f"Input file: '{input_file}'") |
| 104 | + print(f"Output file: '{output_file}'\n") |
| 105 | + |
| 106 | + source_ids = load_input(input_file) |
| 107 | + source_ids_not_cached, cached_data = load_cache_and_subset_ids(cache_dir, from_db, to_db, source_ids) |
| 108 | + mapped_ids_dict = map_ids(source_ids_not_cached, from_db, to_db, batch_size, delay) |
| 109 | + write_mapped_ids(output_file, source_ids, mapped_ids_dict, cached_data) |
| 110 | + |
| 111 | + if cache_dir: |
| 112 | + save_cache(cache_dir, from_db, to_db, mapped_ids_dict) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + print('Script version:', os.getenv('VERSION', 'NA')) |
| 117 | + parser = argparse.ArgumentParser(description="Converts identifiers using the UniProt ID mapping server.") |
| 118 | + |
| 119 | + parser.add_argument("--from-db", type=str, help="Source database.", required=True) |
| 120 | + parser.add_argument("--to-db", type=str, help="Destination database.", required=True) |
| 121 | + parser.add_argument("--input", type=str, help="Path to the input data file with the source IDs to be converted (one per line).", required=True) |
| 122 | + parser.add_argument("--output", type=str, help="Path to the output file.", required=True) |
| 123 | + |
| 124 | + parser.add_argument("--batch-size", type=int, default=10, help="Batch size for querying IDs to the UniProt server.") |
| 125 | + parser.add_argument("--delay", type=int, default=1, help="Delay in seconds between batches.") |
| 126 | + parser.add_argument("--cache-dir", type=str, default="", help="Cache directory.") |
| 127 | + |
| 128 | + args = parser.parse_args() |
| 129 | + main(args.from_db, args.to_db, args.input, args.output, args.batch_size, args.delay, args.cache_dir) |
0 commit comments