How to use mothur modules in python scripts

I would like to integrate mothur modules in my python scripts. How can I do this? How should I call mothur modules (e.g. sffinfo) in python? Thanks a lot for your help.

You can run mothur like so from the command line/within your scripts…

mothur "#sffinfo(XYZ.sff)"

The keys are the quotes and the #

Thanks, with your help, I found part of the answer.
After importing the os module, I typed:

os.system(“mothur “# sffinfo(sff=xyz.sff)” “)
The mothur command seems to be executed (which is already a progress), but I only see the secondary prompt (”…”) as for multiple-line statements, and nothing else happened
Hm… any idea about what should then be done?

You might be getting issues from not escaping those internal quote marks (also, since python uses # to denote comments, that might be getting in the way). Reading that straight as you’ve written it, I’d imagine it’s being parsed as os.system("mothur " and then everything else is hidden by your #.

If you escape it, it seems to work:

os.system(“mothur “#sffinfo(sff=xyz.sff)””)

If that’s getting messy, a tidier way to do it is also to use the call() function from the subprocess library, which parses a list of strings as a command

from subprocess import call

call([“mothur”, “#sffinfo(sff=xyz.sff)”])

Great, it works! Thanks a lot! :smiley:

What if I have e.g. 356 fastq files in a folder and want to do a batch in Python:

from subprocess import call
for filename in os.listdir(path_to_folder):
call([“mothur”, “#fastq.info(fastq=filename)”])

but it doesn’t work. What I miss to get it work? There’s a way to give ‘filename’ as an input to fastq.info?

Thank you for your help!

Ah, you just need to write the script to use the value of ‘filename’, not the word ‘filename’. Changing your code to something like

import os
from subprocess import call

for filename in os.listdir(path_to_folder):
   call(["mothur", "#fastq.info(fastq=%s)" % filename])

Should work fine.

Thank you it works!! :slight_smile: