1 Introduction

Generative modeling for protein engineering is key to solving fundamental problems in synthetic biology, medicine, and material science. Machine learning has enabled us to generate useful protein sequences on a variety of scales. Generative models are machine learning methods which seek to model the distribution underlying the data, allowing for the generation of novel samples with similar properties to those on which the model was trained. Generative models of proteins can learn biologically meaningful representations helpful for a variety of downstream tasks. Furthermore, they can learn to generate protein sequences that have not been observed before and to assign higher probability to protein sequences that satisfy desired criteria. In this package, common deep generative models for protein sequences, such as variational autoencoder (VAE), generative adversarial networks (GAN), and autoregressive models are available. In the VAE and GAN, the Word2vec is used for embedding. The transformer encoder is applied to protein sequences for the autoregressive model.

The first step in molecular machine learning is to convert the molecular data into a numerical format suitable for the machine learning models. As an example of a raw representation is the sparse or one-hot encoding. Here, each amino acid is encoded as a vector, where a bit is set according to an index using the known amino acids. The naive way of representing a word in vector form is one hot representation but it is a very ineffective way for representing a huge corpus. In a more effective way, we need some semantic similarities to nearby points, thus creating the representation bring beneficial information about the word actual meaning, called word embedding models. In natural language processing, often large, one-hot encoded vectors are transformed into smaller dimension vectors, by finding word relations based on proximity or positions of similar words using the contexts of nearby neighbors. The Word2vec is one of the most common word embedding models that gives low-dimensional dense vector (word embedding) of the term (words and phrases). The Word2vec has also been applied to amino acids as a basis for traditional machine learning methods.

The generative adversarial network (GAN) is able to mimic any distribution of data in any domain containing images, music, speech, and prose. The GAN is an example of a network that use unsupervised learning to train two models in parallel. The network is forced to efficiently represent the training data, making it more effective at generating data similar to the training data. The GAN is made up of a discriminator and a generator that compete in a two-player minimax game. The generator generates a simulated example given as its input from a specified distribution. The objective of the generator is to produce an output that is so close to real that it confuses the discriminator in being able to differentiate the fake data from the real data. Both the generated examples and authentic ones are fed to the discriminator network, whose job is to distinguish between the fake and the real data. Generative adversarial nets can be extended to a conditional model if both the generator and discriminator are conditioned on some extra information, such as class labels or data from other views. The conditional GAN (CGAN) is based on vanilla GAN with additional conditional input to generator and discriminator. This extracts features based on the modelling of the conditional input. The auxiliary classifier GAN (ACGAN) is an extension of CGAN that adds conditional input only to the generator.

The autoencoder is the unsupervised neural network approach to learn how to encode data efficiently in a latent space. In the autoencoder, an encoder maps data from the input space to the latent space and a decoder is used to reconstruct the input data from the encoded latent data. The variational autoencoder (VAE) is a class of autoencoder where the encoder module is used to learn the parameter of a distribution and the decoder is used to generate examples from samples drawn from the learned distribution. The VAE does not allow us to constrain the generated sample to have a particular characteristic, but one may want to draw samples with the desired feature. A question then arises on how to endow the model to create targeted samples rather than completely random ones. The conditional variational autoencoder (CAVE) is designed to generate desired samples by including additional conditioning information.

Language models learn the syntactic order of a language from a given set of examples as probability values to predict a sequence of words. A language model is an indispensable ingredient in many advanced natural language processing (NLP) tasks such as text summarization, machine translation, and language generation. The language modeling is a typical self-supervised objective, for it does not require any labels. Language models developed for NLP tasks have evolved from statistical language models to neural language models. Approaches such as n-gram and hidden Markov models have become the primary ingredient in developing statistical language models. In contrast, various deep learning neural networks build neural language models. In the autoregressive (AR) language modeling, models are tasked with generating subsequent tokens based on previously generated tokens. Thus the autoregressive generative model predicts the next amino acid in a protein given the amino acid sequence up to that point. The autoregressive model generates proteins one amino acid at a time. For one step of generation, it takes a context sequence of amino acids as input and outputs a probability distribution over amino acids. We sample from that distribution and then update the context sequence with the sampled amino acid. This process repeats until a protein of desired length has been generated.

The Transformer architecture demonstrates impressive text generation capabilities. This perspective is adapted to protein engineering by training the autoregressive language model with the Transformer encoder on amino acid sequences. Compared to conventional sequential models such as RNN, the Transformer is a new architecture that is more effective in modeling long-term dependence in temporal sequences, where all tokens will be equally considered during the attention operation. And it is more efficient in training while eliminating sequential dependencies from previous tokens. The Transformer models can generally overcome the inherent limitations of classic neural network architectures. For example, these models overcome the problem of speed inherent in RNN, LSTM, or GRU, which require sequential operations that are slow in nature. They can also overcome the long-term dependencies problem of CNN, which can never accurately handle long-range dependences in the text corpus. Unlike RNN, the Transformer cannot consider the order of the input data, by using not only tokens but also positions for embedding. Since the Transformer has a very powerful ability to model sequential data, it becomes the most popular backbone of NLP applications. The Transformer architecture is a nonrecurrent architecture with a series of attention-based blocks. Each block is composed of a multi-head attention layer and a position-wise feedforward layer with an add and normalize layer in between. These layers process input sequences simultaneously, in parallel, independently of sequential order.



2 Example

2.1 GAN

The sequences of the PTEN are used to train the GAN. The input sequences must be aligned to have the same length. To train the model, we can use the function “fit_GAN” with aligned sequence data. To generate sequences, we can use the function “gen_GAN” with the trained model. It is expected that the model can rapidly generate highly diverse novel functional proteins within the allowed biological constraints of the sequence space. Note that the same dataset is used for training and validation here.

