pythonfor-loopipythonmagic-function

replace loop variable in system shell command of ipython


I would like to iterate through a list (of sample names) and repeat the same command for each sample using the system shell command of ipython ("!"). I have done this previously with no problem but keep getting a SyntaxError under this specific code. If you know what is wrong please let me know - thank you!

Here is the sample code (in reality there are more samples):

samples = ["ERR007200", "ERR007204", "ERR007208"]
def remove_ambMap():
    !samtools view -q 20 -b home/pathToFile/{samp}.realn.bam | samtools sort - {samp}
for samp in samples:
    remove_ambMap()

Note that samtools is a program that is in the $PATH and that if I perform the command specifying the path of the file it works - sorry this will not be reproducible as you need to have the program installed and these are massive genomic files - I am hoping someone might be able to spot what is wrong!


Solution

  • For this kind of task you'd use to use the subprocess module; the easiest would be to use the call method with shell=True:

    from subprocess import call
    
    def remove_ambMap(samp):
        call('samtools view -q 20 -b home/pathToFile/{samp}.realn.bam '
             '| samtools sort - {samp}'.format(samp=samp), shell=True)
    
    for samp in samples:
        remove_ambMap(samp)