Package Bio :: Package Application
[hide private]
[frames] | no frames]

Source Code for Package Bio.Application

  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   
9 -def generic_run(commandline):
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 # print str(commandline) 18 if sys.platform[:3]=='win': 19 # Windows does not have popen2.Popen3 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 # No way to get the error code; setting it to a dummy variable 29 error_code = 0 30 31 else: 32 child = popen2.Popen3(str(commandline), 1) 33 # get information and close the files, so if we call this function 34 # repeatedly we won't end up with too many open files 35 36 # here are the file descriptors 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 # capture error code 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
54 -class ApplicationResult:
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):
61 """Intialize with the commandline from the program. 62 """ 63 self._cl = application_cl 64 65 # provide the return code of the application 66 self.return_code = return_code 67 68 # get the application dependent results we can provide 69 # right now the only results we handle are output files 70 self._results = {} 71 72 for parameter in self._cl.parameters: 73 if "file" in parameter.param_types and \ 74 "output" in parameter.param_types: 75 if parameter.is_set: 76 self._results[parameter.names[-1]] = parameter.value
77
78 - def get_result(self, output_name):
79 """Retrieve result information for the given output. 80 """ 81 return self._results[output_name]
82
83 - def available_results(self):
84 """Retrieve a list of all available results. 85 """ 86 result_names = self._results.keys() 87 result_names.sort() 88 return result_names
89
90 -class AbstractCommandline:
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 """
96 - def __init__(self):
97 self.program_name = "" 98 self.parameters = []
99
100 - def __str__(self):
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
112 - def set_parameter(self, name, value = None):
113 """Set a commandline option for a program. 114 """ 115 set_option = 0 116 for parameter in self.parameters: 117 if name in parameter.names: 118 if value is not None: 119 self._check_value(value, name, parameter.checker_function) 120 parameter.value = value 121 parameter.is_set = 1 122 set_option = 1 123 124 if set_option == 0: 125 raise ValueError("Option name %s was not found." % name)
126
127 - def _check_value(self, value, name, check_function):
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]: # if we are dealing with a good/bad check 138 if not(is_good): 139 raise ValueError( 140 "Invalid parameter value %r for parameter %s" % 141 (value, name))
142
143 -class _AbstractParameter:
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
186 -class _Option(_AbstractParameter):
187 """Represent an option that can be set for a program. 188 189 This holds UNIXish options like --append=yes and -a yes 190 """
191 - def __str__(self):
192 """Return the value of this option for the commandline. 193 """ 194 # first deal with long options 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 # now short options 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
211 -class _Argument(_AbstractParameter):
212 """Represent an argument on a commandline. 213 """
214 - def __str__(self):
215 if self.value is not None: 216 return "%s " % self.value 217 else: 218 return " "
219