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