if (keras::is_keras_available() & reticulate::py_available()) {
    library(GenProSeq)
    data("example_PTEN")
    
    # model parameters
    length_seq <- 403
    embedding_dim <- 8
    latent_dim <- 4
    epochs <- 20
    batch_size <- 64
    
    # GAN
    GAN_result <- fit_GAN(prot_seq = example_PTEN,
                        length_seq = length_seq,
                        embedding_dim = embedding_dim,
                        latent_dim = latent_dim,
                        intermediate_generator_layers = list(
                            layer_dense(units = 16),
                            layer_dense(units = 128)),
                        intermediate_discriminator_layers = list(
                            layer_dense(units = 128, activation = "relu"),
                            layer_dense(units = 16, activation = "relu")),
                        prot_seq_val = example_PTEN,
                        epochs = epochs,
                        batch_size = batch_size)
}
## Loading required package: keras
## Loading required package: mclust
## Package 'mclust' version 6.0.0
## Type 'citation("mclust")' for citing this R package in publications.
## pre-processing...
## at least one of protein sequences may not be valid
## at least one of protein sequences may not be valid
## training...
## Epoch 1/20 
## 2/2 - 0s - 256ms/epoch - 128ms/step
## 
1/5 [=====>........................] - ETA: 8s2/2 - 0s - 20ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 21ms/epoch - 10ms/step
## 
5/5 [==============================] - 3s 127ms/step
## 11/11 - 0s - 31ms/epoch - 3ms/step
## generator (train) : loss 0.709002 
## generator (test) : loss 0.554978 
## discriminator (train) : loss 0.484507 
## discriminator (test) : loss 0.431227 
## 
## Epoch 2/20 
## 2/2 - 0s - 20ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 21ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
5/5 [==============================] - 1s 125ms/step
## 11/11 - 0s - 30ms/epoch - 3ms/step
## generator (train) : loss 0.592400 
## generator (test) : loss 0.504269 
## discriminator (train) : loss 0.456389 
## discriminator (test) : loss 0.467295 
## 
## Epoch 3/20 
## 2/2 - 0s - 20ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
5/5 [==============================] - 1s 122ms/step
## 11/11 - 0s - 30ms/epoch - 3ms/step
## generator (train) : loss 0.605627 
## generator (test) : loss 0.586293 
## discriminator (train) : loss 0.455971 
## discriminator (test) : loss 0.414072 
## 
## Epoch 4/20 
## 2/2 - 0s - 20ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 1s2/2 - 0s - 20ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 19ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
5/5 [==============================] - 1s 119ms/step
## 11/11 - 0s - 28ms/epoch - 3ms/step
## generator (train) : loss 0.792284 
## generator (test) : loss 0.782275 
## discriminator (train) : loss 0.378251 
## discriminator (test) : loss 0.324398 
## 
## Epoch 5/20 
## 2/2 - 0s - 21ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 21ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
5/5 [==============================] - 1s 123ms/step
## 11/11 - 0s - 30ms/epoch - 3ms/step
## generator (train) : loss 1.126501 
## generator (test) : loss 1.002818 
## discriminator (train) : loss 0.287614 
## discriminator (test) : loss 0.253647 
## 
## Epoch 6/20 
## 2/2 - 0s - 20ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 21ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
5/5 [==============================] - 1s 172ms/step
## 11/11 - 0s - 27ms/epoch - 2ms/step
## generator (train) : loss 1.327715 
## generator (test) : loss 1.033265 
## discriminator (train) : loss 0.243877 
## discriminator (test) : loss 0.241969 
## 
## Epoch 7/20 
## 2/2 - 0s - 20ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
5/5 [==============================] - 1s 180ms/step
## 11/11 - 0s - 30ms/epoch - 3ms/step
## generator (train) : loss 1.282688 
## generator (test) : loss 1.059794 
## discriminator (train) : loss 0.249522 
## discriminator (test) : loss 0.232484 
## 
## Epoch 8/20 
## 2/2 - 0s - 19ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 21ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 21ms/epoch - 11ms/step
## 
5/5 [==============================] - 1s 130ms/step
## 11/11 - 0s - 30ms/epoch - 3ms/step
## generator (train) : loss 1.508987 
## generator (test) : loss 1.576584 
## discriminator (train) : loss 0.198947 
## discriminator (test) : loss 0.148245 
## 
## Epoch 9/20 
## 2/2 - 0s - 21ms/epoch - 11ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 19ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
5/5 [==============================] - 1s 124ms/step
## 11/11 - 0s - 31ms/epoch - 3ms/step
## generator (train) : loss 1.944254 
## generator (test) : loss 1.620216 
## discriminator (train) : loss 0.135953 
## discriminator (test) : loss 0.132498 
## 
## Epoch 10/20 
## 2/2 - 0s - 20ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
5/5 [==============================] - 1s 176ms/step
## 11/11 - 0s - 32ms/epoch - 3ms/step
## generator (train) : loss 1.914042 
## generator (test) : loss 1.592085 
## discriminator (train) : loss 0.131069 
## discriminator (test) : loss 0.123918 
## 
## Epoch 11/20 
## 2/2 - 0s - 20ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 21ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 21ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 21ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
5/5 [==============================] - 1s 124ms/step
## 11/11 - 0s - 31ms/epoch - 3ms/step
## generator (train) : loss 2.154202 
## generator (test) : loss 1.856568 
## discriminator (train) : loss 0.109742 
## discriminator (test) : loss 0.090264 
## 
## Epoch 12/20 
## 2/2 - 0s - 20ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 19ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
5/5 [==============================] - 1s 169ms/step
## 11/11 - 0s - 30ms/epoch - 3ms/step
## generator (train) : loss 2.632561 
## generator (test) : loss 2.065026 
## discriminator (train) : loss 0.080945 
## discriminator (test) : loss 0.072356 
## 
## Epoch 13/20 
## 2/2 - 0s - 19ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 19ms/epoch - 9ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 19ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 19ms/epoch - 9ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 18ms/epoch - 9ms/step
## 
5/5 [==============================] - 1s 114ms/step
## 11/11 - 0s - 27ms/epoch - 2ms/step
## generator (train) : loss 2.874437 
## generator (test) : loss 1.906221 
## discriminator (train) : loss 0.078992 
## discriminator (test) : loss 0.088113 
## 
## Epoch 14/20 
## 2/2 - 0s - 19ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 21ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 19ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 19ms/epoch - 9ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 18ms/epoch - 9ms/step
## 
5/5 [==============================] - 1s 116ms/step
## 11/11 - 0s - 27ms/epoch - 2ms/step
## generator (train) : loss 3.030869 
## generator (test) : loss 2.276859 
## discriminator (train) : loss 0.082521 
## discriminator (test) : loss 0.059191 
## 
## Epoch 15/20 
## 2/2 - 0s - 19ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 19ms/epoch - 9ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 19ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 18ms/epoch - 9ms/step
## 
5/5 [==============================] - 1s 116ms/step
## 11/11 - 0s - 27ms/epoch - 2ms/step
## generator (train) : loss 3.776321 
## generator (test) : loss 2.967081 
## discriminator (train) : loss 0.043635 
## discriminator (test) : loss 0.031517 
## 
## Epoch 16/20 
## 2/2 - 0s - 20ms/epoch - 10ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 19ms/epoch - 9ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 19ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 21ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 18ms/epoch - 9ms/step
## 
5/5 [==============================] - 1s 119ms/step
## 11/11 - 0s - 28ms/epoch - 3ms/step
## generator (train) : loss 4.018580 
## generator (test) : loss 3.051940 
## discriminator (train) : loss 0.030774 
## discriminator (test) : loss 0.032595 
## 
## Epoch 17/20 
## 2/2 - 0s - 18ms/epoch - 9ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 18ms/epoch - 9ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 18ms/epoch - 9ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 19ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
5/5 [==============================] - 1s 115ms/step
## 11/11 - 0s - 34ms/epoch - 3ms/step
## generator (train) : loss 3.746773 
## generator (test) : loss 2.406273 
## discriminator (train) : loss 0.042733 
## discriminator (test) : loss 0.055307 
## 
## Epoch 18/20 
## 2/2 - 0s - 19ms/epoch - 9ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 18ms/epoch - 9ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 19ms/epoch - 9ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 19ms/epoch - 9ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 19ms/epoch - 9ms/step
## 
5/5 [==============================] - 1s 116ms/step
## 11/11 - 0s - 30ms/epoch - 3ms/step
## generator (train) : loss 3.061614 
## generator (test) : loss 1.907181 
## discriminator (train) : loss 0.068392 
## discriminator (test) : loss 0.091598 
## 
## Epoch 19/20 
## 2/2 - 0s - 19ms/epoch - 9ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 19ms/epoch - 10ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 19ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 19ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 19ms/epoch - 10ms/step
## 
5/5 [==============================] - 1s 171ms/step
## 11/11 - 0s - 29ms/epoch - 3ms/step
## generator (train) : loss 2.524130 
## generator (test) : loss 1.936110 
## discriminator (train) : loss 0.125951 
## discriminator (test) : loss 0.123317 
## 
## Epoch 20/20 
## 2/2 - 0s - 19ms/epoch - 9ms/step
## 
1/5 [=====>........................] - ETA: 0s2/2 - 0s - 19ms/epoch - 9ms/step
## 
2/5 [===========>..................] - ETA: 0s2/2 - 0s - 19ms/epoch - 10ms/step
## 
3/5 [=================>............] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
4/5 [=======================>......] - ETA: 0s2/2 - 0s - 20ms/epoch - 10ms/step
## 
5/5 [==============================] - 1s 119ms/step
## 11/11 - 0s - 29ms/epoch - 3ms/step
## generator (train) : loss 3.285272 
## generator (test) : loss 3.976129 
## discriminator (train) : loss 0.061186 
## discriminator (test) : loss 0.012534

