1 """General mechanisms to access applications in biopython.
2 """
3 import os
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 child = popen2.Popen3(str(commandline), 1)
19
20
21
22
23 r = child.fromchild
24 w = child.tochild
25 e = child.childerr
26
27 r_out = r.read()
28 e_out = e.read()
29 w.close()
30 r.close()
31 e.close()
32
33
34 error_code = os.WEXITSTATUS(child.wait())
35
36 return ApplicationResult(commandline, error_code), \
37 File.UndoHandle(StringIO.StringIO(r_out)), \
38 File.UndoHandle(StringIO.StringIO(e_out))
39
41 """Make results of a program available through a standard interface.
42
43 This tries to pick up output information available from the program
44 and make it available programmatically.
45 """
46 - def __init__(self, application_cl, return_code):
63
65 """Retrieve result information for the given output.
66 """
67 return self._results[output_name]
68
70 """Retrieve a list of all available results.
71 """
72 result_names = self._results.keys()
73 result_names.sort()
74 return result_names
75
77 """Generic interface for running applications from biopython.
78
79 This class shouldn't be called directly; it should be subclassed to
80 provide an implementation for a specific application.
81 """
85
87 """Make the commandline with the currently set options.
88 """
89 commandline = "%s " % self.program_name
90 for parameter in self.parameters:
91 if parameter.is_required and not(parameter.is_set):
92 raise ValueError("Parameter %s is not set." % parameter.names)
93 if parameter.is_set:
94 commandline += str(parameter)
95
96 return commandline
97
112
114 """Check whether the given value is valid.
115
116 This uses the passed function 'check_function', which can either
117 return a [0, 1] (bad, good) value or raise an error. Either way
118 this function will raise an error if the value is not valid, or
119 finish silently otherwise.
120 """
121 if check_function is not None:
122 is_good = check_function(value)
123 if is_good in [0, 1]:
124 if not(is_good):
125 raise ValueError(
126 "Invalid parameter value %r for parameter %s" %
127 (value, name))
128
130 """A class to hold information about a parameter for a commandline.
131
132 Do not use this directly, instead use one of the subclasses.
133
134 Attributes:
135
136 o names -- a list of string names by which the parameter can be
137 referenced (ie. ["-a", "--append", "append"]). The first name in
138 the list is considered to be the one that goes on the commandline,
139 for those parameters that print the option. The last name in the list
140 is assumed to be a "human readable" name describing the option in one
141 word.
142
143 o param_type -- a list of string describing the type of parameter,
144 which can help let programs know how to use it. Example descriptions
145 include 'input', 'output', 'file'
146
147 o checker_function -- a reference to a function that will determine
148 if a given value is valid for this parameter. This function can either
149 raise an error when given a bad value, or return a [0, 1] decision on
150 whether the value is correct.
151
152 o description -- a description of the option.
153
154 o is_required -- a flag to indicate if the parameter must be set for
155 the program to be run.
156
157 o is_set -- if the parameter has been set
158
159 o value -- the value of a parameter
160 """
161 - def __init__(self, names = [], types = [], checker_function = None,
162 is_required = 0, description = ""):
163 self.names = names
164 self.param_types = types
165 self.checker_function = checker_function
166 self.description = description
167 self.is_required = is_required
168
169 self.is_set = 0
170 self.value = None
171
173 """Represent an option that can be set for a program.
174
175 This holds UNIXish options like --append=yes and -a yes
176 """
178 """Return the value of this option for the commandline.
179 """
180
181 if self.names[0].find("--") >= 0:
182 output = "%s" % self.names[0]
183 if self.value is not None:
184 output += "=%s " % self.value
185 else:
186 output += " "
187
188 elif self.names[0].find("-") >= 0:
189 output = "%s " % self.names[0]
190 if self.value is not None:
191 output += "%s " % self.value
192 else:
193 raise ValueError("Unrecognized option type: %s" % self.names[0])
194
195 return output
196
198 """Represent an argument on a commandline.
199 """
201 if self.value is not None:
202 return "%s " % self.value
203 else:
204 return " "
205