[Biopython] samtools does not return correct exit code

Peter Cock p.j.a.cock at googlemail.com
Thu Mar 1 11:00:01 UTC 2012


On Thu, Mar 1, 2012 at 1:40 AM, Mic <mictadlo at gmail.com> wrote:
> Hallo,
> Samtools does not return correct the exit code:
>
> import subprocess
> import logging
> import sys
>
> def run_cmd(args):
>        if subprocess.call(args,shell=True) != 0:
>                print 'hello'
>                logging.error("Error copying sequence file args='%s'" %
> str(args))
>                return 1
>        print 'e', sys.stderr
>        print 'o', sys.stdout
>        return 0
>
>
> def runSamtools( cmd ):
>    '''run a samtools command'''
>
>    try:
>        retcode = subprocess.call(cmd, shell=True)
>        print retcode
>        if retcode < 0:
>            print >>sys.stderr, "Child was terminated by signal", -retcode
>    except OSError, e:
>        print >>sys.stderr, "Execution failed:", e
>
> print run_cmd("samtools faidx ex1.fa")
> print runSamtools("samtools faidx ex1.fa")
>
> print 'Hello still alive'
>
>
> and as output I got:
>
> $ python p3.py
> open: No such file or directory
> [_razf_open] fail to open ex1.fa
> [fai_build] fail to open the FASTA file ex1.fa
> e <open file '<stderr>', mode 'w' at 0x7ffa4658d270>
> o <open file '<stdout>', mode 'w' at 0x7ffa4658d1e0>
> 0
> open: No such file or directory
> [_razf_open] fail to open ex1.fa
> [fai_build] fail to open the FASTA file ex1.fa
> 0
> None
> Hello still alive
>
> How can I get sure that all samtools commands were executed successfully?
>
> Thank you in advance.

Hi Mic,

General Bioinformatics with Python questions are fine on the Biopython
mailing list, but I think this qurey might be better asked elsewhere.

Are you saying the samtools binary returns an error code 0 (success)
even when it fails? If so, that should be raised as a bug with samtools.

Alternatively pysam has support built in for calling the samtools commands.
I'm not sure exactly how that works internally (e.g. via subprocess or by
a C API call), but ask on the pysam mailing list.

Regards,

Peter




More information about the Biopython mailing list