The model architecture of the generator is shown below.

if (keras::is_keras_available() & reticulate::py_available()) {
    ttgsea::plot_model(GAN_result$generator)
}

The model architecture of the discriminator is shown below.

if (keras::is_keras_available() & reticulate::py_available()) {
    ttgsea::plot_model(GAN_result$discriminator)
}

In a graphical manner, a sequence logo shows the probability of occurrence for each symbol at specific positions. Thus, the height of a symbol indicates the relative frequency of the symbol at that position. The sequence logo of the first 20 amino acids of the generated protein sequences is shown below.

if (keras::is_keras_available() & reticulate::py_available()) {
    set.seed(1)
    gen_prot_GAN <- gen_GAN(GAN_result, num_seq = 100)
    ggseqlogo::ggseqlogo(substr(gen_prot_GAN$gen_seq, 1, 20))
}
## generating...
## 4/4 - 0s - 63ms/epoch - 16ms/step
## post-processing...

The sequence logo of the first 20 amino acids of the real protein sequences is shown below. Here, it is assumed that the conserved position is the first amino acid. The sequence logo is a visualization approach to graphically represent the sequence conservation.

if (keras::is_keras_available() & reticulate::py_available()) {
    ggseqlogo::ggseqlogo(substr(example_PTEN, 1, 20))
}



2.2 VAE

Consider the aligned sequences of the luxA for traing the CVAE with labels. Suppose that the label is the third amino acid of each sequence. Thus there are two class labels. Using the function “fit_VAE”, we build an encoder model that takes a protein sequence and projects it on the latent space and a decoder model that goes from the latent space back to the amino acid representation. And then, the function “gen_VAE” generates sequences with the desired labels. Note that the same dataset is used for training and validation.

if (keras::is_keras_available() & reticulate::py_available()) {
    library(GenProSeq)
    data("example_luxA")
    label <- substr(example_luxA, 3, 3)
    
    # model parameters
    length_seq <- 360
    embedding_dim <- 8
    batch_size <- 128
    epochs <- 20
    
    # CVAE
    VAE_result <- fit_VAE(prot_seq = example_luxA,
                        label = label,
                        length_seq = length_seq,
                        embedding_dim = embedding_dim,
                        embedding_args = list(iter = 20),
                        intermediate_encoder_layers = list(layer_dense(units = 128),
                                                            layer_dense(units = 16)),
                        intermediate_decoder_layers = list(layer_dense(units = 16),
                                                            layer_dense(units = 128)),
                        prot_seq_val = example_luxA,
                        label_val = label,
                        epochs = epochs,
                        batch_size = batch_size,
                        use_generator = FALSE,
                        optimizer = keras::optimizer_adam(clipnorm = 0.1),
                        callbacks = keras::callback_early_stopping(
                            monitor = "val_loss",
                            patience = 10,
                            restore_best_weights = TRUE))
}
## pre-processing...
## training...
## In training : 'Adam' object has no attribute 'get_updates'

The plot for model architecture of the CVAE is drawn below.

if (keras::is_keras_available() & reticulate::py_available()) {
    VAExprs::plot_vae(VAE_result$model)
}

The sequence logo of the first 20 amino acids of the generated protein sequences with the label “I” is shown below.

if (keras::is_keras_available() & reticulate::py_available()) {
    set.seed(1)
    gen_prot_VAE <- gen_VAE(VAE_result, label = rep("I", 100), num_seq = 100)
    ggseqlogo::ggseqlogo(substr(gen_prot_VAE$gen_seq, 1, 20))
}
## generating...
## post-processing...

The sequence logo of the first 20 amino acids of the generated protein sequences with the label “L” is shown below.

if (keras::is_keras_available() & reticulate::py_available()) {
    gen_prot_VAE <- gen_VAE(VAE_result, label = rep("L", 100), num_seq = 100)
    ggseqlogo::ggseqlogo(substr(gen_prot_VAE$gen_seq, 1, 20))
}
## generating...
## post-processing...

The sequence logo of the first 20 amino acids of the real protein sequences is shown below.

if (keras::is_keras_available() & reticulate::py_available()) {
    ggseqlogo::ggseqlogo(substr(example_luxA, 1, 20))
}



2.3 AR with Transformer

The SARS coronavirus 3C-like Protease is used to train the autoregressive language model with the Transformer. The same dataset is used for training and validation.

if (keras::is_keras_available() & reticulate::py_available()) {
    library(GenProSeq)
    prot_seq <- DeepPINCS::SARS_CoV2_3CL_Protease
    
    # model parameters
    length_seq <- 10
    embedding_dim <- 16
    num_heads <- 2
    ff_dim <- 16
    num_transformer_blocks <- 2
    batch_size <- 32
    epochs <- 100
    
    # ART
    ART_result <- fit_ART(prot_seq = prot_seq,
                        length_seq = length_seq,
                        embedding_dim = embedding_dim,
                        num_heads = num_heads,
                        ff_dim = ff_dim,
                        num_transformer_blocks = num_transformer_blocks,
                        layers = list(layer_dropout(rate = 0.1),
                                    layer_dense(units = 32, activation = "relu"),
                                    layer_dropout(rate = 0.1)),
                        prot_seq_val = prot_seq,
                        epochs = epochs,
                        batch_size = batch_size,
                        use_generator = FALSE,
                        callbacks = callback_early_stopping(
                            monitor = "val_loss",
                            patience = 50,
                            restore_best_weights = TRUE))
}
## pre-processing...
## training...
## Train on 296 samples, validate on 296 samples
## Epoch 1/100
## 296/296 - 1s - loss: 3.0573 - accuracy: 0.0473 - val_loss: 2.9942 - val_accuracy: 0.0709 - 1s/epoch - 4ms/sample
## Epoch 2/100
## 296/296 - 0s - loss: 2.9980 - accuracy: 0.0541 - val_loss: 2.9585 - val_accuracy: 0.0811 - 193ms/epoch - 653us/sample
## Epoch 3/100
## 296/296 - 0s - loss: 2.9868 - accuracy: 0.0574 - val_loss: 2.9349 - val_accuracy: 0.0980 - 153ms/epoch - 517us/sample
## Epoch 4/100
## 296/296 - 0s - loss: 2.9399 - accuracy: 0.0777 - val_loss: 2.9040 - val_accuracy: 0.1419 - 153ms/epoch - 517us/sample
## Epoch 5/100
## 296/296 - 0s - loss: 2.9294 - accuracy: 0.1182 - val_loss: 2.8873 - val_accuracy: 0.1014 - 154ms/epoch - 519us/sample
## Epoch 6/100
## 296/296 - 0s - loss: 2.9121 - accuracy: 0.1115 - val_loss: 2.8626 - val_accuracy: 0.1182 - 161ms/epoch - 544us/sample
## Epoch 7/100
## 296/296 - 0s - loss: 2.8866 - accuracy: 0.1014 - val_loss: 2.8452 - val_accuracy: 0.1554 - 154ms/epoch - 519us/sample
## Epoch 8/100
## 296/296 - 0s - loss: 2.8503 - accuracy: 0.1047 - val_loss: 2.8183 - val_accuracy: 0.1385 - 156ms/epoch - 528us/sample
## Epoch 9/100
## 296/296 - 0s - loss: 2.8575 - accuracy: 0.1047 - val_loss: 2.7979 - val_accuracy: 0.1318 - 159ms/epoch - 538us/sample
## Epoch 10/100
## 296/296 - 0s - loss: 2.8547 - accuracy: 0.1216 - val_loss: 2.7832 - val_accuracy: 0.1588 - 158ms/epoch - 535us/sample
## Epoch 11/100
## 296/296 - 0s - loss: 2.8283 - accuracy: 0.1250 - val_loss: 2.7809 - val_accuracy: 0.1486 - 168ms/epoch - 567us/sample
## Epoch 12/100
## 296/296 - 0s - loss: 2.8020 - accuracy: 0.1858 - val_loss: 2.7477 - val_accuracy: 0.1689 - 174ms/epoch - 586us/sample
## Epoch 13/100
## 296/296 - 0s - loss: 2.7858 - accuracy: 0.1486 - val_loss: 2.7276 - val_accuracy: 0.1993 - 168ms/epoch - 567us/sample
## Epoch 14/100
## 296/296 - 0s - loss: 2.7703 - accuracy: 0.1520 - val_loss: 2.7096 - val_accuracy: 0.1926 - 164ms/epoch - 554us/sample
## Epoch 15/100
## 296/296 - 0s - loss: 2.7613 - accuracy: 0.1622 - val_loss: 2.6933 - val_accuracy: 0.1993 - 150ms/epoch - 506us/sample
## Epoch 16/100
## 296/296 - 0s - loss: 2.7420 - accuracy: 0.1588 - val_loss: 2.6772 - val_accuracy: 0.1824 - 160ms/epoch - 542us/sample
## Epoch 17/100
## 296/296 - 0s - loss: 2.7326 - accuracy: 0.1486 - val_loss: 2.6648 - val_accuracy: 0.2264 - 149ms/epoch - 504us/sample
## Epoch 18/100
## 296/296 - 0s - loss: 2.7324 - accuracy: 0.1655 - val_loss: 2.6340 - val_accuracy: 0.2128 - 157ms/epoch - 530us/sample
## Epoch 19/100
## 296/296 - 0s - loss: 2.7017 - accuracy: 0.1622 - val_loss: 2.6179 - val_accuracy: 0.2095 - 153ms/epoch - 518us/sample
## Epoch 20/100
## 296/296 - 0s - loss: 2.6742 - accuracy: 0.1723 - val_loss: 2.6088 - val_accuracy: 0.2027 - 128ms/epoch - 433us/sample
## Epoch 21/100
## 296/296 - 0s - loss: 2.6397 - accuracy: 0.2196 - val_loss: 2.5911 - val_accuracy: 0.2500 - 143ms/epoch - 482us/sample
## Epoch 22/100
## 296/296 - 0s - loss: 2.6346 - accuracy: 0.1791 - val_loss: 2.5672 - val_accuracy: 0.1993 - 159ms/epoch - 538us/sample
## Epoch 23/100
## 296/296 - 0s - loss: 2.6349 - accuracy: 0.1689 - val_loss: 2.5392 - val_accuracy: 0.2432 - 134ms/epoch - 454us/sample
## Epoch 24/100
## 296/296 - 0s - loss: 2.6304 - accuracy: 0.1791 - val_loss: 2.5367 - val_accuracy: 0.2432 - 152ms/epoch - 514us/sample
## Epoch 25/100
## 296/296 - 0s - loss: 2.6099 - accuracy: 0.2128 - val_loss: 2.5067 - val_accuracy: 0.2635 - 154ms/epoch - 520us/sample
## Epoch 26/100
## 296/296 - 0s - loss: 2.6009 - accuracy: 0.1926 - val_loss: 2.4804 - val_accuracy: 0.2264 - 138ms/epoch - 465us/sample
## Epoch 27/100
## 296/296 - 0s - loss: 2.5396 - accuracy: 0.2061 - val_loss: 2.4639 - val_accuracy: 0.2703 - 146ms/epoch - 493us/sample
## Epoch 28/100
## 296/296 - 0s - loss: 2.5645 - accuracy: 0.2432 - val_loss: 2.4611 - val_accuracy: 0.2297 - 157ms/epoch - 531us/sample
## Epoch 29/100
## 296/296 - 0s - loss: 2.5494 - accuracy: 0.1926 - val_loss: 2.4382 - val_accuracy: 0.2432 - 160ms/epoch - 541us/sample
## Epoch 30/100
## 296/296 - 0s - loss: 2.5358 - accuracy: 0.2128 - val_loss: 2.4132 - val_accuracy: 0.2466 - 157ms/epoch - 529us/sample
## Epoch 31/100
## 296/296 - 0s - loss: 2.5176 - accuracy: 0.2027 - val_loss: 2.4277 - val_accuracy: 0.2432 - 150ms/epoch - 507us/sample
## Epoch 32/100
## 296/296 - 0s - loss: 2.4934 - accuracy: 0.2297 - val_loss: 2.3990 - val_accuracy: 0.2601 - 159ms/epoch - 536us/sample
## Epoch 33/100
## 296/296 - 0s - loss: 2.4570 - accuracy: 0.2703 - val_loss: 2.3451 - val_accuracy: 0.2838 - 152ms/epoch - 514us/sample
## Epoch 34/100
## 296/296 - 0s - loss: 2.4434 - accuracy: 0.2432 - val_loss: 2.3333 - val_accuracy: 0.2770 - 154ms/epoch - 520us/sample
## Epoch 35/100
## 296/296 - 0s - loss: 2.3934 - accuracy: 0.2669 - val_loss: 2.3452 - val_accuracy: 0.2500 - 152ms/epoch - 515us/sample
## Epoch 36/100
## 296/296 - 0s - loss: 2.4045 - accuracy: 0.2534 - val_loss: 2.3103 - val_accuracy: 0.3311 - 162ms/epoch - 547us/sample
## Epoch 37/100
## 296/296 - 0s - loss: 2.4161 - accuracy: 0.2466 - val_loss: 2.3139 - val_accuracy: 0.2939 - 158ms/epoch - 535us/sample
## Epoch 38/100
## 296/296 - 0s - loss: 2.4122 - accuracy: 0.2534 - val_loss: 2.2542 - val_accuracy: 0.3176 - 168ms/epoch - 569us/sample
## Epoch 39/100
## 296/296 - 0s - loss: 2.3879 - accuracy: 0.2838 - val_loss: 2.2072 - val_accuracy: 0.3378 - 162ms/epoch - 548us/sample
## Epoch 40/100
## 296/296 - 0s - loss: 2.3299 - accuracy: 0.2703 - val_loss: 2.1797 - val_accuracy: 0.3074 - 163ms/epoch - 552us/sample
## Epoch 41/100
## 296/296 - 0s - loss: 2.2635 - accuracy: 0.2872 - val_loss: 2.1633 - val_accuracy: 0.3480 - 156ms/epoch - 528us/sample
## Epoch 42/100
## 296/296 - 0s - loss: 2.3073 - accuracy: 0.2872 - val_loss: 2.1341 - val_accuracy: 0.3581 - 144ms/epoch - 487us/sample
## Epoch 43/100
## 296/296 - 0s - loss: 2.2874 - accuracy: 0.2736 - val_loss: 2.1452 - val_accuracy: 0.3581 - 159ms/epoch - 536us/sample
## Epoch 44/100
## 296/296 - 0s - loss: 2.3396 - accuracy: 0.2770 - val_loss: 2.1620 - val_accuracy: 0.3108 - 162ms/epoch - 546us/sample
## Epoch 45/100
## 296/296 - 0s - loss: 2.2440 - accuracy: 0.2905 - val_loss: 2.0760 - val_accuracy: 0.4054 - 161ms/epoch - 543us/sample
## Epoch 46/100
## 296/296 - 0s - loss: 2.2718 - accuracy: 0.2736 - val_loss: 2.0724 - val_accuracy: 0.4122 - 159ms/epoch - 537us/sample
## Epoch 47/100
## 296/296 - 0s - loss: 2.2247 - accuracy: 0.3514 - val_loss: 2.0138 - val_accuracy: 0.4054 - 158ms/epoch - 534us/sample
## Epoch 48/100
## 296/296 - 0s - loss: 2.1891 - accuracy: 0.3041 - val_loss: 1.9961 - val_accuracy: 0.3716 - 150ms/epoch - 506us/sample
## Epoch 49/100
## 296/296 - 0s - loss: 2.1570 - accuracy: 0.3007 - val_loss: 2.0396 - val_accuracy: 0.4054 - 146ms/epoch - 495us/sample
## Epoch 50/100
## 296/296 - 0s - loss: 2.2238 - accuracy: 0.3480 - val_loss: 1.9988 - val_accuracy: 0.3514 - 154ms/epoch - 520us/sample
## Epoch 51/100
## 296/296 - 0s - loss: 2.1144 - accuracy: 0.3547 - val_loss: 1.9779 - val_accuracy: 0.3818 - 162ms/epoch - 547us/sample
## Epoch 52/100
## 296/296 - 0s - loss: 2.1082 - accuracy: 0.3547 - val_loss: 1.9015 - val_accuracy: 0.4223 - 162ms/epoch - 547us/sample
## Epoch 53/100
## 296/296 - 0s - loss: 2.0751 - accuracy: 0.3581 - val_loss: 1.8652 - val_accuracy: 0.4426 - 144ms/epoch - 485us/sample
## Epoch 54/100
## 296/296 - 0s - loss: 2.0348 - accuracy: 0.3446 - val_loss: 1.8429 - val_accuracy: 0.4899 - 148ms/epoch - 501us/sample
## Epoch 55/100
## 296/296 - 0s - loss: 2.0818 - accuracy: 0.3243 - val_loss: 1.8936 - val_accuracy: 0.4155 - 155ms/epoch - 523us/sample
## Epoch 56/100
## 296/296 - 0s - loss: 2.0695 - accuracy: 0.3581 - val_loss: 1.8473 - val_accuracy: 0.4426 - 159ms/epoch - 536us/sample
## Epoch 57/100
## 296/296 - 0s - loss: 2.0551 - accuracy: 0.3480 - val_loss: 1.8278 - val_accuracy: 0.4493 - 169ms/epoch - 570us/sample
## Epoch 58/100
## 296/296 - 0s - loss: 2.0667 - accuracy: 0.3311 - val_loss: 1.7983 - val_accuracy: 0.4493 - 120ms/epoch - 404us/sample
## Epoch 59/100
## 296/296 - 0s - loss: 2.0162 - accuracy: 0.3412 - val_loss: 1.7676 - val_accuracy: 0.4865 - 161ms/epoch - 542us/sample
## Epoch 60/100
## 296/296 - 0s - loss: 1.9479 - accuracy: 0.4020 - val_loss: 1.7185 - val_accuracy: 0.5000 - 166ms/epoch - 562us/sample
## Epoch 61/100
## 296/296 - 0s - loss: 1.9329 - accuracy: 0.3818 - val_loss: 1.7256 - val_accuracy: 0.5169 - 161ms/epoch - 543us/sample
## Epoch 62/100
## 296/296 - 0s - loss: 1.9239 - accuracy: 0.4088 - val_loss: 1.6985 - val_accuracy: 0.5270 - 165ms/epoch - 559us/sample
## Epoch 63/100
## 296/296 - 0s - loss: 1.9259 - accuracy: 0.3547 - val_loss: 1.6561 - val_accuracy: 0.5169 - 169ms/epoch - 571us/sample
## Epoch 64/100
## 296/296 - 0s - loss: 1.8625 - accuracy: 0.3919 - val_loss: 1.6196 - val_accuracy: 0.5507 - 168ms/epoch - 568us/sample
## Epoch 65/100
## 296/296 - 0s - loss: 1.8650 - accuracy: 0.3919 - val_loss: 1.6405 - val_accuracy: 0.5270 - 157ms/epoch - 530us/sample
## Epoch 66/100
## 296/296 - 0s - loss: 1.8986 - accuracy: 0.3919 - val_loss: 1.6090 - val_accuracy: 0.5338 - 163ms/epoch - 549us/sample
## Epoch 67/100
## 296/296 - 0s - loss: 1.8650 - accuracy: 0.4189 - val_loss: 1.5639 - val_accuracy: 0.5642 - 160ms/epoch - 542us/sample
## Epoch 68/100
## 296/296 - 0s - loss: 1.7965 - accuracy: 0.4324 - val_loss: 1.5449 - val_accuracy: 0.5642 - 162ms/epoch - 547us/sample
## Epoch 69/100
## 296/296 - 0s - loss: 1.8197 - accuracy: 0.4223 - val_loss: 1.5437 - val_accuracy: 0.5372 - 152ms/epoch - 513us/sample
## Epoch 70/100
## 296/296 - 0s - loss: 1.8174 - accuracy: 0.4257 - val_loss: 1.5161 - val_accuracy: 0.5405 - 136ms/epoch - 458us/sample
## Epoch 71/100
## 296/296 - 0s - loss: 1.7598 - accuracy: 0.4426 - val_loss: 1.5189 - val_accuracy: 0.5541 - 124ms/epoch - 418us/sample
## Epoch 72/100
## 296/296 - 0s - loss: 1.7473 - accuracy: 0.4291 - val_loss: 1.4750 - val_accuracy: 0.5912 - 131ms/epoch - 443us/sample
## Epoch 73/100
## 296/296 - 0s - loss: 1.7815 - accuracy: 0.4223 - val_loss: 1.4751 - val_accuracy: 0.5709 - 154ms/epoch - 519us/sample
## Epoch 74/100
## 296/296 - 0s - loss: 1.7845 - accuracy: 0.4324 - val_loss: 1.4603 - val_accuracy: 0.5777 - 166ms/epoch - 559us/sample
## Epoch 75/100
## 296/296 - 0s - loss: 1.7321 - accuracy: 0.4358 - val_loss: 1.4248 - val_accuracy: 0.5912 - 164ms/epoch - 555us/sample
## Epoch 76/100
## 296/296 - 0s - loss: 1.7219 - accuracy: 0.4797 - val_loss: 1.4447 - val_accuracy: 0.5507 - 143ms/epoch - 485us/sample
## Epoch 77/100
## 296/296 - 0s - loss: 1.6758 - accuracy: 0.4527 - val_loss: 1.4114 - val_accuracy: 0.6250 - 161ms/epoch - 544us/sample
## Epoch 78/100
## 296/296 - 0s - loss: 1.7019 - accuracy: 0.4459 - val_loss: 1.3594 - val_accuracy: 0.5912 - 90ms/epoch - 304us/sample
## Epoch 79/100
## 296/296 - 0s - loss: 1.6233 - accuracy: 0.4932 - val_loss: 1.3470 - val_accuracy: 0.6318 - 111ms/epoch - 374us/sample
## Epoch 80/100
## 296/296 - 0s - loss: 1.6458 - accuracy: 0.4764 - val_loss: 1.3602 - val_accuracy: 0.6182 - 83ms/epoch - 281us/sample
## Epoch 81/100
## 296/296 - 0s - loss: 1.6822 - accuracy: 0.4459 - val_loss: 1.3704 - val_accuracy: 0.5878 - 140ms/epoch - 474us/sample
## Epoch 82/100
## 296/296 - 0s - loss: 1.6784 - accuracy: 0.4662 - val_loss: 1.3183 - val_accuracy: 0.6385 - 160ms/epoch - 541us/sample
## Epoch 83/100
## 296/296 - 0s - loss: 1.6189 - accuracy: 0.4730 - val_loss: 1.3050 - val_accuracy: 0.6385 - 125ms/epoch - 421us/sample
## Epoch 84/100
## 296/296 - 0s - loss: 1.5507 - accuracy: 0.5000 - val_loss: 1.2691 - val_accuracy: 0.6453 - 158ms/epoch - 535us/sample
## Epoch 85/100
## 296/296 - 0s - loss: 1.6150 - accuracy: 0.5000 - val_loss: 1.2541 - val_accuracy: 0.6655 - 157ms/epoch - 531us/sample
## Epoch 86/100
## 296/296 - 0s - loss: 1.6064 - accuracy: 0.4628 - val_loss: 1.2933 - val_accuracy: 0.6520 - 118ms/epoch - 398us/sample
## Epoch 87/100
## 296/296 - 0s - loss: 1.5711 - accuracy: 0.5135 - val_loss: 1.2177 - val_accuracy: 0.6858 - 156ms/epoch - 528us/sample
## Epoch 88/100
## 296/296 - 0s - loss: 1.5759 - accuracy: 0.5135 - val_loss: 1.2030 - val_accuracy: 0.6824 - 166ms/epoch - 560us/sample
## Epoch 89/100
## 296/296 - 0s - loss: 1.5379 - accuracy: 0.4899 - val_loss: 1.2087 - val_accuracy: 0.6655 - 167ms/epoch - 566us/sample
## Epoch 90/100
## 296/296 - 0s - loss: 1.5258 - accuracy: 0.4595 - val_loss: 1.1772 - val_accuracy: 0.6689 - 144ms/epoch - 487us/sample
## Epoch 91/100
## 296/296 - 0s - loss: 1.5526 - accuracy: 0.5270 - val_loss: 1.1540 - val_accuracy: 0.6824 - 150ms/epoch - 507us/sample
## Epoch 92/100
## 296/296 - 0s - loss: 1.4874 - accuracy: 0.5304 - val_loss: 1.1410 - val_accuracy: 0.6791 - 155ms/epoch - 525us/sample
## Epoch 93/100
## 296/296 - 0s - loss: 1.5475 - accuracy: 0.5101 - val_loss: 1.1516 - val_accuracy: 0.6824 - 151ms/epoch - 511us/sample
## Epoch 94/100
## 296/296 - 0s - loss: 1.5031 - accuracy: 0.5068 - val_loss: 1.1314 - val_accuracy: 0.6993 - 159ms/epoch - 539us/sample
## Epoch 95/100
## 296/296 - 0s - loss: 1.4996 - accuracy: 0.5338 - val_loss: 1.1125 - val_accuracy: 0.7061 - 140ms/epoch - 473us/sample
## Epoch 96/100
## 296/296 - 0s - loss: 1.4291 - accuracy: 0.5270 - val_loss: 1.1014 - val_accuracy: 0.6689 - 157ms/epoch - 532us/sample
## Epoch 97/100
## 296/296 - 0s - loss: 1.4812 - accuracy: 0.5473 - val_loss: 1.0834 - val_accuracy: 0.6993 - 149ms/epoch - 505us/sample
## Epoch 98/100
## 296/296 - 0s - loss: 1.4703 - accuracy: 0.5169 - val_loss: 1.1166 - val_accuracy: 0.6959 - 146ms/epoch - 493us/sample
## Epoch 99/100
## 296/296 - 0s - loss: 1.4714 - accuracy: 0.5304 - val_loss: 1.1570 - val_accuracy: 0.6791 - 149ms/epoch - 505us/sample
## Epoch 100/100
## 296/296 - 0s - loss: 1.5462 - accuracy: 0.4932 - val_loss: 1.1893 - val_accuracy: 0.6385 - 159ms/epoch - 537us/sample

The model architecture is shown below.

if (keras::is_keras_available() & reticulate::py_available()) {
    ttgsea::plot_model(ART_result$model)
}

The learned autoregressive model generates new sequences of proteins. After the language model generates a conditional probability distribution over vocabulary for the given input sequence, we need to decide how to choose the next word or token from the distribution. The greedy search simply selects the token with the highest probability value as its next token. Rather than just considering the highest probable word, the top b words is taken as the next word in the beam search. Here b is also called the beam size which is itself a parameter. Now to compute the second word each of these b first words is fed and top b words are obtained again. The process continues till the end. In temperature sampling, we sample the token from the modified conditional probability distribution over the vocabulary for the given temperature value. In the top-k sampling, the k most likely next tokens are filtered and their probabilities are adjusted among only those k tokens. In a similar manner, the top-p sampling is another way to exclude very low probability tokens and it finds the smallest set of tokens that have summed probability at least p.

if (keras::is_keras_available() & reticulate::py_available()) {
    set.seed(1)
    seed_prot <- "SGFRKMAFPS"
    print(gen_ART(ART_result, seed_prot, length_AA = 20, method = "greedy"))
    print(substr(prot_seq, 1, 30))
    print(gen_ART(ART_result, seed_prot, length_AA = 20, method = "beam", b = 5))
    print(substr(prot_seq, 1, 30))
    print(gen_ART(ART_result, seed_prot, length_AA = 20, method = "temperature", t = 0.1))
    print(substr(prot_seq, 1, 30))
    print(gen_ART(ART_result, seed_prot, length_AA = 20, method = "top_k", k = 3))
    print(substr(prot_seq, 1, 30))
    print(gen_ART(ART_result, seed_prot, length_AA = 20, method = "top_p", p = 0.75))
    print(substr(prot_seq, 1, 30))
}
## generating...
## [1] "SGFRKMAFPSGVYELNLDCLHVHLNGHDPG"
## [1] "SGFRKMAFPSGKVEGCMVQVTCGTTTLNGL"
## generating...
## [1] "SGFRKMAFPSGVKEGNCRQFTGTTGTNLLN"
## [1] "SGFRKMAFPSGKVEGCMVQVTCGTTTLNGL"
## generating...
## [1] "SGFRKMAFPSGVYMLNLREENPIDVRAPAF"
## [1] "SGFRKMAFPSGKVEGCMVQVTCGTTTLNGL"
## generating...
## [1] "SGFRKMAFPSLLGFVNLDYAHKPGTEMPLI"
## [1] "SGFRKMAFPSGKVEGCMVQVTCGTTTLNGL"
## generating...
## [1] "SGFRKMAFPSGTGMLLGLATLGNLGNFLKN"
## [1] "SGFRKMAFPSGKVEGCMVQVTCGTTTLNGL"

