Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 1,788 Bytes
89be9f9 |
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 |
import os
import sys
import tensorflow as tf
import pandas as pd
GUIDE_LEN = 23
NUCLEOTIDE_TOKENS = dict(zip(['A', 'C', 'G', 'T'], [0, 1, 2, 3]))
# load model
if os.path.exists('model'):
tiger = tf.keras.models.load_model('model')
else:
print('no saved model!')
exit()
def process_data(x):
x = [item.upper() for item in x]
number_of_input = len(x) - GUIDE_LEN + 1
input_gens = []
for i in range(number_of_input):
input_gens.append("".join(x[i:i + GUIDE_LEN]))
merged_token = []
token_x = [NUCLEOTIDE_TOKENS[item] for item in x]
for i in range(number_of_input):
merged_token.extend(token_x[i:i + GUIDE_LEN])
one_hot_x = tf.one_hot(merged_token, depth=4)
model_input_x = tf.reshape(one_hot_x, [-1, GUIDE_LEN * 4])
return input_gens, model_input_x
def gen_report_table(input_gens, res):
res = res.numpy().flatten().tolist()
# print("ftaltten res: ", res)
data = {"Gene": input_gens, "res": res}
tbl = pd.DataFrame.from_dict(data)
return tbl
def run(x):
input_gens, model_input_x = process_data(x)
# print("input gene: ", input_gens)
# print("model_input: ", model_input_x)
res = tiger.model.predict_step(model_input_x)
# print("res: ", res)
return gen_report_table(input_gens, res)
if __name__ == "__main__":
if len(sys.argv) == 1:
print("you need to specify 23 character gen information")
exit()
x = sys.argv[1]
if len(x) != 23:
print("you need to specify 23 character gen information. You typed %s chars" % len(x))
exit()
elif all([True if item in "ACGT" else False for item in x]):
print("run succesfully: ", run(x))
else:
print("your gen sequence need 23 character only from ACGT")
exit()
|