1
$\begingroup$

I am very new to using python3 and am hoping to use it to reduce the time I need to spend running Infernal to look for a variety of RNAs in a large collection of bacterial genomes. I am hoping to write a short module in python that will let me sequentially run infernal using a single covariance model on a list of genome files.

At the moment I am trying to use subprocess.run() or subprocess.Popen() to do so.


import subprocess
import os 
import glob

genomes = list(glob.glob(os.path.join("/path/to/genomes/", "*.fa")))

for x in files:
    subprocess.Popen(args=["cmsearch", "/path/to/covariancemodel/", "genomes"], shell=False)

my issue is I do not know a way to have "genomes" passed as the components of the list within the command.

Is this a way off approach to trying to automate this? If so is there a better way to do it or is there a way to simply pass each file in the list to the cmsearch command?

$\endgroup$
3
  • $\begingroup$ what delimiter does cmsearch expect? is the list separated with spaces, commas etc? 1 2 3 or 1,2,3 or 1, 2, 3 or something else? $\endgroup$
    – Bioathlete
    Feb 5, 2020 at 22:39
  • $\begingroup$ The output of genomes = list(glob.glob(os.path.join("/path/to/genomes/", "*.fa"))) is comma delimted eg. /path/genome1.fa, /path/genome2.fa, ... For cmsearch it normal just takes a single file so you would use the command: cmsearch /path/to/covariancemodel.cm /path/to/genomefile.fa What I am hoping to do is replace that single genomefile.fa with each of the genome files in the genomes list. I hope that is clear! Thanks for the help. $\endgroup$
    – DavidM
    Feb 5, 2020 at 23:01
  • $\begingroup$ You might be interested in a tool like snakemake. $\endgroup$
    – bli
    Feb 10, 2020 at 13:13

1 Answer 1

1
$\begingroup$

I think what you want to do is:

import subprocess
import os 
import glob

genomes = glob.glob(os.path.join("/path/to/genomes/", "*.fa"))

for gfile in genomes:
    subprocess.Popen(args=["cmsearch", "/path/to/covariancemodel/", gfile], shell=False)

each pass through the for loop will take a one of the genome files and pass it into the cmsearch command.

Note:

1) glob.glob returns a list so no need to cast it again - https://docs.python.org/2/library/glob.html

$\endgroup$
2
  • $\begingroup$ Thank you very much for the response! I ended up solving it a slightly different way with for x in genomes: subprocess.Popen(args=["cmsearch", "/path/to/covariancemodel/", "%s" %x], shell=False) but I just gave your recommendation and it works great and cleans things up a bit! $\endgroup$
    – DavidM
    Feb 6, 2020 at 4:21
  • $\begingroup$ ok you shouldn't need "%s" %x since the values within the genomes list are already string. Also str(variable) it the preferred way to convert and variable to a string $\endgroup$
    – Bioathlete
    Feb 6, 2020 at 16:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.