We can compute pairwise similarities between the real and generated protein sequences. The function “stringsim” vary between 0 for strings that are not similar at all, to 1 for strings that are identical. One advantage of the string similarity over the string distance function is that similarities are easier to interpret because they are normalized.

if (keras::is_keras_available() & reticulate::py_available()) {
    print(stringdist::stringsim(gen_ART(ART_result, seed_prot, length_AA = 20, method = "greedy"),
                        substr(prot_seq, 1, 30)))
    print(stringdist::stringsim(gen_ART(ART_result, seed_prot, length_AA = 30, method = "greedy"),
                        substr(prot_seq, 1, 40)))
    print(stringdist::stringsim(gen_ART(ART_result, seed_prot, length_AA = 40, method = "greedy"),
                        substr(prot_seq, 1, 50)))
    print(stringdist::stringsim(gen_ART(ART_result, seed_prot, length_AA = 50, method = "greedy"),
                        substr(prot_seq, 1, 60)))
}
## generating...
## [1] 0.4333333
## generating...
## [1] 0.325
## generating...
## [1] 0.28
## generating...
## [1] 0.2833333



3 Session information

sessionInfo()
## R version 4.3.1 (2023-06-16)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.18-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB              LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: America/New_York
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] GenProSeq_1.6.0 mclust_6.0.0    keras_2.13.0   
## 
## loaded via a namespace (and not attached):
##   [1] RColorBrewer_1.1-3          rstudioapi_0.15.0          
##   [3] jsonlite_1.8.7              magrittr_2.0.3             
##   [5] ggbeeswarm_0.7.2            farver_2.1.1               
##   [7] rmarkdown_2.25              zlibbioc_1.48.0            
##   [9] vctrs_0.6.4                 DelayedMatrixStats_1.24.0  
##  [11] RCurl_1.98-1.12             ttgsea_1.10.0              
##  [13] base64enc_0.1-3             PRROC_1.3.1                
##  [15] koRpus.lang.en_0.1-4        htmltools_0.5.6.1          
##  [17] S4Arrays_1.2.0              BiocNeighbors_1.20.0       
##  [19] SparseArray_1.2.0           sass_0.4.7                 
##  [21] bslib_0.5.1                 htmlwidgets_1.6.2          
##  [23] tokenizers_0.3.0            cachem_1.0.8               
##  [25] whisker_0.4.1               lifecycle_1.0.3            
##  [27] pkgconfig_2.0.3             rsvd_1.0.5                 
##  [29] Matrix_1.6-1.1              R6_2.5.1                   
##  [31] fastmap_1.1.1               GenomeInfoDbData_1.2.11    
##  [33] MatrixGenerics_1.14.0       digest_0.6.33              
##  [35] colorspace_2.1-0            S4Vectors_0.40.0           
##  [37] scater_1.30.0               VAExprs_1.8.0              
##  [39] irlba_2.3.5.1               GenomicRanges_1.54.0       
##  [41] SnowballC_0.7.1             beachmat_2.18.0            
##  [43] labeling_0.4.3              fansi_1.0.5                
##  [45] tfruns_1.5.1                httr_1.4.7                 
##  [47] abind_1.4-5                 compiler_4.3.1             
##  [49] withr_2.5.1                 BiocParallel_1.36.0        
##  [51] sylly_0.1-6                 viridis_0.6.4              
##  [53] koRpus_0.13-8               tensorflow_2.14.0          
##  [55] float_0.3-1                 DelayedArray_0.28.0        
##  [57] CatEncoders_0.1.1           tools_4.3.1                
##  [59] vipor_0.4.5                 beeswarm_0.4.0             
##  [61] word2vec_0.4.0              stopwords_2.3              
##  [63] sylly.en_0.1-3              ggseqlogo_0.1              
##  [65] webchem_1.3.0               glue_1.6.2                 
##  [67] lgr_0.4.4                   DiagrammeR_1.0.10          
##  [69] grid_4.3.1                  stringdist_0.9.10          
##  [71] generics_0.1.3              gtable_0.3.4               
##  [73] data.table_1.14.8           BiocSingular_1.18.0        
##  [75] ScaledMatrix_1.10.0         xml2_1.3.5                 
##  [77] utf8_1.2.4                  XVector_0.42.0             
##  [79] BiocGenerics_0.48.0         ggrepel_0.9.4              
##  [81] pillar_1.9.0                stringr_1.5.0              
##  [83] dplyr_1.1.3                 lattice_0.22-5             
##  [85] tidyselect_1.2.0            SingleCellExperiment_1.24.0
##  [87] tm_0.7-11                   scuttle_1.12.0             
##  [89] knitr_1.44                  gridExtra_2.3              
##  [91] NLP_0.2-1                   IRanges_2.36.0             
##  [93] SummarizedExperiment_1.32.0 textstem_0.1.4             
##  [95] RhpcBLASctl_0.23-42         stats4_4.3.1               
##  [97] xfun_0.40                   Biobase_2.62.0             
##  [99] matrixStats_1.0.0           visNetwork_2.1.2           
## [101] stringi_1.7.12              yaml_2.3.7                 
## [103] rsparse_0.5.1               evaluate_0.22              
## [105] codetools_0.2-19            data.tree_1.0.0            
## [107] tibble_3.2.1                cli_3.6.1                  
## [109] matlab_1.0.4                reticulate_1.34.0          
## [111] munsell_0.5.0               jquerylib_0.1.4            
## [113] Rcpp_1.0.11                 GenomeInfoDb_1.38.0        
## [115] DeepPINCS_1.10.0            mlapi_0.1.1                
## [117] zeallot_0.1.0               png_0.1-8                  
## [119] parallel_4.3.1              ellipsis_0.3.2             
## [121] ggplot2_3.4.4               sparseMatrixStats_1.14.0   
## [123] bitops_1.0-7                viridisLite_0.4.2          
## [125] slam_0.1-50                 text2vec_0.6.3             
## [127] scales_1.2.1                purrr_1.0.2                
## [129] crayon_1.5.2                rlang_1.1.1                
## [131] rvest_1.0.3



