annotate manipulate/extend_linearcode/lc_extend.py @ 1:0a5e0df17054 draft default tip

Uploaded
author chrisb
date Fri, 06 May 2016 08:05:48 -0400
parents 89592faa2875
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
89592faa2875 Uploaded
chrisb
parents:
diff changeset
1 __license__ = "MIT"
89592faa2875 Uploaded
chrisb
parents:
diff changeset
2 __version = "0.3"
89592faa2875 Uploaded
chrisb
parents:
diff changeset
3
89592faa2875 Uploaded
chrisb
parents:
diff changeset
4 def splitkeepsep(s, sep): # seems great but adds element to an awkward spot!
89592faa2875 Uploaded
chrisb
parents:
diff changeset
5 """items and keep the separator i.e. a,b,c becomes ['a,', 'b,', 'c']"""
89592faa2875 Uploaded
chrisb
parents:
diff changeset
6 import re
89592faa2875 Uploaded
chrisb
parents:
diff changeset
7
89592faa2875 Uploaded
chrisb
parents:
diff changeset
8 return reduce(lambda acc, elem: acc[:-1] + [acc[-1] + elem] if elem == sep else acc + [elem],
89592faa2875 Uploaded
chrisb
parents:
diff changeset
9 re.split("(%s)" % re.escape(sep), s), [])
89592faa2875 Uploaded
chrisb
parents:
diff changeset
10
89592faa2875 Uploaded
chrisb
parents:
diff changeset
11
89592faa2875 Uploaded
chrisb
parents:
diff changeset
12 def create_logger(logfile):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
13 import logging as logging
89592faa2875 Uploaded
chrisb
parents:
diff changeset
14 # logging.basicConfig(filename=logfile, level=logging.DEBUG)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
15 logger = logging.getLogger()
89592faa2875 Uploaded
chrisb
parents:
diff changeset
16 hdlr = logging.FileHandler(logfile)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
17 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
89592faa2875 Uploaded
chrisb
parents:
diff changeset
18 hdlr.setFormatter(formatter)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
19 logger.addHandler(hdlr) # technically calling this multiple times will not add additional loggers
89592faa2875 Uploaded
chrisb
parents:
diff changeset
20 logger.setLevel(logging.DEBUG)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
21
89592faa2875 Uploaded
chrisb
parents:
diff changeset
22
89592faa2875 Uploaded
chrisb
parents:
diff changeset
23 def create_permutations(num_branches, num_additions):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
24 """
89592faa2875 Uploaded
chrisb
parents:
diff changeset
25 # create all permutations of placing num_additions monosaccharides on the end on a glycan with num_branches branch points
89592faa2875 Uploaded
chrisb
parents:
diff changeset
26 """
89592faa2875 Uploaded
chrisb
parents:
diff changeset
27 permute = []
89592faa2875 Uploaded
chrisb
parents:
diff changeset
28 import itertools
89592faa2875 Uploaded
chrisb
parents:
diff changeset
29
89592faa2875 Uploaded
chrisb
parents:
diff changeset
30 a = range(0, num_branches)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
31 if num_additions > 12 or num_additions == 1: # unsupported return None
89592faa2875 Uploaded
chrisb
parents:
diff changeset
32 return []
89592faa2875 Uploaded
chrisb
parents:
diff changeset
33 elif num_additions == 2:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
34 for r in itertools.product(a, a):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
35 if len(set(r)) == num_additions:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
36 permute.append((r[0], r[1]))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
37 elif num_additions == 3:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
38 for r in itertools.product(a, a, a):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
39 if len(set(r)) == num_additions:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
40 permute.append((r[0], r[1], r[2]))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
41 elif num_additions == 4:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
42 for r in itertools.product(a, a, a, a):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
43 if len(set(r)) == num_additions:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
44 permute.append((r[0], r[1], r[2], r[3]))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
45 elif num_additions == 5:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
46 for r in itertools.product(a, a, a, a, a):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
47 if len(set(r)) == num_additions:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
48 permute.append((r[0], r[1], r[2], r[3], r[4]))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
49 elif num_additions == 6:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
50 for r in itertools.product(a, a, a, a, a, a):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
51 if len(set(r)) == num_additions:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
52 permute.append((r[0], r[1], r[2], r[3], r[4], r[5]))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
53 elif num_additions == 7:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
54 for r in itertools.product(a, a, a, a, a, a, a):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
55 if len(set(r)) == num_additions:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
56 permute.append((r[0], r[1], r[2], r[3], r[4], r[5], r[6]))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
57 elif num_additions == 8:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
58 for r in itertools.product(a, a, a, a, a, a, a, a):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
59 if len(set(r)) == num_additions:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
60 permute.append((r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7]))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
61 elif num_additions == 9:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
62 for r in itertools.product(a, a, a, a, a, a, a, a, a):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
63 if len(set(r)) == num_additions:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
64 permute.append((r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8]))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
65 elif num_additions == 10:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
66 for r in itertools.product(a, a, a, a, a, a, a, a, a, a):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
67 if len(set(r)) == num_additions:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
68 permute.append((r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9]))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
69 elif num_additions == 11:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
70 for r in itertools.product(a, a, a, a, a, a, a, a, a, a, a):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
71 if len(set(r)) == num_additions:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
72 permute.append((r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10]))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
73 elif num_additions == 12:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
74 for r in itertools.product(a, a, a, a, a, a, a, a, a, a, a, a):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
75 if len(set(r)) == num_additions:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
76 permute.append((r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11]))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
77 return permute
89592faa2875 Uploaded
chrisb
parents:
diff changeset
78
89592faa2875 Uploaded
chrisb
parents:
diff changeset
79
89592faa2875 Uploaded
chrisb
parents:
diff changeset
80 def read_linearcode(inputstream, logfile="logging.log", extend=False):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
81 """
89592faa2875 Uploaded
chrisb
parents:
diff changeset
82 read linear code and either append or expand to provide all forms
89592faa2875 Uploaded
chrisb
parents:
diff changeset
83 append mode, appends xylose and comma'd glycans to the root
89592faa2875 Uploaded
chrisb
parents:
diff changeset
84 extend mode create all permutations if the comma'd glycan
89592faa2875 Uploaded
chrisb
parents:
diff changeset
85 more on append: # . read linear code and append comma separated data as a new rooted branch with a xylose tag
89592faa2875 Uploaded
chrisb
parents:
diff changeset
86 # example (A??GN??(A??GN??)Ma3(A??GN??(A??GN??)Ma6)Mb4GNb4(Fa6)GN,F,F,NN,A??GN)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
87 # will become
89592faa2875 Uploaded
chrisb
parents:
diff changeset
88 # (((A??GN??)(F??F??)(NN??)X??)(A??GN??(A??GN??)Ma3(A??GN??(A??GN??)Ma6)Mb4GNb4)(Fa6)GN)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
89 # as all the additional glycans are attached to a xylose at the root position
89592faa2875 Uploaded
chrisb
parents:
diff changeset
90 # but why? analysis tools do not support these unattached glycans and creating all the permutations
89592faa2875 Uploaded
chrisb
parents:
diff changeset
91 # is a large task. Additionally, extend mode produces too many glycans of unknown 'veracity'.
89592faa2875 Uploaded
chrisb
parents:
diff changeset
92 :param inputstream:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
93 :param logfile:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
94 :param extend: boolean flag. default is append (false). True extend and creates all permutes
89592faa2875 Uploaded
chrisb
parents:
diff changeset
95 :return:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
96 """
89592faa2875 Uploaded
chrisb
parents:
diff changeset
97 import logging as logging
89592faa2875 Uploaded
chrisb
parents:
diff changeset
98
89592faa2875 Uploaded
chrisb
parents:
diff changeset
99 create_logger(logfile)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
100 linearcode_list = []
89592faa2875 Uploaded
chrisb
parents:
diff changeset
101 if inputstream is None or inputstream == [] or inputstream == "":
89592faa2875 Uploaded
chrisb
parents:
diff changeset
102 raise IOError("empty input stream")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
103 for line in inputstream:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
104 line = line.strip()
89592faa2875 Uploaded
chrisb
parents:
diff changeset
105 # does this linear code have unassigned glycans on leaves
89592faa2875 Uploaded
chrisb
parents:
diff changeset
106 if "," in line:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
107 # remove brackets over entire linear code
89592faa2875 Uploaded
chrisb
parents:
diff changeset
108
89592faa2875 Uploaded
chrisb
parents:
diff changeset
109 if line[0] == "(" and line[-1] == ")":
89592faa2875 Uploaded
chrisb
parents:
diff changeset
110 lin = line[1:-1]
89592faa2875 Uploaded
chrisb
parents:
diff changeset
111 else:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
112 lin = line
89592faa2875 Uploaded
chrisb
parents:
diff changeset
113 # split by , to get additional glycans
89592faa2875 Uploaded
chrisb
parents:
diff changeset
114 glycan = lin.split(",")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
115 additional_glycans = glycan[1:]
89592faa2875 Uploaded
chrisb
parents:
diff changeset
116 logging.debug('These are the %s additional glycans', additional_glycans)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
117 logging.debug('There are %s additional glycans', len(additional_glycans))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
118
89592faa2875 Uploaded
chrisb
parents:
diff changeset
119 # assume additional glycans of size one need a ?? appended to indicate unknown linkage
89592faa2875 Uploaded
chrisb
parents:
diff changeset
120 for i in range(0, len(additional_glycans)):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
121 additional_glycans[i] += "??"
89592faa2875 Uploaded
chrisb
parents:
diff changeset
122
89592faa2875 Uploaded
chrisb
parents:
diff changeset
123 if extend: # permute
89592faa2875 Uploaded
chrisb
parents:
diff changeset
124 # How many branches
89592faa2875 Uploaded
chrisb
parents:
diff changeset
125 gly = splitkeepsep(glycan[0], "(")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
126 # . Intervention to prevent core fucose glycosylation
89592faa2875 Uploaded
chrisb
parents:
diff changeset
127 if len(gly[-1]) < 7 and gly[-1][0].upper() == "F":
89592faa2875 Uploaded
chrisb
parents:
diff changeset
128 logging.debug('Merging Core Fucose with adjacent branch %s %s %s ', gly[-1], len(gly[-1]),
89592faa2875 Uploaded
chrisb
parents:
diff changeset
129 gly[-1][0].upper()) # initial part + splits
89592faa2875 Uploaded
chrisb
parents:
diff changeset
130 last = gly.pop()
89592faa2875 Uploaded
chrisb
parents:
diff changeset
131 last2 = gly.pop()
89592faa2875 Uploaded
chrisb
parents:
diff changeset
132 gly.append(last2 + last)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
133 logging.debug('There are %s additional leaves', len(gly)) # initial part + splits
89592faa2875 Uploaded
chrisb
parents:
diff changeset
134 logging.debug('These are the %s additional leaves', gly)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
135
89592faa2875 Uploaded
chrisb
parents:
diff changeset
136 # Permutations...
89592faa2875 Uploaded
chrisb
parents:
diff changeset
137 # . Only compute permutations for certain size as not sure how to generate for all sizes
89592faa2875 Uploaded
chrisb
parents:
diff changeset
138 if len(additional_glycans) > len(gly):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
139 # ignoring for now, although this is possible
89592faa2875 Uploaded
chrisb
parents:
diff changeset
140 logging.warning('Glycans> Leaves - Ignored adding permutations')
89592faa2875 Uploaded
chrisb
parents:
diff changeset
141 linearcode_list.append(line)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
142 elif len(additional_glycans) > 12:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
143 logging.warning('Ignored adding permutations, too many additional glys %s', additional_glycans)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
144 linearcode_list.append(line)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
145 elif len(additional_glycans) == 1:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
146 logging.debug('There are %s permutations', len(gly))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
147 for i in range(0, len(gly)):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
148 tempgly = list(gly) # make sure I make a new copy every time
89592faa2875 Uploaded
chrisb
parents:
diff changeset
149 tempgly[i] = additional_glycans[0] + tempgly[i]
89592faa2875 Uploaded
chrisb
parents:
diff changeset
150 linearcode_list.append("".join(tempgly))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
151 logging.debug('This, %s, is one of the permutations', "".join(tempgly))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
152 else:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
153 perms = create_permutations(len(gly), len(additional_glycans))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
154 logging.debug('There are %s permutations', len(perms))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
155 for itm in perms:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
156 tempgly = list(gly) # make sure I make a new copy every time
89592faa2875 Uploaded
chrisb
parents:
diff changeset
157 # .refactor
89592faa2875 Uploaded
chrisb
parents:
diff changeset
158 for idx in range(0, len(additional_glycans)):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
159 tempgly[int(itm[idx])] = additional_glycans[idx] + tempgly[int(itm[idx])]
89592faa2875 Uploaded
chrisb
parents:
diff changeset
160 logging.debug('This, %s, is one of the permutations', "".join(tempgly))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
161 linearcode_list.append("".join(tempgly))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
162
89592faa2875 Uploaded
chrisb
parents:
diff changeset
163 else: # append xylose tag
89592faa2875 Uploaded
chrisb
parents:
diff changeset
164 # create the additional branch and keep things sorted
89592faa2875 Uploaded
chrisb
parents:
diff changeset
165 additional_branch = "("
89592faa2875 Uploaded
chrisb
parents:
diff changeset
166 for subtype in sorted(set(additional_glycans)):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
167 base = "("
89592faa2875 Uploaded
chrisb
parents:
diff changeset
168 for i in range(0, additional_glycans.count(subtype)):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
169 base += subtype
89592faa2875 Uploaded
chrisb
parents:
diff changeset
170 base += ")"
89592faa2875 Uploaded
chrisb
parents:
diff changeset
171 additional_branch += base
89592faa2875 Uploaded
chrisb
parents:
diff changeset
172 additional_branch += "X??)"
89592faa2875 Uploaded
chrisb
parents:
diff changeset
173 logging.debug('These are the %s additional glycans after munging', additional_branch)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
174 # How many branches
89592faa2875 Uploaded
chrisb
parents:
diff changeset
175 gly = splitkeepsep(glycan[0], "(")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
176
89592faa2875 Uploaded
chrisb
parents:
diff changeset
177 # . check if the branch needs brackets
89592faa2875 Uploaded
chrisb
parents:
diff changeset
178 if ")(" not in gly[-2]: # then need to bracket this
89592faa2875 Uploaded
chrisb
parents:
diff changeset
179 gly[-2] = gly[-2][:-1] + ")(" # bracket this
89592faa2875 Uploaded
chrisb
parents:
diff changeset
180 gly[0] = "(" + gly[0]
89592faa2875 Uploaded
chrisb
parents:
diff changeset
181 logging.debug('The glycan with brackets enclosing previous main branch%s', gly)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
182 else:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
183 # .. todo check all others until fails and bracket.
89592faa2875 Uploaded
chrisb
parents:
diff changeset
184 pass
89592faa2875 Uploaded
chrisb
parents:
diff changeset
185
89592faa2875 Uploaded
chrisb
parents:
diff changeset
186 # prepend the additional branch
89592faa2875 Uploaded
chrisb
parents:
diff changeset
187 gly.insert(0, additional_branch)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
188 logging.debug('There are %s additional leaves', len(gly)) # initial part + splits
89592faa2875 Uploaded
chrisb
parents:
diff changeset
189 logging.debug('These are the %s additional leaves', gly)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
190 linearcode_list.append("".join(gly))
89592faa2875 Uploaded
chrisb
parents:
diff changeset
191 else:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
192 if line != "":
89592faa2875 Uploaded
chrisb
parents:
diff changeset
193 linearcode_list.append(line)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
194 return linearcode_list
89592faa2875 Uploaded
chrisb
parents:
diff changeset
195
89592faa2875 Uploaded
chrisb
parents:
diff changeset
196
89592faa2875 Uploaded
chrisb
parents:
diff changeset
197 if __name__ == "__main__":
89592faa2875 Uploaded
chrisb
parents:
diff changeset
198 from optparse import OptionParser
89592faa2875 Uploaded
chrisb
parents:
diff changeset
199
89592faa2875 Uploaded
chrisb
parents:
diff changeset
200 usage = "usage: python %prog [options]\n"
89592faa2875 Uploaded
chrisb
parents:
diff changeset
201 parser = OptionParser(usage=usage)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
202 parser.add_option("-i", action="store", type="string", dest="i", default="input",
89592faa2875 Uploaded
chrisb
parents:
diff changeset
203 help="input glycan linear code ")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
204 parser.add_option("-l", action="store", type="string", dest="l", default="linearcode.log",
89592faa2875 Uploaded
chrisb
parents:
diff changeset
205 help="log file name ")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
206 parser.add_option("-o", action="store", type="string", dest="o", default="output",
89592faa2875 Uploaded
chrisb
parents:
diff changeset
207 help="output file name (linear code) ")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
208 parser.add_option("-e", action="store_true", dest="e", default=False,
89592faa2875 Uploaded
chrisb
parents:
diff changeset
209 help="turn on extender, which creates all possible permutations . Append by default")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
210 (options, args) = parser.parse_args()
89592faa2875 Uploaded
chrisb
parents:
diff changeset
211
89592faa2875 Uploaded
chrisb
parents:
diff changeset
212 try:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
213 instream = file(options.i, 'r')
89592faa2875 Uploaded
chrisb
parents:
diff changeset
214 except Exception as e:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
215 raise IOError(e, "the input file specified does not exist. Use -h flag for help")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
216 print options.e
89592faa2875 Uploaded
chrisb
parents:
diff changeset
217 allpermutes = read_linearcode(instream, options.l, options.e)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
218 try:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
219 out = file(options.o, "w")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
220 except Exception as e:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
221 raise IOError(e, "cannot write to output file. Use -h flag for help")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
222 try:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
223 for item in allpermutes:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
224 out.write(item + "\r\n")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
225 except Exception as e:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
226 raise e
89592faa2875 Uploaded
chrisb
parents:
diff changeset
227 finally:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
228 out.close()