Mercurial > repos > devteam > convert_characters
comparison convert_characters.py @ 1:81a6c3de5de3 draft default tip
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
author | devteam |
---|---|
date | Mon, 06 Jul 2020 18:10:56 +0000 |
parents | 3100886bf698 |
children |
comparison
equal
deleted
inserted
replaced
0:3100886bf698 | 1:81a6c3de5de3 |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 #By, Guruprasad Ananda. | 2 """ |
3 By, Guruprasad Ananda. | |
4 """ | |
5 from __future__ import print_function | |
3 | 6 |
4 from galaxy import eggs | 7 import re |
5 import sys, re | 8 import sys |
6 | 9 |
7 def stop_err(msg): | 10 |
8 sys.stderr.write(msg) | |
9 sys.exit() | |
10 | |
11 def main(): | 11 def main(): |
12 if len(sys.argv) != 4: | 12 if len(sys.argv) != 4: |
13 stop_err("usage: convert_characters infile from_char outfile") | 13 sys.exit("usage: convert_characters infile from_char outfile") |
14 | 14 |
15 try: | 15 try: |
16 fin = open(sys.argv[1],'r') | 16 fin = open(sys.argv[1], 'r') |
17 except: | 17 except Exception: |
18 stop_err("Input file cannot be opened for reading.") | 18 sys.exit("Input file cannot be opened for reading.") |
19 | 19 |
20 from_char = sys.argv[2] | 20 from_char = sys.argv[2] |
21 | 21 |
22 try: | 22 try: |
23 fout = open(sys.argv[3],'w') | 23 fout = open(sys.argv[3], 'w') |
24 except: | 24 except Exception: |
25 stop_err("Output file cannot be opened for writing.") | 25 sys.exit("Output file cannot be opened for writing.") |
26 | 26 |
27 char_dict = {'T':'\t','s':'\s','Dt':'\.','C':',','D':'-','U':'_','P':'\|','Co':':'} | 27 char_dict = {'T': r'\t', 's': r'\s', 'Dt': r'\.', 'C': r',', 'D': r'-', |
28 from_ch = char_dict[from_char] + '+' #making an RE to match 1 or more occurences. | 28 'U': r'_', 'P': r'\|', 'Co': r':'} |
29 # making an RE to match 1 or more occurences. | |
30 from_ch = char_dict[from_char] + '+' | |
29 skipped = 0 | 31 skipped = 0 |
30 | 32 |
31 for line in fin: | 33 for line in fin: |
32 line = line.strip() | 34 line = line.strip() |
33 try: | 35 try: |
34 fout.write("%s\n" %(re.sub(from_ch,'\t',line))) | 36 fout.write("%s\n" % (re.sub(from_ch, '\t', line))) |
35 except: | 37 except Exception: |
36 skipped += 1 | 38 skipped += 1 |
37 | 39 |
38 if skipped: | 40 if skipped: |
39 print "Skipped %d lines as invalid." %skipped | 41 print("Skipped %d lines as invalid." % skipped) |
40 | 42 |
41 if __name__ == "__main__": | 43 |
44 if __name__ == "__main__": | |
42 main() | 45 main() |