Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MBender
    Junior Member
    • Jan 2011
    • 9

    #1

    parallel-python and pysam

    Hi next-gen sequencing community,

    as you all know, the analysis of next-generation sequencing data can be very time consuming. Therefore, i'd like to integrate parallelization into my python scripts. What i use is the python-pp package which makes it very convenient to put independent tasks into different processes which can be run in parallel. On the other hand i use the pysam package which gives me the desired functionality to access, and manipulate sequencing data. Unfortunately, it turned out that python-pp and pysam are not the best friends, and thus trouble was bound to occur.

    A small example what can go wrong:
    If you want to sort a set bam files within a python script in parallel, the following code should be sufficient:

    Code:
    import pp
    import pysam
    
    def sortBam(bam_filepath, out_prefix):
      pysam.sort(bam_filepath, out_prefix)
    
    job_server = pp.Server(ncpus=2, ppservers = (), secret='secret')
    
    job1 = job_server.submit(sortBam, ('<in1.bam>', '<out_prefix1>'), modules=('pysam',))
    job2 = job_server.submit(sortBam, ('<in2.bam>', '<out_prefix2>'), modules=('pysam',))
    
    result1 = job1()
    result1 = job2()
    Unfortunately, i got the following error message:

    An error has occured during the function execution
    Traceback (most recent call last):
    File "/home/mbieg/.local/lib/python2.6/site-packages/ppworker.py", line 90, in run
    __result = __f(*__args)
    File "<string>", line 2, in sortBam
    File "/home/mbieg/project/tools/python_packages/lib/python2.6/site-packages/pysam-0.6-py2.6-linux-x86_64.egg/pysam/__init__.py", line 50, in __call__
    retval, stderr, stdout = csamtools._samtools_dispatch( self.dispatch, args )
    File "csamtools.pyx", line 2860, in csamtools._samtools_dispatch (pysam/csamtools.c:26160)
    AttributeError: StringIO instance has no attribute 'fileno'

    Does anybody have an idea how to fix this?

    Best,

    Matthias
  • rflrob
    Member
    • May 2010
    • 50

    #2
    I'm not sure exactly what the issue is there (I'm assuming it works when you take the parallelization out), but you might try checking out GNU parallel: http://www.gnu.org/software/parallel/
    From the command line, you could do:

    $ parallel 'samtools sort {} {.}_sorted' ::: in1.bam in2.bam in3.bam ... inN.bam

    It should also be possible to do this call from within a python script using, e.g., subprocess.Popen().

    Comment

    • MBender
      Junior Member
      • Jan 2011
      • 9

      #3
      Hi rflrob,

      thanks for your reply. First of all, the example i posted is just an example. I want to integrate the features of pysam into a larger software project with much more complicated analyses. Therefore, i need native parallelization in python, combined with the features of pysam. Second, the python packages thread, or threading are not suitable for me, since they are limited by the global interpreter lock of python, which means, even if i fork some threads there will be no real parallelization. The python-pp package circumvents the GIL and thus seems to be a good choice. The problem is, that it seems that python-pp has some problems with pysam (especially the samtools function calls like pysam.sort, or pysam.index, etc.).

      I would be really glad, if somebody could tell me about his/her experience with pysam and python-pp.

      Best,

      Matthias

      Comment

      • rflrob
        Member
        • May 2010
        • 50

        #4
        Hi Matthias,

        If I had to guess, I'd say what pp is doing is turning all file buffers into StringIO buffers, which is making pysam unhappy. Are you locked into pp? Using the multiprocessing module (which also gets around the GIL) seems to work fine for me:

        Code:
        import multiprocessing as mp
        import pysam
        
        def sortBam(args):
            pysam.sort(args[0], args[1])
        
        pool = mp.Pool()
        pool.map(sortBam, [('<in1.bam>', '<sorted_prefix1>'),
                           ('<in2.bam>', '<sorted_prefix2>')])

        EDIT: Looking more at the code of pysam and pp, it actually looks like they're both competing for access to stdout or stderr. in ppworker.py, pp sets stdout and stderr to StringIOs, whereas I'd bet that pysam is expecting them to be bona fide filedescriptors.
        Last edited by rflrob; 10-18-2012, 12:12 PM.

        Comment

        Latest Articles

        Collapse

        • SEQadmin2
          Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
          by SEQadmin2



          Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

          Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
          ...
          10-14-2026, 09:55 AM
        • SEQadmin2
          New Genomics Technologies Take Aim at Long-Standing Limits
          by SEQadmin2


          Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

          We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
          ...
          09-28-2026, 10:25 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by SEQadmin2, 10-22-2026, 11:13 AM
        0 responses
        23 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-22-2026, 10:48 AM
        0 responses
        20 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-15-2026, 10:52 AM
        0 responses
        36 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-14-2026, 05:39 AM
        0 responses
        42 views
        0 reactions
        Last Post SEQadmin2  
        Working...