0

I am using the python script that takes two input files (goodProteins.fasta and list.txt) and save result in gene.fasta output files.

fasta_file = "goodProteins.fasta" # First input 
wanted_file = "list.txt" # Second input
result_file = "result.txt" # 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")

But now I have several list files (list1.txt, list100.txt etc) that are present in the subdirectory 'outfile' within the current directory. I want to take each list file one at a time, proceed through the script and generates the corresponding output files (gene1.fasta, gene100.fasta) and saved them in a separate sub directory 'result'.

Any help??

4

1 回答 1

0

这相当简单

def process(wanted_file, result_file):
    fasta_file = "goodProteins.fasta" # First input 

    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")

for i in range(100):
    wanted_file = "list" + str(i) + ".txt"
    result_file = "gene" + str(i) + ".txt"
    process(wanted_file, result_file)
于 2013-06-16T21:00:22.573 回答