0
|
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() |