-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
70 lines (57 loc) · 2.66 KB
/
Copy pathscript.py
File metadata and controls
70 lines (57 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from Bio import SeqIO
def load_genbank_file(file_path):
try:
with open(file_path, 'r') as handle:
return list(SeqIO.parse(handle, "genbank"))
except Exception as e:
print(f"Error loading file {file_path}: {e}")
return []
def calculate_gene_statistics(records):
gene_lengths = []
for record in records:
for feature in record.features:
if feature.type == "gene":
start = int(feature.location.start)
end = int(feature.location.end)
gene_lengths.append(abs(end - start))
gene_count = len(gene_lengths)
avg_length = sum(gene_lengths) / gene_count if gene_count > 0 else 0
return gene_count, avg_length
def extract_polymerase_genes(records):
polymerase_genes = []
keywords = ["RNA polymerase", "DNA polymerase"]
for record in records:
for feature in record.features:
if feature.type == "CDS" and "product" in feature.qualifiers:
product = feature.qualifiers["product"][0].lower()
if any(keyword.lower() in product for keyword in keywords):
polymerase_genes.append({
"location": feature.location,
"product": product,
"sequence": feature.extract(record.seq)
})
return polymerase_genes
def save_polymerase_genes(polymerase_genes, output_file):
with open(output_file, 'w') as f:
for gene in polymerase_genes:
f.write(f"> {gene['product']} | {gene['location']}\n")
f.write(f"{gene['sequence']}\n\n")
def main():
gbff_file = "assembly.gbff"
gbk_file = "assembly.gbk"
records_bakta = load_genbank_file(gbff_file)
records_prokka = load_genbank_file(gbk_file)
bakta_stats = calculate_gene_statistics(records_bakta)
prokka_stats = calculate_gene_statistics(records_prokka)
print("Gene statistics:")
print(f"Bakta - Number of genes: {bakta_stats[0]}, Average length: {bakta_stats[1]:.2f} bp")
print(f"Prokka - Number of genes: {prokka_stats[0]}, Average length: {prokka_stats[1]:.2f} bp")
bakta_polymerases = extract_polymerase_genes(records_bakta)
prokka_polymerases = extract_polymerase_genes(records_prokka)
print(f"Number of polymerase genes in Bakta: {len(bakta_polymerases)}")
print(f"Number of polymerase genes in Prokka: {len(prokka_polymerases)}")
save_polymerase_genes(bakta_polymerases, "bakta_polymerases.txt")
save_polymerase_genes(prokka_polymerases, "prokka_polymerases.txt")
print("Polymerase genes saved to bakta_polymerases.txt and prokka_polymerases.txt")
if __name__ == "__main__":
main()