-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
90 lines (67 loc) · 3.04 KB
/
main.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import sys
import subprocess
import os
from module.checks import check_headers, modify_file, sort_file
from module.logging import logger
from configs.config import *
main_logger = logger(__name__)
def check_arg():
if len(sys.argv) < 2:
print('Please provide a directory path containing BED files')
exit(0)
return sys.argv[1]
def create_dir(dir_path):
updated_dir_path = f"{dir_path}/updated"
bigbed_dir_path = f"{dir_path}/bigbed"
try:
updated_exist = os.path.exists(updated_dir_path)
bigbed_exist = os.path.exists(bigbed_dir_path)
if not updated_exist:
os.mkdir(updated_dir_path)
if not bigbed_exist:
os.mkdir(bigbed_dir_path)
return updated_dir_path, bigbed_dir_path
except OSError as error:
main_logger.error(f"Error creating subdirectory {updated_dir_path} \n {error}")
def run_process(dir_path):
try:
(updated_dir_path, bigbed_dir_path) = create_dir(dir_path)
for entry in os.scandir(dir_path):
if entry.path.endswith(".bed") and entry.is_file():
filepath = entry.path
bed_to_bigbed(filepath, updated_dir_path, bigbed_dir_path)
except RuntimeError as error:
main_logger.error(f"Error with bigBed conversion {error}")
def bed_to_bigbed(filepath, updated_dir_path, bigbed_dir_path):
try:
filename = os.path.basename(filepath)
modified_filepath = f"{updated_dir_path}/{filename}"
bigbed_filepath = f"{bigbed_dir_path}/{filename.split('.')[0]}.bb"
# file conversion to correct bed format
header_res = check_headers(filepath)
if header_res.returncode == 0 and int(header_res.stdout) == 1:
modify_res = modify_file(filepath, modified_filepath)
if modify_res.returncode != 0:
main_logger.error(f"Error modifying file {filepath}")
else:
sort_res = sort_file(modified_filepath)
if sort_res.returncode != 0:
main_logger.error(f"Error sorting file {filepath}")
else:
# convert bed to bigbed
print(f"Proceeding with file conversion from bed to bigbed - {filename}.")
cmd = f"./bedToBigBed -as={AUTOSQL_FILE} -type=bed6+1 {modified_filepath} " \
f"{CHROMSIZES} {bigbed_filepath}"
conversion = subprocess.run(
cmd, capture_output=True, text=True, shell=True)
if conversion.returncode == 0:
print(f"File conversion from bed to bigbed has been successful - {filename}")
else:
main_logger.error(f"Error converting file {modified_filepath} to bigBed format")
else:
main_logger.error(f"Incorrect headers in file {filename}")
except RuntimeError as error:
main_logger.error(f"Error with bigBed conversion {error}")
if __name__ == '__main__':
dir_path = check_arg()
run_process(dir_path)