2016-11-11 120 views
0

我有一個python腳本如下:如何運行python腳本作爲不同文件的循環?

#!/usr/bin/python 
from Bio import SeqIO 

fasta_file = "input.fa" # Input fasta file 
wanted_file = "A_ids.txt" # Input interesting sequence IDs, one per line 
result_file = "A.fasta" # Output fasta file 

wanted = set() 
with open(wanted_file) as f: 
    for line in f: 
     line = line.strip() 
     if line != "": 
      wanted.add(line) 

fasta_sequences = SeqIO.parse(open(fasta_file),'fasta') 
with open(result_file, "w") as f: 
    for seq in fasta_sequences: 
     if seq.id in wanted: 
      SeqIO.write([seq], f, "fasta") 

我想運行上面相同的輸入文件中的腳本,但對於40個不同wanted_files - 使用不同的名稱 - A_ids.txt,B_ids.txt等。 我想有他們各自不同的輸出 - A.fasta,B.fasta等

我需要更改我的python腳本或我需要創建一個循環來運行它爲我所有想要的文件?

謝謝

+1

你可以當然要列出所有輸入文件和相應的輸出並循環播放。如果這是一個你期望重複的過程,可能需要改變你的腳本從文件中讀取文件名,然後編寫一個函數來構造輸出文件名。 – hoyland

回答

3

我@BlackVegetable同意。將它設置爲使用命令行參數,通過做這樣的事情:

#!/usr/bin/python 
from Bio import SeqIO 

import sys # for sys.argv 

fasta_file = sys.argv[1] # This is now going to be name.fa, the fasta file 
wanted_file = sys.argv[2] # This is now going to be name_ids.txt, or whatever you passed 
# as an argument 
result_file = sys.argv[3] # Output fasta file, now passed as arg 

wanted = set() 
with open(wanted_file) as f: 
    for line in f: 
     line = line.strip() 
     if line != "": 
      wanted.add(line) 

fasta_sequences = SeqIO.parse(open(fasta_file),'fasta') 
with open(result_file, "w") as f: 
    for seq in fasta_sequences: 
     if seq.id in wanted: 
      SeqIO.write([seq], f, "fasta") 

然後,你可以調用該程序python input.fa A_ids.txt A.fasta,你的情況。或者,python inputB.fa B_ids.txt B.fasta

+0

這比argparse更輕量級,可能更適合OP。另外,你提供了一個有用的具體應用程序。 +1 – BlackVegetable

+0

謝謝@BlackVegetable我的道歉偷榮耀XD – SolarPixelGaming

+0

但在這種情況下,我需要運行指令40次...謝謝! – Paul

0

考慮讓這個程序採取命令行選項。這將允許用戶從命令行作爲參數讀取wanted_file名稱,你可以通過解析給定的參數和以下的圖案(如取代.fasta給定延伸),或具有的輸出模式是另一命令推斷適當的輸出文件名有些種類的在線爭論。

你可以通過慶典打電話給你的程序作爲python my_script.py A_ids.txt和遍歷這一點。您也可以選擇允許可變數量的參數,每個參數都會調用您的邏輯來獲取給定的名稱。

根據您的python版本,我喜歡處理命令行參數是https://docs.python.org/3.3/library/argparse.htmlhttps://docs.python.org/2/library/argparse.html

(此外,如果採取使用單個命令行參數爲wanted_file的路徑,則可以簡單地輸出經由print或類似的功能的內容,以stdout和使用重定向操作者在命令行輸出發送到文件名設置有:python my_script.py A_ids.txt > A.fasta

0

我想更簡單的方法可以在一個文件中存儲的40名(代碼:wanted_filenames_file),將它們存儲在陣列(wanted_files)和環路沿文件中的每一個:

# !/usr/bin/python 
from Bio import SeqIO 

fasta_file = "input.fa" # Input fasta file 
wanted_filenames_file = "filenames.txt" 
with open(wanted_filenames_file) as f: 
    wanted_files = f.readlines() 
result_file = [] # Output fasta file 
for wanted_file in wanted_files: 
    wanted = set() 
    with open(wanted_file) as f: 
     for line in f: 
      line = line.strip() 
      if line != "": 
       wanted.add(line) 

    fasta_sequences = SeqIO.parse(open(fasta_file), 'fasta') 
    result_file = wanted_file.replace("_ids.txt", ".fasta") 
    with open(result_file, "w") as f: 
     for seq in fasta_sequences: 
      if seq.id in wanted: 
       SeqIO.write([seq], f, "fasta") 
+0

沒有工作。我創建了一個文件,其中包含我想要的所有文件名(文件名.txt)。我得到了一些錯誤 - 來自:看不懂的/ var /郵件/生物 ./modified.py:4號線:fasta_file:找不到命令 ./modified.py:5號線:wanted_filenames_file:找不到命令 ./ modified.py:第6行:意外標記附近的語法錯誤'(' ./modified。py:第6行:'打開(wanted_filenames_file)爲f:' – Paul

+0

@Clarissa是同時引發的三個錯誤?我想我沒有明確解釋。您必須創建一個名爲'filenames.txt'的文件,您應該在其中存儲所有文件名,每行一個。 –

+0

是的,我做到了。我創建了一個名爲filenames.txt的文件,其中包含所有文件名,每行一個。 – Paul

相關問題