Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:31:26+00:00 2026-05-28T23:31:26+00:00

Hi I had a question about linking input and output with sub-processes in python.

  • 0

Hi I had a question about linking input and output with sub-processes in python. I am trying to simplify the program by skipping the output of one step by passing it to another subprocess rather than output it to a file. Then open another process to run on that file.

E.g. First process uses SAMTOOLS to output a specific chromosome from a large bam file.
So…
bigfile.bam is read in and outputs chromosome22.bam

The next subprocess uses BEDTOOLS to convert that chromosome22.bam to chromosome22.bed
So…
chromosome22.bam is read in and outputs chromosome22.bed

What I want to do is pass the stdout of the first process into the second so there is no need for the intermediate file.

So far I have this…

for x in 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,'X','Y':
   subprocess.call("%s view -bh %s %s > %s/%s/%s.bam" % (samtools,bam,x,bampath,out,x), shell=True)

This makes the chromosome[1-22,X,Y].bam files. But can I avoid this and put another subprocess command in the same loop to convert them to bed files?

The command for bed conversion is:

bedpath/bedtools bamtobed -i [bamfile] > [bedfile]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:31:26+00:00Added an answer on May 28, 2026 at 11:31 pm

    No need to use python here. Much easier in shell. But essentially, it works the same as in python.

    If bedtools can read from stdin, you can e.g. do

    #!/bin/sh
    for x in `seq 1 22` X Y; do
       $samtools view -bh $bam $x | $bedtools bamtobed > $bampath/$out/$x.bam
    done
    

    Depending on how bedtools was desinged, you might also need to use -i - to have it read from stdin.

    If you stick with python, I strongly recommend about learning how to do this

    1. without doing it in all shell,
    2. without producing shell commands, that you need to escape properly to avoid errors

    subprocess is more safe to use when you use the array-based syntax and no shell.
    Make that two subprocess invocations, one for each command. See http://docs.python.org/library/subprocess.html#replacing-shell-pipeline for more details.

    cmd1 = [samtools, "view", "-bh", bam, x]
    cmd2 = [bedtools, "bamtobed"]
    
    c1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE)
    c2 = subprocess.Popen(cmd2, stdin=c1.stdout, stdout=open(outputfilename, "w"))
    c1.stdout.close()
    c2.communicate()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a question about a program I've been trying to get running. Encrypt
I had a question about boost python. I've been working on exporting some functionality
I am in the process of learning Python and had a question about the
I am learning xslt and had one question about how can i use xslt
I had a question about indices on a table and I put it up
I had a question about Java Class Path variables. If I have multiple jars
I just had a question about aligning two expand-collapse boxes and got an answer
I am new to vimdiff and had a question about outputting the similarities of
I'm playing around with RhinoCommons and NHibernate and I had a question about the
just had a general question about how to approach a certain problem I'm facing.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.