comparison filters.py @ 13:4d5aae46f850 draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
author iuc
date Thu, 19 Aug 2021 19:39:58 +0000
parents 6544e4b87a4f
children
comparison
equal deleted inserted replaced
12:37cde8134c6a 13:4d5aae46f850
2 2
3 from __future__ import print_function 3 from __future__ import print_function
4 4
5 import re 5 import re
6 import sys 6 import sys
7 from itertools import chain
7 8
8 9
9 class LineFilter(object): 10 class LineFilter(object):
10 def __init__(self, source, filter_dict): 11 def __init__(self, source, filter_dict):
11 self.source = source 12 self.source = source
12 self.filter_dict = filter_dict 13 self.filter_dict = filter_dict
13 self.func = lambda i, l: l.rstrip('\r\n') if l else None 14 self.func = lambda i, l: l.rstrip('\r\n') if l else None
14 self.src_lines = [] 15 self.src_lines = []
15 self.src_line_cnt = 0 16 self.src_line_cnt = 0
17
18 def xint(x):
19 if isinstance(x, int):
20 return x
21 try:
22 return int(x)
23 except Exception:
24 return x if x else None
25
16 if not filter_dict: 26 if not filter_dict:
17 return 27 return
18 if filter_dict['filter'] == 'regex': 28 if filter_dict['filter'] == 'regex':
19 rgx = re.compile(filter_dict['pattern']) 29 rgx = re.compile(filter_dict['pattern'])
20 if filter_dict['action'] == 'exclude_match': 30 if filter_dict['action'] == 'exclude_match':
26 elif filter_dict['action'] == 'include_find': 36 elif filter_dict['action'] == 'include_find':
27 self.func = lambda i, l: l if rgx.search(l) else None 37 self.func = lambda i, l: l if rgx.search(l) else None
28 elif filter_dict['filter'] == 'select_columns': 38 elif filter_dict['filter'] == 'select_columns':
29 cols = [int(c) - 1 for c in filter_dict['columns']] 39 cols = [int(c) - 1 for c in filter_dict['columns']]
30 self.func = lambda i, l: self.select_columns(l, cols) 40 self.func = lambda i, l: self.select_columns(l, cols)
41 elif filter_dict['filter'] == 'select_column_slices':
42 cols = [x if isinstance(x, int) else [y if y is not None else None for y in [xint(k) for k in x.split(':')]] for x in [xint(c) for c in filter_dict['columns']]]
43 if all([isinstance(x, int) for x in cols]):
44 self.func = lambda i, l: self.select_columns(l, cols)
45 else:
46 cols = [slice(x[0], x[1], x[2] if len(x) > 2 else None) if isinstance(x, list) else x for x in cols]
47 self.func = lambda i, l: self.select_slices(l, cols)
31 elif filter_dict['filter'] == 'replace': 48 elif filter_dict['filter'] == 'replace':
32 p = filter_dict['pattern'] 49 p = filter_dict['pattern']
33 r = filter_dict['replace'] 50 r = filter_dict['replace']
34 c = int(filter_dict['column']) - 1 51 c = int(filter_dict['column']) - 1
35 if 'add' not in filter_dict\ 52 if 'add' not in filter_dict\
77 next = __next__ 94 next = __next__
78 95
79 def select_columns(self, line, cols): 96 def select_columns(self, line, cols):
80 fields = line.split('\t') 97 fields = line.split('\t')
81 return '\t'.join([fields[x] for x in cols]) 98 return '\t'.join([fields[x] for x in cols])
99
100 def select_slices(self, line, cols):
101 fields = line.split('\t')
102 return '\t'.join(chain.from_iterable([y if isinstance(y, list) else [y] for y in [fields[x] for x in cols]]))
82 103
83 def replace_add(self, line, pat, rep, col, pos): 104 def replace_add(self, line, pat, rep, col, pos):
84 fields = line.rstrip('\r\n').split('\t') 105 fields = line.rstrip('\r\n').split('\t')
85 i = pos if pos is not None else len(fields) 106 i = pos if pos is not None else len(fields)
86 val = '' 107 val = ''