]> git.donarmstrong.com Git - biopieces.git/blob - code_python/Cjung/lowercase_seq
migrated extract_seq
[biopieces.git] / code_python / Cjung / lowercase_seq
1 #!/usr/bin/python
2
3 import os, string, sys, getopt, Args, optparse
4
5 record_delimiter = "\n---\n"
6
7 class Lowercase_seq:
8         in_stream = None
9         out_stream = None
10         eo_buffer = False
11         buffer = ''
12         rec_dic = {}
13         rec_num = 0
14
15         ###########################################
16         def __init__(self):
17                 pass
18         ###########################################
19
20         ###########################################
21         def open_streams(self, input_file, output_file):
22                 #print input_file, output_file
23                 if input_file == '':
24                         self.in_stream = sys.stdin
25                         #print "in_stream = <STDIN>"
26                 else:
27                         try:
28                                 self.in_stream = open(input_file, 'r')
29                                 #print "in_stream = %s" % (input_file)
30                         except:
31                                 raise IOError
32
33                 if output_file == '':
34                         self.out_stream = sys.stdout
35                         #print "out_stream = <STDOUT>"
36                 else:
37                         try:
38                                 self.out_stream = open(output_file, 'w')
39                                 #print "out_stream = %s" % (output_file)
40                         except:
41                                 raise IOError
42         ###########################################
43
44         ###########################################
45         def close_streams(self):
46                 if self.in_stream:
47                         self.in_stream.close()
48                 if self.out_stream:
49                         self.out_stream.close()
50         ###########################################
51
52         ###########################################
53         def get_record(self):
54                 rec = ''
55                 eof_flag = False
56                 while not self.eo_buffer:
57                         #print "STDIN.ISATTY :", self.in_stream.isatty()
58
59                         if self.in_stream.isatty():
60                                 eof_flag = True
61
62                         if eof_flag:
63                                 if self.buffer == '':
64                                         self.eo_buffer = True
65                                         break
66                         else:
67                                 tmp = self.in_stream.read(1000)
68                         if not tmp:
69                                 eof_flag = True
70
71                         self.buffer = self.buffer + tmp
72                         delim_index = self.buffer.find(record_delimiter)
73                         if delim_index >= 0:
74                                 rec = self.buffer[:delim_index]
75                                 self.buffer = self.buffer[delim_index + len(record_delimiter):]
76                                 break
77                 return rec
78         ###########################################
79
80         ###########################################
81         def process_record(self, rec):
82                 #print "PARSE_RECORD"
83                 #print rec
84                 #print "==="
85                 lines = rec.split("\n")
86                 self.rec_num += 1
87                 self.rec_dic[self.rec_num] = {}
88                 for l in lines:
89                         toks = l.split(": ")
90                         if toks[0]=="SEQ":
91                                 self.rec_dic[self.rec_num][toks[0]] = toks[1].lower()
92                         else:
93                                 self.rec_dic[self.rec_num][toks[0]] = toks[1]
94                         #self.rec_dic[self.rec_num][toks[0]] = toks[1]
95                 #print self.rec_dic[self.rec_num]
96                 return self.rec_num
97         ###########################################
98
99         ###########################################
100         def put_record(self, r_num):
101                 rec = self.rec_dic[r_num]
102                 for k in rec.keys():
103                         #print "%s: %s" % (k, rec[k])
104                         self.out_stream.write("%s: %s\n" % (k, rec[k]))
105                 #print "---"
106                 self.out_stream.write("---\n")
107         ###########################################
108
109         ###########################################
110         def print_usage(self, opt):
111                 #print opt
112                 bp_dir = os.environ['BP_DIR']
113                 usage_path = bp_dir + os.path.sep + "bp_usage" + os.path.sep + "lowercase_seq.wiki"
114                 os.system("print_usage -i %s %s" % (usage_path, opt))
115         ###########################################
116
117
118 # main
119
120 lc_seq = Lowercase_seq()
121
122 try:
123         opts, args = getopt.getopt(sys.argv[1:], "I:O:?vx", ["stream_in=", "stream_out=", "help", "verbose", "no_stream"])
124 except getopt.GetoptError, err:
125         # print help information and exit:
126         print str(err) # will print something like "option -a not recognized"
127         lc_seq.print_usage("")
128         sys.exit(2)
129
130
131 if len(opts)==0:
132         lc_seq.print_usage("")
133         sys.exit(1)
134
135 stream_in = ""
136 stream_out = ""
137 verbose = False
138 for o, a in opts:
139         if o in ("-I", "--stream_in"):
140                 stream_in = a
141         elif o in ("-O", "--stream_out"):
142                 stream_out = a
143         elif o == "-?":
144                 lc_seq.print_usage("")
145                 sys.exit(1)
146         elif o == "--help":
147                 lc_seq.print_usage("-?")
148                 sys.exit(1)
149         elif o in ("-v", "--verbose"):
150                 verbose = True
151         #else:
152         #       assert False
153         #       print_usage()
154
155 try:
156         lc_seq.open_streams(stream_in, stream_out)
157 except:
158         sys.stderr.write("%s\n" % ("IOError"))
159         sys.exit(1)
160
161
162 while True:
163         rec = lc_seq.get_record()
164         if rec=='':
165                 break
166         rec_num = lc_seq.process_record(rec)
167         lc_seq.put_record(rec_num)
168
169 lc_seq.close_streams()
170
171 #source = "Dmel_tRNAs_key_record_tuples.txt"
172 #lc_seq.open_stream(source)
173
174 #lc_seq.get_record()
175 #lc_seq.put_record()
176
177
178