f=open('D:/QLNU/writing/homework/BI/2022-2023第二学期作业/20230607/hORFeome_V8.1.fasta','r+') f_out=open('D:/QLNU/writing/homework/BI/2022-2023第二学期作业/20230607/out.CSV','w+') global codon_counts codon_counts = {} # DNA序列 lines=f.readlines() if lines[0]=='>': pass else: dna_sequence = lines # 将序列分割成三个一组的密码子 codons = [dna_sequence[i:i+3] for i in range(0, len(dna_sequence), 3)] # 统计每个密码子出现的次数 for codon in codons: if codon in codon_counts: codon_counts[codon] += 1 else: codon_counts[codon] = 1 # 计算每个密码子的使用频率 total_codons = len(codons) codon_frequencies = {} for codon in codon_counts: codon_frequencies[codon] = codon_counts[codon] / total_codons # 输出结果 f.write(codon_frequencies)优化这段代码
时间: 2024-03-24 07:36:46 浏览: 293
Sure, I can help you optimize the code. Here are some suggestions:
1. Use "with open" to handle file I/O instead of "open" and "close". This ensures that the file is properly closed even if an exception is raised.
2. Use a dictionary comprehension to count the occurrences of each codon.
3. Use a generator expression to calculate the total number of codons.
4. Use the csv module to write the results to the output file.
Here's the optimized code:
```python
import csv
codon_counts = {}
with open('D:/QLNU/writing/homework/BI/2022-2023第二学期作业/20230607/hORFeome_V8.1.fasta', 'r') as f:
dna_sequence = f.read().replace('\n', '')
# count the occurrences of each codon
codon_counts = {dna_sequence[i:i+3]: codon_counts.get(dna_sequence[i:i+3], 0) + 1 for i in range(0, len(dna_sequence), 3)}
# calculate the total number of codons
total_codons = sum(codon_counts.values())
codon_frequencies = {}
for codon in codon_counts:
codon_frequencies[codon] = codon_counts[codon] / total_codons
# write the results to the output file
with open('D:/QLNU/writing/homework/BI/2022-2023第二学期作业/20230607/out.CSV', 'w', newline='') as f_out:
writer = csv.writer(f_out)
writer.writerow(['Codon', 'Frequency'])
for codon in codon_frequencies:
writer.writerow([codon, codon_frequencies[codon]])
```
This code reads in the DNA sequence from the input file, counts the occurrences of each codon using a dictionary comprehension, calculates the total number of codons using a generator expression, calculates the frequency of each codon, and writes the results to the output file using the csv module.
阅读全文