Skip to content

subprocess.run and error codes #31

@srobb1

Description

@srobb1

https://github.com/prog4biol/evop2019/blob/master/EVOP_Python_2019.md/#use-error-code-to-control-pipeline

Use Error Code to Control Pipeline
If the error code is good (0) then we can proceed, if the error code is bad (!=0) stop.

#!/usr/bin/env python3
import subprocess
import sys

blastcmd = "blastx -query test.query -db ~/dbs/uniprot_sprot.fasta -outfmt 7 -out test.blastout.tab -evalue 1e-5"
countcmd = 'grep \'hits found\' test.blastout.tab  | perl -ne \'m/(\d+)/; $count=$1; print $count,"\n"\''

blastcmd_run = subprocess.run(blastcmd, shell=True , stdout = subprocess.PIPE, stderr=subprocess.PIPE)
if blastcmd_run.returncode == 0:
  # run count cmd if blast exit code is 0  
  countcmd_run = subprocess.run(countcmd,  shell=True , stdout = subprocess.PIPE, stderr=subprocess.PIPE)
else:
   sys.exit("BLAST had issues " + blastcmd_run.stderr.decode('utf-8'))


if countcmd_run.returncode == 0:
  if int(countcmd_run.stdout.decode('utf-8')) > 0:
    # parse results
    print("We will put parsing code here")
  else:
    sys.exit("no hits")
else:
  sys.exit("count had issues " + countcmd_run.stderr.decode('utf-8'))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions