簡體   English   中英

python 中的子進程循環

[英]Subprocess loop in python

我有一個包含幾百個 .bed 文件的文件夾,我想循環這些文件以提取 fasta 序列。 在終端中,我的命令是:

twoBitToFa -bed=PA2_03_2bit.bed -udcDir=. https://hgdownload.cse.ucsc.edu/goldenpath/hg38/bigZips/hg38.2bit stdout > PA2_03.fa

這適用於單人床文件,但我寧願不這樣做數百次。

我是子流程和 python 的新手,但這似乎是一個選項。 我願意接受其他選擇。

到目前為止我有:


import os
path_of_the_directory= '/home/2bit_L1_beds'
for filename in os.listdir(path_of_the_directory):
    f = os.path.join(path_of_the_directory,filename)
    if os.path.isfile(f):
        print(f)

它輸出目錄中每個文件的路徑。 要添加子流程,我嘗試過:

import subprocess
import sys
import os
from subprocess import Popen, PIPE

path_of_the_directory= '/home/2bit_L1_beds'
for filename in os.listdir(path_of_the_directory):
    f = os.path.join(path_of_the_directory,filename)
    if os.path.isfile(f):
        result = subprocess.run([twoBitToFa -bed=f -udcDir=. https://hgdownload.cse.ucsc.edu/goldenpath/hg38/bigZips/hg38.2bit stdout > f.fa],
                                capture_output=True, text=True)
        print(stdout)
        print(f)

我收到“無效語法”,希望得到一些幫助。 我的目標是為每個 .bed 文件輸入 1.fa 文件 output。

考慮subprocess.Popen (您導入但不使用)並傳遞命令列表和 arguments。實際上,您甚至可以使用cwd將目錄更改為文件路徑以進行相對引用。 下面假定所有以_2bit.bed結尾的文件都將替換為.fa

import os
from subprocess import Popen, PIPE

path_of_the_directory = "/home/mrsmeta/Axiotl/2bit_L1_beds"
url = "https://hgdownload.cse.ucsc.edu/goldenpath/hg38/bigZips/hg38.2bit"

for f in os.listdir(path_of_the_directory):
    if os.path.isfile(f):
        print(f)

        cmd = [
            "twoBitToFa", f"-bed={f}", "-udcDir=.", url, 
            "stdout", ">", f.replace("_2bit.bed", ".fa")
        ]

        result = subprocess.Popen(
            cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=path_of_the_directory
        )

        output, error = result.communicate()
        print(output)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM