我有一个python脚本如下:
<?php
$soapClient = new SoapClient("http://168.187.136.18:8090/APIService/PostaWebClient.svc?wsdl");
// Prepare SoapHeader parameters
$sh_param = array(
'Username' => 'username',
'Password' => 'password');
$headers = new SoapHeader('http://soapserver.example.com/webservices', 'UserCredentials', $sh_param);
// Prepare Soap Client
$soapClient->__setSoapHeaders(array($headers));
// Setup the RemoteFunction parameters
$ap_param = array(
'amount' => $irow['total_price']);
// Call RemoteFunction ()
$error = 0;
try {
$info = $soapClient->__call("RemoteFunction", array($ap_param));
} catch (SoapFault $fault) {
$error = 1;
print("
alert('Sorry, blah returned the following ERROR: ".$fault->faultcode."-".$fault->faultstring.". We will now take you back to our home page.');
window.location = 'main.php';
");
}
if ($error == 0) {
$auth_num = $info->RemoteFunctionResult;
if ($auth_num < 0) {
....
// Setup the OtherRemoteFunction() parameters
$at_param = array(
'amount' => $irow['total_price'],
'description' => $description);
// Call OtherRemoteFunction()
$trans = $soapClient->__call("OtherRemoteFunction", array($at_param));
$trans_result = $trans->OtherRemoteFunctionResult;
....
} else {
// Record the transaction error in the database
// Kill the link to Soap
unset($soapClient);
}
}
}
}
?>
我想为同一个输入文件运行上面的脚本,但是对于40个不同的wanted_files - 具有不同的名称--A_ids.txt,B_ids.txt等。 我想各自不同的产出 - A.fasta,B.fasta等。
我是否需要更改我的python脚本,或者我需要创建一个循环来为所有想要的文件运行它?
感谢
答案 0 :(得分: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
。
答案 1 :(得分:0)
考虑让这个程序采用命令行选项。这将允许您从命令行中读取wanted_file
名称作为参数,您可以通过解析给定的参数并遵循模式(例如用.fasta
给出的替换扩展名来推断出相应的输出文件名)或者输出模式是某种形式的另一个命令行参数。
你可以将你的程序称为python my_script.py A_ids.txt
并通过bash循环。您还可以选择允许可变数量的参数,每个参数都会调用给定名称的逻辑。
我对处理命令行参数的偏好是https://docs.python.org/3.3/library/argparse.html和https://docs.python.org/2/library/argparse.html,具体取决于你的python版本。
(另外,如果您采用wanted_file
使用单个命令行参数的路径,则只需通过stdout
或类似函数将内容输出到print
并使用命令行中的重定向操作符将输出发送到提供的文件名:python my_script.py A_ids.txt > A.fasta
)
答案 2 :(得分:0)
我认为更简单的方法是将40个文件名存储在一个文件中(在代码中:wanted_filenames_file
),将它们存储在一个数组(wanted_files
)中并循环每个文件:< / p>
# !/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")