Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subprocess.run and error codes #31

Open
srobb1 opened this issue Oct 19, 2019 · 0 comments
Open

subprocess.run and error codes #31

srobb1 opened this issue Oct 19, 2019 · 0 comments

Comments

@srobb1
Copy link
Collaborator

srobb1 commented Oct 19, 2019

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'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant