|
| 1 | +import re |
| 2 | +import gzip |
| 3 | +import sys |
| 4 | + |
| 5 | +class genome_position: |
| 6 | + global all_chromosomes |
| 7 | + all_chromosomes = [str(i) for i in range(1,23)] + ["MT", "X", "Y", ""] |
| 8 | + def __init__(self, chromosome, position): |
| 9 | + self.chromosome = str(chromosome) |
| 10 | + self.position = int(position) |
| 11 | + def __eq__(self, other): |
| 12 | + if (self.chromosome == other.chromosome) and (self.position == other.position): |
| 13 | + return True |
| 14 | + else: |
| 15 | + return False |
| 16 | + def __lt__(self, other): |
| 17 | + if (all_chromosomes.index(self.chromosome) < all_chromosomes.index(other.chromosome)): |
| 18 | + return True |
| 19 | + elif (all_chromosomes.index(self.chromosome) == all_chromosomes.index(other.chromosome)) and (self.position < other.position): |
| 20 | + return True |
| 21 | + else: |
| 22 | + return False |
| 23 | + def __gr__(self, other): |
| 24 | + if (all_chromosomes.index(self.chromosome) > all_chromosomes.index(other.chromosome)): |
| 25 | + return True |
| 26 | + elif (all_chromosomes.index(self.chromosome) == all_chromosomes.index(other.chromosome)) and (self.position > other.position): |
| 27 | + return True |
| 28 | + else: |
| 29 | + return False |
| 30 | + def __le__(self, other): |
| 31 | + return (self < other) or (self == other) |
| 32 | + def __ge__(self, other): |
| 33 | + return (self > other) or (self == other) |
| 34 | + |
| 35 | +def process_region(region_file): |
| 36 | + region = region_file.readline() |
| 37 | + if region == "": |
| 38 | + return genome_position("", 0), genome_position("", 0) |
| 39 | + region_split = region.strip().split("\t") |
| 40 | + region_start = genome_position(region_split[0], region_split[1]) |
| 41 | + region_end = genome_position(region_split[0], region_split[2]) |
| 42 | + return region_start, region_end |
| 43 | + |
| 44 | +def main(): |
| 45 | + pos_file = gzip.open("AOU_af_cds_eur.txt.gz", "rt") |
| 46 | + region_file = open("../gnomad_AF/exome_regions.txt", "r") |
| 47 | + output_file = open(f"exome_AN_AOU_eur.bed", "w") |
| 48 | + |
| 49 | + region_start, region_end = process_region(region_file) |
| 50 | + curr_AN = None |
| 51 | + marker_pos = None |
| 52 | + pos_file.readline() |
| 53 | + for line in pos_file: |
| 54 | + line_split = line.strip().split("\t") |
| 55 | + AN_pos = genome_position(line_split[0], line_split[1]) |
| 56 | + while region_end <= AN_pos: |
| 57 | + if curr_AN is not None: |
| 58 | + print(region_start.chromosome, str(region_start.position - 1), str(region_end.position - 1), curr_AN, sep = "\t", file = output_file, flush = True) |
| 59 | + region_start, region_end = process_region(region_file) |
| 60 | + curr_AN = None |
| 61 | + marker_pos = None |
| 62 | + if region_start > AN_pos: |
| 63 | + continue |
| 64 | + AN = line_split[4] |
| 65 | + if (curr_AN is not None) and (curr_AN != AN): |
| 66 | + print(region_start.chromosome, str(region_start.position - 1), str((marker_pos + AN_pos.position + 1) // 2 - 1), curr_AN, sep = "\t", file = output_file, flush = True) |
| 67 | + region_start.position = (marker_pos + AN_pos.position + 1) // 2 |
| 68 | + marker_pos = AN_pos.position |
| 69 | + curr_AN = AN |
| 70 | + if curr_AN is not None: |
| 71 | + print(region_start.chromosome, str(region_start.position - 1), str(region_end.position - 1), curr_AN, sep = "\t", file = output_file, flush = True) |
| 72 | + |
| 73 | + pos_file.close() |
| 74 | + region_file.close() |
| 75 | + output_file.close() |
| 76 | + |
| 77 | + |
| 78 | +if __name__=="__main__": |
| 79 | + main() |
| 80 | + |
0 commit comments