]> git.donarmstrong.com Git - biopieces.git/blob - code_python/Cjung/lowercase_seq
fixed seq qual length check
[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_wiki -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         if sys.stdin.isatty():
133                 lc_seq.print_usage("")
134                 sys.exit(1)
135
136 stream_in = ""
137 stream_out = ""
138 verbose = False
139 for o, a in opts:
140         if o in ("-I", "--stream_in"):
141                 stream_in = a
142         elif o in ("-O", "--stream_out"):
143                 stream_out = a
144         elif o == "-?":
145                 lc_seq.print_usage("")
146                 sys.exit(1)
147         elif o == "--help":
148                 lc_seq.print_usage("-?")
149                 sys.exit(1)
150         elif o in ("-v", "--verbose"):
151                 verbose = True
152
153 try:
154         lc_seq.open_streams(stream_in, stream_out)
155 except:
156         sys.stderr.write("%s\n" % ("IOError"))
157         sys.exit(1)
158
159
160 while True:
161         rec = lc_seq.get_record()
162         if rec=='':
163                 break
164         rec_num = lc_seq.process_record(rec)
165         lc_seq.put_record(rec_num)
166
167 lc_seq.close_streams()
168
169