annotate convert_characters.py @ 0:64d46676a13e draft

Uploaded convert_characters tarball.
author devteam
date Tue, 04 Dec 2012 10:55:58 -0500
parents
children 042ed2d0a017
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
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
2 #By, Guruprasad Ananda.
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
3
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
4 from galaxy import eggs
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
5 import sys, re
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
6
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
7 def stop_err(msg):
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
8 sys.stderr.write(msg)
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
9 sys.exit()
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
10
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:
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
13 stop_err("usage: convert_characters infile from_char outfile")
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
14
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
15 try:
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
16 fin = open(sys.argv[1],'r')
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
17 except:
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
18 stop_err("Input file cannot be opened for reading.")
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
19
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
20 from_char = sys.argv[2]
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
21
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
22 try:
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
23 fout = open(sys.argv[3],'w')
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
24 except:
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
25 stop_err("Output file cannot be opened for writing.")
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
26
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
27 char_dict = {'T':'\t','s':'\s','Dt':'\.','C':',','D':'-','U':'_','P':'\|','Co':':'}
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
28 from_ch = char_dict[from_char] + '+' #making an RE to match 1 or more occurences.
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
29 skipped = 0
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
30
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
31 for line in fin:
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
32 line = line.strip()
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
33 try:
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
34 fout.write("%s\n" %(re.sub(from_ch,'\t',line)))
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
35 except:
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
36 skipped += 1
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
37
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
38 if skipped:
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
39 print "Skipped %d lines as invalid." %skipped
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
40
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
41 if __name__ == "__main__":
64d46676a13e Uploaded convert_characters tarball.
devteam
parents:
diff changeset
42 main()