annotate convert_characters.py @ 3:042ed2d0a017 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 20:25:21 -0400
parents 64d46676a13e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
1 #!/usr/bin/env python
3
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
2 """
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
3 By, Guruprasad Ananda.
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
4 """
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
5 from __future__ import print_function
0
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
6
3
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
7 import re
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
8 import sys
0
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
9
3
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
10
0
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
11 def main():
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
12 if len(sys.argv) != 4:
3
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
13 sys.exit("usage: convert_characters infile from_char outfile")
0
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
14
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
15 try:
3
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
16 fin = open(sys.argv[1], 'r')
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
17 except Exception:
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
18 sys.exit("Input file cannot be opened for reading.")
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
19
0
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
20 from_char = sys.argv[2]
3
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
21
0
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
22 try:
3
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
23 fout = open(sys.argv[3], 'w')
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
24 except Exception:
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
25 sys.exit("Output file cannot be opened for writing.")
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
26
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
27 char_dict = {'T': r'\t', 's': r'\s', 'Dt': r'\.', 'C': r',', 'D': r'-',
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
28 'U': r'_', 'P': r'\|', 'Co': r':'}
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
29 # making an RE to match 1 or more occurences.
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
30 from_ch = char_dict[from_char] + '+'
0
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
31 skipped = 0
3
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
32
0
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
33 for line in fin:
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
34 line = line.strip()
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
35 try:
3
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
36 fout.write("%s\n" % (re.sub(from_ch, '\t', line)))
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
37 except Exception:
0
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
38 skipped += 1
3
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
39
0
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
40 if skipped:
3
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
41 print("Skipped %d lines as invalid." % skipped)
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
42
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
43
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
44 if __name__ == "__main__":
042ed2d0a017 "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/convert_characters commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
devteam
parents: 0
diff changeset
45 main()