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 
  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 child = popen2.Popen3(str(commandline), 1) 19 # get information and close the files, so if we call this function 20 # repeatedly we won't end up with too many open files 21 22 # here are the file descriptors 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 # capture error code 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
40 -class ApplicationResult:
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):
47 """Intialize with the commandline from the program. 48 """ 49 self._cl = application_cl 50 51 # provide the return code of the application 52 self.return_code = return_code 53 54 # get the application dependent results we can provide 55 # right now the only results we handle are output files 56 self._results = {} 57 58 for parameter in self._cl.parameters: 59 if "file" in parameter.param_types and \ 60 "output" in parameter.param_types: 61 if parameter.is_set: 62 self._results[parameter.names[-1]] = parameter.value
63
64 - def get_result(self, output_name):
65 """Retrieve result information for the given output. 66 """ 67 return self._results[output_name]
68
69 - def available_results(self):
70 """Retrieve a list of all available results. 71 """ 72 result_names = self._results.keys() 73 result_names.sort() 74 return result_names
75
76 -class AbstractCommandline:
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 """
82 - def __init__(self):
83 self.program_name = "" 84 self.parameters = []
85
86 - def __str__(self):
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
98 - def set_parameter(self, name, value = None):
99 """Set a commandline option for a program. 100 """ 101 set_option = 0 102 for parameter in self.parameters: 103 if name in parameter.names: 104 if value is not None: 105 self._check_value(value, name, parameter.checker_function) 106 parameter.value = value 107 parameter.is_set = 1 108 set_option = 1 109 110 if set_option == 0: 111 raise ValueError("Option name %s was not found." % name)
112
113 - def _check_value(self, value, name, check_function):
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]: # if we are dealing with a good/bad check 124 if not(is_good): 125 raise ValueError( 126 "Invalid parameter value %r for parameter %s" % 127 (value, name))
128
129 -class _AbstractParameter:
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
172 -class _Option(_AbstractParameter):
173 """Represent an option that can be set for a program. 174 175 This holds UNIXish options like --append=yes and -a yes 176 """
177 - def __str__(self):
178 """Return the value of this option for the commandline. 179 """ 180 # first deal with long options 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 # now short options 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
197 -class _Argument(_AbstractParameter):
198 """Represent an argument on a commandline. 199 """
200 - def __str__(self):
201 if self.value is not None: 202 return "%s " % self.value 203 else: 204 return " "
205