def upload(self, tree): """ Uploads a tree to iTOL. Examples -------- :: import dendropy from dendropy.utility import itol tree = dendropy.Tree.get(data="[&R] (A1,(B2,(C3,(D4,E5))));", schema="newick") itol_service = ItolService() tree_id = itol_service.upload(tree) print(tree_id) """ import requests with tempfile.TemporaryDirectory() as temp_dirname: zf_path = os.path.join(temp_dirname, "data.zip") tree_str = tree.as_string(schema="newick") zf = zipfile.ZipFile(zf_path, mode="w", compression=COMPRESSION_TYPE) # zf = zipfile.ZipFile(zf_path, mode="w") with zf: zf.writestr( "tree.tree", tree_str, ) with open(zf_path, "rb") as zf: files = {"zipFile": open(zf_path, "rb")} response = urlio.post_request(url=self._base_upload_url, files=files) response_text = response.text if "SUCCESS" not in response_text: raise error.ExternalServiceError( service_name="iTOL", invocation_command=self._base_upload_url, service_input="", returncode=-1, stdout=response_text, stderr="") response_lines = [row for row in response_text.split("\n") if row] warnings = response_lines[0:-1] tree_id = response_lines[-1].split()[1] return tree_id
def call( paup_commands, suppress_standard_preamble=False, ignore_error_returncode=False, ignore_nonempty_stderr=False, strip_extraneous_prompts_from_stdout=True, strip_extraneous_prompts_from_stderr=True, cwd=None, env=None, paup_path=PAUP_PATH ): """ Executes a sequence of commands in PAUP* and returns the results. Parameters ---------- paup_commands : iterable of strings A list or some other iterable of strings representing PAUP commands. suppress_standard_preamble : bool If |True|, then the command sequence will not be prefaced by the standard preamble. ignore_error_returncode : bool If |True|, then a non-0 return code from the PAUP process will not result in an exception being raised. ignore_nonempty_stderr : bool If |True|, then the PAUP process writing to standard error will not result in an exception being raised. strip_extraneous_prompts_from_stdout : bool If |True|, then all occurrences of 'paup>' will be removed from the standard output contents. strip_extraneous_prompts_from_stderr : bool If |True|, then all occurrences of 'paup>' will be removed from the standard error contents. cwd : string Set the working directory of the PAUP* process to this directory. env : dictionary Environmental variables to set for the PAUP* process. paup_path : string Path to the PAUP* executable. Returns ------- returncode : exit value of PAUP process. stdout : string Contents of the PAUP process standard output. stderr : string Contents of the PAUP process standard error. """ if textprocessing.is_str_type(paup_commands): commands = [paup_commands] else: commands = list(paup_commands) if not suppress_standard_preamble: commands.insert(0, STANDARD_PREAMBLE) commands.append("quit") paup_block = ";\n".join(commands) + ";\n" invocation_command = [paup_path, "-n", "-u"] p = subprocess.Popen( invocation_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, env=env, ) raw_stdout, raw_stderr = processio.communicate(p, paup_block) stdout = raw_stdout stderr = raw_stderr if strip_extraneous_prompts_from_stdout: # weird dev/paup error ... lots or prompts spring up stdout = stdout.replace("paup>", "") if strip_extraneous_prompts_from_stderr: # weird dev/paup error ... lots or prompts spring up stderr = stderr.replace("paup>", "") chk_stderr = stderr else: chk_stderr = stderr.replace("paup>", "") if (p.returncode != 0 and not ignore_error_returncode) or (chk_stderr != "" and not ignore_nonempty_stderr): raise error.ExternalServiceError( service_name="PAUP*", invocation_command=invocation_command, service_input=paup_block, returncode = p.returncode, stdout=raw_stdout, stderr=raw_stderr) return p.returncode, stdout, stderr
def call(r_commands, ignore_error_returncode=False, cwd=None, env=None, rscript_path=RSCRIPT_EXECUTABLE, ): """ Executes a sequence of commands in R and returns the results. All the noise is sunk into the stderr return variable, and just the output comes out cleanly in the stdout return variable. Parameters ---------- r_commands : iterable of strings A list or some other iterable of strings of R commands. ignore_error_returncode : bool If |True|, then a non-0 return code from the R process will not result in an exception being raised. cwd : string Set the working directory of the R process to this directory. env : dictionary Environmental variables to set for the R process. rscript_path : string Path to the Rscript executable. Returns ------- returncode : exit value of the R process stdout : string Contents of the R process standard output. stderr : string Contents of the R process standard error. Examples -------- Build up a script (``s``) to calculate a range of values, print them to the standard output, and then post-process this to extract the values:: import itertools from dendropy.interop import rstats bb = [0.01, 0.05, 0.10, 0.50, 1.0] cc = [0.01, 0.05, 0.10, 0.50, 1.0] ee = [0.0, 0.1, 0.2] # store commands of script as a list # to be passed to the ``call()`` s = [] # set options, load required libraries, etc. s.append("options(digits=22)") s.append("library(PBD)") # build up list of commands in script params = [] for b, c, e in itertools.product(bb, cc, ee): s.append("print(pbd_durspec_mean(pars=c({},{},{})))".format(b, c, e)) # execute script returncode, stdout, stderr = rstats.call(s) # peek at the results print(stdout) # [1] 69.31472 # [1] 9.853723 # [1] 4.981369 # [1] 0.9950331 # ... # post-process the stdout to extract values results = [float(x.split(" ")[1]) for x in stdout.split("\n") if x] Notes ----- Note that newlines ('\n') and other special characters will be converted before being passed to the R interpreter, so need to be escaped or entered as raw string expressions. That is, instead of, e.g.:: returncode, stdout, stderr = RService.call([ "cat('hello, world\n')", ]) use this:: returncode, stdout, stderr = RService.call([ "cat('hello, world\\n')", ]) or:: returncode, stdout, stderr = RService.call([ r"cat('hello, world\n')", ]) """ if not textprocessing.is_str_type(r_commands): r_commands = "\n".join(r_commands) r_commands += "\n" invocation_command = [RSCRIPT_EXECUTABLE, rsubprocess_pipe_path] p = subprocess.Popen( invocation_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, env=env, ) stdout, stderr = processio.communicate(p, r_commands) if (p.returncode != 0 and not ignore_error_returncode): raise error.ExternalServiceError( service_name="Rscript", invocation_command=invocation_command, service_input=r_commands, returncode = p.returncode, stdout=stdout, stderr=stderr) return p.returncode, stdout, stderr