Mercurial > repos > galaxyp > regex_find_replace
comparison regex.py @ 2:538933d9fccc draft
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/regex_find_replace commit 8871a847daed1f996c0a2069c3e876685bf8d220"
author | galaxyp |
---|---|
date | Tue, 07 Apr 2020 11:31:27 -0400 |
parents | 60d04307b027 |
children | 399da6b5ec21 |
comparison
equal
deleted
inserted
replaced
1:209b7c5ee9d7 | 2:538933d9fccc |
---|---|
1 import sys | 1 from optparse import OptionParser |
2 import os | |
3 import re | 2 import re |
4 import string | 3 |
5 import commands | |
6 from optparse import OptionParser | |
7 from tempfile import NamedTemporaryFile | |
8 | 4 |
9 def main(): | 5 def main(): |
10 parser = OptionParser() | 6 parser = OptionParser() |
11 parser.add_option("--input", dest="input") | 7 parser.add_option("--input", dest="input") |
12 parser.add_option("--output", dest="output") | 8 parser.add_option("--output", dest="output") |
13 parser.add_option("--input_display_name", dest="input_display_name") | 9 parser.add_option("--input_display_name", dest="input_display_name") |
14 parser.add_option("--pattern", dest="patterns", action="append", | 10 parser.add_option("--pattern", dest="patterns", action="append", |
15 help="regex pattern for replacement") | 11 help="regex pattern for replacement") |
16 parser.add_option("--replacement", dest="replacements", action="append", | 12 parser.add_option("--replacement", dest="replacements", action="append", |
17 help="replacement for regex match") | 13 help="replacement for regex match") |
18 parser.add_option("--column", dest="column", default=None) | 14 parser.add_option("--column", dest="column", default=None) |
19 (options, args) = parser.parse_args() | 15 (options, args) = parser.parse_args() |
20 | 16 |
21 mapped_chars = { '\'' :'__sq__', '\\' : '__backslash__' } | 17 mapped_chars = {'\'': '__sq__', '\\': '__backslash__'} |
22 | 18 |
23 column = None | 19 column = None |
24 if options.column is not None: | 20 if options.column is not None: |
25 column = int(options.column) - 1 # galaxy tabular is 1-based, python array are zero-based | 21 # galaxy tabular is 1-based, python array are zero-based |
22 column = int(options.column) - 1 | |
26 | 23 |
27 with open(options.input, 'r') as input: | 24 with open(options.input, 'r') as input, open(options.output, 'w') as output: |
28 with open(options.output, 'w') as output: | 25 while True: |
29 while True: | 26 line = input.readline() |
30 line = input.readline() | 27 if line == "": |
31 if line == "": | 28 break |
32 break | 29 for (pattern, replacement) in zip(options.patterns, options.replacements): |
33 for (pattern, replacement) in zip(options.patterns, options.replacements): | 30 for key, value in mapped_chars.items(): |
34 for key, value in mapped_chars.items(): | 31 pattern = pattern.replace(value, key) |
35 pattern = pattern.replace(value, key) | 32 replacement = replacement.replace(value, key) |
36 replacement = replacement.replace(value, key) | 33 replacement = replacement.replace("#{input_name}", options.input_display_name) |
37 replacement = replacement.replace("#{input_name}", options.input_display_name) | 34 if column is None: |
38 if column is None: | 35 line = re.sub(pattern, replacement, line) |
39 line = re.sub(pattern, replacement, line) | 36 else: |
40 else: | 37 cells = line.split("\t") |
41 cells = line.split("\t") | 38 if cells and len(cells) > column: |
42 if cells and len(cells) > column: | 39 cell = cells[column] |
43 cell = cells[column] | 40 cell = re.sub(pattern, replacement, cell) |
44 cell = re.sub(pattern, replacement, cell) | 41 cells[column] = cell |
45 cells[column] = cell | 42 line = "\t".join(cells) |
46 line = "\t".join(cells) | 43 output.write(line) |
47 output.write(line) | 44 |
48 | 45 |
49 if __name__ == "__main__": | 46 if __name__ == "__main__": |
50 main() | 47 main() |