Mercurial > repos > iuc > sqlite_to_tabular
comparison filters.py @ 9:4678715f7147 draft
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
author | iuc |
---|---|
date | Sat, 12 Sep 2020 01:22:24 +0000 |
parents | c1b700bc0150 |
children | bce29ec10b78 |
comparison
equal
deleted
inserted
replaced
8:5a4ae7678bc5 | 9:4678715f7147 |
---|---|
30 self.func = lambda i, l: self.select_columns(l, cols) | 30 self.func = lambda i, l: self.select_columns(l, cols) |
31 elif filter_dict['filter'] == 'replace': | 31 elif filter_dict['filter'] == 'replace': |
32 p = filter_dict['pattern'] | 32 p = filter_dict['pattern'] |
33 r = filter_dict['replace'] | 33 r = filter_dict['replace'] |
34 c = int(filter_dict['column']) - 1 | 34 c = int(filter_dict['column']) - 1 |
35 self.func = lambda i, l: '\t'.join( | 35 if 'add' not in filter_dict\ |
36 [x if j != c else re.sub(p, r, x) | 36 or filter_dict['add'] not in ['prepend', |
37 for j, x in enumerate(l.split('\t'))]) | 37 'append', |
38 'before', | |
39 'after']: | |
40 self.func = lambda i, l: '\t'.join( | |
41 [x if j != c else re.sub(p, r, x) | |
42 for j, x in enumerate(l.split('\t'))]) | |
43 else: | |
44 a = 0 if filter_dict['add'] == 'prepend'\ | |
45 else min(0, c - 1) if filter_dict['add'] == 'before'\ | |
46 else c + 1 if filter_dict['add'] == 'after'\ | |
47 else None | |
48 self.func = lambda i, l: self.replace_add(l, p, r, c, a) | |
38 elif filter_dict['filter'] == 'prepend_line_num': | 49 elif filter_dict['filter'] == 'prepend_line_num': |
39 self.func = lambda i, l: '%d\t%s' % (i, l) | 50 self.func = lambda i, l: '%d\t%s' % (i, l) |
40 elif filter_dict['filter'] == 'append_line_num': | 51 elif filter_dict['filter'] == 'append_line_num': |
41 self.func = lambda i, l: '%s\t%d' % (l.rstrip('\r\n'), i) | 52 self.func = lambda i, l: '%s\t%d' % (l.rstrip('\r\n'), i) |
42 elif filter_dict['filter'] == 'prepend_text': | 53 elif filter_dict['filter'] == 'prepend_text': |
66 next = __next__ | 77 next = __next__ |
67 | 78 |
68 def select_columns(self, line, cols): | 79 def select_columns(self, line, cols): |
69 fields = line.split('\t') | 80 fields = line.split('\t') |
70 return '\t'.join([fields[x] for x in cols]) | 81 return '\t'.join([fields[x] for x in cols]) |
82 | |
83 def replace_add(self, line, pat, rep, col, pos): | |
84 fields = line.rstrip('\r\n').split('\t') | |
85 i = pos if pos else len(fields) | |
86 val = '' | |
87 if col < len(fields) and re.search(pat, fields[col]): | |
88 val = re.sub(pat, rep, fields[col]).replace('\t', ' ') | |
89 return '\t'.join(fields[:i] + [val] + fields[i:]) | |
71 | 90 |
72 def normalize(self, line, split_cols, sep): | 91 def normalize(self, line, split_cols, sep): |
73 lines = [] | 92 lines = [] |
74 fields = line.rstrip('\r\n').split('\t') | 93 fields = line.rstrip('\r\n').split('\t') |
75 split_fields = dict() | 94 split_fields = dict() |