Imported Upstream version 0.5
[pysam.git] / tests / example.py
index 473158ee0a885ae50c451d05ead03b79b1226e06..a1ca7a019b3df50703d1978d24ec3db89c56d90d 100644 (file)
@@ -1,11 +1,4 @@
-## This script contains some example code 
-## illustrating ways to to use the pysam 
-## interface to samtools.
-##
-## The unit tests in the script pysam_test.py
-## contain more examples.
-##
-
+import sys
 import pysam
 
 samfile = pysam.Samfile( "ex1.bam", "rb" )
@@ -41,8 +34,20 @@ print "########## pileup with callback ################"
 def my_pileup_callback( column ): print str(column)
 samfile.pileup( region="chr1:10-200", callback=my_pileup_callback )
 
+print "##########iterator row #################"
+iter = pysam.IteratorRow( samfile, 0, 10, 200)
+for x in iter: print str(x)
+
+print "##########iterator col #################"
+iter = pysam.IteratorColumn( samfile, 0, 10, 200 )
+for x in iter: print str(x)
+
+print "#########row all##################"
+iter = pysam.IteratorRowAll( samfile )
+for x in iter: print str(x)
 
-print "########### Using a callback object ###### "
+
+print "###################"
 
 class Counter:
     mCounts = 0
@@ -50,24 +55,61 @@ class Counter:
         self.mCounts += 1
 
 c = Counter()
-samfile.fetch( region = "chr1:10-200", callback = c )
+samfile.fetch( "chr1:10-200", c )
 print "counts=", c.mCounts
 
-print "########### Calling a samtools command line function ############"
+sys.exit(0)
+print samfile.getTarget( 0 )
+print samfile.getTarget( 1 )
 
-for p in pysam.mpileup( "-c", "ex1.bam" ):
+for p in pysam.pileup( "-c", "ex1.bam" ):
     print str(p)
 
-print pysam.mpileup.getMessages()
+print pysam.pileup.getMessages()
+
+for p in pysam.pileup( "-c", "ex1.bam", raw=True ):
+    print str(p),
+
+
+
+print "###########################"
+
+samfile = pysam.Samfile( "ex2.sam.gz", "r" )
+
+print "num targets=", samfile.getNumTargets()
+
+iter = pysam.IteratorRowAll( samfile )
+for x in iter: print str(x)
+
+samfile.close()
+
+print "###########################"
+samfile = pysam.Samfile( "ex2.sam.gz", "r" )
+def my_fetch_callback( alignment ):
+    print str(alignment)
+
+try:
+    samfile.fetch( "chr1:10-20", my_fetch_callback )
+except AssertionError:
+    print "caught fetch exception"
+
+samfile.close()
 
-print "########### Investigating headers #######################"
+print "###########################"
+samfile = pysam.Samfile( "ex2.sam.gz", "r" )
+def my_pileup_callback( pileups ):
+    print str(pileups)
+try:
+    samfile.pileup( "chr1:10-20", my_pileup_callback )
+except NotImplementedError:
+    print "caught pileup exception"
 
 # playing arount with headers
 samfile = pysam.Samfile( "ex3.sam", "r" )
-print samfile.references
+print samfile.targets
 print samfile.lengths
 print samfile.text
-print samfile.header
+print samdile.header
 header = samfile.header
 samfile.close()