annotate riboseqr/utils.py @ 0:e01de823e919 draft default tip

Uploaded
author biotechcoder
date Fri, 01 May 2015 05:41:51 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
1 """Common functions"""
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
2
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
3
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
4 def process_args(args, ret_type='str', ret_mode=None):
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
5 """Split arguments (only strings) on comma, return in requested
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
6
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
7 ret_type
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
8 (str, int or bool)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
9
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
10 as
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
11
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
12 ret_mode
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
13 char vector - c(1,2,3)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
14 list vector - list(1,2,3)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
15 list - as list [1,2,3]
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
16 None - same as input
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
17
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
18 """
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
19 all_args = [item.strip() for item in args.split(',')]
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
20 num_options = len(all_args)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
21 if num_options == 1 and len(all_args[0]):
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
22 if ret_type == 'int':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
23 option = int(all_args[0])
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
24 if ret_mode == 'charvector':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
25 # if option:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
26 return 'c({})'.format(option)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
27 # else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
28 # return 'c()'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
29 elif ret_mode == 'listvector':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
30 # if option:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
31 return 'list({})'.format(option)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
32 # else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
33 # return 'list()'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
34 elif ret_mode == 'list':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
35 # if option:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
36 return [option]
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
37 # else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
38 # return []
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
39 else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
40 return option
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
41 else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
42 # str, bool
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
43 option = all_args[0]
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
44 if ret_mode == 'charvector':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
45 # if len(option):
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
46 return 'c("{}")'.format(option)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
47 # else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
48 # return 'c("")'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
49 elif ret_mode == 'listvector':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
50 # if option:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
51 return 'list("{}")'.format(option)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
52 # else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
53 # return 'list("")'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
54 elif ret_mode == 'list':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
55 # if option:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
56 return [option]
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
57 # else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
58 # return []
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
59 else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
60 return option
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
61 elif num_options > 1:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
62 if ret_type == 'int':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
63 options = tuple([int(item) for item in all_args])
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
64 if ret_mode == 'charvector':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
65 # if len(options):
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
66 return 'c{}'.format(options)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
67 # else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
68 # return 'c()'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
69 elif ret_mode == 'listvector':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
70 # if len(options):
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
71 return 'list{}'.format(options)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
72 # else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
73 # return 'list()'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
74 elif ret_mode == 'list':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
75 return list(options)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
76 else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
77 options = tuple(all_args)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
78 if ret_mode == 'charvector':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
79 # if len(options):
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
80 return 'c{}'.format(options)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
81 elif ret_mode == 'listvector':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
82 return 'list{}'.format(options)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
83 elif ret_mode == 'list':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
84 # if len(all_args):
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
85 return all_args
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
86 # else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
87 # return []
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
88 else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
89 # as original with spaces stripped
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
90 return ','.join(all_args)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
91
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
92