1 """General mechanisms to access applications in biopython.
2 """
3 import os, sys
4 import StringIO
5 import popen2
6
7 from Bio import File
8
10 """Run an application with the given commandline.
11
12 This expects a pre-built commandline that derives from
13 AbstractCommandline, and returns a ApplicationResult object
14 to get results from a program, along with handles of the
15 standard output and standard error.
16 """
17
18 if sys.platform[:3]=='win':
19
20 r, w, e = popen2.popen3(str(commandline))
21
22 r_out = r.read()
23 e_out = e.read()
24 w.close()
25 r.close()
26 e.close()
27
28
29 error_code = 0
30
31 else:
32 child = popen2.Popen3(str(commandline), 1)
33
34
35
36
37 r = child.fromchild
38 w = child.tochild
39 e = child.childerr
40
41 r_out = r.read()
42 e_out = e.read()
43 w.close()
44 r.close()
45 e.close()
46
47
48 error_code = os.WEXITSTATUS(child.wait())
49
50 return ApplicationResult(commandline, error_code), \
51 File.UndoHandle(StringIO.StringIO(r_out)), \
52 File.UndoHandle(StringIO.StringIO(e_out))
53
55 """Make results of a program available through a standard interface.
56
57 This tries to pick up output information available from the program
58 and make it available programmatically.
59 """
60 - def __init__(self, application_cl, return_code):
77
79 """Retrieve result information for the given output.
80 """
81 return self._results[output_name]
82
84 """Retrieve a list of all available results.
85 """
86 result_names = self._results.keys()
87 result_names.sort()
88 return result_names
89
91 """Generic interface for running applications from biopython.
92
93 This class shouldn't be called directly; it should be subclassed to
94 provide an implementation for a specific application.
95 """
99
101 """Make the commandline with the currently set options.
102 """
103 commandline = "%s " % self.program_name
104 for parameter in self.parameters:
105 if parameter.is_required and not(parameter.is_set):
106 raise ValueError("Parameter %s is not set." % parameter.names)
107 if parameter.is_set:
108 commandline += str(parameter)
109
110 return commandline
111
126
128 """Check whether the given value is valid.
129
130 This uses the passed function 'check_function', which can either
131 return a [0, 1] (bad, good) value or raise an error. Either way
132 this function will raise an error if the value is not valid, or
133 finish silently otherwise.
134 """
135 if check_function is not None:
136 is_good = check_function(value)
137 if is_good in [0, 1]:
138 if not(is_good):
139 raise ValueError(
140 "Invalid parameter value %r for parameter %s" %
141 (value, name))
142
144 """A class to hold information about a parameter for a commandline.
145
146 Do not use this directly, instead use one of the subclasses.
147
148 Attributes:
149
150 o names -- a list of string names by which the parameter can be
151 referenced (ie. ["-a", "--append", "append"]). The first name in
152 the list is considered to be the one that goes on the commandline,
153 for those parameters that print the option. The last name in the list
154 is assumed to be a "human readable" name describing the option in one
155 word.
156
157 o param_type -- a list of string describing the type of parameter,
158 which can help let programs know how to use it. Example descriptions
159 include 'input', 'output', 'file'
160
161 o checker_function -- a reference to a function that will determine
162 if a given value is valid for this parameter. This function can either
163 raise an error when given a bad value, or return a [0, 1] decision on
164 whether the value is correct.
165
166 o description -- a description of the option.
167
168 o is_required -- a flag to indicate if the parameter must be set for
169 the program to be run.
170
171 o is_set -- if the parameter has been set
172
173 o value -- the value of a parameter
174 """
175 - def __init__(self, names = [], types = [], checker_function = None,
176 is_required = 0, description = ""):
177 self.names = names
178 self.param_types = types
179 self.checker_function = checker_function
180 self.description = description
181 self.is_required = is_required
182
183 self.is_set = 0
184 self.value = None
185
187 """Represent an option that can be set for a program.
188
189 This holds UNIXish options like --append=yes and -a yes
190 """
192 """Return the value of this option for the commandline.
193 """
194
195 if self.names[0].find("--") >= 0:
196 output = "%s" % self.names[0]
197 if self.value is not None:
198 output += "=%s " % self.value
199 else:
200 output += " "
201
202 elif self.names[0].find("-") >= 0:
203 output = "%s " % self.names[0]
204 if self.value is not None:
205 output += "%s " % self.value
206 else:
207 raise ValueError("Unrecognized option type: %s" % self.names[0])
208
209 return output
210
212 """Represent an argument on a commandline.
213 """
215 if self.value is not None:
216 return "%s " % self.value
217 else:
218 return " "
219