1 """General mechanisms to access applications in biopython.
2 """
3 import os, sys
4 import StringIO
5
6 from Bio import File
7
9 """Run an application with the given commandline.
10
11 This expects a pre-built commandline that derives from
12 AbstractCommandline, and returns a ApplicationResult object
13 to get results from a program, along with handles of the
14 standard output and standard error.
15
16 WARNING - This will read in the full program output into memory!
17 This may be in issue when the program write a large amount of
18 data to standard output.
19 """
20
21
22
23 try :
24 import subprocess, sys
25 child = subprocess.Popen(str(commandline),
26 stdout=subprocess.PIPE,
27 stderr=subprocess.PIPE,
28 shell=(sys.platform!="win32"))
29 r = child.stdout
30 e = child.stderr
31
32 r_out = r.read()
33 e_out = e.read()
34 r.close()
35 e.close()
36
37
38 error_code = child.wait()
39
40 except ImportError :
41
42
43 import popen2
44 if sys.platform[:3]=='win':
45
46 r, w, e = popen2.popen3(str(commandline))
47
48 r_out = r.read()
49 e_out = e.read()
50 w.close()
51 r.close()
52 e.close()
53
54
55 error_code = 0
56
57 else:
58 child = popen2.Popen3(str(commandline), 1)
59
60
61
62
63 r = child.fromchild
64 w = child.tochild
65 e = child.childerr
66
67 r_out = r.read()
68 e_out = e.read()
69 w.close()
70 r.close()
71 e.close()
72
73
74 error_code = os.WEXITSTATUS(child.wait())
75
76 return ApplicationResult(commandline, error_code), \
77 File.UndoHandle(StringIO.StringIO(r_out)), \
78 File.UndoHandle(StringIO.StringIO(e_out))
79
81 """Make results of a program available through a standard interface.
82
83 This tries to pick up output information available from the program
84 and make it available programmatically.
85 """
86 - def __init__(self, application_cl, return_code):
87 """Intialize with the commandline from the program.
88 """
89 self._cl = application_cl
90
91
92 self.return_code = return_code
93
94
95
96 self._results = {}
97
98 for parameter in self._cl.parameters:
99 if "file" in parameter.param_types and \
100 "output" in parameter.param_types:
101 if parameter.is_set:
102 self._results[parameter.names[-1]] = parameter.value
103
105 """Retrieve result information for the given output.
106 """
107 return self._results[output_name]
108
110 """Retrieve a list of all available results.
111 """
112 result_names = self._results.keys()
113 result_names.sort()
114 return result_names
115
117 """Generic interface for running applications from biopython.
118
119 This class shouldn't be called directly; it should be subclassed to
120 provide an implementation for a specific application.
121 """
123 self.program_name = ""
124 self.parameters = []
125
127 """Make the commandline with the currently set options.
128 """
129 commandline = "%s " % self.program_name
130 for parameter in self.parameters:
131 if parameter.is_required and not(parameter.is_set):
132 raise ValueError("Parameter %s is not set." % parameter.names)
133 if parameter.is_set:
134 commandline += str(parameter)
135
136 return commandline
137
152
154 """Check whether the given value is valid.
155
156 This uses the passed function 'check_function', which can either
157 return a [0, 1] (bad, good) value or raise an error. Either way
158 this function will raise an error if the value is not valid, or
159 finish silently otherwise.
160 """
161 if check_function is not None:
162 is_good = check_function(value)
163 if is_good in [0, 1]:
164 if not(is_good):
165 raise ValueError(
166 "Invalid parameter value %r for parameter %s" %
167 (value, name))
168
170 """A class to hold information about a parameter for a commandline.
171
172 Do not use this directly, instead use one of the subclasses.
173
174 Attributes:
175
176 o names -- a list of string names by which the parameter can be
177 referenced (ie. ["-a", "--append", "append"]). The first name in
178 the list is considered to be the one that goes on the commandline,
179 for those parameters that print the option. The last name in the list
180 is assumed to be a "human readable" name describing the option in one
181 word.
182
183 o param_type -- a list of string describing the type of parameter,
184 which can help let programs know how to use it. Example descriptions
185 include 'input', 'output', 'file'
186
187 o checker_function -- a reference to a function that will determine
188 if a given value is valid for this parameter. This function can either
189 raise an error when given a bad value, or return a [0, 1] decision on
190 whether the value is correct.
191
192 o description -- a description of the option.
193
194 o is_required -- a flag to indicate if the parameter must be set for
195 the program to be run.
196
197 o is_set -- if the parameter has been set
198
199 o value -- the value of a parameter
200 """
201 - def __init__(self, names = [], types = [], checker_function = None,
202 is_required = 0, description = ""):
203 self.names = names
204 self.param_types = types
205 self.checker_function = checker_function
206 self.description = description
207 self.is_required = is_required
208
209 self.is_set = 0
210 self.value = None
211
213 """Represent an option that can be set for a program.
214
215 This holds UNIXish options like --append=yes and -a yes
216 """
218 """Return the value of this option for the commandline.
219 """
220
221 if self.names[0].find("--") >= 0:
222 output = "%s" % self.names[0]
223 if self.value is not None:
224 output += "=%s " % self.value
225 else:
226 output += " "
227
228 elif self.names[0].find("-") >= 0:
229 output = "%s " % self.names[0]
230 if self.value is not None:
231 output += "%s " % self.value
232 else:
233 raise ValueError("Unrecognized option type: %s" % self.names[0])
234
235 return output
236
238 """Represent an argument on a commandline.
239 """
241 if self.value is not None:
242 return "%s " % self.value
243 else:
244 return " "
245