4 References

Barbosa, V. A. F., Santana, M. A., Andrade, M. K. S., Lima, R. C. F., & Santos, W. P. (2020). Deep Learning for Data Analytics: Foundations, Biomedical Applications, and Challenges. Academic Press.

Cinelli, L. P., Marins, M. A., da Silva, E. A. B., & Netto, S. L. (2021). Variational Methods for Machine Learning with Applications to Deep Networks. Springer.

Dash, S., Acharya, B. R., Mittal, M., Abraham, A., & Kelemen, A. (Eds.). (2020). Deep learning techniques for biomedical and health informatics. Springer.

Deepak, P., Chakraborty, T., & Long, C. (2021). Data Science for Fake News: Surveys and Perspectives. Springer.

Dong, G., & Pei, J. (2007). Sequence data mining. Springer.

Frazer, J., Notin, P., Dias, M., Gomez, A., Brock, K., Gal, Y., & Marks, D. (2020). Large-scale clinical interpretation of genetic variants using evolutionary data and deep learning. bioRxiv.

Gagniuc, P. A. (2021). Algorithms in Bioinformatics: Theory and Implementation. Wiley & Sons.

Hawkins-Hooker, A., Depardieu, F., Baur, S., Couairon, G., Chen, A., & Bikard, D. (2020). Generating functional protein variants with variational autoencoders. bioRxiv.

