-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcodonusage.py
More file actions
189 lines (159 loc) · 5.65 KB
/
codonusage.py
File metadata and controls
189 lines (159 loc) · 5.65 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python
import argparse
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import generic_dna
from CAI import RSCU
from dnachisel import (
DnaOptimizationProblem,
random_protein_sequence,
reverse_translate,
CodonOptimize,
EnforceTranslation,
AvoidPattern,
AvoidChanges,
EnforceSequence,
EnforceGCContent,
)
ap = argparse.ArgumentParser()
ap.add_argument("-e", "--highexp", required=False,
help="path to the input fa file", type=str)
ap.add_argument("-i", "--inputfasta", required=True,
help="path to the gene to be optimized (fasta file)", type=str)
ap.add_argument("-t", "--taxid", required=False,
help="Host organism NCBI Taxonomic ID", type=str)
ap.add_argument("-c", "--genetic_code", required=False,
help="Genetic code # for organism", type=int, default=11)
ap.add_argument("-r", "--report", required=False,
help="Path for output report", type=str)
ap.add_argument("-o", "--output", required=True,
help="Path for fasta ouput", type=str)
ap.add_argument("-m", "--method", required=False,
help="Method for codon optimization", type=str, default="match_codon_usage")
ap.add_argument("-p", "--protein",
help="Flag if protein input", action='store_true')
args = vars(ap.parse_args())
input_path = args["highexp"]
fasta_path = args["inputfasta"]
taxid = args["taxid"]
genetic_code = args["genetic_code"]
report_path = args["report"]
output_path = args["output"]
protein_flag = args["protein"]
## Codon table
#``{'*': {"TGA": 0.112, "TAA": 0.68}, 'K': ...}``
codon_table_11={"*": {"TGA":1.0, "TAA":1.0, "TAG":1.0},
"T": {"ACA":0, "ACC":0, "ACG":0, "ACT":0},
"R": {"AGA":0, "AGG":0, "CGA":0, "CGC":0, "CGG":0, "CGT":0},
"A": {"GCA":0, "GCC":0, "GCG":0, "GCT":0},
"C": {"TGC":0, "TGT":0},
"D": {"GAC":0, "GAT":0},
"E": {"GAA":0, "GAG":0},
"F": {"TTC":0, "TTT":0},
"G": {"GGA":0, "GGC":0, "GGG":0, "GGT":0},
"H": {"CAC":0, "CAT":0},
"I": {"ATA":0, "ATC":0, "ATT":0},
"K": {"AAA":0, "AAG":0},
"L": {"CTA":0, "CTC":0, "CTG":0, "CTT":0, "TTA":0, "TTG":0},
"M": {"ATG":1.0},
"N": {"AAC":0, "AAT":0},
"P": {"CCA":0, "CCC":0, "CCG":0, "CCT":0},
"Q": {"CAA":0, "CAG":0},
"S": {"AGC":0, "AGT":0, "TCA":0, "TCC":0, "TCG":0, "TCT":0},
"V": {"GTA":0, "GTC":0, "GTG":0, "GTT":0},
"W": {"TGG":0},
"Y": {"TAC":0, "TAT":0}}
if genetic_code == 11:
codon_table = codon_table_11
else:
print("\ngenetic codes other than 11 (Bacterial, Archaeal) not supported")
#Import target gene
#To do: refactor to process multiple input sequences
gene_object = SeqIO.parse(fasta_path, "fasta")
for dna_seq in gene_object:
dna_id = dna_seq.id
print("\nImporting target gene " + dna_id)
dna = str(dna_seq.seq)
gene = dna
if protein_flag:
gene = reverse_translate(gene)
if input_path and taxid:
print("\ngene list and taxonomic ID both provided. Defaulting to gene list")
#Import gene list for RSCU calculation
if input_path:
print("\nImporting genes for RSCU calculation")
seq_list = []
counter = 0
n_count = 0
seq_object = SeqIO.parse(input_path, "fasta")
for seqs in seq_object:
seq_id = seqs.id
seq = str(seqs.seq)
seq_list.append(seq)
counter += 1
nn = len(seq)
n_count += nn
print("\n" + str(counter) + " genes imported containing " + str(n_count) + " nucleotides")
print("\nCalculating RSCU for imported genes\n")
try:
RSCU_list = RSCU(seq_list)
except:
print("\nEXCEPTION: RSCU could not be caluclated for imported genes")
print("\nParsing RSCU for codon optimization")
#To do: Create RSCU parser function
for k, v in codon_table_11.items():
for k2, v2 in v.items():
if k2 in RSCU_list:
codon_table_11[k][k2] = RSCU_list[k2]
print("\nOptimizing codons for input gene list")
#Read gene fasta sequence and initiate optimizer
problem = DnaOptimizationProblem(
sequence=gene,
constraints=[
EnforceTranslation(),
AvoidPattern("BsmBI_site", "BamHI"),
EnforceTranslation(),
EnforceGCContent(mini=0.35, maxi=0.65, window=50), #TWIST: 25% and 65% GC
],
objectives=[CodonOptimize(codon_usage_table=codon_table_11)],
)
if taxid and not input_path:
print("\nOptimizing codons for taxonomic ID: " + taxid)
#Read gene fasta sequence and initiate optimizer
if not protein_flag:
problem = DnaOptimizationProblem(
sequence=gene,
constraints=[
#EnforceSequence(sequence = "ATG", location=(0, 2)),
AvoidChanges(location=(0, 2)),
AvoidPattern("BsmBI_site", "BamHI"),
EnforceTranslation(),
EnforceGCContent(mini=0.35, maxi=0.65, window=50), #TWIST: 25% and 65% GC
],
objectives=[CodonOptimize(species=taxid)],
)
#Output and reporting
print("\nBefore optimization:")
print(problem.constraints_text_summary())
print(problem.objectives_text_summary())
problem.resolve_constraints(final_check=True)
if report_path:
print("\nSaving report " + report_path)
problem.optimize_with_report(target=report_path)
else:
problem.optimize()
print("\nAfter optimization:")
print(problem.constraints_text_summary())
print(problem.objectives_text_summary())
print("\nWriting optimized sequence as fasta: " + output_path)
dna_id_new=dna_id+"_opt"
dna_seq_new=Seq(problem.sequence, generic_dna)
records = (SeqRecord(dna_seq_new, id=dna_id_new, description=""))
with open(output_path, "w") as output_handle:
SeqIO.write(records, output_handle, "fasta")
protein_seq_new=dna_seq_new.translate(table=11, cds=True)
protein_records=(SeqRecord(protein_seq_new, id=dna_id_new, description=""))
protein_path=output_path.split(".")[0]+".faa"
with open(protein_path, "w") as output_handle:
SeqIO.write(protein_records, output_handle, "fasta")