Sure! Here's an example code snippet using Keras to build a neural network for predicting transcription factor binding sites (TFBS) in a one-hot encoded DNA sequence of length 101:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Flatten
# Define the length of the DNA sequence and number of classes
seq_length = 101
num_classes = 2
# Create a random example dataset
num_examples = 1000
X = np.random.randint(2, size=(num_examples, seq_length, 4))
y = np.random.randint(num_classes, size=num_examples)
# Create a sequential model
model = Sequential()
model.add(Flatten(input_shape=(seq_length, 4))) # Flatten the input
model.add(Dense(64, activation='relu')) # Add a dense layer with 64 units and ReLU activation
model.add(Dense(num_classes, activation='softmax')) # Add a dense layer with num_classes units and softmax activation
# Compile the model
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Print the model summary
model.summary()
# Train the model
model.fit(X, y, epochs=10, batch_size=32)
In this code, we first import the necessary libraries, including numpy for generating random data, Sequential and Dense from keras.models and keras.layers respectively.
Then, we define the length of the DNA sequence (seq_length) and the number of classes (num_classes), which would represent the TFBS and non-TFBS classes in this case.
Next, we create a random example dataset with num_examples DNA sequences of length seq_length, where each sequence is one-hot encoded into a 3D array X. The corresponding TFBS labels are stored in a 1D array y.
We create a sequential model and add layers to it. The first layer is a Flatten layer, which flattens the input 3D array into a 2D array. Then, we add a Dense layer with 64 units and ReLU activation function. Finally, we add another Dense layer with num_classes units and softmax activation function, which will output the predicted probabilities for each class.
After compiling the model with the appropriate loss function, optimizer, and metrics, we print the summary of the model.
Finally, we train the