# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + id="amended-rebecca" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1615263714426, "user_tz": 300, "elapsed": 32626, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgMwPlnKw7I-jN5XkbRlGeMro_PH58tNaE2oWC0dw=s64", "userId": "16675707467486783174"}} outputId="e14a78ff-63c0-4ccb-fc52-285a36e19ba8"
import pandas as pd
#Google colab does not have pickle
try:
  import pickle5 as pickle
except:
  # !pip install pickle5
  import pickle5 as pickle
import os
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Dense, Input, GlobalMaxPooling1D
from keras.layers import Conv1D, MaxPooling1D, Embedding
from keras.models import Model
from numpy import random
from keras.layers import LSTM, Bidirectional, GlobalMaxPool1D, Dropout
from keras.optimizers import Adam
from sklearn.metrics import roc_auc_score,confusion_matrix, roc_curve, auc

from google.colab import drive
drive.mount('/content/drive')


# + id="NwqgVumzaN5G" executionInfo={"status": "ok", "timestamp": 1615263714428, "user_tz": 300, "elapsed": 32618, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgMwPlnKw7I-jN5XkbRlGeMro_PH58tNaE2oWC0dw=s64", "userId": "16675707467486783174"}}

def combine_AC(df,chunksize=3,seperate_chunks=False):
  if not seperate_chunks:
    df.Human = df.Human.apply(lambda x: [''.join(x)[i:i+chunksize] for i in range(0, len(''.join(x))) if len(''.join(x)[i:i+chunksize])>=chunksize])
    df.Yersinia = df.Yersinia.apply(lambda x: [''.join(x)[i:i+chunksize] for i in range(0, len(''.join(x))) if len(''.join(x)[i:i+chunksize])>=chunksize])
    df.Joined = [df.loc[row]['Human']+df.loc[row]['Yersinia'] for row in range(df.shape[0])]
    return df
  #print("JHGVBJGHGHKHGKG")
  df.Human = df.Human.apply(lambda x: [''.join(x)[i:i+chunksize] for i in range(0, len(''.join(x)), chunksize)])
  df.Yersinia = df.Yersinia.apply(lambda x: [''.join(x)[i:i+chunksize] for i in range(0, len(''.join(x)), chunksize)])
  df.Joined = [df.loc[row]['Human']+df.loc[row]['Yersinia'] for row in range(df.shape[0])]
  return df

def shuff_together(df1,df2):
    joined = pd.concat([df1,df2], axis=0)
    joined = joined.iloc[np.random.permutation(len(joined))].reset_index(drop=True)
    return joined.iloc[:df1.shape[0],:],joined.iloc[df1.shape[0]:,:].reset_index(drop=True)
def load_data(D=1,randomize=False):
    try:
        with open('/content/drive/MyDrive/ML_Data/df_train_'+str(D)+'D.pickle', 'rb') as handle:
            df_train = pickle.load(handle)
    except:
        df_train = pd.read_pickle("C:/Users/nik00/py/proj/hyppi-train.pkl")
    try:
        with open('/content/drive/MyDrive/ML_Data/df_test_'+str(D)+'D.pickle', 'rb') as handle:
            df_test = pickle.load(handle)
    except:
        df_test = pd.read_pickle("C:/Users/nik00/py/proj/hyppi-independent.pkl")
    if randomize:
        return shuff_together(df_train,df_test)
    else:
        return df_train,df_test

def get_seq_data_join(MAX_VOCAB_SIZE, MAX_SEQUENCE_LENGTH):
  print("MAX_VOCAB_SIZE is",MAX_VOCAB_SIZE)
  print("MAX_SEQUENCE_LENGTH is",MAX_SEQUENCE_LENGTH)
  sentences = pd.DataFrame(' '.join(df_train.loc[i]['Joined']) for i in range(df_train.shape[0])).values.flatten()
  tokenizer = Tokenizer(num_words=MAX_VOCAB_SIZE)
  tokenizer.fit_on_texts(sentences)
  sequences = tokenizer.texts_to_sequences(sentences)
  print("max sequence_data length:", max(len(s) for s in sequences))
  print("min sequence_data length:", min(len(s) for s in sequences))
  s = sorted(len(s) for s in sequences)
  print("median sequence_data length:", s[len(s) // 2])

  print("max word index:", max(max(seq) for seq in sequences if len(seq) > 0))
  word2idx = tokenizer.word_index
  print('Found %s unique tokens.' % len(word2idx))
  data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)
  print('Shape of data tensor:', data.shape)
  sentences_test = pd.DataFrame(' '.join(df_test.loc[i]['Joined']) for i in range(df_test.shape[0])).values.flatten()
  sequences_test = tokenizer.texts_to_sequences(sentences_test)
  print("max sequences_test length:", max(len(s) for s in sequences_test))
  print("min sequences_test length:", min(len(s) for s in sequences_test))
  s = sorted(len(s) for s in sequences_test)
  print("median sequences_test length:", s[len(s) // 2])
  data_test = pad_sequences(sequences_test, maxlen=MAX_SEQUENCE_LENGTH)
  print('Shape of data_test tensor:', data_test.shape)
  num_words = min(MAX_VOCAB_SIZE, len(word2idx) + 1)
  print("num_words is",num_words)
  return data,data_test,num_words,MAX_SEQUENCE_LENGTH,MAX_VOCAB_SIZE

def embedding_layer(num_words,MAX_SEQUENCE_LENGTH,EMBEDDING_DIM):
  embedding_matrix = random.uniform(-1, 1,(num_words,EMBEDDING_DIM))
  embedding_layer = Embedding(
  num_words,
  EMBEDDING_DIM,
  weights=[embedding_matrix],
  input_length=MAX_SEQUENCE_LENGTH,
  trainable=True)
  return embedding_layer


# + colab={"base_uri": "https://localhost:8080/", "height": 432} id="1mBLRJo2I_Qt" executionInfo={"status": "ok", "timestamp": 1615263717418, "user_tz": 300, "elapsed": 35602, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgMwPlnKw7I-jN5XkbRlGeMro_PH58tNaE2oWC0dw=s64", "userId": "16675707467486783174"}} outputId="42a270e2-9c87-471b-f722-57703acf8ab3"
df_train,df_test = load_data(6)
# combine_AC(df_train,5)
# combine_AC(df_test,5)
# with open('/content/drive/MyDrive/ML_Data/df_train_6D.pickle', 'wb') as handle:
#     pickle.dump(df_train, handle, protocol=pickle.HIGHEST_PROTOCOL)

# with open('/content/drive/MyDrive/ML_Data/df_train_6D.pickle', 'rb') as handle:
#     b = pickle.load(handle)
print('The data used will be:')
df_train[['Joined']]

# + colab={"base_uri": "https://localhost:8080/", "height": 298} id="3nm8SQKlyE4c" executionInfo={"status": "ok", "timestamp": 1615263717792, "user_tz": 300, "elapsed": 35968, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgMwPlnKw7I-jN5XkbRlGeMro_PH58tNaE2oWC0dw=s64", "userId": "16675707467486783174"}} outputId="00a4b7ea-4363-46ad-90ac-bdcca8cb3384"
lengths = sorted(len(s) for s in df_train['Joined'])
print("Median length of Joined sequence is",lengths[len(lengths)//2])
_ = plt.hist(lengths)
_=plt.title("Most Joined sequences seem to be less than 2000 in length")

# + colab={"base_uri": "https://localhost:8080/"} id="D0dW_CIiyE7e" executionInfo={"status": "ok", "timestamp": 1615263738027, "user_tz": 300, "elapsed": 56196, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgMwPlnKw7I-jN5XkbRlGeMro_PH58tNaE2oWC0dw=s64", "userId": "16675707467486783174"}} outputId="9e5ae84e-a73c-4e2b-b925-d6a2946e16b8"
data_6D_join,data_test_6D_join,num_words_6D,MAX_SEQUENCE_LENGTH_6D,MAX_VOCAB_SIZE_6D = get_seq_data_join(MAX_SEQUENCE_LENGTH = 1000,MAX_VOCAB_SIZE = 1000000)

# + id="c9rm20Osrc4e" executionInfo={"status": "ok", "timestamp": 1615263741868, "user_tz": 300, "elapsed": 60030, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgMwPlnKw7I-jN5XkbRlGeMro_PH58tNaE2oWC0dw=s64", "userId": "16675707467486783174"}}
embedding_layer_6D_join = embedding_layer(num_words_6D,MAX_SEQUENCE_LENGTH_6D,EMBEDDING_DIM = 400)
BATCH_SIZE = 128
EPOCHS = 20
M_6D=100

# + colab={"base_uri": "https://localhost:8080/"} id="0HbWAiTSr0zT" executionInfo={"status": "ok", "timestamp": 1615266069068, "user_tz": 300, "elapsed": 2387223, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgMwPlnKw7I-jN5XkbRlGeMro_PH58tNaE2oWC0dw=s64", "userId": "16675707467486783174"}} outputId="ba78c1c6-d1eb-44c4-f47b-73ebfdcd5082"

print('Building model...')

ip = Input(shape=(MAX_SEQUENCE_LENGTH_6D,),name = 'model6D_join_ip')
x = embedding_layer_6D_join(ip)
x = Bidirectional(LSTM(M_6D, return_sequences=True),name = 'model6D_join_Bidirectional')(x)
x = Dropout(0.2)(x)
x = Dense(128, activation='relu')(x)
x = GlobalMaxPool1D()(x)
x = Dense(128, activation='relu')(x)
x = Dropout(0.2)(x)
output = Dense(1, activation="sigmoid")(x)
model6D_join = Model(ip, output)

model6D_join.compile(
  loss='binary_crossentropy',
  optimizer=Adam(lr=0.01),
  metrics=['accuracy'],
)


print('Training model...')
jk = model6D_join.fit(
  data_6D_join,
  df_train['label'].values,
  batch_size=BATCH_SIZE,
  epochs=EPOCHS,
  #validation_split=VALIDATION_SPLIT
)


# + colab={"base_uri": "https://localhost:8080/"} id="kD0R5zhCr03b" executionInfo={"status": "ok", "timestamp": 1615266071616, "user_tz": 300, "elapsed": 2389764, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgMwPlnKw7I-jN5XkbRlGeMro_PH58tNaE2oWC0dw=s64", "userId": "16675707467486783174"}} outputId="dc7acb2d-185f-4fec-8158-749d7a6c908b"
yhat =  model6D_join.predict(data_test_6D_join)
y = df_test['label'].values
fpr, tpr, thresholds = roc_curve(y, yhat)
auc = auc(fpr, tpr)
auc

# + id="Jl4CMV_apACW" executionInfo={"status": "ok", "timestamp": 1615266127287, "user_tz": 300, "elapsed": 45166, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgMwPlnKw7I-jN5XkbRlGeMro_PH58tNaE2oWC0dw=s64", "userId": "16675707467486783174"}}
#model6D_join.save('/content/drive/MyDrive/ML_Data/model6D_join.h5')

StarcoderdataJupyterScriptsDedupFiltered