diff 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
line wrap: on
line diff
--- a/regex.py	Thu Jan 19 11:26:15 2017 -0500
+++ b/regex.py	Tue Apr 07 11:31:27 2020 -0400
@@ -1,50 +1,47 @@
-import sys
-import os
+from optparse import OptionParser
 import re
-import string
-import commands
-from optparse import OptionParser
-from tempfile import NamedTemporaryFile
+
 
 def main():
-  parser = OptionParser()
-  parser.add_option("--input", dest="input")
-  parser.add_option("--output", dest="output")
-  parser.add_option("--input_display_name", dest="input_display_name")
-  parser.add_option("--pattern", dest="patterns", action="append",
-                    help="regex pattern for replacement")
-  parser.add_option("--replacement", dest="replacements", action="append",
-                    help="replacement for regex match")
-  parser.add_option("--column", dest="column", default=None)
-  (options, args) = parser.parse_args()
+    parser = OptionParser()
+    parser.add_option("--input", dest="input")
+    parser.add_option("--output", dest="output")
+    parser.add_option("--input_display_name", dest="input_display_name")
+    parser.add_option("--pattern", dest="patterns", action="append",
+                      help="regex pattern for replacement")
+    parser.add_option("--replacement", dest="replacements", action="append",
+                      help="replacement for regex match")
+    parser.add_option("--column", dest="column", default=None)
+    (options, args) = parser.parse_args()
 
-  mapped_chars = { '\'' :'__sq__', '\\' : '__backslash__' }
+    mapped_chars = {'\'': '__sq__', '\\': '__backslash__'}
 
-  column = None
-  if options.column is not None:
-    column = int(options.column) - 1 # galaxy tabular is 1-based, python array are zero-based 
+    column = None
+    if options.column is not None:
+        # galaxy tabular is 1-based, python array are zero-based
+        column = int(options.column) - 1
 
-  with open(options.input, 'r') as input:
-    with open(options.output, 'w') as output:
-      while True:
-        line = input.readline()
-        if line == "":
-          break
-        for (pattern, replacement) in zip(options.patterns, options.replacements):
-          for key, value in mapped_chars.items():
-            pattern = pattern.replace(value, key)
-            replacement = replacement.replace(value, key)
-          replacement = replacement.replace("#{input_name}", options.input_display_name)
-          if column is None:
-            line = re.sub(pattern, replacement, line)
-          else:
-            cells = line.split("\t")
-            if cells and len(cells) > column:
-              cell = cells[column]
-              cell = re.sub(pattern, replacement, cell)
-              cells[column] = cell
-              line = "\t".join(cells)
-        output.write(line)
+    with open(options.input, 'r') as input, open(options.output, 'w') as output:
+        while True:
+            line = input.readline()
+            if line == "":
+                break
+            for (pattern, replacement) in zip(options.patterns, options.replacements):
+                for key, value in mapped_chars.items():
+                    pattern = pattern.replace(value, key)
+                    replacement = replacement.replace(value, key)
+                replacement = replacement.replace("#{input_name}", options.input_display_name)
+                if column is None:
+                    line = re.sub(pattern, replacement, line)
+                else:
+                    cells = line.split("\t")
+                    if cells and len(cells) > column:
+                        cell = cells[column]
+                        cell = re.sub(pattern, replacement, cell)
+                        cells[column] = cell
+                        line = "\t".join(cells)
+            output.write(line)
+
 
 if __name__ == "__main__":
     main()