-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.jl
202 lines (171 loc) · 5.58 KB
/
Main.jl
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
190
191
192
193
194
195
196
197
198
199
200
201
202
# import packages
import Pkg
setup_environment = true
if setup_environment
Pkg.activate("venv")
Pkg.instantiate()
# run(`conda create -n cutfree python=3.9 conda tensorflow=2.12 transformers=4.18`)
USERNAME = "wrick"
ENV["CONDA_JL_HOME"] = "C:/Users/$USERNAME/anaconda3/envs/cutfree"
Pkg.build("Conda")
# ENV["PYTHON"] = ""
# Pkg.build("PyCall")
end
using ArgParse, PyCall, Conda
Conda.add(["transformers", "tensorflow", "numpy"])
# import julia files
include("./cutfree-algorithms/CutFree.jl")
include("./cutfree-algorithms/CutFreeRL.jl")
# helper functions
"""
parse_commandline()
Description:
Retrieve information from command line.
"""
function parse_commandline()
s = ArgParseSettings()
@add_arg_table s begin
"--starting_oligo"
help = "starting options for oligo"
arg_type = String
default = "NNNNNNNNNNNNNNNNNNNN"
"--restriction_sites"
help = "restriction enzyme sequences to be restricted"
arg_type = String
default = "GGTCTC,GGCCGG"
"--min_blocks"
help = "minimum number of desired blocks"
arg_type = Int64
default = 1
"--increase_diversity"
help = "increase randomer diversity while maintaining degeneracy"
arg_type = Bool
default = false
"--algorithm"
help = "force solve with specific algorithm (CutFree or CutFreeRL)"
arg_type = String
default = ""
end
return parse_args(s)
end
"""
main()
Description:
Run CutFree (or CutFreeRL) algorithm on command line arguments.
"""
function main(;
starting_oligo::String="NNNNNNNNNNNNNNNNNNNN",
restriction_sites::Any=["GGTCTC"],
min_blocks::Int64=1,
increase_diversity::Bool=false,
forced_algorithm::String="auto",
command_line::Bool=true
)
if command_line
@show parsed_args = parse_commandline()
starting_oligo = parsed_args["starting_oligo"]
restriction_sites = split(parsed_args["restriction_sites"], ",")
min_blocks = parsed_args["min_blocks"]
increase_diversity = parsed_args["increase_diversity"]
forced_algorithm = lowercase(parsed_args["algorithm"])
end
if forced_algorithm == "cutfree"
println("Optimizing...")
algorithm_name = "CutFree"
cutfree_output = @timed cutfree(
starting_oligo,
restriction_sites,
min_blocks,
increase_diversity
)
elseif forced_algorithm == "cutfreerl"
println("Optimizing...")
algorithm_name = "CutFreeRL"
cutfree_output = @timed cutfreeRL(
starting_oligo,
restriction_sites,
simulate=simulate_random,
nsims=1000
)
else
oligo_length = length(starting_oligo)
restriction_sites = "GGTCTC"
py"""
import tensorflow as tf
import numpy as np
from transformers import DistilBertTokenizer, TFDistilBertModel, \
AdamWeightDecay
def choose_algorithm(oligo_length, restriction_sites):
model = tf.keras.models.load_model(
"algorithm-classifier/models/AlgorithmClassifier-V2/"
+ "checkpoints/best_model.h5",
custom_objects={
"TFDistilBertModel": TFDistilBertModel,
"AdamWeightDecay": AdamWeightDecay
}
)
model_name = "distilbert-base-uncased"
tokenizer = DistilBertTokenizer.from_pretrained(model_name)
text_attributes = tf.convert_to_tensor(
([oligo_length])
)
tokens = tokenizer.batch_encode_plus(
[restriction_sites],
padding="max_length",
truncation=True,
max_length=100,
return_tensors="tf"
)
input_ids = tokens["input_ids"]
attention_mask = tokens["attention_mask"]
texts = (input_ids, attention_mask, text_attributes)
return np.argmax(model.predict(texts), axis=1)
"""
algorithm_choice = py"choose_algorithm"(
oligo_length,
restriction_sites
)
algorithm_choice = [1]
if (
algorithm_choice[1] == 0
|| min_blocks > 1
|| increase_diversity == true
)
println("Optimizing...")
algorithm_name = "CutFree"
cutfree_output = @timed cutfree(
starting_oligo,
restriction_sites,
min_blocks,
increase_diversity
)
else
println("Optimizing...")
algorithm_name = "CutFreeRL"
cutfree_output = @timed cutfreeRL(
starting_oligo,
restriction_sites,
simulate=simulate_random,
nsims=1000
)
end
end
println("\nAlgorithm: ", algorithm_name)
println("Randomer: ", String(cutfree_output.value))
println("Degeneracy: ", get_degeneracy(String(cutfree_output.value)))
println("Time: ", cutfree_output.time, " seconds")
return [
algorithm_name,
String(cutfree_output.value),
get_degeneracy(String(cutfree_output.value)),
cutfree_output.time
]
end
# test case
starting_oligo = "NNNNNNNNNNNNNNNNNNNN"
restriction_sites = ["GGTCTC"] # BsaI
main(
starting_oligo=starting_oligo,
restriction_sites=restriction_sites,
command_line=false
)