0

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!

3
  • Can you post the SyntaxError? The same code (replacing samtools with various bash built-ins) works for me. Commented Mar 9, 2015 at 20:44
  • Sure @jedwards: !samtools view -q 20 -b /home/tmsmith/data/expEvo/RGA/RGAbams/Bais/{samp}.realn.bam | samtools sort - {samp} ^ SyntaxError: invalid syntax Commented Mar 9, 2015 at 20:47
  • I've had it work for other programs in the past, perhaps it is just something specific to this program Commented Mar 9, 2015 at 20:48

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I was just wondering if taking out the ipython dependencies would solve the problem and it appears to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.