forked from nloyfer/wgbs_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wgbs_tools.py
executable file
·97 lines (79 loc) · 2.12 KB
/
wgbs_tools.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
91
92
93
94
95
96
#!/usr/bin/env python3
import sys
import argparse
import importlib
from unittest.mock import patch
VERSION = '0.2.0'
commands = [
# view data
'vis',
'view',
'cview',
'convert',
'pat_fig',
# convert beta to other formats
'beta_to_blocks',
'beta_to_table',
'beta2bed',
'beta2bw',
'beta_cov',
'beta_to_450k',
# generate pats and betas
'init_genome',
'set_default_ref',
'bam2pat',
'index',
'pat2beta',
'bed2beta',
'mix_pat',
'merge',
'segment',
# less important
'compare_betas',
'homog',
'find_markers',
'add_cpg_counts',
'frag_len',
'split_by_allele',
'test_bimodal'
]
def main():
if len(sys.argv) < 2 or (len(sys.argv) == 2 and sys.argv[1] in ('-h', '--help')):
print_help()
return
elif '--version' in sys.argv:
print('wgbstools version', VERSION)
return
parser = argparse.ArgumentParser(
description='WGBS data analysis',
usage='wgbstools <command> [<args>]')
parser.add_argument('command', help='Subcommand to run')
args = parser.parse_args(sys.argv[1:2])
try:
with patch.object(sys, 'argv', sys.argv[1:]):
importlib.import_module(args.command).main()
except ModuleNotFoundError as e:
if args.command not in str(e):
raise e
print_invalid_command(args.command)
print_help()
except ValueError as e:
eprint(f'Invalid input argument\n{e}')
return 1
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def print_invalid_command(command):
eprint('Invalid command:', f'\033[01;31m{command}\033[00m')
from difflib import get_close_matches
closets = [x for x in get_close_matches(command, commands)]
if closets:
eprint(f'did you mean \033[01;32m{closets[0]}\033[00m?')
def print_help(command=None):
msg = '\nUsage: wgbstools <command> [<args>]'
msg += '\nrun wgbstools <command> -h for more information'
msg += '\nOptional commands:\n'
print(msg)
print(*commands, sep='\n')
return 1
if __name__ == '__main__':
main()