-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick_bed_sort.py
More file actions
34 lines (26 loc) · 958 Bytes
/
Copy pathQuick_bed_sort.py
File metadata and controls
34 lines (26 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import gzip
good_chromosome = set()
chromosome_order = []
with open("standard_selection.tsv", "r") as f_1:
for line in f_1:
chromosome = line.strip()
good_chromosome.add(chromosome)
chromosome_order.append(chromosome)
data_to_sort = {}
file_list = ["shuf.a.bed.gz", "shuf.b.bed.gz"]
for bed_file in file_list:
with gzip.open(bed_file, "rt") as f:
for line in f:
column = line.split('\t')
chromosome = column[0]
if chromosome in good_chromosome:
if chromosome not in data_to_sort:
data_to_sort[chromosome] = []
data_to_sort[chromosome].append(line)
open_filename = "X.standard.sorted.bed"
with open(open_filename, "w") as out_file:
for chromosome in chromosome_order:
line_list = data_to_sort.get(chromosome, [])
line_list.sort(key=lambda bed_line: (int(bed_line.split('\t')[1]), int(bed_line.split('\t')[2])))
for bed_line in line_list:
out_file.write(bed_line)