Mercurial > repos > iuc > filter_tabular
annotate filters.py @ 15:90f657745fea draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
author | iuc |
---|---|
date | Thu, 27 Jun 2024 17:23:47 +0000 |
parents | 4d5aae46f850 |
children |
rev | line source |
---|---|
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
1 #!/usr/binsenv python |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
2 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
3 from __future__ import print_function |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
4 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
5 import re |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
6 import sys |
13
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
7 from itertools import chain |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
8 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
9 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
10 class LineFilter(object): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
11 def __init__(self, source, filter_dict): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
12 self.source = source |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
13 self.filter_dict = filter_dict |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
14 self.func = lambda i, line: line.rstrip('\r\n') if line else None |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
15 self.src_lines = [] |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
16 self.src_line_cnt = 0 |
13
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
17 |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
18 def xint(x): |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
19 if isinstance(x, int): |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
20 return x |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
21 try: |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
22 return int(x) |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
23 except Exception: |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
24 return x if x else None |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
25 |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
26 if not filter_dict: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
27 return |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
28 if filter_dict['filter'] == 'regex': |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
29 rgx = re.compile(filter_dict['pattern']) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
30 if filter_dict['action'] == 'exclude_match': |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
31 self.func = lambda i, line: line if not rgx.match(line) else None |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
32 elif filter_dict['action'] == 'include_match': |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
33 self.func = lambda i, line: line if rgx.match(line) else None |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
34 elif filter_dict['action'] == 'exclude_find': |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
35 self.func = lambda i, line: line if not rgx.search(line) else None |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
36 elif filter_dict['action'] == 'include_find': |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
37 self.func = lambda i, line: line if rgx.search(line) else None |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
38 elif filter_dict['filter'] == 'select_columns': |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
39 cols = [int(c) - 1 for c in filter_dict['columns']] |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
40 self.func = lambda i, line: self.select_columns(line, cols) |
13
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
41 elif filter_dict['filter'] == 'select_column_slices': |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
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']]] |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
43 if all([isinstance(x, int) for x in cols]): |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
44 self.func = lambda i, line: self.select_columns(line, cols) |
13
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
45 else: |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
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] |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
47 self.func = lambda i, line: self.select_slices(line, cols) |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
48 elif filter_dict['filter'] == 'replace': |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
49 p = filter_dict['pattern'] |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
50 r = filter_dict['replace'] |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
51 c = int(filter_dict['column']) - 1 |
9
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
52 if 'add' not in filter_dict\ |
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
53 or filter_dict['add'] not in ['prepend', |
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
54 'append', |
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
55 'before', |
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
56 'after']: |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
57 self.func = lambda i, line: '\t'.join( |
9
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
58 [x if j != c else re.sub(p, r, x) |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
59 for j, x in enumerate(line.split('\t'))]) |
9
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
60 else: |
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
61 a = 0 if filter_dict['add'] == 'prepend'\ |
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
62 else min(0, c - 1) if filter_dict['add'] == 'before'\ |
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
63 else c + 1 if filter_dict['add'] == 'after'\ |
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
64 else None |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
65 self.func = lambda i, line: self.replace_add(line, p, r, c, a) |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
66 elif filter_dict['filter'] == 'prepend_line_num': |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
67 self.func = lambda i, line: '%d\t%s' % (i, line) |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
68 elif filter_dict['filter'] == 'append_line_num': |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
69 self.func = lambda i, line: '%s\t%d' % (line.rstrip('\r\n'), i) |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
70 elif filter_dict['filter'] == 'prepend_text': |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
71 s = filter_dict['column_text'] |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
72 self.func = lambda i, line: '%s\t%s' % (s, line) |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
73 elif filter_dict['filter'] == 'append_text': |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
74 s = filter_dict['column_text'] |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
75 self.func = lambda i, line: '%s\t%s' % (line.rstrip('\r\n'), s) |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
76 elif filter_dict['filter'] == 'skip': |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
77 cnt = filter_dict['count'] |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
78 self.func = lambda i, line: line if i > cnt else None |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
79 elif filter_dict['filter'] == 'normalize': |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
80 cols = [int(c) - 1 for c in filter_dict['columns']] |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
81 sep = filter_dict['separator'] |
15
90f657745fea
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit dd35055c76d86fe98985b5825c1751efb8208242
iuc
parents:
13
diff
changeset
|
82 self.func = lambda i, line: self.normalize(line, cols, sep) |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
83 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
84 def __iter__(self): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
85 return self |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
86 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
87 def __next__(self): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
88 if not self.src_lines: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
89 self.get_lines() |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
90 if self.src_lines: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
91 return self.src_lines.pop(0) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
92 raise StopIteration |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
93 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
94 next = __next__ |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
95 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
96 def select_columns(self, line, cols): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
97 fields = line.split('\t') |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
98 return '\t'.join([fields[x] for x in cols]) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
99 |
13
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
100 def select_slices(self, line, cols): |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
101 fields = line.split('\t') |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
102 return '\t'.join(chain.from_iterable([y if isinstance(y, list) else [y] for y in [fields[x] for x in cols]])) |
4d5aae46f850
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 35576d64a12fa664d72559172c5960c09da2b632"
iuc
parents:
11
diff
changeset
|
103 |
9
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
104 def replace_add(self, line, pat, rep, col, pos): |
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
105 fields = line.rstrip('\r\n').split('\t') |
11
6544e4b87a4f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit d624cde6382bc326a5ae318482e16e643ef7d7d1"
iuc
parents:
9
diff
changeset
|
106 i = pos if pos is not None else len(fields) |
9
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
107 val = '' |
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
108 if col < len(fields) and re.search(pat, fields[col]): |
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
109 val = re.sub(pat, rep, fields[col]).replace('\t', ' ') |
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
110 return '\t'.join(fields[:i] + [val] + fields[i:]) |
69b08fc9557c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit daa9af57fe07ee83a45ddc9f855716f9d14a8e12"
iuc
parents:
1
diff
changeset
|
111 |
0
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
112 def normalize(self, line, split_cols, sep): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
113 lines = [] |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
114 fields = line.rstrip('\r\n').split('\t') |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
115 split_fields = dict() |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
116 cnt = 0 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
117 for c in split_cols: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
118 if c < len(fields): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
119 split_fields[c] = fields[c].split(sep) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
120 cnt = max(cnt, len(split_fields[c])) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
121 if cnt == 0: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
122 lines.append('\t'.join(fields)) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
123 else: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
124 for n in range(0, cnt): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
125 flds = [x if c not in split_cols else split_fields[c][n] |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
126 if n < len(split_fields[c]) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
127 else '' for (c, x) in enumerate(fields)] |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
128 lines.append('\t'.join(flds)) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
129 return lines |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
130 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
131 def get_lines(self): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
132 for i, next_line in enumerate(self.source): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
133 self.src_line_cnt += 1 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
134 line = self.func(self.src_line_cnt, next_line) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
135 if line: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
136 if isinstance(line, list): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
137 self.src_lines.extend(line) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
138 else: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
139 self.src_lines.append(line) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
140 return |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
141 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
142 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
143 class TabularReader: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
144 """ |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
145 Tabular file iterator. Returns a list |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
146 """ |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
147 def __init__(self, input_file, skip=0, comment_char=None, col_idx=None, |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
148 filters=None): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
149 self.skip = skip |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
150 self.comment_char = comment_char |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
151 self.col_idx = col_idx |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
152 self.filters = filters |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
153 self.tsv_file = \ |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
154 input_file if hasattr(input_file, 'readline') else open(input_file) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
155 if skip and skip > 0: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
156 for i in range(skip): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
157 if not self.tsv_file.readline(): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
158 break |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
159 source = LineFilter(self.tsv_file, None) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
160 if comment_char: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
161 source = LineFilter(source, |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
162 {"filter": "regex", "pattern": comment_char, |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
163 "action": "exclude_match"}) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
164 if filters: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
165 for f in filters: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
166 source = LineFilter(source, f) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
167 self.source = source |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
168 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
169 def __iter__(self): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
170 return self |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
171 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
172 def __next__(self): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
173 ''' Iteration ''' |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
174 for i, line in enumerate(self.source): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
175 fields = line.rstrip('\r\n').split('\t') |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
176 if self.col_idx: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
177 fields = [fields[i] for i in self.col_idx] |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
178 return fields |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
179 raise StopIteration |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
180 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
181 next = __next__ |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
182 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
183 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
184 def filter_file(input_file, output, skip=0, comment_char='#', filters=None): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
185 data_lines = 0 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
186 try: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
187 tr = TabularReader(input_file, skip=skip, comment_char=comment_char, |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
188 filters=filters) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
189 for linenum, fields in enumerate(tr): |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
190 data_lines += 1 |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
191 try: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
192 output.write('%s\n' % '\t'.join(fields)) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
193 except Exception as e: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
194 print('Failed at line: %d err: %s' % (linenum, e), |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
195 file=sys.stderr) |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
196 except Exception as e: |
6fbd9d25ceef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
iuc
parents:
diff
changeset
|
197 exit('Error: %s' % (e)) |