Hemanth, J., Bhatia, M., & Geman, O. (2020). Data Visualization and Knowledge Engineering: Spotting Data Points with Artificial Intelligence. Springer.

Lappin, S. (2021). Deep learning and linguistic representation. CRC Press.

Liebowitz, J. (Ed.). (2020). Data Analytics and AI. CRC Press.

Liu, Z., Lin, Y., & Sun, M. (2020). Representation learning for natural language processing. Springer.

Madani, A., McCann, B., Naik, N., Keskar, N. S., Anand, N., Eguchi, R. R., Huang, P., & Socher, R. (2020). Progen: Language modeling for protein generation. arXiv:2004.03497.

Pearson, R. K. (2018). Exploratory data analysis using R. CRC Press.

Pedrycz, W., & Chen, S. M. (Eds.). (2020). Deep Learning: Concepts and Architectures. Springer.

Peter, J. D., Fernandes, S. L., Thomaz, C. E., & Viriri, S. (Eds.). (2019). Computer aided intervention and diagnostics in clinical and medical images. Springer.

Repecka, D., et al. (2019). Expanding functional protein sequence space using generative adversarial networks. bioRxiv.

Suguna, S. K., Dhivya, M., & Paiva, S. (Eds.). (2021). Artificial Intelligence (AI): Recent Trends and Applications. CRC Press.

Sun, S., Mao, L., Dong, Z., & Wu, L. (2019). Multiview machine learning. Springer.

Wolkenhauer, O. (2020). Systems Medicine: Integrative, Qualitative and Computational Approaches. Academic Press.

Wu, Z., Johnston, K. E., Arnold, F. H., & Yang, K. K. (2021). Protein sequence design with deep generative models. arXiv:2104.04457.