1

Im trying to create a simple unix style pipe for chaining the output of one program to the input of another. I have it working on my Mac but when I try it on a linux server (CentOS) it fails, as the external programs are unable to understand the - for some reason. I don't understand how this works on my Mac and not on a linux platform. My code so far is:

from subprocess import Popen, PIPE

# Commands to run
p1 = "samtools view -h small.bam".split()
p2 = "samtools view -hb -".split()  # The '-' seems to be the problem
p3 = "samtools view -hb -o fini.bam -".split()

process_list = [p1, p2, p3]


class Pipe_manager(object):
    """Takes a list of commands to run and pipes their in/out together."""
    def __init__(self, process_list):
        self.process_list = process_list
        self.open_processes = []
        self.output = 0
        print 'Starting processes'
        self.__open_procs()

    def __open_procs(self):
        for i in range(len(self.process_list)):
            if i == 0:
                self.open_processes.append(Popen(self.process_list[i], stdout=PIPE))
            else:
                old_pipe = self.open_processes[i-1]
                self.open_processes.append(Popen(self.process_list[i], stdin=old_pipe.stdout, stdout=PIPE))
        self.__close_procs()

    def __close_procs(self):
        for j in range(len(self.open_processes)-1):
            self.open_processes[j].stdout.close()
        self.__get_output()

    def __get_output(self):
        self.output = self.open_processes[-1].communicate()[0]


P = Pipe_manager(process_list)
if P.output:
    print P.output

The error messages are a bit program specific but may be of help here:

[main_samview] fail to read the header from "-".

What can I do to make unix work in this case?

kezzos
  • 3,023
  • 3
  • 20
  • 37
  • Ahh, it doesnt work in bash on unix either. BUT it does work in bash on my Mac. So is this a problem with the program itself, or is this some problem with bashims? – kezzos Jul 22 '15 at 11:30
  • Sorry see updated comment. The versions are the same using 'which', but the system could be defaulting to another samtools version on the system. Ill check – kezzos Jul 22 '15 at 11:34
  • Thanks you've directed me to the problem! Sorry to waste your time. It was a case of the wrong version being used by bash. Thanks for the help – kezzos Jul 22 '15 at 11:39
  • unrelated: your code can be more readable if you [use `shell=True` or `plumbum`](http://stackoverflow.com/a/16709666/4279) – jfs Jul 22 '15 at 20:58

1 Answers1

4

Your Python code looks reasonable. I would check to make sure that your program actually understands - to mean "standard input". This is simply a convention that many programs follow, but it is not required by any specifications.

I would manually test your pipeline in a Bash session on your CentOS machine to see if the same problem occurs. CentOS (RHEL, actually) is notoriously "slow" at updating packages, so you may have a newer version on your Mac which does understand -.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328