首页 > 代码库 > using samtools commands within python

using samtools commands within python

Commands available in csamtools are available as simple function calls. For example:

pysam.sort( "ex1.bam", "output" )

corresponds to the command line:

samtools sort ex1.bam output

Command line options can be provided as arguments:

pysam.sort( "-n", "ex1.bam", "output" )

or:

pysam.sort( "-m", "1000000", "ex1.bam", "output" )

In order to get usage information, try:

print pysam.sort.usage()

Argument errors raise a pysam.SamtoolsError:

pysam.sort()Traceback (most recent call last):File "x.py", line 12, in <module>  pysam.sort()File "/home/andreas/pysam/build/lib.linux-x86_64-2.6/pysam/__init__.py", line 37, in __call__  if retval: raise SamtoolsError( "\n".join( stderr ) )pysam.SamtoolsError: ‘Usage: samtools sort [-n] [-m <maxMem>] <in.bam> <out.prefix>\n‘

Messages from csamtools on stderr are captured and are available using the getMessages() method:

pysam.sort.getMessage()

Note that only the output from the last invocation of a command is stored.